libcli: Remove define STATUS_EA_LIST_INCONSISTENT
[samba.git] / libcli / util / nterr.c
1 /*
2  *  Unix SMB/CIFS implementation.
3  *  RPC Pipe client / server routines
4  *
5  *  Copyright (C) Luke Kenneth Casson Leighton 1997-2001.
6  *  Copyright (C) Andrew Bartlett
7  *  Copyright (C) Andrew Tridgell
8  *
9  *  This program is free software; you can redistribute it and/or modify
10  *  it under the terms of the GNU General Public License as published by
11  *  the Free Software Foundation; either version 3 of the License, or
12  *  (at your option) any later version.
13  *
14  *  This program is distributed in the hope that it will be useful,
15  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
16  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  *  GNU General Public License for more details.
18  *
19  *  You should have received a copy of the GNU General Public License
20  *  along with this program; if not, see <http://www.gnu.org/licenses/>.
21  */
22
23 /* NT error codes.  please read nterr.h */
24
25 #include "includes.h"
26 #include "../libcli/ldap/ldap_errors.h"
27 #undef strcasecmp
28
29 #if !defined(N_)
30 #define N_(string) string
31 #endif
32
33 #define DOS_CODE(class, code) { #class ":" #code, NT_STATUS_DOS(class, code) }
34 #define LDAP_CODE(code) { #code, NT_STATUS_LDAP(code) }
35
36 typedef struct
37 {
38         const char *nt_errstr;
39         NTSTATUS nt_errcode;
40 } nt_err_code_struct;
41
42 #include "nterr_gen.c"
43
44 /* Errors which aren't in the generated code because they're not in the
45  * same table as the other ones. */
46 static const nt_err_code_struct special_errs[] =
47 {
48         { "NT_STATUS_OK", NT_STATUS_OK },
49         { "STATUS_NO_MORE_FILES", STATUS_NO_MORE_FILES },
50         { "STATUS_INVALID_EA_NAME", STATUS_INVALID_EA_NAME },
51         { "STATUS_BUFFER_OVERFLOW", STATUS_BUFFER_OVERFLOW },
52         { "STATUS_MORE_ENTRIES", STATUS_MORE_ENTRIES },
53         { "STATUS_SOME_UNMAPPED", STATUS_SOME_UNMAPPED },
54         { "NT_STATUS_ERROR_DS_OBJ_STRING_NAME_EXISTS", NT_STATUS_ERROR_DS_OBJ_STRING_NAME_EXISTS },
55         { "NT_STATUS_ERROR_DS_INCOMPATIBLE_VERSION", NT_STATUS_ERROR_DS_INCOMPATIBLE_VERSION },
56         { "NT_STATUS_SMB_NO_PREAUTH_INTEGRITY_HASH_OVERLAP", NT_STATUS_SMB_NO_PREAUTH_INTEGRITY_HASH_OVERLAP },
57         { "NT_STATUS_INACCESSIBLE_SYSTEM_SHORTCUT", NT_STATUS_INACCESSIBLE_SYSTEM_SHORTCUT },
58         { "NT_STATUS_ABIOS_NOT_PRESENT", NT_STATUS_ABIOS_NOT_PRESENT },
59         { "NT_STATUS_ABIOS_LID_NOT_EXIST", NT_STATUS_ABIOS_LID_NOT_EXIST },
60         { "NT_STATUS_ABIOS_LID_ALREADY_OWNED", NT_STATUS_ABIOS_LID_ALREADY_OWNED },
61         { "NT_STATUS_ABIOS_NOT_LID_OWNER", NT_STATUS_ABIOS_NOT_LID_OWNER },
62         { "NT_STATUS_ABIOS_INVALID_COMMAND", NT_STATUS_ABIOS_INVALID_COMMAND },
63         { "NT_STATUS_ABIOS_INVALID_LID", NT_STATUS_ABIOS_INVALID_LID },
64         { "NT_STATUS_ABIOS_SELECTOR_NOT_AVAILABLE", NT_STATUS_ABIOS_SELECTOR_NOT_AVAILABLE },
65         { "NT_STATUS_ABIOS_INVALID_SELECTOR", NT_STATUS_ABIOS_INVALID_SELECTOR },
66         { "NT_STATUS_HANDLE_NOT_WAITABLE", NT_STATUS_HANDLE_NOT_WAITABLE },
67         { "NT_STATUS_DEVICE_POWER_FAILURE", NT_STATUS_DEVICE_POWER_FAILURE },
68         { "NT_STATUS_VHD_SHARED", NT_STATUS_VHD_SHARED },
69         { "NT_STATUS_SMB_BAD_CLUSTER_DIALECT", NT_STATUS_SMB_BAD_CLUSTER_DIALECT },
70         { "NT_STATUS_NO_SUCH_JOB", NT_STATUS_NO_SUCH_JOB },
71
72         DOS_CODE(ERRDOS, ERRsuccess),
73         DOS_CODE(ERRDOS, ERRbadfunc),
74         DOS_CODE(ERRDOS, ERRbadfile),
75         DOS_CODE(ERRDOS, ERRbadpath),
76         DOS_CODE(ERRDOS, ERRnofids),
77         DOS_CODE(ERRDOS, ERRnoaccess),
78         DOS_CODE(ERRDOS, ERRbadfid),
79         DOS_CODE(ERRDOS, ERRbadmcb),
80         DOS_CODE(ERRDOS, ERRnomem),
81         DOS_CODE(ERRDOS, ERRbadmem),
82         DOS_CODE(ERRDOS, ERRbadenv),
83         DOS_CODE(ERRDOS, ERRbadaccess),
84         DOS_CODE(ERRDOS, ERRbaddata),
85         DOS_CODE(ERRDOS, ERRres),
86         DOS_CODE(ERRDOS, ERRbaddrive),
87         DOS_CODE(ERRDOS, ERRremcd),
88         DOS_CODE(ERRDOS, ERRdiffdevice),
89         DOS_CODE(ERRDOS, ERRnofiles),
90         DOS_CODE(ERRDOS, ERRgeneral),
91         DOS_CODE(ERRDOS, ERRbadshare),
92         DOS_CODE(ERRDOS, ERRlock),
93         DOS_CODE(ERRDOS, ERRunsup),
94         DOS_CODE(ERRDOS, ERRnetnamedel),
95         DOS_CODE(ERRDOS, ERRnosuchshare),
96         DOS_CODE(ERRDOS, ERRfilexists),
97         DOS_CODE(ERRDOS, ERRinvalidparam),
98         DOS_CODE(ERRDOS, ERRcannotopen),
99         DOS_CODE(ERRDOS, ERRinsufficientbuffer),
100         DOS_CODE(ERRDOS, ERRinvalidname),
101         DOS_CODE(ERRDOS, ERRunknownlevel),
102         DOS_CODE(ERRDOS, ERRnotlocked),
103         DOS_CODE(ERRDOS, ERRinvalidpath),
104         DOS_CODE(ERRDOS, ERRcancelviolation),
105         DOS_CODE(ERRDOS, ERRnoatomiclocks),
106         DOS_CODE(ERRDOS, ERRrename),
107         DOS_CODE(ERRDOS, ERRbadpipe),
108         DOS_CODE(ERRDOS, ERRpipebusy),
109         DOS_CODE(ERRDOS, ERRpipeclosing),
110         DOS_CODE(ERRDOS, ERRnotconnected),
111         DOS_CODE(ERRDOS, ERRmoredata),
112         DOS_CODE(ERRDOS, ERRnomoreitems),
113         DOS_CODE(ERRDOS, ERRbaddirectory),
114         DOS_CODE(ERRDOS, ERReasnotsupported),
115         DOS_CODE(ERRDOS, ERRlogonfailure),
116         DOS_CODE(ERRDOS, ERRbuftoosmall),
117         DOS_CODE(ERRDOS, ERRunknownipc),
118         DOS_CODE(ERRDOS, ERRnosuchprintjob),
119         DOS_CODE(ERRDOS, ERRinvgroup),
120         DOS_CODE(ERRDOS, ERRnoipc),
121         DOS_CODE(ERRDOS, ERRdriveralreadyinstalled),
122         DOS_CODE(ERRDOS, ERRunknownprinterport),
123         DOS_CODE(ERRDOS, ERRunknownprinterdriver),
124         DOS_CODE(ERRDOS, ERRunknownprintprocessor),
125         DOS_CODE(ERRDOS, ERRinvalidseparatorfile),
126         DOS_CODE(ERRDOS, ERRinvalidjobpriority),
127         DOS_CODE(ERRDOS, ERRinvalidprintername),
128         DOS_CODE(ERRDOS, ERRprinteralreadyexists),
129         DOS_CODE(ERRDOS, ERRinvalidprintercommand),
130         DOS_CODE(ERRDOS, ERRinvaliddatatype),
131         DOS_CODE(ERRDOS, ERRinvalidenvironment),
132         DOS_CODE(ERRDOS, ERRunknownprintmonitor),
133         DOS_CODE(ERRDOS, ERRprinterdriverinuse),
134         DOS_CODE(ERRDOS, ERRspoolfilenotfound),
135         DOS_CODE(ERRDOS, ERRnostartdoc),
136         DOS_CODE(ERRDOS, ERRnoaddjob),
137         DOS_CODE(ERRDOS, ERRprintprocessoralreadyinstalled),
138         DOS_CODE(ERRDOS, ERRprintmonitoralreadyinstalled),
139         DOS_CODE(ERRDOS, ERRinvalidprintmonitor),
140         DOS_CODE(ERRDOS, ERRprintmonitorinuse),
141         DOS_CODE(ERRDOS, ERRprinterhasjobsqueued),
142         DOS_CODE(ERRDOS, ERReainconsistent),
143
144         DOS_CODE(ERRSRV, ERRerror),
145         DOS_CODE(ERRSRV, ERRbadpw),
146         DOS_CODE(ERRSRV, ERRbadtype),
147         DOS_CODE(ERRSRV, ERRaccess),
148         DOS_CODE(ERRSRV, ERRinvnid),
149         DOS_CODE(ERRSRV, ERRinvnetname),
150         DOS_CODE(ERRSRV, ERRinvdevice),
151         DOS_CODE(ERRSRV, ERRqfull),
152         DOS_CODE(ERRSRV, ERRqtoobig),
153         DOS_CODE(ERRSRV, ERRinvpfid),
154         DOS_CODE(ERRSRV, ERRsmbcmd),
155         DOS_CODE(ERRSRV, ERRsrverror),
156         DOS_CODE(ERRSRV, ERRfilespecs),
157         DOS_CODE(ERRSRV, ERRbadlink),
158         DOS_CODE(ERRSRV, ERRbadpermits),
159         DOS_CODE(ERRSRV, ERRbadpid),
160         DOS_CODE(ERRSRV, ERRsetattrmode),
161         DOS_CODE(ERRSRV, ERRpaused),
162         DOS_CODE(ERRSRV, ERRmsgoff),
163         DOS_CODE(ERRSRV, ERRnoroom),
164         DOS_CODE(ERRSRV, ERRrmuns),
165         DOS_CODE(ERRSRV, ERRtimeout),
166         DOS_CODE(ERRSRV, ERRnoresource),
167         DOS_CODE(ERRSRV, ERRtoomanyuids),
168         DOS_CODE(ERRSRV, ERRbaduid),
169         DOS_CODE(ERRSRV, ERRuseMPX),
170         DOS_CODE(ERRSRV, ERRuseSTD),
171         DOS_CODE(ERRSRV, ERRcontMPX),
172         DOS_CODE(ERRSRV, ERRnosupport),
173         DOS_CODE(ERRSRV, ERRunknownsmb),
174
175         DOS_CODE(ERRHRD, ERRnowrite),
176         DOS_CODE(ERRHRD, ERRbadunit),
177         DOS_CODE(ERRHRD, ERRnotready),
178         DOS_CODE(ERRHRD, ERRbadcmd),
179         DOS_CODE(ERRHRD, ERRdata),
180         DOS_CODE(ERRHRD, ERRbadreq),
181         DOS_CODE(ERRHRD, ERRseek),
182         DOS_CODE(ERRHRD, ERRbadmedia),
183         DOS_CODE(ERRHRD, ERRbadsector),
184         DOS_CODE(ERRHRD, ERRnopaper),
185         DOS_CODE(ERRHRD, ERRwrite),
186         DOS_CODE(ERRHRD, ERRread),
187         DOS_CODE(ERRHRD, ERRgeneral),
188         DOS_CODE(ERRHRD, ERRwrongdisk),
189         DOS_CODE(ERRHRD, ERRFCBunavail),
190         DOS_CODE(ERRHRD, ERRsharebufexc),
191         DOS_CODE(ERRHRD, ERRdiskfull),
192
193         LDAP_CODE(LDAP_SUCCESS),
194         LDAP_CODE(LDAP_OPERATIONS_ERROR),
195         LDAP_CODE(LDAP_PROTOCOL_ERROR),
196         LDAP_CODE(LDAP_TIME_LIMIT_EXCEEDED),
197         LDAP_CODE(LDAP_SIZE_LIMIT_EXCEEDED),
198         LDAP_CODE(LDAP_COMPARE_FALSE),
199         LDAP_CODE(LDAP_COMPARE_TRUE),
200         LDAP_CODE(LDAP_AUTH_METHOD_NOT_SUPPORTED),
201         LDAP_CODE(LDAP_STRONG_AUTH_REQUIRED),
202         LDAP_CODE(LDAP_REFERRAL),
203         LDAP_CODE(LDAP_ADMIN_LIMIT_EXCEEDED),
204         LDAP_CODE(LDAP_UNAVAILABLE_CRITICAL_EXTENSION),
205         LDAP_CODE(LDAP_CONFIDENTIALITY_REQUIRED),
206         LDAP_CODE(LDAP_SASL_BIND_IN_PROGRESS),
207         LDAP_CODE(LDAP_NO_SUCH_ATTRIBUTE),
208         LDAP_CODE(LDAP_UNDEFINED_ATTRIBUTE_TYPE),
209         LDAP_CODE(LDAP_INAPPROPRIATE_MATCHING),
210         LDAP_CODE(LDAP_CONSTRAINT_VIOLATION),
211         LDAP_CODE(LDAP_ATTRIBUTE_OR_VALUE_EXISTS),
212         LDAP_CODE(LDAP_INVALID_ATTRIBUTE_SYNTAX),
213         LDAP_CODE(LDAP_NO_SUCH_OBJECT),
214         LDAP_CODE(LDAP_ALIAS_PROBLEM),
215         LDAP_CODE(LDAP_INVALID_DN_SYNTAX),
216         LDAP_CODE(LDAP_ALIAS_DEREFERENCING_PROBLEM),
217         LDAP_CODE(LDAP_INAPPROPRIATE_AUTHENTICATION),
218         LDAP_CODE(LDAP_INVALID_CREDENTIALS),
219         LDAP_CODE(LDAP_INSUFFICIENT_ACCESS_RIGHTS),
220         LDAP_CODE(LDAP_BUSY),
221         LDAP_CODE(LDAP_UNAVAILABLE),
222         LDAP_CODE(LDAP_UNWILLING_TO_PERFORM),
223         LDAP_CODE(LDAP_LOOP_DETECT),
224         LDAP_CODE(LDAP_NAMING_VIOLATION),
225         LDAP_CODE(LDAP_OBJECT_CLASS_VIOLATION),
226         LDAP_CODE(LDAP_NOT_ALLOWED_ON_NON_LEAF),
227         LDAP_CODE(LDAP_NOT_ALLOWED_ON_RDN),
228         LDAP_CODE(LDAP_ENTRY_ALREADY_EXISTS),
229         LDAP_CODE(LDAP_OBJECT_CLASS_MODS_PROHIBITED),
230         LDAP_CODE(LDAP_AFFECTS_MULTIPLE_DSAS),
231         LDAP_CODE(LDAP_OTHER),
232
233         { NULL, NT_STATUS(0) }
234 };
235
236 /*****************************************************************************
237  Returns an NT_STATUS constant as a string for inclusion in autogen C code.
238  *****************************************************************************/
239
240 const char *get_nt_error_c_code(TALLOC_CTX *mem_ctx, NTSTATUS nt_code)
241 {
242         char *result;
243         int idx = 0;
244
245         while (special_errs[idx].nt_errstr != NULL) {
246                 if (NT_STATUS_V(special_errs[idx].nt_errcode) ==
247                     NT_STATUS_V(nt_code)) {
248                         result = talloc_strdup(mem_ctx, special_errs[idx].nt_errstr);
249                         return result;
250                 }
251                 idx++;
252         }
253
254         idx = 0;
255
256         while (nt_errs[idx].nt_errstr != NULL) {
257                 if (NT_STATUS_V(nt_errs[idx].nt_errcode) ==
258                     NT_STATUS_V(nt_code)) {
259                         result = talloc_strdup(mem_ctx, nt_errs[idx].nt_errstr);
260                         return result;
261                 }
262                 idx++;
263         }
264
265         result = talloc_asprintf(mem_ctx, "NT_STATUS(0x%08x)",
266                                  NT_STATUS_V(nt_code));
267         return result;
268 }
269
270 /*****************************************************************************
271  Returns the NT_STATUS constant matching the string supplied (as an NTSTATUS)
272  *****************************************************************************/
273
274 NTSTATUS nt_status_string_to_code(const char *nt_status_str)
275 {
276         int idx = 0;
277
278         while (special_errs[idx].nt_errstr != NULL) {
279                 if (strcasecmp(special_errs[idx].nt_errstr, nt_status_str) == 0) {
280                         return special_errs[idx].nt_errcode;
281                 }
282                 idx++;
283         }
284
285         idx = 0;
286
287         while (nt_errs[idx].nt_errstr != NULL) {
288                 if (strcasecmp(nt_errs[idx].nt_errstr, nt_status_str) == 0) {
289                         return nt_errs[idx].nt_errcode;
290                 }
291                 idx++;
292         }
293
294         return NT_STATUS_UNSUCCESSFUL;
295 }
296
297 /**
298  * Squash an NT_STATUS in line with security requirements.
299  * In an attempt to avoid giving the whole game away when users
300  * are authenticating, NT replaces both NT_STATUS_NO_SUCH_USER and
301  * NT_STATUS_WRONG_PASSWORD with NT_STATUS_LOGON_FAILURE in certain situations
302  * (session setups in particular).
303  *
304  * @param nt_status NTSTATUS input for squashing.
305  * @return the 'squashed' nt_status
306  **/
307
308 NTSTATUS nt_status_squash(NTSTATUS nt_status)
309 {
310         if NT_STATUS_IS_OK(nt_status) {
311                 return nt_status;
312         } else if NT_STATUS_EQUAL(nt_status, NT_STATUS_NO_SUCH_USER) {
313                 /* Match WinXP and don't give the game away */
314                 return NT_STATUS_LOGON_FAILURE;
315
316         } else if NT_STATUS_EQUAL(nt_status, NT_STATUS_WRONG_PASSWORD) {
317                 /* Match WinXP and don't give the game away */
318                 return NT_STATUS_LOGON_FAILURE;
319         } else {
320                 return nt_status;
321         }
322 }
323
324 /*****************************************************************************
325  Returns an NT error message.  not amazingly helpful, but better than a number.
326  *****************************************************************************/
327
328 const char *nt_errstr(NTSTATUS nt_code)
329 {
330         static char msg[20];
331         int idx = 0;
332
333         while (special_errs[idx].nt_errstr != NULL) {
334                 if (NT_STATUS_V(special_errs[idx].nt_errcode) ==
335                     NT_STATUS_V(nt_code)) {
336                         return special_errs[idx].nt_errstr;
337                 }
338                 idx++;
339         }
340
341         idx = 0;
342
343         while (nt_errs[idx].nt_errstr != NULL) {
344                 if (NT_STATUS_V(nt_errs[idx].nt_errcode) ==
345                     NT_STATUS_V(nt_code)) {
346                         return nt_errs[idx].nt_errstr;
347                 }
348                 idx++;
349         }
350
351         /*
352          * This should not really happen, we should have all error codes
353          * available. We have a problem that this might get wrongly
354          * overwritten by later calls in the same DEBUG statement.
355          */
356
357         snprintf(msg, sizeof(msg), "NT code 0x%08x", NT_STATUS_V(nt_code));
358         return msg;
359 }
360
361 /************************************************************************
362  Print friendler version fo NT error code
363  ***********************************************************************/
364
365 const char *get_friendly_nt_error_msg(NTSTATUS nt_code)
366 {
367         int idx = 0;
368
369         while (nt_err_desc[idx].nt_errstr != NULL) {
370                 if (NT_STATUS_V(nt_err_desc[idx].nt_errcode) == NT_STATUS_V(nt_code)) {
371                         return nt_err_desc[idx].nt_errstr;
372                 }
373                 idx++;
374         }
375
376         /* fall back to NT_STATUS_XXX string */
377
378         return nt_errstr(nt_code);
379 }