sync'ing up for 3.0alpha20 release
[jra/samba/.git] / source3 / libsmb / clierror.c
1 /* 
2    Unix SMB/CIFS implementation.
3    client error handling routines
4    Copyright (C) Andrew Tridgell 1994-1998
5    
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.
10    
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.
15    
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.
19 */
20
21 #define NO_SYSLOG
22
23 #include "includes.h"
24
25 /*****************************************************
26  RAP error codes - a small start but will be extended.
27
28  XXX: Perhaps these should move into a common function because they're
29  duplicated in clirap2.c
30
31 *******************************************************/
32
33 static const struct
34 {
35   int err;
36   char *message;
37 } rap_errmap[] =
38 {
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."},
52
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"},
58
59   {0, NULL}
60 };  
61
62 /****************************************************************************
63   return a description of an SMB error
64 ****************************************************************************/
65 static char *cli_smb_errstr(struct cli_state *cli)
66 {
67         return smb_dos_errstr(cli->inbuf);
68 }
69
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 ****************************************************************************/
75     
76 char *cli_errstr(struct cli_state *cli)
77 {   
78         static fstring cli_error_message;
79         uint32 flgs2 = SVAL(cli->inbuf,smb_flg2), errnum;
80         uint8 errclass;
81         int i;
82
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;
86         }
87
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;
93                         }
94                 }
95
96                 slprintf(cli_error_message, sizeof(cli_error_message) - 1, "RAP code %d",
97                         cli->rap_error);
98
99                 return cli_error_message;
100         }
101
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));
105
106                 return nt_errstr(status);
107         }
108
109         cli_dos_error(cli, &errclass, &errnum);
110
111         /* Case #3: SMB error */
112
113         return cli_smb_errstr(cli);
114 }
115
116
117 /* Return the 32-bit NT status code from the last packet */
118 NTSTATUS cli_nt_error(struct cli_state *cli)
119 {
120         int flgs2 = SVAL(cli->inbuf,smb_flg2);
121
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);
126         }
127
128         return NT_STATUS(IVAL(cli->inbuf,smb_rcls));
129 }
130
131
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)
135 {
136         int  flgs2;
137         char rcls;
138         int code;
139
140         if(!cli->initialised) return;
141
142         flgs2 = SVAL(cli->inbuf,smb_flg2);
143
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;
148         }
149
150         rcls  = CVAL(cli->inbuf,smb_rcls);
151         code  = SVAL(cli->inbuf,smb_err);
152
153         if (eclass) *eclass = rcls;
154         if (ecode) *ecode    = code;
155 }
156
157 /* Return a UNIX errno from a dos error class, error number tuple */
158
159 static int cli_errno_from_dos(uint8 eclass, uint32 num)
160 {
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;
172                 }
173         }
174
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;
182                 }
183         }
184
185         /* for other cases */
186         return EINVAL;
187 }
188
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}
206 };
207
208 static int cli_errno_from_nt(NTSTATUS status)
209 {
210         int i;
211         DEBUG(10,("cli_errno_from_nt: 32 bit codes: code=%08x\n", NT_STATUS_V(status)));
212
213         /* Status codes without this bit set are not errors */
214
215         if (!(NT_STATUS_V(status) & 0xc0000000))
216                 return 0;
217
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;
221         }
222
223         /* for all other cases - a default code */
224         return EINVAL;
225 }
226
227 /* Return a UNIX errno appropriate for the error received in the last
228    packet. */
229
230 int cli_errno(struct cli_state *cli)
231 {
232         NTSTATUS status;
233
234         if (cli_is_dos_error(cli)) {
235                 uint8 eclass;
236                 uint32 ecode;
237
238                 cli_dos_error(cli, &eclass, &ecode);
239                 return cli_errno_from_dos(eclass, ecode);
240         }
241
242         status = cli_nt_error(cli);
243
244         return cli_errno_from_nt(status);
245 }
246
247 /* Return true if the last packet was in error */
248
249 BOOL cli_is_error(struct cli_state *cli)
250 {
251         uint32 flgs2 = SVAL(cli->inbuf,smb_flg2), rcls = 0;
252
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;
257         }
258                 
259         /* Return error if error class in non-zero */
260
261         rcls = CVAL(cli->inbuf, smb_rcls);
262         return rcls != 0;
263 }
264
265 /* Return true if the last error was an NT error */
266
267 BOOL cli_is_nt_error(struct cli_state *cli)
268 {
269         uint32 flgs2 = SVAL(cli->inbuf,smb_flg2);
270
271         return cli_is_error(cli) && (flgs2 & FLAGS2_32_BIT_ERROR_CODES);
272 }
273
274 /* Return true if the last error was a DOS error */
275
276 BOOL cli_is_dos_error(struct cli_state *cli)
277 {
278         uint32 flgs2 = SVAL(cli->inbuf,smb_flg2);
279
280         return cli_is_error(cli) && !(flgs2 & FLAGS2_32_BIT_ERROR_CODES);
281 }