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