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 */
78 if (flgs2 & FLAGS2_32_BIT_ERROR_CODES) {
79 uint32 status = IVAL(cli->inbuf,smb_rcls);
81 return get_nt_error_msg(status);
84 cli_dos_error(cli, &errclass, &errnum);
86 /* Case #2: SMB error */
89 return cli_smb_errstr(cli);
91 /* Case #3: RAP error */
92 for (i = 0; rap_errmap[i].message != NULL; i++) {
93 if (rap_errmap[i].err == cli->rap_error) {
94 return rap_errmap[i].message;
98 slprintf(error_message, sizeof(error_message) - 1, "code %d",
101 return error_message;
105 /* Return the 32-bit NT status code from the last packet */
106 uint32 cli_nt_error(struct cli_state *cli)
108 int flgs2 = SVAL(cli->inbuf,smb_flg2);
110 if (!(flgs2 & FLAGS2_32_BIT_ERROR_CODES)) {
111 int class = CVAL(cli->inbuf,smb_rcls);
112 int code = SVAL(cli->inbuf,smb_err);
113 return dos_to_ntstatus(class, code);
116 return IVAL(cli->inbuf,smb_rcls);
120 /* Return the DOS error from the last packet - an error class and an error
122 void cli_dos_error(struct cli_state *cli, uint8 *eclass, uint32 *ecode)
128 if(!cli->initialised) return;
130 flgs2 = SVAL(cli->inbuf,smb_flg2);
132 if (flgs2 & FLAGS2_32_BIT_ERROR_CODES) {
133 uint32 ntstatus = IVAL(cli->inbuf, smb_rcls);
134 ntstatus_to_dos(ntstatus, eclass, ecode);
138 rcls = CVAL(cli->inbuf,smb_rcls);
139 code = SVAL(cli->inbuf,smb_err);
141 if (eclass) *eclass = rcls;
142 if (ecode) *ecode = code;
145 /* Return a UNIX errno from a dos error class, error number tuple */
147 int cli_errno_from_dos(uint8 eclass, uint32 num)
149 if (eclass == ERRDOS) {
151 case ERRbadfile: return ENOENT;
152 case ERRbadpath: return ENOTDIR;
153 case ERRnoaccess: return EACCES;
154 case ERRfilexists: return EEXIST;
155 case ERRrename: return EEXIST;
156 case ERRbadshare: return EBUSY;
157 case ERRlock: return EBUSY;
158 case ERRinvalidname: return ENOENT;
159 case ERRnosuchshare: return ENODEV;
163 if (eclass == ERRSRV) {
165 case ERRbadpw: return EPERM;
166 case ERRaccess: return EACCES;
167 case ERRnoresource: return ENOMEM;
168 case ERRinvdevice: return ENODEV;
169 case ERRinvnetname: return ENODEV;
173 /* for other cases */
177 /* Return a UNIX errno from a NT status code */
179 int cli_errno_from_nt(uint32 status)
181 DEBUG(10,("cli_errno_from_nt: 32 bit codes: code=%08x\n", status));
183 /* Status codes without this bit set are not errors */
185 if (!(status & 0xc0000000))
189 case NT_STATUS_ACCESS_VIOLATION: return EACCES;
190 case NT_STATUS_NO_SUCH_FILE: return ENOENT;
191 case NT_STATUS_NO_SUCH_DEVICE: return ENODEV;
192 case NT_STATUS_INVALID_HANDLE: return EBADF;
193 case NT_STATUS_NO_MEMORY: return ENOMEM;
194 case NT_STATUS_ACCESS_DENIED: return EACCES;
195 case NT_STATUS_OBJECT_NAME_NOT_FOUND: return ENOENT;
196 case NT_STATUS_SHARING_VIOLATION: return EBUSY;
197 case NT_STATUS_OBJECT_PATH_INVALID: return ENOTDIR;
198 case NT_STATUS_OBJECT_NAME_COLLISION: return EEXIST;
199 case NT_STATUS_PATH_NOT_COVERED: return ENOENT;
202 /* for all other cases - a default code */
206 /* Return a UNIX errno appropriate for the error received in the last
209 int cli_errno(struct cli_state *cli)
213 if (cli_is_dos_error) {
217 cli_dos_error(cli, &eclass, &ecode);
218 return cli_errno_from_dos(eclass, ecode);
221 status = cli_nt_error(cli);
223 return cli_errno_from_nt(status);
226 /* Return true if the last packet was in error */
228 BOOL cli_is_error(struct cli_state *cli)
230 uint32 flgs2 = SVAL(cli->inbuf,smb_flg2), rcls = 0;
232 if (flgs2 & FLAGS2_32_BIT_ERROR_CODES) {
233 /* Return error is error bits are set */
234 rcls = IVAL(cli->inbuf, smb_rcls);
235 return (rcls & 0xF0000000) == 0xC0000000;
238 /* Return error if error class in non-zero */
240 rcls = CVAL(cli->inbuf, smb_rcls);
244 /* Return true if the last error was an NT error */
246 BOOL cli_is_nt_error(struct cli_state *cli)
248 uint32 flgs2 = SVAL(cli->inbuf,smb_flg2);
250 return cli_is_error(cli) && (flgs2 & FLAGS2_32_BIT_ERROR_CODES);
253 /* Return true if the last error was a DOS error */
255 BOOL cli_is_dos_error(struct cli_state *cli)
257 uint32 flgs2 = SVAL(cli->inbuf,smb_flg2);
259 return cli_is_error(cli) && !(flgs2 & FLAGS2_32_BIT_ERROR_CODES);