added sid_name_use array argument to lsa_lookup_names and lsa_lookup_sids.
[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 /****************************************************************************
32   create an error packet from a cached error.
33 ****************************************************************************/
34 int cached_error_packet(char *inbuf,char *outbuf,files_struct *fsp,int line)
35 {
36   write_bmpx_struct *wbmpx = fsp->wbmpx_ptr;
37
38   int32 eclass = wbmpx->wr_errclass;
39   int32 err = wbmpx->wr_error;
40
41   /* We can now delete the auxiliary struct */
42   free((char *)wbmpx);
43   fsp->wbmpx_ptr = NULL;
44   return error_packet(inbuf,outbuf,eclass,err,line);
45 }
46
47
48 struct
49 {
50   int unixerror;
51   int smbclass;
52   int smbcode;
53 } unix_smb_errmap[] =
54 {
55   {EPERM,ERRDOS,ERRnoaccess},
56   {EACCES,ERRDOS,ERRnoaccess},
57   {ENOENT,ERRDOS,ERRbadfile},
58   {ENOTDIR,ERRDOS,ERRbadpath},
59   {EIO,ERRHRD,ERRgeneral},
60   {EBADF,ERRSRV,ERRsrverror},
61   {EINVAL,ERRSRV,ERRsrverror},
62   {EEXIST,ERRDOS,ERRfilexists},
63   {ENFILE,ERRDOS,ERRnofids},
64   {EMFILE,ERRDOS,ERRnofids},
65   {ENOSPC,ERRHRD,ERRdiskfull},
66 #ifdef EDQUOT
67   {EDQUOT,ERRHRD,ERRdiskfull},
68 #endif
69 #ifdef ENOTEMPTY
70   {ENOTEMPTY,ERRDOS,ERRnoaccess},
71 #endif
72 #ifdef EXDEV
73   {EXDEV,ERRDOS,ERRdiffdevice},
74 #endif
75   {EROFS,ERRHRD,ERRnowrite},
76   {0,0,0}
77 };
78
79 /****************************************************************************
80   create an error packet from errno
81 ****************************************************************************/
82 int unix_error_packet(char *inbuf,char *outbuf,int def_class,uint32 def_code,int line)
83 {
84   int eclass=def_class;
85   int ecode=def_code;
86   int i=0;
87
88   if (unix_ERR_class != SMB_SUCCESS)
89     {
90       eclass = unix_ERR_class;
91       ecode = unix_ERR_code;
92       unix_ERR_class = SMB_SUCCESS;
93       unix_ERR_code = 0;
94     }
95   else
96     {
97       while (unix_smb_errmap[i].smbclass != 0)
98       {
99             if (unix_smb_errmap[i].unixerror == errno)
100             {
101               eclass = unix_smb_errmap[i].smbclass;
102               ecode = unix_smb_errmap[i].smbcode;
103               break;
104             }
105           i++;
106       }
107     }
108
109   return(error_packet(inbuf,outbuf,eclass,ecode,line));
110 }
111
112
113 /****************************************************************************
114   create an error packet. Normally called using the ERROR() macro
115 ****************************************************************************/
116 int error_packet(char *inbuf,char *outbuf,int error_class,uint32 error_code,int line)
117 {
118   int outsize = set_message(outbuf,0,0,True);
119   int cmd = CVAL(inbuf,smb_com);
120   int flgs2 = SVAL(outbuf,smb_flg2); 
121
122   if ((flgs2 & FLAGS2_32_BIT_ERROR_CODES) == FLAGS2_32_BIT_ERROR_CODES)
123   {
124     SIVAL(outbuf,smb_rcls,error_code);
125     
126     DEBUG( 3, ( "32 bit error packet at line %d cmd=%d (%s) eclass=%08x [%s]\n",
127               line, cmd, smb_fn_name(cmd), error_code, smb_errstr(outbuf) ) );
128   }
129   else
130   {
131     CVAL(outbuf,smb_rcls) = error_class;
132     SSVAL(outbuf,smb_err,error_code);  
133     DEBUG( 3, ( "error packet at line %d cmd=%d (%s) eclass=%d ecode=%d\n",
134               line,
135               (int)CVAL(inbuf,smb_com),
136               smb_fn_name(CVAL(inbuf,smb_com)),
137               error_class,
138               error_code ) );
139
140   }
141   
142   if (errno != 0)
143     DEBUG(3,("error string = %s\n",strerror(errno)));
144   
145   return(outsize);
146 }