smbd: detect EOF on stdin in --foreground mode
[Samba/gebeck_regimport.git] / source3 / modules / gpfs.c
blob5ce23810c428a8c761abddcb1ddb8b17a3b54c43
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 int (*gpfs_set_share_fn)(int fd, unsigned int allow, unsigned int deny);
29 static int (*gpfs_set_lease_fn)(int fd, unsigned int leaseType);
30 static int (*gpfs_getacl_fn)(char *pathname, int flags, void *acl);
31 static int (*gpfs_putacl_fn)(char *pathname, int flags, void *acl);
32 static int (*gpfs_get_realfilename_path_fn)(char *pathname, char *filenamep,
33 int *buflen);
34 static int (*gpfs_set_winattrs_path_fn)(char *pathname, int flags, struct gpfs_winattr *attrs);
35 static int (*gpfs_get_winattrs_path_fn)(char *pathname, struct gpfs_winattr *attrs);
36 static int (*gpfs_get_winattrs_fn)(int fd, struct gpfs_winattr *attrs);
37 static int (*gpfs_ftruncate_fn)(int fd, gpfs_off64_t length);
38 static int (*gpfs_lib_init_fn)(int flags);
40 bool set_gpfs_sharemode(files_struct *fsp, uint32 access_mask,
41 uint32 share_access)
43 unsigned int allow = GPFS_SHARE_NONE;
44 unsigned int deny = GPFS_DENY_NONE;
45 int result;
47 if (gpfs_set_share_fn == NULL) {
48 return False;
51 if ((fsp == NULL) || (fsp->fh == NULL) || (fsp->fh->fd < 0)) {
52 /* No real file, don't disturb */
53 return True;
56 allow |= (access_mask & (FILE_WRITE_DATA|FILE_APPEND_DATA|
57 DELETE_ACCESS)) ? GPFS_SHARE_WRITE : 0;
58 allow |= (access_mask & (FILE_READ_DATA|FILE_EXECUTE)) ?
59 GPFS_SHARE_READ : 0;
61 if (allow == GPFS_SHARE_NONE) {
62 DEBUG(10, ("special case am=no_access:%x\n",access_mask));
64 else {
65 deny |= (share_access & FILE_SHARE_WRITE) ?
66 0 : GPFS_DENY_WRITE;
67 deny |= (share_access & (FILE_SHARE_READ)) ?
68 0 : GPFS_DENY_READ;
70 DEBUG(10, ("am=%x, allow=%d, sa=%x, deny=%d\n",
71 access_mask, allow, share_access, deny));
73 result = gpfs_set_share_fn(fsp->fh->fd, allow, deny);
74 if (result != 0) {
75 if (errno == ENOSYS) {
76 DEBUG(5, ("VFS module vfs_gpfs loaded, but no gpfs "
77 "support has been compiled into Samba. Allowing access\n"));
78 return True;
79 } else {
80 DEBUG(10, ("gpfs_set_share failed: %s\n",
81 strerror(errno)));
85 return (result == 0);
88 int set_gpfs_lease(int fd, int leasetype)
90 int gpfs_type = GPFS_LEASE_NONE;
92 if (gpfs_set_lease_fn == NULL) {
93 errno = EINVAL;
94 return -1;
97 if (leasetype == F_RDLCK) {
98 gpfs_type = GPFS_LEASE_READ;
100 if (leasetype == F_WRLCK) {
101 gpfs_type = GPFS_LEASE_WRITE;
104 /* we unconditionally set CAP_LEASE, rather than looking for
105 -1/EACCES as there is a bug in some versions of
106 libgpfs_gpl.so which results in a leaked fd on /dev/ss0
107 each time we try this with the wrong capabilities set
109 linux_set_lease_capability();
110 return gpfs_set_lease_fn(fd, gpfs_type);
113 int smbd_gpfs_getacl(char *pathname, int flags, void *acl)
115 if (gpfs_getacl_fn == NULL) {
116 errno = ENOSYS;
117 return -1;
120 return gpfs_getacl_fn(pathname, flags, acl);
123 int smbd_gpfs_putacl(char *pathname, int flags, void *acl)
125 if (gpfs_putacl_fn == NULL) {
126 errno = ENOSYS;
127 return -1;
130 return gpfs_putacl_fn(pathname, flags, acl);
133 int smbd_gpfs_ftruncate(int fd, gpfs_off64_t length)
135 if (gpfs_ftruncate_fn == NULL) {
136 errno = ENOSYS;
137 return -1;
140 return gpfs_ftruncate_fn(fd, length);
143 int smbd_gpfs_get_realfilename_path(char *pathname, char *filenamep,
144 int *buflen)
146 if (gpfs_get_realfilename_path_fn == NULL) {
147 errno = ENOSYS;
148 return -1;
151 return gpfs_get_realfilename_path_fn(pathname, filenamep, buflen);
154 int get_gpfs_winattrs(char *pathname,struct gpfs_winattr *attrs)
157 if (gpfs_get_winattrs_path_fn == NULL) {
158 errno = ENOSYS;
159 return -1;
161 DEBUG(10, ("gpfs_get_winattrs_path:open call %s\n",pathname));
162 return gpfs_get_winattrs_path_fn(pathname, attrs);
165 int smbd_fget_gpfs_winattrs(int fd, struct gpfs_winattr *attrs)
168 if (gpfs_get_winattrs_fn == NULL) {
169 errno = ENOSYS;
170 return -1;
172 DEBUG(10, ("gpfs_get_winattrs_path:open call %d\n", fd));
173 return gpfs_get_winattrs_fn(fd, attrs);
176 int set_gpfs_winattrs(char *pathname,int flags,struct gpfs_winattr *attrs)
178 if (gpfs_set_winattrs_path_fn == NULL) {
179 errno = ENOSYS;
180 return -1;
183 DEBUG(10, ("gpfs_set_winattrs_path:open call %s\n",pathname));
184 return gpfs_set_winattrs_path_fn(pathname,flags, attrs);
187 void smbd_gpfs_lib_init()
189 if (gpfs_lib_init_fn) {
190 int rc = gpfs_lib_init_fn(0);
191 DEBUG(10, ("gpfs_lib_init() finished with rc %d "
192 "and errno %d\n", rc, errno));
193 } else {
194 DEBUG(10, ("libgpfs lacks gpfs_lib_init\n"));
198 static bool init_gpfs_function_lib(void *plibhandle_pointer,
199 const char *libname,
200 void *pfn_pointer, const char *fn_name)
202 bool did_open_here = false;
203 void **libhandle_pointer = (void **)plibhandle_pointer;
204 void **fn_pointer = (void **)pfn_pointer;
206 DEBUG(10, ("trying to load name %s from %s\n",
207 fn_name, libname));
209 if (*libhandle_pointer == NULL) {
210 *libhandle_pointer = dlopen(libname, RTLD_LAZY);
211 did_open_here = true;
213 if (*libhandle_pointer == NULL) {
214 DEBUG(10, ("Could not open lib %s\n", libname));
215 return false;
218 *fn_pointer = dlsym(*libhandle_pointer, fn_name);
219 if (*fn_pointer == NULL) {
220 DEBUG(10, ("Did not find symbol %s in lib %s\n",
221 fn_name, libname));
222 if (did_open_here) {
223 dlclose(*libhandle_pointer);
224 *libhandle_pointer = NULL;
226 return false;
229 return true;
232 static bool init_gpfs_function(void *fn_pointer, const char *fn_name)
234 static void *libgpfs_handle = NULL;
235 static void *libgpfs_gpl_handle = NULL;
237 if (init_gpfs_function_lib(&libgpfs_handle, "libgpfs.so",
238 fn_pointer, fn_name)) {
239 return true;
241 if (init_gpfs_function_lib(&libgpfs_gpl_handle, "libgpfs_gpl.so",
242 fn_pointer, fn_name)) {
243 return true;
245 return false;
248 void init_gpfs(void)
250 init_gpfs_function(&gpfs_set_share_fn, "gpfs_set_share");
251 init_gpfs_function(&gpfs_set_lease_fn, "gpfs_set_lease");
252 init_gpfs_function(&gpfs_getacl_fn, "gpfs_getacl");
253 init_gpfs_function(&gpfs_putacl_fn, "gpfs_putacl");
254 init_gpfs_function(&gpfs_get_realfilename_path_fn,
255 "gpfs_get_realfilename_path");
256 init_gpfs_function(&gpfs_get_winattrs_path_fn,"gpfs_get_winattrs_path");
257 init_gpfs_function(&gpfs_set_winattrs_path_fn,"gpfs_set_winattrs_path");
258 init_gpfs_function(&gpfs_get_winattrs_fn,"gpfs_get_winattrs");
259 init_gpfs_function(&gpfs_ftruncate_fn, "gpfs_ftruncate");
260 init_gpfs_function(&gpfs_lib_init_fn,"gpfs_lib_init");
262 return;