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