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