Don't use errno's when they're not available
[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    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         {NT_STATUS_PAGEFILE_QUOTA, EDQUOT},
235 #ifdef ETIME
236         {NT_STATUS_TIMER_NOT_CANCELED, ETIME},
237 #endif
238         {NT_STATUS_INVALID_PARAMETER, EINVAL},
239         {NT_STATUS_NO_SUCH_DEVICE, ENODEV},
240         {NT_STATUS_NO_SUCH_FILE, ENOENT},
241 #ifdef ENODATA
242         {NT_STATUS_END_OF_FILE, ENODATA}, 
243 #endif
244 #ifdef ENOMEDIUM
245         {NT_STATUS_NO_MEDIA_IN_DEVICE, ENOMEDIUM}, 
246         {NT_STATUS_NO_MEDIA, ENOMEDIUM},
247 #endif
248         {NT_STATUS_NONEXISTENT_SECTOR, ESPIPE}, 
249         {NT_STATUS_NO_MEMORY, ENOMEM},
250         {NT_STATUS_CONFLICTING_ADDRESSES, EADDRINUSE},
251         {NT_STATUS_NOT_MAPPED_VIEW, EINVAL},
252         {NT_STATUS_UNABLE_TO_FREE_VM, EADDRINUSE},
253         {NT_STATUS_ACCESS_DENIED, EACCES}, 
254         {NT_STATUS_BUFFER_TOO_SMALL, ENOBUFS},
255         {NT_STATUS_QUOTA_EXCEEDED, EDQUOT},
256         {NT_STATUS_WRONG_PASSWORD, EACCES},
257         {NT_STATUS_LOGON_FAILURE, EACCES},
258         {NT_STATUS_INVALID_WORKSTATION, EACCES},
259         {NT_STATUS_INVALID_LOGON_HOURS, EACCES},
260         {NT_STATUS_PASSWORD_EXPIRED, EACCES},
261         {NT_STATUS_ACCOUNT_DISABLED, EACCES},
262         {NT_STATUS_DISK_FULL, ENOSPC},
263         {NT_STATUS_INVALID_PIPE_STATE, EPIPE},
264         {NT_STATUS_PIPE_BUSY, EPIPE},
265         {NT_STATUS_PIPE_DISCONNECTED, EPIPE},
266         {NT_STATUS_PIPE_NOT_AVAILABLE, ENOSYS},
267         {NT_STATUS_FILE_IS_A_DIRECTORY, EISDIR},
268         {NT_STATUS_NOT_SUPPORTED, ENOSYS},
269         {NT_STATUS_NOT_A_DIRECTORY, ENOTDIR},
270         {NT_STATUS_DIRECTORY_NOT_EMPTY, ENOTEMPTY},
271         {NT_STATUS_NETWORK_UNREACHABLE, ENETUNREACH},
272         {NT_STATUS_HOST_UNREACHABLE, EHOSTUNREACH},
273         {NT_STATUS_CONNECTION_ABORTED, ECONNABORTED},
274         {NT_STATUS_CONNECTION_REFUSED, ECONNREFUSED},
275         {NT_STATUS_REGISTRY_QUOTA_LIMIT, EDQUOT},
276         {NT_STATUS_LICENSE_QUOTA_EXCEEDED, EDQUOT},
277         {NT_STATUS_TOO_MANY_LINKS, EMLINK},
278         {NT_STATUS_NETWORK_BUSY, EBUSY},
279         {NT_STATUS_DEVICE_DOES_NOT_EXIST, ENODEV},
280 #ifdef ELIBACC
281         {NT_STATUS_DLL_NOT_FOUND, ELIBACC},
282 #endif
283         {NT_STATUS_PIPE_BROKEN, EPIPE},
284         {NT_STATUS_REMOTE_NOT_LISTENING, ECONNREFUSED},
285         {NT_STATUS_NETWORK_ACCESS_DENIED, EACCES},
286         {NT_STATUS_TOO_MANY_OPENED_FILES, EMFILE},
287 #ifdef EPROTO
288         {NT_STATUS_DEVICE_PROTOCOL_ERROR, EPROTO},
289 #endif
290         {NT_STATUS_FLOAT_OVERFLOW, ERANGE},
291         {NT_STATUS_FLOAT_UNDERFLOW, ERANGE},
292         {NT_STATUS_INTEGER_OVERFLOW, ERANGE},
293         {NT_STATUS_MEDIA_WRITE_PROTECTED, EROFS},
294         {NT_STATUS_PIPE_CONNECTED, EISCONN},
295         {NT_STATUS_MEMORY_NOT_ALLOCATED, EFAULT},
296         {NT_STATUS_FLOAT_INEXACT_RESULT, ERANGE},
297         {NT_STATUS_ILL_FORMED_PASSWORD, EACCES},
298         {NT_STATUS_PASSWORD_RESTRICTION, EACCES},
299         {NT_STATUS_ACCOUNT_RESTRICTION, EACCES},
300         {NT_STATUS_PORT_CONNECTION_REFUSED, ECONNREFUSED},
301         {NT_STATUS_NAME_TOO_LONG, ENAMETOOLONG},
302         {NT_STATUS_REMOTE_DISCONNECT, ESHUTDOWN},
303         {NT_STATUS_CONNECTION_DISCONNECTED, ECONNABORTED},
304         {NT_STATUS_CONNECTION_RESET, ENETRESET},
305 #ifdef ENOTUNIQ
306         {NT_STATUS_IP_ADDRESS_CONFLICT1, ENOTUNIQ},
307         {NT_STATUS_IP_ADDRESS_CONFLICT2, ENOTUNIQ},
308 #endif
309         {NT_STATUS_PORT_MESSAGE_TOO_LONG, EMSGSIZE},
310         {NT_STATUS_PROTOCOL_UNREACHABLE, ENOPROTOOPT},
311         {NT_STATUS_ADDRESS_ALREADY_EXISTS, EADDRINUSE},
312         {NT_STATUS_PORT_UNREACHABLE, EHOSTUNREACH},
313         {NT_STATUS_IO_TIMEOUT, ETIMEDOUT},
314         {NT_STATUS_RETRY, EAGAIN},
315 #ifdef ECOMM
316         {NT_STATUS_NET_WRITE_FAULT, ECOMM},
317 #endif
318
319         {NT_STATUS(0), 0}
320 };
321
322 static int cli_errno_from_nt(NTSTATUS status)
323 {
324         int i;
325         DEBUG(10,("cli_errno_from_nt: 32 bit codes: code=%08x\n", NT_STATUS_V(status)));
326
327         /* Status codes without this bit set are not errors */
328
329         if (!(NT_STATUS_V(status) & 0xc0000000))
330                 return 0;
331
332         for (i=0;nt_errno_map[i].error;i++) {
333                 if (NT_STATUS_V(nt_errno_map[i].status) ==
334                     NT_STATUS_V(status)) return nt_errno_map[i].error;
335         }
336
337         /* for all other cases - a default code */
338         return EINVAL;
339 }
340
341 /* Return a UNIX errno appropriate for the error received in the last
342    packet. */
343
344 int cli_errno(struct cli_state *cli)
345 {
346         NTSTATUS status;
347
348         if (cli_is_dos_error(cli)) {
349                 uint8 eclass;
350                 uint32 ecode;
351
352                 cli_dos_error(cli, &eclass, &ecode);
353                 return cli_errno_from_dos(eclass, ecode);
354         }
355
356         status = cli_nt_error(cli);
357
358         return cli_errno_from_nt(status);
359 }
360
361 /* Return true if the last packet was in error */
362
363 BOOL cli_is_error(struct cli_state *cli)
364 {
365         uint32 flgs2 = SVAL(cli->inbuf,smb_flg2), rcls = 0;
366
367         if (flgs2 & FLAGS2_32_BIT_ERROR_CODES) {
368                 /* Return error is error bits are set */
369                 rcls = IVAL(cli->inbuf, smb_rcls);
370                 return (rcls & 0xF0000000) == 0xC0000000;
371         }
372                 
373         /* Return error if error class in non-zero */
374
375         rcls = CVAL(cli->inbuf, smb_rcls);
376         return rcls != 0;
377 }
378
379 /* Return true if the last error was an NT error */
380
381 BOOL cli_is_nt_error(struct cli_state *cli)
382 {
383         uint32 flgs2 = SVAL(cli->inbuf,smb_flg2);
384
385         return cli_is_error(cli) && (flgs2 & FLAGS2_32_BIT_ERROR_CODES);
386 }
387
388 /* Return true if the last error was a DOS error */
389
390 BOOL cli_is_dos_error(struct cli_state *cli)
391 {
392         uint32 flgs2 = SVAL(cli->inbuf,smb_flg2);
393
394         return cli_is_error(cli) && !(flgs2 & FLAGS2_32_BIT_ERROR_CODES);
395 }