added filename to error_packet()
[samba.git] / source / 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 extern int DEBUGLEVEL;
25
26 /* these can be set by some functions to override the error codes */
27 int unix_ERR_class=SMB_SUCCESS;
28 int unix_ERR_code=0;
29
30
31 struct
32 {
33   int unixerror;
34   int smbclass;
35   int smbcode;
36 } unix_smb_errmap[] =
37 {
38   {EPERM,ERRDOS,ERRnoaccess},
39   {EACCES,ERRDOS,ERRnoaccess},
40   {ENOENT,ERRDOS,ERRbadfile},
41   {ENOTDIR,ERRDOS,ERRbadpath},
42   {EIO,ERRHRD,ERRgeneral},
43   {EBADF,ERRSRV,ERRsrverror},
44   {EINVAL,ERRSRV,ERRsrverror},
45   {EEXIST,ERRDOS,ERRfilexists},
46   {ENFILE,ERRDOS,ERRnofids},
47   {EMFILE,ERRDOS,ERRnofids},
48   {ENOSPC,ERRHRD,ERRdiskfull},
49 #ifdef EDQUOT
50   {EDQUOT,ERRHRD,ERRdiskfull},
51 #endif
52 #ifdef ENOTEMPTY
53   {ENOTEMPTY,ERRDOS,ERRnoaccess},
54 #endif
55 #ifdef EXDEV
56   {EXDEV,ERRDOS,ERRdiffdevice},
57 #endif
58   {EROFS,ERRHRD,ERRnowrite},
59   {0,0,0}
60 };
61
62 /****************************************************************************
63   create an error packet from errno
64 ****************************************************************************/
65 int unix_error_packet(char *outbuf,int def_class,uint32 def_code,
66                       int line, const char *file)
67 {
68         int eclass=def_class;
69         int ecode=def_code;
70         int i=0;
71
72         if (unix_ERR_class != SMB_SUCCESS) {
73                 eclass = unix_ERR_class;
74                 ecode = unix_ERR_code;
75                 unix_ERR_class = SMB_SUCCESS;
76                 unix_ERR_code = 0;
77         } else {
78                 while (unix_smb_errmap[i].smbclass != 0) {
79                         if (unix_smb_errmap[i].unixerror == errno) {
80                                 eclass = unix_smb_errmap[i].smbclass;
81                                 ecode = unix_smb_errmap[i].smbcode;
82                                 break;
83                         }
84                         i++;
85                 }
86         }
87
88         return error_packet(outbuf,NT_STATUS_OK,eclass,ecode,line,file);
89 }
90
91
92 /****************************************************************************
93   create an error packet. Normally called using the ERROR() macro
94 ****************************************************************************/
95 int error_packet(char *outbuf,NTSTATUS ntstatus,
96                  uint8 eclass,uint32 ecode,int line, const char *file)
97 {
98         int outsize = set_message(outbuf,0,0,True);
99         extern uint32 global_client_caps;
100
101         if (errno != 0)
102                 DEBUG(3,("error string = %s\n",strerror(errno)));
103   
104         if (global_client_caps & CAP_STATUS32) {
105                 if (NT_STATUS_V(ntstatus) == 0 && eclass) {
106                         ntstatus = dos_to_ntstatus(eclass, ecode);
107                 }
108                 SIVAL(outbuf,smb_rcls,NT_STATUS_V(ntstatus));
109                 SSVAL(outbuf,smb_flg2, SVAL(outbuf,smb_flg2)|FLAGS2_32_BIT_ERROR_CODES);
110                 DEBUG(3,("error packet at %s(%d) cmd=%d (%s) %s\n",
111                          file, line,
112                          (int)CVAL(outbuf,smb_com),
113                          smb_fn_name(CVAL(outbuf,smb_com)),
114                          get_nt_error_msg(ntstatus)));
115                 return outsize;
116         } 
117
118         if (eclass == 0 && NT_STATUS_V(ntstatus)) {
119                 ntstatus_to_dos(ntstatus, &eclass, &ecode);
120         }
121
122         SSVAL(outbuf,smb_flg2, SVAL(outbuf,smb_flg2)&~FLAGS2_32_BIT_ERROR_CODES);
123         SSVAL(outbuf,smb_rcls,eclass);
124         SSVAL(outbuf,smb_err,ecode);  
125
126         DEBUG(3,("error packet at %s(%d) cmd=%d (%s) eclass=%d ecode=%d\n",
127                   file, line,
128                   (int)CVAL(outbuf,smb_com),
129                   smb_fn_name(CVAL(outbuf,smb_com)),
130                   eclass,
131                   ecode));
132
133         return outsize;
134 }