changes to sync with 2.2. tree
[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
27 extern int DEBUGLEVEL;
28
29
30 /*****************************************************
31  RAP error codes - a small start but will be extended.
32 *******************************************************/
33
34 static struct
35 {
36   int err;
37   char *message;
38 } rap_errmap[] =
39 {
40   {5,    "User has insufficient privilege" },
41   {86,   "The specified password is invalid" },
42   {2226, "Operation only permitted on a Primary Domain Controller"  },
43   {2242, "The password of this user has expired." },
44   {2243, "The password of this user cannot change." },
45   {2244, "This password cannot be used now (password history conflict)." },
46   {2245, "The password is shorter than required." },
47   {2246, "The password of this user is too recent to change."},
48
49   /* these really shouldn't be here ... */
50   {0x80, "Not listening on called name"},
51   {0x81, "Not listening for calling name"},
52   {0x82, "Called name not present"},
53   {0x83, "Called name present, but insufficient resources"},
54
55   {0, NULL}
56 };  
57
58 /****************************************************************************
59   return a description of an SMB error
60 ****************************************************************************/
61 static char *cli_smb_errstr(struct cli_state *cli)
62 {
63         return smb_errstr(cli->inbuf);
64 }
65
66 /******************************************************
67  Return an error message - either an SMB error or a RAP
68  error.
69 *******************************************************/
70     
71 char *cli_errstr(struct cli_state *cli)
72 {   
73         static fstring error_message;
74         uint8 errclass;
75         uint32 errnum;
76         uint32 nt_rpc_error;
77         int i;      
78
79         /*  
80          * Errors are of three kinds - smb errors,
81          * dealt with by cli_smb_errstr, NT errors,
82          * whose code is in cli.nt_error, and rap
83          * errors, whose error code is in cli.rap_error.
84          */ 
85
86         cli_error(cli, &errclass, &errnum, &nt_rpc_error);
87
88         if (errclass != 0)
89         {
90                 return cli_smb_errstr(cli);
91         }
92
93         /*
94          * Was it an NT error ?
95          */
96
97         if (nt_rpc_error)
98         {
99                 char *nt_msg = (char *)get_nt_error_msg(nt_rpc_error);
100
101                 if (nt_msg == NULL)
102                 {
103                         slprintf(error_message, sizeof(fstring) - 1, "NT code %d", nt_rpc_error);
104                 }
105                 else
106                 {
107                         fstrcpy(error_message, nt_msg);
108                 }
109
110                 return error_message;
111         }
112
113         /*
114          * Must have been a rap error.
115          */
116
117         slprintf(error_message, sizeof(error_message) - 1, "code %d", cli->rap_error);
118
119         for (i = 0; rap_errmap[i].message != NULL; i++)
120         {
121                 if (rap_errmap[i].err == cli->rap_error)
122                 {
123                         fstrcpy( error_message, rap_errmap[i].message);
124                         break;
125                 }
126         } 
127
128         return error_message;
129 }
130
131
132 /****************************************************************************
133   return error codes for the last packet
134   returns 0 if there was no error and the best approx of a unix errno
135   otherwise
136
137   for 32 bit "warnings", a return code of 0 is expected.
138
139 ****************************************************************************/
140 int cli_error(struct cli_state *cli, uint8 *eclass, uint32 *num, uint32 *nt_rpc_error)
141 {
142         int  flgs2;
143         char rcls;
144         int code;
145
146         if (eclass) *eclass = 0;
147         if (num   ) *num = 0;
148         if (nt_rpc_error) *nt_rpc_error = 0;
149
150         if(!cli->initialised)
151                 return EINVAL;
152
153         if(!cli->inbuf)
154                 return ENOMEM;
155
156         flgs2 = SVAL(cli->inbuf,smb_flg2);
157         if (nt_rpc_error) *nt_rpc_error = cli->nt_error;
158
159         if (flgs2 & FLAGS2_32_BIT_ERROR_CODES) {
160                 /* 32 bit error codes detected */
161                 uint32 nt_err = IVAL(cli->inbuf,smb_rcls);
162                 if (num) *num = nt_err;
163                 DEBUG(10,("cli_error: 32 bit codes: code=%08x\n", nt_err));
164                 if (!(nt_err & 0xc0000000))
165                         return 0;
166
167                 switch (nt_err) {
168                 case NT_STATUS_ACCESS_VIOLATION: return EACCES;
169                 case NT_STATUS_NO_SUCH_FILE: return ENOENT;
170                 case NT_STATUS_NO_SUCH_DEVICE: return ENODEV;
171                 case NT_STATUS_INVALID_HANDLE: return EBADF;
172                 case NT_STATUS_NO_MEMORY: return ENOMEM;
173                 case NT_STATUS_ACCESS_DENIED: return EACCES;
174                 case NT_STATUS_OBJECT_NAME_NOT_FOUND: return ENOENT;
175                 case NT_STATUS_SHARING_VIOLATION: return EBUSY;
176                 case NT_STATUS_OBJECT_PATH_INVALID: return ENOTDIR;
177                 case NT_STATUS_OBJECT_NAME_COLLISION: return EEXIST;
178                 }
179
180                 /* for all other cases - a default code */
181                 return EINVAL;
182         }
183
184         rcls  = CVAL(cli->inbuf,smb_rcls);
185         code  = SVAL(cli->inbuf,smb_err);
186         if (rcls == 0) return 0;
187
188         if (eclass) *eclass = rcls;
189         if (num   ) *num    = code;
190
191         if (rcls == ERRDOS) {
192                 switch (code) {
193                 case ERRbadfile: return ENOENT;
194                 case ERRbadpath: return ENOTDIR;
195                 case ERRnoaccess: return EACCES;
196                 case ERRfilexists: return EEXIST;
197                 case ERRrename: return EEXIST;
198                 case ERRbadshare: return EBUSY;
199                 case ERRlock: return EBUSY;
200                 case ERROR_INVALID_NAME: return ENOENT;
201                 }
202         }
203         if (rcls == ERRSRV) {
204                 switch (code) {
205                 case ERRbadpw: return EPERM;
206                 case ERRaccess: return EACCES;
207                 case ERRnoresource: return ENOMEM;
208                 case ERRinvdevice: return ENODEV;
209                 case ERRinvnetname: return ENODEV;
210                 }
211         }
212         /* for other cases */
213         return EINVAL;
214 }
215