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 2 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, write to the Free Software
20 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 /*****************************************************
26 RAP error codes - a small start but will be extended.
28 XXX: Perhaps these should move into a common function because they're
29 duplicated in clirap2.c
31 *******************************************************/
37 {5, "RAP5: User has insufficient privilege" },
38 {50, "RAP50: Not supported by server" },
39 {65, "RAP65: Access denied" },
40 {86, "RAP86: The specified password is invalid" },
41 {2220, "RAP2220: Group does not exist" },
42 {2221, "RAP2221: User does not exist" },
43 {2226, "RAP2226: Operation only permitted on a Primary Domain Controller" },
44 {2237, "RAP2237: User is not in group" },
45 {2242, "RAP2242: The password of this user has expired." },
46 {2243, "RAP2243: The password of this user cannot change." },
47 {2244, "RAP2244: This password cannot be used now (password history conflict)." },
48 {2245, "RAP2245: The password is shorter than required." },
49 {2246, "RAP2246: The password of this user is too recent to change."},
51 /* these really shouldn't be here ... */
52 {0x80, "Not listening on called name"},
53 {0x81, "Not listening for calling name"},
54 {0x82, "Called name not present"},
55 {0x83, "Called name present, but insufficient resources"},
60 /****************************************************************************
61 Return a description of an SMB error.
62 ****************************************************************************/
64 static const char *cli_smb_errstr(struct cli_state
*cli
)
66 return smb_dos_errstr(cli
->inbuf
);
69 /****************************************************************************
70 Convert a socket error into an NTSTATUS.
71 ****************************************************************************/
73 static NTSTATUS
cli_smb_rw_error_to_ntstatus(struct cli_state
*cli
)
75 switch(cli
->smb_rw_error
) {
77 return NT_STATUS_IO_TIMEOUT
;
79 return NT_STATUS_END_OF_FILE
;
80 /* What we shoud really do for read/write errors is convert from errno. */
83 return NT_STATUS_INVALID_NETWORK_RESPONSE
;
85 return NT_STATUS_UNEXPECTED_NETWORK_ERROR
;
87 case READ_BAD_DECRYPT
:
88 return NT_STATUS_INVALID_PARAMETER
;
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 static fstring cli_error_message
;
104 uint32 flgs2
= SVAL(cli
->inbuf
,smb_flg2
), errnum
;
108 if (!cli
->initialised
) {
109 fstrcpy(cli_error_message
, "[Programmer's error] cli_errstr called on unitialized cli_stat struct!\n");
110 return cli_error_message
;
113 /* Was it server socket error ? */
114 if (cli
->fd
== -1 && cli
->smb_rw_error
) {
115 switch(cli
->smb_rw_error
) {
117 slprintf(cli_error_message
, sizeof(cli_error_message
) - 1,
118 "Call timed out: server did not respond after %d milliseconds",
122 slprintf(cli_error_message
, sizeof(cli_error_message
) - 1,
123 "Call returned zero bytes (EOF)" );
126 slprintf(cli_error_message
, sizeof(cli_error_message
) - 1,
127 "Read error: %s", strerror(errno
) );
130 slprintf(cli_error_message
, sizeof(cli_error_message
) - 1,
131 "Write error: %s", strerror(errno
) );
134 slprintf(cli_error_message
, sizeof(cli_error_message
) - 1,
135 "Server packet had invalid SMB signature!");
137 case READ_BAD_DECRYPT
:
138 slprintf(cli_error_message
, sizeof(cli_error_message
) - 1,
139 "Server packet could not be decrypted !");
142 slprintf(cli_error_message
, sizeof(cli_error_message
) - 1,
143 "Unknown error code %d\n", cli
->smb_rw_error
);
146 return cli_error_message
;
149 /* Case #1: RAP error */
150 if (cli
->rap_error
) {
151 for (i
= 0; rap_errmap
[i
].message
!= NULL
; i
++) {
152 if (rap_errmap
[i
].err
== cli
->rap_error
) {
153 return rap_errmap
[i
].message
;
157 slprintf(cli_error_message
, sizeof(cli_error_message
) - 1, "RAP code %d",
160 return cli_error_message
;
163 /* Case #2: 32-bit NT errors */
164 if (flgs2
& FLAGS2_32_BIT_ERROR_CODES
) {
165 NTSTATUS status
= NT_STATUS(IVAL(cli
->inbuf
,smb_rcls
));
167 return nt_errstr(status
);
170 cli_dos_error(cli
, &errclass
, &errnum
);
172 /* Case #3: SMB error */
174 return cli_smb_errstr(cli
);
178 /****************************************************************************
179 Return the 32-bit NT status code from the last packet.
180 ****************************************************************************/
182 NTSTATUS
cli_nt_error(struct cli_state
*cli
)
184 int flgs2
= SVAL(cli
->inbuf
,smb_flg2
);
186 /* Deal with socket errors first. */
187 if (cli
->fd
== -1 && cli
->smb_rw_error
) {
188 return cli_smb_rw_error_to_ntstatus(cli
);
191 if (!(flgs2
& FLAGS2_32_BIT_ERROR_CODES
)) {
192 int e_class
= CVAL(cli
->inbuf
,smb_rcls
);
193 int code
= SVAL(cli
->inbuf
,smb_err
);
194 return dos_to_ntstatus(e_class
, code
);
197 return NT_STATUS(IVAL(cli
->inbuf
,smb_rcls
));
201 /****************************************************************************
202 Return the DOS error from the last packet - an error class and an error
204 ****************************************************************************/
206 void cli_dos_error(struct cli_state
*cli
, uint8
*eclass
, uint32
*ecode
)
210 if(!cli
->initialised
) {
214 /* Deal with socket errors first. */
215 if (cli
->fd
== -1 && cli
->smb_rw_error
) {
216 NTSTATUS status
= cli_smb_rw_error_to_ntstatus(cli
);
217 ntstatus_to_dos( status
, eclass
, ecode
);
221 flgs2
= SVAL(cli
->inbuf
,smb_flg2
);
223 if (flgs2
& FLAGS2_32_BIT_ERROR_CODES
) {
224 NTSTATUS ntstatus
= NT_STATUS(IVAL(cli
->inbuf
, smb_rcls
));
225 ntstatus_to_dos(ntstatus
, eclass
, ecode
);
229 *eclass
= CVAL(cli
->inbuf
,smb_rcls
);
230 *ecode
= SVAL(cli
->inbuf
,smb_err
);
233 /* Return a UNIX errno from a NT status code */
234 static const struct {
238 {NT_STATUS_ACCESS_VIOLATION
, EACCES
},
239 {NT_STATUS_INVALID_HANDLE
, EBADF
},
240 {NT_STATUS_ACCESS_DENIED
, EACCES
},
241 {NT_STATUS_OBJECT_NAME_NOT_FOUND
, ENOENT
},
242 {NT_STATUS_SHARING_VIOLATION
, EBUSY
},
243 {NT_STATUS_OBJECT_PATH_INVALID
, ENOTDIR
},
244 {NT_STATUS_OBJECT_NAME_COLLISION
, EEXIST
},
245 {NT_STATUS_PATH_NOT_COVERED
, ENOENT
},
246 {NT_STATUS_UNSUCCESSFUL
, EINVAL
},
247 {NT_STATUS_NOT_IMPLEMENTED
, ENOSYS
},
248 {NT_STATUS_IN_PAGE_ERROR
, EFAULT
},
249 {NT_STATUS_BAD_NETWORK_NAME
, ENOENT
},
251 {NT_STATUS_PAGEFILE_QUOTA
, EDQUOT
},
252 {NT_STATUS_QUOTA_EXCEEDED
, EDQUOT
},
253 {NT_STATUS_REGISTRY_QUOTA_LIMIT
, EDQUOT
},
254 {NT_STATUS_LICENSE_QUOTA_EXCEEDED
, EDQUOT
},
257 {NT_STATUS_TIMER_NOT_CANCELED
, ETIME
},
259 {NT_STATUS_INVALID_PARAMETER
, EINVAL
},
260 {NT_STATUS_NO_SUCH_DEVICE
, ENODEV
},
261 {NT_STATUS_NO_SUCH_FILE
, ENOENT
},
263 {NT_STATUS_END_OF_FILE
, ENODATA
},
266 {NT_STATUS_NO_MEDIA_IN_DEVICE
, ENOMEDIUM
},
267 {NT_STATUS_NO_MEDIA
, ENOMEDIUM
},
269 {NT_STATUS_NONEXISTENT_SECTOR
, ESPIPE
},
270 {NT_STATUS_NO_MEMORY
, ENOMEM
},
271 {NT_STATUS_CONFLICTING_ADDRESSES
, EADDRINUSE
},
272 {NT_STATUS_NOT_MAPPED_VIEW
, EINVAL
},
273 {NT_STATUS_UNABLE_TO_FREE_VM
, EADDRINUSE
},
274 {NT_STATUS_ACCESS_DENIED
, EACCES
},
275 {NT_STATUS_BUFFER_TOO_SMALL
, ENOBUFS
},
276 {NT_STATUS_WRONG_PASSWORD
, EACCES
},
277 {NT_STATUS_LOGON_FAILURE
, EACCES
},
278 {NT_STATUS_INVALID_WORKSTATION
, EACCES
},
279 {NT_STATUS_INVALID_LOGON_HOURS
, EACCES
},
280 {NT_STATUS_PASSWORD_EXPIRED
, EACCES
},
281 {NT_STATUS_ACCOUNT_DISABLED
, EACCES
},
282 {NT_STATUS_DISK_FULL
, ENOSPC
},
283 {NT_STATUS_INVALID_PIPE_STATE
, EPIPE
},
284 {NT_STATUS_PIPE_BUSY
, EPIPE
},
285 {NT_STATUS_PIPE_DISCONNECTED
, EPIPE
},
286 {NT_STATUS_PIPE_NOT_AVAILABLE
, ENOSYS
},
287 {NT_STATUS_FILE_IS_A_DIRECTORY
, EISDIR
},
288 {NT_STATUS_NOT_SUPPORTED
, ENOSYS
},
289 {NT_STATUS_NOT_A_DIRECTORY
, ENOTDIR
},
290 {NT_STATUS_DIRECTORY_NOT_EMPTY
, ENOTEMPTY
},
291 {NT_STATUS_NETWORK_UNREACHABLE
, ENETUNREACH
},
292 {NT_STATUS_HOST_UNREACHABLE
, EHOSTUNREACH
},
293 {NT_STATUS_CONNECTION_ABORTED
, ECONNABORTED
},
294 {NT_STATUS_CONNECTION_REFUSED
, ECONNREFUSED
},
295 {NT_STATUS_TOO_MANY_LINKS
, EMLINK
},
296 {NT_STATUS_NETWORK_BUSY
, EBUSY
},
297 {NT_STATUS_DEVICE_DOES_NOT_EXIST
, ENODEV
},
299 {NT_STATUS_DLL_NOT_FOUND
, ELIBACC
},
301 {NT_STATUS_PIPE_BROKEN
, EPIPE
},
302 {NT_STATUS_REMOTE_NOT_LISTENING
, ECONNREFUSED
},
303 {NT_STATUS_NETWORK_ACCESS_DENIED
, EACCES
},
304 {NT_STATUS_TOO_MANY_OPENED_FILES
, EMFILE
},
306 {NT_STATUS_DEVICE_PROTOCOL_ERROR
, EPROTO
},
308 {NT_STATUS_FLOAT_OVERFLOW
, ERANGE
},
309 {NT_STATUS_FLOAT_UNDERFLOW
, ERANGE
},
310 {NT_STATUS_INTEGER_OVERFLOW
, ERANGE
},
311 {NT_STATUS_MEDIA_WRITE_PROTECTED
, EROFS
},
312 {NT_STATUS_PIPE_CONNECTED
, EISCONN
},
313 {NT_STATUS_MEMORY_NOT_ALLOCATED
, EFAULT
},
314 {NT_STATUS_FLOAT_INEXACT_RESULT
, ERANGE
},
315 {NT_STATUS_ILL_FORMED_PASSWORD
, EACCES
},
316 {NT_STATUS_PASSWORD_RESTRICTION
, EACCES
},
317 {NT_STATUS_ACCOUNT_RESTRICTION
, EACCES
},
318 {NT_STATUS_PORT_CONNECTION_REFUSED
, ECONNREFUSED
},
319 {NT_STATUS_NAME_TOO_LONG
, ENAMETOOLONG
},
320 {NT_STATUS_REMOTE_DISCONNECT
, ESHUTDOWN
},
321 {NT_STATUS_CONNECTION_DISCONNECTED
, ECONNABORTED
},
322 {NT_STATUS_CONNECTION_RESET
, ENETRESET
},
324 {NT_STATUS_IP_ADDRESS_CONFLICT1
, ENOTUNIQ
},
325 {NT_STATUS_IP_ADDRESS_CONFLICT2
, ENOTUNIQ
},
327 {NT_STATUS_PORT_MESSAGE_TOO_LONG
, EMSGSIZE
},
328 {NT_STATUS_PROTOCOL_UNREACHABLE
, ENOPROTOOPT
},
329 {NT_STATUS_ADDRESS_ALREADY_EXISTS
, EADDRINUSE
},
330 {NT_STATUS_PORT_UNREACHABLE
, EHOSTUNREACH
},
331 {NT_STATUS_IO_TIMEOUT
, ETIMEDOUT
},
332 {NT_STATUS_RETRY
, EAGAIN
},
334 {NT_STATUS_DUPLICATE_NAME
, ENOTUNIQ
},
337 {NT_STATUS_NET_WRITE_FAULT
, ECOMM
},
340 {NT_STATUS_NOT_SAME_DEVICE
, EXDEV
},
345 /****************************************************************************
346 The following mappings need tidying up and moving into libsmb/errormap.c...
347 ****************************************************************************/
349 static int cli_errno_from_nt(NTSTATUS status
)
352 DEBUG(10,("cli_errno_from_nt: 32 bit codes: code=%08x\n", NT_STATUS_V(status
)));
354 /* Status codes without this bit set are not errors */
356 if (!(NT_STATUS_V(status
) & 0xc0000000)) {
360 for (i
=0;nt_errno_map
[i
].error
;i
++) {
361 if (NT_STATUS_V(nt_errno_map
[i
].status
) ==
362 NT_STATUS_V(status
)) return nt_errno_map
[i
].error
;
365 /* for all other cases - a default code */
369 /* Return a UNIX errno appropriate for the error received in the last
372 int cli_errno(struct cli_state
*cli
)
376 if (cli_is_nt_error(cli
)) {
377 status
= cli_nt_error(cli
);
378 return cli_errno_from_nt(status
);
381 if (cli_is_dos_error(cli
)) {
385 cli_dos_error(cli
, &eclass
, &ecode
);
386 status
= dos_to_ntstatus(eclass
, ecode
);
387 return cli_errno_from_nt(status
);
391 * Yuck! A special case for this Vista error. Since its high-order
392 * byte isn't 0xc0, it doesn't match cli_is_nt_error() above.
394 status
= cli_nt_error(cli
);
395 if (NT_STATUS_V(status
) == NT_STATUS_V(NT_STATUS_INACCESSIBLE_SYSTEM_SHORTCUT
)) {
399 /* for other cases */
403 /* Return true if the last packet was in error */
405 BOOL
cli_is_error(struct cli_state
*cli
)
407 uint32 flgs2
= SVAL(cli
->inbuf
,smb_flg2
), rcls
= 0;
409 /* A socket error is always an error. */
410 if (cli
->fd
== -1 && cli
->smb_rw_error
!= 0) {
414 if (flgs2
& FLAGS2_32_BIT_ERROR_CODES
) {
415 /* Return error is error bits are set */
416 rcls
= IVAL(cli
->inbuf
, smb_rcls
);
417 return (rcls
& 0xF0000000) == 0xC0000000;
420 /* Return error if error class in non-zero */
422 rcls
= CVAL(cli
->inbuf
, smb_rcls
);
426 /* Return true if the last error was an NT error */
428 BOOL
cli_is_nt_error(struct cli_state
*cli
)
430 uint32 flgs2
= SVAL(cli
->inbuf
,smb_flg2
);
432 /* A socket error is always an NT error. */
433 if (cli
->fd
== -1 && cli
->smb_rw_error
!= 0) {
437 return cli_is_error(cli
) && (flgs2
& FLAGS2_32_BIT_ERROR_CODES
);
440 /* Return true if the last error was a DOS error */
442 BOOL
cli_is_dos_error(struct cli_state
*cli
)
444 uint32 flgs2
= SVAL(cli
->inbuf
,smb_flg2
);
446 /* A socket error is always a DOS error. */
447 if (cli
->fd
== -1 && cli
->smb_rw_error
!= 0) {
451 return cli_is_error(cli
) && !(flgs2
& FLAGS2_32_BIT_ERROR_CODES
);
454 /* Return the last error always as an NTSTATUS. */
456 NTSTATUS
cli_get_nt_error(struct cli_state
*cli
)
458 if (cli_is_nt_error(cli
)) {
459 return cli_nt_error(cli
);
460 } else if (cli_is_dos_error(cli
)) {
463 cli_dos_error(cli
, &eclass
, &ecode
);
464 return dos_to_ntstatus(eclass
, ecode
);
466 /* Something went wrong, we don't know what. */
467 return NT_STATUS_UNSUCCESSFUL
;
471 /* Push an error code into the inbuf to be returned on the next
474 void cli_set_nt_error(struct cli_state
*cli
, NTSTATUS status
)
476 SSVAL(cli
->inbuf
,smb_flg2
, SVAL(cli
->inbuf
,smb_flg2
)|FLAGS2_32_BIT_ERROR_CODES
);
477 SIVAL(cli
->inbuf
, smb_rcls
, NT_STATUS_V(status
));