s4: add blackbox test for the demote function
[Samba/gebeck_regimport.git] / source3 / modules / gpfs.c
blobd73b94b75fbdfd6d683e77ed7fe161a29ebb4a3e
1 /*
2 * Unix SMB/CIFS implementation.
3 * Provide a connection to GPFS specific features
4 * Copyright (C) Volker Lendecke 2005
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"
21 #include "system/filesys.h"
22 #include "smbd/smbd.h"
24 #include "libcli/security/security.h"
25 #include "gpfs_gpl.h"
26 #include "vfs_gpfs.h"
28 static bool gpfs_getrealfilename;
29 static bool gpfs_winattr;
30 static bool gpfs_do_ftruncate;
32 static int (*gpfs_set_share_fn)(int fd, unsigned int allow, unsigned int deny);
33 static int (*gpfs_set_lease_fn)(int fd, unsigned int leaseType);
34 static int (*gpfs_getacl_fn)(char *pathname, int flags, void *acl);
35 static int (*gpfs_putacl_fn)(char *pathname, int flags, void *acl);
36 static int (*gpfs_get_realfilename_path_fn)(char *pathname, char *filenamep,
37 int *buflen);
38 static int (*gpfs_set_winattrs_path_fn)(char *pathname, int flags, struct gpfs_winattr *attrs);
39 static int (*gpfs_get_winattrs_path_fn)(char *pathname, struct gpfs_winattr *attrs);
40 static int (*gpfs_get_winattrs_fn)(int fd, struct gpfs_winattr *attrs);
41 static int (*gpfs_ftruncate_fn)(int fd, gpfs_off64_t length);
42 static int (*gpfs_lib_init_fn)(int flags);
44 bool set_gpfs_sharemode(files_struct *fsp, uint32 access_mask,
45 uint32 share_access)
47 unsigned int allow = GPFS_SHARE_NONE;
48 unsigned int deny = GPFS_DENY_NONE;
49 int result;
51 if (gpfs_set_share_fn == NULL) {
52 return False;
55 if ((fsp == NULL) || (fsp->fh == NULL) || (fsp->fh->fd < 0)) {
56 /* No real file, don't disturb */
57 return True;
60 allow |= (access_mask & (FILE_WRITE_DATA|FILE_APPEND_DATA|
61 DELETE_ACCESS)) ? GPFS_SHARE_WRITE : 0;
62 allow |= (access_mask & (FILE_READ_DATA|FILE_EXECUTE)) ?
63 GPFS_SHARE_READ : 0;
65 if (allow == GPFS_SHARE_NONE) {
66 DEBUG(10, ("special case am=no_access:%x\n",access_mask));
68 else {
69 deny |= (share_access & FILE_SHARE_WRITE) ?
70 0 : GPFS_DENY_WRITE;
71 deny |= (share_access & (FILE_SHARE_READ)) ?
72 0 : GPFS_DENY_READ;
74 DEBUG(10, ("am=%x, allow=%d, sa=%x, deny=%d\n",
75 access_mask, allow, share_access, deny));
77 result = gpfs_set_share_fn(fsp->fh->fd, allow, deny);
78 if (result != 0) {
79 if (errno == ENOSYS) {
80 DEBUG(5, ("VFS module vfs_gpfs loaded, but no gpfs "
81 "support has been compiled into Samba. Allowing access\n"));
82 return True;
83 } else {
84 DEBUG(10, ("gpfs_set_share failed: %s\n",
85 strerror(errno)));
89 return (result == 0);
92 int set_gpfs_lease(int fd, int leasetype)
94 int gpfs_type = GPFS_LEASE_NONE;
96 if (gpfs_set_lease_fn == NULL) {
97 errno = EINVAL;
98 return -1;
101 if (leasetype == F_RDLCK) {
102 gpfs_type = GPFS_LEASE_READ;
104 if (leasetype == F_WRLCK) {
105 gpfs_type = GPFS_LEASE_WRITE;
108 /* we unconditionally set CAP_LEASE, rather than looking for
109 -1/EACCES as there is a bug in some versions of
110 libgpfs_gpl.so which results in a leaked fd on /dev/ss0
111 each time we try this with the wrong capabilities set
113 linux_set_lease_capability();
114 return gpfs_set_lease_fn(fd, gpfs_type);
117 int smbd_gpfs_getacl(char *pathname, int flags, void *acl)
119 if (gpfs_getacl_fn == NULL) {
120 errno = ENOSYS;
121 return -1;
124 return gpfs_getacl_fn(pathname, flags, acl);
127 int smbd_gpfs_putacl(char *pathname, int flags, void *acl)
129 if (gpfs_putacl_fn == NULL) {
130 errno = ENOSYS;
131 return -1;
134 return gpfs_putacl_fn(pathname, flags, acl);
137 int smbd_gpfs_ftruncate(int fd, gpfs_off64_t length)
139 if (!gpfs_do_ftruncate || (gpfs_ftruncate_fn == NULL)) {
140 errno = ENOSYS;
141 return -1;
144 return gpfs_ftruncate_fn(fd, length);
147 int smbd_gpfs_get_realfilename_path(char *pathname, char *filenamep,
148 int *buflen)
150 if ((!gpfs_getrealfilename)
151 || (gpfs_get_realfilename_path_fn == NULL)) {
152 errno = ENOSYS;
153 return -1;
156 return gpfs_get_realfilename_path_fn(pathname, filenamep, buflen);
159 int get_gpfs_winattrs(char *pathname,struct gpfs_winattr *attrs)
162 if ((!gpfs_winattr) || (gpfs_get_winattrs_path_fn == NULL)) {
163 errno = ENOSYS;
164 return -1;
166 DEBUG(10, ("gpfs_get_winattrs_path:open call %s\n",pathname));
167 return gpfs_get_winattrs_path_fn(pathname, attrs);
170 int smbd_fget_gpfs_winattrs(int fd, struct gpfs_winattr *attrs)
173 if ((!gpfs_winattr) || (gpfs_get_winattrs_fn == NULL)) {
174 errno = ENOSYS;
175 return -1;
177 DEBUG(10, ("gpfs_get_winattrs_path:open call %d\n", fd));
178 return gpfs_get_winattrs_fn(fd, attrs);
181 int set_gpfs_winattrs(char *pathname,int flags,struct gpfs_winattr *attrs)
183 if ((!gpfs_winattr) || (gpfs_set_winattrs_path_fn == NULL)) {
184 errno = ENOSYS;
185 return -1;
188 DEBUG(10, ("gpfs_set_winattrs_path:open call %s\n",pathname));
189 return gpfs_set_winattrs_path_fn(pathname,flags, attrs);
192 void smbd_gpfs_lib_init()
194 if (gpfs_lib_init_fn) {
195 int rc = gpfs_lib_init_fn(0);
196 DEBUG(10, ("gpfs_lib_init() finished with rc %d "
197 "and errno %d\n", rc, errno));
198 } else {
199 DEBUG(10, ("libgpfs lacks gpfs_lib_init\n"));
203 static bool init_gpfs_function_lib(void *plibhandle_pointer,
204 const char *libname,
205 void *pfn_pointer, const char *fn_name)
207 bool did_open_here = false;
208 void **libhandle_pointer = (void **)plibhandle_pointer;
209 void **fn_pointer = (void **)pfn_pointer;
211 DEBUG(10, ("trying to load name %s from %s\n",
212 fn_name, libname));
214 if (*libhandle_pointer == NULL) {
215 *libhandle_pointer = dlopen(libname, RTLD_LAZY);
216 did_open_here = true;
218 if (*libhandle_pointer == NULL) {
219 DEBUG(10, ("Could not open lib %s\n", libname));
220 return false;
223 *fn_pointer = dlsym(*libhandle_pointer, fn_name);
224 if (*fn_pointer == NULL) {
225 DEBUG(10, ("Did not find symbol %s in lib %s\n",
226 fn_name, libname));
227 if (did_open_here) {
228 dlclose(*libhandle_pointer);
229 *libhandle_pointer = NULL;
231 return false;
234 return true;
237 static bool init_gpfs_function(void *fn_pointer, const char *fn_name)
239 static void *libgpfs_handle = NULL;
240 static void *libgpfs_gpl_handle = NULL;
242 if (init_gpfs_function_lib(&libgpfs_handle, "libgpfs.so",
243 fn_pointer, fn_name)) {
244 return true;
246 if (init_gpfs_function_lib(&libgpfs_gpl_handle, "libgpfs_gpl.so",
247 fn_pointer, fn_name)) {
248 return true;
250 return false;
253 void init_gpfs(void)
255 init_gpfs_function(&gpfs_set_share_fn, "gpfs_set_share");
256 init_gpfs_function(&gpfs_set_lease_fn, "gpfs_set_lease");
257 init_gpfs_function(&gpfs_getacl_fn, "gpfs_getacl");
258 init_gpfs_function(&gpfs_putacl_fn, "gpfs_putacl");
259 init_gpfs_function(&gpfs_get_realfilename_path_fn,
260 "gpfs_get_realfilename_path");
261 init_gpfs_function(&gpfs_get_winattrs_path_fn,"gpfs_get_winattrs_path");
262 init_gpfs_function(&gpfs_set_winattrs_path_fn,"gpfs_set_winattrs_path");
263 init_gpfs_function(&gpfs_get_winattrs_fn,"gpfs_get_winattrs");
264 init_gpfs_function(&gpfs_ftruncate_fn, "gpfs_ftruncate");
265 init_gpfs_function(&gpfs_lib_init_fn,"gpfs_lib_init");
267 gpfs_getrealfilename = lp_parm_bool(-1, "gpfs", "getrealfilename",
268 True);
269 gpfs_winattr = lp_parm_bool(-1, "gpfs", "winattr", False);
270 gpfs_do_ftruncate = lp_parm_bool(-1, "gpfs", "ftruncate", True);
272 return;