use cli_is_error() instead of looking in smb_rcls, otherwise NT status
[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_dos_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         if (flgs2 & FLAGS2_32_BIT_ERROR_CODES) {
79                 NTSTATUS status = NT_STATUS(IVAL(cli->inbuf,smb_rcls));
80
81                 return get_nt_error_msg(status);
82         }
83
84         cli_dos_error(cli, &errclass, &errnum);
85
86         /* Case #2: SMB error */
87
88         if (errclass != 0)
89                 return cli_smb_errstr(cli);
90
91         /* Case #3: RAP error */
92         for (i = 0; rap_errmap[i].message != NULL; i++) {
93                 if (rap_errmap[i].err == cli->rap_error) {
94                         return rap_errmap[i].message;
95                 }
96         } 
97
98         slprintf(error_message, sizeof(error_message) - 1, "code %d", 
99                  cli->rap_error);
100
101         return error_message;
102 }
103
104
105 /* Return the 32-bit NT status code from the last packet */
106 NTSTATUS cli_nt_error(struct cli_state *cli)
107 {
108         int flgs2 = SVAL(cli->inbuf,smb_flg2);
109
110         if (!(flgs2 & FLAGS2_32_BIT_ERROR_CODES)) {
111                 int class  = CVAL(cli->inbuf,smb_rcls);
112                 int code  = SVAL(cli->inbuf,smb_err);
113                 return dos_to_ntstatus(class, code);
114         }
115
116         return NT_STATUS(IVAL(cli->inbuf,smb_rcls));
117 }
118
119
120 /* Return the DOS error from the last packet - an error class and an error
121    code. */
122 void cli_dos_error(struct cli_state *cli, uint8 *eclass, uint32 *ecode)
123 {
124         int  flgs2;
125         char rcls;
126         int code;
127
128         if(!cli->initialised) return;
129
130         flgs2 = SVAL(cli->inbuf,smb_flg2);
131
132         if (flgs2 & FLAGS2_32_BIT_ERROR_CODES) {
133                 NTSTATUS ntstatus = NT_STATUS(IVAL(cli->inbuf, smb_rcls));
134                 ntstatus_to_dos(ntstatus, eclass, ecode);
135                 return;
136         }
137
138         rcls  = CVAL(cli->inbuf,smb_rcls);
139         code  = SVAL(cli->inbuf,smb_err);
140
141         if (eclass) *eclass = rcls;
142         if (ecode) *ecode    = code;
143 }
144
145 /* Return a UNIX errno from a dos error class, error number tuple */
146
147 int cli_errno_from_dos(uint8 eclass, uint32 num)
148 {
149         if (eclass == ERRDOS) {
150                 switch (num) {
151                 case ERRbadfile: return ENOENT;
152                 case ERRbadpath: return ENOTDIR;
153                 case ERRnoaccess: return EACCES;
154                 case ERRfilexists: return EEXIST;
155                 case ERRrename: return EEXIST;
156                 case ERRbadshare: return EBUSY;
157                 case ERRlock: return EBUSY;
158                 case ERRinvalidname: return ENOENT;
159                 case ERRnosuchshare: return ENODEV;
160                 }
161         }
162
163         if (eclass == ERRSRV) {
164                 switch (num) {
165                 case ERRbadpw: return EPERM;
166                 case ERRaccess: return EACCES;
167                 case ERRnoresource: return ENOMEM;
168                 case ERRinvdevice: return ENODEV;
169                 case ERRinvnetname: return ENODEV;
170                 }
171         }
172
173         /* for other cases */
174         return EINVAL;
175 }
176
177 /* Return a UNIX errno from a NT status code */
178 static struct {
179         NTSTATUS status;
180         int error;
181 } nt_errno_map[] = {
182         {NT_STATUS_ACCESS_VIOLATION, EACCES},
183         {NT_STATUS_NO_SUCH_FILE, ENOENT},
184         {NT_STATUS_NO_SUCH_DEVICE, ENODEV},
185         {NT_STATUS_INVALID_HANDLE, EBADF},
186         {NT_STATUS_NO_MEMORY, ENOMEM},
187         {NT_STATUS_ACCESS_DENIED, EACCES},
188         {NT_STATUS_OBJECT_NAME_NOT_FOUND, ENOENT},
189         {NT_STATUS_SHARING_VIOLATION, EBUSY},
190         {NT_STATUS_OBJECT_PATH_INVALID, ENOTDIR},
191         {NT_STATUS_OBJECT_NAME_COLLISION, EEXIST},
192         {NT_STATUS_PATH_NOT_COVERED, ENOENT},
193         {NT_STATUS(0), 0}
194 };
195
196 int cli_errno_from_nt(NTSTATUS status)
197 {
198         int i;
199         DEBUG(10,("cli_errno_from_nt: 32 bit codes: code=%08x\n", NT_STATUS_V(status)));
200
201         /* Status codes without this bit set are not errors */
202
203         if (!(NT_STATUS_V(status) & 0xc0000000))
204                 return 0;
205
206         for (i=0;nt_errno_map[i].error;i++) {
207                 if (NT_STATUS_V(nt_errno_map[i].status) ==
208                     NT_STATUS_V(status)) return nt_errno_map[i].error;
209         }
210
211         /* for all other cases - a default code */
212         return EINVAL;
213 }
214
215 /* Return a UNIX errno appropriate for the error received in the last
216    packet. */
217
218 int cli_errno(struct cli_state *cli)
219 {
220         NTSTATUS status;
221
222         if (cli_is_dos_error(cli)) {
223                 uint8 eclass;
224                 uint32 ecode;
225
226                 cli_dos_error(cli, &eclass, &ecode);
227                 return cli_errno_from_dos(eclass, ecode);
228         }
229
230         status = cli_nt_error(cli);
231
232         return cli_errno_from_nt(status);
233 }
234
235 /* Return true if the last packet was in error */
236
237 BOOL cli_is_error(struct cli_state *cli)
238 {
239         uint32 flgs2 = SVAL(cli->inbuf,smb_flg2), rcls = 0;
240
241         if (flgs2 & FLAGS2_32_BIT_ERROR_CODES) {
242                 /* Return error is error bits are set */
243                 rcls = IVAL(cli->inbuf, smb_rcls);
244                 return (rcls & 0xF0000000) == 0xC0000000;
245         }
246                 
247         /* Return error if error class in non-zero */
248
249         rcls = CVAL(cli->inbuf, smb_rcls);
250         return rcls != 0;
251 }
252
253 /* Return true if the last error was an NT error */
254
255 BOOL cli_is_nt_error(struct cli_state *cli)
256 {
257         uint32 flgs2 = SVAL(cli->inbuf,smb_flg2);
258
259         return cli_is_error(cli) && (flgs2 & FLAGS2_32_BIT_ERROR_CODES);
260 }
261
262 /* Return true if the last error was a DOS error */
263
264 BOOL cli_is_dos_error(struct cli_state *cli)
265 {
266         uint32 flgs2 = SVAL(cli->inbuf,smb_flg2);
267
268         return cli_is_error(cli) && !(flgs2 & FLAGS2_32_BIT_ERROR_CODES);
269 }
270