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.
26 /*****************************************************
27 RAP error codes - a small start but will be extended.
29 XXX: Perhaps these should move into a common function because they're
30 duplicated in clirap2.c
32 *******************************************************/
40 {5, "RAP5: User has insufficient privilege" },
41 {50, "RAP50: Not supported by server" },
42 {65, "RAP65: Access denied" },
43 {86, "RAP86: The specified password is invalid" },
44 {2220, "RAP2220: Group does not exist" },
45 {2221, "RAP2221: User does not exist" },
46 {2226, "RAP2226: Operation only permitted on a Primary Domain Controller" },
47 {2237, "RAP2237: User is not in group" },
48 {2242, "RAP2242: The password of this user has expired." },
49 {2243, "RAP2243: The password of this user cannot change." },
50 {2244, "RAP2244: This password cannot be used now (password history conflict)." },
51 {2245, "RAP2245: The password is shorter than required." },
52 {2246, "RAP2246: The password of this user is too recent to change."},
54 /* these really shouldn't be here ... */
55 {0x80, "Not listening on called name"},
56 {0x81, "Not listening for calling name"},
57 {0x82, "Called name not present"},
58 {0x83, "Called name present, but insufficient resources"},
63 /****************************************************************************
64 return a description of an SMB error
65 ****************************************************************************/
66 static const char *cli_smb_errstr(struct cli_state
*cli
)
68 return smb_dos_errstr(cli
->inbuf
);
71 /***************************************************************************
72 Return an error message - either an NT error, SMB error or a RAP error.
73 Note some of the NT errors are actually warnings or "informational" errors
74 in which case they can be safely ignored.
75 ****************************************************************************/
77 const char *cli_errstr(struct cli_state
*cli
)
79 static fstring cli_error_message
;
80 uint32 flgs2
= SVAL(cli
->inbuf
,smb_flg2
), errnum
;
84 if (!cli
->initialised
) {
85 fstrcpy(cli_error_message
, "[Programmer's error] cli_errstr called on unitialized cli_stat struct!\n");
86 return cli_error_message
;
89 /* Was it server socket error ? */
90 if (cli
->fd
== -1 && cli
->smb_rw_error
) {
91 switch(cli
->smb_rw_error
) {
93 slprintf(cli_error_message
, sizeof(cli_error_message
) - 1,
94 "Call timed out: server did not respond after %d milliseconds",
98 slprintf(cli_error_message
, sizeof(cli_error_message
) - 1,
99 "Call returned zero bytes (EOF)" );
102 slprintf(cli_error_message
, sizeof(cli_error_message
) - 1,
103 "Read error: %s", strerror(errno
) );
106 slprintf(cli_error_message
, sizeof(cli_error_message
) - 1,
107 "Write error: %s", strerror(errno
) );
110 slprintf(cli_error_message
, sizeof(cli_error_message
) - 1,
111 "Server packet had invalid SMB signiture!");
114 slprintf(cli_error_message
, sizeof(cli_error_message
) - 1,
115 "Unknown error code %d\n", cli
->smb_rw_error
);
118 return cli_error_message
;
121 /* Case #1: RAP error */
122 if (cli
->rap_error
) {
123 for (i
= 0; rap_errmap
[i
].message
!= NULL
; i
++) {
124 if (rap_errmap
[i
].err
== cli
->rap_error
) {
125 return rap_errmap
[i
].message
;
129 slprintf(cli_error_message
, sizeof(cli_error_message
) - 1, "RAP code %d",
132 return cli_error_message
;
135 /* Case #2: 32-bit NT errors */
136 if (flgs2
& FLAGS2_32_BIT_ERROR_CODES
) {
137 NTSTATUS status
= NT_STATUS(IVAL(cli
->inbuf
,smb_rcls
));
139 return nt_errstr(status
);
142 cli_dos_error(cli
, &errclass
, &errnum
);
144 /* Case #3: SMB error */
146 return cli_smb_errstr(cli
);
150 /* Return the 32-bit NT status code from the last packet */
151 NTSTATUS
cli_nt_error(struct cli_state
*cli
)
153 int flgs2
= SVAL(cli
->inbuf
,smb_flg2
);
155 if (!(flgs2
& FLAGS2_32_BIT_ERROR_CODES
)) {
156 int class = CVAL(cli
->inbuf
,smb_rcls
);
157 int code
= SVAL(cli
->inbuf
,smb_err
);
158 return dos_to_ntstatus(class, code
);
161 return NT_STATUS(IVAL(cli
->inbuf
,smb_rcls
));
165 /* Return the DOS error from the last packet - an error class and an error
167 void cli_dos_error(struct cli_state
*cli
, uint8
*eclass
, uint32
*ecode
)
173 if(!cli
->initialised
) return;
175 flgs2
= SVAL(cli
->inbuf
,smb_flg2
);
177 if (flgs2
& FLAGS2_32_BIT_ERROR_CODES
) {
178 NTSTATUS ntstatus
= NT_STATUS(IVAL(cli
->inbuf
, smb_rcls
));
179 ntstatus_to_dos(ntstatus
, eclass
, ecode
);
183 rcls
= CVAL(cli
->inbuf
,smb_rcls
);
184 code
= SVAL(cli
->inbuf
,smb_err
);
186 if (eclass
) *eclass
= rcls
;
187 if (ecode
) *ecode
= code
;
190 /* Return a UNIX errno from a dos error class, error number tuple */
192 static int cli_errno_from_dos(uint8 eclass
, uint32 num
)
194 if (eclass
== ERRDOS
) {
196 case ERRbadfile
: return ENOENT
;
197 case ERRbadpath
: return ENOTDIR
;
198 case ERRnoaccess
: return EACCES
;
199 case ERRfilexists
: return EEXIST
;
200 case ERRrename
: return EEXIST
;
201 case ERRbadshare
: return EBUSY
;
202 case ERRlock
: return EBUSY
;
203 case ERRinvalidname
: return ENOENT
;
204 case ERRnosuchshare
: return ENODEV
;
208 if (eclass
== ERRSRV
) {
210 case ERRbadpw
: return EPERM
;
211 case ERRaccess
: return EACCES
;
212 case ERRnoresource
: return ENOMEM
;
213 case ERRinvdevice
: return ENODEV
;
214 case ERRinvnetname
: return ENODEV
;
218 /* for other cases */
222 /* Return a UNIX errno from a NT status code */
227 {NT_STATUS_ACCESS_VIOLATION
, EACCES
},
228 {NT_STATUS_INVALID_HANDLE
, EBADF
},
229 {NT_STATUS_ACCESS_DENIED
, EACCES
},
230 {NT_STATUS_OBJECT_NAME_NOT_FOUND
, ENOENT
},
231 {NT_STATUS_SHARING_VIOLATION
, EBUSY
},
232 {NT_STATUS_OBJECT_PATH_INVALID
, ENOTDIR
},
233 {NT_STATUS_OBJECT_NAME_COLLISION
, EEXIST
},
234 {NT_STATUS_PATH_NOT_COVERED
, ENOENT
},
235 {NT_STATUS_UNSUCCESSFUL
, EINVAL
},
236 {NT_STATUS_NOT_IMPLEMENTED
, ENOSYS
},
237 {NT_STATUS_IN_PAGE_ERROR
, EFAULT
},
238 {NT_STATUS_BAD_NETWORK_NAME
, ENOENT
},
240 {NT_STATUS_PAGEFILE_QUOTA
, EDQUOT
},
241 {NT_STATUS_QUOTA_EXCEEDED
, EDQUOT
},
242 {NT_STATUS_REGISTRY_QUOTA_LIMIT
, EDQUOT
},
243 {NT_STATUS_LICENSE_QUOTA_EXCEEDED
, EDQUOT
},
246 {NT_STATUS_TIMER_NOT_CANCELED
, ETIME
},
248 {NT_STATUS_INVALID_PARAMETER
, EINVAL
},
249 {NT_STATUS_NO_SUCH_DEVICE
, ENODEV
},
250 {NT_STATUS_NO_SUCH_FILE
, ENOENT
},
252 {NT_STATUS_END_OF_FILE
, ENODATA
},
255 {NT_STATUS_NO_MEDIA_IN_DEVICE
, ENOMEDIUM
},
256 {NT_STATUS_NO_MEDIA
, ENOMEDIUM
},
258 {NT_STATUS_NONEXISTENT_SECTOR
, ESPIPE
},
259 {NT_STATUS_NO_MEMORY
, ENOMEM
},
260 {NT_STATUS_CONFLICTING_ADDRESSES
, EADDRINUSE
},
261 {NT_STATUS_NOT_MAPPED_VIEW
, EINVAL
},
262 {NT_STATUS_UNABLE_TO_FREE_VM
, EADDRINUSE
},
263 {NT_STATUS_ACCESS_DENIED
, EACCES
},
264 {NT_STATUS_BUFFER_TOO_SMALL
, ENOBUFS
},
265 {NT_STATUS_WRONG_PASSWORD
, EACCES
},
266 {NT_STATUS_LOGON_FAILURE
, EACCES
},
267 {NT_STATUS_INVALID_WORKSTATION
, EACCES
},
268 {NT_STATUS_INVALID_LOGON_HOURS
, EACCES
},
269 {NT_STATUS_PASSWORD_EXPIRED
, EACCES
},
270 {NT_STATUS_ACCOUNT_DISABLED
, EACCES
},
271 {NT_STATUS_DISK_FULL
, ENOSPC
},
272 {NT_STATUS_INVALID_PIPE_STATE
, EPIPE
},
273 {NT_STATUS_PIPE_BUSY
, EPIPE
},
274 {NT_STATUS_PIPE_DISCONNECTED
, EPIPE
},
275 {NT_STATUS_PIPE_NOT_AVAILABLE
, ENOSYS
},
276 {NT_STATUS_FILE_IS_A_DIRECTORY
, EISDIR
},
277 {NT_STATUS_NOT_SUPPORTED
, ENOSYS
},
278 {NT_STATUS_NOT_A_DIRECTORY
, ENOTDIR
},
279 {NT_STATUS_DIRECTORY_NOT_EMPTY
, ENOTEMPTY
},
280 {NT_STATUS_NETWORK_UNREACHABLE
, ENETUNREACH
},
281 {NT_STATUS_HOST_UNREACHABLE
, EHOSTUNREACH
},
282 {NT_STATUS_CONNECTION_ABORTED
, ECONNABORTED
},
283 {NT_STATUS_CONNECTION_REFUSED
, ECONNREFUSED
},
284 {NT_STATUS_TOO_MANY_LINKS
, EMLINK
},
285 {NT_STATUS_NETWORK_BUSY
, EBUSY
},
286 {NT_STATUS_DEVICE_DOES_NOT_EXIST
, ENODEV
},
288 {NT_STATUS_DLL_NOT_FOUND
, ELIBACC
},
290 {NT_STATUS_PIPE_BROKEN
, EPIPE
},
291 {NT_STATUS_REMOTE_NOT_LISTENING
, ECONNREFUSED
},
292 {NT_STATUS_NETWORK_ACCESS_DENIED
, EACCES
},
293 {NT_STATUS_TOO_MANY_OPENED_FILES
, EMFILE
},
295 {NT_STATUS_DEVICE_PROTOCOL_ERROR
, EPROTO
},
297 {NT_STATUS_FLOAT_OVERFLOW
, ERANGE
},
298 {NT_STATUS_FLOAT_UNDERFLOW
, ERANGE
},
299 {NT_STATUS_INTEGER_OVERFLOW
, ERANGE
},
300 {NT_STATUS_MEDIA_WRITE_PROTECTED
, EROFS
},
301 {NT_STATUS_PIPE_CONNECTED
, EISCONN
},
302 {NT_STATUS_MEMORY_NOT_ALLOCATED
, EFAULT
},
303 {NT_STATUS_FLOAT_INEXACT_RESULT
, ERANGE
},
304 {NT_STATUS_ILL_FORMED_PASSWORD
, EACCES
},
305 {NT_STATUS_PASSWORD_RESTRICTION
, EACCES
},
306 {NT_STATUS_ACCOUNT_RESTRICTION
, EACCES
},
307 {NT_STATUS_PORT_CONNECTION_REFUSED
, ECONNREFUSED
},
308 {NT_STATUS_NAME_TOO_LONG
, ENAMETOOLONG
},
309 {NT_STATUS_REMOTE_DISCONNECT
, ESHUTDOWN
},
310 {NT_STATUS_CONNECTION_DISCONNECTED
, ECONNABORTED
},
311 {NT_STATUS_CONNECTION_RESET
, ENETRESET
},
313 {NT_STATUS_IP_ADDRESS_CONFLICT1
, ENOTUNIQ
},
314 {NT_STATUS_IP_ADDRESS_CONFLICT2
, ENOTUNIQ
},
316 {NT_STATUS_PORT_MESSAGE_TOO_LONG
, EMSGSIZE
},
317 {NT_STATUS_PROTOCOL_UNREACHABLE
, ENOPROTOOPT
},
318 {NT_STATUS_ADDRESS_ALREADY_EXISTS
, EADDRINUSE
},
319 {NT_STATUS_PORT_UNREACHABLE
, EHOSTUNREACH
},
320 {NT_STATUS_IO_TIMEOUT
, ETIMEDOUT
},
321 {NT_STATUS_RETRY
, EAGAIN
},
323 {NT_STATUS_NET_WRITE_FAULT
, ECOMM
},
329 static int cli_errno_from_nt(NTSTATUS status
)
332 DEBUG(10,("cli_errno_from_nt: 32 bit codes: code=%08x\n", NT_STATUS_V(status
)));
334 /* Status codes without this bit set are not errors */
336 if (!(NT_STATUS_V(status
) & 0xc0000000))
339 for (i
=0;nt_errno_map
[i
].error
;i
++) {
340 if (NT_STATUS_V(nt_errno_map
[i
].status
) ==
341 NT_STATUS_V(status
)) return nt_errno_map
[i
].error
;
344 /* for all other cases - a default code */
348 /* Return a UNIX errno appropriate for the error received in the last
351 int cli_errno(struct cli_state
*cli
)
355 if (cli_is_dos_error(cli
)) {
359 cli_dos_error(cli
, &eclass
, &ecode
);
360 return cli_errno_from_dos(eclass
, ecode
);
363 status
= cli_nt_error(cli
);
365 return cli_errno_from_nt(status
);
368 /* Return true if the last packet was in error */
370 BOOL
cli_is_error(struct cli_state
*cli
)
372 uint32 flgs2
= SVAL(cli
->inbuf
,smb_flg2
), rcls
= 0;
374 if (flgs2
& FLAGS2_32_BIT_ERROR_CODES
) {
375 /* Return error is error bits are set */
376 rcls
= IVAL(cli
->inbuf
, smb_rcls
);
377 return (rcls
& 0xF0000000) == 0xC0000000;
380 /* Return error if error class in non-zero */
382 rcls
= CVAL(cli
->inbuf
, smb_rcls
);
386 /* Return true if the last error was an NT error */
388 BOOL
cli_is_nt_error(struct cli_state
*cli
)
390 uint32 flgs2
= SVAL(cli
->inbuf
,smb_flg2
);
392 return cli_is_error(cli
) && (flgs2
& FLAGS2_32_BIT_ERROR_CODES
);
395 /* Return true if the last error was a DOS error */
397 BOOL
cli_is_dos_error(struct cli_state
*cli
)
399 uint32 flgs2
= SVAL(cli
->inbuf
,smb_flg2
);
401 return cli_is_error(cli
) && !(flgs2
& FLAGS2_32_BIT_ERROR_CODES
);