r3461: another place where "open" was used as a structure element
[sfrench/samba-autobuild/.git] / source4 / ntvfs / posix / vfs_posix.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    POSIX NTVFS backend
5
6    Copyright (C) Andrew Tridgell 2004
7
8    This program is free software; you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 2 of the License, or
11    (at your option) any later version.
12    
13    This program is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17    
18    You should have received a copy of the GNU General Public License
19    along with this program; if not, write to the Free Software
20    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 */
22 /*
23   this implements most of the POSIX NTVFS backend
24   This is the default backend
25 */
26
27 #include "include/includes.h"
28 #include "vfs_posix.h"
29
30
31 /*
32   setup config options for a posix share
33 */
34 static void pvfs_setup_options(struct pvfs_state *pvfs)
35 {
36         int snum = pvfs->tcon->service;
37
38         if (lp_map_hidden(snum))     pvfs->flags |= PVFS_FLAG_MAP_HIDDEN;
39         if (lp_map_archive(snum))    pvfs->flags |= PVFS_FLAG_MAP_ARCHIVE;
40         if (lp_map_system(snum))     pvfs->flags |= PVFS_FLAG_MAP_SYSTEM;
41         if (lp_readonly(snum))       pvfs->flags |= PVFS_FLAG_READONLY;
42         if (lp_strict_sync(snum))    pvfs->flags |= PVFS_FLAG_STRICT_SYNC;
43         if (lp_strict_locking(snum)) pvfs->flags |= PVFS_FLAG_STRICT_LOCKING;
44         if (lp_ci_filesystem(snum))  pvfs->flags |= PVFS_FLAG_CI_FILESYSTEM;
45
46         pvfs->share_name = talloc_strdup(pvfs, lp_servicename(snum));
47 }
48
49
50 /*
51   connect to a share - used when a tree_connect operation comes
52   in. For a disk based backend we needs to ensure that the base
53   directory exists (tho it doesn't need to be accessible by the user,
54   that comes later)
55 */
56 static NTSTATUS pvfs_connect(struct ntvfs_module_context *ntvfs,
57                              struct smbsrv_request *req, const char *sharename)
58 {
59         struct smbsrv_tcon *tcon = req->tcon;
60         struct pvfs_state *pvfs;
61         struct stat st;
62         char *base_directory;
63         NTSTATUS status;
64
65         pvfs = talloc_zero_p(tcon, struct pvfs_state);
66         if (pvfs == NULL) {
67                 return NT_STATUS_NO_MEMORY;
68         }
69
70         /* for simplicity of path construction, remove any trailing slash now */
71         base_directory = talloc_strdup(pvfs, lp_pathname(tcon->service));
72         trim_string(base_directory, NULL, "/");
73
74         pvfs->tcon = tcon;
75         pvfs->base_directory = base_directory;
76
77         /* the directory must exist. Note that we deliberately don't
78            check that it is readable */
79         if (stat(pvfs->base_directory, &st) != 0 || !S_ISDIR(st.st_mode)) {
80                 DEBUG(0,("pvfs_connect: '%s' is not a directory, when connecting to [%s]\n", 
81                          pvfs->base_directory, sharename));
82                 return NT_STATUS_BAD_NETWORK_NAME;
83         }
84
85         tcon->fs_type = talloc_strdup(tcon, "NTFS");
86         tcon->dev_type = talloc_strdup(tcon, "A:");
87
88         ntvfs->private_data = pvfs;
89
90         pvfs->brl_context = brl_init(pvfs, 
91                                      pvfs->tcon->smb_conn->connection->server_id,  
92                                      pvfs->tcon->service,
93                                      pvfs->tcon->smb_conn->connection->messaging_ctx);
94         if (pvfs->brl_context == NULL) {
95                 return NT_STATUS_INTERNAL_DB_CORRUPTION;
96         }
97
98         pvfs->odb_context = odb_init(pvfs, 
99                                      pvfs->tcon->smb_conn->connection->server_id,  
100                                      pvfs->tcon->service,
101                                      pvfs->tcon->smb_conn->connection->messaging_ctx);
102         if (pvfs->odb_context == NULL) {
103                 return NT_STATUS_INTERNAL_DB_CORRUPTION;
104         }
105
106         /* allocate the fnum id -> ptr tree */
107         pvfs->idtree_fnum = idr_init(pvfs);
108         if (pvfs->idtree_fnum == NULL) {
109                 return NT_STATUS_NO_MEMORY;
110         }
111
112         /* allocate the search handle -> ptr tree */
113         pvfs->idtree_search = idr_init(pvfs);
114         if (pvfs->idtree_search == NULL) {
115                 return NT_STATUS_NO_MEMORY;
116         }
117
118         status = pvfs_mangle_init(pvfs);
119         if (!NT_STATUS_IS_OK(status)) {
120                 return status;
121         }
122
123         pvfs_setup_options(pvfs);
124
125 #ifdef SIGXFSZ
126         /* who had the stupid idea to generate a signal on a large
127            file write instead of just failing it!? */
128         BlockSignals(True, SIGXFSZ);
129 #endif
130
131         return NT_STATUS_OK;
132 }
133
134 /*
135   disconnect from a share
136 */
137 static NTSTATUS pvfs_disconnect(struct ntvfs_module_context *ntvfs,
138                                 struct smbsrv_tcon *tcon)
139 {
140         return NT_STATUS_OK;
141 }
142
143 /*
144   check if a directory exists
145 */
146 static NTSTATUS pvfs_chkpath(struct ntvfs_module_context *ntvfs,
147                              struct smbsrv_request *req, struct smb_chkpath *cp)
148 {
149         struct pvfs_state *pvfs = ntvfs->private_data;
150         struct pvfs_filename *name;
151         NTSTATUS status;
152
153         /* resolve the cifs name to a posix name */
154         status = pvfs_resolve_name(pvfs, req, cp->in.path, 
155                                    PVFS_RESOLVE_NO_WILDCARD, &name);
156         if (!NT_STATUS_IS_OK(status)) {
157                 return status;
158         }
159
160         if (!name->exists) {
161                 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
162         }
163
164         if (!S_ISDIR(name->st.st_mode)) {
165                 return NT_STATUS_NOT_A_DIRECTORY;
166         }
167
168         return NT_STATUS_OK;
169 }
170
171 /*
172   copy a set of files
173 */
174 static NTSTATUS pvfs_copy(struct ntvfs_module_context *ntvfs,
175                           struct smbsrv_request *req, struct smb_copy *cp)
176 {
177         DEBUG(0,("pvfs_copy not implemented\n"));
178         return NT_STATUS_NOT_SUPPORTED;
179 }
180
181 /*
182   return print queue info
183 */
184 static NTSTATUS pvfs_lpq(struct ntvfs_module_context *ntvfs,
185                          struct smbsrv_request *req, union smb_lpq *lpq)
186 {
187         return NT_STATUS_NOT_SUPPORTED;
188 }
189
190 /* SMBtrans - not used on file shares */
191 static NTSTATUS pvfs_trans(struct ntvfs_module_context *ntvfs,
192                            struct smbsrv_request *req, struct smb_trans2 *trans2)
193 {
194         return NT_STATUS_ACCESS_DENIED;
195 }
196
197 /*
198   initialialise the POSIX disk backend, registering ourselves with the ntvfs subsystem
199  */
200 NTSTATUS ntvfs_posix_init(void)
201 {
202         NTSTATUS ret;
203         struct ntvfs_ops ops;
204
205         ZERO_STRUCT(ops);
206
207         ops.type = NTVFS_DISK;
208         
209         /* fill in all the operations */
210         ops.connect = pvfs_connect;
211         ops.disconnect = pvfs_disconnect;
212         ops.unlink = pvfs_unlink;
213         ops.chkpath = pvfs_chkpath;
214         ops.qpathinfo = pvfs_qpathinfo;
215         ops.setpathinfo = pvfs_setpathinfo;
216         ops.openfile = pvfs_open;
217         ops.mkdir = pvfs_mkdir;
218         ops.rmdir = pvfs_rmdir;
219         ops.rename = pvfs_rename;
220         ops.copy = pvfs_copy;
221         ops.ioctl = pvfs_ioctl;
222         ops.read = pvfs_read;
223         ops.write = pvfs_write;
224         ops.seek = pvfs_seek;
225         ops.flush = pvfs_flush; 
226         ops.close = pvfs_close;
227         ops.exit = pvfs_exit;
228         ops.lock = pvfs_lock;
229         ops.setfileinfo = pvfs_setfileinfo;
230         ops.qfileinfo = pvfs_qfileinfo;
231         ops.fsinfo = pvfs_fsinfo;
232         ops.lpq = pvfs_lpq;
233         ops.search_first = pvfs_search_first;
234         ops.search_next = pvfs_search_next;
235         ops.search_close = pvfs_search_close;
236         ops.trans = pvfs_trans;
237         ops.logoff = pvfs_logoff;
238         ops.async_setup = pvfs_async_setup;
239
240         /* register ourselves with the NTVFS subsystem. We register
241            under the name 'default' as we wish to be the default
242            backend, and also register as 'posix' */
243         ops.name = "default";
244         ret = register_backend("ntvfs", &ops);
245
246         ops.name = "posix";
247         ret = register_backend("ntvfs", &ops);
248
249         if (!NT_STATUS_IS_OK(ret)) {
250                 DEBUG(0,("Failed to register POSIX backend!\n"));
251         }
252
253         return ret;
254 }