This commit was manufactured by cvs2svn to create branch 'SAMBA_3_0'.
[samba.git] / source / 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   const 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 const 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 const 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         /* Was it server socket error ? */
89         if (cli->fd == -1 && cli->smb_rw_error) {
90                 switch(cli->smb_rw_error) {
91                         case READ_TIMEOUT:
92                                 slprintf(cli_error_message, sizeof(cli_error_message) - 1,
93                                         "Call timed out: server did not respond after %d milliseconds",
94                                         cli->timeout);
95                                 break;
96                         case READ_EOF:
97                                 slprintf(cli_error_message, sizeof(cli_error_message) - 1,
98                                         "Call returned zero bytes (EOF)\n" );
99                                 break;
100                         case READ_ERROR:
101                                 slprintf(cli_error_message, sizeof(cli_error_message) - 1,
102                                         "Read error: %s\n", strerror(errno) );
103                                 break;
104                         case WRITE_ERROR:
105                                 slprintf(cli_error_message, sizeof(cli_error_message) - 1,
106                                         "Write error: %s\n", strerror(errno) );
107                                 break;
108                         default:
109                                 slprintf(cli_error_message, sizeof(cli_error_message) - 1,
110                                         "Unknown error code %d\n", cli->smb_rw_error );
111                                 break;
112                 }
113                 return cli_error_message;
114         }
115
116         /* Case #1: RAP error */
117         if (cli->rap_error) {
118                 for (i = 0; rap_errmap[i].message != NULL; i++) {
119                         if (rap_errmap[i].err == cli->rap_error) {
120                                 return rap_errmap[i].message;
121                         }
122                 }
123
124                 slprintf(cli_error_message, sizeof(cli_error_message) - 1, "RAP code %d",
125                         cli->rap_error);
126
127                 return cli_error_message;
128         }
129
130         /* Case #2: 32-bit NT errors */
131         if (flgs2 & FLAGS2_32_BIT_ERROR_CODES) {
132                 NTSTATUS status = NT_STATUS(IVAL(cli->inbuf,smb_rcls));
133
134                 return nt_errstr(status);
135         }
136
137         cli_dos_error(cli, &errclass, &errnum);
138
139         /* Case #3: SMB error */
140
141         return cli_smb_errstr(cli);
142 }
143
144
145 /* Return the 32-bit NT status code from the last packet */
146 NTSTATUS cli_nt_error(struct cli_state *cli)
147 {
148         int flgs2 = SVAL(cli->inbuf,smb_flg2);
149
150         if (!(flgs2 & FLAGS2_32_BIT_ERROR_CODES)) {
151                 int class  = CVAL(cli->inbuf,smb_rcls);
152                 int code  = SVAL(cli->inbuf,smb_err);
153                 return dos_to_ntstatus(class, code);
154         }
155
156         return NT_STATUS(IVAL(cli->inbuf,smb_rcls));
157 }
158
159
160 /* Return the DOS error from the last packet - an error class and an error
161    code. */
162 void cli_dos_error(struct cli_state *cli, uint8 *eclass, uint32 *ecode)
163 {
164         int  flgs2;
165         char rcls;
166         int code;
167
168         if(!cli->initialised) return;
169
170         flgs2 = SVAL(cli->inbuf,smb_flg2);
171
172         if (flgs2 & FLAGS2_32_BIT_ERROR_CODES) {
173                 NTSTATUS ntstatus = NT_STATUS(IVAL(cli->inbuf, smb_rcls));
174                 ntstatus_to_dos(ntstatus, eclass, ecode);
175                 return;
176         }
177
178         rcls  = CVAL(cli->inbuf,smb_rcls);
179         code  = SVAL(cli->inbuf,smb_err);
180
181         if (eclass) *eclass = rcls;
182         if (ecode) *ecode    = code;
183 }
184
185 /* Return a UNIX errno from a dos error class, error number tuple */
186
187 static int cli_errno_from_dos(uint8 eclass, uint32 num)
188 {
189         if (eclass == ERRDOS) {
190                 switch (num) {
191                 case ERRbadfile: return ENOENT;
192                 case ERRbadpath: return ENOTDIR;
193                 case ERRnoaccess: return EACCES;
194                 case ERRfilexists: return EEXIST;
195                 case ERRrename: return EEXIST;
196                 case ERRbadshare: return EBUSY;
197                 case ERRlock: return EBUSY;
198                 case ERRinvalidname: return ENOENT;
199                 case ERRnosuchshare: return ENODEV;
200                 }
201         }
202
203         if (eclass == ERRSRV) {
204                 switch (num) {
205                 case ERRbadpw: return EPERM;
206                 case ERRaccess: return EACCES;
207                 case ERRnoresource: return ENOMEM;
208                 case ERRinvdevice: return ENODEV;
209                 case ERRinvnetname: return ENODEV;
210                 }
211         }
212
213         /* for other cases */
214         return EINVAL;
215 }
216
217 /* Return a UNIX errno from a NT status code */
218 static struct {
219         NTSTATUS status;
220         int error;
221 } nt_errno_map[] = {
222         {NT_STATUS_ACCESS_VIOLATION, EACCES},
223         {NT_STATUS_NO_SUCH_FILE, ENOENT},
224         {NT_STATUS_NO_SUCH_DEVICE, ENODEV},
225         {NT_STATUS_INVALID_HANDLE, EBADF},
226         {NT_STATUS_NO_MEMORY, ENOMEM},
227         {NT_STATUS_ACCESS_DENIED, EACCES},
228         {NT_STATUS_OBJECT_NAME_NOT_FOUND, ENOENT},
229         {NT_STATUS_SHARING_VIOLATION, EBUSY},
230         {NT_STATUS_OBJECT_PATH_INVALID, ENOTDIR},
231         {NT_STATUS_OBJECT_NAME_COLLISION, EEXIST},
232         {NT_STATUS_PATH_NOT_COVERED, ENOENT},
233         {NT_STATUS(0), 0}
234 };
235
236 static int cli_errno_from_nt(NTSTATUS status)
237 {
238         int i;
239         DEBUG(10,("cli_errno_from_nt: 32 bit codes: code=%08x\n", NT_STATUS_V(status)));
240
241         /* Status codes without this bit set are not errors */
242
243         if (!(NT_STATUS_V(status) & 0xc0000000))
244                 return 0;
245
246         for (i=0;nt_errno_map[i].error;i++) {
247                 if (NT_STATUS_V(nt_errno_map[i].status) ==
248                     NT_STATUS_V(status)) return nt_errno_map[i].error;
249         }
250
251         /* for all other cases - a default code */
252         return EINVAL;
253 }
254
255 /* Return a UNIX errno appropriate for the error received in the last
256    packet. */
257
258 int cli_errno(struct cli_state *cli)
259 {
260         NTSTATUS status;
261
262         if (cli_is_dos_error(cli)) {
263                 uint8 eclass;
264                 uint32 ecode;
265
266                 cli_dos_error(cli, &eclass, &ecode);
267                 return cli_errno_from_dos(eclass, ecode);
268         }
269
270         status = cli_nt_error(cli);
271
272         return cli_errno_from_nt(status);
273 }
274
275 /* Return true if the last packet was in error */
276
277 BOOL cli_is_error(struct cli_state *cli)
278 {
279         uint32 flgs2 = SVAL(cli->inbuf,smb_flg2), rcls = 0;
280
281         if (flgs2 & FLAGS2_32_BIT_ERROR_CODES) {
282                 /* Return error is error bits are set */
283                 rcls = IVAL(cli->inbuf, smb_rcls);
284                 return (rcls & 0xF0000000) == 0xC0000000;
285         }
286                 
287         /* Return error if error class in non-zero */
288
289         rcls = CVAL(cli->inbuf, smb_rcls);
290         return rcls != 0;
291 }
292
293 /* Return true if the last error was an NT error */
294
295 BOOL cli_is_nt_error(struct cli_state *cli)
296 {
297         uint32 flgs2 = SVAL(cli->inbuf,smb_flg2);
298
299         return cli_is_error(cli) && (flgs2 & FLAGS2_32_BIT_ERROR_CODES);
300 }
301
302 /* Return true if the last error was a DOS error */
303
304 BOOL cli_is_dos_error(struct cli_state *cli)
305 {
306         uint32 flgs2 = SVAL(cli->inbuf,smb_flg2);
307
308         return cli_is_error(cli) && !(flgs2 & FLAGS2_32_BIT_ERROR_CODES);
309 }