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 * $P4: //depot/projects/trustedbsd/openbsm/libbsm/bsm_fcntl.c#2 $
30191271Srwatson */
31191271Srwatson
32191271Srwatson#include <sys/param.h>
33191271Srwatson#include <sys/fcntl.h>
34191271Srwatson
35191271Srwatson#include <config/config.h>
36191271Srwatson
37191271Srwatson#include <bsm/audit_fcntl.h>
38191271Srwatson#include <bsm/libbsm.h>
39191271Srwatson
40191271Srwatsonstruct bsm_fcntl_cmd {
41191271Srwatson	u_short	bfc_bsm_fcntl_cmd;
42191271Srwatson	int	bfc_local_fcntl_cmd;
43191271Srwatson};
44191271Srwatsontypedef struct bsm_fcntl_cmd	bsm_fcntl_cmd_t;
45191271Srwatson
46191271Srwatsonstatic const bsm_fcntl_cmd_t bsm_fcntl_cmdtab[] = {
47191271Srwatson	{ BSM_F_DUPFD, 		F_DUPFD },
48191271Srwatson	{ BSM_F_GETFD,		F_GETFD },
49191271Srwatson	{ BSM_F_SETFD,		F_SETFD	},
50191271Srwatson	{ BSM_F_GETFL,		F_GETFL },
51191271Srwatson	{ BSM_F_SETFL,		F_SETFL },
52191271Srwatson#ifdef	F_O_GETLK
53191271Srwatson	{ BSM_F_O_GETLK,	F_O_GETLK },
54191271Srwatson#endif
55191271Srwatson	{ BSM_F_SETLK,		F_SETLK },
56191271Srwatson	{ BSM_F_SETLKW,		F_SETLK },
57191271Srwatson#ifdef	F_CHFL
58191271Srwatson	{ BSM_F_CHKFL,		F_CHKFL },
59191271Srwatson#endif
60191271Srwatson#ifdef 	F_DUP2FD
61191271Srwatson	{ BSM_F_DUP2FD,		F_DUP2FD },
62191271Srwatson#endif
63191271Srwatson#ifdef	F_ALLOCSP
64191271Srwatson	{ BSM_F_ALLOCSP,	F_ALLOCSP },
65191271Srwatson#endif
66191271Srwatson#ifdef	F_FREESP
67191271Srwatson	{ BSM_F_FREESP,		F_FREESP },
68191271Srwatson#endif
69191271Srwatson#ifdef	F_ISSTREAM
70191271Srwatson	{ BSM_F_ISSTREAM,	F_ISSTREAM},
71191271Srwatson#endif
72191271Srwatson	{ BSM_F_GETLK,		F_GETLK },
73191271Srwatson#ifdef 	F_PRIV
74191271Srwatson	{ BSM_F_PRIV,		F_PRIV },
75191271Srwatson#endif
76191271Srwatson#ifdef	F_NPRIV
77191271Srwatson	{ BSM_F_NPRIV,		F_NPRIV },
78191271Srwatson#endif
79191271Srwatson#ifdef 	F_QUOTACTL
80191271Srwatson	{ BSM_F_QUOTACTL,	F_QUOTACTL },
81191271Srwatson#endif
82191271Srwatson#ifdef	F_BLOCKS
83191271Srwatson	{ BSM_F_BLOCKS,		F_BLOCKS },
84191271Srwatson#endif
85191271Srwatson#ifdef	F_BLKSIZE
86191271Srwatson	{ BSM_F_BLKSIZE,	F_BLKSIZE },
87191271Srwatson#endif
88191271Srwatson	{ BSM_F_GETOWN,		F_GETOWN },
89191271Srwatson	{ BSM_F_SETOWN,		F_SETOWN },
90191271Srwatson#ifdef	F_REVOKE
91191271Srwatson	{ BSM_F_REVOKE,		F_REVOKE },
92191271Srwatson#endif
93191271Srwatson#ifdef 	F_HASREMOTEBLOCKS
94191271Srwatson	{ BSM_F_HASREMOTEBLOCKS,
95191271Srwatson				F_HASREMOTEBLOCKS },
96191271Srwatson#endif
97191271Srwatson#ifdef 	F_FREESP
98191271Srwatson	{ BSM_F_FREESP,		F_FREESP },
99191271Srwatson#endif
100191271Srwatson#ifdef 	F_ALLOCSP
101191271Srwatson	{ BSM_F_ALLOCSP,	F_ALLOCSP },
102191271Srwatson#endif
103191271Srwatson#ifdef	F_FREESP64
104191271Srwatson	{ BSM_F_FREESP64,	F_FREESP64 },
105191271Srwatson#endif
106191271Srwatson#ifdef 	F_ALLOCSP64
107191271Srwatson	{ BSM_F_ALLOCSP64,	F_ALLOCSP64 },
108191271Srwatson#endif
109191271Srwatson#ifdef	F_GETLK64
110191271Srwatson	{ BSM_F_GETLK64, 	F_GETLK64 },
111191271Srwatson#endif
112191271Srwatson#ifdef	F_SETLK64
113191271Srwatson	{ BSM_F_SETLK64, 	F_SETLK64 },
114191271Srwatson#endif
115191271Srwatson#ifdef	F_SETLKW64
116191271Srwatson	{ BSM_F_SETLKW64, 	F_SETLKW64 },
117191271Srwatson#endif
118191271Srwatson#ifdef	F_SHARE
119191271Srwatson	{ BSM_F_SHARE,		F_SHARE },
120191271Srwatson#endif
121191271Srwatson#ifdef	F_UNSHARE
122191271Srwatson	{ BSM_F_UNSHARE,	F_UNSHARE },
123191271Srwatson#endif
124191271Srwatson#ifdef	F_SETLK_NBMAND
125191271Srwatson	{ BSM_F_SETLK_NBMAND,	F_SETLK_NBMAND },
126191271Srwatson#endif
127191271Srwatson#ifdef	F_SHARE_NBMAND
128191271Srwatson	{ BSM_F_SHARE_NBMAND,	F_SHARE_NBMAND },
129191271Srwatson#endif
130191271Srwatson#ifdef	F_SETLK64_NBMAND
131191271Srwatson	{ BSM_F_SETLK64_NBMAND,	F_SETLK64_NBMAND },
132191271Srwatson#endif
133191271Srwatson#ifdef	F_GETXFL
134191271Srwatson	{ BSM_F_GETXFL,		F_GETXFL },
135191271Srwatson#endif
136191271Srwatson#ifdef	F_BADFD
137191271Srwatson	{ BSM_F_BADFD,		F_BADFD },
138191271Srwatson#endif
139191271Srwatson#ifdef	F_OGETLK
140191271Srwatson	{ BSM_F_OGETLK,		F_OGETLK },
141191271Srwatson#endif
142191271Srwatson#ifdef	F_OSETLK
143191271Srwatson	{ BSM_F_OSETLK,		F_OSETLK },
144191271Srwatson#endif
145191271Srwatson#ifdef	F_OSETLKW
146191271Srwatson	{ BSM_F_OSETLKW,	F_OSETLKW },
147191271Srwatson#endif
148191271Srwatson#ifdef	F_SETLK_REMOTE
149191271Srwatson	{ BSM_F_SETLK_REMOTE,	F_SETLK_REMOTE },
150191271Srwatson#endif
151191271Srwatson
152191271Srwatson#ifdef	F_SETSIG
153191271Srwatson	{ BSM_F_SETSIG,		F_SETSIG },
154191271Srwatson#endif
155191271Srwatson#ifdef	F_GETSIG
156191271Srwatson	{ BSM_F_GETSIG,		F_GETSIG },
157191271Srwatson#endif
158191271Srwatson
159191271Srwatson#ifdef	F_CHKCLEAN
160191271Srwatson	{ BSM_F_CHKCLEAN,	F_CHKCLEAN },
161191271Srwatson#endif
162191271Srwatson#ifdef	F_PREALLOCATE
163191271Srwatson	{ BSM_F_PREALLOCATE,	F_PREALLOCATE },
164191271Srwatson#endif
165191271Srwatson#ifdef	F_SETSIZE
166191271Srwatson	{ BSM_F_SETSIZE,	F_SETSIZE },
167191271Srwatson#endif
168191271Srwatson#ifdef	F_RDADVISE
169191271Srwatson	{ BSM_F_RDADVISE,	F_RDADVISE },
170191271Srwatson#endif
171191271Srwatson#ifdef	F_RDAHEAD
172191271Srwatson	{ BSM_F_RDAHEAD,	F_RDAHEAD },
173191271Srwatson#endif
174191271Srwatson#ifdef	F_READBOOTSTRAP
175191271Srwatson	{ BSM_F_READBOOTSTRAP,	F_READBOOTSTRAP },
176191271Srwatson#endif
177191271Srwatson#ifdef	F_WRITEBOOTSTRAP
178191271Srwatson	{ BSM_F_WRITEBOOTSTRAP,	F_WRITEBOOTSTRAP },
179191271Srwatson#endif
180191271Srwatson#ifdef	F_NOCACHE
181191271Srwatson	{ BSM_F_NOCACHE,	F_NOCACHE },
182191271Srwatson#endif
183191271Srwatson#ifdef	F_LOG2PHYS
184191271Srwatson	{ BSM_F_LOG2PHYS,	F_LOG2PHYS },
185191271Srwatson#endif
186191271Srwatson#ifdef	F_GETPATH
187191271Srwatson	{ BSM_F_GETPATH,	F_GETPATH },
188191271Srwatson#endif
189191271Srwatson#ifdef	F_FULLFSYNC
190191271Srwatson	{ BSM_F_FULLFSYNC,	F_FULLFSYNC },
191191271Srwatson#endif
192191271Srwatson#ifdef	F_PATHPKG_CHECK
193191271Srwatson	{ BSM_F_PATHPKG_CHECK,	F_PATHPKG_CHECK },
194191271Srwatson#endif
195191271Srwatson#ifdef	F_FREEZE_FS
196191271Srwatson	{ BSM_F_FREEZE_FS,	F_FREEZE_FS },
197191271Srwatson#endif
198191271Srwatson#ifdef	F_THAW_FS
199191271Srwatson	{ BSM_F_THAW_FS,	F_THAW_FS },
200191271Srwatson#endif
201191271Srwatson#ifdef	F_GLOBAL_NOCACHE
202191271Srwatson	{ BSM_F_GLOBAL_NOCACHE,	F_GLOBAL_NOCACHE },
203191271Srwatson#endif
204191271Srwatson#ifdef	F_OPENFROM
205191271Srwatson	{ BSM_F_OPENFROM,	F_OPENFROM },
206191271Srwatson#endif
207191271Srwatson#ifdef	F_UNLINKFROM
208191271Srwatson	{ BSM_F_UNLINKFROM,	F_UNLINKFROM },
209191271Srwatson#endif
210191271Srwatson#ifdef	F_CHECK_OPENEVT
211191271Srwatson	{ BSM_F_CHECK_OPENEVT,	F_CHECK_OPENEVT },
212191271Srwatson#endif
213191271Srwatson#ifdef	F_ADDSIGS
214191271Srwatson	{ BSM_F_ADDSIGS,	F_ADDSIGS },
215191271Srwatson#endif
216191271Srwatson#ifdef	F_MARKDEPENDENCY
217191271Srwatson	{ BSM_F_MARKDEPENDENCY,	F_MARKDEPENDENCY },
218191271Srwatson#endif
219191271Srwatson
220191271Srwatson#ifdef	FCNTL_FS_SPECIFIC_BASE
221191271Srwatson	{ BSM_F_FS_SPECIFIC_0,	FCNTL_FS_SPECIFIC_BASE},
222191271Srwatson	{ BSM_F_FS_SPECIFIC_1,	FCNTL_FS_SPECIFIC_BASE + 1},
223191271Srwatson	{ BSM_F_FS_SPECIFIC_2,	FCNTL_FS_SPECIFIC_BASE + 2},
224191271Srwatson	{ BSM_F_FS_SPECIFIC_3,	FCNTL_FS_SPECIFIC_BASE + 3},
225191271Srwatson	{ BSM_F_FS_SPECIFIC_4,	FCNTL_FS_SPECIFIC_BASE + 4},
226191271Srwatson	{ BSM_F_FS_SPECIFIC_5,	FCNTL_FS_SPECIFIC_BASE + 5},
227191271Srwatson	{ BSM_F_FS_SPECIFIC_6,	FCNTL_FS_SPECIFIC_BASE + 6},
228191271Srwatson	{ BSM_F_FS_SPECIFIC_7,	FCNTL_FS_SPECIFIC_BASE + 7},
229191271Srwatson	{ BSM_F_FS_SPECIFIC_8,	FCNTL_FS_SPECIFIC_BASE + 8},
230191271Srwatson	{ BSM_F_FS_SPECIFIC_9,	FCNTL_FS_SPECIFIC_BASE + 9},
231191271Srwatson	{ BSM_F_FS_SPECIFIC_10,	FCNTL_FS_SPECIFIC_BASE + 10},
232191271Srwatson	{ BSM_F_FS_SPECIFIC_11,	FCNTL_FS_SPECIFIC_BASE + 11},
233191271Srwatson	{ BSM_F_FS_SPECIFIC_12,	FCNTL_FS_SPECIFIC_BASE + 12},
234191271Srwatson	{ BSM_F_FS_SPECIFIC_13,	FCNTL_FS_SPECIFIC_BASE + 13},
235191271Srwatson	{ BSM_F_FS_SPECIFIC_14,	FCNTL_FS_SPECIFIC_BASE + 14},
236191271Srwatson	{ BSM_F_FS_SPECIFIC_15,	FCNTL_FS_SPECIFIC_BASE + 15},
237191271Srwatson#endif	/* FCNTL_FS_SPECIFIC_BASE */
238191271Srwatson};
239191271Srwatsonstatic const int bsm_fcntl_cmd_count = sizeof(bsm_fcntl_cmdtab) /
240191271Srwatson	    sizeof(bsm_fcntl_cmdtab[0]);
241191271Srwatson
242191271Srwatsonstatic const bsm_fcntl_cmd_t *
243191271Srwatsonbsm_lookup_local_fcntl_cmd(int local_fcntl_cmd)
244191271Srwatson{
245191271Srwatson	int i;
246191271Srwatson
247191271Srwatson	for (i = 0; i < bsm_fcntl_cmd_count; i++) {
248191271Srwatson		if (bsm_fcntl_cmdtab[i].bfc_local_fcntl_cmd ==
249191271Srwatson		    local_fcntl_cmd)
250191271Srwatson			return (&bsm_fcntl_cmdtab[i]);
251191271Srwatson	}
252191271Srwatson	return (NULL);
253191271Srwatson}
254191271Srwatson
255191271Srwatsonu_short
256191271Srwatsonau_fcntl_cmd_to_bsm(int local_fcntl_cmd)
257191271Srwatson{
258191271Srwatson	const bsm_fcntl_cmd_t *bfcp;
259191271Srwatson
260191271Srwatson	bfcp = bsm_lookup_local_fcntl_cmd(local_fcntl_cmd);
261191271Srwatson	if (bfcp == NULL)
262191271Srwatson		return (BSM_F_UNKNOWN);
263191271Srwatson	return (bfcp->bfc_bsm_fcntl_cmd);
264191271Srwatson}
265191271Srwatson
266191271Srwatsonstatic const bsm_fcntl_cmd_t *
267191271Srwatsonbsm_lookup_bsm_fcntl_cmd(u_short bsm_fcntl_cmd)
268191271Srwatson{
269191271Srwatson	int i;
270191271Srwatson
271191271Srwatson	for (i = 0; i < bsm_fcntl_cmd_count; i++) {
272191271Srwatson		if (bsm_fcntl_cmdtab[i].bfc_bsm_fcntl_cmd ==
273191271Srwatson		    bsm_fcntl_cmd)
274191271Srwatson			return (&bsm_fcntl_cmdtab[i]);
275191271Srwatson	}
276191271Srwatson	return (NULL);
277191271Srwatson}
278191271Srwatson
279191271Srwatsonint
280191271Srwatsonau_bsm_to_fcntl_cmd(u_short bsm_fcntl_cmd, int *local_fcntl_cmdp)
281191271Srwatson{
282191271Srwatson	const bsm_fcntl_cmd_t *bfcp;
283191271Srwatson
284191271Srwatson	bfcp = bsm_lookup_bsm_fcntl_cmd(bsm_fcntl_cmd);
285191271Srwatson	if (bfcp == NULL || bfcp->bfc_local_fcntl_cmd)
286191271Srwatson		return (-1);
287191271Srwatson	*local_fcntl_cmdp = bfcp->bfc_local_fcntl_cmd;
288191271Srwatson	return (0);
289191271Srwatson}
290