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