1191147Srwatson/*-
2191147Srwatson * Copyright (c) 2008-2009 Apple Inc.
3191147Srwatson * All rights reserved.
4191147Srwatson *
5191147Srwatson * Redistribution and use in source and binary forms, with or without
6191147Srwatson * modification, are permitted provided that the following conditions
7191147Srwatson * are met:
8191147Srwatson * 1.  Redistributions of source code must retain the above copyright
9191147Srwatson *     notice, this list of conditions and the following disclaimer.
10191147Srwatson * 2.  Redistributions in binary form must reproduce the above copyright
11191147Srwatson *     notice, this list of conditions and the following disclaimer in the
12191147Srwatson *     documentation and/or other materials provided with the distribution.
13191147Srwatson * 3.  Neither the name of Apple Inc. ("Apple") nor the names of
14191147Srwatson *     its contributors may be used to endorse or promote products derived
15191147Srwatson *     from this software without specific prior written permission.
16191147Srwatson *
17191147Srwatson * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND
18191147Srwatson * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19191147Srwatson * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20191147Srwatson * ARE DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR
21191147Srwatson * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22191147Srwatson * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23191147Srwatson * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24191147Srwatson * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
25191147Srwatson * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
26191147Srwatson * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27191147Srwatson * POSSIBILITY OF SUCH DAMAGE.
28191147Srwatson */
29191147Srwatson
30191270Srwatson#include <sys/cdefs.h>
31191270Srwatson__FBSDID("$FreeBSD$");
32191270Srwatson
33191147Srwatson#include <sys/param.h>
34191147Srwatson#include <sys/fcntl.h>
35191147Srwatson
36191270Srwatson#include <security/audit/audit.h>
37191147Srwatson
38191147Srwatson#include <bsm/audit_fcntl.h>
39191270Srwatson#include <bsm/audit_record.h>
40191147Srwatson
41191147Srwatsonstruct bsm_fcntl_cmd {
42191147Srwatson	u_short	bfc_bsm_fcntl_cmd;
43191147Srwatson	int	bfc_local_fcntl_cmd;
44191147Srwatson};
45191147Srwatsontypedef struct bsm_fcntl_cmd	bsm_fcntl_cmd_t;
46191147Srwatson
47191147Srwatsonstatic const bsm_fcntl_cmd_t bsm_fcntl_cmdtab[] = {
48191147Srwatson	{ BSM_F_DUPFD, 		F_DUPFD },
49191147Srwatson	{ BSM_F_GETFD,		F_GETFD },
50191147Srwatson	{ BSM_F_SETFD,		F_SETFD	},
51191147Srwatson	{ BSM_F_GETFL,		F_GETFL },
52191147Srwatson	{ BSM_F_SETFL,		F_SETFL },
53191147Srwatson#ifdef	F_O_GETLK
54191147Srwatson	{ BSM_F_O_GETLK,	F_O_GETLK },
55191147Srwatson#endif
56191147Srwatson	{ BSM_F_SETLK,		F_SETLK },
57191147Srwatson	{ BSM_F_SETLKW,		F_SETLK },
58191147Srwatson#ifdef	F_CHFL
59191147Srwatson	{ BSM_F_CHKFL,		F_CHKFL },
60191147Srwatson#endif
61191147Srwatson#ifdef 	F_DUP2FD
62191147Srwatson	{ BSM_F_DUP2FD,		F_DUP2FD },
63191147Srwatson#endif
64191147Srwatson#ifdef	F_ALLOCSP
65191147Srwatson	{ BSM_F_ALLOCSP,	F_ALLOCSP },
66191147Srwatson#endif
67191147Srwatson#ifdef	F_FREESP
68191147Srwatson	{ BSM_F_FREESP,		F_FREESP },
69191147Srwatson#endif
70191147Srwatson#ifdef	F_ISSTREAM
71191147Srwatson	{ BSM_F_ISSTREAM,	F_ISSTREAM},
72191147Srwatson#endif
73191147Srwatson	{ BSM_F_GETLK,		F_GETLK },
74191147Srwatson#ifdef 	F_PRIV
75191147Srwatson	{ BSM_F_PRIV,		F_PRIV },
76191147Srwatson#endif
77191147Srwatson#ifdef	F_NPRIV
78191147Srwatson	{ BSM_F_NPRIV,		F_NPRIV },
79191147Srwatson#endif
80191147Srwatson#ifdef 	F_QUOTACTL
81191147Srwatson	{ BSM_F_QUOTACTL,	F_QUOTACTL },
82191147Srwatson#endif
83191147Srwatson#ifdef	F_BLOCKS
84191147Srwatson	{ BSM_F_BLOCKS,		F_BLOCKS },
85191147Srwatson#endif
86191147Srwatson#ifdef	F_BLKSIZE
87191147Srwatson	{ BSM_F_BLKSIZE,	F_BLKSIZE },
88191147Srwatson#endif
89191147Srwatson	{ BSM_F_GETOWN,		F_GETOWN },
90191147Srwatson	{ BSM_F_SETOWN,		F_SETOWN },
91191147Srwatson#ifdef	F_REVOKE
92191147Srwatson	{ BSM_F_REVOKE,		F_REVOKE },
93191147Srwatson#endif
94191147Srwatson#ifdef 	F_HASREMOTEBLOCKS
95191147Srwatson	{ BSM_F_HASREMOTEBLOCKS,
96191147Srwatson				F_HASREMOTEBLOCKS },
97191147Srwatson#endif
98191147Srwatson#ifdef 	F_FREESP
99191147Srwatson	{ BSM_F_FREESP,		F_FREESP },
100191147Srwatson#endif
101191147Srwatson#ifdef 	F_ALLOCSP
102191147Srwatson	{ BSM_F_ALLOCSP,	F_ALLOCSP },
103191147Srwatson#endif
104191147Srwatson#ifdef	F_FREESP64
105191147Srwatson	{ BSM_F_FREESP64,	F_FREESP64 },
106191147Srwatson#endif
107191147Srwatson#ifdef 	F_ALLOCSP64
108191147Srwatson	{ BSM_F_ALLOCSP64,	F_ALLOCSP64 },
109191147Srwatson#endif
110191147Srwatson#ifdef	F_GETLK64
111191147Srwatson	{ BSM_F_GETLK64, 	F_GETLK64 },
112191147Srwatson#endif
113191147Srwatson#ifdef	F_SETLK64
114191147Srwatson	{ BSM_F_SETLK64, 	F_SETLK64 },
115191147Srwatson#endif
116191147Srwatson#ifdef	F_SETLKW64
117191147Srwatson	{ BSM_F_SETLKW64, 	F_SETLKW64 },
118191147Srwatson#endif
119191147Srwatson#ifdef	F_SHARE
120191147Srwatson	{ BSM_F_SHARE,		F_SHARE },
121191147Srwatson#endif
122191147Srwatson#ifdef	F_UNSHARE
123191147Srwatson	{ BSM_F_UNSHARE,	F_UNSHARE },
124191147Srwatson#endif
125191147Srwatson#ifdef	F_SETLK_NBMAND
126191147Srwatson	{ BSM_F_SETLK_NBMAND,	F_SETLK_NBMAND },
127191147Srwatson#endif
128191147Srwatson#ifdef	F_SHARE_NBMAND
129191147Srwatson	{ BSM_F_SHARE_NBMAND,	F_SHARE_NBMAND },
130191147Srwatson#endif
131191147Srwatson#ifdef	F_SETLK64_NBMAND
132191147Srwatson	{ BSM_F_SETLK64_NBMAND,	F_SETLK64_NBMAND },
133191147Srwatson#endif
134191147Srwatson#ifdef	F_GETXFL
135191147Srwatson	{ BSM_F_GETXFL,		F_GETXFL },
136191147Srwatson#endif
137191147Srwatson#ifdef	F_BADFD
138191147Srwatson	{ BSM_F_BADFD,		F_BADFD },
139191147Srwatson#endif
140191147Srwatson#ifdef	F_OGETLK
141191147Srwatson	{ BSM_F_OGETLK,		F_OGETLK },
142191147Srwatson#endif
143191147Srwatson#ifdef	F_OSETLK
144191147Srwatson	{ BSM_F_OSETLK,		F_OSETLK },
145191147Srwatson#endif
146191147Srwatson#ifdef	F_OSETLKW
147191147Srwatson	{ BSM_F_OSETLKW,	F_OSETLKW },
148191147Srwatson#endif
149191147Srwatson#ifdef	F_SETLK_REMOTE
150191147Srwatson	{ BSM_F_SETLK_REMOTE,	F_SETLK_REMOTE },
151191147Srwatson#endif
152191147Srwatson
153191147Srwatson#ifdef	F_SETSIG
154191147Srwatson	{ BSM_F_SETSIG,		F_SETSIG },
155191147Srwatson#endif
156191147Srwatson#ifdef	F_GETSIG
157191147Srwatson	{ BSM_F_GETSIG,		F_GETSIG },
158191147Srwatson#endif
159191147Srwatson
160191147Srwatson#ifdef	F_CHKCLEAN
161191147Srwatson	{ BSM_F_CHKCLEAN,	F_CHKCLEAN },
162191147Srwatson#endif
163191147Srwatson#ifdef	F_PREALLOCATE
164191147Srwatson	{ BSM_F_PREALLOCATE,	F_PREALLOCATE },
165191147Srwatson#endif
166191147Srwatson#ifdef	F_SETSIZE
167191147Srwatson	{ BSM_F_SETSIZE,	F_SETSIZE },
168191147Srwatson#endif
169191147Srwatson#ifdef	F_RDADVISE
170191147Srwatson	{ BSM_F_RDADVISE,	F_RDADVISE },
171191147Srwatson#endif
172191147Srwatson#ifdef	F_RDAHEAD
173191147Srwatson	{ BSM_F_RDAHEAD,	F_RDAHEAD },
174191147Srwatson#endif
175191147Srwatson#ifdef	F_READBOOTSTRAP
176191147Srwatson	{ BSM_F_READBOOTSTRAP,	F_READBOOTSTRAP },
177191147Srwatson#endif
178191147Srwatson#ifdef	F_WRITEBOOTSTRAP
179191147Srwatson	{ BSM_F_WRITEBOOTSTRAP,	F_WRITEBOOTSTRAP },
180191147Srwatson#endif
181191147Srwatson#ifdef	F_NOCACHE
182191147Srwatson	{ BSM_F_NOCACHE,	F_NOCACHE },
183191147Srwatson#endif
184191147Srwatson#ifdef	F_LOG2PHYS
185191147Srwatson	{ BSM_F_LOG2PHYS,	F_LOG2PHYS },
186191147Srwatson#endif
187191147Srwatson#ifdef	F_GETPATH
188191147Srwatson	{ BSM_F_GETPATH,	F_GETPATH },
189191147Srwatson#endif
190191147Srwatson#ifdef	F_FULLFSYNC
191191147Srwatson	{ BSM_F_FULLFSYNC,	F_FULLFSYNC },
192191147Srwatson#endif
193191147Srwatson#ifdef	F_PATHPKG_CHECK
194191147Srwatson	{ BSM_F_PATHPKG_CHECK,	F_PATHPKG_CHECK },
195191147Srwatson#endif
196191147Srwatson#ifdef	F_FREEZE_FS
197191147Srwatson	{ BSM_F_FREEZE_FS,	F_FREEZE_FS },
198191147Srwatson#endif
199191147Srwatson#ifdef	F_THAW_FS
200191147Srwatson	{ BSM_F_THAW_FS,	F_THAW_FS },
201191147Srwatson#endif
202191147Srwatson#ifdef	F_GLOBAL_NOCACHE
203191147Srwatson	{ BSM_F_GLOBAL_NOCACHE,	F_GLOBAL_NOCACHE },
204191147Srwatson#endif
205191147Srwatson#ifdef	F_OPENFROM
206191147Srwatson	{ BSM_F_OPENFROM,	F_OPENFROM },
207191147Srwatson#endif
208191147Srwatson#ifdef	F_UNLINKFROM
209191147Srwatson	{ BSM_F_UNLINKFROM,	F_UNLINKFROM },
210191147Srwatson#endif
211191147Srwatson#ifdef	F_CHECK_OPENEVT
212191147Srwatson	{ BSM_F_CHECK_OPENEVT,	F_CHECK_OPENEVT },
213191147Srwatson#endif
214191147Srwatson#ifdef	F_ADDSIGS
215191147Srwatson	{ BSM_F_ADDSIGS,	F_ADDSIGS },
216191147Srwatson#endif
217191147Srwatson#ifdef	F_MARKDEPENDENCY
218191147Srwatson	{ BSM_F_MARKDEPENDENCY,	F_MARKDEPENDENCY },
219191147Srwatson#endif
220191147Srwatson
221191147Srwatson#ifdef	FCNTL_FS_SPECIFIC_BASE
222191147Srwatson	{ BSM_F_FS_SPECIFIC_0,	FCNTL_FS_SPECIFIC_BASE},
223191147Srwatson	{ BSM_F_FS_SPECIFIC_1,	FCNTL_FS_SPECIFIC_BASE + 1},
224191147Srwatson	{ BSM_F_FS_SPECIFIC_2,	FCNTL_FS_SPECIFIC_BASE + 2},
225191147Srwatson	{ BSM_F_FS_SPECIFIC_3,	FCNTL_FS_SPECIFIC_BASE + 3},
226191147Srwatson	{ BSM_F_FS_SPECIFIC_4,	FCNTL_FS_SPECIFIC_BASE + 4},
227191147Srwatson	{ BSM_F_FS_SPECIFIC_5,	FCNTL_FS_SPECIFIC_BASE + 5},
228191147Srwatson	{ BSM_F_FS_SPECIFIC_6,	FCNTL_FS_SPECIFIC_BASE + 6},
229191147Srwatson	{ BSM_F_FS_SPECIFIC_7,	FCNTL_FS_SPECIFIC_BASE + 7},
230191147Srwatson	{ BSM_F_FS_SPECIFIC_8,	FCNTL_FS_SPECIFIC_BASE + 8},
231191147Srwatson	{ BSM_F_FS_SPECIFIC_9,	FCNTL_FS_SPECIFIC_BASE + 9},
232191147Srwatson	{ BSM_F_FS_SPECIFIC_10,	FCNTL_FS_SPECIFIC_BASE + 10},
233191147Srwatson	{ BSM_F_FS_SPECIFIC_11,	FCNTL_FS_SPECIFIC_BASE + 11},
234191147Srwatson	{ BSM_F_FS_SPECIFIC_12,	FCNTL_FS_SPECIFIC_BASE + 12},
235191147Srwatson	{ BSM_F_FS_SPECIFIC_13,	FCNTL_FS_SPECIFIC_BASE + 13},
236191147Srwatson	{ BSM_F_FS_SPECIFIC_14,	FCNTL_FS_SPECIFIC_BASE + 14},
237191147Srwatson	{ BSM_F_FS_SPECIFIC_15,	FCNTL_FS_SPECIFIC_BASE + 15},
238191147Srwatson#endif	/* FCNTL_FS_SPECIFIC_BASE */
239191147Srwatson};
240191147Srwatsonstatic const int bsm_fcntl_cmd_count = sizeof(bsm_fcntl_cmdtab) /
241191147Srwatson	    sizeof(bsm_fcntl_cmdtab[0]);
242191147Srwatson
243191147Srwatsonstatic const bsm_fcntl_cmd_t *
244191147Srwatsonbsm_lookup_local_fcntl_cmd(int local_fcntl_cmd)
245191147Srwatson{
246191147Srwatson	int i;
247191147Srwatson
248191147Srwatson	for (i = 0; i < bsm_fcntl_cmd_count; i++) {
249191147Srwatson		if (bsm_fcntl_cmdtab[i].bfc_local_fcntl_cmd ==
250191147Srwatson		    local_fcntl_cmd)
251191147Srwatson			return (&bsm_fcntl_cmdtab[i]);
252191147Srwatson	}
253191147Srwatson	return (NULL);
254191147Srwatson}
255191147Srwatson
256191147Srwatsonu_short
257191147Srwatsonau_fcntl_cmd_to_bsm(int local_fcntl_cmd)
258191147Srwatson{
259191147Srwatson	const bsm_fcntl_cmd_t *bfcp;
260191147Srwatson
261191147Srwatson	bfcp = bsm_lookup_local_fcntl_cmd(local_fcntl_cmd);
262191147Srwatson	if (bfcp == NULL)
263191147Srwatson		return (BSM_F_UNKNOWN);
264191147Srwatson	return (bfcp->bfc_bsm_fcntl_cmd);
265191147Srwatson}
266191147Srwatson
267191147Srwatsonstatic const bsm_fcntl_cmd_t *
268191147Srwatsonbsm_lookup_bsm_fcntl_cmd(u_short bsm_fcntl_cmd)
269191147Srwatson{
270191147Srwatson	int i;
271191147Srwatson
272191147Srwatson	for (i = 0; i < bsm_fcntl_cmd_count; i++) {
273191147Srwatson		if (bsm_fcntl_cmdtab[i].bfc_bsm_fcntl_cmd ==
274191147Srwatson		    bsm_fcntl_cmd)
275191147Srwatson			return (&bsm_fcntl_cmdtab[i]);
276191147Srwatson	}
277191147Srwatson	return (NULL);
278191147Srwatson}
279191147Srwatson
280191147Srwatsonint
281191147Srwatsonau_bsm_to_fcntl_cmd(u_short bsm_fcntl_cmd, int *local_fcntl_cmdp)
282191147Srwatson{
283191147Srwatson	const bsm_fcntl_cmd_t *bfcp;
284191147Srwatson
285191147Srwatson	bfcp = bsm_lookup_bsm_fcntl_cmd(bsm_fcntl_cmd);
286191147Srwatson	if (bfcp == NULL || bfcp->bfc_local_fcntl_cmd)
287191147Srwatson		return (-1);
288191147Srwatson	*local_fcntl_cmdp = bfcp->bfc_local_fcntl_cmd;
289191147Srwatson	return (0);
290191147Srwatson}
291