f2613dc3a8d136a1276368872d507e4b7d3ddf1b
[jra/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 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,int line)
66 {
67         int eclass=def_class;
68         int ecode=def_code;
69         int i=0;
70
71         if (unix_ERR_class != SMB_SUCCESS) {
72                 eclass = unix_ERR_class;
73                 ecode = unix_ERR_code;
74                 unix_ERR_class = SMB_SUCCESS;
75                 unix_ERR_code = 0;
76         } else {
77                 while (unix_smb_errmap[i].smbclass != 0) {
78                         if (unix_smb_errmap[i].unixerror == errno) {
79                                 eclass = unix_smb_errmap[i].smbclass;
80                                 ecode = unix_smb_errmap[i].smbcode;
81                                 break;
82                         }
83                         i++;
84                 }
85         }
86
87         return error_packet(outbuf,NT_STATUS_OK,eclass,ecode,line);
88 }
89
90
91 /****************************************************************************
92   create an error packet. Normally called using the ERROR() macro
93 ****************************************************************************/
94 int error_packet(char *outbuf,NTSTATUS ntstatus,
95                  uint8 eclass,uint32 ecode,int line)
96 {
97         int outsize = set_message(outbuf,0,0,True);
98         extern uint32 global_client_caps;
99
100         if (errno != 0)
101                 DEBUG(3,("error string = %s\n",strerror(errno)));
102   
103         if (global_client_caps & CAP_STATUS32) {
104                 if (NT_STATUS_V(ntstatus) == 0 && eclass) {
105                         ntstatus = dos_to_ntstatus(eclass, ecode);
106                 }
107                 SIVAL(outbuf,smb_rcls,NT_STATUS_V(ntstatus));
108                 SSVAL(outbuf,smb_flg2, SVAL(outbuf,smb_flg2)|FLAGS2_32_BIT_ERROR_CODES);
109                 DEBUG(3,("error packet at line %d cmd=%d (%s) %s\n",
110                          line,
111                          (int)CVAL(outbuf,smb_com),
112                          smb_fn_name(CVAL(outbuf,smb_com)),
113                          get_nt_error_msg(ntstatus)));
114                 return outsize;
115         } 
116
117         if (eclass == 0 && NT_STATUS_V(ntstatus)) {
118                 ntstatus_to_dos(ntstatus, &eclass, &ecode);
119         }
120
121         SSVAL(outbuf,smb_flg2, SVAL(outbuf,smb_flg2)&~FLAGS2_32_BIT_ERROR_CODES);
122         SVAL(outbuf,smb_rcls) = eclass;
123         SSVAL(outbuf,smb_err,ecode);  
124
125         DEBUG(3,("error packet at line %d cmd=%d (%s) eclass=%d ecode=%d\n",
126                   line,
127                   (int)CVAL(outbuf,smb_com),
128                   smb_fn_name(CVAL(outbuf,smb_com)),
129                   eclass,
130                   ecode));
131
132         return outsize;
133 }