idmap_tdb.8: Use new idmap syntax in examples
[abartlet/samba.git/.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 "smbd/smbd.h"
22 #include "fake_file.h"
23 #include "auth.h"
24
25 struct fake_file_type {
26         const char *name;
27         enum FAKE_FILE_TYPE type;
28         void *(*init_pd)(TALLOC_CTX *mem_ctx);
29 };
30
31 static const struct fake_file_type fake_files[] = {
32 #ifdef WITH_QUOTAS
33         {FAKE_FILE_NAME_QUOTA_UNIX, FAKE_FILE_TYPE_QUOTA, init_quota_handle},
34 #endif /* WITH_QUOTAS */
35         {NULL, FAKE_FILE_TYPE_NONE, NULL}
36 };
37
38 /****************************************************************************
39  Create a fake file handle
40 ****************************************************************************/
41
42 static struct fake_file_handle *init_fake_file_handle(enum FAKE_FILE_TYPE type)
43 {
44         struct fake_file_handle *fh = NULL;
45         int i;
46
47         for (i=0; fake_files[i].name!=NULL; i++) {
48                 if (fake_files[i].type==type) {
49                         break;
50                 }
51         }
52
53         if (fake_files[i].name == NULL) {
54                 return NULL;
55         }
56
57         DEBUG(5,("init_fake_file_handle: for [%s]\n",fake_files[i].name));
58
59         fh = talloc(NULL, struct fake_file_handle);
60         if (fh == NULL) {
61                 DEBUG(0,("TALLOC_ZERO() failed.\n"));
62                 return NULL;
63         }
64
65         fh->type = type;
66
67         if (fake_files[i].init_pd) {
68                 fh->private_data = fake_files[i].init_pd(fh);
69         }
70         return fh;
71 }
72
73 /****************************************************************************
74  Does this name match a fake filename ?
75 ****************************************************************************/
76
77 enum FAKE_FILE_TYPE is_fake_file_path(const char *path)
78 {
79         int i;
80
81         if (!path) {
82                 return FAKE_FILE_TYPE_NONE;
83         }
84
85         for (i=0;fake_files[i].name!=NULL;i++) {
86                 if (strncmp(path,fake_files[i].name,strlen(fake_files[i].name))==0) {
87                         DEBUG(5,("is_fake_file: [%s] is a fake file\n",path));
88                         return fake_files[i].type;
89                 }
90         }
91
92         return FAKE_FILE_TYPE_NONE;
93 }
94
95 enum FAKE_FILE_TYPE is_fake_file(const struct smb_filename *smb_fname)
96 {
97         char *fname = NULL;
98         NTSTATUS status;
99         enum FAKE_FILE_TYPE ret;
100
101         if (!smb_fname) {
102                 return FAKE_FILE_TYPE_NONE;
103         }
104
105         status = get_full_smb_filename(talloc_tos(), smb_fname, &fname);
106         if (!NT_STATUS_IS_OK(status)) {
107                 return FAKE_FILE_TYPE_NONE;
108         }
109
110         ret = is_fake_file_path(fname);
111
112         TALLOC_FREE(fname);
113
114         return ret;
115 }
116
117 /****************************************************************************
118  Open a fake quota file with a share mode.
119 ****************************************************************************/
120
121 NTSTATUS open_fake_file(struct smb_request *req, connection_struct *conn,
122                                 uint16_t current_vuid,
123                                 enum FAKE_FILE_TYPE fake_file_type,
124                                 const struct smb_filename *smb_fname,
125                                 uint32 access_mask,
126                                 files_struct **result)
127 {
128         files_struct *fsp = NULL;
129         NTSTATUS status;
130
131         /* access check */
132         if (geteuid() != sec_initial_uid()) {
133                 DEBUG(3, ("open_fake_file_shared: access_denied to "
134                           "service[%s] file[%s] user[%s]\n",
135                           lp_servicename(SNUM(conn)),
136                           smb_fname_str_dbg(smb_fname),
137                           conn->session_info->unix_name));
138                 return NT_STATUS_ACCESS_DENIED;
139
140         }
141
142         status = file_new(req, conn, &fsp);
143         if(!NT_STATUS_IS_OK(status)) {
144                 return status;
145         }
146
147         DEBUG(5,("open_fake_file_shared: fname = %s, FID = %d, access_mask = 0x%x\n",
148                  smb_fname_str_dbg(smb_fname), fsp->fnum,
149                  (unsigned int)access_mask));
150
151         fsp->conn = conn;
152         fsp->fh->fd = -1;
153         fsp->vuid = current_vuid;
154         fsp->fh->pos = -1;
155         fsp->can_lock = False; /* Should this be true ? - No, JRA */
156         fsp->access_mask = access_mask;
157         status = fsp_set_smb_fname(fsp, smb_fname);
158         if (!NT_STATUS_IS_OK(status)) {
159                 file_free(req, fsp);
160                 return NT_STATUS_NO_MEMORY;
161         }
162
163         fsp->fake_file_handle = init_fake_file_handle(fake_file_type);
164         
165         if (fsp->fake_file_handle==NULL) {
166                 file_free(req, fsp);
167                 return NT_STATUS_NO_MEMORY;
168         }
169
170         *result = fsp;
171         return NT_STATUS_OK;
172 }
173
174 NTSTATUS close_fake_file(struct smb_request *req, files_struct *fsp)
175 {
176         file_free(req, fsp);
177         return NT_STATUS_OK;
178 }