Restored old Bmpx code - actually used by OS/2.
[kai/samba.git] / source3 / smbd / error.c
1 /* 
2    Unix SMB/Netbios implementation.
3    Version 1.9.
4    error packet handling
5    Copyright (C) Andrew Tridgell 1992-1998
6    
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 2 of the License, or
10    (at your option) any later version.
11    
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16    
17    You should have received a copy of the GNU General Public License
18    along with this program; if not, write to the Free Software
19    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 */
21
22 #include "includes.h"
23
24 /* these can be set by some functions to override the error codes */
25 int unix_ERR_class=SMB_SUCCESS;
26 int unix_ERR_code=0;
27
28 /****************************************************************************
29  Create an error packet from a cached error.
30 ****************************************************************************/
31  
32 int cached_error_packet(char *outbuf,files_struct *fsp,int line,const char *file)
33 {
34         write_bmpx_struct *wbmpx = fsp->wbmpx_ptr;
35  
36         int32 eclass = wbmpx->wr_errclass;
37         int32 err = wbmpx->wr_error;
38  
39         /* We can now delete the auxiliary struct */
40         free((char *)wbmpx);
41         fsp->wbmpx_ptr = NULL;
42         return error_packet(outbuf,NT_STATUS_OK,eclass,err,line,file);
43 }
44
45 struct
46 {
47   int unixerror;
48   int smbclass;
49   int smbcode;
50 } unix_smb_errmap[] =
51 {
52   {EPERM,ERRDOS,ERRnoaccess},
53   {EACCES,ERRDOS,ERRnoaccess},
54   {ENOENT,ERRDOS,ERRbadfile},
55   {ENOTDIR,ERRDOS,ERRbadpath},
56   {EIO,ERRHRD,ERRgeneral},
57   {EBADF,ERRSRV,ERRsrverror},
58   {EINVAL,ERRSRV,ERRsrverror},
59   {EEXIST,ERRDOS,ERRfilexists},
60   {ENFILE,ERRDOS,ERRnofids},
61   {EMFILE,ERRDOS,ERRnofids},
62   {ENOSPC,ERRHRD,ERRdiskfull},
63 #ifdef EDQUOT
64   {EDQUOT,ERRHRD,ERRdiskfull},
65 #endif
66 #ifdef ENOTEMPTY
67   {ENOTEMPTY,ERRDOS,ERRnoaccess},
68 #endif
69 #ifdef EXDEV
70   {EXDEV,ERRDOS,ERRdiffdevice},
71 #endif
72   {EROFS,ERRHRD,ERRnowrite},
73   {0,0,0}
74 };
75
76 /****************************************************************************
77   create an error packet from errno
78 ****************************************************************************/
79 int unix_error_packet(char *outbuf,int def_class,uint32 def_code,
80                       int line, const char *file)
81 {
82         int eclass=def_class;
83         int ecode=def_code;
84         int i=0;
85
86         if (unix_ERR_class != SMB_SUCCESS) {
87                 eclass = unix_ERR_class;
88                 ecode = unix_ERR_code;
89                 unix_ERR_class = SMB_SUCCESS;
90                 unix_ERR_code = 0;
91         } else {
92                 while (unix_smb_errmap[i].smbclass != 0) {
93                         if (unix_smb_errmap[i].unixerror == errno) {
94                                 eclass = unix_smb_errmap[i].smbclass;
95                                 ecode = unix_smb_errmap[i].smbcode;
96                                 break;
97                         }
98                         i++;
99                 }
100         }
101
102         return error_packet(outbuf,NT_STATUS_OK,eclass,ecode,line,file);
103 }
104
105
106 /****************************************************************************
107   create an error packet. Normally called using the ERROR() macro
108 ****************************************************************************/
109 int error_packet(char *outbuf,NTSTATUS ntstatus,
110                  uint8 eclass,uint32 ecode,int line, const char *file)
111 {
112         int outsize = set_message(outbuf,0,0,True);
113         extern uint32 global_client_caps;
114
115         if (errno != 0)
116                 DEBUG(3,("error string = %s\n",strerror(errno)));
117   
118         if (global_client_caps & CAP_STATUS32) {
119                 if (NT_STATUS_V(ntstatus) == 0 && eclass) {
120                         ntstatus = dos_to_ntstatus(eclass, ecode);
121                 }
122                 SIVAL(outbuf,smb_rcls,NT_STATUS_V(ntstatus));
123                 SSVAL(outbuf,smb_flg2, SVAL(outbuf,smb_flg2)|FLAGS2_32_BIT_ERROR_CODES);
124                 DEBUG(3,("error packet at %s(%d) cmd=%d (%s) %s\n",
125                          file, line,
126                          (int)CVAL(outbuf,smb_com),
127                          smb_fn_name(CVAL(outbuf,smb_com)),
128                          get_nt_error_msg(ntstatus)));
129                 return outsize;
130         } 
131
132         if (eclass == 0 && NT_STATUS_V(ntstatus)) {
133                 ntstatus_to_dos(ntstatus, &eclass, &ecode);
134         }
135
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);  
139
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));
146
147         return outsize;
148 }