Had the test for cli_is_error() reversed. You idiot Stimpy!
[samba.git] / source / libsmb / clierror.c
1 /* 
2    Unix SMB/Netbios implementation.
3    Version 3.0
4    client error handling routines
5    Copyright (C) Andrew Tridgell 1994-1998
6    
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.
11    
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.
16    
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.
20 */
21
22 #define NO_SYSLOG
23
24 #include "includes.h"
25
26 extern int DEBUGLEVEL;
27
28 /*****************************************************
29  RAP error codes - a small start but will be extended.
30 *******************************************************/
31
32 static struct
33 {
34   int err;
35   char *message;
36 } rap_errmap[] =
37 {
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."},
46
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"},
52
53   {0, NULL}
54 };  
55
56 /****************************************************************************
57   return a description of an SMB error
58 ****************************************************************************/
59 static char *cli_smb_errstr(struct cli_state *cli)
60 {
61         return smb_errstr(cli->inbuf);
62 }
63
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 ****************************************************************************/
69     
70 char *cli_errstr(struct cli_state *cli)
71 {   
72         static fstring error_message;
73         uint32 flgs2 = SVAL(cli->inbuf,smb_flg2), errnum;
74         uint8 errclass;
75         int i;
76
77         /* Case #1: 32-bit NT errors */
78
79         if (flgs2 & FLAGS2_32_BIT_ERROR_CODES) {
80                 uint32 status = IVAL(cli->inbuf,smb_rcls);
81
82                 return get_nt_error_msg(status);
83         }
84
85         cli_dos_error(cli, &errclass, &errnum);
86
87         /* Case #2: SMB error */
88
89         if (errclass != 0)
90                 return cli_smb_errstr(cli);
91
92         /* Case #3: RAP error */
93
94         for (i = 0; rap_errmap[i].message != NULL; i++) {
95                 if (rap_errmap[i].err == cli->rap_error) {
96                         return rap_errmap[i].message;
97                 }
98         } 
99
100         slprintf(error_message, sizeof(error_message) - 1, "code %d", 
101                  cli->rap_error);
102
103         return error_message;
104 }
105
106 /* Return the 32-bit NT status code from the last packet */
107
108 uint32 cli_nt_error(struct cli_state *cli)
109 {
110         int flgs2 = SVAL(cli->inbuf,smb_flg2);
111
112         if (!(flgs2 & FLAGS2_32_BIT_ERROR_CODES)) {
113
114                 /* Eek!  We've requested a NT error when the packet that
115                    came back does not contain one.  What do we return 
116                    here? */
117
118                 DEBUG(1, ("ERROR: cli_error() called to read a status code "
119                           "from a packet that does not contain one!\n"));
120
121                 return NT_STATUS_UNSUCCESSFUL;
122         }
123
124         return IVAL(cli->inbuf,smb_rcls);
125 }
126
127 /* Return the DOS error from the last packet - an error class and an error
128    code. */
129
130 void cli_dos_error(struct cli_state *cli, uint8 *eclass, uint32 *num)
131 {
132         int  flgs2;
133         char rcls;
134         int code;
135
136         if (eclass) 
137                 *eclass = 0;
138
139         if (num) 
140                 *num = 0;
141
142         if(!cli->initialised)
143                 return;
144
145         if(!cli->inbuf)
146                 return;
147
148         flgs2 = SVAL(cli->inbuf,smb_flg2);
149
150         if (flgs2 & FLAGS2_32_BIT_ERROR_CODES) {
151                 /* Eek!  We've requested a DOS error when the packet that
152                    came back does not contain one.  What do we return 
153                    here? */
154
155                 DEBUG(1, ("ERROR: cli_error() called to read a dos error code "
156                           "from a packet that does not contain one!\n"));
157
158                 return;
159         }
160
161         rcls  = CVAL(cli->inbuf,smb_rcls);
162         code  = SVAL(cli->inbuf,smb_err);
163
164         if (rcls == 0) 
165                 return;
166
167         if (eclass) *eclass = rcls;
168         if (num   ) *num    = code;
169 }
170
171 /* Return a UNIX errno from a dos error class, error number tuple */
172
173 int cli_errno_from_dos(uint8 eclass, uint32 num)
174 {
175         if (eclass == ERRDOS) {
176                 switch (num) {
177                 case ERRbadfile: return ENOENT;
178                 case ERRbadpath: return ENOTDIR;
179                 case ERRnoaccess: return EACCES;
180                 case ERRfilexists: return EEXIST;
181                 case ERRrename: return EEXIST;
182                 case ERRbadshare: return EBUSY;
183                 case ERRlock: return EBUSY;
184                 case ERRinvalidname: return ENOENT;
185                 case ERRnosuchshare: return ENODEV;
186                 }
187         }
188
189         if (eclass == ERRSRV) {
190                 switch (num) {
191                 case ERRbadpw: return EPERM;
192                 case ERRaccess: return EACCES;
193                 case ERRnoresource: return ENOMEM;
194                 case ERRinvdevice: return ENODEV;
195                 case ERRinvnetname: return ENODEV;
196                 }
197         }
198
199         /* for other cases */
200         return EINVAL;
201 }
202
203 /* Return a UNIX errno from a NT status code */
204
205 int cli_errno_from_nt(uint32 status)
206 {
207         DEBUG(10,("cli_errno_from_nt: 32 bit codes: code=%08x\n", status));
208
209         /* Status codes without this bit set are not errors */
210
211         if (!(status & 0xc0000000))
212                 return 0;
213
214         switch (status) {
215         case NT_STATUS_ACCESS_VIOLATION: return EACCES;
216         case NT_STATUS_NO_SUCH_FILE: return ENOENT;
217         case NT_STATUS_NO_SUCH_DEVICE: return ENODEV;
218         case NT_STATUS_INVALID_HANDLE: return EBADF;
219         case NT_STATUS_NO_MEMORY: return ENOMEM;
220         case NT_STATUS_ACCESS_DENIED: return EACCES;
221         case NT_STATUS_OBJECT_NAME_NOT_FOUND: return ENOENT;
222         case NT_STATUS_SHARING_VIOLATION: return EBUSY;
223         case NT_STATUS_OBJECT_PATH_INVALID: return ENOTDIR;
224         case NT_STATUS_OBJECT_NAME_COLLISION: return EEXIST;
225         case NT_STATUS_PATH_NOT_COVERED: return ENOENT;
226         }
227
228         /* for all other cases - a default code */
229         return EINVAL;
230 }
231
232 /* Return a UNIX errno appropriate for the error received in the last
233    packet. */
234
235 int cli_errno(struct cli_state *cli)
236 {
237         uint32 status;
238
239         if (cli_is_dos_error) {
240                 uint8 eclass;
241                 uint32 ecode;
242
243                 cli_dos_error(cli, &eclass, &ecode);
244                 return cli_errno_from_dos(eclass, ecode);
245         }
246
247         status = cli_nt_error(cli);
248
249         return cli_errno_from_nt(status);
250 }
251
252 /* Return true if the last packet was in error */
253
254 BOOL cli_is_error(struct cli_state *cli)
255 {
256         uint32 flgs2 = SVAL(cli->inbuf,smb_flg2), rcls = 0;
257
258         if (flgs2 & FLAGS2_32_BIT_ERROR_CODES)
259                 rcls = IVAL(cli->inbuf, smb_rcls);
260         else
261                 rcls = CVAL(cli->inbuf, smb_rcls);
262
263         return (rcls != 0);
264 }
265
266 /* Return true if the last error was an NT error */
267
268 BOOL cli_is_nt_error(struct cli_state *cli)
269 {
270         uint32 flgs2 = SVAL(cli->inbuf,smb_flg2);
271
272         return cli_is_error(cli) && (flgs2 & FLAGS2_32_BIT_ERROR_CODES);
273 }
274
275 /* Return true if the last error was a DOS error */
276
277 BOOL cli_is_dos_error(struct cli_state *cli)
278 {
279         uint32 flgs2 = SVAL(cli->inbuf,smb_flg2);
280
281         return cli_is_error(cli) && !(flgs2 & FLAGS2_32_BIT_ERROR_CODES);
282 }