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 /*****************************************************
27 RAP error codes - a small start but will be extended.
28 *******************************************************/
36 {5, "User has insufficient privilege" },
37 {86, "The specified password is invalid" },
38 {2226, "Operation only permitted on a Primary Domain Controller" },
39 {2242, "The password of this user has expired." },
40 {2243, "The password of this user cannot change." },
41 {2244, "This password cannot be used now (password history conflict)." },
42 {2245, "The password is shorter than required." },
43 {2246, "The password of this user is too recent to change."},
45 /* these really shouldn't be here ... */
46 {0x80, "Not listening on called name"},
47 {0x81, "Not listening for calling name"},
48 {0x82, "Called name not present"},
49 {0x83, "Called name present, but insufficient resources"},
54 /****************************************************************************
55 return a description of an SMB error
56 ****************************************************************************/
57 static char *cli_smb_errstr(struct cli_state *cli)
59 return smb_dos_errstr(cli->inbuf);
62 /***************************************************************************
63 Return an error message - either an NT error, SMB error or a RAP error.
64 Note some of the NT errors are actually warnings or "informational" errors
65 in which case they can be safely ignored.
66 ****************************************************************************/
68 char *cli_errstr(struct cli_state *cli)
70 static fstring cli_error_message;
71 uint32 flgs2 = SVAL(cli->inbuf,smb_flg2), errnum;
75 /* Case #1: 32-bit NT errors */
76 if (flgs2 & FLAGS2_32_BIT_ERROR_CODES) {
77 NTSTATUS status = NT_STATUS(IVAL(cli->inbuf,smb_rcls));
79 return get_nt_error_msg(status);
82 cli_dos_error(cli, &errclass, &errnum);
84 /* Case #2: SMB error */
87 return cli_smb_errstr(cli);
89 /* Case #3: RAP error */
90 for (i = 0; rap_errmap[i].message != NULL; i++) {
91 if (rap_errmap[i].err == cli->rap_error) {
92 return rap_errmap[i].message;
96 slprintf(cli_error_message, sizeof(cli_error_message) - 1, "code %d",
99 return cli_error_message;
103 /* Return the 32-bit NT status code from the last packet */
104 NTSTATUS cli_nt_error(struct cli_state *cli)
106 int flgs2 = SVAL(cli->inbuf,smb_flg2);
108 if (!(flgs2 & FLAGS2_32_BIT_ERROR_CODES)) {
109 int class = CVAL(cli->inbuf,smb_rcls);
110 int code = SVAL(cli->inbuf,smb_err);
111 return dos_to_ntstatus(class, code);
114 return NT_STATUS(IVAL(cli->inbuf,smb_rcls));
118 /* Return the DOS error from the last packet - an error class and an error
120 void cli_dos_error(struct cli_state *cli, uint8 *eclass, uint32 *ecode)
126 if(!cli->initialised) return;
128 flgs2 = SVAL(cli->inbuf,smb_flg2);
130 if (flgs2 & FLAGS2_32_BIT_ERROR_CODES) {
131 NTSTATUS ntstatus = NT_STATUS(IVAL(cli->inbuf, smb_rcls));
132 ntstatus_to_dos(ntstatus, eclass, ecode);
136 rcls = CVAL(cli->inbuf,smb_rcls);
137 code = SVAL(cli->inbuf,smb_err);
139 if (eclass) *eclass = rcls;
140 if (ecode) *ecode = code;
143 /* Return a UNIX errno from a dos error class, error number tuple */
145 int cli_errno_from_dos(uint8 eclass, uint32 num)
147 if (eclass == ERRDOS) {
149 case ERRbadfile: return ENOENT;
150 case ERRbadpath: return ENOTDIR;
151 case ERRnoaccess: return EACCES;
152 case ERRfilexists: return EEXIST;
153 case ERRrename: return EEXIST;
154 case ERRbadshare: return EBUSY;
155 case ERRlock: return EBUSY;
156 case ERRinvalidname: return ENOENT;
157 case ERRnosuchshare: return ENODEV;
161 if (eclass == ERRSRV) {
163 case ERRbadpw: return EPERM;
164 case ERRaccess: return EACCES;
165 case ERRnoresource: return ENOMEM;
166 case ERRinvdevice: return ENODEV;
167 case ERRinvnetname: return ENODEV;
171 /* for other cases */
175 /* Return a UNIX errno from a NT status code */
180 {NT_STATUS_ACCESS_VIOLATION, EACCES},
181 {NT_STATUS_NO_SUCH_FILE, ENOENT},
182 {NT_STATUS_NO_SUCH_DEVICE, ENODEV},
183 {NT_STATUS_INVALID_HANDLE, EBADF},
184 {NT_STATUS_NO_MEMORY, ENOMEM},
185 {NT_STATUS_ACCESS_DENIED, EACCES},
186 {NT_STATUS_OBJECT_NAME_NOT_FOUND, ENOENT},
187 {NT_STATUS_SHARING_VIOLATION, EBUSY},
188 {NT_STATUS_OBJECT_PATH_INVALID, ENOTDIR},
189 {NT_STATUS_OBJECT_NAME_COLLISION, EEXIST},
190 {NT_STATUS_PATH_NOT_COVERED, ENOENT},
194 int cli_errno_from_nt(NTSTATUS status)
197 DEBUG(10,("cli_errno_from_nt: 32 bit codes: code=%08x\n", NT_STATUS_V(status)));
199 /* Status codes without this bit set are not errors */
201 if (!(NT_STATUS_V(status) & 0xc0000000))
204 for (i=0;nt_errno_map[i].error;i++) {
205 if (NT_STATUS_V(nt_errno_map[i].status) ==
206 NT_STATUS_V(status)) return nt_errno_map[i].error;
209 /* for all other cases - a default code */
213 /* Return a UNIX errno appropriate for the error received in the last
216 int cli_errno(struct cli_state *cli)
220 if (cli_is_dos_error(cli)) {
224 cli_dos_error(cli, &eclass, &ecode);
225 return cli_errno_from_dos(eclass, ecode);
228 status = cli_nt_error(cli);
230 return cli_errno_from_nt(status);
233 /* Return true if the last packet was in error */
235 BOOL cli_is_error(struct cli_state *cli)
237 uint32 flgs2 = SVAL(cli->inbuf,smb_flg2), rcls = 0;
239 if (flgs2 & FLAGS2_32_BIT_ERROR_CODES) {
240 /* Return error is error bits are set */
241 rcls = IVAL(cli->inbuf, smb_rcls);
242 return (rcls & 0xF0000000) == 0xC0000000;
245 /* Return error if error class in non-zero */
247 rcls = CVAL(cli->inbuf, smb_rcls);
251 /* Return true if the last error was an NT error */
253 BOOL cli_is_nt_error(struct cli_state *cli)
255 uint32 flgs2 = SVAL(cli->inbuf,smb_flg2);
257 return cli_is_error(cli) && (flgs2 & FLAGS2_32_BIT_ERROR_CODES);
260 /* Return true if the last error was a DOS error */
262 BOOL cli_is_dos_error(struct cli_state *cli)
264 uint32 flgs2 = SVAL(cli->inbuf,smb_flg2);
266 return cli_is_error(cli) && !(flgs2 & FLAGS2_32_BIT_ERROR_CODES);