xref: /freebsd/contrib/openbsm/libbsm/bsm_fcntl.c (revision a0ee8cc636cd5c2374ec44ca71226564ea0bca95)
1 /*-
2  * Copyright (c) 2008-2009 Apple Inc.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1.  Redistributions of source code must retain the above copyright
9  *     notice, this list of conditions and the following disclaimer.
10  * 2.  Redistributions in binary form must reproduce the above copyright
11  *     notice, this list of conditions and the following disclaimer in the
12  *     documentation and/or other materials provided with the distribution.
13  * 3.  Neither the name of Apple Inc. ("Apple") nor the names of
14  *     its contributors may be used to endorse or promote products derived
15  *     from this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR
21  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
25  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
26  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27  * POSSIBILITY OF SUCH DAMAGE.
28  */
29 
30 #include <sys/param.h>
31 #include <sys/fcntl.h>
32 
33 #include <config/config.h>
34 
35 #include <bsm/audit_fcntl.h>
36 #include <bsm/libbsm.h>
37 
38 struct bsm_fcntl_cmd {
39 	u_short	bfc_bsm_fcntl_cmd;
40 	int	bfc_local_fcntl_cmd;
41 };
42 typedef struct bsm_fcntl_cmd	bsm_fcntl_cmd_t;
43 
44 static const bsm_fcntl_cmd_t bsm_fcntl_cmdtab[] = {
45 	{ BSM_F_DUPFD, 		F_DUPFD },
46 	{ BSM_F_GETFD,		F_GETFD },
47 	{ BSM_F_SETFD,		F_SETFD	},
48 	{ BSM_F_GETFL,		F_GETFL },
49 	{ BSM_F_SETFL,		F_SETFL },
50 #ifdef	F_O_GETLK
51 	{ BSM_F_O_GETLK,	F_O_GETLK },
52 #endif
53 	{ BSM_F_SETLK,		F_SETLK },
54 	{ BSM_F_SETLKW,		F_SETLK },
55 #ifdef	F_CHFL
56 	{ BSM_F_CHKFL,		F_CHKFL },
57 #endif
58 #ifdef 	F_DUP2FD
59 	{ BSM_F_DUP2FD,		F_DUP2FD },
60 #endif
61 #ifdef	F_ALLOCSP
62 	{ BSM_F_ALLOCSP,	F_ALLOCSP },
63 #endif
64 #ifdef	F_FREESP
65 	{ BSM_F_FREESP,		F_FREESP },
66 #endif
67 #ifdef	F_ISSTREAM
68 	{ BSM_F_ISSTREAM,	F_ISSTREAM},
69 #endif
70 	{ BSM_F_GETLK,		F_GETLK },
71 #ifdef 	F_PRIV
72 	{ BSM_F_PRIV,		F_PRIV },
73 #endif
74 #ifdef	F_NPRIV
75 	{ BSM_F_NPRIV,		F_NPRIV },
76 #endif
77 #ifdef 	F_QUOTACTL
78 	{ BSM_F_QUOTACTL,	F_QUOTACTL },
79 #endif
80 #ifdef	F_BLOCKS
81 	{ BSM_F_BLOCKS,		F_BLOCKS },
82 #endif
83 #ifdef	F_BLKSIZE
84 	{ BSM_F_BLKSIZE,	F_BLKSIZE },
85 #endif
86 	{ BSM_F_GETOWN,		F_GETOWN },
87 	{ BSM_F_SETOWN,		F_SETOWN },
88 #ifdef	F_REVOKE
89 	{ BSM_F_REVOKE,		F_REVOKE },
90 #endif
91 #ifdef 	F_HASREMOTEBLOCKS
92 	{ BSM_F_HASREMOTEBLOCKS,
93 				F_HASREMOTEBLOCKS },
94 #endif
95 #ifdef 	F_FREESP
96 	{ BSM_F_FREESP,		F_FREESP },
97 #endif
98 #ifdef 	F_ALLOCSP
99 	{ BSM_F_ALLOCSP,	F_ALLOCSP },
100 #endif
101 #ifdef	F_FREESP64
102 	{ BSM_F_FREESP64,	F_FREESP64 },
103 #endif
104 #ifdef 	F_ALLOCSP64
105 	{ BSM_F_ALLOCSP64,	F_ALLOCSP64 },
106 #endif
107 #ifdef	F_GETLK64
108 	{ BSM_F_GETLK64, 	F_GETLK64 },
109 #endif
110 #ifdef	F_SETLK64
111 	{ BSM_F_SETLK64, 	F_SETLK64 },
112 #endif
113 #ifdef	F_SETLKW64
114 	{ BSM_F_SETLKW64, 	F_SETLKW64 },
115 #endif
116 #ifdef	F_SHARE
117 	{ BSM_F_SHARE,		F_SHARE },
118 #endif
119 #ifdef	F_UNSHARE
120 	{ BSM_F_UNSHARE,	F_UNSHARE },
121 #endif
122 #ifdef	F_SETLK_NBMAND
123 	{ BSM_F_SETLK_NBMAND,	F_SETLK_NBMAND },
124 #endif
125 #ifdef	F_SHARE_NBMAND
126 	{ BSM_F_SHARE_NBMAND,	F_SHARE_NBMAND },
127 #endif
128 #ifdef	F_SETLK64_NBMAND
129 	{ BSM_F_SETLK64_NBMAND,	F_SETLK64_NBMAND },
130 #endif
131 #ifdef	F_GETXFL
132 	{ BSM_F_GETXFL,		F_GETXFL },
133 #endif
134 #ifdef	F_BADFD
135 	{ BSM_F_BADFD,		F_BADFD },
136 #endif
137 #ifdef	F_OGETLK
138 	{ BSM_F_OGETLK,		F_OGETLK },
139 #endif
140 #ifdef	F_OSETLK
141 	{ BSM_F_OSETLK,		F_OSETLK },
142 #endif
143 #ifdef	F_OSETLKW
144 	{ BSM_F_OSETLKW,	F_OSETLKW },
145 #endif
146 #ifdef	F_SETLK_REMOTE
147 	{ BSM_F_SETLK_REMOTE,	F_SETLK_REMOTE },
148 #endif
149 
150 #ifdef	F_SETSIG
151 	{ BSM_F_SETSIG,		F_SETSIG },
152 #endif
153 #ifdef	F_GETSIG
154 	{ BSM_F_GETSIG,		F_GETSIG },
155 #endif
156 
157 #ifdef	F_CHKCLEAN
158 	{ BSM_F_CHKCLEAN,	F_CHKCLEAN },
159 #endif
160 #ifdef	F_PREALLOCATE
161 	{ BSM_F_PREALLOCATE,	F_PREALLOCATE },
162 #endif
163 #ifdef	F_SETSIZE
164 	{ BSM_F_SETSIZE,	F_SETSIZE },
165 #endif
166 #ifdef	F_RDADVISE
167 	{ BSM_F_RDADVISE,	F_RDADVISE },
168 #endif
169 #ifdef	F_RDAHEAD
170 	{ BSM_F_RDAHEAD,	F_RDAHEAD },
171 #endif
172 #ifdef	F_READBOOTSTRAP
173 	{ BSM_F_READBOOTSTRAP,	F_READBOOTSTRAP },
174 #endif
175 #ifdef	F_WRITEBOOTSTRAP
176 	{ BSM_F_WRITEBOOTSTRAP,	F_WRITEBOOTSTRAP },
177 #endif
178 #ifdef	F_NOCACHE
179 	{ BSM_F_NOCACHE,	F_NOCACHE },
180 #endif
181 #ifdef	F_LOG2PHYS
182 	{ BSM_F_LOG2PHYS,	F_LOG2PHYS },
183 #endif
184 #ifdef	F_GETPATH
185 	{ BSM_F_GETPATH,	F_GETPATH },
186 #endif
187 #ifdef	F_FULLFSYNC
188 	{ BSM_F_FULLFSYNC,	F_FULLFSYNC },
189 #endif
190 #ifdef	F_PATHPKG_CHECK
191 	{ BSM_F_PATHPKG_CHECK,	F_PATHPKG_CHECK },
192 #endif
193 #ifdef	F_FREEZE_FS
194 	{ BSM_F_FREEZE_FS,	F_FREEZE_FS },
195 #endif
196 #ifdef	F_THAW_FS
197 	{ BSM_F_THAW_FS,	F_THAW_FS },
198 #endif
199 #ifdef	F_GLOBAL_NOCACHE
200 	{ BSM_F_GLOBAL_NOCACHE,	F_GLOBAL_NOCACHE },
201 #endif
202 #ifdef	F_OPENFROM
203 	{ BSM_F_OPENFROM,	F_OPENFROM },
204 #endif
205 #ifdef	F_UNLINKFROM
206 	{ BSM_F_UNLINKFROM,	F_UNLINKFROM },
207 #endif
208 #ifdef	F_CHECK_OPENEVT
209 	{ BSM_F_CHECK_OPENEVT,	F_CHECK_OPENEVT },
210 #endif
211 #ifdef	F_ADDSIGS
212 	{ BSM_F_ADDSIGS,	F_ADDSIGS },
213 #endif
214 #ifdef	F_MARKDEPENDENCY
215 	{ BSM_F_MARKDEPENDENCY,	F_MARKDEPENDENCY },
216 #endif
217 
218 #ifdef	FCNTL_FS_SPECIFIC_BASE
219 	{ BSM_F_FS_SPECIFIC_0,	FCNTL_FS_SPECIFIC_BASE},
220 	{ BSM_F_FS_SPECIFIC_1,	FCNTL_FS_SPECIFIC_BASE + 1},
221 	{ BSM_F_FS_SPECIFIC_2,	FCNTL_FS_SPECIFIC_BASE + 2},
222 	{ BSM_F_FS_SPECIFIC_3,	FCNTL_FS_SPECIFIC_BASE + 3},
223 	{ BSM_F_FS_SPECIFIC_4,	FCNTL_FS_SPECIFIC_BASE + 4},
224 	{ BSM_F_FS_SPECIFIC_5,	FCNTL_FS_SPECIFIC_BASE + 5},
225 	{ BSM_F_FS_SPECIFIC_6,	FCNTL_FS_SPECIFIC_BASE + 6},
226 	{ BSM_F_FS_SPECIFIC_7,	FCNTL_FS_SPECIFIC_BASE + 7},
227 	{ BSM_F_FS_SPECIFIC_8,	FCNTL_FS_SPECIFIC_BASE + 8},
228 	{ BSM_F_FS_SPECIFIC_9,	FCNTL_FS_SPECIFIC_BASE + 9},
229 	{ BSM_F_FS_SPECIFIC_10,	FCNTL_FS_SPECIFIC_BASE + 10},
230 	{ BSM_F_FS_SPECIFIC_11,	FCNTL_FS_SPECIFIC_BASE + 11},
231 	{ BSM_F_FS_SPECIFIC_12,	FCNTL_FS_SPECIFIC_BASE + 12},
232 	{ BSM_F_FS_SPECIFIC_13,	FCNTL_FS_SPECIFIC_BASE + 13},
233 	{ BSM_F_FS_SPECIFIC_14,	FCNTL_FS_SPECIFIC_BASE + 14},
234 	{ BSM_F_FS_SPECIFIC_15,	FCNTL_FS_SPECIFIC_BASE + 15},
235 #endif	/* FCNTL_FS_SPECIFIC_BASE */
236 };
237 static const int bsm_fcntl_cmd_count = sizeof(bsm_fcntl_cmdtab) /
238 	    sizeof(bsm_fcntl_cmdtab[0]);
239 
240 static const bsm_fcntl_cmd_t *
241 bsm_lookup_local_fcntl_cmd(int local_fcntl_cmd)
242 {
243 	int i;
244 
245 	for (i = 0; i < bsm_fcntl_cmd_count; i++) {
246 		if (bsm_fcntl_cmdtab[i].bfc_local_fcntl_cmd ==
247 		    local_fcntl_cmd)
248 			return (&bsm_fcntl_cmdtab[i]);
249 	}
250 	return (NULL);
251 }
252 
253 u_short
254 au_fcntl_cmd_to_bsm(int local_fcntl_cmd)
255 {
256 	const bsm_fcntl_cmd_t *bfcp;
257 
258 	bfcp = bsm_lookup_local_fcntl_cmd(local_fcntl_cmd);
259 	if (bfcp == NULL)
260 		return (BSM_F_UNKNOWN);
261 	return (bfcp->bfc_bsm_fcntl_cmd);
262 }
263 
264 static const bsm_fcntl_cmd_t *
265 bsm_lookup_bsm_fcntl_cmd(u_short bsm_fcntl_cmd)
266 {
267 	int i;
268 
269 	for (i = 0; i < bsm_fcntl_cmd_count; i++) {
270 		if (bsm_fcntl_cmdtab[i].bfc_bsm_fcntl_cmd ==
271 		    bsm_fcntl_cmd)
272 			return (&bsm_fcntl_cmdtab[i]);
273 	}
274 	return (NULL);
275 }
276 
277 int
278 au_bsm_to_fcntl_cmd(u_short bsm_fcntl_cmd, int *local_fcntl_cmdp)
279 {
280 	const bsm_fcntl_cmd_t *bfcp;
281 
282 	bfcp = bsm_lookup_bsm_fcntl_cmd(bsm_fcntl_cmd);
283 	if (bfcp == NULL || bfcp->bfc_local_fcntl_cmd)
284 		return (-1);
285 	*local_fcntl_cmdp = bfcp->bfc_local_fcntl_cmd;
286 	return (0);
287 }
288