1/*-
2 * Copyright (c) 2008 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 * P4: //depot/projects/trustedbsd/openbsm/libbsm/bsm_errno.c#22
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 */
53struct 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 */
84static 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_ENOTRECOVERABLE,
247#ifdef ENOTRECOVERABLE
248	ENOTRECOVERABLE,
249#else
250	ERRNO_NO_LOCAL_MAPPING,
251#endif
252	ES("Lock is not recoverable") },
253	{ BSM_ERRNO_ENOSTR,
254#ifdef ENOSTR
255	ENOSTR,
256#else
257	ERRNO_NO_LOCAL_MAPPING,
258#endif
259	ES("Device not a stream") },
260	{ BSM_ERRNO_ENONET,
261#ifdef ENONET
262	ENONET,
263#else
264	ERRNO_NO_LOCAL_MAPPING,
265#endif
266	ES("Machine is not on the network") },
267	{ BSM_ERRNO_ENOPKG,
268#ifdef ENOPKG
269	ENOPKG,
270#else
271	ERRNO_NO_LOCAL_MAPPING,
272#endif
273	ES("Package not installed") },
274	{ BSM_ERRNO_EREMOTE, EREMOTE,
275	    ES("Too many levels of remote in path") },
276	{ BSM_ERRNO_ENOLINK,
277#ifdef ENOLINK
278	ENOLINK,
279#else
280	ERRNO_NO_LOCAL_MAPPING,
281#endif
282	ES("Link has been severed") },
283	{ BSM_ERRNO_EADV,
284#ifdef EADV
285	EADV,
286#else
287	ERRNO_NO_LOCAL_MAPPING,
288#endif
289	ES("Advertise error") },
290	{ BSM_ERRNO_ESRMNT,
291#ifdef ESRMNT
292	ESRMNT,
293#else
294	ERRNO_NO_LOCAL_MAPPING,
295#endif
296	ES("srmount error") },
297	{ BSM_ERRNO_ECOMM,
298#ifdef ECOMM
299	ECOMM,
300#else
301	ERRNO_NO_LOCAL_MAPPING,
302#endif
303	ES("Communication error on send") },
304	{ BSM_ERRNO_EPROTO,
305#ifdef EPROTO
306	EPROTO,
307#else
308	ERRNO_NO_LOCAL_MAPPING,
309#endif
310	ES("Protocol error") },
311	{ BSM_ERRNO_ELOCKUNMAPPED,
312#ifdef ELOCKUNMAPPED
313	ELOCKUNMAPPED,
314#else
315	ERRNO_NO_LOCAL_MAPPING,
316#endif
317	ES("Locked lock was unmapped") },
318	{ BSM_ERRNO_ENOTACTIVE,
319#ifdef ENOTACTIVE
320	ENOTACTIVE,
321#else
322	ERRNO_NO_LOCAL_MAPPING,
323#endif
324	ES("Facility is not active") },
325	{ BSM_ERRNO_EMULTIHOP,
326#ifdef EMULTIHOP
327	EMULTIHOP,
328#else
329	ERRNO_NO_LOCAL_MAPPING,
330#endif
331	ES("Multihop attempted") },
332	{ BSM_ERRNO_EBADMSG,
333#ifdef EBADMSG
334	EBADMSG,
335#else
336	ERRNO_NO_LOCAL_MAPPING,
337#endif
338	ES("Bad message") },
339	{ BSM_ERRNO_ENAMETOOLONG, ENAMETOOLONG, ES("File name too long") },
340	{ BSM_ERRNO_EOVERFLOW, EOVERFLOW,
341	    ES("Value too large to be stored in data type") },
342	{ BSM_ERRNO_ENOTUNIQ,
343#ifdef ENOTUNIQ
344	ENOTUNIQ,
345#else
346	ERRNO_NO_LOCAL_MAPPING,
347#endif
348	ES("Given log name not unique") },
349	{ BSM_ERRNO_EBADFD,
350#ifdef EBADFD
351	EBADFD,
352#else
353	ERRNO_NO_LOCAL_MAPPING,
354#endif
355	ES("Given f.d. invalid for this operation") },
356	{ BSM_ERRNO_EREMCHG,
357#ifdef EREMCHG
358	EREMCHG,
359#else
360	ERRNO_NO_LOCAL_MAPPING,
361#endif
362	ES("Remote address changed") },
363	{ BSM_ERRNO_ELIBACC,
364#ifdef ELIBACC
365	ELIBACC,
366#else
367	ERRNO_NO_LOCAL_MAPPING,
368#endif
369	ES("Can't access a needed shared lib") },
370	{ BSM_ERRNO_ELIBBAD,
371#ifdef ELIBBAD
372	ELIBBAD,
373#else
374	ERRNO_NO_LOCAL_MAPPING,
375#endif
376	ES("Accessing a corrupted shared lib") },
377	{ BSM_ERRNO_ELIBSCN,
378#ifdef ELIBSCN
379	ELIBSCN,
380#else
381	ERRNO_NO_LOCAL_MAPPING,
382#endif
383	ES(".lib section in a.out corrupted") },
384	{ BSM_ERRNO_ELIBMAX,
385#ifdef ELIBMAX
386	ELIBMAX,
387#else
388	ERRNO_NO_LOCAL_MAPPING,
389#endif
390	ES("Attempting to link in too many libs") },
391	{ BSM_ERRNO_ELIBEXEC,
392#ifdef ELIBEXEC
393	ELIBEXEC,
394#else
395	ERRNO_NO_LOCAL_MAPPING,
396#endif
397	ES("Attempting to exec a shared library") },
398	{ BSM_ERRNO_EILSEQ, EILSEQ, ES("Illegal byte sequence") },
399	{ BSM_ERRNO_ENOSYS, ENOSYS, ES("Function not implemented") },
400	{ BSM_ERRNO_ELOOP, ELOOP, ES("Too many levels of symbolic links") },
401	{ BSM_ERRNO_ERESTART,
402#ifdef ERESTART
403	ERESTART,
404#else
405	ERRNO_NO_LOCAL_MAPPING,
406#endif
407	ES("Restart syscall") },
408	{ BSM_ERRNO_ESTRPIPE,
409#ifdef ESTRPIPE
410	ESTRPIPE,
411#else
412	ERRNO_NO_LOCAL_MAPPING,
413#endif
414	ES("If pipe/FIFO, don't sleep in stream head") },
415	{ BSM_ERRNO_ENOTEMPTY, ENOTEMPTY, ES("Directory not empty") },
416	{ BSM_ERRNO_EUSERS, EUSERS, ES("Too many users") },
417	{ BSM_ERRNO_ENOTSOCK, ENOTSOCK,
418	    ES("Socket operation on non-socket") },
419	{ BSM_ERRNO_EDESTADDRREQ, EDESTADDRREQ,
420	    ES("Destination address required") },
421	{ BSM_ERRNO_EMSGSIZE, EMSGSIZE, ES("Message too long") },
422	{ BSM_ERRNO_EPROTOTYPE, EPROTOTYPE,
423	    ES("Protocol wrong type for socket") },
424	{ BSM_ERRNO_ENOPROTOOPT, ENOPROTOOPT, ES("Protocol not available") },
425	{ BSM_ERRNO_EPROTONOSUPPORT, EPROTONOSUPPORT,
426	    ES("Protocol not supported") },
427	{ BSM_ERRNO_ESOCKTNOSUPPORT, ESOCKTNOSUPPORT,
428	    ES("Socket type not supported") },
429	{ BSM_ERRNO_EOPNOTSUPP, EOPNOTSUPP, ES("Operation not supported") },
430	{ BSM_ERRNO_EPFNOSUPPORT, EPFNOSUPPORT,
431	    ES("Protocol family not supported") },
432	{ BSM_ERRNO_EAFNOSUPPORT, EAFNOSUPPORT,
433	    ES("Address family not supported by protocol family") },
434	{ BSM_ERRNO_EADDRINUSE, EADDRINUSE, ES("Address already in use") },
435	{ BSM_ERRNO_EADDRNOTAVAIL, EADDRNOTAVAIL,
436	    ES("Can't assign requested address") },
437	{ BSM_ERRNO_ENETDOWN, ENETDOWN, ES("Network is down") },
438	{ BSM_ERRNO_ENETRESET, ENETRESET,
439	    ES("Network dropped connection on reset") },
440	{ BSM_ERRNO_ECONNABORTED, ECONNABORTED,
441	    ES("Software caused connection abort") },
442	{ BSM_ERRNO_ECONNRESET, ECONNRESET, ES("Connection reset by peer") },
443	{ BSM_ERRNO_ENOBUFS, ENOBUFS, ES("No buffer space available") },
444	{ BSM_ERRNO_EISCONN, EISCONN, ES("Socket is already connected") },
445	{ BSM_ERRNO_ENOTCONN, ENOTCONN, ES("Socket is not connected") },
446	{ BSM_ERRNO_ESHUTDOWN, ESHUTDOWN,
447	    ES("Can't send after socket shutdown") },
448	{ BSM_ERRNO_ETOOMANYREFS, ETOOMANYREFS,
449	    ES("Too many references: can't splice") },
450	{ BSM_ERRNO_ETIMEDOUT, ETIMEDOUT, ES("Operation timed out") },
451	{ BSM_ERRNO_ECONNREFUSED, ECONNREFUSED, ES("Connection refused") },
452	{ BSM_ERRNO_EHOSTDOWN, EHOSTDOWN, ES("Host is down") },
453	{ BSM_ERRNO_EHOSTUNREACH, EHOSTUNREACH, ES("No route to host") },
454	{ BSM_ERRNO_EALREADY, EALREADY, ES("Operation already in progress") },
455	{ BSM_ERRNO_EINPROGRESS, EINPROGRESS,
456	    ES("Operation now in progress") },
457	{ BSM_ERRNO_ESTALE, ESTALE, ES("Stale NFS file handle") },
458	{ BSM_ERRNO_EPROCLIM,
459#ifdef EPROCLIM
460	EPROCLIM,
461#else
462	ERRNO_NO_LOCAL_MAPPING,
463#endif
464	ES("Too many processes") },
465	{ BSM_ERRNO_EBADRPC,
466#ifdef EBADRPC
467	EBADRPC,
468#else
469	ERRNO_NO_LOCAL_MAPPING,
470#endif
471	ES("RPC struct is bad") },
472	{ BSM_ERRNO_ERPCMISMATCH,
473#ifdef ERPCMISMATCH
474	ERPCMISMATCH,
475#else
476	ERRNO_NO_LOCAL_MAPPING,
477#endif
478	ES("RPC version wrong") },
479	{ BSM_ERRNO_EPROGUNAVAIL,
480#ifdef EPROGUNAVAIL
481	EPROGUNAVAIL,
482#else
483	ERRNO_NO_LOCAL_MAPPING,
484#endif
485	ES("RPC prog. not avail") },
486	{ BSM_ERRNO_EPROGMISMATCH,
487#ifdef EPROGMISMATCH
488	EPROGMISMATCH,
489#else
490	ERRNO_NO_LOCAL_MAPPING,
491#endif
492	ES("RPC version wrong") },
493	{ BSM_ERRNO_EPROCUNAVAIL,
494#ifdef EPROCUNAVAIL
495	EPROCUNAVAIL,
496#else
497	ERRNO_NO_LOCAL_MAPPING,
498#endif
499	ES("Bad procedure for program") },
500	{ BSM_ERRNO_EFTYPE,
501#ifdef EFTYPE
502	EFTYPE,
503#else
504	ERRNO_NO_LOCAL_MAPPING,
505#endif
506	ES("Inappropriate file type or format") },
507	{ BSM_ERRNO_EAUTH,
508#ifdef EAUTH
509	EAUTH,
510#else
511	ERRNO_NO_LOCAL_MAPPING,
512#endif
513	ES("Authenticateion error") },
514	{ BSM_ERRNO_ENEEDAUTH,
515#ifdef ENEEDAUTH
516	ENEEDAUTH,
517#else
518	ERRNO_NO_LOCAL_MAPPING,
519#endif
520	ES("Need authenticator") },
521	{ BSM_ERRNO_ENOATTR,
522#ifdef ENOATTR
523	ENOATTR,
524#else
525	ERRNO_NO_LOCAL_MAPPING,
526#endif
527	ES("Attribute not found") },
528	{ BSM_ERRNO_EDOOFUS,
529#ifdef EDOOFUS
530	EDOOFUS,
531#else
532	ERRNO_NO_LOCAL_MAPPING,
533#endif
534	ES("Programming error") },
535	{ BSM_ERRNO_EJUSTRETURN,
536#ifdef EJUSTRETURN
537	EJUSTRETURN,
538#else
539	ERRNO_NO_LOCAL_MAPPING,
540#endif
541	ES("Just return") },
542	{ BSM_ERRNO_ENOIOCTL,
543#ifdef ENOIOCTL
544	ENOIOCTL,
545#else
546	ERRNO_NO_LOCAL_MAPPING,
547#endif
548	ES("ioctl not handled by this layer") },
549	{ BSM_ERRNO_EDIRIOCTL,
550#ifdef EDIRIOCTL
551	EDIRIOCTL,
552#else
553	ERRNO_NO_LOCAL_MAPPING,
554#endif
555	ES("do direct ioctl in GEOM") },
556	{ BSM_ERRNO_EPWROFF,
557#ifdef EPWROFF
558	EPWROFF,
559#else
560	ERRNO_NO_LOCAL_MAPPING,
561#endif
562	ES("Device power is off") },
563	{ BSM_ERRNO_EDEVERR,
564#ifdef EDEVERR
565	EDEVERR,
566#else
567	ERRNO_NO_LOCAL_MAPPING,
568#endif
569	ES("Device error") },
570	{ BSM_ERRNO_EBADEXEC,
571#ifdef EBADEXEC
572	EBADEXEC,
573#else
574	ERRNO_NO_LOCAL_MAPPING,
575#endif
576	ES("Bad executable") },
577	{ BSM_ERRNO_EBADARCH,
578#ifdef EBADARCH
579	EBADARCH,
580#else
581	ERRNO_NO_LOCAL_MAPPING,
582#endif
583	ES("Bad CPU type in executable") },
584	{ BSM_ERRNO_ESHLIBVERS,
585#ifdef ESHLIBVERS
586	ESHLIBVERS,
587#else
588	ERRNO_NO_LOCAL_MAPPING,
589#endif
590	ES("Shared library version mismatch") },
591	{ BSM_ERRNO_EBADMACHO,
592#ifdef EBADMACHO
593	EBADMACHO,
594#else
595	ERRNO_NO_LOCAL_MAPPING,
596#endif
597	ES("Malformed Macho file") },
598	{ BSM_ERRNO_EPOLICY,
599#ifdef EPOLICY
600	EPOLICY,
601#else
602	ERRNO_NO_LOCAL_MAPPING,
603#endif
604	ES("Operation failed by policy") },
605	{ BSM_ERRNO_EDOTDOT,
606#ifdef EDOTDOT
607	EDOTDOT,
608#else
609	ERRNO_NO_LOCAL_MAPPING,
610#endif
611	ES("RFS specific error") },
612	{ BSM_ERRNO_EUCLEAN,
613#ifdef EUCLEAN
614	EUCLEAN,
615#else
616	ERRNO_NO_LOCAL_MAPPING,
617#endif
618	ES("Structure needs cleaning") },
619	{ BSM_ERRNO_ENOTNAM,
620#ifdef ENOTNAM
621	ENOTNAM,
622#else
623	ERRNO_NO_LOCAL_MAPPING,
624#endif
625	ES("Not a XENIX named type file") },
626	{ BSM_ERRNO_ENAVAIL,
627#ifdef ENAVAIL
628	ENAVAIL,
629#else
630	ERRNO_NO_LOCAL_MAPPING,
631#endif
632	ES("No XENIX semaphores available") },
633	{ BSM_ERRNO_EISNAM,
634#ifdef EISNAM
635	EISNAM,
636#else
637	ERRNO_NO_LOCAL_MAPPING,
638#endif
639	ES("Is a named type file") },
640	{ BSM_ERRNO_EREMOTEIO,
641#ifdef EREMOTEIO
642	EREMOTEIO,
643#else
644	ERRNO_NO_LOCAL_MAPPING,
645#endif
646	ES("Remote I/O error") },
647	{ BSM_ERRNO_ENOMEDIUM,
648#ifdef ENOMEDIUM
649	ENOMEDIUM,
650#else
651	ERRNO_NO_LOCAL_MAPPING,
652#endif
653	ES("No medium found") },
654	{ BSM_ERRNO_EMEDIUMTYPE,
655#ifdef EMEDIUMTYPE
656	EMEDIUMTYPE,
657#else
658	ERRNO_NO_LOCAL_MAPPING,
659#endif
660	ES("Wrong medium type") },
661	{ BSM_ERRNO_ENOKEY,
662#ifdef ENOKEY
663	ENOKEY,
664#else
665	ERRNO_NO_LOCAL_MAPPING,
666#endif
667	ES("Required key not available") },
668	{ BSM_ERRNO_EKEYEXPIRED,
669#ifdef EKEYEXPIRED
670	EKEYEXPIRED,
671#else
672	ERRNO_NO_LOCAL_MAPPING,
673#endif
674	ES("Key has expired") },
675	{ BSM_ERRNO_EKEYREVOKED,
676#ifdef EKEYREVOKED
677	EKEYREVOKED,
678#else
679	ERRNO_NO_LOCAL_MAPPING,
680#endif
681	ES("Key has been revoked") },
682	{ BSM_ERRNO_EKEYREJECTED,
683#ifdef EKEYREJECTED
684	EKEYREJECTED,
685#else
686	ERRNO_NO_LOCAL_MAPPING,
687#endif
688	ES("Key was rejected by service") },
689	{ BSM_ERRNO_ENOTCAPABLE,
690#ifdef ENOTCAPABLE
691	ENOTCAPABLE,
692#else
693	ERRNO_NO_LOCAL_MAPPING,
694#endif
695	ES("Capabilities insufficient") },
696	{ BSM_ERRNO_ECAPMODE,
697#ifdef ECAPMODE
698	ECAPMODE,
699#else
700	ERRNO_NO_LOCAL_MAPPING,
701#endif
702	ES("Not permitted in capability mode") },
703};
704static const int bsm_errnos_count = sizeof(bsm_errnos) / sizeof(bsm_errnos[0]);
705
706static const struct bsm_errno *
707bsm_lookup_errno_local(int local_errno)
708{
709	int i;
710
711	for (i = 0; i < bsm_errnos_count; i++) {
712		if (bsm_errnos[i].be_local_errno == local_errno)
713			return (&bsm_errnos[i]);
714	}
715	return (NULL);
716}
717
718/*
719 * Conversion to the BSM errno space isn't allowed to fail; we simply map to
720 * BSM_ERRNO_UNKNOWN and let the remote endpoint deal with it.
721 */
722u_char
723au_errno_to_bsm(int local_errno)
724{
725	const struct bsm_errno *bsme;
726
727	bsme = bsm_lookup_errno_local(local_errno);
728	if (bsme == NULL)
729		return (BSM_ERRNO_UNKNOWN);
730	return (bsme->be_bsm_errno);
731}
732
733static const struct bsm_errno *
734bsm_lookup_errno_bsm(u_char bsm_errno)
735{
736	int i;
737
738	for (i = 0; i < bsm_errnos_count; i++) {
739		if (bsm_errnos[i].be_bsm_errno == bsm_errno)
740			return (&bsm_errnos[i]);
741	}
742	return (NULL);
743}
744
745/*
746 * Converstion from a BSM error to a local error number may fail if either
747 * OpenBSM doesn't recognize the error on the wire, or because there is no
748 * appropriate local mapping.
749 */
750int
751au_bsm_to_errno(u_char bsm_errno, int *errorp)
752{
753	const struct bsm_errno *bsme;
754
755	bsme = bsm_lookup_errno_bsm(bsm_errno);
756	if (bsme == NULL || bsme->be_local_errno == ERRNO_NO_LOCAL_MAPPING)
757		return (-1);
758	*errorp = bsme->be_local_errno;
759	return (0);
760}
761
762#if !defined(KERNEL) && !defined(_KERNEL)
763const char *
764au_strerror(u_char bsm_errno)
765{
766	const struct bsm_errno *bsme;
767
768	bsme = bsm_lookup_errno_bsm(bsm_errno);
769	if (bsme == NULL)
770		return ("Unrecognized BSM error");
771	if (bsme->be_local_errno != ERRNO_NO_LOCAL_MAPPING)
772		return (strerror(bsme->be_local_errno));
773	return (bsme->be_strerror);
774}
775#endif
776