1 | /*
|
---|
2 | * AppleTalk VFS module for Samba-3.x
|
---|
3 | *
|
---|
4 | * Copyright (C) Alexei Kotovich, 2002
|
---|
5 | * Copyright (C) Stefan (metze) Metzmacher, 2003
|
---|
6 | *
|
---|
7 | * This program is free software; you can redistribute it and/or modify
|
---|
8 | * it under the terms of the GNU General Public License as published by
|
---|
9 | * the Free Software Foundation; either version 3 of the License, or
|
---|
10 | * (at your option) any later version.
|
---|
11 | *
|
---|
12 | * This program is distributed in the hope that it will be useful,
|
---|
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
15 | * GNU General Public License for more details.
|
---|
16 | *
|
---|
17 | * You should have received a copy of the GNU General Public License
|
---|
18 | * along with this program; if not, see <http://www.gnu.org/licenses/>.
|
---|
19 | */
|
---|
20 |
|
---|
21 | #include "includes.h"
|
---|
22 |
|
---|
23 | #undef DBGC_CLASS
|
---|
24 | #define DBGC_CLASS DBGC_VFS
|
---|
25 |
|
---|
26 | #define APPLEDOUBLE ".AppleDouble"
|
---|
27 | #define ADOUBLEMODE 0777
|
---|
28 |
|
---|
29 | /* atalk functions */
|
---|
30 |
|
---|
31 | static int atalk_build_paths(TALLOC_CTX *ctx, const char *path,
|
---|
32 | const char *fname,
|
---|
33 | char **adbl_path, char **orig_path,
|
---|
34 | SMB_STRUCT_STAT *adbl_info,
|
---|
35 | SMB_STRUCT_STAT *orig_info,
|
---|
36 | bool fake_dir_create_times);
|
---|
37 |
|
---|
38 | static int atalk_unlink_file(const char *path);
|
---|
39 |
|
---|
40 | static int atalk_get_path_ptr(char *path)
|
---|
41 | {
|
---|
42 | int i = 0;
|
---|
43 | int ptr = 0;
|
---|
44 |
|
---|
45 | for (i = 0; path[i]; i ++) {
|
---|
46 | if (path[i] == '/')
|
---|
47 | ptr = i;
|
---|
48 | /* get out some 'spam';) from win32's file name */
|
---|
49 | else if (path[i] == ':') {
|
---|
50 | path[i] = '\0';
|
---|
51 | break;
|
---|
52 | }
|
---|
53 | }
|
---|
54 |
|
---|
55 | return ptr;
|
---|
56 | }
|
---|
57 |
|
---|
58 | static int atalk_build_paths(TALLOC_CTX *ctx, const char *path,
|
---|
59 | const char *fname,
|
---|
60 | char **adbl_path, char **orig_path,
|
---|
61 | SMB_STRUCT_STAT *adbl_info,
|
---|
62 | SMB_STRUCT_STAT *orig_info,
|
---|
63 | bool fake_dir_create_times)
|
---|
64 | {
|
---|
65 | int ptr0 = 0;
|
---|
66 | int ptr1 = 0;
|
---|
67 | char *dname = 0;
|
---|
68 | char *name = 0;
|
---|
69 |
|
---|
70 | if (!ctx || !path || !fname || !adbl_path || !orig_path ||
|
---|
71 | !adbl_info || !orig_info)
|
---|
72 | return -1;
|
---|
73 | #if 0
|
---|
74 | DEBUG(3, ("ATALK: PATH: %s[%s]\n", path, fname));
|
---|
75 | #endif
|
---|
76 | if (strstr(path, APPLEDOUBLE) || strstr(fname, APPLEDOUBLE)) {
|
---|
77 | DEBUG(3, ("ATALK: path %s[%s] already contains %s\n", path, fname, APPLEDOUBLE));
|
---|
78 | return -1;
|
---|
79 | }
|
---|
80 |
|
---|
81 | if (fname[0] == '.') ptr0 ++;
|
---|
82 | if (fname[1] == '/') ptr0 ++;
|
---|
83 |
|
---|
84 | *orig_path = talloc_asprintf(ctx, "%s/%s", path, &fname[ptr0]);
|
---|
85 |
|
---|
86 | /* get pointer to last '/' */
|
---|
87 | ptr1 = atalk_get_path_ptr(*orig_path);
|
---|
88 |
|
---|
89 | sys_lstat(*orig_path, orig_info, fake_dir_create_times);
|
---|
90 |
|
---|
91 | if (S_ISDIR(orig_info->st_ex_mode)) {
|
---|
92 | *adbl_path = talloc_asprintf(ctx, "%s/%s/%s/",
|
---|
93 | path, &fname[ptr0], APPLEDOUBLE);
|
---|
94 | } else {
|
---|
95 | dname = talloc_strdup(ctx, *orig_path);
|
---|
96 | dname[ptr1] = '\0';
|
---|
97 | name = *orig_path;
|
---|
98 | *adbl_path = talloc_asprintf(ctx, "%s/%s/%s",
|
---|
99 | dname, APPLEDOUBLE, &name[ptr1 + 1]);
|
---|
100 | }
|
---|
101 | #if 0
|
---|
102 | DEBUG(3, ("ATALK: DEBUG:\n%s\n%s\n", *orig_path, *adbl_path));
|
---|
103 | #endif
|
---|
104 | sys_lstat(*adbl_path, adbl_info, fake_dir_create_times);
|
---|
105 | return 0;
|
---|
106 | }
|
---|
107 |
|
---|
108 | static int atalk_unlink_file(const char *path)
|
---|
109 | {
|
---|
110 | int ret = 0;
|
---|
111 |
|
---|
112 | become_root();
|
---|
113 | ret = unlink(path);
|
---|
114 | unbecome_root();
|
---|
115 |
|
---|
116 | return ret;
|
---|
117 | }
|
---|
118 |
|
---|
119 | static void atalk_add_to_list(name_compare_entry **list)
|
---|
120 | {
|
---|
121 | int i, count = 0;
|
---|
122 | name_compare_entry *new_list = 0;
|
---|
123 | name_compare_entry *cur_list = 0;
|
---|
124 |
|
---|
125 | cur_list = *list;
|
---|
126 |
|
---|
127 | if (cur_list) {
|
---|
128 | for (i = 0, count = 0; cur_list[i].name; i ++, count ++) {
|
---|
129 | if (strstr(cur_list[i].name, APPLEDOUBLE))
|
---|
130 | return;
|
---|
131 | }
|
---|
132 | }
|
---|
133 |
|
---|
134 | if (!(new_list = SMB_CALLOC_ARRAY(name_compare_entry, (count == 0 ? 1 : count + 1))))
|
---|
135 | return;
|
---|
136 |
|
---|
137 | for (i = 0; i < count; i ++) {
|
---|
138 | new_list[i].name = SMB_STRDUP(cur_list[i].name);
|
---|
139 | new_list[i].is_wild = cur_list[i].is_wild;
|
---|
140 | }
|
---|
141 |
|
---|
142 | new_list[i].name = SMB_STRDUP(APPLEDOUBLE);
|
---|
143 | new_list[i].is_wild = False;
|
---|
144 |
|
---|
145 | free_namearray(*list);
|
---|
146 |
|
---|
147 | *list = new_list;
|
---|
148 | new_list = 0;
|
---|
149 | cur_list = 0;
|
---|
150 | }
|
---|
151 |
|
---|
152 | static void atalk_rrmdir(TALLOC_CTX *ctx, char *path)
|
---|
153 | {
|
---|
154 | char *dpath;
|
---|
155 | SMB_STRUCT_DIRENT *dent = 0;
|
---|
156 | SMB_STRUCT_DIR *dir;
|
---|
157 |
|
---|
158 | if (!path) return;
|
---|
159 |
|
---|
160 | dir = sys_opendir(path);
|
---|
161 | if (!dir) return;
|
---|
162 |
|
---|
163 | while (NULL != (dent = sys_readdir(dir))) {
|
---|
164 | if (strcmp(dent->d_name, ".") == 0 ||
|
---|
165 | strcmp(dent->d_name, "..") == 0)
|
---|
166 | continue;
|
---|
167 | if (!(dpath = talloc_asprintf(ctx, "%s/%s",
|
---|
168 | path, dent->d_name)))
|
---|
169 | continue;
|
---|
170 | atalk_unlink_file(dpath);
|
---|
171 | }
|
---|
172 |
|
---|
173 | sys_closedir(dir);
|
---|
174 | }
|
---|
175 |
|
---|
176 | /* Disk operations */
|
---|
177 |
|
---|
178 | /* Directory operations */
|
---|
179 |
|
---|
180 | static SMB_STRUCT_DIR *atalk_opendir(struct vfs_handle_struct *handle, const char *fname, const char *mask, uint32 attr)
|
---|
181 | {
|
---|
182 | SMB_STRUCT_DIR *ret = 0;
|
---|
183 |
|
---|
184 | ret = SMB_VFS_NEXT_OPENDIR(handle, fname, mask, attr);
|
---|
185 |
|
---|
186 | /*
|
---|
187 | * when we try to perform delete operation upon file which has fork
|
---|
188 | * in ./.AppleDouble and this directory wasn't hidden by Samba,
|
---|
189 | * MS Windows explorer causes the error: "Cannot find the specified file"
|
---|
190 | * There is some workaround to avoid this situation, i.e. if
|
---|
191 | * connection has not .AppleDouble entry in either veto or hide
|
---|
192 | * list then it would be nice to add one.
|
---|
193 | */
|
---|
194 |
|
---|
195 | atalk_add_to_list(&handle->conn->hide_list);
|
---|
196 | atalk_add_to_list(&handle->conn->veto_list);
|
---|
197 |
|
---|
198 | return ret;
|
---|
199 | }
|
---|
200 |
|
---|
201 | static int atalk_rmdir(struct vfs_handle_struct *handle, const char *path)
|
---|
202 | {
|
---|
203 | bool add = False;
|
---|
204 | TALLOC_CTX *ctx = 0;
|
---|
205 | char *dpath;
|
---|
206 |
|
---|
207 | if (!handle->conn->origpath || !path) goto exit_rmdir;
|
---|
208 |
|
---|
209 | /* due to there is no way to change bDeleteVetoFiles variable
|
---|
210 | * from this module, gotta use talloc stuff..
|
---|
211 | */
|
---|
212 |
|
---|
213 | strstr(path, APPLEDOUBLE) ? (add = False) : (add = True);
|
---|
214 |
|
---|
215 | if (!(ctx = talloc_init("remove_directory")))
|
---|
216 | goto exit_rmdir;
|
---|
217 |
|
---|
218 | if (!(dpath = talloc_asprintf(ctx, "%s/%s%s",
|
---|
219 | handle->conn->origpath, path, add ? "/"APPLEDOUBLE : "")))
|
---|
220 | goto exit_rmdir;
|
---|
221 |
|
---|
222 | atalk_rrmdir(ctx, dpath);
|
---|
223 |
|
---|
224 | exit_rmdir:
|
---|
225 | talloc_destroy(ctx);
|
---|
226 | return SMB_VFS_NEXT_RMDIR(handle, path);
|
---|
227 | }
|
---|
228 |
|
---|
229 | /* File operations */
|
---|
230 |
|
---|
231 | static int atalk_rename(struct vfs_handle_struct *handle,
|
---|
232 | const struct smb_filename *smb_fname_src,
|
---|
233 | const struct smb_filename *smb_fname_dst)
|
---|
234 | {
|
---|
235 | int ret = 0;
|
---|
236 | char *oldname = NULL;
|
---|
237 | char *adbl_path = NULL;
|
---|
238 | char *orig_path = NULL;
|
---|
239 | SMB_STRUCT_STAT adbl_info;
|
---|
240 | SMB_STRUCT_STAT orig_info;
|
---|
241 | NTSTATUS status;
|
---|
242 |
|
---|
243 | ret = SMB_VFS_NEXT_RENAME(handle, smb_fname_src, smb_fname_dst);
|
---|
244 |
|
---|
245 | status = get_full_smb_filename(talloc_tos(), smb_fname_src, &oldname);
|
---|
246 | if (!NT_STATUS_IS_OK(status)) {
|
---|
247 | return ret;
|
---|
248 | }
|
---|
249 |
|
---|
250 | if (atalk_build_paths(talloc_tos(), handle->conn->origpath, oldname,
|
---|
251 | &adbl_path, &orig_path, &adbl_info,
|
---|
252 | &orig_info, false) != 0)
|
---|
253 | goto exit_rename;
|
---|
254 |
|
---|
255 | if (S_ISDIR(orig_info.st_ex_mode) || S_ISREG(orig_info.st_ex_mode)) {
|
---|
256 | DEBUG(3, ("ATALK: %s has passed..\n", adbl_path));
|
---|
257 | goto exit_rename;
|
---|
258 | }
|
---|
259 |
|
---|
260 | atalk_unlink_file(adbl_path);
|
---|
261 |
|
---|
262 | exit_rename:
|
---|
263 | TALLOC_FREE(oldname);
|
---|
264 | TALLOC_FREE(adbl_path);
|
---|
265 | TALLOC_FREE(orig_path);
|
---|
266 | return ret;
|
---|
267 | }
|
---|
268 |
|
---|
269 | static int atalk_unlink(struct vfs_handle_struct *handle,
|
---|
270 | const struct smb_filename *smb_fname)
|
---|
271 | {
|
---|
272 | int ret = 0, i;
|
---|
273 | char *path = NULL;
|
---|
274 | char *adbl_path = NULL;
|
---|
275 | char *orig_path = NULL;
|
---|
276 | SMB_STRUCT_STAT adbl_info;
|
---|
277 | SMB_STRUCT_STAT orig_info;
|
---|
278 | NTSTATUS status;
|
---|
279 |
|
---|
280 | ret = SMB_VFS_NEXT_UNLINK(handle, smb_fname);
|
---|
281 |
|
---|
282 | status = get_full_smb_filename(talloc_tos(), smb_fname, &path);
|
---|
283 | if (!NT_STATUS_IS_OK(status)) {
|
---|
284 | return ret;
|
---|
285 | }
|
---|
286 |
|
---|
287 | /* no .AppleDouble sync if veto or hide list is empty,
|
---|
288 | * otherwise "Cannot find the specified file" error will be caused
|
---|
289 | */
|
---|
290 |
|
---|
291 | if (!handle->conn->veto_list) return ret;
|
---|
292 | if (!handle->conn->hide_list) return ret;
|
---|
293 |
|
---|
294 | for (i = 0; handle->conn->veto_list[i].name; i ++) {
|
---|
295 | if (strstr(handle->conn->veto_list[i].name, APPLEDOUBLE))
|
---|
296 | break;
|
---|
297 | }
|
---|
298 |
|
---|
299 | if (!handle->conn->veto_list[i].name) {
|
---|
300 | for (i = 0; handle->conn->hide_list[i].name; i ++) {
|
---|
301 | if (strstr(handle->conn->hide_list[i].name, APPLEDOUBLE))
|
---|
302 | break;
|
---|
303 | else {
|
---|
304 | DEBUG(3, ("ATALK: %s is not hidden, skipped..\n",
|
---|
305 | APPLEDOUBLE));
|
---|
306 | goto exit_unlink;
|
---|
307 | }
|
---|
308 | }
|
---|
309 | }
|
---|
310 |
|
---|
311 | if (atalk_build_paths(talloc_tos(), handle->conn->origpath, path,
|
---|
312 | &adbl_path, &orig_path,
|
---|
313 | &adbl_info, &orig_info, false) != 0)
|
---|
314 | goto exit_unlink;
|
---|
315 |
|
---|
316 | if (S_ISDIR(orig_info.st_ex_mode) || S_ISREG(orig_info.st_ex_mode)) {
|
---|
317 | DEBUG(3, ("ATALK: %s has passed..\n", adbl_path));
|
---|
318 | goto exit_unlink;
|
---|
319 | }
|
---|
320 |
|
---|
321 | atalk_unlink_file(adbl_path);
|
---|
322 |
|
---|
323 | exit_unlink:
|
---|
324 | TALLOC_FREE(path);
|
---|
325 | TALLOC_FREE(adbl_path);
|
---|
326 | TALLOC_FREE(orig_path);
|
---|
327 | return ret;
|
---|
328 | }
|
---|
329 |
|
---|
330 | static int atalk_chmod(struct vfs_handle_struct *handle, const char *path, mode_t mode)
|
---|
331 | {
|
---|
332 | int ret = 0;
|
---|
333 | char *adbl_path = 0;
|
---|
334 | char *orig_path = 0;
|
---|
335 | SMB_STRUCT_STAT adbl_info;
|
---|
336 | SMB_STRUCT_STAT orig_info;
|
---|
337 | TALLOC_CTX *ctx;
|
---|
338 |
|
---|
339 | ret = SMB_VFS_NEXT_CHMOD(handle, path, mode);
|
---|
340 |
|
---|
341 | if (!path) return ret;
|
---|
342 |
|
---|
343 | if (!(ctx = talloc_init("chmod_file")))
|
---|
344 | return ret;
|
---|
345 |
|
---|
346 | if (atalk_build_paths(ctx, handle->conn->origpath, path, &adbl_path,
|
---|
347 | &orig_path, &adbl_info, &orig_info,
|
---|
348 | false) != 0)
|
---|
349 | goto exit_chmod;
|
---|
350 |
|
---|
351 | if (!S_ISDIR(orig_info.st_ex_mode) && !S_ISREG(orig_info.st_ex_mode)) {
|
---|
352 | DEBUG(3, ("ATALK: %s has passed..\n", orig_path));
|
---|
353 | goto exit_chmod;
|
---|
354 | }
|
---|
355 |
|
---|
356 | chmod(adbl_path, ADOUBLEMODE);
|
---|
357 |
|
---|
358 | exit_chmod:
|
---|
359 | talloc_destroy(ctx);
|
---|
360 | return ret;
|
---|
361 | }
|
---|
362 |
|
---|
363 | static int atalk_chown(struct vfs_handle_struct *handle, const char *path, uid_t uid, gid_t gid)
|
---|
364 | {
|
---|
365 | int ret = 0;
|
---|
366 | char *adbl_path = 0;
|
---|
367 | char *orig_path = 0;
|
---|
368 | SMB_STRUCT_STAT adbl_info;
|
---|
369 | SMB_STRUCT_STAT orig_info;
|
---|
370 | TALLOC_CTX *ctx;
|
---|
371 |
|
---|
372 | ret = SMB_VFS_NEXT_CHOWN(handle, path, uid, gid);
|
---|
373 |
|
---|
374 | if (!path) return ret;
|
---|
375 |
|
---|
376 | if (!(ctx = talloc_init("chown_file")))
|
---|
377 | return ret;
|
---|
378 |
|
---|
379 | if (atalk_build_paths(ctx, handle->conn->origpath, path,
|
---|
380 | &adbl_path, &orig_path,
|
---|
381 | &adbl_info, &orig_info, false) != 0)
|
---|
382 | goto exit_chown;
|
---|
383 |
|
---|
384 | if (!S_ISDIR(orig_info.st_ex_mode) && !S_ISREG(orig_info.st_ex_mode)) {
|
---|
385 | DEBUG(3, ("ATALK: %s has passed..\n", orig_path));
|
---|
386 | goto exit_chown;
|
---|
387 | }
|
---|
388 |
|
---|
389 | if (chown(adbl_path, uid, gid) == -1) {
|
---|
390 | DEBUG(3, ("ATALK: chown error %s\n", strerror(errno)));
|
---|
391 | }
|
---|
392 |
|
---|
393 | exit_chown:
|
---|
394 | talloc_destroy(ctx);
|
---|
395 | return ret;
|
---|
396 | }
|
---|
397 |
|
---|
398 | static int atalk_lchown(struct vfs_handle_struct *handle, const char *path, uid_t uid, gid_t gid)
|
---|
399 | {
|
---|
400 | int ret = 0;
|
---|
401 | char *adbl_path = 0;
|
---|
402 | char *orig_path = 0;
|
---|
403 | SMB_STRUCT_STAT adbl_info;
|
---|
404 | SMB_STRUCT_STAT orig_info;
|
---|
405 | TALLOC_CTX *ctx;
|
---|
406 |
|
---|
407 | ret = SMB_VFS_NEXT_CHOWN(handle, path, uid, gid);
|
---|
408 |
|
---|
409 | if (!path) return ret;
|
---|
410 |
|
---|
411 | if (!(ctx = talloc_init("lchown_file")))
|
---|
412 | return ret;
|
---|
413 |
|
---|
414 | if (atalk_build_paths(ctx, handle->conn->origpath, path,
|
---|
415 | &adbl_path, &orig_path,
|
---|
416 | &adbl_info, &orig_info, false) != 0)
|
---|
417 | goto exit_lchown;
|
---|
418 |
|
---|
419 | if (!S_ISDIR(orig_info.st_ex_mode) && !S_ISREG(orig_info.st_ex_mode)) {
|
---|
420 | DEBUG(3, ("ATALK: %s has passed..\n", orig_path));
|
---|
421 | goto exit_lchown;
|
---|
422 | }
|
---|
423 |
|
---|
424 | if (lchown(adbl_path, uid, gid) == -1) {
|
---|
425 | DEBUG(3, ("ATALK: lchown error %s\n", strerror(errno)));
|
---|
426 | }
|
---|
427 |
|
---|
428 | exit_lchown:
|
---|
429 | talloc_destroy(ctx);
|
---|
430 | return ret;
|
---|
431 | }
|
---|
432 |
|
---|
433 | static struct vfs_fn_pointers vfs_netatalk_fns = {
|
---|
434 | .opendir = atalk_opendir,
|
---|
435 | .rmdir = atalk_rmdir,
|
---|
436 | .rename = atalk_rename,
|
---|
437 | .unlink = atalk_unlink,
|
---|
438 | .chmod = atalk_chmod,
|
---|
439 | .chown = atalk_chown,
|
---|
440 | .lchown = atalk_lchown,
|
---|
441 | };
|
---|
442 |
|
---|
443 | NTSTATUS vfs_netatalk_init(void);
|
---|
444 | NTSTATUS vfs_netatalk_init(void)
|
---|
445 | {
|
---|
446 | return smb_register_vfs(SMB_VFS_INTERFACE_VERSION, "netatalk",
|
---|
447 | &vfs_netatalk_fns);
|
---|
448 | }
|
---|