1191271Srwatson/*-
2191271Srwatson * Copyright (c) 2008-2009 Apple Inc.
3191271Srwatson * All rights reserved.
4191271Srwatson *
5191271Srwatson * Redistribution and use in source and binary forms, with or without
6191271Srwatson * modification, are permitted provided that the following conditions
7191271Srwatson * are met:
8191271Srwatson * 1.  Redistributions of source code must retain the above copyright
9191271Srwatson *     notice, this list of conditions and the following disclaimer.
10191271Srwatson * 2.  Redistributions in binary form must reproduce the above copyright
11191271Srwatson *     notice, this list of conditions and the following disclaimer in the
12191271Srwatson *     documentation and/or other materials provided with the distribution.
13191271Srwatson * 3.  Neither the name of Apple Inc. ("Apple") nor the names of
14191271Srwatson *     its contributors may be used to endorse or promote products derived
15191271Srwatson *     from this software without specific prior written permission.
16191271Srwatson *
17191271Srwatson * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND
18191271Srwatson * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19191271Srwatson * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20191271Srwatson * ARE DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR
21191271Srwatson * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22191271Srwatson * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23191271Srwatson * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24191271Srwatson * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
25191271Srwatson * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
26191271Srwatson * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27191271Srwatson * POSSIBILITY OF SUCH DAMAGE.
28191271Srwatson */
29191271Srwatson
30191271Srwatson#include <sys/param.h>
31191271Srwatson#include <sys/fcntl.h>
32191271Srwatson
33191271Srwatson#include <config/config.h>
34191271Srwatson
35191271Srwatson#include <bsm/audit_fcntl.h>
36191271Srwatson#include <bsm/libbsm.h>
37191271Srwatson
38191271Srwatsonstruct bsm_fcntl_cmd {
39191271Srwatson	u_short	bfc_bsm_fcntl_cmd;
40191271Srwatson	int	bfc_local_fcntl_cmd;
41191271Srwatson};
42191271Srwatsontypedef struct bsm_fcntl_cmd	bsm_fcntl_cmd_t;
43191271Srwatson
44191271Srwatsonstatic const bsm_fcntl_cmd_t bsm_fcntl_cmdtab[] = {
45191271Srwatson	{ BSM_F_DUPFD, 		F_DUPFD },
46191271Srwatson	{ BSM_F_GETFD,		F_GETFD },
47191271Srwatson	{ BSM_F_SETFD,		F_SETFD	},
48191271Srwatson	{ BSM_F_GETFL,		F_GETFL },
49191271Srwatson	{ BSM_F_SETFL,		F_SETFL },
50191271Srwatson#ifdef	F_O_GETLK
51191271Srwatson	{ BSM_F_O_GETLK,	F_O_GETLK },
52191271Srwatson#endif
53191271Srwatson	{ BSM_F_SETLK,		F_SETLK },
54191271Srwatson	{ BSM_F_SETLKW,		F_SETLK },
55191271Srwatson#ifdef	F_CHFL
56191271Srwatson	{ BSM_F_CHKFL,		F_CHKFL },
57191271Srwatson#endif
58191271Srwatson#ifdef 	F_DUP2FD
59191271Srwatson	{ BSM_F_DUP2FD,		F_DUP2FD },
60191271Srwatson#endif
61191271Srwatson#ifdef	F_ALLOCSP
62191271Srwatson	{ BSM_F_ALLOCSP,	F_ALLOCSP },
63191271Srwatson#endif
64191271Srwatson#ifdef	F_FREESP
65191271Srwatson	{ BSM_F_FREESP,		F_FREESP },
66191271Srwatson#endif
67191271Srwatson#ifdef	F_ISSTREAM
68191271Srwatson	{ BSM_F_ISSTREAM,	F_ISSTREAM},
69191271Srwatson#endif
70191271Srwatson	{ BSM_F_GETLK,		F_GETLK },
71191271Srwatson#ifdef 	F_PRIV
72191271Srwatson	{ BSM_F_PRIV,		F_PRIV },
73191271Srwatson#endif
74191271Srwatson#ifdef	F_NPRIV
75191271Srwatson	{ BSM_F_NPRIV,		F_NPRIV },
76191271Srwatson#endif
77191271Srwatson#ifdef 	F_QUOTACTL
78191271Srwatson	{ BSM_F_QUOTACTL,	F_QUOTACTL },
79191271Srwatson#endif
80191271Srwatson#ifdef	F_BLOCKS
81191271Srwatson	{ BSM_F_BLOCKS,		F_BLOCKS },
82191271Srwatson#endif
83191271Srwatson#ifdef	F_BLKSIZE
84191271Srwatson	{ BSM_F_BLKSIZE,	F_BLKSIZE },
85191271Srwatson#endif
86191271Srwatson	{ BSM_F_GETOWN,		F_GETOWN },
87191271Srwatson	{ BSM_F_SETOWN,		F_SETOWN },
88191271Srwatson#ifdef	F_REVOKE
89191271Srwatson	{ BSM_F_REVOKE,		F_REVOKE },
90191271Srwatson#endif
91191271Srwatson#ifdef 	F_HASREMOTEBLOCKS
92191271Srwatson	{ BSM_F_HASREMOTEBLOCKS,
93191271Srwatson				F_HASREMOTEBLOCKS },
94191271Srwatson#endif
95191271Srwatson#ifdef 	F_FREESP
96191271Srwatson	{ BSM_F_FREESP,		F_FREESP },
97191271Srwatson#endif
98191271Srwatson#ifdef 	F_ALLOCSP
99191271Srwatson	{ BSM_F_ALLOCSP,	F_ALLOCSP },
100191271Srwatson#endif
101191271Srwatson#ifdef	F_FREESP64
102191271Srwatson	{ BSM_F_FREESP64,	F_FREESP64 },
103191271Srwatson#endif
104191271Srwatson#ifdef 	F_ALLOCSP64
105191271Srwatson	{ BSM_F_ALLOCSP64,	F_ALLOCSP64 },
106191271Srwatson#endif
107191271Srwatson#ifdef	F_GETLK64
108191271Srwatson	{ BSM_F_GETLK64, 	F_GETLK64 },
109191271Srwatson#endif
110191271Srwatson#ifdef	F_SETLK64
111191271Srwatson	{ BSM_F_SETLK64, 	F_SETLK64 },
112191271Srwatson#endif
113191271Srwatson#ifdef	F_SETLKW64
114191271Srwatson	{ BSM_F_SETLKW64, 	F_SETLKW64 },
115191271Srwatson#endif
116191271Srwatson#ifdef	F_SHARE
117191271Srwatson	{ BSM_F_SHARE,		F_SHARE },
118191271Srwatson#endif
119191271Srwatson#ifdef	F_UNSHARE
120191271Srwatson	{ BSM_F_UNSHARE,	F_UNSHARE },
121191271Srwatson#endif
122191271Srwatson#ifdef	F_SETLK_NBMAND
123191271Srwatson	{ BSM_F_SETLK_NBMAND,	F_SETLK_NBMAND },
124191271Srwatson#endif
125191271Srwatson#ifdef	F_SHARE_NBMAND
126191271Srwatson	{ BSM_F_SHARE_NBMAND,	F_SHARE_NBMAND },
127191271Srwatson#endif
128191271Srwatson#ifdef	F_SETLK64_NBMAND
129191271Srwatson	{ BSM_F_SETLK64_NBMAND,	F_SETLK64_NBMAND },
130191271Srwatson#endif
131191271Srwatson#ifdef	F_GETXFL
132191271Srwatson	{ BSM_F_GETXFL,		F_GETXFL },
133191271Srwatson#endif
134191271Srwatson#ifdef	F_BADFD
135191271Srwatson	{ BSM_F_BADFD,		F_BADFD },
136191271Srwatson#endif
137191271Srwatson#ifdef	F_OGETLK
138191271Srwatson	{ BSM_F_OGETLK,		F_OGETLK },
139191271Srwatson#endif
140191271Srwatson#ifdef	F_OSETLK
141191271Srwatson	{ BSM_F_OSETLK,		F_OSETLK },
142191271Srwatson#endif
143191271Srwatson#ifdef	F_OSETLKW
144191271Srwatson	{ BSM_F_OSETLKW,	F_OSETLKW },
145191271Srwatson#endif
146191271Srwatson#ifdef	F_SETLK_REMOTE
147191271Srwatson	{ BSM_F_SETLK_REMOTE,	F_SETLK_REMOTE },
148191271Srwatson#endif
149191271Srwatson
150191271Srwatson#ifdef	F_SETSIG
151191271Srwatson	{ BSM_F_SETSIG,		F_SETSIG },
152191271Srwatson#endif
153191271Srwatson#ifdef	F_GETSIG
154191271Srwatson	{ BSM_F_GETSIG,		F_GETSIG },
155191271Srwatson#endif
156191271Srwatson
157191271Srwatson#ifdef	F_CHKCLEAN
158191271Srwatson	{ BSM_F_CHKCLEAN,	F_CHKCLEAN },
159191271Srwatson#endif
160191271Srwatson#ifdef	F_PREALLOCATE
161191271Srwatson	{ BSM_F_PREALLOCATE,	F_PREALLOCATE },
162191271Srwatson#endif
163191271Srwatson#ifdef	F_SETSIZE
164191271Srwatson	{ BSM_F_SETSIZE,	F_SETSIZE },
165191271Srwatson#endif
166191271Srwatson#ifdef	F_RDADVISE
167191271Srwatson	{ BSM_F_RDADVISE,	F_RDADVISE },
168191271Srwatson#endif
169191271Srwatson#ifdef	F_RDAHEAD
170191271Srwatson	{ BSM_F_RDAHEAD,	F_RDAHEAD },
171191271Srwatson#endif
172191271Srwatson#ifdef	F_READBOOTSTRAP
173191271Srwatson	{ BSM_F_READBOOTSTRAP,	F_READBOOTSTRAP },
174191271Srwatson#endif
175191271Srwatson#ifdef	F_WRITEBOOTSTRAP
176191271Srwatson	{ BSM_F_WRITEBOOTSTRAP,	F_WRITEBOOTSTRAP },
177191271Srwatson#endif
178191271Srwatson#ifdef	F_NOCACHE
179191271Srwatson	{ BSM_F_NOCACHE,	F_NOCACHE },
180191271Srwatson#endif
181191271Srwatson#ifdef	F_LOG2PHYS
182191271Srwatson	{ BSM_F_LOG2PHYS,	F_LOG2PHYS },
183191271Srwatson#endif
184191271Srwatson#ifdef	F_GETPATH
185191271Srwatson	{ BSM_F_GETPATH,	F_GETPATH },
186191271Srwatson#endif
187191271Srwatson#ifdef	F_FULLFSYNC
188191271Srwatson	{ BSM_F_FULLFSYNC,	F_FULLFSYNC },
189191271Srwatson#endif
190191271Srwatson#ifdef	F_PATHPKG_CHECK
191191271Srwatson	{ BSM_F_PATHPKG_CHECK,	F_PATHPKG_CHECK },
192191271Srwatson#endif
193191271Srwatson#ifdef	F_FREEZE_FS
194191271Srwatson	{ BSM_F_FREEZE_FS,	F_FREEZE_FS },
195191271Srwatson#endif
196191271Srwatson#ifdef	F_THAW_FS
197191271Srwatson	{ BSM_F_THAW_FS,	F_THAW_FS },
198191271Srwatson#endif
199191271Srwatson#ifdef	F_GLOBAL_NOCACHE
200191271Srwatson	{ BSM_F_GLOBAL_NOCACHE,	F_GLOBAL_NOCACHE },
201191271Srwatson#endif
202191271Srwatson#ifdef	F_OPENFROM
203191271Srwatson	{ BSM_F_OPENFROM,	F_OPENFROM },
204191271Srwatson#endif
205191271Srwatson#ifdef	F_UNLINKFROM
206191271Srwatson	{ BSM_F_UNLINKFROM,	F_UNLINKFROM },
207191271Srwatson#endif
208191271Srwatson#ifdef	F_CHECK_OPENEVT
209191271Srwatson	{ BSM_F_CHECK_OPENEVT,	F_CHECK_OPENEVT },
210191271Srwatson#endif
211191271Srwatson#ifdef	F_ADDSIGS
212191271Srwatson	{ BSM_F_ADDSIGS,	F_ADDSIGS },
213191271Srwatson#endif
214191271Srwatson#ifdef	F_MARKDEPENDENCY
215191271Srwatson	{ BSM_F_MARKDEPENDENCY,	F_MARKDEPENDENCY },
216191271Srwatson#endif
217191271Srwatson
218191271Srwatson#ifdef	FCNTL_FS_SPECIFIC_BASE
219191271Srwatson	{ BSM_F_FS_SPECIFIC_0,	FCNTL_FS_SPECIFIC_BASE},
220191271Srwatson	{ BSM_F_FS_SPECIFIC_1,	FCNTL_FS_SPECIFIC_BASE + 1},
221191271Srwatson	{ BSM_F_FS_SPECIFIC_2,	FCNTL_FS_SPECIFIC_BASE + 2},
222191271Srwatson	{ BSM_F_FS_SPECIFIC_3,	FCNTL_FS_SPECIFIC_BASE + 3},
223191271Srwatson	{ BSM_F_FS_SPECIFIC_4,	FCNTL_FS_SPECIFIC_BASE + 4},
224191271Srwatson	{ BSM_F_FS_SPECIFIC_5,	FCNTL_FS_SPECIFIC_BASE + 5},
225191271Srwatson	{ BSM_F_FS_SPECIFIC_6,	FCNTL_FS_SPECIFIC_BASE + 6},
226191271Srwatson	{ BSM_F_FS_SPECIFIC_7,	FCNTL_FS_SPECIFIC_BASE + 7},
227191271Srwatson	{ BSM_F_FS_SPECIFIC_8,	FCNTL_FS_SPECIFIC_BASE + 8},
228191271Srwatson	{ BSM_F_FS_SPECIFIC_9,	FCNTL_FS_SPECIFIC_BASE + 9},
229191271Srwatson	{ BSM_F_FS_SPECIFIC_10,	FCNTL_FS_SPECIFIC_BASE + 10},
230191271Srwatson	{ BSM_F_FS_SPECIFIC_11,	FCNTL_FS_SPECIFIC_BASE + 11},
231191271Srwatson	{ BSM_F_FS_SPECIFIC_12,	FCNTL_FS_SPECIFIC_BASE + 12},
232191271Srwatson	{ BSM_F_FS_SPECIFIC_13,	FCNTL_FS_SPECIFIC_BASE + 13},
233191271Srwatson	{ BSM_F_FS_SPECIFIC_14,	FCNTL_FS_SPECIFIC_BASE + 14},
234191271Srwatson	{ BSM_F_FS_SPECIFIC_15,	FCNTL_FS_SPECIFIC_BASE + 15},
235191271Srwatson#endif	/* FCNTL_FS_SPECIFIC_BASE */
236191271Srwatson};
237191271Srwatsonstatic const int bsm_fcntl_cmd_count = sizeof(bsm_fcntl_cmdtab) /
238191271Srwatson	    sizeof(bsm_fcntl_cmdtab[0]);
239191271Srwatson
240191271Srwatsonstatic const bsm_fcntl_cmd_t *
241191271Srwatsonbsm_lookup_local_fcntl_cmd(int local_fcntl_cmd)
242191271Srwatson{
243191271Srwatson	int i;
244191271Srwatson
245191271Srwatson	for (i = 0; i < bsm_fcntl_cmd_count; i++) {
246191271Srwatson		if (bsm_fcntl_cmdtab[i].bfc_local_fcntl_cmd ==
247191271Srwatson		    local_fcntl_cmd)
248191271Srwatson			return (&bsm_fcntl_cmdtab[i]);
249191271Srwatson	}
250191271Srwatson	return (NULL);
251191271Srwatson}
252191271Srwatson
253191271Srwatsonu_short
254191271Srwatsonau_fcntl_cmd_to_bsm(int local_fcntl_cmd)
255191271Srwatson{
256191271Srwatson	const bsm_fcntl_cmd_t *bfcp;
257191271Srwatson
258191271Srwatson	bfcp = bsm_lookup_local_fcntl_cmd(local_fcntl_cmd);
259191271Srwatson	if (bfcp == NULL)
260191271Srwatson		return (BSM_F_UNKNOWN);
261191271Srwatson	return (bfcp->bfc_bsm_fcntl_cmd);
262191271Srwatson}
263191271Srwatson
264191271Srwatsonstatic const bsm_fcntl_cmd_t *
265191271Srwatsonbsm_lookup_bsm_fcntl_cmd(u_short bsm_fcntl_cmd)
266191271Srwatson{
267191271Srwatson	int i;
268191271Srwatson
269191271Srwatson	for (i = 0; i < bsm_fcntl_cmd_count; i++) {
270191271Srwatson		if (bsm_fcntl_cmdtab[i].bfc_bsm_fcntl_cmd ==
271191271Srwatson		    bsm_fcntl_cmd)
272191271Srwatson			return (&bsm_fcntl_cmdtab[i]);
273191271Srwatson	}
274191271Srwatson	return (NULL);
275191271Srwatson}
276191271Srwatson
277191271Srwatsonint
278191271Srwatsonau_bsm_to_fcntl_cmd(u_short bsm_fcntl_cmd, int *local_fcntl_cmdp)
279191271Srwatson{
280191271Srwatson	const bsm_fcntl_cmd_t *bfcp;
281191271Srwatson
282191271Srwatson	bfcp = bsm_lookup_bsm_fcntl_cmd(bsm_fcntl_cmd);
283191271Srwatson	if (bfcp == NULL || bfcp->bfc_local_fcntl_cmd)
284191271Srwatson		return (-1);
285191271Srwatson	*local_fcntl_cmdp = bfcp->bfc_local_fcntl_cmd;
286191271Srwatson	return (0);
287191271Srwatson}
288