Merge branch 'master' of ssh://git.samba.org/data/git/samba into crypt
[samba.git] / source3 / smbd / error.c
1 /* 
2    Unix SMB/CIFS implementation.
3    error packet handling
4    Copyright (C) Andrew Tridgell 1992-1998
5    
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 3 of the License, or
9    (at your option) any later version.
10    
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15    
16    You should have received a copy of the GNU General Public License
17    along with this program.  If not, see <http://www.gnu.org/licenses/>.
18 */
19
20 #include "includes.h"
21
22 /* From lib/error.c */
23 extern struct unix_error_map unix_dos_nt_errmap[];
24
25 extern uint32 global_client_caps;
26
27 bool use_nt_status(void)
28 {
29         return lp_nt_status_support() && (global_client_caps & CAP_STATUS32);
30 }
31
32 /****************************************************************************
33  Create an error packet. Normally called using the ERROR() macro.
34  Setting eclass and ecode only and status to NT_STATUS_OK forces DOS errors.
35  Setting status only and eclass and ecode to zero forces NT errors.
36  If the override errors are set they take precedence over any passed in values.
37 ****************************************************************************/
38
39 void error_packet_set(char *outbuf, uint8 eclass, uint32 ecode, NTSTATUS ntstatus, int line, const char *file)
40 {
41         bool force_nt_status = False;
42         bool force_dos_status = False;
43
44         if (eclass == (uint8)-1) {
45                 force_nt_status = True;
46         } else if (NT_STATUS_IS_DOS(ntstatus)) {
47                 force_dos_status = True;
48         }
49
50         if (force_nt_status || (!force_dos_status && lp_nt_status_support() && (global_client_caps & CAP_STATUS32))) {
51                 /* We're returning an NT error. */
52                 if (NT_STATUS_V(ntstatus) == 0 && eclass) {
53                         ntstatus = dos_to_ntstatus(eclass, ecode);
54                 }
55                 SIVAL(outbuf,smb_rcls,NT_STATUS_V(ntstatus));
56                 SSVAL(outbuf,smb_flg2, SVAL(outbuf,smb_flg2)|FLAGS2_32_BIT_ERROR_CODES);
57                 DEBUG(3,("error packet at %s(%d) cmd=%d (%s) %s\n",
58                          file, line,
59                          (int)CVAL(outbuf,smb_com),
60                          smb_fn_name(CVAL(outbuf,smb_com)),
61                          nt_errstr(ntstatus)));
62         } else {
63                 /* We're returning a DOS error only. */
64                 if (NT_STATUS_IS_DOS(ntstatus)) {
65                         eclass = NT_STATUS_DOS_CLASS(ntstatus);
66                         ecode = NT_STATUS_DOS_CODE(ntstatus);
67                 } else  if (eclass == 0 && NT_STATUS_V(ntstatus)) {
68                         ntstatus_to_dos(ntstatus, &eclass, &ecode);
69                 }
70
71                 SSVAL(outbuf,smb_flg2, SVAL(outbuf,smb_flg2)&~FLAGS2_32_BIT_ERROR_CODES);
72                 SSVAL(outbuf,smb_rcls,eclass);
73                 SSVAL(outbuf,smb_err,ecode);  
74
75                 DEBUG(3,("error packet at %s(%d) cmd=%d (%s) eclass=%d ecode=%d\n",
76                           file, line,
77                           (int)CVAL(outbuf,smb_com),
78                           smb_fn_name(CVAL(outbuf,smb_com)),
79                           eclass,
80                           ecode));
81         }
82 }
83
84 int error_packet(char *outbuf, uint8 eclass, uint32 ecode, NTSTATUS ntstatus, int line, const char *file)
85 {
86         int outsize = srv_set_message(outbuf,0,0,True);
87         error_packet_set(outbuf, eclass, ecode, ntstatus, line, file);
88         return outsize;
89 }
90
91 void reply_nt_error(struct smb_request *req, NTSTATUS ntstatus,
92                     int line, const char *file)
93 {
94         TALLOC_FREE(req->outbuf);
95         reply_outbuf(req, 0, 0);
96         error_packet_set((char *)req->outbuf, 0, 0, ntstatus, line, file);
97 }
98
99 void reply_force_nt_error(struct smb_request *req, NTSTATUS ntstatus,
100                           int line, const char *file)
101 {
102         TALLOC_FREE(req->outbuf);
103         reply_outbuf(req, 0, 0);
104         error_packet_set((char *)req->outbuf, -1, -1, ntstatus, line, file);
105 }
106
107 void reply_dos_error(struct smb_request *req, uint8 eclass, uint32 ecode,
108                     int line, const char *file)
109 {
110         TALLOC_FREE(req->outbuf);
111         reply_outbuf(req, 0, 0);
112         error_packet_set((char *)req->outbuf, eclass, ecode, NT_STATUS_OK, line,
113                          file);
114 }
115
116 void reply_both_error(struct smb_request *req, uint8 eclass, uint32 ecode,
117                       NTSTATUS status, int line, const char *file)
118 {
119         TALLOC_FREE(req->outbuf);
120         reply_outbuf(req, 0, 0);
121         error_packet_set((char *)req->outbuf, eclass, ecode, status,
122                          line, file);
123 }
124
125 void reply_openerror(struct smb_request *req, NTSTATUS status)
126 {
127         if (NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_COLLISION)) {
128                 /*
129                  * We hit an existing file, and if we're returning DOS
130                  * error codes OBJECT_NAME_COLLISION would map to
131                  * ERRDOS/183, we need to return ERRDOS/80, see bug
132                  * 4852.
133                  */
134                 reply_botherror(req, NT_STATUS_OBJECT_NAME_COLLISION,
135                         ERRDOS, ERRfilexists);
136         } else {
137                 reply_nterror(req, status);
138         }
139 }
140
141 void reply_unix_error(struct smb_request *req, uint8 defclass, uint32 defcode,
142                         NTSTATUS defstatus, int line, const char *file)
143 {
144         int eclass=defclass;
145         int ecode=defcode;
146         NTSTATUS ntstatus = defstatus;
147         int i=0;
148
149         TALLOC_FREE(req->outbuf);
150         reply_outbuf(req, 0, 0);
151
152         if (errno != 0) {
153                 DEBUG(3,("unix_error_packet: error string = %s\n",
154                         strerror(errno)));
155
156                 while (unix_dos_nt_errmap[i].dos_class != 0) {
157                         if (unix_dos_nt_errmap[i].unix_error == errno) {
158                                 eclass = unix_dos_nt_errmap[i].dos_class;
159                                 ecode = unix_dos_nt_errmap[i].dos_code;
160                                 ntstatus = unix_dos_nt_errmap[i].nt_error;
161                                 break;
162                         }
163                         i++;
164                 }
165         }
166
167         error_packet_set((char *)req->outbuf, eclass, ecode, ntstatus,
168                 line, file);
169 }