After technical consultation, add Steven Danneman's <steven.danneman@isilon.com>...
[Samba.git] / source / libsmb / clierror.c
blob36746419f7a0ab1c0d7c9888bf5ba49a3f8697d8
1 /*
2 Unix SMB/CIFS implementation.
3 client error handling routines
4 Copyright (C) Andrew Tridgell 1994-1998
5 Copyright (C) Jelmer Vernooij 2003
6 Copyright (C) Jeremy Allison 2006
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>.
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 {
33 int err;
34 const char *message;
35 } rap_errmap[] = {
36 {5, "RAP5: User has insufficient privilege" },
37 {50, "RAP50: Not supported by server" },
38 {65, "RAP65: Access denied" },
39 {86, "RAP86: The specified password is invalid" },
40 {2220, "RAP2220: Group does not exist" },
41 {2221, "RAP2221: User does not exist" },
42 {2226, "RAP2226: Operation only permitted on a Primary Domain Controller" },
43 {2237, "RAP2237: User is not in group" },
44 {2242, "RAP2242: The password of this user has expired." },
45 {2243, "RAP2243: The password of this user cannot change." },
46 {2244, "RAP2244: This password cannot be used now (password history conflict)." },
47 {2245, "RAP2245: The password is shorter than required." },
48 {2246, "RAP2246: The password of this user is too recent to change."},
50 /* these really shouldn't be here ... */
51 {0x80, "Not listening on called name"},
52 {0x81, "Not listening for calling name"},
53 {0x82, "Called name not present"},
54 {0x83, "Called name present, but insufficient resources"},
56 {0, NULL}
57 };
59 /****************************************************************************
60 Return a description of an SMB error.
61 ****************************************************************************/
63 static const char *cli_smb_errstr(struct cli_state *cli)
65 return smb_dos_errstr(cli->inbuf);
68 /****************************************************************************
69 Convert a socket error into an NTSTATUS.
70 ****************************************************************************/
72 static NTSTATUS cli_smb_rw_error_to_ntstatus(struct cli_state *cli)
74 switch(cli->smb_rw_error) {
75 case SMB_READ_TIMEOUT:
76 return NT_STATUS_IO_TIMEOUT;
77 case SMB_READ_EOF:
78 return NT_STATUS_END_OF_FILE;
79 /* What we shoud really do for read/write errors is convert from errno. */
80 /* FIXME. JRA. */
81 case SMB_READ_ERROR:
82 return NT_STATUS_INVALID_NETWORK_RESPONSE;
83 case SMB_WRITE_ERROR:
84 return NT_STATUS_UNEXPECTED_NETWORK_ERROR;
85 case SMB_READ_BAD_SIG:
86 return NT_STATUS_INVALID_PARAMETER;
87 case SMB_NO_MEMORY:
88 return NT_STATUS_NO_MEMORY;
89 default:
90 break;
92 return NT_STATUS_UNSUCCESSFUL;
95 /***************************************************************************
96 Return an error message - either an NT error, SMB error or a RAP error.
97 Note some of the NT errors are actually warnings or "informational" errors
98 in which case they can be safely ignored.
99 ****************************************************************************/
101 const char *cli_errstr(struct cli_state *cli)
103 fstring cli_error_message;
104 uint32 flgs2 = SVAL(cli->inbuf,smb_flg2), errnum;
105 uint8 errclass;
106 int i;
107 char *result;
109 if (!cli->initialised) {
110 fstrcpy(cli_error_message, "[Programmer's error] cli_errstr called on unitialized cli_stat struct!\n");
111 goto done;
114 /* Was it server socket error ? */
115 if (cli->fd == -1 && cli->smb_rw_error) {
116 switch(cli->smb_rw_error) {
117 case SMB_READ_TIMEOUT:
118 slprintf(cli_error_message, sizeof(cli_error_message) - 1,
119 "Call timed out: server did not respond after %d milliseconds",
120 cli->timeout);
121 break;
122 case SMB_READ_EOF:
123 slprintf(cli_error_message, sizeof(cli_error_message) - 1,
124 "Call returned zero bytes (EOF)" );
125 break;
126 case SMB_READ_ERROR:
127 slprintf(cli_error_message, sizeof(cli_error_message) - 1,
128 "Read error: %s", strerror(errno) );
129 break;
130 case SMB_WRITE_ERROR:
131 slprintf(cli_error_message, sizeof(cli_error_message) - 1,
132 "Write error: %s", strerror(errno) );
133 break;
134 case SMB_READ_BAD_SIG:
135 slprintf(cli_error_message, sizeof(cli_error_message) - 1,
136 "Server packet had invalid SMB signature!");
137 break;
138 case SMB_NO_MEMORY:
139 slprintf(cli_error_message, sizeof(cli_error_message) - 1,
140 "Out of memory");
141 break;
142 default:
143 slprintf(cli_error_message, sizeof(cli_error_message) - 1,
144 "Unknown error code %d\n", cli->smb_rw_error );
145 break;
147 goto done;
150 /* Case #1: RAP error */
151 if (cli->rap_error) {
152 for (i = 0; rap_errmap[i].message != NULL; i++) {
153 if (rap_errmap[i].err == cli->rap_error) {
154 return rap_errmap[i].message;
158 slprintf(cli_error_message, sizeof(cli_error_message) - 1, "RAP code %d",
159 cli->rap_error);
161 goto done;
164 /* Case #2: 32-bit NT errors */
165 if (flgs2 & FLAGS2_32_BIT_ERROR_CODES) {
166 NTSTATUS status = NT_STATUS(IVAL(cli->inbuf,smb_rcls));
168 return nt_errstr(status);
171 cli_dos_error(cli, &errclass, &errnum);
173 /* Case #3: SMB error */
175 return cli_smb_errstr(cli);
177 done:
178 result = talloc_strdup(talloc_tos(), cli_error_message);
179 SMB_ASSERT(result);
180 return result;
184 /****************************************************************************
185 Return the 32-bit NT status code from the last packet.
186 ****************************************************************************/
188 NTSTATUS cli_nt_error(struct cli_state *cli)
190 int flgs2 = SVAL(cli->inbuf,smb_flg2);
192 /* Deal with socket errors first. */
193 if (cli->fd == -1 && cli->smb_rw_error) {
194 return cli_smb_rw_error_to_ntstatus(cli);
197 if (!(flgs2 & FLAGS2_32_BIT_ERROR_CODES)) {
198 int e_class = CVAL(cli->inbuf,smb_rcls);
199 int code = SVAL(cli->inbuf,smb_err);
200 return dos_to_ntstatus(e_class, code);
203 return NT_STATUS(IVAL(cli->inbuf,smb_rcls));
207 /****************************************************************************
208 Return the DOS error from the last packet - an error class and an error
209 code.
210 ****************************************************************************/
212 void cli_dos_error(struct cli_state *cli, uint8 *eclass, uint32 *ecode)
214 int flgs2;
216 if(!cli->initialised) {
217 return;
220 /* Deal with socket errors first. */
221 if (cli->fd == -1 && cli->smb_rw_error) {
222 NTSTATUS status = cli_smb_rw_error_to_ntstatus(cli);
223 ntstatus_to_dos( status, eclass, ecode);
224 return;
227 flgs2 = SVAL(cli->inbuf,smb_flg2);
229 if (flgs2 & FLAGS2_32_BIT_ERROR_CODES) {
230 NTSTATUS ntstatus = NT_STATUS(IVAL(cli->inbuf, smb_rcls));
231 ntstatus_to_dos(ntstatus, eclass, ecode);
232 return;
235 *eclass = CVAL(cli->inbuf,smb_rcls);
236 *ecode = SVAL(cli->inbuf,smb_err);
239 /* Return a UNIX errno from a NT status code */
240 static const struct {
241 NTSTATUS status;
242 int error;
243 } nt_errno_map[] = {
244 {NT_STATUS_ACCESS_VIOLATION, EACCES},
245 {NT_STATUS_INVALID_HANDLE, EBADF},
246 {NT_STATUS_ACCESS_DENIED, EACCES},
247 {NT_STATUS_OBJECT_NAME_NOT_FOUND, ENOENT},
248 {NT_STATUS_OBJECT_PATH_NOT_FOUND, ENOENT},
249 {NT_STATUS_SHARING_VIOLATION, EBUSY},
250 {NT_STATUS_OBJECT_PATH_INVALID, ENOTDIR},
251 {NT_STATUS_OBJECT_NAME_COLLISION, EEXIST},
252 {NT_STATUS_PATH_NOT_COVERED, ENOENT},
253 {NT_STATUS_UNSUCCESSFUL, EINVAL},
254 {NT_STATUS_NOT_IMPLEMENTED, ENOSYS},
255 {NT_STATUS_IN_PAGE_ERROR, EFAULT},
256 {NT_STATUS_BAD_NETWORK_NAME, ENOENT},
257 #ifdef EDQUOT
258 {NT_STATUS_PAGEFILE_QUOTA, EDQUOT},
259 {NT_STATUS_QUOTA_EXCEEDED, EDQUOT},
260 {NT_STATUS_REGISTRY_QUOTA_LIMIT, EDQUOT},
261 {NT_STATUS_LICENSE_QUOTA_EXCEEDED, EDQUOT},
262 #endif
263 #ifdef ETIME
264 {NT_STATUS_TIMER_NOT_CANCELED, ETIME},
265 #endif
266 {NT_STATUS_INVALID_PARAMETER, EINVAL},
267 {NT_STATUS_NO_SUCH_DEVICE, ENODEV},
268 {NT_STATUS_NO_SUCH_FILE, ENOENT},
269 #ifdef ENODATA
270 {NT_STATUS_END_OF_FILE, ENODATA},
271 #endif
272 #ifdef ENOMEDIUM
273 {NT_STATUS_NO_MEDIA_IN_DEVICE, ENOMEDIUM},
274 {NT_STATUS_NO_MEDIA, ENOMEDIUM},
275 #endif
276 {NT_STATUS_NONEXISTENT_SECTOR, ESPIPE},
277 {NT_STATUS_NO_MEMORY, ENOMEM},
278 {NT_STATUS_CONFLICTING_ADDRESSES, EADDRINUSE},
279 {NT_STATUS_NOT_MAPPED_VIEW, EINVAL},
280 {NT_STATUS_UNABLE_TO_FREE_VM, EADDRINUSE},
281 {NT_STATUS_ACCESS_DENIED, EACCES},
282 {NT_STATUS_BUFFER_TOO_SMALL, ENOBUFS},
283 {NT_STATUS_WRONG_PASSWORD, EACCES},
284 {NT_STATUS_LOGON_FAILURE, EACCES},
285 {NT_STATUS_INVALID_WORKSTATION, EACCES},
286 {NT_STATUS_INVALID_LOGON_HOURS, EACCES},
287 {NT_STATUS_PASSWORD_EXPIRED, EACCES},
288 {NT_STATUS_ACCOUNT_DISABLED, EACCES},
289 {NT_STATUS_DISK_FULL, ENOSPC},
290 {NT_STATUS_INVALID_PIPE_STATE, EPIPE},
291 {NT_STATUS_PIPE_BUSY, EPIPE},
292 {NT_STATUS_PIPE_DISCONNECTED, EPIPE},
293 {NT_STATUS_PIPE_NOT_AVAILABLE, ENOSYS},
294 {NT_STATUS_FILE_IS_A_DIRECTORY, EISDIR},
295 {NT_STATUS_NOT_SUPPORTED, ENOSYS},
296 {NT_STATUS_NOT_A_DIRECTORY, ENOTDIR},
297 {NT_STATUS_DIRECTORY_NOT_EMPTY, ENOTEMPTY},
298 {NT_STATUS_NETWORK_UNREACHABLE, ENETUNREACH},
299 {NT_STATUS_HOST_UNREACHABLE, EHOSTUNREACH},
300 {NT_STATUS_CONNECTION_ABORTED, ECONNABORTED},
301 {NT_STATUS_CONNECTION_REFUSED, ECONNREFUSED},
302 {NT_STATUS_TOO_MANY_LINKS, EMLINK},
303 {NT_STATUS_NETWORK_BUSY, EBUSY},
304 {NT_STATUS_DEVICE_DOES_NOT_EXIST, ENODEV},
305 #ifdef ELIBACC
306 {NT_STATUS_DLL_NOT_FOUND, ELIBACC},
307 #endif
308 {NT_STATUS_PIPE_BROKEN, EPIPE},
309 {NT_STATUS_REMOTE_NOT_LISTENING, ECONNREFUSED},
310 {NT_STATUS_NETWORK_ACCESS_DENIED, EACCES},
311 {NT_STATUS_TOO_MANY_OPENED_FILES, EMFILE},
312 #ifdef EPROTO
313 {NT_STATUS_DEVICE_PROTOCOL_ERROR, EPROTO},
314 #endif
315 {NT_STATUS_FLOAT_OVERFLOW, ERANGE},
316 {NT_STATUS_FLOAT_UNDERFLOW, ERANGE},
317 {NT_STATUS_INTEGER_OVERFLOW, ERANGE},
318 {NT_STATUS_MEDIA_WRITE_PROTECTED, EROFS},
319 {NT_STATUS_PIPE_CONNECTED, EISCONN},
320 {NT_STATUS_MEMORY_NOT_ALLOCATED, EFAULT},
321 {NT_STATUS_FLOAT_INEXACT_RESULT, ERANGE},
322 {NT_STATUS_ILL_FORMED_PASSWORD, EACCES},
323 {NT_STATUS_PASSWORD_RESTRICTION, EACCES},
324 {NT_STATUS_ACCOUNT_RESTRICTION, EACCES},
325 {NT_STATUS_PORT_CONNECTION_REFUSED, ECONNREFUSED},
326 {NT_STATUS_NAME_TOO_LONG, ENAMETOOLONG},
327 {NT_STATUS_REMOTE_DISCONNECT, ESHUTDOWN},
328 {NT_STATUS_CONNECTION_DISCONNECTED, ECONNABORTED},
329 {NT_STATUS_CONNECTION_RESET, ENETRESET},
330 #ifdef ENOTUNIQ
331 {NT_STATUS_IP_ADDRESS_CONFLICT1, ENOTUNIQ},
332 {NT_STATUS_IP_ADDRESS_CONFLICT2, ENOTUNIQ},
333 #endif
334 {NT_STATUS_PORT_MESSAGE_TOO_LONG, EMSGSIZE},
335 {NT_STATUS_PROTOCOL_UNREACHABLE, ENOPROTOOPT},
336 {NT_STATUS_ADDRESS_ALREADY_EXISTS, EADDRINUSE},
337 {NT_STATUS_PORT_UNREACHABLE, EHOSTUNREACH},
338 {NT_STATUS_IO_TIMEOUT, ETIMEDOUT},
339 {NT_STATUS_RETRY, EAGAIN},
340 #ifdef ENOTUNIQ
341 {NT_STATUS_DUPLICATE_NAME, ENOTUNIQ},
342 #endif
343 #ifdef ECOMM
344 {NT_STATUS_NET_WRITE_FAULT, ECOMM},
345 #endif
346 #ifdef EXDEV
347 {NT_STATUS_NOT_SAME_DEVICE, EXDEV},
348 #endif
349 {NT_STATUS(0), 0}
352 /****************************************************************************
353 The following mappings need tidying up and moving into libsmb/errormap.c...
354 ****************************************************************************/
356 static int cli_errno_from_nt(NTSTATUS status)
358 int i;
359 DEBUG(10,("cli_errno_from_nt: 32 bit codes: code=%08x\n", NT_STATUS_V(status)));
361 /* Status codes without this bit set are not errors */
363 if (!(NT_STATUS_V(status) & 0xc0000000)) {
364 return 0;
367 for (i=0;nt_errno_map[i].error;i++) {
368 if (NT_STATUS_V(nt_errno_map[i].status) ==
369 NT_STATUS_V(status)) return nt_errno_map[i].error;
372 /* for all other cases - a default code */
373 return EINVAL;
376 /* Return a UNIX errno appropriate for the error received in the last
377 packet. */
379 int cli_errno(struct cli_state *cli)
381 NTSTATUS status;
383 if (cli_is_nt_error(cli)) {
384 status = cli_nt_error(cli);
385 return cli_errno_from_nt(status);
388 if (cli_is_dos_error(cli)) {
389 uint8 eclass;
390 uint32 ecode;
392 cli_dos_error(cli, &eclass, &ecode);
393 status = dos_to_ntstatus(eclass, ecode);
394 return cli_errno_from_nt(status);
398 * Yuck! A special case for this Vista error. Since its high-order
399 * byte isn't 0xc0, it doesn't match cli_is_nt_error() above.
401 status = cli_nt_error(cli);
402 if (NT_STATUS_V(status) == NT_STATUS_V(NT_STATUS_INACCESSIBLE_SYSTEM_SHORTCUT)) {
403 return EACCES;
406 /* for other cases */
407 return EINVAL;
410 /* Return true if the last packet was in error */
412 bool cli_is_error(struct cli_state *cli)
414 uint32 flgs2 = SVAL(cli->inbuf,smb_flg2), rcls = 0;
416 /* A socket error is always an error. */
417 if (cli->fd == -1 && cli->smb_rw_error != 0) {
418 return True;
421 if (flgs2 & FLAGS2_32_BIT_ERROR_CODES) {
422 /* Return error is error bits are set */
423 rcls = IVAL(cli->inbuf, smb_rcls);
424 return (rcls & 0xF0000000) == 0xC0000000;
427 /* Return error if error class in non-zero */
429 rcls = CVAL(cli->inbuf, smb_rcls);
430 return rcls != 0;
433 /* Return true if the last error was an NT error */
435 bool cli_is_nt_error(struct cli_state *cli)
437 uint32 flgs2 = SVAL(cli->inbuf,smb_flg2);
439 /* A socket error is always an NT error. */
440 if (cli->fd == -1 && cli->smb_rw_error != 0) {
441 return True;
444 return cli_is_error(cli) && (flgs2 & FLAGS2_32_BIT_ERROR_CODES);
447 /* Return true if the last error was a DOS error */
449 bool cli_is_dos_error(struct cli_state *cli)
451 uint32 flgs2 = SVAL(cli->inbuf,smb_flg2);
453 /* A socket error is always a DOS error. */
454 if (cli->fd == -1 && cli->smb_rw_error != 0) {
455 return True;
458 return cli_is_error(cli) && !(flgs2 & FLAGS2_32_BIT_ERROR_CODES);
461 /* Return the last error always as an NTSTATUS. */
463 NTSTATUS cli_get_nt_error(struct cli_state *cli)
465 if (cli_is_nt_error(cli)) {
466 return cli_nt_error(cli);
467 } else if (cli_is_dos_error(cli)) {
468 uint32 ecode;
469 uint8 eclass;
470 cli_dos_error(cli, &eclass, &ecode);
471 return dos_to_ntstatus(eclass, ecode);
472 } else {
473 /* Something went wrong, we don't know what. */
474 return NT_STATUS_UNSUCCESSFUL;
478 /* Push an error code into the inbuf to be returned on the next
479 * query. */
481 void cli_set_nt_error(struct cli_state *cli, NTSTATUS status)
483 SSVAL(cli->inbuf,smb_flg2, SVAL(cli->inbuf,smb_flg2)|FLAGS2_32_BIT_ERROR_CODES);
484 SIVAL(cli->inbuf, smb_rcls, NT_STATUS_V(status));
487 /* Reset an error. */
489 void cli_reset_error(struct cli_state *cli)
491 if (SVAL(cli->inbuf,smb_flg2) & FLAGS2_32_BIT_ERROR_CODES) {
492 SIVAL(cli->inbuf, smb_rcls, NT_STATUS_V(NT_STATUS_OK));
493 } else {
494 SCVAL(cli->inbuf,smb_rcls,0);
495 SSVAL(cli->inbuf,smb_err,0);