r23204: Add MSG_SMB_BRL_VALIDATE. Tridge, this is a bit different from your bzr
[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
28 /****************************************************************************
29  Create an error packet from a cached error.
30 ****************************************************************************/
31  
32 int cached_error_packet(const char *inbuf,char *outbuf,files_struct *fsp,int line,const char *file)
33 {
34         write_bmpx_struct *wbmpx = fsp->wbmpx_ptr;
35         int32 eclass = wbmpx->wr_errclass;
36         int32 err = wbmpx->wr_error;
37         NTSTATUS ntstatus = wbmpx->wr_status;
38  
39         /* We can now delete the auxiliary struct */
40         SAFE_FREE(fsp->wbmpx_ptr);
41         return error_packet(inbuf,outbuf,eclass,err,ntstatus,line,file);
42 }
43
44 /****************************************************************************
45  Create an error packet from errno.
46 ****************************************************************************/
47
48 int unix_error_packet(const char *inbuf,char *outbuf,int def_class,uint32 def_code, NTSTATUS def_status, int line, const char *file)
49 {
50         int eclass=def_class;
51         int ecode=def_code;
52         NTSTATUS ntstatus = def_status;
53         int i=0;
54
55         if (errno != 0) {
56                 DEBUG(3,("unix_error_packet: error string = %s\n",strerror(errno)));
57   
58                 while (unix_dos_nt_errmap[i].dos_class != 0) {
59                         if (unix_dos_nt_errmap[i].unix_error == errno) {
60                                 eclass = unix_dos_nt_errmap[i].dos_class;
61                                 ecode = unix_dos_nt_errmap[i].dos_code;
62                                 ntstatus = unix_dos_nt_errmap[i].nt_error;
63                                 break;
64                         }
65                         i++;
66                 }
67         }
68
69         return error_packet(inbuf,outbuf,eclass,ecode,ntstatus,line,file);
70 }
71
72 BOOL use_nt_status(void)
73 {
74         return lp_nt_status_support() && (global_client_caps & CAP_STATUS32);
75 }
76
77 /****************************************************************************
78  Create an error packet. Normally called using the ERROR() macro.
79  Setting eclass and ecode only and status to NT_STATUS_OK forces DOS errors.
80  Setting status only and eclass and ecode to zero forces NT errors.
81  If the override errors are set they take precedence over any passed in values.
82 ****************************************************************************/
83
84 void error_packet_set(char *outbuf, uint8 eclass, uint32 ecode, NTSTATUS ntstatus, int line, const char *file)
85 {
86         BOOL force_nt_status = False;
87         BOOL force_dos_status = False;
88
89         if (eclass == (uint8)-1) {
90                 force_nt_status = True;
91         } else if (NT_STATUS_IS_DOS(ntstatus)) {
92                 force_dos_status = True;
93         }
94
95         if (force_nt_status || (!force_dos_status && lp_nt_status_support() && (global_client_caps & CAP_STATUS32))) {
96                 /* We're returning an NT error. */
97                 if (NT_STATUS_V(ntstatus) == 0 && eclass) {
98                         ntstatus = dos_to_ntstatus(eclass, ecode);
99                 }
100                 SIVAL(outbuf,smb_rcls,NT_STATUS_V(ntstatus));
101                 SSVAL(outbuf,smb_flg2, SVAL(outbuf,smb_flg2)|FLAGS2_32_BIT_ERROR_CODES);
102                 DEBUG(3,("error packet at %s(%d) cmd=%d (%s) %s\n",
103                          file, line,
104                          (int)CVAL(outbuf,smb_com),
105                          smb_fn_name(CVAL(outbuf,smb_com)),
106                          nt_errstr(ntstatus)));
107         } else {
108                 /* We're returning a DOS error only. */
109                 if (NT_STATUS_IS_DOS(ntstatus)) {
110                         eclass = NT_STATUS_DOS_CLASS(ntstatus);
111                         ecode = NT_STATUS_DOS_CODE(ntstatus);
112                 } else  if (eclass == 0 && NT_STATUS_V(ntstatus)) {
113                         ntstatus_to_dos(ntstatus, &eclass, &ecode);
114                 }
115
116                 SSVAL(outbuf,smb_flg2, SVAL(outbuf,smb_flg2)&~FLAGS2_32_BIT_ERROR_CODES);
117                 SSVAL(outbuf,smb_rcls,eclass);
118                 SSVAL(outbuf,smb_err,ecode);  
119
120                 DEBUG(3,("error packet at %s(%d) cmd=%d (%s) eclass=%d ecode=%d\n",
121                           file, line,
122                           (int)CVAL(outbuf,smb_com),
123                           smb_fn_name(CVAL(outbuf,smb_com)),
124                           eclass,
125                           ecode));
126         }
127 }
128
129 int error_packet(const char *inbuf, char *outbuf, uint8 eclass, uint32 ecode, NTSTATUS ntstatus, int line, const char *file)
130 {
131         int outsize = set_message(inbuf,outbuf,0,0,True);
132         error_packet_set(outbuf, eclass, ecode, ntstatus, line, file);
133         return outsize;
134 }