s3-dbwrap: Move "lock_order" initialization to db_open_xx
[samba.git] / source3 / lib / errmap_unix.c
1 /* 
2  *  Unix SMB/CIFS implementation.
3  *  map unix to NT errors, an excerpt of libsmb/errormap.c
4  *  Copyright (C) Andrew Tridgell 2001
5  *  Copyright (C) Andrew Bartlett 2001
6  *  Copyright (C) Tim Potter 2000
7  *  Copyright (C) Jeremy Allison 2007
8  *  
9  *  This program is free software; you can redistribute it and/or modify
10  *  it under the terms of the GNU General Public License as published by
11  *  the Free Software Foundation; either version 3 of the License, or
12  *  (at your option) any later version.
13  *  
14  *  This program is distributed in the hope that it will be useful,
15  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
16  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  *  GNU General Public License for more details.
18  *  
19  *  You should have received a copy of the GNU General Public License
20  *  along with this program; if not, see <http://www.gnu.org/licenses/>.
21  */
22
23 #include "includes.h"
24
25 /* Mapping from Unix, to NT error numbers */
26
27 static const struct {
28         int unix_error;
29         NTSTATUS nt_error;
30 } unix_nt_errmap[] = {
31         { EAGAIN,       NT_STATUS_NETWORK_BUSY },
32         { EINTR,        NT_STATUS_RETRY },
33 #ifdef ENOBUFS
34         { ENOBUFS,      NT_STATUS_INSUFFICIENT_RESOURCES },
35 #endif
36 #ifdef EWOULDBLOCK
37         { EWOULDBLOCK,  NT_STATUS_NETWORK_BUSY },
38 #endif
39         { EPERM,        NT_STATUS_ACCESS_DENIED },
40         { EACCES,       NT_STATUS_ACCESS_DENIED },
41         { ENOENT,       NT_STATUS_OBJECT_NAME_NOT_FOUND },
42         { ENOTDIR,      NT_STATUS_NOT_A_DIRECTORY },
43         { EIO,          NT_STATUS_IO_DEVICE_ERROR },
44         { EBADF,        NT_STATUS_INVALID_HANDLE },
45         { EINVAL,       NT_STATUS_INVALID_PARAMETER },
46         { EEXIST,       NT_STATUS_OBJECT_NAME_COLLISION},
47         { ENFILE,       NT_STATUS_TOO_MANY_OPENED_FILES },
48         { EMFILE,       NT_STATUS_TOO_MANY_OPENED_FILES },
49         { ENOSPC,       NT_STATUS_DISK_FULL },
50         { ENOMEM,       NT_STATUS_NO_MEMORY },
51         { EISDIR,       NT_STATUS_FILE_IS_A_DIRECTORY},
52 #ifdef EPIPE
53         { EPIPE,        NT_STATUS_CONNECTION_DISCONNECTED},
54 #endif
55         { EMLINK,       NT_STATUS_TOO_MANY_LINKS },
56         { ENOSYS,       NT_STATUS_NOT_SUPPORTED },
57 #ifdef ELOOP
58         { ELOOP,        NT_STATUS_OBJECT_PATH_NOT_FOUND },
59 #endif
60 #ifdef EFTYPE
61         { EFTYPE,       NT_STATUS_OBJECT_PATH_NOT_FOUND },
62 #endif
63 #ifdef EDQUOT
64         { EDQUOT,       NT_STATUS_DISK_FULL }, /* Windows apps need this, not NT_STATUS_QUOTA_EXCEEDED */
65 #endif
66 #ifdef ENOTEMPTY
67         { ENOTEMPTY,    NT_STATUS_DIRECTORY_NOT_EMPTY },
68 #endif
69 #ifdef EXDEV
70         { EXDEV,        NT_STATUS_NOT_SAME_DEVICE },
71 #endif
72 #ifdef EROFS
73         { EROFS,        NT_STATUS_ACCESS_DENIED },
74 #endif
75 #ifdef ENAMETOOLONG
76         { ENAMETOOLONG, NT_STATUS_OBJECT_NAME_INVALID },
77 #endif
78 #ifdef EFBIG
79         { EFBIG,        NT_STATUS_DISK_FULL },
80 #endif
81 #ifdef EADDRINUSE
82         { EADDRINUSE,   NT_STATUS_ADDRESS_ALREADY_ASSOCIATED},
83 #endif
84 #ifdef ENETUNREACH
85         { ENETUNREACH,  NT_STATUS_NETWORK_UNREACHABLE},
86 #endif
87 #ifdef EHOSTUNREACH
88         { EHOSTUNREACH, NT_STATUS_HOST_UNREACHABLE},
89 #endif
90 #ifdef ECONNREFUSED
91         { ECONNREFUSED, NT_STATUS_CONNECTION_REFUSED},
92 #endif
93 #ifdef ETIMEDOUT
94         { ETIMEDOUT,    NT_STATUS_IO_TIMEOUT},
95 #endif
96 #ifdef ECONNABORTED
97         { ECONNABORTED, NT_STATUS_CONNECTION_ABORTED},
98 #endif
99 #ifdef ECONNRESET
100         { ECONNRESET,   NT_STATUS_CONNECTION_RESET},
101 #endif
102 #ifdef ENODEV
103         { ENODEV,       NT_STATUS_DEVICE_DOES_NOT_EXIST},
104 #endif
105 #ifdef ENOATTR
106         { ENOATTR,      NT_STATUS_NOT_FOUND },
107 #endif
108 #ifdef ECANCELED
109         { ECANCELED,    NT_STATUS_CANCELLED},
110 #endif
111 #ifdef ENOTSUP
112         { ENOTSUP,      NT_STATUS_NOT_SUPPORTED},
113 #endif
114 };
115
116 /*********************************************************************
117  Map an NT error code from a Unix error code.
118 *********************************************************************/
119
120 NTSTATUS map_nt_error_from_unix(int unix_error)
121 {
122         int i = 0;
123
124         if (unix_error == 0) {
125                 /* we map this to an error, not success, as this
126                    function is only called in an error path. Lots of
127                    our virtualised functions may fail without making a
128                    unix system call that fails (such as when they are
129                    checking for some handle existing), so unix_error
130                    may be unset
131                 */
132                 return NT_STATUS_UNSUCCESSFUL;
133         }
134
135         /* Look through list */
136         for (i=0;i<ARRAY_SIZE(unix_nt_errmap);i++) {
137                 if (unix_nt_errmap[i].unix_error == unix_error) {
138                         return unix_nt_errmap[i].nt_error;
139                 }
140         }
141
142         /* Default return */
143         return NT_STATUS_ACCESS_DENIED;
144 }
145
146 /* Return a UNIX errno from a NT status code */
147 static const struct {
148         NTSTATUS status;
149         int error;
150 } nt_errno_map[] = {
151         {NT_STATUS_ACCESS_VIOLATION, EACCES},
152         {NT_STATUS_INVALID_HANDLE, EBADF},
153         {NT_STATUS_ACCESS_DENIED, EACCES},
154         {NT_STATUS_OBJECT_NAME_NOT_FOUND, ENOENT},
155         {NT_STATUS_OBJECT_PATH_NOT_FOUND, ENOENT},
156         {NT_STATUS_SHARING_VIOLATION, EBUSY},
157         {NT_STATUS_OBJECT_PATH_INVALID, ENOTDIR},
158         {NT_STATUS_OBJECT_NAME_COLLISION, EEXIST},
159         {NT_STATUS_PATH_NOT_COVERED, ENOENT},
160         {NT_STATUS_UNSUCCESSFUL, EINVAL},
161         {NT_STATUS_NOT_IMPLEMENTED, ENOSYS},
162         {NT_STATUS_IN_PAGE_ERROR, EFAULT}, 
163         {NT_STATUS_BAD_NETWORK_NAME, ENOENT},
164 #ifdef EDQUOT
165         {NT_STATUS_PAGEFILE_QUOTA, EDQUOT},
166         {NT_STATUS_QUOTA_EXCEEDED, EDQUOT},
167         {NT_STATUS_REGISTRY_QUOTA_LIMIT, EDQUOT},
168         {NT_STATUS_LICENSE_QUOTA_EXCEEDED, EDQUOT},
169 #endif
170 #ifdef ETIME
171         {NT_STATUS_TIMER_NOT_CANCELED, ETIME},
172 #endif
173         {NT_STATUS_INVALID_PARAMETER, EINVAL},
174         {NT_STATUS_NO_SUCH_DEVICE, ENODEV},
175         {NT_STATUS_NO_SUCH_FILE, ENOENT},
176 #ifdef ENODATA
177         {NT_STATUS_END_OF_FILE, ENODATA}, 
178 #endif
179 #ifdef ENOMEDIUM
180         {NT_STATUS_NO_MEDIA_IN_DEVICE, ENOMEDIUM}, 
181         {NT_STATUS_NO_MEDIA, ENOMEDIUM},
182 #endif
183         {NT_STATUS_NONEXISTENT_SECTOR, ESPIPE}, 
184         {NT_STATUS_NO_MEMORY, ENOMEM},
185         {NT_STATUS_CONFLICTING_ADDRESSES, EADDRINUSE},
186         {NT_STATUS_NOT_MAPPED_VIEW, EINVAL},
187         {NT_STATUS_UNABLE_TO_FREE_VM, EADDRINUSE},
188         {NT_STATUS_ACCESS_DENIED, EACCES}, 
189         {NT_STATUS_BUFFER_TOO_SMALL, ENOBUFS},
190         {NT_STATUS_WRONG_PASSWORD, EACCES},
191         {NT_STATUS_LOGON_FAILURE, EACCES},
192         {NT_STATUS_INVALID_WORKSTATION, EACCES},
193         {NT_STATUS_INVALID_LOGON_HOURS, EACCES},
194         {NT_STATUS_PASSWORD_EXPIRED, EACCES},
195         {NT_STATUS_ACCOUNT_DISABLED, EACCES},
196         {NT_STATUS_DISK_FULL, ENOSPC},
197         {NT_STATUS_INVALID_PIPE_STATE, EPIPE},
198         {NT_STATUS_PIPE_BUSY, EPIPE},
199         {NT_STATUS_PIPE_DISCONNECTED, EPIPE},
200         {NT_STATUS_PIPE_NOT_AVAILABLE, ENOSYS},
201         {NT_STATUS_FILE_IS_A_DIRECTORY, EISDIR},
202         {NT_STATUS_NOT_SUPPORTED, ENOSYS},
203         {NT_STATUS_NOT_A_DIRECTORY, ENOTDIR},
204         {NT_STATUS_DIRECTORY_NOT_EMPTY, ENOTEMPTY},
205         {NT_STATUS_NETWORK_UNREACHABLE, ENETUNREACH},
206         {NT_STATUS_HOST_UNREACHABLE, EHOSTUNREACH},
207         {NT_STATUS_CONNECTION_ABORTED, ECONNABORTED},
208         {NT_STATUS_CONNECTION_REFUSED, ECONNREFUSED},
209         {NT_STATUS_TOO_MANY_LINKS, EMLINK},
210         {NT_STATUS_NETWORK_BUSY, EBUSY},
211         {NT_STATUS_DEVICE_DOES_NOT_EXIST, ENODEV},
212 #ifdef ELIBACC
213         {NT_STATUS_DLL_NOT_FOUND, ELIBACC},
214 #endif
215         {NT_STATUS_PIPE_BROKEN, EPIPE},
216         {NT_STATUS_REMOTE_NOT_LISTENING, ECONNREFUSED},
217         {NT_STATUS_NETWORK_ACCESS_DENIED, EACCES},
218         {NT_STATUS_TOO_MANY_OPENED_FILES, EMFILE},
219 #ifdef EPROTO
220         {NT_STATUS_DEVICE_PROTOCOL_ERROR, EPROTO},
221 #endif
222         {NT_STATUS_FLOAT_OVERFLOW, ERANGE},
223         {NT_STATUS_FLOAT_UNDERFLOW, ERANGE},
224         {NT_STATUS_INTEGER_OVERFLOW, ERANGE},
225         {NT_STATUS_MEDIA_WRITE_PROTECTED, EROFS},
226         {NT_STATUS_PIPE_CONNECTED, EISCONN},
227         {NT_STATUS_MEMORY_NOT_ALLOCATED, EFAULT},
228         {NT_STATUS_FLOAT_INEXACT_RESULT, ERANGE},
229         {NT_STATUS_ILL_FORMED_PASSWORD, EACCES},
230         {NT_STATUS_PASSWORD_RESTRICTION, EACCES},
231         {NT_STATUS_ACCOUNT_RESTRICTION, EACCES},
232         {NT_STATUS_PORT_CONNECTION_REFUSED, ECONNREFUSED},
233         {NT_STATUS_NAME_TOO_LONG, ENAMETOOLONG},
234         {NT_STATUS_REMOTE_DISCONNECT, ESHUTDOWN},
235         {NT_STATUS_CONNECTION_DISCONNECTED, ECONNABORTED},
236         {NT_STATUS_CONNECTION_RESET, ENETRESET},
237 #ifdef ENOTUNIQ
238         {NT_STATUS_IP_ADDRESS_CONFLICT1, ENOTUNIQ},
239         {NT_STATUS_IP_ADDRESS_CONFLICT2, ENOTUNIQ},
240 #endif
241         {NT_STATUS_PORT_MESSAGE_TOO_LONG, EMSGSIZE},
242         {NT_STATUS_PROTOCOL_UNREACHABLE, ENOPROTOOPT},
243         {NT_STATUS_ADDRESS_ALREADY_EXISTS, EADDRINUSE},
244         {NT_STATUS_PORT_UNREACHABLE, EHOSTUNREACH},
245         {NT_STATUS_IO_TIMEOUT, ETIMEDOUT},
246         {NT_STATUS_RETRY, EAGAIN},
247 #ifdef ENOTUNIQ
248         {NT_STATUS_DUPLICATE_NAME, ENOTUNIQ},
249 #endif
250 #ifdef ECOMM
251         {NT_STATUS_NET_WRITE_FAULT, ECOMM},
252 #endif
253 #ifdef EXDEV
254         {NT_STATUS_NOT_SAME_DEVICE, EXDEV},
255 #endif
256 };
257
258 int map_errno_from_nt_status(NTSTATUS status)
259 {
260         int i;
261         DEBUG(10,("map_errno_from_nt_status: 32 bit codes: code=%08x\n",
262                 NT_STATUS_V(status)));
263
264         /* Status codes without this bit set are not errors */
265
266         if (!(NT_STATUS_V(status) & 0xc0000000)) {
267                 return 0;
268         }
269
270         for (i=0;i<ARRAY_SIZE(nt_errno_map);i++) {
271                 if (NT_STATUS_V(nt_errno_map[i].status) ==
272                             NT_STATUS_V(status)) {
273                         return nt_errno_map[i].error;
274                 }
275         }
276
277         /* for all other cases - a default code */
278         return EINVAL;
279 }