r2436: the second big lump of posix vfs code.
[samba.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
43         pvfs->share_name = talloc_strdup(pvfs, lp_servicename(snum));
44 }
45
46
47 /*
48   connect to a share - used when a tree_connect operation comes
49   in. For a disk based backend we needs to ensure that the base
50   directory exists (tho it doesn't need to be accessible by the user,
51   that comes later)
52 */
53 static NTSTATUS pvfs_connect(struct smbsrv_request *req, const char *sharename)
54 {
55         struct smbsrv_tcon *tcon = req->tcon;
56         struct pvfs_state *pvfs;
57         struct stat st;
58         char *base_directory;
59
60         DEBUG(0,("WARNING: the posix vfs handler is incomplete - you probably want \"ntvfs handler = simple\"\n"));
61
62         pvfs = talloc_named(tcon, sizeof(struct pvfs_state), "pvfs_connect(%s)", sharename);
63         if (pvfs == NULL) {
64                 return NT_STATUS_NO_MEMORY;
65         }
66         ZERO_STRUCTP(pvfs);
67
68         /* for simplicity of path construction, remove any trailing slash now */
69         base_directory = talloc_strdup(pvfs, lp_pathname(tcon->service));
70         trim_string(base_directory, NULL, "/");
71
72         pvfs->tcon = tcon;
73         pvfs->base_directory = base_directory;
74
75         /* the directory must exist. Note that we deliberately don't
76            check that it is readable */
77         if (stat(pvfs->base_directory, &st) != 0 || !S_ISDIR(st.st_mode)) {
78                 DEBUG(0,("pvfs_connect: '%s' is not a directory, when connecting to [%s]\n", 
79                          pvfs->base_directory, sharename));
80                 return NT_STATUS_BAD_NETWORK_NAME;
81         }
82
83         tcon->fs_type = talloc_strdup(tcon, "NTFS");
84         tcon->dev_type = talloc_strdup(tcon, "A:");
85         tcon->ntvfs_private = pvfs;
86
87         pvfs_setup_options(pvfs);
88
89         return NT_STATUS_OK;
90 }
91
92 /*
93   disconnect from a share
94 */
95 static NTSTATUS pvfs_disconnect(struct smbsrv_tcon *tcon)
96 {
97         return NT_STATUS_OK;
98 }
99
100 /*
101   ioctl interface - we don't do any
102 */
103 static NTSTATUS pvfs_ioctl(struct smbsrv_request *req, union smb_ioctl *io)
104 {
105         DEBUG(0,("pvfs_ioctl not implemented\n"));
106         return NT_STATUS_INVALID_PARAMETER;
107 }
108
109 /*
110   check if a directory exists
111 */
112 static NTSTATUS pvfs_chkpath(struct smbsrv_request *req, struct smb_chkpath *cp)
113 {
114         struct pvfs_state *pvfs = req->tcon->ntvfs_private;
115         struct pvfs_filename *name;
116         NTSTATUS status;
117
118         /* resolve the cifs name to a posix name */
119         status = pvfs_resolve_name(pvfs, req, cp->in.path, 0, &name);
120         if (!NT_STATUS_IS_OK(status)) {
121                 return status;
122         }
123
124         if (!name->exists) {
125                 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
126         }
127
128         if (!S_ISDIR(name->st.st_mode)) {
129                 return NT_STATUS_NOT_A_DIRECTORY;
130         }
131
132         return NT_STATUS_OK;
133 }
134
135 /*
136   rename a set of files
137 */
138 static NTSTATUS pvfs_rename(struct smbsrv_request *req, union smb_rename *ren)
139 {
140         DEBUG(0,("pvfs_rename not implemented\n"));
141         return NT_STATUS_NOT_IMPLEMENTED;
142 }
143
144 /*
145   copy a set of files
146 */
147 static NTSTATUS pvfs_copy(struct smbsrv_request *req, struct smb_copy *cp)
148 {
149         DEBUG(0,("pvfs_copy not implemented\n"));
150         return NT_STATUS_NOT_SUPPORTED;
151 }
152
153 /*
154   seek in a file
155 */
156 static NTSTATUS pvfs_seek(struct smbsrv_request *req, struct smb_seek *io)
157 {
158         DEBUG(0,("pvfs_seek not implemented\n"));
159         return NT_STATUS_NOT_SUPPORTED;
160 }
161
162 /*
163   flush a file
164 */
165 static NTSTATUS pvfs_flush(struct smbsrv_request *req, struct smb_flush *io)
166 {
167         DEBUG(0,("pvfs_flush not implemented\n"));
168         return NT_STATUS_NOT_IMPLEMENTED;
169 }
170
171 /*
172   exit - closing files?
173 */
174 static NTSTATUS pvfs_exit(struct smbsrv_request *req)
175 {
176         DEBUG(0,("pvfs_exit not implemented\n"));
177         return NT_STATUS_NOT_SUPPORTED;
178 }
179
180 /*
181   lock a byte range
182 */
183 static NTSTATUS pvfs_lock(struct smbsrv_request *req, union smb_lock *lck)
184 {
185         DEBUG(0,("pvfs_lock not implemented\n"));
186         return NT_STATUS_NOT_IMPLEMENTED;
187 }
188
189 /*
190   set info on a pathname
191 */
192 static NTSTATUS pvfs_setpathinfo(struct smbsrv_request *req, union smb_setfileinfo *st)
193 {
194         DEBUG(0,("pvfs_setpathinfo not implemented\n"));
195         return NT_STATUS_NOT_SUPPORTED;
196 }
197
198 /*
199   return print queue info
200 */
201 static NTSTATUS pvfs_lpq(struct smbsrv_request *req, union smb_lpq *lpq)
202 {
203         return NT_STATUS_NOT_SUPPORTED;
204 }
205
206 /* SMBtrans - not used on file shares */
207 static NTSTATUS pvfs_trans(struct smbsrv_request *req, struct smb_trans2 *trans2)
208 {
209         return NT_STATUS_ACCESS_DENIED;
210 }
211
212 /*
213   initialialise the POSIX disk backend, registering ourselves with the ntvfs subsystem
214  */
215 NTSTATUS ntvfs_posix_init(void)
216 {
217         NTSTATUS ret;
218         struct ntvfs_ops ops;
219
220         ZERO_STRUCT(ops);
221
222         ops.type = NTVFS_DISK;
223         
224         /* fill in all the operations */
225         ops.connect = pvfs_connect;
226         ops.disconnect = pvfs_disconnect;
227         ops.unlink = pvfs_unlink;
228         ops.chkpath = pvfs_chkpath;
229         ops.qpathinfo = pvfs_qpathinfo;
230         ops.setpathinfo = pvfs_setpathinfo;
231         ops.open = pvfs_open;
232         ops.mkdir = pvfs_mkdir;
233         ops.rmdir = pvfs_rmdir;
234         ops.rename = pvfs_rename;
235         ops.copy = pvfs_copy;
236         ops.ioctl = pvfs_ioctl;
237         ops.read = pvfs_read;
238         ops.write = pvfs_write;
239         ops.seek = pvfs_seek;
240         ops.flush = pvfs_flush; 
241         ops.close = pvfs_close;
242         ops.exit = pvfs_exit;
243         ops.lock = pvfs_lock;
244         ops.setfileinfo = pvfs_setfileinfo;
245         ops.qfileinfo = pvfs_qfileinfo;
246         ops.fsinfo = pvfs_fsinfo;
247         ops.lpq = pvfs_lpq;
248         ops.search_first = pvfs_search_first;
249         ops.search_next = pvfs_search_next;
250         ops.search_close = pvfs_search_close;
251         ops.trans = pvfs_trans;
252
253         /* register ourselves with the NTVFS subsystem. We register
254            under the name 'default' as we wish to be the default
255            backend, and also register as 'posix' */
256         ops.name = "default";
257         ret = register_backend("ntvfs", &ops);
258
259         ops.name = "posix";
260         ret = register_backend("ntvfs", &ops);
261
262         if (!NT_STATUS_IS_OK(ret)) {
263                 DEBUG(0,("Failed to register POSIX backend!\n"));
264         }
265
266         return ret;
267 }