xref: /freebsd/sys/security/audit/bsm_errno.c (revision d0b2dbfa0ecf2bbc9709efc5e20baf8e4b44bbbf)
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright (c) 2008 Apple Inc.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1.  Redistributions of source code must retain the above copyright
11  *     notice, this list of conditions and the following disclaimer.
12  * 2.  Redistributions in binary form must reproduce the above copyright
13  *     notice, this list of conditions and the following disclaimer in the
14  *     documentation and/or other materials provided with the distribution.
15  * 3.  Neither the name of Apple Inc. ("Apple") nor the names of
16  *     its contributors may be used to endorse or promote products derived
17  *     from this software without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR
23  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
27  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
28  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 #include <sys/cdefs.h>
33 #include <sys/param.h>
34 
35 #include <security/audit/audit.h>
36 
37 #include <bsm/audit_errno.h>
38 #include <bsm/audit_record.h>
39 
40 #include <sys/errno.h>
41 
42 /*
43  * Different operating systems use different numeric constants for different
44  * error numbers, and sometimes error numbers don't exist in more than one
45  * operating system.  These routines convert between BSM and local error
46  * number spaces, subject to the above realities.  BSM error numbers are
47  * stored in a single 8-bit character, so don't have a byte order.
48  *
49  * Don't include string definitions when this code is compiled into a kernel.
50  */
51 struct bsm_errno {
52 	int		 be_bsm_errno;
53 	int		 be_local_errno;
54 #if !defined(KERNEL) && !defined(_KERNEL)
55 	const char	*be_strerror;
56 #endif
57 };
58 
59 #define	ERRNO_NO_LOCAL_MAPPING	-600
60 
61 #if !defined(KERNEL) && !defined(_KERNEL)
62 #define	ES(x)	x
63 #else
64 #define	ES(x)
65 #endif
66 
67 /*
68  * Mapping table -- please maintain in numeric sorted order with respect to
69  * the BSM constant.  Today we do a linear lookup, but could switch to a
70  * binary search if it makes sense.  We only ifdef errors that aren't
71  * generally available, but it does make the table a lot more ugly.
72  *
73  * XXXRW: It would be nice to have a similar ordered table mapping to BSM
74  * constant from local constant, but the order of local constants varies by
75  * OS.  Really we need to build that table at compile-time but don't do that
76  * yet.
77  *
78  * XXXRW: We currently embed English-language error strings here, but should
79  * support catalogues; these are only used if the OS doesn't have an error
80  * string using strerror(3).
81  */
82 static const struct bsm_errno bsm_errnos[] = {
83 	{ BSM_ERRNO_ESUCCESS, 0, ES("Success") },
84 	{ BSM_ERRNO_EPERM, EPERM, ES("Operation not permitted") },
85 	{ BSM_ERRNO_ENOENT, ENOENT, ES("No such file or directory") },
86 	{ BSM_ERRNO_ESRCH, ESRCH, ES("No such process") },
87 	{ BSM_ERRNO_EINTR, EINTR, ES("Interrupted system call") },
88 	{ BSM_ERRNO_EIO, EIO, ES("Input/output error") },
89 	{ BSM_ERRNO_ENXIO, ENXIO, ES("Device not configured") },
90 	{ BSM_ERRNO_E2BIG, E2BIG, ES("Argument list too long") },
91 	{ BSM_ERRNO_ENOEXEC, ENOEXEC, ES("Exec format error") },
92 	{ BSM_ERRNO_EBADF, EBADF, ES("Bad file descriptor") },
93 	{ BSM_ERRNO_ECHILD, ECHILD, ES("No child processes") },
94 	{ BSM_ERRNO_EAGAIN, EAGAIN, ES("Resource temporarily unavailable") },
95 	{ BSM_ERRNO_ENOMEM, ENOMEM, ES("Cannot allocate memory") },
96 	{ BSM_ERRNO_EACCES, EACCES, ES("Permission denied") },
97 	{ BSM_ERRNO_EFAULT, EFAULT, ES("Bad address") },
98 	{ BSM_ERRNO_ENOTBLK, ENOTBLK, ES("Block device required") },
99 	{ BSM_ERRNO_EBUSY, EBUSY, ES("Device busy") },
100 	{ BSM_ERRNO_EEXIST, EEXIST, ES("File exists") },
101 	{ BSM_ERRNO_EXDEV, EXDEV, ES("Cross-device link") },
102 	{ BSM_ERRNO_ENODEV, ENODEV, ES("Operation not supported by device") },
103 	{ BSM_ERRNO_ENOTDIR, ENOTDIR, ES("Not a directory") },
104 	{ BSM_ERRNO_EISDIR, EISDIR, ES("Is a directory") },
105 	{ BSM_ERRNO_EINVAL, EINVAL, ES("Invalid argument") },
106 	{ BSM_ERRNO_ENFILE, ENFILE, ES("Too many open files in system") },
107 	{ BSM_ERRNO_EMFILE, EMFILE, ES("Too many open files") },
108 	{ BSM_ERRNO_ENOTTY, ENOTTY, ES("Inappropriate ioctl for device") },
109 	{ BSM_ERRNO_ETXTBSY, ETXTBSY, ES("Text file busy") },
110 	{ BSM_ERRNO_EFBIG, EFBIG, ES("File too large") },
111 	{ BSM_ERRNO_ENOSPC, ENOSPC, ES("No space left on device") },
112 	{ BSM_ERRNO_ESPIPE, ESPIPE, ES("Illegal seek") },
113 	{ BSM_ERRNO_EROFS, EROFS, ES("Read-only file system") },
114 	{ BSM_ERRNO_EMLINK, EMLINK, ES("Too many links") },
115 	{ BSM_ERRNO_EPIPE, EPIPE, ES("Broken pipe") },
116 	{ BSM_ERRNO_EDOM, EDOM, ES("Numerical argument out of domain") },
117 	{ BSM_ERRNO_ERANGE, ERANGE, ES("Result too large") },
118 	{ BSM_ERRNO_ENOMSG, ENOMSG, ES("No message of desired type") },
119 	{ BSM_ERRNO_EIDRM, EIDRM, ES("Identifier removed") },
120 	{ BSM_ERRNO_ECHRNG,
121 #ifdef ECHRNG
122 	ECHRNG,
123 #else
124 	ERRNO_NO_LOCAL_MAPPING,
125 #endif
126 	ES("Channel number out of range") },
127 	{ BSM_ERRNO_EL2NSYNC,
128 #ifdef EL2NSYNC
129 	EL2NSYNC,
130 #else
131 	ERRNO_NO_LOCAL_MAPPING,
132 #endif
133 	ES("Level 2 not synchronized") },
134 	{ BSM_ERRNO_EL3HLT,
135 #ifdef EL3HLT
136 	EL3HLT,
137 #else
138 	ERRNO_NO_LOCAL_MAPPING,
139 #endif
140 	ES("Level 3 halted") },
141 	{ BSM_ERRNO_EL3RST,
142 #ifdef EL3RST
143 	EL3RST,
144 #else
145 	ERRNO_NO_LOCAL_MAPPING,
146 #endif
147 	ES("Level 3 reset") },
148 	{ BSM_ERRNO_ELNRNG,
149 #ifdef ELNRNG
150 	ELNRNG,
151 #else
152 	ERRNO_NO_LOCAL_MAPPING,
153 #endif
154 	ES("Link number out of range") },
155 	{ BSM_ERRNO_EUNATCH,
156 #ifdef EUNATCH
157 	EUNATCH,
158 #else
159 	ERRNO_NO_LOCAL_MAPPING,
160 #endif
161 	ES("Protocol driver not attached") },
162 	{ BSM_ERRNO_ENOCSI,
163 #ifdef ENOCSI
164 	ENOCSI,
165 #else
166 	ERRNO_NO_LOCAL_MAPPING,
167 #endif
168 	ES("No CSI structure available") },
169 	{ BSM_ERRNO_EL2HLT,
170 #ifdef EL2HLT
171 	EL2HLT,
172 #else
173 	ERRNO_NO_LOCAL_MAPPING,
174 #endif
175 	ES("Level 2 halted") },
176 	{ BSM_ERRNO_EDEADLK, EDEADLK, ES("Resource deadlock avoided") },
177 	{ BSM_ERRNO_ENOLCK, ENOLCK, ES("No locks available") },
178 	{ BSM_ERRNO_ECANCELED, ECANCELED, ES("Operation canceled") },
179 	{ BSM_ERRNO_ENOTSUP, ENOTSUP, ES("Operation not supported") },
180 	{ BSM_ERRNO_EDQUOT, EDQUOT, ES("Disc quota exceeded") },
181 	{ BSM_ERRNO_EBADE,
182 #ifdef EBADE
183 	EBADE,
184 #else
185 	ERRNO_NO_LOCAL_MAPPING,
186 #endif
187 	ES("Invalid exchange") },
188 	{ BSM_ERRNO_EBADR,
189 #ifdef EBADR
190 	EBADR,
191 #else
192 	ERRNO_NO_LOCAL_MAPPING,
193 #endif
194 	ES("Invalid request descriptor") },
195 	{ BSM_ERRNO_EXFULL,
196 #ifdef EXFULL
197 	EXFULL,
198 #else
199 	ERRNO_NO_LOCAL_MAPPING,
200 #endif
201 	ES("Exchange full") },
202 	{ BSM_ERRNO_ENOANO,
203 #ifdef ENOANO
204 	ENOANO,
205 #else
206 	ERRNO_NO_LOCAL_MAPPING,
207 #endif
208 	ES("No anode") },
209 	{ BSM_ERRNO_EBADRQC,
210 #ifdef EBADRQC
211 	EBADRQC,
212 #else
213 	ERRNO_NO_LOCAL_MAPPING,
214 #endif
215 	ES("Invalid request descriptor") },
216 	{ BSM_ERRNO_EBADSLT,
217 #ifdef EBADSLT
218 	EBADSLT,
219 #else
220 	ERRNO_NO_LOCAL_MAPPING,
221 #endif
222 	ES("Invalid slot") },
223 	{ BSM_ERRNO_EDEADLOCK,
224 #ifdef EDEADLOCK
225 	EDEADLOCK,
226 #else
227 	ERRNO_NO_LOCAL_MAPPING,
228 #endif
229 	ES("Resource deadlock avoided") },
230 	{ BSM_ERRNO_EBFONT,
231 #ifdef EBFONT
232 	EBFONT,
233 #else
234 	ERRNO_NO_LOCAL_MAPPING,
235 #endif
236 	ES("Bad font file format") },
237 	{ BSM_ERRNO_EOWNERDEAD,
238 #ifdef EOWNERDEAD
239 	EOWNERDEAD,
240 #else
241 	ERRNO_NO_LOCAL_MAPPING,
242 #endif
243 	ES("Process died with the lock") },
244 	{ BSM_ERRNO_EINTEGRITY,
245 #ifdef EINTEGRITY
246 	EINTEGRITY,
247 #else
248 	ERRNO_NO_LOCAL_MAPPING,
249 #endif
250 	ES("Integrity check failed") },
251 	{ BSM_ERRNO_ENOTRECOVERABLE,
252 #ifdef ENOTRECOVERABLE
253 	ENOTRECOVERABLE,
254 #else
255 	ERRNO_NO_LOCAL_MAPPING,
256 #endif
257 	ES("Lock is not recoverable") },
258 	{ BSM_ERRNO_ENOSTR,
259 #ifdef ENOSTR
260 	ENOSTR,
261 #else
262 	ERRNO_NO_LOCAL_MAPPING,
263 #endif
264 	ES("Device not a stream") },
265 	{ BSM_ERRNO_ENONET,
266 #ifdef ENONET
267 	ENONET,
268 #else
269 	ERRNO_NO_LOCAL_MAPPING,
270 #endif
271 	ES("Machine is not on the network") },
272 	{ BSM_ERRNO_ENOPKG,
273 #ifdef ENOPKG
274 	ENOPKG,
275 #else
276 	ERRNO_NO_LOCAL_MAPPING,
277 #endif
278 	ES("Package not installed") },
279 	{ BSM_ERRNO_EREMOTE, EREMOTE,
280 	    ES("Too many levels of remote in path") },
281 	{ BSM_ERRNO_ENOLINK,
282 #ifdef ENOLINK
283 	ENOLINK,
284 #else
285 	ERRNO_NO_LOCAL_MAPPING,
286 #endif
287 	ES("Link has been severed") },
288 	{ BSM_ERRNO_EADV,
289 #ifdef EADV
290 	EADV,
291 #else
292 	ERRNO_NO_LOCAL_MAPPING,
293 #endif
294 	ES("Advertise error") },
295 	{ BSM_ERRNO_ESRMNT,
296 #ifdef ESRMNT
297 	ESRMNT,
298 #else
299 	ERRNO_NO_LOCAL_MAPPING,
300 #endif
301 	ES("srmount error") },
302 	{ BSM_ERRNO_ECOMM,
303 #ifdef ECOMM
304 	ECOMM,
305 #else
306 	ERRNO_NO_LOCAL_MAPPING,
307 #endif
308 	ES("Communication error on send") },
309 	{ BSM_ERRNO_EPROTO,
310 #ifdef EPROTO
311 	EPROTO,
312 #else
313 	ERRNO_NO_LOCAL_MAPPING,
314 #endif
315 	ES("Protocol error") },
316 	{ BSM_ERRNO_ELOCKUNMAPPED,
317 #ifdef ELOCKUNMAPPED
318 	ELOCKUNMAPPED,
319 #else
320 	ERRNO_NO_LOCAL_MAPPING,
321 #endif
322 	ES("Locked lock was unmapped") },
323 	{ BSM_ERRNO_ENOTACTIVE,
324 #ifdef ENOTACTIVE
325 	ENOTACTIVE,
326 #else
327 	ERRNO_NO_LOCAL_MAPPING,
328 #endif
329 	ES("Facility is not active") },
330 	{ BSM_ERRNO_EMULTIHOP,
331 #ifdef EMULTIHOP
332 	EMULTIHOP,
333 #else
334 	ERRNO_NO_LOCAL_MAPPING,
335 #endif
336 	ES("Multihop attempted") },
337 	{ BSM_ERRNO_EBADMSG,
338 #ifdef EBADMSG
339 	EBADMSG,
340 #else
341 	ERRNO_NO_LOCAL_MAPPING,
342 #endif
343 	ES("Bad message") },
344 	{ BSM_ERRNO_ENAMETOOLONG, ENAMETOOLONG, ES("File name too long") },
345 	{ BSM_ERRNO_EOVERFLOW, EOVERFLOW,
346 	    ES("Value too large to be stored in data type") },
347 	{ BSM_ERRNO_ENOTUNIQ,
348 #ifdef ENOTUNIQ
349 	ENOTUNIQ,
350 #else
351 	ERRNO_NO_LOCAL_MAPPING,
352 #endif
353 	ES("Given log name not unique") },
354 	{ BSM_ERRNO_EBADFD,
355 #ifdef EBADFD
356 	EBADFD,
357 #else
358 	ERRNO_NO_LOCAL_MAPPING,
359 #endif
360 	ES("Given f.d. invalid for this operation") },
361 	{ BSM_ERRNO_EREMCHG,
362 #ifdef EREMCHG
363 	EREMCHG,
364 #else
365 	ERRNO_NO_LOCAL_MAPPING,
366 #endif
367 	ES("Remote address changed") },
368 	{ BSM_ERRNO_ELIBACC,
369 #ifdef ELIBACC
370 	ELIBACC,
371 #else
372 	ERRNO_NO_LOCAL_MAPPING,
373 #endif
374 	ES("Can't access a needed shared lib") },
375 	{ BSM_ERRNO_ELIBBAD,
376 #ifdef ELIBBAD
377 	ELIBBAD,
378 #else
379 	ERRNO_NO_LOCAL_MAPPING,
380 #endif
381 	ES("Accessing a corrupted shared lib") },
382 	{ BSM_ERRNO_ELIBSCN,
383 #ifdef ELIBSCN
384 	ELIBSCN,
385 #else
386 	ERRNO_NO_LOCAL_MAPPING,
387 #endif
388 	ES(".lib section in a.out corrupted") },
389 	{ BSM_ERRNO_ELIBMAX,
390 #ifdef ELIBMAX
391 	ELIBMAX,
392 #else
393 	ERRNO_NO_LOCAL_MAPPING,
394 #endif
395 	ES("Attempting to link in too many libs") },
396 	{ BSM_ERRNO_ELIBEXEC,
397 #ifdef ELIBEXEC
398 	ELIBEXEC,
399 #else
400 	ERRNO_NO_LOCAL_MAPPING,
401 #endif
402 	ES("Attempting to exec a shared library") },
403 	{ BSM_ERRNO_EILSEQ, EILSEQ, ES("Illegal byte sequence") },
404 	{ BSM_ERRNO_ENOSYS, ENOSYS, ES("Function not implemented") },
405 	{ BSM_ERRNO_ELOOP, ELOOP, ES("Too many levels of symbolic links") },
406 	{ BSM_ERRNO_ERESTART,
407 #ifdef ERESTART
408 	ERESTART,
409 #else
410 	ERRNO_NO_LOCAL_MAPPING,
411 #endif
412 	ES("Restart syscall") },
413 	{ BSM_ERRNO_ESTRPIPE,
414 #ifdef ESTRPIPE
415 	ESTRPIPE,
416 #else
417 	ERRNO_NO_LOCAL_MAPPING,
418 #endif
419 	ES("If pipe/FIFO, don't sleep in stream head") },
420 	{ BSM_ERRNO_ENOTEMPTY, ENOTEMPTY, ES("Directory not empty") },
421 	{ BSM_ERRNO_EUSERS, EUSERS, ES("Too many users") },
422 	{ BSM_ERRNO_ENOTSOCK, ENOTSOCK,
423 	    ES("Socket operation on non-socket") },
424 	{ BSM_ERRNO_EDESTADDRREQ, EDESTADDRREQ,
425 	    ES("Destination address required") },
426 	{ BSM_ERRNO_EMSGSIZE, EMSGSIZE, ES("Message too long") },
427 	{ BSM_ERRNO_EPROTOTYPE, EPROTOTYPE,
428 	    ES("Protocol wrong type for socket") },
429 	{ BSM_ERRNO_ENOPROTOOPT, ENOPROTOOPT, ES("Protocol not available") },
430 	{ BSM_ERRNO_EPROTONOSUPPORT, EPROTONOSUPPORT,
431 	    ES("Protocol not supported") },
432 	{ BSM_ERRNO_ESOCKTNOSUPPORT, ESOCKTNOSUPPORT,
433 	    ES("Socket type not supported") },
434 	{ BSM_ERRNO_EOPNOTSUPP, EOPNOTSUPP, ES("Operation not supported") },
435 	{ BSM_ERRNO_EPFNOSUPPORT, EPFNOSUPPORT,
436 	    ES("Protocol family not supported") },
437 	{ BSM_ERRNO_EAFNOSUPPORT, EAFNOSUPPORT,
438 	    ES("Address family not supported by protocol family") },
439 	{ BSM_ERRNO_EADDRINUSE, EADDRINUSE, ES("Address already in use") },
440 	{ BSM_ERRNO_EADDRNOTAVAIL, EADDRNOTAVAIL,
441 	    ES("Can't assign requested address") },
442 	{ BSM_ERRNO_ENETDOWN, ENETDOWN, ES("Network is down") },
443 	{ BSM_ERRNO_ENETRESET, ENETRESET,
444 	    ES("Network dropped connection on reset") },
445 	{ BSM_ERRNO_ECONNABORTED, ECONNABORTED,
446 	    ES("Software caused connection abort") },
447 	{ BSM_ERRNO_ECONNRESET, ECONNRESET, ES("Connection reset by peer") },
448 	{ BSM_ERRNO_ENOBUFS, ENOBUFS, ES("No buffer space available") },
449 	{ BSM_ERRNO_EISCONN, EISCONN, ES("Socket is already connected") },
450 	{ BSM_ERRNO_ENOTCONN, ENOTCONN, ES("Socket is not connected") },
451 	{ BSM_ERRNO_ESHUTDOWN, ESHUTDOWN,
452 	    ES("Can't send after socket shutdown") },
453 	{ BSM_ERRNO_ETOOMANYREFS, ETOOMANYREFS,
454 	    ES("Too many references: can't splice") },
455 	{ BSM_ERRNO_ETIMEDOUT, ETIMEDOUT, ES("Operation timed out") },
456 	{ BSM_ERRNO_ECONNREFUSED, ECONNREFUSED, ES("Connection refused") },
457 	{ BSM_ERRNO_EHOSTDOWN, EHOSTDOWN, ES("Host is down") },
458 	{ BSM_ERRNO_EHOSTUNREACH, EHOSTUNREACH, ES("No route to host") },
459 	{ BSM_ERRNO_EALREADY, EALREADY, ES("Operation already in progress") },
460 	{ BSM_ERRNO_EINPROGRESS, EINPROGRESS,
461 	    ES("Operation now in progress") },
462 	{ BSM_ERRNO_ESTALE, ESTALE, ES("Stale NFS file handle") },
463 	{ BSM_ERRNO_EPROCLIM,
464 #ifdef EPROCLIM
465 	EPROCLIM,
466 #else
467 	ERRNO_NO_LOCAL_MAPPING,
468 #endif
469 	ES("Too many processes") },
470 	{ BSM_ERRNO_EBADRPC,
471 #ifdef EBADRPC
472 	EBADRPC,
473 #else
474 	ERRNO_NO_LOCAL_MAPPING,
475 #endif
476 	ES("RPC struct is bad") },
477 	{ BSM_ERRNO_ERPCMISMATCH,
478 #ifdef ERPCMISMATCH
479 	ERPCMISMATCH,
480 #else
481 	ERRNO_NO_LOCAL_MAPPING,
482 #endif
483 	ES("RPC version wrong") },
484 	{ BSM_ERRNO_EPROGUNAVAIL,
485 #ifdef EPROGUNAVAIL
486 	EPROGUNAVAIL,
487 #else
488 	ERRNO_NO_LOCAL_MAPPING,
489 #endif
490 	ES("RPC prog. not avail") },
491 	{ BSM_ERRNO_EPROGMISMATCH,
492 #ifdef EPROGMISMATCH
493 	EPROGMISMATCH,
494 #else
495 	ERRNO_NO_LOCAL_MAPPING,
496 #endif
497 	ES("RPC version wrong") },
498 	{ BSM_ERRNO_EPROCUNAVAIL,
499 #ifdef EPROCUNAVAIL
500 	EPROCUNAVAIL,
501 #else
502 	ERRNO_NO_LOCAL_MAPPING,
503 #endif
504 	ES("Bad procedure for program") },
505 	{ BSM_ERRNO_EFTYPE,
506 #ifdef EFTYPE
507 	EFTYPE,
508 #else
509 	ERRNO_NO_LOCAL_MAPPING,
510 #endif
511 	ES("Inappropriate file type or format") },
512 	{ BSM_ERRNO_EAUTH,
513 #ifdef EAUTH
514 	EAUTH,
515 #else
516 	ERRNO_NO_LOCAL_MAPPING,
517 #endif
518 	ES("Authenticateion error") },
519 	{ BSM_ERRNO_ENEEDAUTH,
520 #ifdef ENEEDAUTH
521 	ENEEDAUTH,
522 #else
523 	ERRNO_NO_LOCAL_MAPPING,
524 #endif
525 	ES("Need authenticator") },
526 	{ BSM_ERRNO_ENOATTR,
527 #ifdef ENOATTR
528 	ENOATTR,
529 #else
530 	ERRNO_NO_LOCAL_MAPPING,
531 #endif
532 	ES("Attribute not found") },
533 	{ BSM_ERRNO_EDOOFUS,
534 #ifdef EDOOFUS
535 	EDOOFUS,
536 #else
537 	ERRNO_NO_LOCAL_MAPPING,
538 #endif
539 	ES("Programming error") },
540 	{ BSM_ERRNO_EJUSTRETURN,
541 #ifdef EJUSTRETURN
542 	EJUSTRETURN,
543 #else
544 	ERRNO_NO_LOCAL_MAPPING,
545 #endif
546 	ES("Just return") },
547 	{ BSM_ERRNO_ENOIOCTL,
548 #ifdef ENOIOCTL
549 	ENOIOCTL,
550 #else
551 	ERRNO_NO_LOCAL_MAPPING,
552 #endif
553 	ES("ioctl not handled by this layer") },
554 	{ BSM_ERRNO_EDIRIOCTL,
555 #ifdef EDIRIOCTL
556 	EDIRIOCTL,
557 #else
558 	ERRNO_NO_LOCAL_MAPPING,
559 #endif
560 	ES("do direct ioctl in GEOM") },
561 	{ BSM_ERRNO_EPWROFF,
562 #ifdef EPWROFF
563 	EPWROFF,
564 #else
565 	ERRNO_NO_LOCAL_MAPPING,
566 #endif
567 	ES("Device power is off") },
568 	{ BSM_ERRNO_EDEVERR,
569 #ifdef EDEVERR
570 	EDEVERR,
571 #else
572 	ERRNO_NO_LOCAL_MAPPING,
573 #endif
574 	ES("Device error") },
575 	{ BSM_ERRNO_EBADEXEC,
576 #ifdef EBADEXEC
577 	EBADEXEC,
578 #else
579 	ERRNO_NO_LOCAL_MAPPING,
580 #endif
581 	ES("Bad executable") },
582 	{ BSM_ERRNO_EBADARCH,
583 #ifdef EBADARCH
584 	EBADARCH,
585 #else
586 	ERRNO_NO_LOCAL_MAPPING,
587 #endif
588 	ES("Bad CPU type in executable") },
589 	{ BSM_ERRNO_ESHLIBVERS,
590 #ifdef ESHLIBVERS
591 	ESHLIBVERS,
592 #else
593 	ERRNO_NO_LOCAL_MAPPING,
594 #endif
595 	ES("Shared library version mismatch") },
596 	{ BSM_ERRNO_EBADMACHO,
597 #ifdef EBADMACHO
598 	EBADMACHO,
599 #else
600 	ERRNO_NO_LOCAL_MAPPING,
601 #endif
602 	ES("Malformed Macho file") },
603 	{ BSM_ERRNO_EPOLICY,
604 #ifdef EPOLICY
605 	EPOLICY,
606 #else
607 	ERRNO_NO_LOCAL_MAPPING,
608 #endif
609 	ES("Operation failed by policy") },
610 	{ BSM_ERRNO_EDOTDOT,
611 #ifdef EDOTDOT
612 	EDOTDOT,
613 #else
614 	ERRNO_NO_LOCAL_MAPPING,
615 #endif
616 	ES("RFS specific error") },
617 	{ BSM_ERRNO_EUCLEAN,
618 #ifdef EUCLEAN
619 	EUCLEAN,
620 #else
621 	ERRNO_NO_LOCAL_MAPPING,
622 #endif
623 	ES("Structure needs cleaning") },
624 	{ BSM_ERRNO_ENOTNAM,
625 #ifdef ENOTNAM
626 	ENOTNAM,
627 #else
628 	ERRNO_NO_LOCAL_MAPPING,
629 #endif
630 	ES("Not a XENIX named type file") },
631 	{ BSM_ERRNO_ENAVAIL,
632 #ifdef ENAVAIL
633 	ENAVAIL,
634 #else
635 	ERRNO_NO_LOCAL_MAPPING,
636 #endif
637 	ES("No XENIX semaphores available") },
638 	{ BSM_ERRNO_EISNAM,
639 #ifdef EISNAM
640 	EISNAM,
641 #else
642 	ERRNO_NO_LOCAL_MAPPING,
643 #endif
644 	ES("Is a named type file") },
645 	{ BSM_ERRNO_EREMOTEIO,
646 #ifdef EREMOTEIO
647 	EREMOTEIO,
648 #else
649 	ERRNO_NO_LOCAL_MAPPING,
650 #endif
651 	ES("Remote I/O error") },
652 	{ BSM_ERRNO_ENOMEDIUM,
653 #ifdef ENOMEDIUM
654 	ENOMEDIUM,
655 #else
656 	ERRNO_NO_LOCAL_MAPPING,
657 #endif
658 	ES("No medium found") },
659 	{ BSM_ERRNO_EMEDIUMTYPE,
660 #ifdef EMEDIUMTYPE
661 	EMEDIUMTYPE,
662 #else
663 	ERRNO_NO_LOCAL_MAPPING,
664 #endif
665 	ES("Wrong medium type") },
666 	{ BSM_ERRNO_ENOKEY,
667 #ifdef ENOKEY
668 	ENOKEY,
669 #else
670 	ERRNO_NO_LOCAL_MAPPING,
671 #endif
672 	ES("Required key not available") },
673 	{ BSM_ERRNO_EKEYEXPIRED,
674 #ifdef EKEYEXPIRED
675 	EKEYEXPIRED,
676 #else
677 	ERRNO_NO_LOCAL_MAPPING,
678 #endif
679 	ES("Key has expired") },
680 	{ BSM_ERRNO_EKEYREVOKED,
681 #ifdef EKEYREVOKED
682 	EKEYREVOKED,
683 #else
684 	ERRNO_NO_LOCAL_MAPPING,
685 #endif
686 	ES("Key has been revoked") },
687 	{ BSM_ERRNO_EKEYREJECTED,
688 #ifdef EKEYREJECTED
689 	EKEYREJECTED,
690 #else
691 	ERRNO_NO_LOCAL_MAPPING,
692 #endif
693 	ES("Key was rejected by service") },
694 	{ BSM_ERRNO_ENOTCAPABLE,
695 #ifdef ENOTCAPABLE
696 	ENOTCAPABLE,
697 #else
698 	ERRNO_NO_LOCAL_MAPPING,
699 #endif
700 	ES("Capabilities insufficient") },
701 	{ BSM_ERRNO_ECAPMODE,
702 #ifdef ECAPMODE
703 	ECAPMODE,
704 #else
705 	ERRNO_NO_LOCAL_MAPPING,
706 #endif
707 	ES("Not permitted in capability mode") },
708 };
709 
710 static const struct bsm_errno *
711 bsm_lookup_errno_local(int local_errno)
712 {
713 	int i;
714 
715 	for (i = 0; i < nitems(bsm_errnos); i++) {
716 		if (bsm_errnos[i].be_local_errno == local_errno)
717 			return (&bsm_errnos[i]);
718 	}
719 	return (NULL);
720 }
721 
722 /*
723  * Conversion to the BSM errno space isn't allowed to fail; we simply map to
724  * BSM_ERRNO_UNKNOWN and let the remote endpoint deal with it.
725  */
726 u_char
727 au_errno_to_bsm(int local_errno)
728 {
729 	const struct bsm_errno *bsme;
730 
731 	bsme = bsm_lookup_errno_local(local_errno);
732 	if (bsme == NULL)
733 		return (BSM_ERRNO_UNKNOWN);
734 	return (bsme->be_bsm_errno);
735 }
736 
737 static const struct bsm_errno *
738 bsm_lookup_errno_bsm(u_char bsm_errno)
739 {
740 	int i;
741 
742 	for (i = 0; i < nitems(bsm_errnos); i++) {
743 		if (bsm_errnos[i].be_bsm_errno == bsm_errno)
744 			return (&bsm_errnos[i]);
745 	}
746 	return (NULL);
747 }
748 
749 /*
750  * Converstion from a BSM error to a local error number may fail if either
751  * OpenBSM doesn't recognize the error on the wire, or because there is no
752  * appropriate local mapping.
753  */
754 int
755 au_bsm_to_errno(u_char bsm_errno, int *errorp)
756 {
757 	const struct bsm_errno *bsme;
758 
759 	bsme = bsm_lookup_errno_bsm(bsm_errno);
760 	if (bsme == NULL || bsme->be_local_errno == ERRNO_NO_LOCAL_MAPPING)
761 		return (-1);
762 	*errorp = bsme->be_local_errno;
763 	return (0);
764 }
765 
766 #if !defined(KERNEL) && !defined(_KERNEL)
767 const char *
768 au_strerror(u_char bsm_errno)
769 {
770 	const struct bsm_errno *bsme;
771 
772 	bsme = bsm_lookup_errno_bsm(bsm_errno);
773 	if (bsme == NULL)
774 		return ("Unrecognized BSM error");
775 	if (bsme->be_local_errno != ERRNO_NO_LOCAL_MAPPING)
776 		return (strerror(bsme->be_local_errno));
777 	return (bsme->be_strerror);
778 }
779 #endif
780