2 Unix SMB/Netbios implementation.
4 client error handling routines
5 Copyright (C) Andrew Tridgell 1994-1998
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 extern int DEBUGLEVEL;
28 /*****************************************************
29 RAP error codes - a small start but will be extended.
30 *******************************************************/
38 {5, "User has insufficient privilege" },
39 {86, "The specified password is invalid" },
40 {2226, "Operation only permitted on a Primary Domain Controller" },
41 {2242, "The password of this user has expired." },
42 {2243, "The password of this user cannot change." },
43 {2244, "This password cannot be used now (password history conflict)." },
44 {2245, "The password is shorter than required." },
45 {2246, "The password of this user is too recent to change."},
47 /* these really shouldn't be here ... */
48 {0x80, "Not listening on called name"},
49 {0x81, "Not listening for calling name"},
50 {0x82, "Called name not present"},
51 {0x83, "Called name present, but insufficient resources"},
56 /****************************************************************************
57 return a description of an SMB error
58 ****************************************************************************/
59 static char *cli_smb_errstr(struct cli_state *cli)
61 return smb_errstr(cli->inbuf);
64 /***************************************************************************
65 Return an error message - either an NT error, SMB error or a RAP error.
66 Note some of the NT errors are actually warnings or "informational" errors
67 in which case they can be safely ignored.
68 ****************************************************************************/
70 char *cli_errstr(struct cli_state *cli)
72 static fstring error_message;
73 uint32 flgs2 = SVAL(cli->inbuf,smb_flg2), errnum;
77 /* Case #1: 32-bit NT errors */
79 if (flgs2 & FLAGS2_32_BIT_ERROR_CODES) {
80 uint32 status = IVAL(cli->inbuf,smb_rcls);
82 return get_nt_error_msg(status);
85 cli_dos_error(cli, &errclass, &errnum);
87 /* Case #2: SMB error */
90 return cli_smb_errstr(cli);
92 /* Case #3: RAP error */
94 for (i = 0; rap_errmap[i].message != NULL; i++) {
95 if (rap_errmap[i].err == cli->rap_error) {
96 return rap_errmap[i].message;
100 slprintf(error_message, sizeof(error_message) - 1, "code %d",
103 return error_message;
106 /* Return the 32-bit NT status code from the last packet */
108 uint32 cli_nt_error(struct cli_state *cli)
110 int flgs2 = SVAL(cli->inbuf,smb_flg2);
112 if (!(flgs2 & FLAGS2_32_BIT_ERROR_CODES)) {
114 /* Eek! We've requested a NT error when the packet that
115 came back does not contain one. What do we return
118 DEBUG(1, ("ERROR: cli_error() called to read a status code "
119 "from a packet that does not contain one!\n"));
121 return NT_STATUS_UNSUCCESSFUL;
124 return IVAL(cli->inbuf,smb_rcls);
127 /* Return the DOS error from the last packet - an error class and an error
130 void cli_dos_error(struct cli_state *cli, uint8 *eclass, uint32 *num)
142 if(!cli->initialised)
148 flgs2 = SVAL(cli->inbuf,smb_flg2);
150 if (flgs2 & FLAGS2_32_BIT_ERROR_CODES) {
151 /* Eek! We've requested a DOS error when the packet that
152 came back does not contain one. What do we return
155 DEBUG(1, ("ERROR: cli_error() called to read a dos error code "
156 "from a packet that does not contain one!\n"));
161 rcls = CVAL(cli->inbuf,smb_rcls);
162 code = SVAL(cli->inbuf,smb_err);
167 if (eclass) *eclass = rcls;
168 if (num ) *num = code;
171 /* Return a UNIX errno from a dos error class, error number tuple */
173 int cli_errno_from_dos(uint8 eclass, uint32 num)
175 if (eclass == ERRDOS) {
177 case ERRbadfile: return ENOENT;
178 case ERRbadpath: return ENOTDIR;
179 case ERRnoaccess: return EACCES;
180 case ERRfilexists: return EEXIST;
181 case ERRrename: return EEXIST;
182 case ERRbadshare: return EBUSY;
183 case ERRlock: return EBUSY;
184 case ERRinvalidname: return ENOENT;
185 case ERRnosuchshare: return ENODEV;
189 if (eclass == ERRSRV) {
191 case ERRbadpw: return EPERM;
192 case ERRaccess: return EACCES;
193 case ERRnoresource: return ENOMEM;
194 case ERRinvdevice: return ENODEV;
195 case ERRinvnetname: return ENODEV;
199 /* for other cases */
203 /* Return a UNIX errno from a NT status code */
205 int cli_errno_from_nt(uint32 status)
207 DEBUG(10,("cli_errno_from_nt: 32 bit codes: code=%08x\n", status));
209 /* Status codes without this bit set are not errors */
211 if (!(status & 0xc0000000))
215 case NT_STATUS_ACCESS_VIOLATION: return EACCES;
216 case NT_STATUS_NO_SUCH_FILE: return ENOENT;
217 case NT_STATUS_NO_SUCH_DEVICE: return ENODEV;
218 case NT_STATUS_INVALID_HANDLE: return EBADF;
219 case NT_STATUS_NO_MEMORY: return ENOMEM;
220 case NT_STATUS_ACCESS_DENIED: return EACCES;
221 case NT_STATUS_OBJECT_NAME_NOT_FOUND: return ENOENT;
222 case NT_STATUS_SHARING_VIOLATION: return EBUSY;
223 case NT_STATUS_OBJECT_PATH_INVALID: return ENOTDIR;
224 case NT_STATUS_OBJECT_NAME_COLLISION: return EEXIST;
225 case NT_STATUS_PATH_NOT_COVERED: return ENOENT;
228 /* for all other cases - a default code */
232 /* Return a UNIX errno appropriate for the error received in the last
235 int cli_errno(struct cli_state *cli)
239 if (cli_is_dos_error) {
243 cli_dos_error(cli, &eclass, &ecode);
244 return cli_errno_from_dos(eclass, ecode);
247 status = cli_nt_error(cli);
249 return cli_errno_from_nt(status);
252 /* Return true if the last packet was in error */
254 BOOL cli_is_error(struct cli_state *cli)
256 uint32 flgs2 = SVAL(cli->inbuf,smb_flg2), rcls = 0;
258 if (flgs2 & FLAGS2_32_BIT_ERROR_CODES)
259 rcls = IVAL(cli->inbuf, smb_rcls);
261 rcls = CVAL(cli->inbuf, smb_rcls);
266 /* Return true if the last error was an NT error */
268 BOOL cli_is_nt_error(struct cli_state *cli)
270 uint32 flgs2 = SVAL(cli->inbuf,smb_flg2);
272 return cli_is_error(cli) && (flgs2 & FLAGS2_32_BIT_ERROR_CODES);
275 /* Return true if the last error was a DOS error */
277 BOOL cli_is_dos_error(struct cli_state *cli)
279 uint32 flgs2 = SVAL(cli->inbuf,smb_flg2);
281 return cli_is_error(cli) && !(flgs2 & FLAGS2_32_BIT_ERROR_CODES);