Add more string explanations of RAP errors that are already documented
[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 /*****************************************************
27  RAP error codes - a small start but will be extended.
28
29  XXX: Perhaps these should move into a common function because they're
30  duplicated in clirap2.c
31
32 *******************************************************/
33
34 static const struct
35 {
36   int err;
37   char *message;
38 } rap_errmap[] =
39 {
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."},
53
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"},
59
60   {0, NULL}
61 };  
62
63 /****************************************************************************
64   return a description of an SMB error
65 ****************************************************************************/
66 static char *cli_smb_errstr(struct cli_state *cli)
67 {
68         return smb_dos_errstr(cli->inbuf);
69 }
70
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 ****************************************************************************/
76     
77 char *cli_errstr(struct cli_state *cli)
78 {   
79         static fstring cli_error_message;
80         uint32 flgs2 = SVAL(cli->inbuf,smb_flg2), errnum;
81         uint8 errclass;
82         int i;
83
84         /* Case #1: RAP error */
85         if (cli->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;
89                         }
90                 }
91
92                 slprintf(cli_error_message, sizeof(cli_error_message) - 1, "RAP code %d",
93                         cli->rap_error);
94
95                 return cli_error_message;
96         }
97
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));
101
102                 return get_nt_error_msg(status);
103         }
104
105         cli_dos_error(cli, &errclass, &errnum);
106
107         /* Case #3: SMB error */
108
109         return cli_smb_errstr(cli);
110 }
111
112
113 /* Return the 32-bit NT status code from the last packet */
114 NTSTATUS cli_nt_error(struct cli_state *cli)
115 {
116         int flgs2 = SVAL(cli->inbuf,smb_flg2);
117
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);
122         }
123
124         return NT_STATUS(IVAL(cli->inbuf,smb_rcls));
125 }
126
127
128 /* Return the DOS error from the last packet - an error class and an error
129    code. */
130 void cli_dos_error(struct cli_state *cli, uint8 *eclass, uint32 *ecode)
131 {
132         int  flgs2;
133         char rcls;
134         int code;
135
136         if(!cli->initialised) return;
137
138         flgs2 = SVAL(cli->inbuf,smb_flg2);
139
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);
143                 return;
144         }
145
146         rcls  = CVAL(cli->inbuf,smb_rcls);
147         code  = SVAL(cli->inbuf,smb_err);
148
149         if (eclass) *eclass = rcls;
150         if (ecode) *ecode    = code;
151 }
152
153 /* Return a UNIX errno from a dos error class, error number tuple */
154
155 int cli_errno_from_dos(uint8 eclass, uint32 num)
156 {
157         if (eclass == ERRDOS) {
158                 switch (num) {
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;
168                 }
169         }
170
171         if (eclass == ERRSRV) {
172                 switch (num) {
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;
178                 }
179         }
180
181         /* for other cases */
182         return EINVAL;
183 }
184
185 /* Return a UNIX errno from a NT status code */
186 static struct {
187         NTSTATUS status;
188         int error;
189 } nt_errno_map[] = {
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},
201         {NT_STATUS(0), 0}
202 };
203
204 int cli_errno_from_nt(NTSTATUS status)
205 {
206         int i;
207         DEBUG(10,("cli_errno_from_nt: 32 bit codes: code=%08x\n", NT_STATUS_V(status)));
208
209         /* Status codes without this bit set are not errors */
210
211         if (!(NT_STATUS_V(status) & 0xc0000000))
212                 return 0;
213
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;
217         }
218
219         /* for all other cases - a default code */
220         return EINVAL;
221 }
222
223 /* Return a UNIX errno appropriate for the error received in the last
224    packet. */
225
226 int cli_errno(struct cli_state *cli)
227 {
228         NTSTATUS status;
229
230         if (cli_is_dos_error(cli)) {
231                 uint8 eclass;
232                 uint32 ecode;
233
234                 cli_dos_error(cli, &eclass, &ecode);
235                 return cli_errno_from_dos(eclass, ecode);
236         }
237
238         status = cli_nt_error(cli);
239
240         return cli_errno_from_nt(status);
241 }
242
243 /* Return true if the last packet was in error */
244
245 BOOL cli_is_error(struct cli_state *cli)
246 {
247         uint32 flgs2 = SVAL(cli->inbuf,smb_flg2), rcls = 0;
248
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;
253         }
254                 
255         /* Return error if error class in non-zero */
256
257         rcls = CVAL(cli->inbuf, smb_rcls);
258         return rcls != 0;
259 }
260
261 /* Return true if the last error was an NT error */
262
263 BOOL cli_is_nt_error(struct cli_state *cli)
264 {
265         uint32 flgs2 = SVAL(cli->inbuf,smb_flg2);
266
267         return cli_is_error(cli) && (flgs2 & FLAGS2_32_BIT_ERROR_CODES);
268 }
269
270 /* Return true if the last error was a DOS error */
271
272 BOOL cli_is_dos_error(struct cli_state *cli)
273 {
274         uint32 flgs2 = SVAL(cli->inbuf,smb_flg2);
275
276         return cli_is_error(cli) && !(flgs2 & FLAGS2_32_BIT_ERROR_CODES);
277 }