few edits
[Samba.git] / source / libsmb / clierror.c
blob30a4344555eb021f65cd5db6adc5baa621556035
1 /*
2 Unix SMB/Netbios implementation.
3 Version 3.0
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.
22 #define NO_SYSLOG
24 #include "includes.h"
26 /*****************************************************
27 RAP error codes - a small start but will be extended.
28 *******************************************************/
30 static const struct
32 int err;
33 char *message;
34 } rap_errmap[] = {
35 {5, "RAP5: User has insufficient privilege" },
36 {50, "RAP50: Not supported by server" },
37 {65, "RAP65: Access denied" },
38 {86, "RAP86: The specified password is invalid" },
39 {2220, "RAP2220: Group does not exist" },
40 {2221, "RAP2221: User does not exist" },
41 {2226, "RAP2226: Operation only permitted on a Primary Domain Controller" },
42 {2237, "RAP2237: User is not in group" },
43 {2242, "RAP2242: The password of this user has expired." },
44 {2243, "RAP2243: The password of this user cannot change." },
45 {2244, "RAP2244: This password cannot be used now (password history conflict)." },
46 {2245, "RAP2245: The password is shorter than required." },
47 {2246, "RAP2246: The password of this user is too recent to change."},
49 /* these really shouldn't be here ... */
50 {0x80, "Not listening on called name"},
51 {0x81, "Not listening for calling name"},
52 {0x82, "Called name not present"},
53 {0x83, "Called name present, but insufficient resources"},
55 {0, NULL}
56 };
58 /****************************************************************************
59 return a description of an SMB error
60 ****************************************************************************/
61 static char *cli_smb_errstr(struct cli_state *cli)
63 return smb_dos_errstr(cli->inbuf);
66 /***************************************************************************
67 Return an error message - either an NT error, SMB error or a RAP error.
68 Note some of the NT errors are actually warnings or "informational" errors
69 in which case they can be safely ignored.
70 ****************************************************************************/
72 char *cli_errstr(struct cli_state *cli)
74 static fstring cli_error_message;
75 uint32 flgs2 = SVAL(cli->inbuf,smb_flg2), errnum;
76 uint8 errclass;
77 int i;
79 if (!cli->initialised) {
80 fstrcpy(cli_error_message, "[Programmer's error] cli_errstr called on unitialized cli_stat struct!\n");
81 return cli_error_message;
84 /* Was it server socket error ? */
85 if (cli->fd == -1 && cli->smb_rw_error) {
86 switch(cli->smb_rw_error) {
87 case READ_TIMEOUT:
88 slprintf(cli_error_message, sizeof(cli_error_message) - 1,
89 "Call timed out: server did not respond after %d milliseconds",
90 cli->timeout);
91 break;
92 case READ_EOF:
93 slprintf(cli_error_message, sizeof(cli_error_message) - 1,
94 "Call returned zero bytes (EOF)\n" );
95 break;
96 case READ_ERROR:
97 slprintf(cli_error_message, sizeof(cli_error_message) - 1,
98 "Read error: %s\n", strerror(errno) );
99 break;
100 case WRITE_ERROR:
101 slprintf(cli_error_message, sizeof(cli_error_message) - 1,
102 "Write error: %s\n", strerror(errno) );
103 break;
104 default:
105 slprintf(cli_error_message, sizeof(cli_error_message) - 1,
106 "Unknown error code %d\n", cli->smb_rw_error );
107 break;
109 return cli_error_message;
112 /* Case #1: RAP error */
113 if (cli->rap_error) {
114 for (i = 0; rap_errmap[i].message != NULL; i++) {
115 if (rap_errmap[i].err == cli->rap_error) {
116 return rap_errmap[i].message;
120 slprintf(cli_error_message, sizeof(cli_error_message) - 1, "RAP code %d",
121 cli->rap_error);
123 return cli_error_message;
126 /* Case #2: 32-bit NT errors */
127 if (flgs2 & FLAGS2_32_BIT_ERROR_CODES) {
128 NTSTATUS status = NT_STATUS(IVAL(cli->inbuf,smb_rcls));
130 return get_nt_error_msg(status);
133 cli_dos_error(cli, &errclass, &errnum);
135 /* Case #3: SMB error */
137 return cli_smb_errstr(cli);
141 /* Return the 32-bit NT status code from the last packet */
142 NTSTATUS cli_nt_error(struct cli_state *cli)
144 int flgs2 = SVAL(cli->inbuf,smb_flg2);
146 if (!(flgs2 & FLAGS2_32_BIT_ERROR_CODES)) {
147 int class = CVAL(cli->inbuf,smb_rcls);
148 int code = SVAL(cli->inbuf,smb_err);
149 return dos_to_ntstatus(class, code);
152 return NT_STATUS(IVAL(cli->inbuf,smb_rcls));
156 /* Return the DOS error from the last packet - an error class and an error
157 code. */
158 void cli_dos_error(struct cli_state *cli, uint8 *eclass, uint32 *ecode)
160 int flgs2;
161 char rcls;
162 int code;
164 if(!cli->initialised) return;
166 flgs2 = SVAL(cli->inbuf,smb_flg2);
168 if (flgs2 & FLAGS2_32_BIT_ERROR_CODES) {
169 NTSTATUS ntstatus = NT_STATUS(IVAL(cli->inbuf, smb_rcls));
170 ntstatus_to_dos(ntstatus, eclass, ecode);
171 return;
174 rcls = CVAL(cli->inbuf,smb_rcls);
175 code = SVAL(cli->inbuf,smb_err);
177 if (eclass) *eclass = rcls;
178 if (ecode) *ecode = code;
181 /* Return a UNIX errno from a dos error class, error number tuple */
183 int cli_errno_from_dos(uint8 eclass, uint32 num)
185 if (eclass == ERRDOS) {
186 switch (num) {
187 case ERRbadfile: return ENOENT;
188 case ERRbadpath: return ENOTDIR;
189 case ERRnoaccess: return EACCES;
190 case ERRfilexists: return EEXIST;
191 case ERRrename: return EEXIST;
192 case ERRbadshare: return EBUSY;
193 case ERRlock: return EBUSY;
194 case ERRinvalidname: return ENOENT;
195 case ERRnosuchshare: return ENODEV;
199 if (eclass == ERRSRV) {
200 switch (num) {
201 case ERRbadpw: return EPERM;
202 case ERRaccess: return EACCES;
203 case ERRnoresource: return ENOMEM;
204 case ERRinvdevice: return ENODEV;
205 case ERRinvnetname: return ENODEV;
209 /* for other cases */
210 return EINVAL;
213 /* Return a UNIX errno from a NT status code */
214 static struct {
215 NTSTATUS status;
216 int error;
217 } nt_errno_map[] = {
218 {NT_STATUS_ACCESS_VIOLATION, EACCES},
219 {NT_STATUS_NO_SUCH_FILE, ENOENT},
220 {NT_STATUS_NO_SUCH_DEVICE, ENODEV},
221 {NT_STATUS_INVALID_HANDLE, EBADF},
222 {NT_STATUS_NO_MEMORY, ENOMEM},
223 {NT_STATUS_ACCESS_DENIED, EACCES},
224 {NT_STATUS_OBJECT_NAME_NOT_FOUND, ENOENT},
225 {NT_STATUS_SHARING_VIOLATION, EBUSY},
226 {NT_STATUS_OBJECT_PATH_INVALID, ENOTDIR},
227 {NT_STATUS_OBJECT_NAME_COLLISION, EEXIST},
228 {NT_STATUS_PATH_NOT_COVERED, ENOENT},
229 {NT_STATUS(0), 0}
232 int cli_errno_from_nt(NTSTATUS status)
234 int i;
235 DEBUG(10,("cli_errno_from_nt: 32 bit codes: code=%08x\n", NT_STATUS_V(status)));
237 /* Status codes without this bit set are not errors */
239 if (!(NT_STATUS_V(status) & 0xc0000000))
240 return 0;
242 for (i=0;nt_errno_map[i].error;i++) {
243 if (NT_STATUS_V(nt_errno_map[i].status) ==
244 NT_STATUS_V(status)) return nt_errno_map[i].error;
247 /* for all other cases - a default code */
248 return EINVAL;
251 /* Return a UNIX errno appropriate for the error received in the last
252 packet. */
254 int cli_errno(struct cli_state *cli)
256 NTSTATUS status;
258 if (cli_is_dos_error(cli)) {
259 uint8 eclass;
260 uint32 ecode;
262 cli_dos_error(cli, &eclass, &ecode);
263 return cli_errno_from_dos(eclass, ecode);
266 status = cli_nt_error(cli);
268 return cli_errno_from_nt(status);
271 /* Return true if the last packet was in error */
273 BOOL cli_is_error(struct cli_state *cli)
275 uint32 flgs2 = SVAL(cli->inbuf,smb_flg2), rcls = 0;
277 if (flgs2 & FLAGS2_32_BIT_ERROR_CODES) {
278 /* Return error is error bits are set */
279 rcls = IVAL(cli->inbuf, smb_rcls);
280 return (rcls & 0xF0000000) == 0xC0000000;
283 /* Return error if error class in non-zero */
285 rcls = CVAL(cli->inbuf, smb_rcls);
286 return rcls != 0;
289 /* Return true if the last error was an NT error */
291 BOOL cli_is_nt_error(struct cli_state *cli)
293 uint32 flgs2 = SVAL(cli->inbuf,smb_flg2);
295 return cli_is_error(cli) && (flgs2 & FLAGS2_32_BIT_ERROR_CODES);
298 /* Return true if the last error was a DOS error */
300 BOOL cli_is_dos_error(struct cli_state *cli)
302 uint32 flgs2 = SVAL(cli->inbuf,smb_flg2);
304 return cli_is_error(cli) && !(flgs2 & FLAGS2_32_BIT_ERROR_CODES);