s3-includes: only include system/filesys.h when needed.
[samba.git] / source3 / modules / vfs_linux_xfs_sgid.c
1 /*
2  * Module to work around a bug in Linux XFS:
3  * http://oss.sgi.com/bugzilla/show_bug.cgi?id=280
4  *
5  * Copyright (c) Volker Lendecke 2010
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 #include "system/filesys.h"
23
24 static int linux_xfs_sgid_mkdir(vfs_handle_struct *handle,  const char *path, mode_t mode)
25 {
26         struct smb_filename fname = { 0, };
27         int mkdir_res;
28         int res;
29
30         DEBUG(10, ("Calling linux_xfs_sgid_mkdir(%s)\n", path));
31
32         mkdir_res = SMB_VFS_NEXT_MKDIR(handle, path, mode);
33         if (mkdir_res == -1) {
34                 DEBUG(10, ("SMB_VFS_NEXT_MKDIR returned error: %s\n",
35                            strerror(errno)));
36                 return mkdir_res;
37         }
38
39         if (!parent_dirname(talloc_tos(), path, &fname.base_name, NULL)) {
40                 DEBUG(1, ("parent_dirname failed\n"));
41                 /* return success, we did the mkdir */
42                 return mkdir_res;
43         }
44
45         res = SMB_VFS_NEXT_STAT(handle, &fname);
46         if (res == -1) {
47                 DEBUG(10, ("NEXT_STAT(%s) failed: %s\n", fname.base_name,
48                            strerror(errno)));
49                 /* return success, we did the mkdir */
50                 return mkdir_res;
51         }
52         TALLOC_FREE(fname.base_name);
53         if ((fname.st.st_ex_mode & S_ISGID) == 0) {
54                 /* No SGID to inherit */
55                 DEBUG(10, ("No SGID to inherit\n"));
56                 return mkdir_res;
57         }
58
59         fname.base_name = discard_const_p(char, path);
60
61         res = SMB_VFS_NEXT_STAT(handle, &fname);
62         if (res == -1) {
63                 DEBUG(2, ("Could not stat just created dir %s: %s\n", path,
64                           strerror(errno)));
65                 /* return success, we did the mkdir */
66                 return mkdir_res;
67         }
68         fname.st.st_ex_mode |= S_ISGID;
69         fname.st.st_ex_mode &= ~S_IFDIR;
70
71         /*
72          * Yes, we have to do this as root. If you do it as
73          * non-privileged user, XFS on Linux will just ignore us and
74          * return success. What can you do...
75          */
76         become_root();
77         res = SMB_VFS_NEXT_CHMOD(handle, path, fname.st.st_ex_mode);
78         unbecome_root();
79
80         if (res == -1) {
81                 DEBUG(2, ("CHMOD(%s, %o) failed: %s\n", path,
82                           (int)fname.st.st_ex_mode, strerror(errno)));
83                 /* return success, we did the mkdir */
84                 return mkdir_res;
85         }
86         return mkdir_res;
87 }
88
89 static int linux_xfs_sgid_chmod_acl(vfs_handle_struct *handle,
90                                     const char *name, mode_t mode)
91 {
92         errno = ENOSYS;
93         return -1;
94 }
95
96 static struct vfs_fn_pointers linux_xfs_sgid_fns = {
97         .mkdir = linux_xfs_sgid_mkdir,
98         .chmod_acl = linux_xfs_sgid_chmod_acl,
99 };
100
101 NTSTATUS vfs_linux_xfs_sgid_init(void);
102 NTSTATUS vfs_linux_xfs_sgid_init(void)
103 {
104         return smb_register_vfs(SMB_VFS_INTERFACE_VERSION,
105                                 "linux_xfs_sgid", &linux_xfs_sgid_fns);
106 }