1d5f4d848609a804d7213cfe1bcffe8a82f1abd6
[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         { EPERM, NT_STATUS_ACCESS_DENIED },
32         { EACCES, NT_STATUS_ACCESS_DENIED },
33         { ENOENT, NT_STATUS_OBJECT_NAME_NOT_FOUND },
34         { ENOTDIR, NT_STATUS_NOT_A_DIRECTORY },
35         { EIO, NT_STATUS_IO_DEVICE_ERROR },
36         { EBADF, NT_STATUS_INVALID_HANDLE },
37         { EINVAL, NT_STATUS_INVALID_PARAMETER },
38         { EEXIST, NT_STATUS_OBJECT_NAME_COLLISION},
39         { ENFILE, NT_STATUS_TOO_MANY_OPENED_FILES },
40         { EMFILE, NT_STATUS_TOO_MANY_OPENED_FILES },
41         { ENOSPC, NT_STATUS_DISK_FULL },
42         { ENOMEM, NT_STATUS_NO_MEMORY },
43         { EISDIR, NT_STATUS_FILE_IS_A_DIRECTORY},
44         { EMLINK, NT_STATUS_TOO_MANY_LINKS },
45         { EINTR,  NT_STATUS_RETRY },
46         { ENOSYS, NT_STATUS_NOT_SUPPORTED },
47 #ifdef ELOOP
48         { ELOOP, NT_STATUS_OBJECT_PATH_NOT_FOUND },
49 #endif
50 #ifdef EFTYPE
51         { EFTYPE, NT_STATUS_OBJECT_PATH_NOT_FOUND },
52 #endif
53 #ifdef EDQUOT
54         { EDQUOT, NT_STATUS_DISK_FULL }, /* Windows apps need this, not NT_STATUS_QUOTA_EXCEEDED */
55 #endif
56 #ifdef ENOTEMPTY
57         { ENOTEMPTY, NT_STATUS_DIRECTORY_NOT_EMPTY },
58 #endif
59 #ifdef EXDEV
60         { EXDEV, NT_STATUS_NOT_SAME_DEVICE },
61 #endif
62 #ifdef EROFS
63         { EROFS, NT_STATUS_ACCESS_DENIED },
64 #endif
65 #ifdef ENAMETOOLONG
66         { ENAMETOOLONG, NT_STATUS_OBJECT_NAME_INVALID },
67 #endif
68 #ifdef EFBIG
69         { EFBIG, NT_STATUS_DISK_FULL },
70 #endif
71 #ifdef ENOBUFS
72         { ENOBUFS, NT_STATUS_INSUFFICIENT_RESOURCES },
73 #endif
74         { EAGAIN, NT_STATUS_NETWORK_BUSY },
75 #ifdef EADDRINUSE
76         { EADDRINUSE, NT_STATUS_ADDRESS_ALREADY_ASSOCIATED},
77 #endif
78 #ifdef ENETUNREACH
79         { ENETUNREACH, NT_STATUS_NETWORK_UNREACHABLE},
80 #endif
81 #ifdef EHOSTUNREACH
82                 { EHOSTUNREACH, NT_STATUS_HOST_UNREACHABLE},
83 #endif
84 #ifdef ECONNREFUSED
85         { ECONNREFUSED, NT_STATUS_CONNECTION_REFUSED},
86 #endif
87 #ifdef ETIMEDOUT
88         { ETIMEDOUT, NT_STATUS_IO_TIMEOUT},
89 #endif
90 #ifdef ECONNABORTED
91         { ECONNABORTED, NT_STATUS_CONNECTION_ABORTED},
92 #endif
93 #ifdef ECONNRESET
94         { ECONNRESET, NT_STATUS_CONNECTION_RESET},
95 #endif
96 #ifdef ENODEV
97         { ENODEV, NT_STATUS_DEVICE_DOES_NOT_EXIST},
98 #endif
99 #ifdef EPIPE
100         { EPIPE, NT_STATUS_PIPE_BROKEN},
101 #endif
102 #ifdef EWOULDBLOCK
103         { EWOULDBLOCK, NT_STATUS_NETWORK_BUSY },
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         { 0, NT_STATUS_OK }
115 };
116
117 /*********************************************************************
118  Map an NT error code from a Unix error code.
119 *********************************************************************/
120
121 NTSTATUS map_nt_error_from_unix(int unix_error)
122 {
123         int i = 0;
124
125         if (unix_error == 0) {
126                 /* we map this to an error, not success, as this
127                    function is only called in an error path. Lots of
128                    our virtualised functions may fail without making a
129                    unix system call that fails (such as when they are
130                    checking for some handle existing), so unix_error
131                    may be unset
132                 */
133                 return NT_STATUS_UNSUCCESSFUL;
134         }
135
136         /* Look through list */
137         while(unix_nt_errmap[i].unix_error != 0) {
138                 if (unix_nt_errmap[i].unix_error == unix_error)
139                         return unix_nt_errmap[i].nt_error;
140                 i++;
141         }
142
143         /* Default return */
144         return NT_STATUS_ACCESS_DENIED;
145 }
146
147 /* Convert a Unix error code to a WERROR. */
148 WERROR unix_to_werror(int unix_error)
149 {
150         return ntstatus_to_werror(map_nt_error_from_unix(unix_error));
151 }
152
153 /* Return a UNIX errno from a NT status code */
154 static const struct {
155         NTSTATUS status;
156         int error;
157 } nt_errno_map[] = {
158         {NT_STATUS_ACCESS_VIOLATION, EACCES},
159         {NT_STATUS_INVALID_HANDLE, EBADF},
160         {NT_STATUS_ACCESS_DENIED, EACCES},
161         {NT_STATUS_OBJECT_NAME_NOT_FOUND, ENOENT},
162         {NT_STATUS_OBJECT_PATH_NOT_FOUND, ENOENT},
163         {NT_STATUS_SHARING_VIOLATION, EBUSY},
164         {NT_STATUS_OBJECT_PATH_INVALID, ENOTDIR},
165         {NT_STATUS_OBJECT_NAME_COLLISION, EEXIST},
166         {NT_STATUS_PATH_NOT_COVERED, ENOENT},
167         {NT_STATUS_UNSUCCESSFUL, EINVAL},
168         {NT_STATUS_NOT_IMPLEMENTED, ENOSYS},
169         {NT_STATUS_IN_PAGE_ERROR, EFAULT}, 
170         {NT_STATUS_BAD_NETWORK_NAME, ENOENT},
171 #ifdef EDQUOT
172         {NT_STATUS_PAGEFILE_QUOTA, EDQUOT},
173         {NT_STATUS_QUOTA_EXCEEDED, EDQUOT},
174         {NT_STATUS_REGISTRY_QUOTA_LIMIT, EDQUOT},
175         {NT_STATUS_LICENSE_QUOTA_EXCEEDED, EDQUOT},
176 #endif
177 #ifdef ETIME
178         {NT_STATUS_TIMER_NOT_CANCELED, ETIME},
179 #endif
180         {NT_STATUS_INVALID_PARAMETER, EINVAL},
181         {NT_STATUS_NO_SUCH_DEVICE, ENODEV},
182         {NT_STATUS_NO_SUCH_FILE, ENOENT},
183 #ifdef ENODATA
184         {NT_STATUS_END_OF_FILE, ENODATA}, 
185 #endif
186 #ifdef ENOMEDIUM
187         {NT_STATUS_NO_MEDIA_IN_DEVICE, ENOMEDIUM}, 
188         {NT_STATUS_NO_MEDIA, ENOMEDIUM},
189 #endif
190         {NT_STATUS_NONEXISTENT_SECTOR, ESPIPE}, 
191         {NT_STATUS_NO_MEMORY, ENOMEM},
192         {NT_STATUS_CONFLICTING_ADDRESSES, EADDRINUSE},
193         {NT_STATUS_NOT_MAPPED_VIEW, EINVAL},
194         {NT_STATUS_UNABLE_TO_FREE_VM, EADDRINUSE},
195         {NT_STATUS_ACCESS_DENIED, EACCES}, 
196         {NT_STATUS_BUFFER_TOO_SMALL, ENOBUFS},
197         {NT_STATUS_WRONG_PASSWORD, EACCES},
198         {NT_STATUS_LOGON_FAILURE, EACCES},
199         {NT_STATUS_INVALID_WORKSTATION, EACCES},
200         {NT_STATUS_INVALID_LOGON_HOURS, EACCES},
201         {NT_STATUS_PASSWORD_EXPIRED, EACCES},
202         {NT_STATUS_ACCOUNT_DISABLED, EACCES},
203         {NT_STATUS_DISK_FULL, ENOSPC},
204         {NT_STATUS_INVALID_PIPE_STATE, EPIPE},
205         {NT_STATUS_PIPE_BUSY, EPIPE},
206         {NT_STATUS_PIPE_DISCONNECTED, EPIPE},
207         {NT_STATUS_PIPE_NOT_AVAILABLE, ENOSYS},
208         {NT_STATUS_FILE_IS_A_DIRECTORY, EISDIR},
209         {NT_STATUS_NOT_SUPPORTED, ENOSYS},
210         {NT_STATUS_NOT_A_DIRECTORY, ENOTDIR},
211         {NT_STATUS_DIRECTORY_NOT_EMPTY, ENOTEMPTY},
212         {NT_STATUS_NETWORK_UNREACHABLE, ENETUNREACH},
213         {NT_STATUS_HOST_UNREACHABLE, EHOSTUNREACH},
214         {NT_STATUS_CONNECTION_ABORTED, ECONNABORTED},
215         {NT_STATUS_CONNECTION_REFUSED, ECONNREFUSED},
216         {NT_STATUS_TOO_MANY_LINKS, EMLINK},
217         {NT_STATUS_NETWORK_BUSY, EBUSY},
218         {NT_STATUS_DEVICE_DOES_NOT_EXIST, ENODEV},
219 #ifdef ELIBACC
220         {NT_STATUS_DLL_NOT_FOUND, ELIBACC},
221 #endif
222         {NT_STATUS_PIPE_BROKEN, EPIPE},
223         {NT_STATUS_REMOTE_NOT_LISTENING, ECONNREFUSED},
224         {NT_STATUS_NETWORK_ACCESS_DENIED, EACCES},
225         {NT_STATUS_TOO_MANY_OPENED_FILES, EMFILE},
226 #ifdef EPROTO
227         {NT_STATUS_DEVICE_PROTOCOL_ERROR, EPROTO},
228 #endif
229         {NT_STATUS_FLOAT_OVERFLOW, ERANGE},
230         {NT_STATUS_FLOAT_UNDERFLOW, ERANGE},
231         {NT_STATUS_INTEGER_OVERFLOW, ERANGE},
232         {NT_STATUS_MEDIA_WRITE_PROTECTED, EROFS},
233         {NT_STATUS_PIPE_CONNECTED, EISCONN},
234         {NT_STATUS_MEMORY_NOT_ALLOCATED, EFAULT},
235         {NT_STATUS_FLOAT_INEXACT_RESULT, ERANGE},
236         {NT_STATUS_ILL_FORMED_PASSWORD, EACCES},
237         {NT_STATUS_PASSWORD_RESTRICTION, EACCES},
238         {NT_STATUS_ACCOUNT_RESTRICTION, EACCES},
239         {NT_STATUS_PORT_CONNECTION_REFUSED, ECONNREFUSED},
240         {NT_STATUS_NAME_TOO_LONG, ENAMETOOLONG},
241         {NT_STATUS_REMOTE_DISCONNECT, ESHUTDOWN},
242         {NT_STATUS_CONNECTION_DISCONNECTED, ECONNABORTED},
243         {NT_STATUS_CONNECTION_RESET, ENETRESET},
244 #ifdef ENOTUNIQ
245         {NT_STATUS_IP_ADDRESS_CONFLICT1, ENOTUNIQ},
246         {NT_STATUS_IP_ADDRESS_CONFLICT2, ENOTUNIQ},
247 #endif
248         {NT_STATUS_PORT_MESSAGE_TOO_LONG, EMSGSIZE},
249         {NT_STATUS_PROTOCOL_UNREACHABLE, ENOPROTOOPT},
250         {NT_STATUS_ADDRESS_ALREADY_EXISTS, EADDRINUSE},
251         {NT_STATUS_PORT_UNREACHABLE, EHOSTUNREACH},
252         {NT_STATUS_IO_TIMEOUT, ETIMEDOUT},
253         {NT_STATUS_RETRY, EAGAIN},
254 #ifdef ENOTUNIQ
255         {NT_STATUS_DUPLICATE_NAME, ENOTUNIQ},
256 #endif
257 #ifdef ECOMM
258         {NT_STATUS_NET_WRITE_FAULT, ECOMM},
259 #endif
260 #ifdef EXDEV
261         {NT_STATUS_NOT_SAME_DEVICE, EXDEV},
262 #endif
263         {NT_STATUS(0), 0}
264 };
265
266 int map_errno_from_nt_status(NTSTATUS status)
267 {
268         int i;
269         DEBUG(10,("map_errno_from_nt_status: 32 bit codes: code=%08x\n",
270                 NT_STATUS_V(status)));
271
272         /* Status codes without this bit set are not errors */
273
274         if (!(NT_STATUS_V(status) & 0xc0000000)) {
275                 return 0;
276         }
277
278         for (i=0;nt_errno_map[i].error;i++) {
279                 if (NT_STATUS_V(nt_errno_map[i].status) ==
280                             NT_STATUS_V(status)) {
281                         return nt_errno_map[i].error;
282                 }
283         }
284
285         /* for all other cases - a default code */
286         return EINVAL;
287 }