sync'ing up for 3.0alpha20 release
[Samba/gbeck.git] / source3 / libsmb / clierror.c
blobe1507c6048e6a4c0c2f9e1d26e855145781a59fb
1 /*
2 Unix SMB/CIFS implementation.
3 client error handling routines
4 Copyright (C) Andrew Tridgell 1994-1998
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 #define NO_SYSLOG
23 #include "includes.h"
25 /*****************************************************
26 RAP error codes - a small start but will be extended.
28 XXX: Perhaps these should move into a common function because they're
29 duplicated in clirap2.c
31 *******************************************************/
33 static const struct
35 int err;
36 char *message;
37 } rap_errmap[] =
39 {5, "RAP5: User has insufficient privilege" },
40 {50, "RAP50: Not supported by server" },
41 {65, "RAP65: Access denied" },
42 {86, "RAP86: The specified password is invalid" },
43 {2220, "RAP2220: Group does not exist" },
44 {2221, "RAP2221: User does not exist" },
45 {2226, "RAP2226: Operation only permitted on a Primary Domain Controller" },
46 {2237, "RAP2237: User is not in group" },
47 {2242, "RAP2242: The password of this user has expired." },
48 {2243, "RAP2243: The password of this user cannot change." },
49 {2244, "RAP2244: This password cannot be used now (password history conflict)." },
50 {2245, "RAP2245: The password is shorter than required." },
51 {2246, "RAP2246: The password of this user is too recent to change."},
53 /* these really shouldn't be here ... */
54 {0x80, "Not listening on called name"},
55 {0x81, "Not listening for calling name"},
56 {0x82, "Called name not present"},
57 {0x83, "Called name present, but insufficient resources"},
59 {0, NULL}
60 };
62 /****************************************************************************
63 return a description of an SMB error
64 ****************************************************************************/
65 static char *cli_smb_errstr(struct cli_state *cli)
67 return smb_dos_errstr(cli->inbuf);
70 /***************************************************************************
71 Return an error message - either an NT error, SMB error or a RAP error.
72 Note some of the NT errors are actually warnings or "informational" errors
73 in which case they can be safely ignored.
74 ****************************************************************************/
76 char *cli_errstr(struct cli_state *cli)
78 static fstring cli_error_message;
79 uint32 flgs2 = SVAL(cli->inbuf,smb_flg2), errnum;
80 uint8 errclass;
81 int i;
83 if (!cli->initialised) {
84 fstrcpy(cli_error_message, "[Programmer's error] cli_errstr called on unitialized cli_stat struct!\n");
85 return cli_error_message;
88 /* Case #1: RAP error */
89 if (cli->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, "RAP code %d",
97 cli->rap_error);
99 return cli_error_message;
102 /* Case #2: 32-bit NT errors */
103 if (flgs2 & FLAGS2_32_BIT_ERROR_CODES) {
104 NTSTATUS status = NT_STATUS(IVAL(cli->inbuf,smb_rcls));
106 return nt_errstr(status);
109 cli_dos_error(cli, &errclass, &errnum);
111 /* Case #3: SMB error */
113 return cli_smb_errstr(cli);
117 /* Return the 32-bit NT status code from the last packet */
118 NTSTATUS cli_nt_error(struct cli_state *cli)
120 int flgs2 = SVAL(cli->inbuf,smb_flg2);
122 if (!(flgs2 & FLAGS2_32_BIT_ERROR_CODES)) {
123 int class = CVAL(cli->inbuf,smb_rcls);
124 int code = SVAL(cli->inbuf,smb_err);
125 return dos_to_ntstatus(class, code);
128 return NT_STATUS(IVAL(cli->inbuf,smb_rcls));
132 /* Return the DOS error from the last packet - an error class and an error
133 code. */
134 void cli_dos_error(struct cli_state *cli, uint8 *eclass, uint32 *ecode)
136 int flgs2;
137 char rcls;
138 int code;
140 if(!cli->initialised) return;
142 flgs2 = SVAL(cli->inbuf,smb_flg2);
144 if (flgs2 & FLAGS2_32_BIT_ERROR_CODES) {
145 NTSTATUS ntstatus = NT_STATUS(IVAL(cli->inbuf, smb_rcls));
146 ntstatus_to_dos(ntstatus, eclass, ecode);
147 return;
150 rcls = CVAL(cli->inbuf,smb_rcls);
151 code = SVAL(cli->inbuf,smb_err);
153 if (eclass) *eclass = rcls;
154 if (ecode) *ecode = code;
157 /* Return a UNIX errno from a dos error class, error number tuple */
159 static int cli_errno_from_dos(uint8 eclass, uint32 num)
161 if (eclass == ERRDOS) {
162 switch (num) {
163 case ERRbadfile: return ENOENT;
164 case ERRbadpath: return ENOTDIR;
165 case ERRnoaccess: return EACCES;
166 case ERRfilexists: return EEXIST;
167 case ERRrename: return EEXIST;
168 case ERRbadshare: return EBUSY;
169 case ERRlock: return EBUSY;
170 case ERRinvalidname: return ENOENT;
171 case ERRnosuchshare: return ENODEV;
175 if (eclass == ERRSRV) {
176 switch (num) {
177 case ERRbadpw: return EPERM;
178 case ERRaccess: return EACCES;
179 case ERRnoresource: return ENOMEM;
180 case ERRinvdevice: return ENODEV;
181 case ERRinvnetname: return ENODEV;
185 /* for other cases */
186 return EINVAL;
189 /* Return a UNIX errno from a NT status code */
190 static struct {
191 NTSTATUS status;
192 int error;
193 } nt_errno_map[] = {
194 {NT_STATUS_ACCESS_VIOLATION, EACCES},
195 {NT_STATUS_NO_SUCH_FILE, ENOENT},
196 {NT_STATUS_NO_SUCH_DEVICE, ENODEV},
197 {NT_STATUS_INVALID_HANDLE, EBADF},
198 {NT_STATUS_NO_MEMORY, ENOMEM},
199 {NT_STATUS_ACCESS_DENIED, EACCES},
200 {NT_STATUS_OBJECT_NAME_NOT_FOUND, ENOENT},
201 {NT_STATUS_SHARING_VIOLATION, EBUSY},
202 {NT_STATUS_OBJECT_PATH_INVALID, ENOTDIR},
203 {NT_STATUS_OBJECT_NAME_COLLISION, EEXIST},
204 {NT_STATUS_PATH_NOT_COVERED, ENOENT},
205 {NT_STATUS(0), 0}
208 static int cli_errno_from_nt(NTSTATUS status)
210 int i;
211 DEBUG(10,("cli_errno_from_nt: 32 bit codes: code=%08x\n", NT_STATUS_V(status)));
213 /* Status codes without this bit set are not errors */
215 if (!(NT_STATUS_V(status) & 0xc0000000))
216 return 0;
218 for (i=0;nt_errno_map[i].error;i++) {
219 if (NT_STATUS_V(nt_errno_map[i].status) ==
220 NT_STATUS_V(status)) return nt_errno_map[i].error;
223 /* for all other cases - a default code */
224 return EINVAL;
227 /* Return a UNIX errno appropriate for the error received in the last
228 packet. */
230 int cli_errno(struct cli_state *cli)
232 NTSTATUS status;
234 if (cli_is_dos_error(cli)) {
235 uint8 eclass;
236 uint32 ecode;
238 cli_dos_error(cli, &eclass, &ecode);
239 return cli_errno_from_dos(eclass, ecode);
242 status = cli_nt_error(cli);
244 return cli_errno_from_nt(status);
247 /* Return true if the last packet was in error */
249 BOOL cli_is_error(struct cli_state *cli)
251 uint32 flgs2 = SVAL(cli->inbuf,smb_flg2), rcls = 0;
253 if (flgs2 & FLAGS2_32_BIT_ERROR_CODES) {
254 /* Return error is error bits are set */
255 rcls = IVAL(cli->inbuf, smb_rcls);
256 return (rcls & 0xF0000000) == 0xC0000000;
259 /* Return error if error class in non-zero */
261 rcls = CVAL(cli->inbuf, smb_rcls);
262 return rcls != 0;
265 /* Return true if the last error was an NT error */
267 BOOL cli_is_nt_error(struct cli_state *cli)
269 uint32 flgs2 = SVAL(cli->inbuf,smb_flg2);
271 return cli_is_error(cli) && (flgs2 & FLAGS2_32_BIT_ERROR_CODES);
274 /* Return true if the last error was a DOS error */
276 BOOL cli_is_dos_error(struct cli_state *cli)
278 uint32 flgs2 = SVAL(cli->inbuf,smb_flg2);
280 return cli_is_error(cli) && !(flgs2 & FLAGS2_32_BIT_ERROR_CODES);