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