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