Don't use EDQUOT on systems where it's not available
[kai/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    
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   const 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 const 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 const 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         if (!cli->initialised) {
85                 fstrcpy(cli_error_message, "[Programmer's error] cli_errstr called on unitialized cli_stat struct!\n");
86                 return cli_error_message;
87         }
88
89         /* Was it server socket error ? */
90         if (cli->fd == -1 && cli->smb_rw_error) {
91                 switch(cli->smb_rw_error) {
92                         case READ_TIMEOUT:
93                                 slprintf(cli_error_message, sizeof(cli_error_message) - 1,
94                                         "Call timed out: server did not respond after %d milliseconds",
95                                         cli->timeout);
96                                 break;
97                         case READ_EOF:
98                                 slprintf(cli_error_message, sizeof(cli_error_message) - 1,
99                                         "Call returned zero bytes (EOF)\n" );
100                                 break;
101                         case READ_ERROR:
102                                 slprintf(cli_error_message, sizeof(cli_error_message) - 1,
103                                         "Read error: %s\n", strerror(errno) );
104                                 break;
105                         case WRITE_ERROR:
106                                 slprintf(cli_error_message, sizeof(cli_error_message) - 1,
107                                         "Write error: %s\n", strerror(errno) );
108                                 break;
109                         default:
110                                 slprintf(cli_error_message, sizeof(cli_error_message) - 1,
111                                         "Unknown error code %d\n", cli->smb_rw_error );
112                                 break;
113                 }
114                 return cli_error_message;
115         }
116
117         /* Case #1: RAP error */
118         if (cli->rap_error) {
119                 for (i = 0; rap_errmap[i].message != NULL; i++) {
120                         if (rap_errmap[i].err == cli->rap_error) {
121                                 return rap_errmap[i].message;
122                         }
123                 }
124
125                 slprintf(cli_error_message, sizeof(cli_error_message) - 1, "RAP code %d",
126                         cli->rap_error);
127
128                 return cli_error_message;
129         }
130
131         /* Case #2: 32-bit NT errors */
132         if (flgs2 & FLAGS2_32_BIT_ERROR_CODES) {
133                 NTSTATUS status = NT_STATUS(IVAL(cli->inbuf,smb_rcls));
134
135                 return nt_errstr(status);
136         }
137
138         cli_dos_error(cli, &errclass, &errnum);
139
140         /* Case #3: SMB error */
141
142         return cli_smb_errstr(cli);
143 }
144
145
146 /* Return the 32-bit NT status code from the last packet */
147 NTSTATUS cli_nt_error(struct cli_state *cli)
148 {
149         int flgs2 = SVAL(cli->inbuf,smb_flg2);
150
151         if (!(flgs2 & FLAGS2_32_BIT_ERROR_CODES)) {
152                 int class  = CVAL(cli->inbuf,smb_rcls);
153                 int code  = SVAL(cli->inbuf,smb_err);
154                 return dos_to_ntstatus(class, code);
155         }
156
157         return NT_STATUS(IVAL(cli->inbuf,smb_rcls));
158 }
159
160
161 /* Return the DOS error from the last packet - an error class and an error
162    code. */
163 void cli_dos_error(struct cli_state *cli, uint8 *eclass, uint32 *ecode)
164 {
165         int  flgs2;
166         char rcls;
167         int code;
168
169         if(!cli->initialised) return;
170
171         flgs2 = SVAL(cli->inbuf,smb_flg2);
172
173         if (flgs2 & FLAGS2_32_BIT_ERROR_CODES) {
174                 NTSTATUS ntstatus = NT_STATUS(IVAL(cli->inbuf, smb_rcls));
175                 ntstatus_to_dos(ntstatus, eclass, ecode);
176                 return;
177         }
178
179         rcls  = CVAL(cli->inbuf,smb_rcls);
180         code  = SVAL(cli->inbuf,smb_err);
181
182         if (eclass) *eclass = rcls;
183         if (ecode) *ecode    = code;
184 }
185
186 /* Return a UNIX errno from a dos error class, error number tuple */
187
188 static int cli_errno_from_dos(uint8 eclass, uint32 num)
189 {
190         if (eclass == ERRDOS) {
191                 switch (num) {
192                 case ERRbadfile: return ENOENT;
193                 case ERRbadpath: return ENOTDIR;
194                 case ERRnoaccess: return EACCES;
195                 case ERRfilexists: return EEXIST;
196                 case ERRrename: return EEXIST;
197                 case ERRbadshare: return EBUSY;
198                 case ERRlock: return EBUSY;
199                 case ERRinvalidname: return ENOENT;
200                 case ERRnosuchshare: return ENODEV;
201                 }
202         }
203
204         if (eclass == ERRSRV) {
205                 switch (num) {
206                 case ERRbadpw: return EPERM;
207                 case ERRaccess: return EACCES;
208                 case ERRnoresource: return ENOMEM;
209                 case ERRinvdevice: return ENODEV;
210                 case ERRinvnetname: return ENODEV;
211                 }
212         }
213
214         /* for other cases */
215         return EINVAL;
216 }
217
218 /* Return a UNIX errno from a NT status code */
219 static struct {
220         NTSTATUS status;
221         int error;
222 } nt_errno_map[] = {
223         {NT_STATUS_ACCESS_VIOLATION, EACCES},
224         {NT_STATUS_INVALID_HANDLE, EBADF},
225         {NT_STATUS_ACCESS_DENIED, EACCES},
226         {NT_STATUS_OBJECT_NAME_NOT_FOUND, ENOENT},
227         {NT_STATUS_SHARING_VIOLATION, EBUSY},
228         {NT_STATUS_OBJECT_PATH_INVALID, ENOTDIR},
229         {NT_STATUS_OBJECT_NAME_COLLISION, EEXIST},
230         {NT_STATUS_PATH_NOT_COVERED, ENOENT},
231         {NT_STATUS_UNSUCCESSFUL, EINVAL},
232         {NT_STATUS_NOT_IMPLEMENTED, ENOSYS},
233         {NT_STATUS_IN_PAGE_ERROR, EFAULT}, 
234 #ifdef EDQUOT
235         {NT_STATUS_PAGEFILE_QUOTA, EDQUOT},
236         {NT_STATUS_QUOTA_EXCEEDED, EDQUOT},
237         {NT_STATUS_REGISTRY_QUOTA_LIMIT, EDQUOT},
238         {NT_STATUS_LICENSE_QUOTA_EXCEEDED, EDQUOT},
239 #endif
240 #ifdef ETIME
241         {NT_STATUS_TIMER_NOT_CANCELED, ETIME},
242 #endif
243         {NT_STATUS_INVALID_PARAMETER, EINVAL},
244         {NT_STATUS_NO_SUCH_DEVICE, ENODEV},
245         {NT_STATUS_NO_SUCH_FILE, ENOENT},
246 #ifdef ENODATA
247         {NT_STATUS_END_OF_FILE, ENODATA}, 
248 #endif
249 #ifdef ENOMEDIUM
250         {NT_STATUS_NO_MEDIA_IN_DEVICE, ENOMEDIUM}, 
251         {NT_STATUS_NO_MEDIA, ENOMEDIUM},
252 #endif
253         {NT_STATUS_NONEXISTENT_SECTOR, ESPIPE}, 
254         {NT_STATUS_NO_MEMORY, ENOMEM},
255         {NT_STATUS_CONFLICTING_ADDRESSES, EADDRINUSE},
256         {NT_STATUS_NOT_MAPPED_VIEW, EINVAL},
257         {NT_STATUS_UNABLE_TO_FREE_VM, EADDRINUSE},
258         {NT_STATUS_ACCESS_DENIED, EACCES}, 
259         {NT_STATUS_BUFFER_TOO_SMALL, ENOBUFS},
260         {NT_STATUS_WRONG_PASSWORD, EACCES},
261         {NT_STATUS_LOGON_FAILURE, EACCES},
262         {NT_STATUS_INVALID_WORKSTATION, EACCES},
263         {NT_STATUS_INVALID_LOGON_HOURS, EACCES},
264         {NT_STATUS_PASSWORD_EXPIRED, EACCES},
265         {NT_STATUS_ACCOUNT_DISABLED, EACCES},
266         {NT_STATUS_DISK_FULL, ENOSPC},
267         {NT_STATUS_INVALID_PIPE_STATE, EPIPE},
268         {NT_STATUS_PIPE_BUSY, EPIPE},
269         {NT_STATUS_PIPE_DISCONNECTED, EPIPE},
270         {NT_STATUS_PIPE_NOT_AVAILABLE, ENOSYS},
271         {NT_STATUS_FILE_IS_A_DIRECTORY, EISDIR},
272         {NT_STATUS_NOT_SUPPORTED, ENOSYS},
273         {NT_STATUS_NOT_A_DIRECTORY, ENOTDIR},
274         {NT_STATUS_DIRECTORY_NOT_EMPTY, ENOTEMPTY},
275         {NT_STATUS_NETWORK_UNREACHABLE, ENETUNREACH},
276         {NT_STATUS_HOST_UNREACHABLE, EHOSTUNREACH},
277         {NT_STATUS_CONNECTION_ABORTED, ECONNABORTED},
278         {NT_STATUS_CONNECTION_REFUSED, ECONNREFUSED},
279         {NT_STATUS_TOO_MANY_LINKS, EMLINK},
280         {NT_STATUS_NETWORK_BUSY, EBUSY},
281         {NT_STATUS_DEVICE_DOES_NOT_EXIST, ENODEV},
282 #ifdef ELIBACC
283         {NT_STATUS_DLL_NOT_FOUND, ELIBACC},
284 #endif
285         {NT_STATUS_PIPE_BROKEN, EPIPE},
286         {NT_STATUS_REMOTE_NOT_LISTENING, ECONNREFUSED},
287         {NT_STATUS_NETWORK_ACCESS_DENIED, EACCES},
288         {NT_STATUS_TOO_MANY_OPENED_FILES, EMFILE},
289 #ifdef EPROTO
290         {NT_STATUS_DEVICE_PROTOCOL_ERROR, EPROTO},
291 #endif
292         {NT_STATUS_FLOAT_OVERFLOW, ERANGE},
293         {NT_STATUS_FLOAT_UNDERFLOW, ERANGE},
294         {NT_STATUS_INTEGER_OVERFLOW, ERANGE},
295         {NT_STATUS_MEDIA_WRITE_PROTECTED, EROFS},
296         {NT_STATUS_PIPE_CONNECTED, EISCONN},
297         {NT_STATUS_MEMORY_NOT_ALLOCATED, EFAULT},
298         {NT_STATUS_FLOAT_INEXACT_RESULT, ERANGE},
299         {NT_STATUS_ILL_FORMED_PASSWORD, EACCES},
300         {NT_STATUS_PASSWORD_RESTRICTION, EACCES},
301         {NT_STATUS_ACCOUNT_RESTRICTION, EACCES},
302         {NT_STATUS_PORT_CONNECTION_REFUSED, ECONNREFUSED},
303         {NT_STATUS_NAME_TOO_LONG, ENAMETOOLONG},
304         {NT_STATUS_REMOTE_DISCONNECT, ESHUTDOWN},
305         {NT_STATUS_CONNECTION_DISCONNECTED, ECONNABORTED},
306         {NT_STATUS_CONNECTION_RESET, ENETRESET},
307 #ifdef ENOTUNIQ
308         {NT_STATUS_IP_ADDRESS_CONFLICT1, ENOTUNIQ},
309         {NT_STATUS_IP_ADDRESS_CONFLICT2, ENOTUNIQ},
310 #endif
311         {NT_STATUS_PORT_MESSAGE_TOO_LONG, EMSGSIZE},
312         {NT_STATUS_PROTOCOL_UNREACHABLE, ENOPROTOOPT},
313         {NT_STATUS_ADDRESS_ALREADY_EXISTS, EADDRINUSE},
314         {NT_STATUS_PORT_UNREACHABLE, EHOSTUNREACH},
315         {NT_STATUS_IO_TIMEOUT, ETIMEDOUT},
316         {NT_STATUS_RETRY, EAGAIN},
317 #ifdef ECOMM
318         {NT_STATUS_NET_WRITE_FAULT, ECOMM},
319 #endif
320
321         {NT_STATUS(0), 0}
322 };
323
324 static int cli_errno_from_nt(NTSTATUS status)
325 {
326         int i;
327         DEBUG(10,("cli_errno_from_nt: 32 bit codes: code=%08x\n", NT_STATUS_V(status)));
328
329         /* Status codes without this bit set are not errors */
330
331         if (!(NT_STATUS_V(status) & 0xc0000000))
332                 return 0;
333
334         for (i=0;nt_errno_map[i].error;i++) {
335                 if (NT_STATUS_V(nt_errno_map[i].status) ==
336                     NT_STATUS_V(status)) return nt_errno_map[i].error;
337         }
338
339         /* for all other cases - a default code */
340         return EINVAL;
341 }
342
343 /* Return a UNIX errno appropriate for the error received in the last
344    packet. */
345
346 int cli_errno(struct cli_state *cli)
347 {
348         NTSTATUS status;
349
350         if (cli_is_dos_error(cli)) {
351                 uint8 eclass;
352                 uint32 ecode;
353
354                 cli_dos_error(cli, &eclass, &ecode);
355                 return cli_errno_from_dos(eclass, ecode);
356         }
357
358         status = cli_nt_error(cli);
359
360         return cli_errno_from_nt(status);
361 }
362
363 /* Return true if the last packet was in error */
364
365 BOOL cli_is_error(struct cli_state *cli)
366 {
367         uint32 flgs2 = SVAL(cli->inbuf,smb_flg2), rcls = 0;
368
369         if (flgs2 & FLAGS2_32_BIT_ERROR_CODES) {
370                 /* Return error is error bits are set */
371                 rcls = IVAL(cli->inbuf, smb_rcls);
372                 return (rcls & 0xF0000000) == 0xC0000000;
373         }
374                 
375         /* Return error if error class in non-zero */
376
377         rcls = CVAL(cli->inbuf, smb_rcls);
378         return rcls != 0;
379 }
380
381 /* Return true if the last error was an NT error */
382
383 BOOL cli_is_nt_error(struct cli_state *cli)
384 {
385         uint32 flgs2 = SVAL(cli->inbuf,smb_flg2);
386
387         return cli_is_error(cli) && (flgs2 & FLAGS2_32_BIT_ERROR_CODES);
388 }
389
390 /* Return true if the last error was a DOS error */
391
392 BOOL cli_is_dos_error(struct cli_state *cli)
393 {
394         uint32 flgs2 = SVAL(cli->inbuf,smb_flg2);
395
396         return cli_is_error(cli) && !(flgs2 & FLAGS2_32_BIT_ERROR_CODES);
397 }