s4:dsdb: Fix stack use after scope in gkdi_create_root_key()
[samba.git] / source3 / modules / gpfs.c
1 /* 
2  *  Unix SMB/CIFS implementation.
3  *  Provide a connection to GPFS specific features
4  *  Copyright (C) Volker Lendecke 2005
5  *
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.
10  *  
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.
15  *  
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/>.
18  */
19
20 #include "includes.h"
21 #include "system/filesys.h"
22 #include "smbd/smbd.h"
23
24 #ifdef HAVE_GPFS
25
26 #include "libcli/security/security.h"
27 #include "gpfs_gpl.h"
28 #include "vfs_gpfs.h"
29
30 static bool gpfs_getrealfilename;
31 static bool gpfs_winattr;
32 static bool gpfs_do_ftruncate;
33
34 static int (*gpfs_set_share_fn)(int fd, unsigned int allow, unsigned int deny);
35 static int (*gpfs_set_lease_fn)(int fd, unsigned int leaseType);
36 static int (*gpfs_getacl_fn)(char *pathname, int flags, void *acl);
37 static int (*gpfs_putacl_fn)(char *pathname, int flags, void *acl);
38 static int (*gpfs_get_realfilename_path_fn)(char *pathname, char *filenamep,
39                                             int *buflen);
40 static int (*gpfs_set_winattrs_path_fn)(char *pathname, int flags, struct gpfs_winattr *attrs);
41 static int (*gpfs_get_winattrs_path_fn)(char *pathname, struct gpfs_winattr *attrs);
42 static int (*gpfs_get_winattrs_fn)(int fd, struct gpfs_winattr *attrs);
43 static int (*gpfs_ftruncate_fn)(int fd, gpfs_off64_t length);
44 static int (*gpfs_lib_init_fn)(int flags);
45
46 bool set_gpfs_sharemode(files_struct *fsp, uint32 access_mask,
47                         uint32 share_access)
48 {
49         unsigned int allow = GPFS_SHARE_NONE;
50         unsigned int deny = GPFS_DENY_NONE;
51         int result;
52
53         if (gpfs_set_share_fn == NULL) {
54                 return False;
55         }
56
57         if ((fsp == NULL) || (fsp->fh == NULL) || (fsp->fh->fd < 0)) {
58                 /* No real file, don't disturb */
59                 return True;
60         }
61
62         allow |= (access_mask & (FILE_WRITE_DATA|FILE_APPEND_DATA|
63                                  DELETE_ACCESS)) ? GPFS_SHARE_WRITE : 0;
64         allow |= (access_mask & (FILE_READ_DATA|FILE_EXECUTE)) ?
65                 GPFS_SHARE_READ : 0;
66
67         if (allow == GPFS_SHARE_NONE) {
68                 DEBUG(10, ("special case am=no_access:%x\n",access_mask));
69         }
70         else {  
71                 deny |= (share_access & FILE_SHARE_WRITE) ?
72                         0 : GPFS_DENY_WRITE;
73                 deny |= (share_access & (FILE_SHARE_READ)) ?
74                         0 : GPFS_DENY_READ;
75         }
76         DEBUG(10, ("am=%x, allow=%d, sa=%x, deny=%d\n",
77                    access_mask, allow, share_access, deny));
78
79         result = gpfs_set_share_fn(fsp->fh->fd, allow, deny);
80         if (result != 0) {
81                 if (errno == ENOSYS) {
82                         DEBUG(5, ("VFS module vfs_gpfs loaded, but no gpfs "
83                                   "support has been compiled into Samba. Allowing access\n"));
84                         return True;
85                 } else {
86                         DEBUG(10, ("gpfs_set_share failed: %s\n",
87                                    strerror(errno)));
88                 }
89         }
90
91         return (result == 0);
92 }
93
94 int set_gpfs_lease(int fd, int leasetype)
95 {
96         int gpfs_type = GPFS_LEASE_NONE;
97
98         if (gpfs_set_lease_fn == NULL) {
99                 errno = EINVAL;
100                 return -1;
101         }
102
103         if (leasetype == F_RDLCK) {
104                 gpfs_type = GPFS_LEASE_READ;
105         }
106         if (leasetype == F_WRLCK) {
107                 gpfs_type = GPFS_LEASE_WRITE;
108         }
109
110         /* we unconditionally set CAP_LEASE, rather than looking for
111            -1/EACCES as there is a bug in some versions of
112            libgpfs_gpl.so which results in a leaked fd on /dev/ss0
113            each time we try this with the wrong capabilities set
114         */
115         linux_set_lease_capability();
116         return gpfs_set_lease_fn(fd, gpfs_type);
117 }
118
119 int smbd_gpfs_getacl(char *pathname, int flags, void *acl)
120 {
121         if (gpfs_getacl_fn == NULL) {
122                 errno = ENOSYS;
123                 return -1;
124         }
125
126         return gpfs_getacl_fn(pathname, flags, acl);
127 }
128
129 int smbd_gpfs_putacl(char *pathname, int flags, void *acl)
130 {
131         if (gpfs_putacl_fn == NULL) {
132                 errno = ENOSYS;
133                 return -1;
134         }
135
136         return gpfs_putacl_fn(pathname, flags, acl);
137 }
138
139 int smbd_gpfs_ftruncate(int fd, gpfs_off64_t length)
140 {
141         if (!gpfs_do_ftruncate || (gpfs_ftruncate_fn == NULL)) {
142                 errno = ENOSYS;
143                 return -1;
144         }
145
146         return gpfs_ftruncate_fn(fd, length);
147 }
148
149 int smbd_gpfs_get_realfilename_path(char *pathname, char *filenamep,
150                                     int *buflen)
151 {
152         if ((!gpfs_getrealfilename)
153             || (gpfs_get_realfilename_path_fn == NULL)) {
154                 errno = ENOSYS;
155                 return -1;
156         }
157
158         return gpfs_get_realfilename_path_fn(pathname, filenamep, buflen);
159 }
160
161 int get_gpfs_winattrs(char *pathname,struct gpfs_winattr *attrs)
162 {
163
164         if ((!gpfs_winattr) || (gpfs_get_winattrs_path_fn == NULL)) {
165                 errno = ENOSYS;
166                 return -1;
167         }
168         DEBUG(10, ("gpfs_get_winattrs_path:open call %s\n",pathname));
169         return gpfs_get_winattrs_path_fn(pathname, attrs);
170 }
171
172 int smbd_fget_gpfs_winattrs(int fd, struct gpfs_winattr *attrs)
173 {
174
175         if ((!gpfs_winattr) || (gpfs_get_winattrs_fn == NULL)) {
176                 errno = ENOSYS;
177                 return -1;
178         }
179         DEBUG(10, ("gpfs_get_winattrs_path:open call %d\n", fd));
180         return gpfs_get_winattrs_fn(fd, attrs);
181 }
182
183 int set_gpfs_winattrs(char *pathname,int flags,struct gpfs_winattr *attrs)
184 {
185         if ((!gpfs_winattr) || (gpfs_set_winattrs_path_fn == NULL)) {
186                 errno = ENOSYS;
187                 return -1;
188         }
189
190         DEBUG(10, ("gpfs_set_winattrs_path:open call %s\n",pathname));
191         return gpfs_set_winattrs_path_fn(pathname,flags, attrs);
192 }
193
194 void smbd_gpfs_lib_init()
195 {
196         if (gpfs_lib_init_fn) {
197                 int rc = gpfs_lib_init_fn(0);
198                 DEBUG(10, ("gpfs_lib_init() finished with rc %d "
199                            "and errno %d\n", rc, errno));
200         } else {
201                 DEBUG(10, ("libgpfs lacks gpfs_lib_init\n"));
202         }
203 }
204
205 static bool init_gpfs_function_lib(void *plibhandle_pointer,
206                                    const char *libname,
207                                    void *pfn_pointer, const char *fn_name)
208 {
209         bool did_open_here = false;
210         void **libhandle_pointer = (void **)plibhandle_pointer;
211         void **fn_pointer = (void **)pfn_pointer;
212
213         DEBUG(10, ("trying to load name %s from %s\n",
214                    fn_name, libname));
215
216         if (*libhandle_pointer == NULL) {
217                 *libhandle_pointer = dlopen(libname, RTLD_LAZY);
218                 did_open_here = true;
219         }
220         if (*libhandle_pointer == NULL) {
221                 DEBUG(10, ("Could not open lib %s\n", libname));
222                 return false;
223         }
224
225         *fn_pointer = dlsym(*libhandle_pointer, fn_name);
226         if (*fn_pointer == NULL) {
227                 DEBUG(10, ("Did not find symbol %s in lib %s\n",
228                            fn_name, libname));
229                 if (did_open_here) {
230                         dlclose(*libhandle_pointer);
231                         *libhandle_pointer = NULL;
232                 }
233                 return false;
234         }
235
236         return true;
237 }
238
239 static bool init_gpfs_function(void *fn_pointer, const char *fn_name)
240 {
241         static void *libgpfs_handle = NULL;
242         static void *libgpfs_gpl_handle = NULL;
243
244         if (init_gpfs_function_lib(&libgpfs_handle, "libgpfs.so",
245                                    fn_pointer, fn_name)) {
246                 return true;
247         }
248         if (init_gpfs_function_lib(&libgpfs_gpl_handle, "libgpfs_gpl.so",
249                                    fn_pointer, fn_name)) {
250                 return true;
251         }
252         return false;
253 }
254
255 void init_gpfs(void)
256 {
257         init_gpfs_function(&gpfs_set_share_fn, "gpfs_set_share");
258         init_gpfs_function(&gpfs_set_lease_fn, "gpfs_set_lease");
259         init_gpfs_function(&gpfs_getacl_fn, "gpfs_getacl");
260         init_gpfs_function(&gpfs_putacl_fn, "gpfs_putacl");
261         init_gpfs_function(&gpfs_get_realfilename_path_fn,
262                            "gpfs_get_realfilename_path");
263         init_gpfs_function(&gpfs_get_winattrs_path_fn,"gpfs_get_winattrs_path");
264         init_gpfs_function(&gpfs_set_winattrs_path_fn,"gpfs_set_winattrs_path");
265         init_gpfs_function(&gpfs_get_winattrs_fn,"gpfs_get_winattrs");
266         init_gpfs_function(&gpfs_ftruncate_fn, "gpfs_ftruncate");
267         init_gpfs_function(&gpfs_lib_init_fn,"gpfs_lib_init");
268
269         gpfs_getrealfilename = lp_parm_bool(-1, "gpfs", "getrealfilename",
270                                             True);
271         gpfs_winattr = lp_parm_bool(-1, "gpfs", "winattr", False);
272         gpfs_do_ftruncate = lp_parm_bool(-1, "gpfs", "ftruncate", True);
273
274         return;
275 }
276
277 #else
278
279 int set_gpfs_lease(int snum, int leasetype)
280 {
281         DEBUG(0, ("'VFS module smbgpfs loaded, without gpfs support compiled\n"));
282
283         /* We need to indicate that no GPFS is around by returning ENOSYS, so
284          * that the normal linux kernel oplock code is called. */
285         errno = ENOSYS;
286         return -1;
287 }
288
289 bool set_gpfs_sharemode(files_struct *fsp, uint32 access_mask,
290                         uint32 share_access)
291 {
292         DEBUG(0, ("VFS module - smbgpfs.so loaded, without gpfs support compiled\n"));
293         /* Don't disturb but complain */
294         return True;
295 }
296
297 int smbd_gpfs_getacl(char *pathname, int flags, void *acl)
298 {
299         errno = ENOSYS;
300         return -1;
301 }
302
303 int smbd_gpfs_putacl(char *pathname, int flags, void *acl)
304 {
305         errno = ENOSYS;
306         return -1;
307 }
308
309 int smbd_gpfs_get_realfilename_path(char *pathname, char *fileamep,
310                                     int *buflen)
311 {
312         errno = ENOSYS;
313         return -1;
314 }
315
316 int set_gpfs_winattrs(char *pathname,int flags,struct gpfs_winattr *attrs)
317 {
318         errno = ENOSYS;
319         return -1;
320 }
321
322 int get_gpfs_winattrs(char *pathname,struct gpfs_winattr *attrs)
323 {
324         errno = ENOSYS;
325         return -1;
326 }
327
328 void smbd_gpfs_lib_init()
329 {
330         return;
331 }
332
333 void init_gpfs(void)
334 {
335         return;
336 }
337
338 #endif /* HAVE_GPFS */