Samba 3: added Samba 3.0.24 sources
[tomato.git] / release / src / router / samba3 / source / smbd / fake_file.c
blobb4f1f02b7244662492c168de312615ed728557de
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
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 2 of the License, or
9 (at your option) any later version.
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.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 #include "includes.h"
23 extern struct current_user current_user;
25 static FAKE_FILE fake_files[] = {
26 #ifdef WITH_QUOTAS
27 {FAKE_FILE_NAME_QUOTA_UNIX, FAKE_FILE_TYPE_QUOTA, init_quota_handle, destroy_quota_handle},
28 #endif /* WITH_QUOTAS */
29 {NULL, FAKE_FILE_TYPE_NONE, NULL, NULL }
32 /****************************************************************************
33 Create a fake file handle
34 ****************************************************************************/
36 static struct _FAKE_FILE_HANDLE *init_fake_file_handle(enum FAKE_FILE_TYPE type)
38 TALLOC_CTX *mem_ctx = NULL;
39 FAKE_FILE_HANDLE *fh = NULL;
40 int i;
42 for (i=0;fake_files[i].name!=NULL;i++) {
43 if (fake_files[i].type==type) {
44 DEBUG(5,("init_fake_file_handle: for [%s]\n",fake_files[i].name));
46 if ((mem_ctx=talloc_init("fake_file_handle"))==NULL) {
47 DEBUG(0,("talloc_init(fake_file_handle) failed.\n"));
48 return NULL;
51 if ((fh =TALLOC_ZERO_P(mem_ctx, FAKE_FILE_HANDLE))==NULL) {
52 DEBUG(0,("talloc_zero() failed.\n"));
53 talloc_destroy(mem_ctx);
54 return NULL;
57 fh->type = type;
58 fh->mem_ctx = mem_ctx;
60 if (fake_files[i].init_pd) {
61 fh->pd = fake_files[i].init_pd(fh->mem_ctx);
64 fh->free_pd = fake_files[i].free_pd;
66 return fh;
70 return NULL;
73 /****************************************************************************
74 Does this name match a fake filename ?
75 ****************************************************************************/
77 enum FAKE_FILE_TYPE is_fake_file(const char *fname)
79 #ifdef HAVE_SYS_QUOTAS
80 int i;
81 #endif
83 if (!fname) {
84 return FAKE_FILE_TYPE_NONE;
87 #ifdef HAVE_SYS_QUOTAS
88 for (i=0;fake_files[i].name!=NULL;i++) {
89 if (strncmp(fname,fake_files[i].name,strlen(fake_files[i].name))==0) {
90 DEBUG(5,("is_fake_file: [%s] is a fake file\n",fname));
91 return fake_files[i].type;
94 #endif
96 return FAKE_FILE_TYPE_NONE;
100 /****************************************************************************
101 Open a fake quota file with a share mode.
102 ****************************************************************************/
104 files_struct *open_fake_file(connection_struct *conn,
105 enum FAKE_FILE_TYPE fake_file_type,
106 const char *fname,
107 uint32 access_mask)
109 files_struct *fsp = NULL;
111 /* access check */
112 if (current_user.ut.uid != 0) {
113 DEBUG(1,("open_fake_file_shared: access_denied to service[%s] file[%s] user[%s]\n",
114 lp_servicename(SNUM(conn)),fname,conn->user));
115 errno = EACCES;
116 return NULL;
119 fsp = file_new(conn);
120 if(!fsp) {
121 return NULL;
124 DEBUG(5,("open_fake_file_shared: fname = %s, FID = %d, access_mask = 0x%x\n",
125 fname, fsp->fnum, (unsigned int)access_mask));
127 fsp->conn = conn;
128 fsp->fh->fd = -1;
129 fsp->vuid = current_user.vuid;
130 fsp->fh->pos = -1;
131 fsp->can_lock = True; /* Should this be true ? */
132 fsp->access_mask = access_mask;
133 string_set(&fsp->fsp_name,fname);
135 fsp->fake_file_handle = init_fake_file_handle(fake_file_type);
137 if (fsp->fake_file_handle==NULL) {
138 file_free(fsp);
139 return NULL;
142 conn->num_files_open++;
143 return fsp;
146 void destroy_fake_file_handle(FAKE_FILE_HANDLE **fh)
148 if (!fh||!(*fh)) {
149 return;
152 if ((*fh)->free_pd) {
153 (*fh)->free_pd(&(*fh)->pd);
156 talloc_destroy((*fh)->mem_ctx);
157 (*fh) = NULL;
160 int close_fake_file(files_struct *fsp)
162 file_free(fsp);
163 return 0;