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