r8839: adding patches from Marcin. Have to still work on
[Samba.git] / source / libsmb / clierror.c
blob355a2adf34d998cd86689ace555073fcd3dabc37
1 /*
2 Unix SMB/CIFS implementation.
3 client error handling routines
4 Copyright (C) Andrew Tridgell 1994-1998
5 Copyright (C) Jelmer Vernooij 2003
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 #include "includes.h"
24 /*****************************************************
25 RAP error codes - a small start but will be extended.
27 XXX: Perhaps these should move into a common function because they're
28 duplicated in clirap2.c
30 *******************************************************/
32 static const struct
34 int err;
35 const char *message;
36 } rap_errmap[] =
38 {5, "RAP5: User has insufficient privilege" },
39 {50, "RAP50: Not supported by server" },
40 {65, "RAP65: Access denied" },
41 {86, "RAP86: The specified password is invalid" },
42 {2220, "RAP2220: Group does not exist" },
43 {2221, "RAP2221: User does not exist" },
44 {2226, "RAP2226: Operation only permitted on a Primary Domain Controller" },
45 {2237, "RAP2237: User is not in group" },
46 {2242, "RAP2242: The password of this user has expired." },
47 {2243, "RAP2243: The password of this user cannot change." },
48 {2244, "RAP2244: This password cannot be used now (password history conflict)." },
49 {2245, "RAP2245: The password is shorter than required." },
50 {2246, "RAP2246: The password of this user is too recent to change."},
52 /* these really shouldn't be here ... */
53 {0x80, "Not listening on called name"},
54 {0x81, "Not listening for calling name"},
55 {0x82, "Called name not present"},
56 {0x83, "Called name present, but insufficient resources"},
58 {0, NULL}
59 };
61 /****************************************************************************
62 return a description of an SMB error
63 ****************************************************************************/
64 static const char *cli_smb_errstr(struct cli_state *cli)
66 return smb_dos_errstr(cli->inbuf);
69 /***************************************************************************
70 Return an error message - either an NT error, SMB error or a RAP error.
71 Note some of the NT errors are actually warnings or "informational" errors
72 in which case they can be safely ignored.
73 ****************************************************************************/
75 const char *cli_errstr(struct cli_state *cli)
77 static fstring cli_error_message;
78 uint32 flgs2 = SVAL(cli->inbuf,smb_flg2), errnum;
79 uint8 errclass;
80 int i;
82 if (!cli->initialised) {
83 fstrcpy(cli_error_message, "[Programmer's error] cli_errstr called on unitialized cli_stat struct!\n");
84 return cli_error_message;
87 /* Was it server socket error ? */
88 if (cli->fd == -1 && cli->smb_rw_error) {
89 switch(cli->smb_rw_error) {
90 case READ_TIMEOUT:
91 slprintf(cli_error_message, sizeof(cli_error_message) - 1,
92 "Call timed out: server did not respond after %d milliseconds",
93 cli->timeout);
94 break;
95 case READ_EOF:
96 slprintf(cli_error_message, sizeof(cli_error_message) - 1,
97 "Call returned zero bytes (EOF)" );
98 break;
99 case READ_ERROR:
100 slprintf(cli_error_message, sizeof(cli_error_message) - 1,
101 "Read error: %s", strerror(errno) );
102 break;
103 case WRITE_ERROR:
104 slprintf(cli_error_message, sizeof(cli_error_message) - 1,
105 "Write error: %s", strerror(errno) );
106 break;
107 case READ_BAD_SIG:
108 slprintf(cli_error_message, sizeof(cli_error_message) - 1,
109 "Server packet had invalid SMB signature!");
110 break;
111 default:
112 slprintf(cli_error_message, sizeof(cli_error_message) - 1,
113 "Unknown error code %d\n", cli->smb_rw_error );
114 break;
116 return cli_error_message;
119 /* Case #1: RAP error */
120 if (cli->rap_error) {
121 for (i = 0; rap_errmap[i].message != NULL; i++) {
122 if (rap_errmap[i].err == cli->rap_error) {
123 return rap_errmap[i].message;
127 slprintf(cli_error_message, sizeof(cli_error_message) - 1, "RAP code %d",
128 cli->rap_error);
130 return cli_error_message;
133 /* Case #2: 32-bit NT errors */
134 if (flgs2 & FLAGS2_32_BIT_ERROR_CODES) {
135 NTSTATUS status = NT_STATUS(IVAL(cli->inbuf,smb_rcls));
137 return nt_errstr(status);
140 cli_dos_error(cli, &errclass, &errnum);
142 /* Case #3: SMB error */
144 return cli_smb_errstr(cli);
148 /* Return the 32-bit NT status code from the last packet */
149 NTSTATUS cli_nt_error(struct cli_state *cli)
151 int flgs2 = SVAL(cli->inbuf,smb_flg2);
153 if (!(flgs2 & FLAGS2_32_BIT_ERROR_CODES)) {
154 int e_class = CVAL(cli->inbuf,smb_rcls);
155 int code = SVAL(cli->inbuf,smb_err);
156 return dos_to_ntstatus(e_class, code);
159 return NT_STATUS(IVAL(cli->inbuf,smb_rcls));
163 /* Return the DOS error from the last packet - an error class and an error
164 code. */
165 void cli_dos_error(struct cli_state *cli, uint8 *eclass, uint32 *ecode)
167 int flgs2;
168 char rcls;
169 int code;
171 if(!cli->initialised) return;
173 flgs2 = SVAL(cli->inbuf,smb_flg2);
175 if (flgs2 & FLAGS2_32_BIT_ERROR_CODES) {
176 NTSTATUS ntstatus = NT_STATUS(IVAL(cli->inbuf, smb_rcls));
177 ntstatus_to_dos(ntstatus, eclass, ecode);
178 return;
181 rcls = CVAL(cli->inbuf,smb_rcls);
182 code = SVAL(cli->inbuf,smb_err);
184 if (eclass) *eclass = rcls;
185 if (ecode) *ecode = code;
188 /* Return a UNIX errno from a dos error class, error number tuple */
190 static int cli_errno_from_dos(uint8 eclass, uint32 num)
192 if (eclass == ERRDOS) {
193 switch (num) {
194 case ERRbadfile: return ENOENT;
195 case ERRbadpath: return ENOTDIR;
196 case ERRnoaccess: return EACCES;
197 case ERRfilexists: return EEXIST;
198 case ERRrename: return EEXIST;
199 case ERRbadshare: return EBUSY;
200 case ERRlock: return EBUSY;
201 case ERRinvalidname: return ENOENT;
202 case ERRnosuchshare: return ENODEV;
206 if (eclass == ERRSRV) {
207 switch (num) {
208 case ERRbadpw: return EPERM;
209 case ERRaccess: return EACCES;
210 case ERRnoresource: return ENOMEM;
211 case ERRinvdevice: return ENODEV;
212 case ERRinvnetname: return ENODEV;
216 /* for other cases */
217 return EINVAL;
220 /* Return a UNIX errno from a NT status code */
221 static struct {
222 NTSTATUS status;
223 int error;
224 } nt_errno_map[] = {
225 {NT_STATUS_ACCESS_VIOLATION, EACCES},
226 {NT_STATUS_INVALID_HANDLE, EBADF},
227 {NT_STATUS_ACCESS_DENIED, EACCES},
228 {NT_STATUS_OBJECT_NAME_NOT_FOUND, ENOENT},
229 {NT_STATUS_SHARING_VIOLATION, EBUSY},
230 {NT_STATUS_OBJECT_PATH_INVALID, ENOTDIR},
231 {NT_STATUS_OBJECT_NAME_COLLISION, EEXIST},
232 {NT_STATUS_PATH_NOT_COVERED, ENOENT},
233 {NT_STATUS_UNSUCCESSFUL, EINVAL},
234 {NT_STATUS_NOT_IMPLEMENTED, ENOSYS},
235 {NT_STATUS_IN_PAGE_ERROR, EFAULT},
236 {NT_STATUS_BAD_NETWORK_NAME, ENOENT},
237 #ifdef EDQUOT
238 {NT_STATUS_PAGEFILE_QUOTA, EDQUOT},
239 {NT_STATUS_QUOTA_EXCEEDED, EDQUOT},
240 {NT_STATUS_REGISTRY_QUOTA_LIMIT, EDQUOT},
241 {NT_STATUS_LICENSE_QUOTA_EXCEEDED, EDQUOT},
242 #endif
243 #ifdef ETIME
244 {NT_STATUS_TIMER_NOT_CANCELED, ETIME},
245 #endif
246 {NT_STATUS_INVALID_PARAMETER, EINVAL},
247 {NT_STATUS_NO_SUCH_DEVICE, ENODEV},
248 {NT_STATUS_NO_SUCH_FILE, ENOENT},
249 #ifdef ENODATA
250 {NT_STATUS_END_OF_FILE, ENODATA},
251 #endif
252 #ifdef ENOMEDIUM
253 {NT_STATUS_NO_MEDIA_IN_DEVICE, ENOMEDIUM},
254 {NT_STATUS_NO_MEDIA, ENOMEDIUM},
255 #endif
256 {NT_STATUS_NONEXISTENT_SECTOR, ESPIPE},
257 {NT_STATUS_NO_MEMORY, ENOMEM},
258 {NT_STATUS_CONFLICTING_ADDRESSES, EADDRINUSE},
259 {NT_STATUS_NOT_MAPPED_VIEW, EINVAL},
260 {NT_STATUS_UNABLE_TO_FREE_VM, EADDRINUSE},
261 {NT_STATUS_ACCESS_DENIED, EACCES},
262 {NT_STATUS_BUFFER_TOO_SMALL, ENOBUFS},
263 {NT_STATUS_WRONG_PASSWORD, EACCES},
264 {NT_STATUS_LOGON_FAILURE, EACCES},
265 {NT_STATUS_INVALID_WORKSTATION, EACCES},
266 {NT_STATUS_INVALID_LOGON_HOURS, EACCES},
267 {NT_STATUS_PASSWORD_EXPIRED, EACCES},
268 {NT_STATUS_ACCOUNT_DISABLED, EACCES},
269 {NT_STATUS_DISK_FULL, ENOSPC},
270 {NT_STATUS_INVALID_PIPE_STATE, EPIPE},
271 {NT_STATUS_PIPE_BUSY, EPIPE},
272 {NT_STATUS_PIPE_DISCONNECTED, EPIPE},
273 {NT_STATUS_PIPE_NOT_AVAILABLE, ENOSYS},
274 {NT_STATUS_FILE_IS_A_DIRECTORY, EISDIR},
275 {NT_STATUS_NOT_SUPPORTED, ENOSYS},
276 {NT_STATUS_NOT_A_DIRECTORY, ENOTDIR},
277 {NT_STATUS_DIRECTORY_NOT_EMPTY, ENOTEMPTY},
278 {NT_STATUS_NETWORK_UNREACHABLE, ENETUNREACH},
279 {NT_STATUS_HOST_UNREACHABLE, EHOSTUNREACH},
280 {NT_STATUS_CONNECTION_ABORTED, ECONNABORTED},
281 {NT_STATUS_CONNECTION_REFUSED, ECONNREFUSED},
282 {NT_STATUS_TOO_MANY_LINKS, EMLINK},
283 {NT_STATUS_NETWORK_BUSY, EBUSY},
284 {NT_STATUS_DEVICE_DOES_NOT_EXIST, ENODEV},
285 #ifdef ELIBACC
286 {NT_STATUS_DLL_NOT_FOUND, ELIBACC},
287 #endif
288 {NT_STATUS_PIPE_BROKEN, EPIPE},
289 {NT_STATUS_REMOTE_NOT_LISTENING, ECONNREFUSED},
290 {NT_STATUS_NETWORK_ACCESS_DENIED, EACCES},
291 {NT_STATUS_TOO_MANY_OPENED_FILES, EMFILE},
292 #ifdef EPROTO
293 {NT_STATUS_DEVICE_PROTOCOL_ERROR, EPROTO},
294 #endif
295 {NT_STATUS_FLOAT_OVERFLOW, ERANGE},
296 {NT_STATUS_FLOAT_UNDERFLOW, ERANGE},
297 {NT_STATUS_INTEGER_OVERFLOW, ERANGE},
298 {NT_STATUS_MEDIA_WRITE_PROTECTED, EROFS},
299 {NT_STATUS_PIPE_CONNECTED, EISCONN},
300 {NT_STATUS_MEMORY_NOT_ALLOCATED, EFAULT},
301 {NT_STATUS_FLOAT_INEXACT_RESULT, ERANGE},
302 {NT_STATUS_ILL_FORMED_PASSWORD, EACCES},
303 {NT_STATUS_PASSWORD_RESTRICTION, EACCES},
304 {NT_STATUS_ACCOUNT_RESTRICTION, EACCES},
305 {NT_STATUS_PORT_CONNECTION_REFUSED, ECONNREFUSED},
306 {NT_STATUS_NAME_TOO_LONG, ENAMETOOLONG},
307 {NT_STATUS_REMOTE_DISCONNECT, ESHUTDOWN},
308 {NT_STATUS_CONNECTION_DISCONNECTED, ECONNABORTED},
309 {NT_STATUS_CONNECTION_RESET, ENETRESET},
310 #ifdef ENOTUNIQ
311 {NT_STATUS_IP_ADDRESS_CONFLICT1, ENOTUNIQ},
312 {NT_STATUS_IP_ADDRESS_CONFLICT2, ENOTUNIQ},
313 #endif
314 {NT_STATUS_PORT_MESSAGE_TOO_LONG, EMSGSIZE},
315 {NT_STATUS_PROTOCOL_UNREACHABLE, ENOPROTOOPT},
316 {NT_STATUS_ADDRESS_ALREADY_EXISTS, EADDRINUSE},
317 {NT_STATUS_PORT_UNREACHABLE, EHOSTUNREACH},
318 {NT_STATUS_IO_TIMEOUT, ETIMEDOUT},
319 {NT_STATUS_RETRY, EAGAIN},
320 #ifdef ENOTUNIQ
321 {NT_STATUS_DUPLICATE_NAME, ENOTUNIQ},
322 #endif
323 #ifdef ECOMM
324 {NT_STATUS_NET_WRITE_FAULT, ECOMM},
325 #endif
327 {NT_STATUS(0), 0}
330 static int cli_errno_from_nt(NTSTATUS status)
332 int i;
333 DEBUG(10,("cli_errno_from_nt: 32 bit codes: code=%08x\n", NT_STATUS_V(status)));
335 /* Status codes without this bit set are not errors */
337 if (!(NT_STATUS_V(status) & 0xc0000000))
338 return 0;
340 for (i=0;nt_errno_map[i].error;i++) {
341 if (NT_STATUS_V(nt_errno_map[i].status) ==
342 NT_STATUS_V(status)) return nt_errno_map[i].error;
345 /* for all other cases - a default code */
346 return EINVAL;
349 /* Return a UNIX errno appropriate for the error received in the last
350 packet. */
352 int cli_errno(struct cli_state *cli)
354 NTSTATUS status;
356 if (cli_is_dos_error(cli)) {
357 uint8 eclass;
358 uint32 ecode;
360 cli_dos_error(cli, &eclass, &ecode);
361 return cli_errno_from_dos(eclass, ecode);
364 status = cli_nt_error(cli);
366 return cli_errno_from_nt(status);
369 /* Return true if the last packet was in error */
371 BOOL cli_is_error(struct cli_state *cli)
373 uint32 flgs2 = SVAL(cli->inbuf,smb_flg2), rcls = 0;
375 if (cli->fd == -1 && cli->smb_rw_error != 0)
376 return True;
378 if (flgs2 & FLAGS2_32_BIT_ERROR_CODES) {
379 /* Return error is error bits are set */
380 rcls = IVAL(cli->inbuf, smb_rcls);
381 return (rcls & 0xF0000000) == 0xC0000000;
384 /* Return error if error class in non-zero */
386 rcls = CVAL(cli->inbuf, smb_rcls);
387 return rcls != 0;
390 /* Return true if the last error was an NT error */
392 BOOL cli_is_nt_error(struct cli_state *cli)
394 uint32 flgs2 = SVAL(cli->inbuf,smb_flg2);
396 return cli_is_error(cli) && (flgs2 & FLAGS2_32_BIT_ERROR_CODES);
399 /* Return true if the last error was a DOS error */
401 BOOL cli_is_dos_error(struct cli_state *cli)
403 uint32 flgs2 = SVAL(cli->inbuf,smb_flg2);
405 return cli_is_error(cli) && !(flgs2 & FLAGS2_32_BIT_ERROR_CODES);