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