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.
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 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 char *cli_errstr(struct cli_state *cli)
79 static fstring cli_error_message;
80 uint32 flgs2 = SVAL(cli->inbuf,smb_flg2), errnum;
84 /* Case #1: RAP error */
86 for (i = 0; rap_errmap[i].message != NULL; i++) {
87 if (rap_errmap[i].err == cli->rap_error) {
88 return rap_errmap[i].message;
92 slprintf(cli_error_message, sizeof(cli_error_message) - 1, "RAP code %d",
95 return cli_error_message;
98 /* Case #2: 32-bit NT errors */
99 if (flgs2 & FLAGS2_32_BIT_ERROR_CODES) {
100 NTSTATUS status = NT_STATUS(IVAL(cli->inbuf,smb_rcls));
102 return get_nt_error_msg(status);
105 cli_dos_error(cli, &errclass, &errnum);
107 /* Case #3: SMB error */
109 return cli_smb_errstr(cli);
113 /* Return the 32-bit NT status code from the last packet */
114 NTSTATUS cli_nt_error(struct cli_state *cli)
116 int flgs2 = SVAL(cli->inbuf,smb_flg2);
118 if (!(flgs2 & FLAGS2_32_BIT_ERROR_CODES)) {
119 int class = CVAL(cli->inbuf,smb_rcls);
120 int code = SVAL(cli->inbuf,smb_err);
121 return dos_to_ntstatus(class, code);
124 return NT_STATUS(IVAL(cli->inbuf,smb_rcls));
128 /* 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 *ecode)
136 if(!cli->initialised) return;
138 flgs2 = SVAL(cli->inbuf,smb_flg2);
140 if (flgs2 & FLAGS2_32_BIT_ERROR_CODES) {
141 NTSTATUS ntstatus = NT_STATUS(IVAL(cli->inbuf, smb_rcls));
142 ntstatus_to_dos(ntstatus, eclass, ecode);
146 rcls = CVAL(cli->inbuf,smb_rcls);
147 code = SVAL(cli->inbuf,smb_err);
149 if (eclass) *eclass = rcls;
150 if (ecode) *ecode = code;
153 /* Return a UNIX errno from a dos error class, error number tuple */
155 int cli_errno_from_dos(uint8 eclass, uint32 num)
157 if (eclass == ERRDOS) {
159 case ERRbadfile: return ENOENT;
160 case ERRbadpath: return ENOTDIR;
161 case ERRnoaccess: return EACCES;
162 case ERRfilexists: return EEXIST;
163 case ERRrename: return EEXIST;
164 case ERRbadshare: return EBUSY;
165 case ERRlock: return EBUSY;
166 case ERRinvalidname: return ENOENT;
167 case ERRnosuchshare: return ENODEV;
171 if (eclass == ERRSRV) {
173 case ERRbadpw: return EPERM;
174 case ERRaccess: return EACCES;
175 case ERRnoresource: return ENOMEM;
176 case ERRinvdevice: return ENODEV;
177 case ERRinvnetname: return ENODEV;
181 /* for other cases */
185 /* Return a UNIX errno from a NT status code */
190 {NT_STATUS_ACCESS_VIOLATION, EACCES},
191 {NT_STATUS_NO_SUCH_FILE, ENOENT},
192 {NT_STATUS_NO_SUCH_DEVICE, ENODEV},
193 {NT_STATUS_INVALID_HANDLE, EBADF},
194 {NT_STATUS_NO_MEMORY, ENOMEM},
195 {NT_STATUS_ACCESS_DENIED, EACCES},
196 {NT_STATUS_OBJECT_NAME_NOT_FOUND, ENOENT},
197 {NT_STATUS_SHARING_VIOLATION, EBUSY},
198 {NT_STATUS_OBJECT_PATH_INVALID, ENOTDIR},
199 {NT_STATUS_OBJECT_NAME_COLLISION, EEXIST},
200 {NT_STATUS_PATH_NOT_COVERED, ENOENT},
204 int cli_errno_from_nt(NTSTATUS status)
207 DEBUG(10,("cli_errno_from_nt: 32 bit codes: code=%08x\n", NT_STATUS_V(status)));
209 /* Status codes without this bit set are not errors */
211 if (!(NT_STATUS_V(status) & 0xc0000000))
214 for (i=0;nt_errno_map[i].error;i++) {
215 if (NT_STATUS_V(nt_errno_map[i].status) ==
216 NT_STATUS_V(status)) return nt_errno_map[i].error;
219 /* for all other cases - a default code */
223 /* Return a UNIX errno appropriate for the error received in the last
226 int cli_errno(struct cli_state *cli)
230 if (cli_is_dos_error(cli)) {
234 cli_dos_error(cli, &eclass, &ecode);
235 return cli_errno_from_dos(eclass, ecode);
238 status = cli_nt_error(cli);
240 return cli_errno_from_nt(status);
243 /* Return true if the last packet was in error */
245 BOOL cli_is_error(struct cli_state *cli)
247 uint32 flgs2 = SVAL(cli->inbuf,smb_flg2), rcls = 0;
249 if (flgs2 & FLAGS2_32_BIT_ERROR_CODES) {
250 /* Return error is error bits are set */
251 rcls = IVAL(cli->inbuf, smb_rcls);
252 return (rcls & 0xF0000000) == 0xC0000000;
255 /* Return error if error class in non-zero */
257 rcls = CVAL(cli->inbuf, smb_rcls);
261 /* Return true if the last error was an NT error */
263 BOOL cli_is_nt_error(struct cli_state *cli)
265 uint32 flgs2 = SVAL(cli->inbuf,smb_flg2);
267 return cli_is_error(cli) && (flgs2 & FLAGS2_32_BIT_ERROR_CODES);
270 /* Return true if the last error was a DOS error */
272 BOOL cli_is_dos_error(struct cli_state *cli)
274 uint32 flgs2 = SVAL(cli->inbuf,smb_flg2);
276 return cli_is_error(cli) && !(flgs2 & FLAGS2_32_BIT_ERROR_CODES);