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