s3-auth Rename auth_serversupplied_info varaiables: server_info -> session_info
[samba.git] / source3 / smbd / fake_file.c
1 /* 
2    Unix SMB/CIFS implementation.
3    FAKE FILE suppport, for faking up special files windows want access to
4    Copyright (C) Stefan (metze) Metzmacher      2003
5    
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 3 of the License, or
9    (at your option) any later version.
10    
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15    
16    You should have received a copy of the GNU General Public License
17    along with this program.  If not, see <http://www.gnu.org/licenses/>.
18 */
19
20 #include "includes.h"
21 #include "fake_file.h"
22
23 struct fake_file_type {
24         const char *name;
25         enum FAKE_FILE_TYPE type;
26         void *(*init_pd)(TALLOC_CTX *mem_ctx);
27 };
28
29 static const struct fake_file_type fake_files[] = {
30 #ifdef WITH_QUOTAS
31         {FAKE_FILE_NAME_QUOTA_UNIX, FAKE_FILE_TYPE_QUOTA, init_quota_handle},
32 #endif /* WITH_QUOTAS */
33         {NULL, FAKE_FILE_TYPE_NONE, NULL}
34 };
35
36 /****************************************************************************
37  Create a fake file handle
38 ****************************************************************************/
39
40 static struct fake_file_handle *init_fake_file_handle(enum FAKE_FILE_TYPE type)
41 {
42         struct fake_file_handle *fh = NULL;
43         int i;
44
45         for (i=0; fake_files[i].name!=NULL; i++) {
46                 if (fake_files[i].type==type) {
47                         break;
48                 }
49         }
50
51         if (fake_files[i].name == NULL) {
52                 return NULL;
53         }
54
55         DEBUG(5,("init_fake_file_handle: for [%s]\n",fake_files[i].name));
56
57         fh = talloc(NULL, struct fake_file_handle);
58         if (fh == NULL) {
59                 DEBUG(0,("TALLOC_ZERO() failed.\n"));
60                 return NULL;
61         }
62
63         fh->type = type;
64
65         if (fake_files[i].init_pd) {
66                 fh->private_data = fake_files[i].init_pd(fh);
67         }
68         return fh;
69 }
70
71 /****************************************************************************
72  Does this name match a fake filename ?
73 ****************************************************************************/
74
75 enum FAKE_FILE_TYPE is_fake_file_path(const char *path)
76 {
77         int i;
78
79         if (!path) {
80                 return FAKE_FILE_TYPE_NONE;
81         }
82
83         for (i=0;fake_files[i].name!=NULL;i++) {
84                 if (strncmp(path,fake_files[i].name,strlen(fake_files[i].name))==0) {
85                         DEBUG(5,("is_fake_file: [%s] is a fake file\n",path));
86                         return fake_files[i].type;
87                 }
88         }
89
90         return FAKE_FILE_TYPE_NONE;
91 }
92
93 enum FAKE_FILE_TYPE is_fake_file(const struct smb_filename *smb_fname)
94 {
95         char *fname = NULL;
96         NTSTATUS status;
97         enum FAKE_FILE_TYPE ret;
98
99         if (!smb_fname) {
100                 return FAKE_FILE_TYPE_NONE;
101         }
102
103         status = get_full_smb_filename(talloc_tos(), smb_fname, &fname);
104         if (!NT_STATUS_IS_OK(status)) {
105                 return FAKE_FILE_TYPE_NONE;
106         }
107
108         ret = is_fake_file_path(fname);
109
110         TALLOC_FREE(fname);
111
112         return ret;
113 }
114
115 /****************************************************************************
116  Open a fake quota file with a share mode.
117 ****************************************************************************/
118
119 NTSTATUS open_fake_file(struct smb_request *req, connection_struct *conn,
120                                 uint16_t current_vuid,
121                                 enum FAKE_FILE_TYPE fake_file_type,
122                                 const struct smb_filename *smb_fname,
123                                 uint32 access_mask,
124                                 files_struct **result)
125 {
126         files_struct *fsp = NULL;
127         NTSTATUS status;
128
129         /* access check */
130         if (geteuid() != sec_initial_uid()) {
131                 DEBUG(3, ("open_fake_file_shared: access_denied to "
132                           "service[%s] file[%s] user[%s]\n",
133                           lp_servicename(SNUM(conn)),
134                           smb_fname_str_dbg(smb_fname),
135                           conn->session_info->unix_name));
136                 return NT_STATUS_ACCESS_DENIED;
137
138         }
139
140         status = file_new(req, conn, &fsp);
141         if(!NT_STATUS_IS_OK(status)) {
142                 return status;
143         }
144
145         DEBUG(5,("open_fake_file_shared: fname = %s, FID = %d, access_mask = 0x%x\n",
146                  smb_fname_str_dbg(smb_fname), fsp->fnum,
147                  (unsigned int)access_mask));
148
149         fsp->conn = conn;
150         fsp->fh->fd = -1;
151         fsp->vuid = current_vuid;
152         fsp->fh->pos = -1;
153         fsp->can_lock = False; /* Should this be true ? - No, JRA */
154         fsp->access_mask = access_mask;
155         status = fsp_set_smb_fname(fsp, smb_fname);
156         if (!NT_STATUS_IS_OK(status)) {
157                 file_free(req, fsp);
158                 return NT_STATUS_NO_MEMORY;
159         }
160
161         fsp->fake_file_handle = init_fake_file_handle(fake_file_type);
162         
163         if (fsp->fake_file_handle==NULL) {
164                 file_free(req, fsp);
165                 return NT_STATUS_NO_MEMORY;
166         }
167
168         *result = fsp;
169         return NT_STATUS_OK;
170 }
171
172 NTSTATUS close_fake_file(struct smb_request *req, files_struct *fsp)
173 {
174         file_free(req, fsp);
175         return NT_STATUS_OK;
176 }