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