Correct fix for excel read-only bug. Add panic for logic error in developer mode.
[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 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 /* these can be set by some functions to override the error codes */
24 int unix_ERR_class=SMB_SUCCESS;
25 int unix_ERR_code=0;
26 NTSTATUS unix_ERR_ntstatus = NT_STATUS_OK;
27
28 /* From lib/error.c */
29 extern struct unix_error_map unix_dos_nt_errmap[];
30
31 /****************************************************************************
32  Create an error packet from a cached error.
33 ****************************************************************************/
34  
35 int cached_error_packet(char *outbuf,files_struct *fsp,int line,const char *file)
36 {
37         write_bmpx_struct *wbmpx = fsp->wbmpx_ptr;
38  
39         int32 eclass = wbmpx->wr_errclass;
40         int32 err = wbmpx->wr_error;
41  
42         /* We can now delete the auxiliary struct */
43         free((char *)wbmpx);
44         fsp->wbmpx_ptr = NULL;
45         return error_packet(outbuf,NT_STATUS_OK,eclass,err,line,file);
46 }
47
48 /****************************************************************************
49  Create an error packet from errno.
50 ****************************************************************************/
51
52 int unix_error_packet(char *outbuf,int def_class,uint32 def_code,
53                       int line, const char *file)
54 {
55         int eclass=def_class;
56         int ecode=def_code;
57         NTSTATUS ntstatus = NT_STATUS_OK;
58         int i=0;
59
60         if (unix_ERR_class != SMB_SUCCESS) {
61                 eclass = unix_ERR_class;
62                 ecode = unix_ERR_code;
63                 ntstatus = unix_ERR_ntstatus;
64                 unix_ERR_class = SMB_SUCCESS;
65                 unix_ERR_code = 0;
66                 unix_ERR_ntstatus = NT_STATUS_OK;
67         } else {
68                 while (unix_dos_nt_errmap[i].dos_class != 0) {
69                         if (unix_dos_nt_errmap[i].unix_error == errno) {
70                                 eclass = unix_dos_nt_errmap[i].dos_class;
71                                 ecode = unix_dos_nt_errmap[i].dos_code;
72                                 ntstatus = unix_dos_nt_errmap[i].nt_error;
73                                 break;
74                         }
75                         i++;
76                 }
77         }
78
79         return error_packet(outbuf,ntstatus,eclass,ecode,line,file);
80 }
81
82
83 /****************************************************************************
84  Create an error packet. Normally called using the ERROR() macro.
85 ****************************************************************************/
86
87 int error_packet(char *outbuf,NTSTATUS ntstatus,
88                  uint8 eclass,uint32 ecode,int line, const char *file)
89 {
90         int outsize = set_message(outbuf,0,0,True);
91         extern uint32 global_client_caps;
92
93         if (errno != 0)
94                 DEBUG(3,("error string = %s\n",strerror(errno)));
95   
96 #if defined(DEVELOPER)
97         if (unix_ERR_class != SMB_SUCCESS || unix_ERR_code != 0 || !NT_STATUS_IS_OK(unix_ERR_ntstatus))
98                 smb_panic("logic error in error processing");
99 #endif
100
101         /*
102          * We can explicitly force 32 bit error codes even when the
103          * parameter "nt status" is set to no by pre-setting the
104          * FLAGS2_32_BIT_ERROR_CODES bit in the smb_flg2 outbuf.
105          * This is to allow work arounds for client bugs that are needed
106          * when talking with clients that normally expect nt status codes. JRA.
107          */
108
109         if ((lp_nt_status_support() || (SVAL(outbuf,smb_flg2) & FLAGS2_32_BIT_ERROR_CODES)) && (global_client_caps & CAP_STATUS32)) {
110                 if (NT_STATUS_V(ntstatus) == 0 && eclass)
111                         ntstatus = dos_to_ntstatus(eclass, ecode);
112                 SIVAL(outbuf,smb_rcls,NT_STATUS_V(ntstatus));
113                 SSVAL(outbuf,smb_flg2, SVAL(outbuf,smb_flg2)|FLAGS2_32_BIT_ERROR_CODES);
114                 DEBUG(3,("error packet at %s(%d) cmd=%d (%s) %s\n",
115                          file, line,
116                          (int)CVAL(outbuf,smb_com),
117                          smb_fn_name(CVAL(outbuf,smb_com)),
118                          nt_errstr(ntstatus)));
119                 return outsize;
120         } 
121
122         if (eclass == 0 && NT_STATUS_V(ntstatus))
123                 ntstatus_to_dos(ntstatus, &eclass, &ecode);
124
125         SSVAL(outbuf,smb_flg2, SVAL(outbuf,smb_flg2)&~FLAGS2_32_BIT_ERROR_CODES);
126         SSVAL(outbuf,smb_rcls,eclass);
127         SSVAL(outbuf,smb_err,ecode);  
128
129         DEBUG(3,("error packet at %s(%d) cmd=%d (%s) eclass=%d ecode=%d\n",
130                   file, line,
131                   (int)CVAL(outbuf,smb_com),
132                   smb_fn_name(CVAL(outbuf,smb_com)),
133                   eclass,
134                   ecode));
135
136         return outsize;
137 }