Fix bug 5860: safe_strcpy gives a nasty error message for overlong strings
[Samba.git] / source / smbd / fake_file.c
blob1761cb66a85c17b3a1b32e6a6dbd490d98150cb9
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 3 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, see <http://www.gnu.org/licenses/>.
20 #include "includes.h"
22 struct fake_file_type {
23 const char *name;
24 enum FAKE_FILE_TYPE type;
25 void *(*init_pd)(TALLOC_CTX *mem_ctx);
28 static struct fake_file_type fake_files[] = {
29 #ifdef WITH_QUOTAS
30 {FAKE_FILE_NAME_QUOTA_UNIX, FAKE_FILE_TYPE_QUOTA, init_quota_handle},
31 #endif /* WITH_QUOTAS */
32 {NULL, FAKE_FILE_TYPE_NONE, NULL}
35 /****************************************************************************
36 Create a fake file handle
37 ****************************************************************************/
39 static struct fake_file_handle *init_fake_file_handle(enum FAKE_FILE_TYPE type)
41 struct fake_file_handle *fh = NULL;
42 int i;
44 for (i=0; fake_files[i].name!=NULL; i++) {
45 if (fake_files[i].type==type) {
46 break;
50 if (fake_files[i].name == NULL) {
51 return NULL;
54 DEBUG(5,("init_fake_file_handle: for [%s]\n",fake_files[i].name));
56 fh = talloc(NULL, struct fake_file_handle);
57 if (fh == NULL) {
58 DEBUG(0,("TALLOC_ZERO() failed.\n"));
59 return NULL;
62 fh->type = type;
64 if (fake_files[i].init_pd) {
65 fh->private_data = fake_files[i].init_pd(fh);
67 return fh;
70 /****************************************************************************
71 Does this name match a fake filename ?
72 ****************************************************************************/
74 enum FAKE_FILE_TYPE is_fake_file(const char *fname)
76 #ifdef HAVE_SYS_QUOTAS
77 int i;
78 #endif
80 if (!fname) {
81 return FAKE_FILE_TYPE_NONE;
84 #ifdef HAVE_SYS_QUOTAS
85 for (i=0;fake_files[i].name!=NULL;i++) {
86 if (strncmp(fname,fake_files[i].name,strlen(fake_files[i].name))==0) {
87 DEBUG(5,("is_fake_file: [%s] is a fake file\n",fname));
88 return fake_files[i].type;
91 #endif
93 return FAKE_FILE_TYPE_NONE;
97 /****************************************************************************
98 Open a fake quota file with a share mode.
99 ****************************************************************************/
101 NTSTATUS open_fake_file(connection_struct *conn,
102 uint16_t current_vuid,
103 enum FAKE_FILE_TYPE fake_file_type,
104 const char *fname,
105 uint32 access_mask,
106 files_struct **result)
108 files_struct *fsp = NULL;
109 NTSTATUS status;
111 /* access check */
112 if (conn->server_info->utok.uid != 0) {
113 DEBUG(3, ("open_fake_file_shared: access_denied to "
114 "service[%s] file[%s] user[%s]\n",
115 lp_servicename(SNUM(conn)), fname,
116 conn->server_info->unix_name));
117 return NT_STATUS_ACCESS_DENIED;
121 status = file_new(conn, &fsp);
122 if(!NT_STATUS_IS_OK(status)) {
123 return status;
126 DEBUG(5,("open_fake_file_shared: fname = %s, FID = %d, access_mask = 0x%x\n",
127 fname, fsp->fnum, (unsigned int)access_mask));
129 fsp->conn = conn;
130 fsp->fh->fd = -1;
131 fsp->vuid = current_vuid;
132 fsp->fh->pos = -1;
133 fsp->can_lock = False; /* Should this be true ? - No, JRA */
134 fsp->access_mask = access_mask;
135 string_set(&fsp->fsp_name,fname);
137 fsp->fake_file_handle = init_fake_file_handle(fake_file_type);
139 if (fsp->fake_file_handle==NULL) {
140 file_free(fsp);
141 return NT_STATUS_NO_MEMORY;
144 conn->num_files_open++;
145 *result = fsp;
146 return NT_STATUS_OK;
149 NTSTATUS close_fake_file(files_struct *fsp)
151 file_free(fsp);
152 return NT_STATUS_OK;