r1383: sync from 3.0 tree
[Samba.git] / source / smbd / error.c
blobd611e0ef873af4812b3b52439612d8d975720761
1 /*
2 Unix SMB/CIFS implementation.
3 error packet handling
4 Copyright (C) Andrew Tridgell 1992-1998
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.
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.
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.
21 #include "includes.h"
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;
28 /* From lib/error.c */
29 extern struct unix_error_map unix_dos_nt_errmap[];
31 /****************************************************************************
32 Ensure we don't have any errors cached.
33 ****************************************************************************/
35 void clear_cached_errors(void)
37 unix_ERR_class = SMB_SUCCESS;
38 unix_ERR_code = 0;
39 unix_ERR_ntstatus = NT_STATUS_OK;
42 /****************************************************************************
43 Create an error packet from a cached error.
44 ****************************************************************************/
46 int cached_error_packet(char *outbuf,files_struct *fsp,int line,const char *file)
48 write_bmpx_struct *wbmpx = fsp->wbmpx_ptr;
50 int32 eclass = wbmpx->wr_errclass;
51 int32 err = wbmpx->wr_error;
53 /* We can now delete the auxiliary struct */
54 free((char *)wbmpx);
55 fsp->wbmpx_ptr = NULL;
56 return error_packet(outbuf,NT_STATUS_OK,eclass,err,False,line,file);
59 /****************************************************************************
60 Create an error packet from errno.
61 ****************************************************************************/
63 int unix_error_packet(char *outbuf,int def_class,uint32 def_code,
64 int line, const char *file)
66 int eclass=def_class;
67 int ecode=def_code;
68 NTSTATUS ntstatus = NT_STATUS_OK;
69 int i=0;
71 if (unix_ERR_class != SMB_SUCCESS) {
72 eclass = unix_ERR_class;
73 ecode = unix_ERR_code;
74 ntstatus = unix_ERR_ntstatus;
75 unix_ERR_class = SMB_SUCCESS;
76 unix_ERR_code = 0;
77 unix_ERR_ntstatus = NT_STATUS_OK;
78 } else {
79 while (unix_dos_nt_errmap[i].dos_class != 0) {
80 if (unix_dos_nt_errmap[i].unix_error == errno) {
81 eclass = unix_dos_nt_errmap[i].dos_class;
82 ecode = unix_dos_nt_errmap[i].dos_code;
83 ntstatus = unix_dos_nt_errmap[i].nt_error;
84 break;
86 i++;
90 return error_packet(outbuf,ntstatus,eclass,ecode,False,line,file);
94 /****************************************************************************
95 Create an error packet. Normally called using the ERROR() macro.
96 ****************************************************************************/
98 int error_packet(char *outbuf,NTSTATUS ntstatus,
99 uint8 eclass,uint32 ecode,BOOL force_dos, int line, const char *file)
101 int outsize = set_message(outbuf,0,0,True);
102 extern uint32 global_client_caps;
104 if (errno != 0)
105 DEBUG(3,("error string = %s\n",strerror(errno)));
107 #if defined(DEVELOPER)
108 if (unix_ERR_class != SMB_SUCCESS || unix_ERR_code != 0 || !NT_STATUS_IS_OK(unix_ERR_ntstatus))
109 smb_panic("logic error in error processing");
110 #endif
113 * We can explicitly force 32 bit error codes even when the
114 * parameter "nt status" is set to no by pre-setting the
115 * FLAGS2_32_BIT_ERROR_CODES bit in the smb_flg2 outbuf.
116 * This is to allow work arounds for client bugs that are needed
117 * when talking with clients that normally expect nt status codes. JRA.
120 if ((lp_nt_status_support() || (SVAL(outbuf,smb_flg2) & FLAGS2_32_BIT_ERROR_CODES)) && (global_client_caps & CAP_STATUS32) && (!force_dos)) {
121 if (NT_STATUS_V(ntstatus) == 0 && eclass)
122 ntstatus = dos_to_ntstatus(eclass, ecode);
123 SIVAL(outbuf,smb_rcls,NT_STATUS_V(ntstatus));
124 SSVAL(outbuf,smb_flg2, SVAL(outbuf,smb_flg2)|FLAGS2_32_BIT_ERROR_CODES);
125 DEBUG(3,("error packet at %s(%d) cmd=%d (%s) %s\n",
126 file, line,
127 (int)CVAL(outbuf,smb_com),
128 smb_fn_name(CVAL(outbuf,smb_com)),
129 nt_errstr(ntstatus)));
130 return outsize;
133 if (eclass == 0 && NT_STATUS_V(ntstatus))
134 ntstatus_to_dos(ntstatus, &eclass, &ecode);
136 SSVAL(outbuf,smb_flg2, SVAL(outbuf,smb_flg2)&~FLAGS2_32_BIT_ERROR_CODES);
137 SSVAL(outbuf,smb_rcls,eclass);
138 SSVAL(outbuf,smb_err,ecode);
140 DEBUG(3,("error packet at %s(%d) cmd=%d (%s) eclass=%d ecode=%d\n",
141 file, line,
142 (int)CVAL(outbuf,smb_com),
143 smb_fn_name(CVAL(outbuf,smb_com)),
144 eclass,
145 ecode));
147 return outsize;