Initial import
[samba] / source / libsmb / clierror.c
1 /* 
2    Unix SMB/CIFS implementation.
3    client error handling routines
4    Copyright (C) Andrew Tridgell 1994-1998
5    Copyright (C) Jelmer Vernooij 2003
6    Copyright (C) Jeremy Allison 2006
7    
8    This program is free software; you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 2 of the License, or
11    (at your option) any later version.
12    
13    This program is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17    
18    You should have received a copy of the GNU General Public License
19    along with this program; if not, write to the Free Software
20    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 */
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         int err;
35         const char *message;
36 } rap_errmap[] = {
37         {5,    "RAP5: User has insufficient privilege" },
38         {50,   "RAP50: Not supported by server" },
39         {65,   "RAP65: Access denied" },
40         {86,   "RAP86: The specified password is invalid" },
41         {2220, "RAP2220: Group does not exist" },
42         {2221, "RAP2221: User does not exist" },
43         {2226, "RAP2226: Operation only permitted on a Primary Domain Controller"  },
44         {2237, "RAP2237: User is not in group" },
45         {2242, "RAP2242: The password of this user has expired." },
46         {2243, "RAP2243: The password of this user cannot change." },
47         {2244, "RAP2244: This password cannot be used now (password history conflict)." },
48         {2245, "RAP2245: The password is shorter than required." },
49         {2246, "RAP2246: The password of this user is too recent to change."},
50
51         /* these really shouldn't be here ... */
52         {0x80, "Not listening on called name"},
53         {0x81, "Not listening for calling name"},
54         {0x82, "Called name not present"},
55         {0x83, "Called name present, but insufficient resources"},
56
57         {0, NULL}
58 };  
59
60 /****************************************************************************
61  Return a description of an SMB error.
62 ****************************************************************************/
63
64 static const char *cli_smb_errstr(struct cli_state *cli)
65 {
66         return smb_dos_errstr(cli->inbuf);
67 }
68
69 /****************************************************************************
70  Convert a socket error into an NTSTATUS.
71 ****************************************************************************/
72
73 static NTSTATUS cli_smb_rw_error_to_ntstatus(struct cli_state *cli)
74 {
75         switch(cli->smb_rw_error) {
76                 case READ_TIMEOUT:
77                         return NT_STATUS_IO_TIMEOUT;
78                 case READ_EOF:
79                         return NT_STATUS_END_OF_FILE;
80                 /* What we shoud really do for read/write errors is convert from errno. */
81                 /* FIXME. JRA. */
82                 case READ_ERROR:
83                         return NT_STATUS_INVALID_NETWORK_RESPONSE;
84                 case WRITE_ERROR:
85                         return NT_STATUS_UNEXPECTED_NETWORK_ERROR;
86                 case READ_BAD_SIG:
87                         return NT_STATUS_INVALID_PARAMETER;
88                 default:
89                         break;
90         }
91         return NT_STATUS_UNSUCCESSFUL;
92 }
93
94 /***************************************************************************
95  Return an error message - either an NT error, SMB error or a RAP error.
96  Note some of the NT errors are actually warnings or "informational" errors
97  in which case they can be safely ignored.
98 ****************************************************************************/
99
100 const char *cli_errstr(struct cli_state *cli)
101 {   
102         static fstring cli_error_message;
103         uint32 flgs2 = SVAL(cli->inbuf,smb_flg2), errnum;
104         uint8 errclass;
105         int i;
106
107         if (!cli->initialised) {
108                 fstrcpy(cli_error_message, "[Programmer's error] cli_errstr called on unitialized cli_stat struct!\n");
109                 return cli_error_message;
110         }
111
112         /* Was it server socket error ? */
113         if (cli->fd == -1 && cli->smb_rw_error) {
114                 switch(cli->smb_rw_error) {
115                         case READ_TIMEOUT:
116                                 slprintf(cli_error_message, sizeof(cli_error_message) - 1,
117                                         "Call timed out: server did not respond after %d milliseconds",
118                                         cli->timeout);
119                                 break;
120                         case READ_EOF:
121                                 slprintf(cli_error_message, sizeof(cli_error_message) - 1,
122                                         "Call returned zero bytes (EOF)" );
123                                 break;
124                         case READ_ERROR:
125                                 slprintf(cli_error_message, sizeof(cli_error_message) - 1,
126                                         "Read error: %s", strerror(errno) );
127                                 break;
128                         case WRITE_ERROR:
129                                 slprintf(cli_error_message, sizeof(cli_error_message) - 1,
130                                         "Write error: %s", strerror(errno) );
131                                 break;
132                         case READ_BAD_SIG:
133                                 slprintf(cli_error_message, sizeof(cli_error_message) - 1,
134                                         "Server packet had invalid SMB signature!");
135                                 break;
136                         default:
137                                 slprintf(cli_error_message, sizeof(cli_error_message) - 1,
138                                         "Unknown error code %d\n", cli->smb_rw_error );
139                                 break;
140                 }
141                 return cli_error_message;
142         }
143
144         /* Case #1: RAP error */
145         if (cli->rap_error) {
146                 for (i = 0; rap_errmap[i].message != NULL; i++) {
147                         if (rap_errmap[i].err == cli->rap_error) {
148                                 return rap_errmap[i].message;
149                         }
150                 }
151
152                 slprintf(cli_error_message, sizeof(cli_error_message) - 1, "RAP code %d",
153                         cli->rap_error);
154
155                 return cli_error_message;
156         }
157
158         /* Case #2: 32-bit NT errors */
159         if (flgs2 & FLAGS2_32_BIT_ERROR_CODES) {
160                 NTSTATUS status = NT_STATUS(IVAL(cli->inbuf,smb_rcls));
161
162                 return nt_errstr(status);
163         }
164
165         cli_dos_error(cli, &errclass, &errnum);
166
167         /* Case #3: SMB error */
168
169         return cli_smb_errstr(cli);
170 }
171
172
173 /****************************************************************************
174  Return the 32-bit NT status code from the last packet.
175 ****************************************************************************/
176
177 NTSTATUS cli_nt_error(struct cli_state *cli)
178 {
179         int flgs2 = SVAL(cli->inbuf,smb_flg2);
180
181         /* Deal with socket errors first. */
182         if (cli->fd == -1 && cli->smb_rw_error) {
183                 return cli_smb_rw_error_to_ntstatus(cli);
184         }
185
186         if (!(flgs2 & FLAGS2_32_BIT_ERROR_CODES)) {
187                 int e_class  = CVAL(cli->inbuf,smb_rcls);
188                 int code  = SVAL(cli->inbuf,smb_err);
189                 return dos_to_ntstatus(e_class, code);
190         }
191
192         return NT_STATUS(IVAL(cli->inbuf,smb_rcls));
193 }
194
195
196 /****************************************************************************
197  Return the DOS error from the last packet - an error class and an error
198  code.
199 ****************************************************************************/
200
201 void cli_dos_error(struct cli_state *cli, uint8 *eclass, uint32 *ecode)
202 {
203         int  flgs2;
204         char rcls;
205         int code;
206
207         if(!cli->initialised) {
208                 return;
209         }
210
211         /* Deal with socket errors first. */
212         if (cli->fd == -1 && cli->smb_rw_error) {
213                 NTSTATUS status = cli_smb_rw_error_to_ntstatus(cli);
214                 ntstatus_to_dos( status, eclass, ecode);
215                 return;
216         }
217
218         flgs2 = SVAL(cli->inbuf,smb_flg2);
219
220         if (flgs2 & FLAGS2_32_BIT_ERROR_CODES) {
221                 NTSTATUS ntstatus = NT_STATUS(IVAL(cli->inbuf, smb_rcls));
222                 ntstatus_to_dos(ntstatus, eclass, ecode);
223                 return;
224         }
225
226         rcls  = CVAL(cli->inbuf,smb_rcls);
227         code  = SVAL(cli->inbuf,smb_err);
228
229         if (eclass) *eclass = rcls;
230         if (ecode) *ecode    = code;
231 }
232
233 /****************************************************************************
234  The following mappings need tidying up and moving into libsmb/errormap.c...
235 ****************************************************************************/
236
237 /* Return a UNIX errno from a dos error class, error number tuple */
238
239 static int cli_errno_from_dos(uint8 eclass, uint32 num)
240 {
241         if (eclass == ERRDOS) {
242                 switch (num) {
243                 case ERRbadfile: return ENOENT;
244                 case ERRbadpath: return ENOTDIR;
245                 case ERRnoaccess: return EACCES;
246                 case ERRfilexists: return EEXIST;
247                 case ERRrename: return EEXIST;
248                 case ERRbadshare: return EBUSY;
249                 case ERRlock: return EBUSY;
250                 case ERRinvalidname: return ENOENT;
251                 case ERRnosuchshare: return ENODEV;
252                 }
253         }
254
255         if (eclass == ERRSRV) {
256                 switch (num) {
257                 case ERRbadpw: return EPERM;
258                 case ERRaccess: return EACCES;
259                 case ERRnoresource: return ENOMEM;
260                 case ERRinvdevice: return ENODEV;
261                 case ERRinvnetname: return ENODEV;
262                 }
263         }
264
265         /* for other cases */
266         return EINVAL;
267 }
268
269 /* Return a UNIX errno from a NT status code */
270 static struct {
271         NTSTATUS status;
272         int error;
273 } nt_errno_map[] = {
274         {NT_STATUS_ACCESS_VIOLATION, EACCES},
275         {NT_STATUS_INVALID_HANDLE, EBADF},
276         {NT_STATUS_ACCESS_DENIED, EACCES},
277         {NT_STATUS_OBJECT_NAME_NOT_FOUND, ENOENT},
278         {NT_STATUS_SHARING_VIOLATION, EBUSY},
279         {NT_STATUS_OBJECT_PATH_INVALID, ENOTDIR},
280         {NT_STATUS_OBJECT_NAME_COLLISION, EEXIST},
281         {NT_STATUS_PATH_NOT_COVERED, ENOENT},
282         {NT_STATUS_UNSUCCESSFUL, EINVAL},
283         {NT_STATUS_NOT_IMPLEMENTED, ENOSYS},
284         {NT_STATUS_IN_PAGE_ERROR, EFAULT}, 
285         {NT_STATUS_BAD_NETWORK_NAME, ENOENT},
286 #ifdef EDQUOT
287         {NT_STATUS_PAGEFILE_QUOTA, EDQUOT},
288         {NT_STATUS_QUOTA_EXCEEDED, EDQUOT},
289         {NT_STATUS_REGISTRY_QUOTA_LIMIT, EDQUOT},
290         {NT_STATUS_LICENSE_QUOTA_EXCEEDED, EDQUOT},
291 #endif
292 #ifdef ETIME
293         {NT_STATUS_TIMER_NOT_CANCELED, ETIME},
294 #endif
295         {NT_STATUS_INVALID_PARAMETER, EINVAL},
296         {NT_STATUS_NO_SUCH_DEVICE, ENODEV},
297         {NT_STATUS_NO_SUCH_FILE, ENOENT},
298 #ifdef ENODATA
299         {NT_STATUS_END_OF_FILE, ENODATA}, 
300 #endif
301 #ifdef ENOMEDIUM
302         {NT_STATUS_NO_MEDIA_IN_DEVICE, ENOMEDIUM}, 
303         {NT_STATUS_NO_MEDIA, ENOMEDIUM},
304 #endif
305         {NT_STATUS_NONEXISTENT_SECTOR, ESPIPE}, 
306         {NT_STATUS_NO_MEMORY, ENOMEM},
307         {NT_STATUS_CONFLICTING_ADDRESSES, EADDRINUSE},
308         {NT_STATUS_NOT_MAPPED_VIEW, EINVAL},
309         {NT_STATUS_UNABLE_TO_FREE_VM, EADDRINUSE},
310         {NT_STATUS_ACCESS_DENIED, EACCES}, 
311         {NT_STATUS_BUFFER_TOO_SMALL, ENOBUFS},
312         {NT_STATUS_WRONG_PASSWORD, EACCES},
313         {NT_STATUS_LOGON_FAILURE, EACCES},
314         {NT_STATUS_INVALID_WORKSTATION, EACCES},
315         {NT_STATUS_INVALID_LOGON_HOURS, EACCES},
316         {NT_STATUS_PASSWORD_EXPIRED, EACCES},
317         {NT_STATUS_ACCOUNT_DISABLED, EACCES},
318         {NT_STATUS_DISK_FULL, ENOSPC},
319         {NT_STATUS_INVALID_PIPE_STATE, EPIPE},
320         {NT_STATUS_PIPE_BUSY, EPIPE},
321         {NT_STATUS_PIPE_DISCONNECTED, EPIPE},
322         {NT_STATUS_PIPE_NOT_AVAILABLE, ENOSYS},
323         {NT_STATUS_FILE_IS_A_DIRECTORY, EISDIR},
324         {NT_STATUS_NOT_SUPPORTED, ENOSYS},
325         {NT_STATUS_NOT_A_DIRECTORY, ENOTDIR},
326         {NT_STATUS_DIRECTORY_NOT_EMPTY, ENOTEMPTY},
327         {NT_STATUS_NETWORK_UNREACHABLE, ENETUNREACH},
328         {NT_STATUS_HOST_UNREACHABLE, EHOSTUNREACH},
329         {NT_STATUS_CONNECTION_ABORTED, ECONNABORTED},
330         {NT_STATUS_CONNECTION_REFUSED, ECONNREFUSED},
331         {NT_STATUS_TOO_MANY_LINKS, EMLINK},
332         {NT_STATUS_NETWORK_BUSY, EBUSY},
333         {NT_STATUS_DEVICE_DOES_NOT_EXIST, ENODEV},
334 #ifdef ELIBACC
335         {NT_STATUS_DLL_NOT_FOUND, ELIBACC},
336 #endif
337         {NT_STATUS_PIPE_BROKEN, EPIPE},
338         {NT_STATUS_REMOTE_NOT_LISTENING, ECONNREFUSED},
339         {NT_STATUS_NETWORK_ACCESS_DENIED, EACCES},
340         {NT_STATUS_TOO_MANY_OPENED_FILES, EMFILE},
341 #ifdef EPROTO
342         {NT_STATUS_DEVICE_PROTOCOL_ERROR, EPROTO},
343 #endif
344         {NT_STATUS_FLOAT_OVERFLOW, ERANGE},
345         {NT_STATUS_FLOAT_UNDERFLOW, ERANGE},
346         {NT_STATUS_INTEGER_OVERFLOW, ERANGE},
347         {NT_STATUS_MEDIA_WRITE_PROTECTED, EROFS},
348         {NT_STATUS_PIPE_CONNECTED, EISCONN},
349         {NT_STATUS_MEMORY_NOT_ALLOCATED, EFAULT},
350         {NT_STATUS_FLOAT_INEXACT_RESULT, ERANGE},
351         {NT_STATUS_ILL_FORMED_PASSWORD, EACCES},
352         {NT_STATUS_PASSWORD_RESTRICTION, EACCES},
353         {NT_STATUS_ACCOUNT_RESTRICTION, EACCES},
354         {NT_STATUS_PORT_CONNECTION_REFUSED, ECONNREFUSED},
355         {NT_STATUS_NAME_TOO_LONG, ENAMETOOLONG},
356         {NT_STATUS_REMOTE_DISCONNECT, ESHUTDOWN},
357         {NT_STATUS_CONNECTION_DISCONNECTED, ECONNABORTED},
358         {NT_STATUS_CONNECTION_RESET, ENETRESET},
359 #ifdef ENOTUNIQ
360         {NT_STATUS_IP_ADDRESS_CONFLICT1, ENOTUNIQ},
361         {NT_STATUS_IP_ADDRESS_CONFLICT2, ENOTUNIQ},
362 #endif
363         {NT_STATUS_PORT_MESSAGE_TOO_LONG, EMSGSIZE},
364         {NT_STATUS_PROTOCOL_UNREACHABLE, ENOPROTOOPT},
365         {NT_STATUS_ADDRESS_ALREADY_EXISTS, EADDRINUSE},
366         {NT_STATUS_PORT_UNREACHABLE, EHOSTUNREACH},
367         {NT_STATUS_IO_TIMEOUT, ETIMEDOUT},
368         {NT_STATUS_RETRY, EAGAIN},
369 #ifdef ENOTUNIQ
370         {NT_STATUS_DUPLICATE_NAME, ENOTUNIQ},
371 #endif
372 #ifdef ECOMM
373         {NT_STATUS_NET_WRITE_FAULT, ECOMM},
374 #endif
375
376         {NT_STATUS(0), 0}
377 };
378
379 /****************************************************************************
380  The following mappings need tidying up and moving into libsmb/errormap.c...
381 ****************************************************************************/
382
383 static int cli_errno_from_nt(NTSTATUS status)
384 {
385         int i;
386         DEBUG(10,("cli_errno_from_nt: 32 bit codes: code=%08x\n", NT_STATUS_V(status)));
387
388         /* Status codes without this bit set are not errors */
389
390         if (!(NT_STATUS_V(status) & 0xc0000000)) {
391                 return 0;
392         }
393
394         for (i=0;nt_errno_map[i].error;i++) {
395                 if (NT_STATUS_V(nt_errno_map[i].status) ==
396                     NT_STATUS_V(status)) return nt_errno_map[i].error;
397         }
398
399         /* for all other cases - a default code */
400         return EINVAL;
401 }
402
403 /* Return a UNIX errno appropriate for the error received in the last
404    packet. */
405
406 int cli_errno(struct cli_state *cli)
407 {
408         if (cli_is_nt_error(cli)) {
409                 NTSTATUS status = cli_nt_error(cli);
410                 return cli_errno_from_nt(status);
411         }
412
413         if (cli_is_dos_error(cli)) {
414                 uint8 eclass;
415                 uint32 ecode;
416
417                 cli_dos_error(cli, &eclass, &ecode);
418                 return cli_errno_from_dos(eclass, ecode);
419         }
420
421         /* for other cases */
422         return EINVAL;
423 }
424
425 /* Return true if the last packet was in error */
426
427 BOOL cli_is_error(struct cli_state *cli)
428 {
429         uint32 flgs2 = SVAL(cli->inbuf,smb_flg2), rcls = 0;
430
431         /* A socket error is always an error. */
432         if (cli->fd == -1 && cli->smb_rw_error != 0) {
433                 return True;
434         }
435
436         if (flgs2 & FLAGS2_32_BIT_ERROR_CODES) {
437                 /* Return error is error bits are set */
438                 rcls = IVAL(cli->inbuf, smb_rcls);
439                 return (rcls & 0xF0000000) == 0xC0000000;
440         }
441                 
442         /* Return error if error class in non-zero */
443
444         rcls = CVAL(cli->inbuf, smb_rcls);
445         return rcls != 0;
446 }
447
448 /* Return true if the last error was an NT error */
449
450 BOOL cli_is_nt_error(struct cli_state *cli)
451 {
452         uint32 flgs2 = SVAL(cli->inbuf,smb_flg2);
453
454         /* A socket error is always an NT error. */
455         if (cli->fd == -1 && cli->smb_rw_error != 0) {
456                 return True;
457         }
458
459         return cli_is_error(cli) && (flgs2 & FLAGS2_32_BIT_ERROR_CODES);
460 }
461
462 /* Return true if the last error was a DOS error */
463
464 BOOL cli_is_dos_error(struct cli_state *cli)
465 {
466         uint32 flgs2 = SVAL(cli->inbuf,smb_flg2);
467
468         /* A socket error is always a DOS error. */
469         if (cli->fd == -1 && cli->smb_rw_error != 0) {
470                 return True;
471         }
472
473         return cli_is_error(cli) && !(flgs2 & FLAGS2_32_BIT_ERROR_CODES);
474 }
475
476 /* Return the last error always as an NTSTATUS. */
477
478 NTSTATUS cli_get_nt_error(struct cli_state *cli)
479 {
480         if (cli_is_nt_error(cli)) {
481                 return cli_nt_error(cli);
482         } else if (cli_is_dos_error(cli)) {
483                 uint32 ecode;
484                 uint8 eclass;
485                 cli_dos_error(cli, &eclass, &ecode);
486                 return dos_to_ntstatus(eclass, ecode);
487         } else {
488                 /* Something went wrong, we don't know what. */
489                 return NT_STATUS_UNSUCCESSFUL;
490         }
491 }