r24311: add a reply_force_nterror() macro
[abartlet/samba.git/.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 /****************************************************************************
28  Create an error packet from a cached error.
29 ****************************************************************************/
30  
31 int cached_error_packet(const char *inbuf,char *outbuf,files_struct *fsp,int line,const char *file)
32 {
33         write_bmpx_struct *wbmpx = fsp->wbmpx_ptr;
34         int32 eclass = wbmpx->wr_errclass;
35         int32 err = wbmpx->wr_error;
36         NTSTATUS ntstatus = wbmpx->wr_status;
37  
38         /* We can now delete the auxiliary struct */
39         SAFE_FREE(fsp->wbmpx_ptr);
40         return error_packet(inbuf,outbuf,eclass,err,ntstatus,line,file);
41 }
42
43 /****************************************************************************
44  Create an error packet from errno.
45 ****************************************************************************/
46
47 int unix_error_packet(const char *inbuf,char *outbuf,int def_class,uint32 def_code, NTSTATUS def_status, int line, const char *file)
48 {
49         int eclass=def_class;
50         int ecode=def_code;
51         NTSTATUS ntstatus = def_status;
52         int i=0;
53
54         if (errno != 0) {
55                 DEBUG(3,("unix_error_packet: error string = %s\n",strerror(errno)));
56   
57                 while (unix_dos_nt_errmap[i].dos_class != 0) {
58                         if (unix_dos_nt_errmap[i].unix_error == errno) {
59                                 eclass = unix_dos_nt_errmap[i].dos_class;
60                                 ecode = unix_dos_nt_errmap[i].dos_code;
61                                 ntstatus = unix_dos_nt_errmap[i].nt_error;
62                                 break;
63                         }
64                         i++;
65                 }
66         }
67
68         return error_packet(inbuf,outbuf,eclass,ecode,ntstatus,line,file);
69 }
70
71 BOOL use_nt_status(void)
72 {
73         return lp_nt_status_support() && (global_client_caps & CAP_STATUS32);
74 }
75
76 /****************************************************************************
77  Create an error packet. Normally called using the ERROR() macro.
78  Setting eclass and ecode only and status to NT_STATUS_OK forces DOS errors.
79  Setting status only and eclass and ecode to zero forces NT errors.
80  If the override errors are set they take precedence over any passed in values.
81 ****************************************************************************/
82
83 void error_packet_set(char *outbuf, uint8 eclass, uint32 ecode, NTSTATUS ntstatus, int line, const char *file)
84 {
85         BOOL force_nt_status = False;
86         BOOL force_dos_status = False;
87
88         if (eclass == (uint8)-1) {
89                 force_nt_status = True;
90         } else if (NT_STATUS_IS_DOS(ntstatus)) {
91                 force_dos_status = True;
92         }
93
94         if (force_nt_status || (!force_dos_status && lp_nt_status_support() && (global_client_caps & CAP_STATUS32))) {
95                 /* We're returning an NT error. */
96                 if (NT_STATUS_V(ntstatus) == 0 && eclass) {
97                         ntstatus = dos_to_ntstatus(eclass, ecode);
98                 }
99                 SIVAL(outbuf,smb_rcls,NT_STATUS_V(ntstatus));
100                 SSVAL(outbuf,smb_flg2, SVAL(outbuf,smb_flg2)|FLAGS2_32_BIT_ERROR_CODES);
101                 DEBUG(3,("error packet at %s(%d) cmd=%d (%s) %s\n",
102                          file, line,
103                          (int)CVAL(outbuf,smb_com),
104                          smb_fn_name(CVAL(outbuf,smb_com)),
105                          nt_errstr(ntstatus)));
106         } else {
107                 /* We're returning a DOS error only. */
108                 if (NT_STATUS_IS_DOS(ntstatus)) {
109                         eclass = NT_STATUS_DOS_CLASS(ntstatus);
110                         ecode = NT_STATUS_DOS_CODE(ntstatus);
111                 } else  if (eclass == 0 && NT_STATUS_V(ntstatus)) {
112                         ntstatus_to_dos(ntstatus, &eclass, &ecode);
113                 }
114
115                 SSVAL(outbuf,smb_flg2, SVAL(outbuf,smb_flg2)&~FLAGS2_32_BIT_ERROR_CODES);
116                 SSVAL(outbuf,smb_rcls,eclass);
117                 SSVAL(outbuf,smb_err,ecode);  
118
119                 DEBUG(3,("error packet at %s(%d) cmd=%d (%s) eclass=%d ecode=%d\n",
120                           file, line,
121                           (int)CVAL(outbuf,smb_com),
122                           smb_fn_name(CVAL(outbuf,smb_com)),
123                           eclass,
124                           ecode));
125         }
126 }
127
128 int error_packet(const char *inbuf, char *outbuf, uint8 eclass, uint32 ecode, NTSTATUS ntstatus, int line, const char *file)
129 {
130         int outsize = set_message(inbuf,outbuf,0,0,True);
131         error_packet_set(outbuf, eclass, ecode, ntstatus, line, file);
132         return outsize;
133 }
134
135 void reply_nt_error(struct smb_request *req, NTSTATUS ntstatus,
136                     int line, const char *file)
137 {
138         TALLOC_FREE(req->outbuf);
139         reply_outbuf(req, 0, 0);
140         error_packet_set((char *)req->outbuf, 0, 0, ntstatus, line, file);
141 }
142
143 void reply_force_nt_error(struct smb_request *req, NTSTATUS ntstatus,
144                           int line, const char *file)
145 {
146         TALLOC_FREE(req->outbuf);
147         reply_outbuf(req, 0, 0);
148         error_packet_set((char *)req->outbuf, -1, -1, ntstatus, line, file);
149 }
150
151 void reply_dos_error(struct smb_request *req, uint8 eclass, uint32 ecode,
152                     int line, const char *file)
153 {
154         TALLOC_FREE(req->outbuf);
155         reply_outbuf(req, 0, 0);
156         error_packet_set((char *)req->outbuf, eclass, ecode, NT_STATUS_OK, line,
157                          file);
158 }
159
160 void reply_both_error(struct smb_request *req, uint8 eclass, uint32 ecode,
161                       NTSTATUS status, int line, const char *file)
162 {
163         TALLOC_FREE(req->outbuf);
164         reply_outbuf(req, 0, 0);
165         error_packet_set((char *)req->outbuf, eclass, ecode, status,
166                          line, file);
167 }
168
169 void reply_unix_error(struct smb_request *req, uint8 defclass, uint32 defcode,
170                       NTSTATUS defstatus, int line, const char *file)
171 {
172         int eclass=defclass;
173         int ecode=defcode;
174         NTSTATUS ntstatus = defstatus;
175         int i=0;
176
177         TALLOC_FREE(req->outbuf);
178         reply_outbuf(req, 0, 0);
179
180         if (errno != 0) {
181                 DEBUG(3,("unix_error_packet: error string = %s\n",
182                          strerror(errno)));
183
184                 while (unix_dos_nt_errmap[i].dos_class != 0) {
185                         if (unix_dos_nt_errmap[i].unix_error == errno) {
186                                 eclass = unix_dos_nt_errmap[i].dos_class;
187                                 ecode = unix_dos_nt_errmap[i].dos_code;
188                                 ntstatus = unix_dos_nt_errmap[i].nt_error;
189                                 break;
190                         }
191                         i++;
192                 }
193         }
194
195         error_packet_set((char *)req->outbuf, eclass, ecode, ntstatus,
196                          line, file);
197 }