Make sure we don't clobber the stack when response consists of the empty
[Samba/gebeck_regimport.git] / source3 / libsmb / clierror.c
blobc27e1955e20c74052b2a7eb2f50d803b7386a21f
1 /*
2 Unix SMB/CIFS implementation.
3 client error handling routines
4 Copyright (C) Andrew Tridgell 1994-1998
5 Copyright (C) Jelmer Vernooij 2003
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.
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.
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.
22 #define NO_SYSLOG
24 #include "includes.h"
26 /*****************************************************
27 RAP error codes - a small start but will be extended.
29 XXX: Perhaps these should move into a common function because they're
30 duplicated in clirap2.c
32 *******************************************************/
34 static const struct
36 int err;
37 const char *message;
38 } rap_errmap[] =
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."},
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"},
60 {0, NULL}
61 };
63 /****************************************************************************
64 return a description of an SMB error
65 ****************************************************************************/
66 static const char *cli_smb_errstr(struct cli_state *cli)
68 return smb_dos_errstr(cli->inbuf);
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 ****************************************************************************/
77 const char *cli_errstr(struct cli_state *cli)
79 static fstring cli_error_message;
80 uint32 flgs2 = SVAL(cli->inbuf,smb_flg2), errnum;
81 uint8 errclass;
82 int i;
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;
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)" );
100 break;
101 case READ_ERROR:
102 slprintf(cli_error_message, sizeof(cli_error_message) - 1,
103 "Read error: %s", strerror(errno) );
104 break;
105 case WRITE_ERROR:
106 slprintf(cli_error_message, sizeof(cli_error_message) - 1,
107 "Write error: %s", strerror(errno) );
108 break;
109 case READ_BAD_SIG:
110 slprintf(cli_error_message, sizeof(cli_error_message) - 1,
111 "Server packet had invalid SMB signature!");
112 break;
113 default:
114 slprintf(cli_error_message, sizeof(cli_error_message) - 1,
115 "Unknown error code %d\n", cli->smb_rw_error );
116 break;
118 return cli_error_message;
121 /* Case #1: RAP error */
122 if (cli->rap_error) {
123 for (i = 0; rap_errmap[i].message != NULL; i++) {
124 if (rap_errmap[i].err == cli->rap_error) {
125 return rap_errmap[i].message;
129 slprintf(cli_error_message, sizeof(cli_error_message) - 1, "RAP code %d",
130 cli->rap_error);
132 return cli_error_message;
135 /* Case #2: 32-bit NT errors */
136 if (flgs2 & FLAGS2_32_BIT_ERROR_CODES) {
137 NTSTATUS status = NT_STATUS(IVAL(cli->inbuf,smb_rcls));
139 return nt_errstr(status);
142 cli_dos_error(cli, &errclass, &errnum);
144 /* Case #3: SMB error */
146 return cli_smb_errstr(cli);
150 /* Return the 32-bit NT status code from the last packet */
151 NTSTATUS cli_nt_error(struct cli_state *cli)
153 int flgs2 = SVAL(cli->inbuf,smb_flg2);
155 if (!(flgs2 & FLAGS2_32_BIT_ERROR_CODES)) {
156 int class = CVAL(cli->inbuf,smb_rcls);
157 int code = SVAL(cli->inbuf,smb_err);
158 return dos_to_ntstatus(class, code);
161 return NT_STATUS(IVAL(cli->inbuf,smb_rcls));
165 /* Return the DOS error from the last packet - an error class and an error
166 code. */
167 void cli_dos_error(struct cli_state *cli, uint8 *eclass, uint32 *ecode)
169 int flgs2;
170 char rcls;
171 int code;
173 if(!cli->initialised) return;
175 flgs2 = SVAL(cli->inbuf,smb_flg2);
177 if (flgs2 & FLAGS2_32_BIT_ERROR_CODES) {
178 NTSTATUS ntstatus = NT_STATUS(IVAL(cli->inbuf, smb_rcls));
179 ntstatus_to_dos(ntstatus, eclass, ecode);
180 return;
183 rcls = CVAL(cli->inbuf,smb_rcls);
184 code = SVAL(cli->inbuf,smb_err);
186 if (eclass) *eclass = rcls;
187 if (ecode) *ecode = code;
190 /* Return a UNIX errno from a dos error class, error number tuple */
192 static int cli_errno_from_dos(uint8 eclass, uint32 num)
194 if (eclass == ERRDOS) {
195 switch (num) {
196 case ERRbadfile: return ENOENT;
197 case ERRbadpath: return ENOTDIR;
198 case ERRnoaccess: return EACCES;
199 case ERRfilexists: return EEXIST;
200 case ERRrename: return EEXIST;
201 case ERRbadshare: return EBUSY;
202 case ERRlock: return EBUSY;
203 case ERRinvalidname: return ENOENT;
204 case ERRnosuchshare: return ENODEV;
208 if (eclass == ERRSRV) {
209 switch (num) {
210 case ERRbadpw: return EPERM;
211 case ERRaccess: return EACCES;
212 case ERRnoresource: return ENOMEM;
213 case ERRinvdevice: return ENODEV;
214 case ERRinvnetname: return ENODEV;
218 /* for other cases */
219 return EINVAL;
222 /* Return a UNIX errno from a NT status code */
223 static struct {
224 NTSTATUS status;
225 int error;
226 } nt_errno_map[] = {
227 {NT_STATUS_ACCESS_VIOLATION, EACCES},
228 {NT_STATUS_INVALID_HANDLE, EBADF},
229 {NT_STATUS_ACCESS_DENIED, EACCES},
230 {NT_STATUS_OBJECT_NAME_NOT_FOUND, ENOENT},
231 {NT_STATUS_SHARING_VIOLATION, EBUSY},
232 {NT_STATUS_OBJECT_PATH_INVALID, ENOTDIR},
233 {NT_STATUS_OBJECT_NAME_COLLISION, EEXIST},
234 {NT_STATUS_PATH_NOT_COVERED, ENOENT},
235 {NT_STATUS_UNSUCCESSFUL, EINVAL},
236 {NT_STATUS_NOT_IMPLEMENTED, ENOSYS},
237 {NT_STATUS_IN_PAGE_ERROR, EFAULT},
238 {NT_STATUS_BAD_NETWORK_NAME, ENOENT},
239 #ifdef EDQUOT
240 {NT_STATUS_PAGEFILE_QUOTA, EDQUOT},
241 {NT_STATUS_QUOTA_EXCEEDED, EDQUOT},
242 {NT_STATUS_REGISTRY_QUOTA_LIMIT, EDQUOT},
243 {NT_STATUS_LICENSE_QUOTA_EXCEEDED, EDQUOT},
244 #endif
245 #ifdef ETIME
246 {NT_STATUS_TIMER_NOT_CANCELED, ETIME},
247 #endif
248 {NT_STATUS_INVALID_PARAMETER, EINVAL},
249 {NT_STATUS_NO_SUCH_DEVICE, ENODEV},
250 {NT_STATUS_NO_SUCH_FILE, ENOENT},
251 #ifdef ENODATA
252 {NT_STATUS_END_OF_FILE, ENODATA},
253 #endif
254 #ifdef ENOMEDIUM
255 {NT_STATUS_NO_MEDIA_IN_DEVICE, ENOMEDIUM},
256 {NT_STATUS_NO_MEDIA, ENOMEDIUM},
257 #endif
258 {NT_STATUS_NONEXISTENT_SECTOR, ESPIPE},
259 {NT_STATUS_NO_MEMORY, ENOMEM},
260 {NT_STATUS_CONFLICTING_ADDRESSES, EADDRINUSE},
261 {NT_STATUS_NOT_MAPPED_VIEW, EINVAL},
262 {NT_STATUS_UNABLE_TO_FREE_VM, EADDRINUSE},
263 {NT_STATUS_ACCESS_DENIED, EACCES},
264 {NT_STATUS_BUFFER_TOO_SMALL, ENOBUFS},
265 {NT_STATUS_WRONG_PASSWORD, EACCES},
266 {NT_STATUS_LOGON_FAILURE, EACCES},
267 {NT_STATUS_INVALID_WORKSTATION, EACCES},
268 {NT_STATUS_INVALID_LOGON_HOURS, EACCES},
269 {NT_STATUS_PASSWORD_EXPIRED, EACCES},
270 {NT_STATUS_ACCOUNT_DISABLED, EACCES},
271 {NT_STATUS_DISK_FULL, ENOSPC},
272 {NT_STATUS_INVALID_PIPE_STATE, EPIPE},
273 {NT_STATUS_PIPE_BUSY, EPIPE},
274 {NT_STATUS_PIPE_DISCONNECTED, EPIPE},
275 {NT_STATUS_PIPE_NOT_AVAILABLE, ENOSYS},
276 {NT_STATUS_FILE_IS_A_DIRECTORY, EISDIR},
277 {NT_STATUS_NOT_SUPPORTED, ENOSYS},
278 {NT_STATUS_NOT_A_DIRECTORY, ENOTDIR},
279 {NT_STATUS_DIRECTORY_NOT_EMPTY, ENOTEMPTY},
280 {NT_STATUS_NETWORK_UNREACHABLE, ENETUNREACH},
281 {NT_STATUS_HOST_UNREACHABLE, EHOSTUNREACH},
282 {NT_STATUS_CONNECTION_ABORTED, ECONNABORTED},
283 {NT_STATUS_CONNECTION_REFUSED, ECONNREFUSED},
284 {NT_STATUS_TOO_MANY_LINKS, EMLINK},
285 {NT_STATUS_NETWORK_BUSY, EBUSY},
286 {NT_STATUS_DEVICE_DOES_NOT_EXIST, ENODEV},
287 #ifdef ELIBACC
288 {NT_STATUS_DLL_NOT_FOUND, ELIBACC},
289 #endif
290 {NT_STATUS_PIPE_BROKEN, EPIPE},
291 {NT_STATUS_REMOTE_NOT_LISTENING, ECONNREFUSED},
292 {NT_STATUS_NETWORK_ACCESS_DENIED, EACCES},
293 {NT_STATUS_TOO_MANY_OPENED_FILES, EMFILE},
294 #ifdef EPROTO
295 {NT_STATUS_DEVICE_PROTOCOL_ERROR, EPROTO},
296 #endif
297 {NT_STATUS_FLOAT_OVERFLOW, ERANGE},
298 {NT_STATUS_FLOAT_UNDERFLOW, ERANGE},
299 {NT_STATUS_INTEGER_OVERFLOW, ERANGE},
300 {NT_STATUS_MEDIA_WRITE_PROTECTED, EROFS},
301 {NT_STATUS_PIPE_CONNECTED, EISCONN},
302 {NT_STATUS_MEMORY_NOT_ALLOCATED, EFAULT},
303 {NT_STATUS_FLOAT_INEXACT_RESULT, ERANGE},
304 {NT_STATUS_ILL_FORMED_PASSWORD, EACCES},
305 {NT_STATUS_PASSWORD_RESTRICTION, EACCES},
306 {NT_STATUS_ACCOUNT_RESTRICTION, EACCES},
307 {NT_STATUS_PORT_CONNECTION_REFUSED, ECONNREFUSED},
308 {NT_STATUS_NAME_TOO_LONG, ENAMETOOLONG},
309 {NT_STATUS_REMOTE_DISCONNECT, ESHUTDOWN},
310 {NT_STATUS_CONNECTION_DISCONNECTED, ECONNABORTED},
311 {NT_STATUS_CONNECTION_RESET, ENETRESET},
312 #ifdef ENOTUNIQ
313 {NT_STATUS_IP_ADDRESS_CONFLICT1, ENOTUNIQ},
314 {NT_STATUS_IP_ADDRESS_CONFLICT2, ENOTUNIQ},
315 #endif
316 {NT_STATUS_PORT_MESSAGE_TOO_LONG, EMSGSIZE},
317 {NT_STATUS_PROTOCOL_UNREACHABLE, ENOPROTOOPT},
318 {NT_STATUS_ADDRESS_ALREADY_EXISTS, EADDRINUSE},
319 {NT_STATUS_PORT_UNREACHABLE, EHOSTUNREACH},
320 {NT_STATUS_IO_TIMEOUT, ETIMEDOUT},
321 {NT_STATUS_RETRY, EAGAIN},
322 #ifdef ECOMM
323 {NT_STATUS_NET_WRITE_FAULT, ECOMM},
324 #endif
326 {NT_STATUS(0), 0}
329 static int cli_errno_from_nt(NTSTATUS status)
331 int i;
332 DEBUG(10,("cli_errno_from_nt: 32 bit codes: code=%08x\n", NT_STATUS_V(status)));
334 /* Status codes without this bit set are not errors */
336 if (!(NT_STATUS_V(status) & 0xc0000000))
337 return 0;
339 for (i=0;nt_errno_map[i].error;i++) {
340 if (NT_STATUS_V(nt_errno_map[i].status) ==
341 NT_STATUS_V(status)) return nt_errno_map[i].error;
344 /* for all other cases - a default code */
345 return EINVAL;
348 /* Return a UNIX errno appropriate for the error received in the last
349 packet. */
351 int cli_errno(struct cli_state *cli)
353 NTSTATUS status;
355 if (cli_is_dos_error(cli)) {
356 uint8 eclass;
357 uint32 ecode;
359 cli_dos_error(cli, &eclass, &ecode);
360 return cli_errno_from_dos(eclass, ecode);
363 status = cli_nt_error(cli);
365 return cli_errno_from_nt(status);
368 /* Return true if the last packet was in error */
370 BOOL cli_is_error(struct cli_state *cli)
372 uint32 flgs2 = SVAL(cli->inbuf,smb_flg2), rcls = 0;
374 if (cli->fd == -1 && cli->smb_rw_error != 0)
375 return True;
377 if (flgs2 & FLAGS2_32_BIT_ERROR_CODES) {
378 /* Return error is error bits are set */
379 rcls = IVAL(cli->inbuf, smb_rcls);
380 return (rcls & 0xF0000000) == 0xC0000000;
383 /* Return error if error class in non-zero */
385 rcls = CVAL(cli->inbuf, smb_rcls);
386 return rcls != 0;
389 /* Return true if the last error was an NT error */
391 BOOL cli_is_nt_error(struct cli_state *cli)
393 uint32 flgs2 = SVAL(cli->inbuf,smb_flg2);
395 return cli_is_error(cli) && (flgs2 & FLAGS2_32_BIT_ERROR_CODES);
398 /* Return true if the last error was a DOS error */
400 BOOL cli_is_dos_error(struct cli_state *cli)
402 uint32 flgs2 = SVAL(cli->inbuf,smb_flg2);
404 return cli_is_error(cli) && !(flgs2 & FLAGS2_32_BIT_ERROR_CODES);