r10656: BIG merge from trunk. Features not copied over
[tprouty/samba.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 2 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, write to the Free Software
18    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 */
20
21 #include "includes.h"
22
23 /* From lib/error.c */
24 extern struct unix_error_map unix_dos_nt_errmap[];
25
26 extern uint32 global_client_caps;
27 /* these can be set by some functions to override the error codes */
28 static int override_ERR_class;
29 static int override_ERR_code;
30 static NTSTATUS override_ERR_ntstatus;
31
32 /****************************************************************************
33  Setting eclass and ecode only and status to NT_STATUS_INVALID forces DOS errors.
34  Setting status only and eclass and ecode to -1 forces NT errors.
35 ****************************************************************************/
36  
37 void set_saved_error_triple(int eclass, int ecode, NTSTATUS status)
38 {
39         override_ERR_class = eclass;
40         override_ERR_code = ecode;
41         override_ERR_ntstatus = status;
42 }
43
44 void set_saved_ntstatus(NTSTATUS status)
45 {
46         uint8 tmp_eclass;       /* Hmmm. override_ERR_class is not uint8... */
47         override_ERR_ntstatus = status;
48         ntstatus_to_dos(status, &tmp_eclass, &override_ERR_code);
49         override_ERR_class = tmp_eclass;
50         
51 }
52
53 /****************************************************************************
54  Return the current settings of the error triple. Return True if any are set.
55 ****************************************************************************/
56
57 NTSTATUS get_saved_ntstatus(void)
58 {
59         return override_ERR_ntstatus;
60 }
61
62 /****************************************************************************
63  Create an error packet from a cached error.
64 ****************************************************************************/
65  
66 int cached_error_packet(char *outbuf,files_struct *fsp,int line,const char *file)
67 {
68         write_bmpx_struct *wbmpx = fsp->wbmpx_ptr;
69         int32 eclass = wbmpx->wr_errclass;
70         int32 err = wbmpx->wr_error;
71         NTSTATUS ntstatus = wbmpx->wr_status;
72  
73         /* We can now delete the auxiliary struct */
74         SAFE_FREE(fsp->wbmpx_ptr);
75         return error_packet(outbuf,eclass,err,ntstatus,line,file);
76 }
77
78 /****************************************************************************
79  Create an error packet from errno.
80 ****************************************************************************/
81
82 int unix_error_packet(char *outbuf,int def_class,uint32 def_code, NTSTATUS def_status, int line, const char *file)
83 {
84         int eclass=def_class;
85         int ecode=def_code;
86         NTSTATUS ntstatus = def_status;
87         int i=0;
88
89         if (errno != 0) {
90                 DEBUG(3,("unix_error_packet: error string = %s\n",strerror(errno)));
91   
92                 while (unix_dos_nt_errmap[i].dos_class != 0) {
93                         if (unix_dos_nt_errmap[i].unix_error == errno) {
94                                 eclass = unix_dos_nt_errmap[i].dos_class;
95                                 ecode = unix_dos_nt_errmap[i].dos_code;
96                                 ntstatus = unix_dos_nt_errmap[i].nt_error;
97                                 break;
98                         }
99                         i++;
100                 }
101         }
102
103         return error_packet(outbuf,eclass,ecode,ntstatus,line,file);
104 }
105
106
107 /****************************************************************************
108  Create an error packet. Normally called using the ERROR() macro.
109  Setting eclass and ecode only and status to NT_STATUS_OK forces DOS errors.
110  Setting status only and eclass and ecode to zero forces NT errors.
111  If the override errors are set they take precedence over any passed in values.
112 ****************************************************************************/
113
114 int error_packet(char *outbuf, uint8 eclass, uint32 ecode, NTSTATUS ntstatus, int line, const char *file)
115 {
116         int outsize = set_message(outbuf,0,0,True);
117         BOOL force_nt_status = False;
118         BOOL force_dos_status = False;
119
120         if (override_ERR_class != SMB_SUCCESS || !NT_STATUS_IS_OK(override_ERR_ntstatus)) {
121                 eclass = override_ERR_class;
122                 ecode = override_ERR_code;
123                 ntstatus = override_ERR_ntstatus;
124                 override_ERR_class = SMB_SUCCESS;
125                 override_ERR_code = 0;
126                 override_ERR_ntstatus = NT_STATUS_OK;
127         }
128
129         if (eclass == (uint8)-1) {
130                 force_nt_status = True;
131         } else if (NT_STATUS_IS_INVALID(ntstatus)) {
132                 force_dos_status = True;
133         }
134
135         if (force_nt_status || (!force_dos_status && lp_nt_status_support() && (global_client_caps & CAP_STATUS32))) {
136                 /* We're returning an NT error. */
137                 if (NT_STATUS_V(ntstatus) == 0 && eclass) {
138                         ntstatus = dos_to_ntstatus(eclass, ecode);
139                 }
140                 SIVAL(outbuf,smb_rcls,NT_STATUS_V(ntstatus));
141                 SSVAL(outbuf,smb_flg2, SVAL(outbuf,smb_flg2)|FLAGS2_32_BIT_ERROR_CODES);
142                 DEBUG(3,("error packet at %s(%d) cmd=%d (%s) %s\n",
143                          file, line,
144                          (int)CVAL(outbuf,smb_com),
145                          smb_fn_name(CVAL(outbuf,smb_com)),
146                          nt_errstr(ntstatus)));
147         } else {
148                 /* We're returning a DOS error only. */
149                 if (eclass == 0 && NT_STATUS_V(ntstatus)) {
150                         ntstatus_to_dos(ntstatus, &eclass, &ecode);
151                 }
152
153                 SSVAL(outbuf,smb_flg2, SVAL(outbuf,smb_flg2)&~FLAGS2_32_BIT_ERROR_CODES);
154                 SSVAL(outbuf,smb_rcls,eclass);
155                 SSVAL(outbuf,smb_err,ecode);  
156
157                 DEBUG(3,("error packet at %s(%d) cmd=%d (%s) eclass=%d ecode=%d\n",
158                           file, line,
159                           (int)CVAL(outbuf,smb_com),
160                           smb_fn_name(CVAL(outbuf,smb_com)),
161                           eclass,
162                           ecode));
163         }
164
165         return outsize;
166 }