Fix --enable-developer shadow warning
[bbaumbach/samba-autobuild/.git] / source3 / libsmb / clierror.c
1 /* 
2    Unix SMB/Netbios implementation.
3    Version 3.0
4    client error handling routines
5    Copyright (C) Andrew Tridgell 1994-1998
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
30 static const struct
31 {
32   int err;
33   char *message;
34 } rap_errmap[] =
35 {
36   {5,    "User has insufficient privilege" },
37   {86,   "The specified password is invalid" },
38   {2226, "Operation only permitted on a Primary Domain Controller"  },
39   {2242, "The password of this user has expired." },
40   {2243, "The password of this user cannot change." },
41   {2244, "This password cannot be used now (password history conflict)." },
42   {2245, "The password is shorter than required." },
43   {2246, "The password of this user is too recent to change."},
44
45   /* these really shouldn't be here ... */
46   {0x80, "Not listening on called name"},
47   {0x81, "Not listening for calling name"},
48   {0x82, "Called name not present"},
49   {0x83, "Called name present, but insufficient resources"},
50
51   {0, NULL}
52 };  
53
54 /****************************************************************************
55   return a description of an SMB error
56 ****************************************************************************/
57 static char *cli_smb_errstr(struct cli_state *cli)
58 {
59         return smb_dos_errstr(cli->inbuf);
60 }
61
62 /***************************************************************************
63  Return an error message - either an NT error, SMB error or a RAP error.
64  Note some of the NT errors are actually warnings or "informational" errors
65  in which case they can be safely ignored.
66 ****************************************************************************/
67     
68 char *cli_errstr(struct cli_state *cli)
69 {   
70         static fstring cli_error_message;
71         uint32 flgs2 = SVAL(cli->inbuf,smb_flg2), errnum;
72         uint8 errclass;
73         int i;
74
75         /* Case #1: 32-bit NT errors */
76         if (flgs2 & FLAGS2_32_BIT_ERROR_CODES) {
77                 NTSTATUS status = NT_STATUS(IVAL(cli->inbuf,smb_rcls));
78
79                 return get_nt_error_msg(status);
80         }
81
82         cli_dos_error(cli, &errclass, &errnum);
83
84         /* Case #2: SMB error */
85
86         if (errclass != 0)
87                 return cli_smb_errstr(cli);
88
89         /* Case #3: RAP error */
90         for (i = 0; rap_errmap[i].message != NULL; i++) {
91                 if (rap_errmap[i].err == cli->rap_error) {
92                         return rap_errmap[i].message;
93                 }
94         } 
95
96         slprintf(cli_error_message, sizeof(cli_error_message) - 1, "code %d", 
97                  cli->rap_error);
98
99         return cli_error_message;
100 }
101
102
103 /* Return the 32-bit NT status code from the last packet */
104 NTSTATUS cli_nt_error(struct cli_state *cli)
105 {
106         int flgs2 = SVAL(cli->inbuf,smb_flg2);
107
108         if (!(flgs2 & FLAGS2_32_BIT_ERROR_CODES)) {
109                 int class  = CVAL(cli->inbuf,smb_rcls);
110                 int code  = SVAL(cli->inbuf,smb_err);
111                 return dos_to_ntstatus(class, code);
112         }
113
114         return NT_STATUS(IVAL(cli->inbuf,smb_rcls));
115 }
116
117
118 /* Return the DOS error from the last packet - an error class and an error
119    code. */
120 void cli_dos_error(struct cli_state *cli, uint8 *eclass, uint32 *ecode)
121 {
122         int  flgs2;
123         char rcls;
124         int code;
125
126         if(!cli->initialised) return;
127
128         flgs2 = SVAL(cli->inbuf,smb_flg2);
129
130         if (flgs2 & FLAGS2_32_BIT_ERROR_CODES) {
131                 NTSTATUS ntstatus = NT_STATUS(IVAL(cli->inbuf, smb_rcls));
132                 ntstatus_to_dos(ntstatus, eclass, ecode);
133                 return;
134         }
135
136         rcls  = CVAL(cli->inbuf,smb_rcls);
137         code  = SVAL(cli->inbuf,smb_err);
138
139         if (eclass) *eclass = rcls;
140         if (ecode) *ecode    = code;
141 }
142
143 /* Return a UNIX errno from a dos error class, error number tuple */
144
145 int cli_errno_from_dos(uint8 eclass, uint32 num)
146 {
147         if (eclass == ERRDOS) {
148                 switch (num) {
149                 case ERRbadfile: return ENOENT;
150                 case ERRbadpath: return ENOTDIR;
151                 case ERRnoaccess: return EACCES;
152                 case ERRfilexists: return EEXIST;
153                 case ERRrename: return EEXIST;
154                 case ERRbadshare: return EBUSY;
155                 case ERRlock: return EBUSY;
156                 case ERRinvalidname: return ENOENT;
157                 case ERRnosuchshare: return ENODEV;
158                 }
159         }
160
161         if (eclass == ERRSRV) {
162                 switch (num) {
163                 case ERRbadpw: return EPERM;
164                 case ERRaccess: return EACCES;
165                 case ERRnoresource: return ENOMEM;
166                 case ERRinvdevice: return ENODEV;
167                 case ERRinvnetname: return ENODEV;
168                 }
169         }
170
171         /* for other cases */
172         return EINVAL;
173 }
174
175 /* Return a UNIX errno from a NT status code */
176 static struct {
177         NTSTATUS status;
178         int error;
179 } nt_errno_map[] = {
180         {NT_STATUS_ACCESS_VIOLATION, EACCES},
181         {NT_STATUS_NO_SUCH_FILE, ENOENT},
182         {NT_STATUS_NO_SUCH_DEVICE, ENODEV},
183         {NT_STATUS_INVALID_HANDLE, EBADF},
184         {NT_STATUS_NO_MEMORY, ENOMEM},
185         {NT_STATUS_ACCESS_DENIED, EACCES},
186         {NT_STATUS_OBJECT_NAME_NOT_FOUND, ENOENT},
187         {NT_STATUS_SHARING_VIOLATION, EBUSY},
188         {NT_STATUS_OBJECT_PATH_INVALID, ENOTDIR},
189         {NT_STATUS_OBJECT_NAME_COLLISION, EEXIST},
190         {NT_STATUS_PATH_NOT_COVERED, ENOENT},
191         {NT_STATUS(0), 0}
192 };
193
194 int cli_errno_from_nt(NTSTATUS status)
195 {
196         int i;
197         DEBUG(10,("cli_errno_from_nt: 32 bit codes: code=%08x\n", NT_STATUS_V(status)));
198
199         /* Status codes without this bit set are not errors */
200
201         if (!(NT_STATUS_V(status) & 0xc0000000))
202                 return 0;
203
204         for (i=0;nt_errno_map[i].error;i++) {
205                 if (NT_STATUS_V(nt_errno_map[i].status) ==
206                     NT_STATUS_V(status)) return nt_errno_map[i].error;
207         }
208
209         /* for all other cases - a default code */
210         return EINVAL;
211 }
212
213 /* Return a UNIX errno appropriate for the error received in the last
214    packet. */
215
216 int cli_errno(struct cli_state *cli)
217 {
218         NTSTATUS status;
219
220         if (cli_is_dos_error(cli)) {
221                 uint8 eclass;
222                 uint32 ecode;
223
224                 cli_dos_error(cli, &eclass, &ecode);
225                 return cli_errno_from_dos(eclass, ecode);
226         }
227
228         status = cli_nt_error(cli);
229
230         return cli_errno_from_nt(status);
231 }
232
233 /* Return true if the last packet was in error */
234
235 BOOL cli_is_error(struct cli_state *cli)
236 {
237         uint32 flgs2 = SVAL(cli->inbuf,smb_flg2), rcls = 0;
238
239         if (flgs2 & FLAGS2_32_BIT_ERROR_CODES) {
240                 /* Return error is error bits are set */
241                 rcls = IVAL(cli->inbuf, smb_rcls);
242                 return (rcls & 0xF0000000) == 0xC0000000;
243         }
244                 
245         /* Return error if error class in non-zero */
246
247         rcls = CVAL(cli->inbuf, smb_rcls);
248         return rcls != 0;
249 }
250
251 /* Return true if the last error was an NT error */
252
253 BOOL cli_is_nt_error(struct cli_state *cli)
254 {
255         uint32 flgs2 = SVAL(cli->inbuf,smb_flg2);
256
257         return cli_is_error(cli) && (flgs2 & FLAGS2_32_BIT_ERROR_CODES);
258 }
259
260 /* Return true if the last error was a DOS error */
261
262 BOOL cli_is_dos_error(struct cli_state *cli)
263 {
264         uint32 flgs2 = SVAL(cli->inbuf,smb_flg2);
265
266         return cli_is_error(cli) && !(flgs2 & FLAGS2_32_BIT_ERROR_CODES);
267 }