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)\n" );
102 slprintf(cli_error_message, sizeof(cli_error_message) - 1,
103 "Read error: %s\n", strerror(errno) );
106 slprintf(cli_error_message, sizeof(cli_error_message) - 1,
107 "Write error: %s\n", strerror(errno) );
110 slprintf(cli_error_message, sizeof(cli_error_message) - 1,
111 "Unknown error code %d\n", cli->smb_rw_error );
114 return cli_error_message;
117 /* Case #1: RAP error */
118 if (cli->rap_error) {
119 for (i = 0; rap_errmap[i].message != NULL; i++) {
120 if (rap_errmap[i].err == cli->rap_error) {
121 return rap_errmap[i].message;
125 slprintf(cli_error_message, sizeof(cli_error_message) - 1, "RAP code %d",
128 return cli_error_message;
131 /* Case #2: 32-bit NT errors */
132 if (flgs2 & FLAGS2_32_BIT_ERROR_CODES) {
133 NTSTATUS status = NT_STATUS(IVAL(cli->inbuf,smb_rcls));
135 return nt_errstr(status);
138 cli_dos_error(cli, &errclass, &errnum);
140 /* Case #3: SMB error */
142 return cli_smb_errstr(cli);
146 /* Return the 32-bit NT status code from the last packet */
147 NTSTATUS cli_nt_error(struct cli_state *cli)
149 int flgs2 = SVAL(cli->inbuf,smb_flg2);
151 if (!(flgs2 & FLAGS2_32_BIT_ERROR_CODES)) {
152 int class = CVAL(cli->inbuf,smb_rcls);
153 int code = SVAL(cli->inbuf,smb_err);
154 return dos_to_ntstatus(class, code);
157 return NT_STATUS(IVAL(cli->inbuf,smb_rcls));
161 /* Return the DOS error from the last packet - an error class and an error
163 void cli_dos_error(struct cli_state *cli, uint8 *eclass, uint32 *ecode)
169 if(!cli->initialised) return;
171 flgs2 = SVAL(cli->inbuf,smb_flg2);
173 if (flgs2 & FLAGS2_32_BIT_ERROR_CODES) {
174 NTSTATUS ntstatus = NT_STATUS(IVAL(cli->inbuf, smb_rcls));
175 ntstatus_to_dos(ntstatus, eclass, ecode);
179 rcls = CVAL(cli->inbuf,smb_rcls);
180 code = SVAL(cli->inbuf,smb_err);
182 if (eclass) *eclass = rcls;
183 if (ecode) *ecode = code;
186 /* Return a UNIX errno from a dos error class, error number tuple */
188 static int cli_errno_from_dos(uint8 eclass, uint32 num)
190 if (eclass == ERRDOS) {
192 case ERRbadfile: return ENOENT;
193 case ERRbadpath: return ENOTDIR;
194 case ERRnoaccess: return EACCES;
195 case ERRfilexists: return EEXIST;
196 case ERRrename: return EEXIST;
197 case ERRbadshare: return EBUSY;
198 case ERRlock: return EBUSY;
199 case ERRinvalidname: return ENOENT;
200 case ERRnosuchshare: return ENODEV;
204 if (eclass == ERRSRV) {
206 case ERRbadpw: return EPERM;
207 case ERRaccess: return EACCES;
208 case ERRnoresource: return ENOMEM;
209 case ERRinvdevice: return ENODEV;
210 case ERRinvnetname: return ENODEV;
214 /* for other cases */
218 /* Return a UNIX errno from a NT status code */
223 {NT_STATUS_ACCESS_VIOLATION, EACCES},
224 {NT_STATUS_INVALID_HANDLE, EBADF},
225 {NT_STATUS_ACCESS_DENIED, EACCES},
226 {NT_STATUS_OBJECT_NAME_NOT_FOUND, ENOENT},
227 {NT_STATUS_SHARING_VIOLATION, EBUSY},
228 {NT_STATUS_OBJECT_PATH_INVALID, ENOTDIR},
229 {NT_STATUS_OBJECT_NAME_COLLISION, EEXIST},
230 {NT_STATUS_PATH_NOT_COVERED, ENOENT},
231 {NT_STATUS_UNSUCCESSFUL, EINVAL},
232 {NT_STATUS_NOT_IMPLEMENTED, ENOSYS},
233 {NT_STATUS_IN_PAGE_ERROR, EFAULT},
234 {NT_STATUS_PAGEFILE_QUOTA, EDQUOT},
236 {NT_STATUS_TIMER_NOT_CANCELED, ETIME},
238 {NT_STATUS_INVALID_PARAMETER, EINVAL},
239 {NT_STATUS_NO_SUCH_DEVICE, ENODEV},
240 {NT_STATUS_NO_SUCH_FILE, ENOENT},
242 {NT_STATUS_END_OF_FILE, ENODATA},
245 {NT_STATUS_NO_MEDIA_IN_DEVICE, ENOMEDIUM},
246 {NT_STATUS_NO_MEDIA, ENOMEDIUM},
248 {NT_STATUS_NONEXISTENT_SECTOR, ESPIPE},
249 {NT_STATUS_NO_MEMORY, ENOMEM},
250 {NT_STATUS_CONFLICTING_ADDRESSES, EADDRINUSE},
251 {NT_STATUS_NOT_MAPPED_VIEW, EINVAL},
252 {NT_STATUS_UNABLE_TO_FREE_VM, EADDRINUSE},
253 {NT_STATUS_ACCESS_DENIED, EACCES},
254 {NT_STATUS_BUFFER_TOO_SMALL, ENOBUFS},
255 {NT_STATUS_QUOTA_EXCEEDED, EDQUOT},
256 {NT_STATUS_WRONG_PASSWORD, EACCES},
257 {NT_STATUS_LOGON_FAILURE, EACCES},
258 {NT_STATUS_INVALID_WORKSTATION, EACCES},
259 {NT_STATUS_INVALID_LOGON_HOURS, EACCES},
260 {NT_STATUS_PASSWORD_EXPIRED, EACCES},
261 {NT_STATUS_ACCOUNT_DISABLED, EACCES},
262 {NT_STATUS_DISK_FULL, ENOSPC},
263 {NT_STATUS_INVALID_PIPE_STATE, EPIPE},
264 {NT_STATUS_PIPE_BUSY, EPIPE},
265 {NT_STATUS_PIPE_DISCONNECTED, EPIPE},
266 {NT_STATUS_PIPE_NOT_AVAILABLE, ENOSYS},
267 {NT_STATUS_FILE_IS_A_DIRECTORY, EISDIR},
268 {NT_STATUS_NOT_SUPPORTED, ENOSYS},
269 {NT_STATUS_NOT_A_DIRECTORY, ENOTDIR},
270 {NT_STATUS_DIRECTORY_NOT_EMPTY, ENOTEMPTY},
271 {NT_STATUS_NETWORK_UNREACHABLE, ENETUNREACH},
272 {NT_STATUS_HOST_UNREACHABLE, EHOSTUNREACH},
273 {NT_STATUS_CONNECTION_ABORTED, ECONNABORTED},
274 {NT_STATUS_CONNECTION_REFUSED, ECONNREFUSED},
275 {NT_STATUS_REGISTRY_QUOTA_LIMIT, EDQUOT},
276 {NT_STATUS_LICENSE_QUOTA_EXCEEDED, EDQUOT},
277 {NT_STATUS_TOO_MANY_LINKS, EMLINK},
278 {NT_STATUS_NETWORK_BUSY, EBUSY},
279 {NT_STATUS_DEVICE_DOES_NOT_EXIST, ENODEV},
281 {NT_STATUS_DLL_NOT_FOUND, ELIBACC},
283 {NT_STATUS_PIPE_BROKEN, EPIPE},
284 {NT_STATUS_REMOTE_NOT_LISTENING, ECONNREFUSED},
285 {NT_STATUS_NETWORK_ACCESS_DENIED, EACCES},
286 {NT_STATUS_TOO_MANY_OPENED_FILES, EMFILE},
288 {NT_STATUS_DEVICE_PROTOCOL_ERROR, EPROTO},
290 {NT_STATUS_FLOAT_OVERFLOW, ERANGE},
291 {NT_STATUS_FLOAT_UNDERFLOW, ERANGE},
292 {NT_STATUS_INTEGER_OVERFLOW, ERANGE},
293 {NT_STATUS_MEDIA_WRITE_PROTECTED, EROFS},
294 {NT_STATUS_PIPE_CONNECTED, EISCONN},
295 {NT_STATUS_MEMORY_NOT_ALLOCATED, EFAULT},
296 {NT_STATUS_FLOAT_INEXACT_RESULT, ERANGE},
297 {NT_STATUS_ILL_FORMED_PASSWORD, EACCES},
298 {NT_STATUS_PASSWORD_RESTRICTION, EACCES},
299 {NT_STATUS_ACCOUNT_RESTRICTION, EACCES},
300 {NT_STATUS_PORT_CONNECTION_REFUSED, ECONNREFUSED},
301 {NT_STATUS_NAME_TOO_LONG, ENAMETOOLONG},
302 {NT_STATUS_REMOTE_DISCONNECT, ESHUTDOWN},
303 {NT_STATUS_CONNECTION_DISCONNECTED, ECONNABORTED},
304 {NT_STATUS_CONNECTION_RESET, ENETRESET},
306 {NT_STATUS_IP_ADDRESS_CONFLICT1, ENOTUNIQ},
307 {NT_STATUS_IP_ADDRESS_CONFLICT2, ENOTUNIQ},
309 {NT_STATUS_PORT_MESSAGE_TOO_LONG, EMSGSIZE},
310 {NT_STATUS_PROTOCOL_UNREACHABLE, ENOPROTOOPT},
311 {NT_STATUS_ADDRESS_ALREADY_EXISTS, EADDRINUSE},
312 {NT_STATUS_PORT_UNREACHABLE, EHOSTUNREACH},
313 {NT_STATUS_IO_TIMEOUT, ETIMEDOUT},
314 {NT_STATUS_RETRY, EAGAIN},
316 {NT_STATUS_NET_WRITE_FAULT, ECOMM},
322 static int cli_errno_from_nt(NTSTATUS status)
325 DEBUG(10,("cli_errno_from_nt: 32 bit codes: code=%08x\n", NT_STATUS_V(status)));
327 /* Status codes without this bit set are not errors */
329 if (!(NT_STATUS_V(status) & 0xc0000000))
332 for (i=0;nt_errno_map[i].error;i++) {
333 if (NT_STATUS_V(nt_errno_map[i].status) ==
334 NT_STATUS_V(status)) return nt_errno_map[i].error;
337 /* for all other cases - a default code */
341 /* Return a UNIX errno appropriate for the error received in the last
344 int cli_errno(struct cli_state *cli)
348 if (cli_is_dos_error(cli)) {
352 cli_dos_error(cli, &eclass, &ecode);
353 return cli_errno_from_dos(eclass, ecode);
356 status = cli_nt_error(cli);
358 return cli_errno_from_nt(status);
361 /* Return true if the last packet was in error */
363 BOOL cli_is_error(struct cli_state *cli)
365 uint32 flgs2 = SVAL(cli->inbuf,smb_flg2), rcls = 0;
367 if (flgs2 & FLAGS2_32_BIT_ERROR_CODES) {
368 /* Return error is error bits are set */
369 rcls = IVAL(cli->inbuf, smb_rcls);
370 return (rcls & 0xF0000000) == 0xC0000000;
373 /* Return error if error class in non-zero */
375 rcls = CVAL(cli->inbuf, smb_rcls);
379 /* Return true if the last error was an NT error */
381 BOOL cli_is_nt_error(struct cli_state *cli)
383 uint32 flgs2 = SVAL(cli->inbuf,smb_flg2);
385 return cli_is_error(cli) && (flgs2 & FLAGS2_32_BIT_ERROR_CODES);
388 /* Return true if the last error was a DOS error */
390 BOOL cli_is_dos_error(struct cli_state *cli)
392 uint32 flgs2 = SVAL(cli->inbuf,smb_flg2);
394 return cli_is_error(cli) && !(flgs2 & FLAGS2_32_BIT_ERROR_CODES);