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