s3: VFS: gpfs: Remove gpfsacl_sys_acl_blob_get_file().
[samba.git] / source3 / modules / vfs_gpfs.c
1 /*
2  *  Unix SMB/CIFS implementation.
3  *  Samba VFS module for GPFS filesystem
4  *  Copyright (C) Christian Ambach <cambach1@de.ibm.com> 2006
5  *  Copyright (C) Christof Schmitt 2015
6  *  Major code contributions by Chetan Shringarpure <chetan.sh@in.ibm.com>
7  *                           and Gomati Mohanan <gomati.mohanan@in.ibm.com>
8  *
9  *  This program is free software; you can redistribute it and/or modify
10  *  it under the terms of the GNU General Public License as published by
11  *  the Free Software Foundation; either version 3 of the License, or
12  *  (at your option) any later version.
13  *
14  *  This program is distributed in the hope that it will be useful,
15  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
16  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  *  GNU General Public License for more details.
18  *
19  *  You should have received a copy of the GNU General Public License
20  *  along with this program; if not, see <http://www.gnu.org/licenses/>.
21  */
22
23 #include "includes.h"
24 #include "smbd/smbd.h"
25 #include "include/smbprofile.h"
26 #include "modules/non_posix_acls.h"
27 #include "libcli/security/security.h"
28 #include "nfs4_acls.h"
29 #include "system/filesys.h"
30 #include "auth.h"
31 #include "lib/util/tevent_unix.h"
32 #include "lib/util/gpfswrap.h"
33
34 #include <gnutls/gnutls.h>
35 #include <gnutls/crypto.h>
36 #include "lib/crypto/gnutls_helpers.h"
37
38 #undef DBGC_CLASS
39 #define DBGC_CLASS DBGC_VFS
40
41 #ifndef GPFS_GETACL_NATIVE
42 #define GPFS_GETACL_NATIVE 0x00000004
43 #endif
44
45 struct gpfs_config_data {
46         struct smbacl4_vfs_params nfs4_params;
47         bool sharemodes;
48         bool leases;
49         bool hsm;
50         bool syncio;
51         bool winattr;
52         bool ftruncate;
53         bool getrealfilename;
54         bool dfreequota;
55         bool acl;
56         bool settimes;
57         bool recalls;
58 };
59
60 struct gpfs_fsp_extension {
61         bool offline;
62 };
63
64 static inline unsigned int gpfs_acl_flags(gpfs_acl_t *gacl)
65 {
66         if (gacl->acl_level == GPFS_ACL_LEVEL_V4FLAGS) {
67                 return gacl->v4Level1.acl_flags;
68         }
69         return 0;
70 }
71
72 static inline gpfs_ace_v4_t *gpfs_ace_ptr(gpfs_acl_t *gacl, unsigned int i)
73 {
74         if (gacl->acl_level == GPFS_ACL_LEVEL_V4FLAGS) {
75                 return &gacl->v4Level1.ace_v4[i];
76         }
77         return &gacl->ace_v4[i];
78 }
79
80 static unsigned int vfs_gpfs_access_mask_to_allow(uint32_t access_mask)
81 {
82         unsigned int allow = GPFS_SHARE_NONE;
83
84         if (access_mask & (FILE_WRITE_DATA|FILE_APPEND_DATA)) {
85                 allow |= GPFS_SHARE_WRITE;
86         }
87         if (access_mask & (FILE_READ_DATA|FILE_EXECUTE)) {
88                 allow |= GPFS_SHARE_READ;
89         }
90
91         return allow;
92 }
93
94 static unsigned int vfs_gpfs_share_access_to_deny(uint32_t share_access)
95 {
96         unsigned int deny = GPFS_DENY_NONE;
97
98         if (!(share_access & FILE_SHARE_WRITE)) {
99                 deny |= GPFS_DENY_WRITE;
100         }
101         if (!(share_access & FILE_SHARE_READ)) {
102                 deny |= GPFS_DENY_READ;
103         }
104
105         /*
106          * GPFS_DENY_DELETE can only be set together with either
107          * GPFS_DENY_WRITE or GPFS_DENY_READ.
108          */
109         if ((deny & (GPFS_DENY_WRITE|GPFS_DENY_READ)) &&
110             !(share_access & FILE_SHARE_DELETE)) {
111                 deny |= GPFS_DENY_DELETE;
112         }
113
114         return deny;
115 }
116
117 static int set_gpfs_sharemode(files_struct *fsp, uint32_t access_mask,
118                               uint32_t share_access)
119 {
120         unsigned int allow = GPFS_SHARE_NONE;
121         unsigned int deny = GPFS_DENY_NONE;
122         int result;
123
124         if (access_mask == 0) {
125                 DBG_DEBUG("Clearing file system share mode.\n");
126         } else {
127                 allow = vfs_gpfs_access_mask_to_allow(access_mask);
128                 deny = vfs_gpfs_share_access_to_deny(share_access);
129         }
130         DBG_DEBUG("access_mask=0x%x, allow=0x%x, share_access=0x%x, "
131                   "deny=0x%x\n", access_mask, allow, share_access, deny);
132
133         result = gpfswrap_set_share(fsp_get_io_fd(fsp), allow, deny);
134         if (result == 0) {
135                 return 0;
136         }
137
138         if (errno == EACCES) {
139                 DBG_NOTICE("GPFS share mode denied for %s/%s.\n",
140                            fsp->conn->connectpath,
141                            fsp->fsp_name->base_name);
142         } else if (errno == EPERM) {
143                 DBG_ERR("Samba requested GPFS sharemode for %s/%s, but the "
144                         "GPFS file system is not configured accordingly. "
145                         "Configure file system with mmchfs -D nfs4 or "
146                         "set gpfs:sharemodes=no in Samba.\n",
147                         fsp->conn->connectpath,
148                         fsp->fsp_name->base_name);
149         } else {
150                 DBG_ERR("gpfs_set_share failed: %s\n", strerror(errno));
151         }
152
153         return result;
154 }
155
156 static int vfs_gpfs_kernel_flock(vfs_handle_struct *handle, files_struct *fsp,
157                                  uint32_t share_access, uint32_t access_mask)
158 {
159
160         struct gpfs_config_data *config;
161         int ret = 0;
162
163         START_PROFILE(syscall_kernel_flock);
164
165         SMB_VFS_HANDLE_GET_DATA(handle, config,
166                                 struct gpfs_config_data,
167                                 return -1);
168
169         if(!config->sharemodes) {
170                 return 0;
171         }
172
173         /*
174          * A named stream fsp will have the basefile open in the fsp
175          * fd, so lacking a distinct fd for the stream we have to skip
176          * kernel_flock and set_gpfs_sharemode for stream.
177          */
178         if (is_named_stream(fsp->fsp_name)) {
179                 DBG_NOTICE("Not requesting GPFS sharemode on stream: %s/%s\n",
180                            fsp->conn->connectpath,
181                            fsp_str_dbg(fsp));
182                 return 0;
183         }
184
185         kernel_flock(fsp_get_io_fd(fsp), share_access, access_mask);
186
187         ret = set_gpfs_sharemode(fsp, access_mask, share_access);
188
189         END_PROFILE(syscall_kernel_flock);
190
191         return ret;
192 }
193
194 static int vfs_gpfs_close(vfs_handle_struct *handle, files_struct *fsp)
195 {
196
197         struct gpfs_config_data *config;
198
199         SMB_VFS_HANDLE_GET_DATA(handle, config,
200                                 struct gpfs_config_data,
201                                 return -1);
202
203         if (config->sharemodes &&
204             (fsp->fsp_flags.kernel_share_modes_taken))
205         {
206                 /*
207                  * Always clear GPFS sharemode in case the actual
208                  * close gets deferred due to outstanding POSIX locks
209                  * (see fd_close_posix)
210                  */
211                 int ret = gpfswrap_set_share(fsp_get_io_fd(fsp), 0, 0);
212                 if (ret != 0) {
213                         DBG_ERR("Clearing GPFS sharemode on close failed for "
214                                 " %s/%s: %s\n",
215                                 fsp->conn->connectpath,
216                                 fsp->fsp_name->base_name,
217                                 strerror(errno));
218                 }
219         }
220
221         return SMB_VFS_NEXT_CLOSE(handle, fsp);
222 }
223
224 static int lease_type_to_gpfs(int leasetype)
225 {
226         if (leasetype == F_RDLCK) {
227                 return GPFS_LEASE_READ;
228         }
229
230         if (leasetype == F_WRLCK) {
231                 return GPFS_LEASE_WRITE;
232         }
233
234         return GPFS_LEASE_NONE;
235 }
236
237 static int vfs_gpfs_setlease(vfs_handle_struct *handle,
238                              files_struct *fsp,
239                              int leasetype)
240 {
241         struct gpfs_config_data *config;
242         int ret=0;
243
244         START_PROFILE(syscall_linux_setlease);
245
246         SMB_VFS_HANDLE_GET_DATA(handle, config,
247                                 struct gpfs_config_data,
248                                 return -1);
249
250         ret = linux_set_lease_sighandler(fsp_get_io_fd(fsp));
251         if (ret == -1) {
252                 goto failure;
253         }
254
255         if (config->leases) {
256                 int gpfs_lease_type = lease_type_to_gpfs(leasetype);
257                 int saved_errno = 0;
258
259                 /*
260                  * Ensure the lease owner is root to allow
261                  * correct delivery of lease-break signals.
262                  */
263                 become_root();
264                 ret = gpfswrap_set_lease(fsp_get_io_fd(fsp), gpfs_lease_type);
265                 if (ret < 0) {
266                         saved_errno = errno;
267                 }
268                 unbecome_root();
269
270                 if (saved_errno != 0) {
271                         errno = saved_errno;
272                 }
273         }
274
275 failure:
276         END_PROFILE(syscall_linux_setlease);
277
278         return ret;
279 }
280
281 static int vfs_gpfs_get_real_filename(struct vfs_handle_struct *handle,
282                                       const struct smb_filename *path,
283                                       const char *name,
284                                       TALLOC_CTX *mem_ctx,
285                                       char **found_name)
286 {
287         int result;
288         char *full_path = NULL;
289         char *to_free = NULL;
290         char real_pathname[PATH_MAX+1], tmpbuf[PATH_MAX];
291         size_t full_path_len;
292         int buflen;
293         bool mangled;
294         struct gpfs_config_data *config;
295
296         SMB_VFS_HANDLE_GET_DATA(handle, config,
297                                 struct gpfs_config_data,
298                                 return -1);
299
300         if (!config->getrealfilename) {
301                 return SMB_VFS_NEXT_GET_REAL_FILENAME(handle, path, name,
302                                                       mem_ctx, found_name);
303         }
304
305         mangled = mangle_is_mangled(name, handle->conn->params);
306         if (mangled) {
307                 return SMB_VFS_NEXT_GET_REAL_FILENAME(handle, path, name,
308                                                       mem_ctx, found_name);
309         }
310
311         full_path_len = full_path_tos(path->base_name, name,
312                                       tmpbuf, sizeof(tmpbuf),
313                                       &full_path, &to_free);
314         if (full_path_len == -1) {
315                 errno = ENOMEM;
316                 return -1;
317         }
318
319         buflen = sizeof(real_pathname) - 1;
320
321         result = gpfswrap_get_realfilename_path(full_path, real_pathname,
322                                                 &buflen);
323
324         TALLOC_FREE(to_free);
325
326         if ((result == -1) && (errno == ENOSYS)) {
327                 return SMB_VFS_NEXT_GET_REAL_FILENAME(
328                         handle, path, name, mem_ctx, found_name);
329         }
330
331         if (result == -1) {
332                 DEBUG(10, ("smbd_gpfs_get_realfilename_path returned %s\n",
333                            strerror(errno)));
334                 return -1;
335         }
336
337         /*
338          * GPFS does not necessarily null-terminate the returned path
339          * but instead returns the buffer length in buflen.
340          */
341
342         if (buflen < sizeof(real_pathname)) {
343                 real_pathname[buflen] = '\0';
344         } else {
345                 real_pathname[sizeof(real_pathname)-1] = '\0';
346         }
347
348         DEBUG(10, ("smbd_gpfs_get_realfilename_path: %s/%s -> %s\n",
349                    path->base_name, name, real_pathname));
350
351         name = strrchr_m(real_pathname, '/');
352         if (name == NULL) {
353                 errno = ENOENT;
354                 return -1;
355         }
356
357         *found_name = talloc_strdup(mem_ctx, name+1);
358         if (*found_name == NULL) {
359                 errno = ENOMEM;
360                 return -1;
361         }
362
363         return 0;
364 }
365
366 static void sd2gpfs_control(uint16_t control, struct gpfs_acl *gacl)
367 {
368         unsigned int gpfs_aclflags = 0;
369         control &= SEC_DESC_DACL_PROTECTED | SEC_DESC_SACL_PROTECTED |
370                 SEC_DESC_DACL_AUTO_INHERITED | SEC_DESC_SACL_AUTO_INHERITED |
371                 SEC_DESC_DACL_DEFAULTED | SEC_DESC_SACL_DEFAULTED |
372                 SEC_DESC_DACL_PRESENT | SEC_DESC_SACL_PRESENT;
373         gpfs_aclflags = control << 8;
374         if (!(control & SEC_DESC_DACL_PRESENT))
375                 gpfs_aclflags |= ACL4_FLAG_NULL_DACL;
376         if (!(control & SEC_DESC_SACL_PRESENT))
377                 gpfs_aclflags |= ACL4_FLAG_NULL_SACL;
378         gacl->acl_level = GPFS_ACL_LEVEL_V4FLAGS;
379         gacl->v4Level1.acl_flags = gpfs_aclflags;
380 }
381
382 static uint16_t gpfs2sd_control(unsigned int gpfs_aclflags)
383 {
384         uint16_t control = gpfs_aclflags >> 8;
385         control &= SEC_DESC_DACL_PROTECTED | SEC_DESC_SACL_PROTECTED |
386                 SEC_DESC_DACL_AUTO_INHERITED | SEC_DESC_SACL_AUTO_INHERITED |
387                 SEC_DESC_DACL_DEFAULTED | SEC_DESC_SACL_DEFAULTED |
388                 SEC_DESC_DACL_PRESENT | SEC_DESC_SACL_PRESENT;
389         control |= SEC_DESC_SELF_RELATIVE;
390         return control;
391 }
392
393 static void gpfs_dumpacl(int level, struct gpfs_acl *gacl)
394 {
395         gpfs_aclCount_t i;
396         if (gacl==NULL)
397         {
398                 DEBUG(0, ("gpfs acl is NULL\n"));
399                 return;
400         }
401
402         DEBUG(level, ("len: %d, level: %d, version: %d, nace: %d, "
403                       "control: %x\n",
404                       gacl->acl_len, gacl->acl_level, gacl->acl_version,
405                       gacl->acl_nace, gpfs_acl_flags(gacl)));
406
407         for(i=0; i<gacl->acl_nace; i++)
408         {
409                 struct gpfs_ace_v4 *gace = gpfs_ace_ptr(gacl, i);
410                 DEBUG(level, ("\tace[%d]: type:%d, flags:0x%x, mask:0x%x, "
411                               "iflags:0x%x, who:%u\n",
412                               i, gace->aceType, gace->aceFlags, gace->aceMask,
413                               gace->aceIFlags, gace->aceWho));
414         }
415 }
416
417 static int gpfs_getacl_with_capability(const char *fname, int flags, void *buf)
418 {
419         int ret, saved_errno;
420
421         set_effective_capability(DAC_OVERRIDE_CAPABILITY);
422
423         ret = gpfswrap_getacl(fname, flags, buf);
424         saved_errno = errno;
425
426         drop_effective_capability(DAC_OVERRIDE_CAPABILITY);
427
428         errno = saved_errno;
429         return ret;
430 }
431
432 /*
433  * get the ACL from GPFS, allocated on the specified mem_ctx
434  * internally retries when initial buffer was too small
435  *
436  * caller needs to cast result to either
437  * raw = yes: struct gpfs_opaque_acl
438  * raw = no: struct gpfs_acl
439  *
440  */
441 static void *vfs_gpfs_getacl(TALLOC_CTX *mem_ctx,
442                          const char *fname,
443                          const bool raw,
444                          const gpfs_aclType_t type)
445 {
446
447         void *aclbuf;
448         size_t size = 512;
449         int ret, flags;
450         unsigned int *len;
451         size_t struct_size;
452         bool use_capability = false;
453
454 again:
455
456         aclbuf = talloc_zero_size(mem_ctx, size);
457         if (aclbuf == NULL) {
458                 errno = ENOMEM;
459                 return NULL;
460         }
461
462         if (raw) {
463                 struct gpfs_opaque_acl *buf = (struct gpfs_opaque_acl *) aclbuf;
464                 buf->acl_type = type;
465                 flags = GPFS_GETACL_NATIVE;
466                 len = (unsigned int *) &(buf->acl_buffer_len);
467                 struct_size = sizeof(struct gpfs_opaque_acl);
468         } else {
469                 struct gpfs_acl *buf = (struct gpfs_acl *) aclbuf;
470                 buf->acl_type = type;
471                 buf->acl_level = GPFS_ACL_LEVEL_V4FLAGS;
472                 flags = GPFS_GETACL_STRUCT;
473                 len = &(buf->acl_len);
474                 /* reserve space for control flags in gpfs 3.5 and beyond */
475                 struct_size = sizeof(struct gpfs_acl) + sizeof(unsigned int);
476         }
477
478         /* set the length of the buffer as input value */
479         *len = size;
480
481         if (use_capability) {
482                 ret = gpfs_getacl_with_capability(fname, flags, aclbuf);
483         } else {
484                 ret = gpfswrap_getacl(fname, flags, aclbuf);
485                 if ((ret != 0) && (errno == EACCES)) {
486                         DBG_DEBUG("Retry with DAC capability for %s\n", fname);
487                         use_capability = true;
488                         ret = gpfs_getacl_with_capability(fname, flags, aclbuf);
489                 }
490         }
491
492         if ((ret != 0) && (errno == ENOSPC)) {
493                 /*
494                  * get the size needed to accommodate the complete buffer
495                  *
496                  * the value returned only applies to the ACL blob in the
497                  * struct so make sure to also have headroom for the first
498                  * struct members by adding room for the complete struct
499                  * (might be a few bytes too much then)
500                  */
501                 size = *len + struct_size;
502                 talloc_free(aclbuf);
503                 DEBUG(10, ("Increasing ACL buffer size to %zu\n", size));
504                 goto again;
505         }
506
507         if (ret != 0) {
508                 DEBUG(5, ("smbd_gpfs_getacl failed with %s\n",
509                           strerror(errno)));
510                 talloc_free(aclbuf);
511                 return NULL;
512         }
513
514         return aclbuf;
515 }
516
517 /* Tries to get nfs4 acls and returns SMB ACL allocated.
518  * On failure returns 1 if it got non-NFSv4 ACL to prompt 
519  * retry with POSIX ACL checks.
520  * On failure returns -1 if there is system (GPFS) error, check errno.
521  * Returns 0 on success
522  */
523 static int gpfs_get_nfs4_acl(TALLOC_CTX *mem_ctx, const char *fname,
524                              struct SMB4ACL_T **ppacl)
525 {
526         gpfs_aclCount_t i;
527         struct gpfs_acl *gacl = NULL;
528         DEBUG(10, ("gpfs_get_nfs4_acl invoked for %s\n", fname));
529
530         /* Get the ACL */
531         gacl = (struct gpfs_acl*) vfs_gpfs_getacl(talloc_tos(), fname,
532                                                   false, 0);
533         if (gacl == NULL) {
534                 DEBUG(9, ("gpfs_getacl failed for %s with %s\n",
535                            fname, strerror(errno)));
536                 if (errno == ENODATA) {
537                         /*
538                          * GPFS returns ENODATA for snapshot
539                          * directories. Retry with POSIX ACLs check.
540                          */
541                         return 1;
542                 }
543
544                 return -1;
545         }
546
547         if (gacl->acl_type != GPFS_ACL_TYPE_NFS4) {
548                 DEBUG(10, ("Got non-nfsv4 acl\n"));
549                 /* Retry with POSIX ACLs check */
550                 talloc_free(gacl);
551                 return 1;
552         }
553
554         *ppacl = smb_create_smb4acl(mem_ctx);
555
556         if (gacl->acl_level == GPFS_ACL_LEVEL_V4FLAGS) {
557                 uint16_t control = gpfs2sd_control(gpfs_acl_flags(gacl));
558                 smbacl4_set_controlflags(*ppacl, control);
559         }
560
561         DEBUG(10, ("len: %d, level: %d, version: %d, nace: %d, control: %x\n",
562                    gacl->acl_len, gacl->acl_level, gacl->acl_version,
563                    gacl->acl_nace, gpfs_acl_flags(gacl)));
564
565         for (i=0; i<gacl->acl_nace; i++) {
566                 struct gpfs_ace_v4 *gace = gpfs_ace_ptr(gacl, i);
567                 SMB_ACE4PROP_T smbace = { 0 };
568                 DEBUG(10, ("type: %d, iflags: %x, flags: %x, mask: %x, "
569                            "who: %d\n", gace->aceType, gace->aceIFlags,
570                            gace->aceFlags, gace->aceMask, gace->aceWho));
571
572                 if (gace->aceIFlags & ACE4_IFLAG_SPECIAL_ID) {
573                         smbace.flags |= SMB_ACE4_ID_SPECIAL;
574                         switch (gace->aceWho) {
575                         case ACE4_SPECIAL_OWNER:
576                                 smbace.who.special_id = SMB_ACE4_WHO_OWNER;
577                                 break;
578                         case ACE4_SPECIAL_GROUP:
579                                 smbace.who.special_id = SMB_ACE4_WHO_GROUP;
580                                 break;
581                         case ACE4_SPECIAL_EVERYONE:
582                                 smbace.who.special_id = SMB_ACE4_WHO_EVERYONE;
583                                 break;
584                         default:
585                                 DEBUG(8, ("invalid special gpfs id %d "
586                                           "ignored\n", gace->aceWho));
587                                 continue; /* don't add it */
588                         }
589                 } else {
590                         if (gace->aceFlags & ACE4_FLAG_GROUP_ID)
591                                 smbace.who.gid = gace->aceWho;
592                         else
593                                 smbace.who.uid = gace->aceWho;
594                 }
595
596                 /* remove redundant deny entries */
597                 if (i > 0 && gace->aceType == SMB_ACE4_ACCESS_DENIED_ACE_TYPE) {
598                         struct gpfs_ace_v4 *prev = gpfs_ace_ptr(gacl, i - 1);
599                         if (prev->aceType == SMB_ACE4_ACCESS_ALLOWED_ACE_TYPE &&
600                             prev->aceFlags == gace->aceFlags &&
601                             prev->aceIFlags == gace->aceIFlags &&
602                             (gace->aceMask & prev->aceMask) == 0 &&
603                             gace->aceWho == prev->aceWho) {
604                                 /* it's redundant - skip it */
605                                 continue;
606                         }
607                 }
608
609                 smbace.aceType = gace->aceType;
610                 smbace.aceFlags = gace->aceFlags;
611                 smbace.aceMask = gace->aceMask;
612                 smb_add_ace4(*ppacl, &smbace);
613         }
614
615         talloc_free(gacl);
616
617         return 0;
618 }
619
620 static NTSTATUS gpfsacl_fget_nt_acl(vfs_handle_struct *handle,
621         files_struct *fsp, uint32_t security_info,
622         TALLOC_CTX *mem_ctx,
623         struct security_descriptor **ppdesc)
624 {
625         struct SMB4ACL_T *pacl = NULL;
626         int     result;
627         struct gpfs_config_data *config;
628         TALLOC_CTX *frame = talloc_stackframe();
629         NTSTATUS status;
630
631         *ppdesc = NULL;
632
633         SMB_VFS_HANDLE_GET_DATA(handle, config,
634                                 struct gpfs_config_data,
635                                 return NT_STATUS_INTERNAL_ERROR);
636
637         if (!config->acl) {
638                 status = SMB_VFS_NEXT_FGET_NT_ACL(handle, fsp, security_info,
639                                                   mem_ctx, ppdesc);
640                 TALLOC_FREE(frame);
641                 return status;
642         }
643
644         result = gpfs_get_nfs4_acl(frame, fsp->fsp_name->base_name, &pacl);
645
646         if (result == 0) {
647                 status = smb_fget_nt_acl_nfs4(fsp, &config->nfs4_params,
648                                               security_info,
649                                               mem_ctx, ppdesc, pacl);
650                 TALLOC_FREE(frame);
651                 return status;
652         }
653
654         if (result > 0) {
655                 DEBUG(10, ("retrying with posix acl...\n"));
656                 status = posix_fget_nt_acl(fsp, security_info,
657                                            mem_ctx, ppdesc);
658                 TALLOC_FREE(frame);
659                 return status;
660         }
661
662         TALLOC_FREE(frame);
663
664         /* GPFS ACL was not read, something wrong happened, error code is set in errno */
665         return map_nt_error_from_unix(errno);
666 }
667
668 static bool vfs_gpfs_nfs4_ace_to_gpfs_ace(SMB_ACE4PROP_T *nfs4_ace,
669                                           struct gpfs_ace_v4 *gace,
670                                           uid_t owner_uid)
671 {
672         gace->aceType = nfs4_ace->aceType;
673         gace->aceFlags = nfs4_ace->aceFlags;
674         gace->aceMask = nfs4_ace->aceMask;
675
676         if (nfs4_ace->flags & SMB_ACE4_ID_SPECIAL) {
677                 switch(nfs4_ace->who.special_id) {
678                 case SMB_ACE4_WHO_EVERYONE:
679                         gace->aceIFlags = ACE4_IFLAG_SPECIAL_ID;
680                         gace->aceWho = ACE4_SPECIAL_EVERYONE;
681                         break;
682                 case SMB_ACE4_WHO_OWNER:
683                         /*
684                          * With GPFS it is not possible to deny ACL or
685                          * attribute access to the owner. Setting an
686                          * ACL with such an entry is not possible.
687                          * Denying ACL or attribute access for the
688                          * owner through a named ACL entry can be
689                          * stored in an ACL, it is just not effective.
690                          *
691                          * Map this case to a named entry to allow at
692                          * least setting this ACL, which will be
693                          * enforced by the smbd permission check. Do
694                          * not do this for an inheriting OWNER entry,
695                          * as this represents a CREATOR OWNER ACE. The
696                          * remaining limitation is that CREATOR OWNER
697                          * cannot deny ACL or attribute access.
698                          */
699                         if (!nfs_ace_is_inherit(nfs4_ace) &&
700                             nfs4_ace->aceType ==
701                                         SMB_ACE4_ACCESS_DENIED_ACE_TYPE &&
702                             nfs4_ace->aceMask & (SMB_ACE4_READ_ATTRIBUTES|
703                                                  SMB_ACE4_WRITE_ATTRIBUTES|
704                                                  SMB_ACE4_READ_ACL|
705                                                  SMB_ACE4_WRITE_ACL)) {
706                                 gace->aceIFlags = 0;
707                                 gace->aceWho = owner_uid;
708                         } else {
709                                 gace->aceIFlags = ACE4_IFLAG_SPECIAL_ID;
710                                 gace->aceWho = ACE4_SPECIAL_OWNER;
711                         }
712                         break;
713                 case SMB_ACE4_WHO_GROUP:
714                         gace->aceIFlags = ACE4_IFLAG_SPECIAL_ID;
715                         gace->aceWho = ACE4_SPECIAL_GROUP;
716                         break;
717                 default:
718                         DBG_WARNING("Unsupported special_id %d\n",
719                                     nfs4_ace->who.special_id);
720                         return false;
721                 }
722
723                 return true;
724         }
725
726         gace->aceIFlags = 0;
727         gace->aceWho = (nfs4_ace->aceFlags & SMB_ACE4_IDENTIFIER_GROUP) ?
728                 nfs4_ace->who.gid : nfs4_ace->who.uid;
729
730         return true;
731 }
732
733 static struct gpfs_acl *vfs_gpfs_smbacl2gpfsacl(TALLOC_CTX *mem_ctx,
734                                                 files_struct *fsp,
735                                                 struct SMB4ACL_T *smbacl,
736                                                 bool controlflags)
737 {
738         struct gpfs_acl *gacl;
739         gpfs_aclLen_t gacl_len;
740         struct SMB4ACE_T *smbace;
741
742         gacl_len = offsetof(gpfs_acl_t, ace_v4) + sizeof(unsigned int)
743                 + smb_get_naces(smbacl) * sizeof(gpfs_ace_v4_t);
744
745         gacl = (struct gpfs_acl *)TALLOC_SIZE(mem_ctx, gacl_len);
746         if (gacl == NULL) {
747                 DEBUG(0, ("talloc failed\n"));
748                 errno = ENOMEM;
749                 return NULL;
750         }
751
752         gacl->acl_level = GPFS_ACL_LEVEL_BASE;
753         gacl->acl_version = GPFS_ACL_VERSION_NFS4;
754         gacl->acl_type = GPFS_ACL_TYPE_NFS4;
755         gacl->acl_nace = 0; /* change later... */
756
757         if (controlflags) {
758                 gacl->acl_level = GPFS_ACL_LEVEL_V4FLAGS;
759                 sd2gpfs_control(smbacl4_get_controlflags(smbacl), gacl);
760         }
761
762         for (smbace=smb_first_ace4(smbacl); smbace!=NULL; smbace = smb_next_ace4(smbace)) {
763                 struct gpfs_ace_v4 *gace = gpfs_ace_ptr(gacl, gacl->acl_nace);
764                 SMB_ACE4PROP_T  *aceprop = smb_get_ace4(smbace);
765                 bool add_ace;
766
767                 add_ace = vfs_gpfs_nfs4_ace_to_gpfs_ace(aceprop, gace,
768                                                         fsp->fsp_name->st.st_ex_uid);
769                 if (!add_ace) {
770                         continue;
771                 }
772
773                 gacl->acl_nace++;
774         }
775         gacl->acl_len = (char *)gpfs_ace_ptr(gacl, gacl->acl_nace)
776                 - (char *)gacl;
777         return gacl;
778 }
779
780 static bool gpfsacl_process_smbacl(vfs_handle_struct *handle,
781                                    files_struct *fsp,
782                                    struct SMB4ACL_T *smbacl)
783 {
784         int ret;
785         struct gpfs_acl *gacl;
786         TALLOC_CTX *mem_ctx = talloc_tos();
787
788         gacl = vfs_gpfs_smbacl2gpfsacl(mem_ctx, fsp, smbacl, true);
789         if (gacl == NULL) { /* out of memory */
790                 return False;
791         }
792         ret = gpfswrap_putacl(fsp->fsp_name->base_name,
793                               GPFS_PUTACL_STRUCT | GPFS_ACL_SAMBA, gacl);
794
795         if ((ret != 0) && (errno == EINVAL)) {
796                 DEBUG(10, ("Retry without nfs41 control flags\n"));
797                 talloc_free(gacl);
798                 gacl = vfs_gpfs_smbacl2gpfsacl(mem_ctx, fsp, smbacl, false);
799                 if (gacl == NULL) { /* out of memory */
800                         return False;
801                 }
802                 ret = gpfswrap_putacl(fsp->fsp_name->base_name,
803                                       GPFS_PUTACL_STRUCT | GPFS_ACL_SAMBA,
804                                       gacl);
805         }
806
807         if (ret != 0) {
808                 DEBUG(8, ("gpfs_putacl failed with %s\n", strerror(errno)));
809                 gpfs_dumpacl(8, gacl);
810                 return False;
811         }
812
813         DEBUG(10, ("gpfs_putacl succeeded\n"));
814         return True;
815 }
816
817 static NTSTATUS gpfsacl_set_nt_acl_internal(vfs_handle_struct *handle, files_struct *fsp, uint32_t security_info_sent, const struct security_descriptor *psd)
818 {
819         struct gpfs_acl *acl;
820         NTSTATUS result = NT_STATUS_ACCESS_DENIED;
821
822         acl = (struct gpfs_acl*) vfs_gpfs_getacl(talloc_tos(),
823                                                  fsp->fsp_name->base_name,
824                                                  false, 0);
825         if (acl == NULL) {
826                 return map_nt_error_from_unix(errno);
827         }
828
829         if (acl->acl_version == GPFS_ACL_VERSION_NFS4) {
830                 struct gpfs_config_data *config;
831
832                 if (lp_parm_bool(fsp->conn->params->service, "gpfs",
833                                  "refuse_dacl_protected", false)
834                     && (psd->type&SEC_DESC_DACL_PROTECTED)) {
835                         DEBUG(2, ("Rejecting unsupported ACL with DACL_PROTECTED bit set\n"));
836                         talloc_free(acl);
837                         return NT_STATUS_NOT_SUPPORTED;
838                 }
839
840                 SMB_VFS_HANDLE_GET_DATA(handle, config,
841                                         struct gpfs_config_data,
842                                         return NT_STATUS_INTERNAL_ERROR);
843
844                 result = smb_set_nt_acl_nfs4(handle,
845                         fsp, &config->nfs4_params, security_info_sent, psd,
846                         gpfsacl_process_smbacl);
847         } else { /* assume POSIX ACL - by default... */
848                 result = set_nt_acl(fsp, security_info_sent, psd);
849         }
850
851         talloc_free(acl);
852         return result;
853 }
854
855 static NTSTATUS gpfsacl_fset_nt_acl(vfs_handle_struct *handle, files_struct *fsp, uint32_t security_info_sent, const struct security_descriptor *psd)
856 {
857         struct gpfs_config_data *config;
858
859         SMB_VFS_HANDLE_GET_DATA(handle, config,
860                                 struct gpfs_config_data,
861                                 return NT_STATUS_INTERNAL_ERROR);
862
863         if (!config->acl) {
864                 return SMB_VFS_NEXT_FSET_NT_ACL(handle, fsp, security_info_sent, psd);
865         }
866
867         return gpfsacl_set_nt_acl_internal(handle, fsp, security_info_sent, psd);
868 }
869
870 static SMB_ACL_T gpfs2smb_acl(const struct gpfs_acl *pacl, TALLOC_CTX *mem_ctx)
871 {
872         SMB_ACL_T result;
873         gpfs_aclCount_t i;
874
875         result = sys_acl_init(mem_ctx);
876         if (result == NULL) {
877                 errno = ENOMEM;
878                 return NULL;
879         }
880
881         result->count = pacl->acl_nace;
882         result->acl = talloc_realloc(result, result->acl, struct smb_acl_entry,
883                                      result->count);
884         if (result->acl == NULL) {
885                 TALLOC_FREE(result);
886                 errno = ENOMEM;
887                 return NULL;
888         }
889
890         for (i=0; i<pacl->acl_nace; i++) {
891                 struct smb_acl_entry *ace = &result->acl[i];
892                 const struct gpfs_ace_v1 *g_ace = &pacl->ace_v1[i];
893
894                 DEBUG(10, ("Converting type %d id %lu perm %x\n",
895                            (int)g_ace->ace_type, (unsigned long)g_ace->ace_who,
896                            (int)g_ace->ace_perm));
897
898                 switch (g_ace->ace_type) {
899                 case GPFS_ACL_USER:
900                         ace->a_type = SMB_ACL_USER;
901                         ace->info.user.uid = (uid_t)g_ace->ace_who;
902                         break;
903                 case GPFS_ACL_USER_OBJ:
904                         ace->a_type = SMB_ACL_USER_OBJ;
905                         break;
906                 case GPFS_ACL_GROUP:
907                         ace->a_type = SMB_ACL_GROUP;
908                         ace->info.group.gid = (gid_t)g_ace->ace_who;
909                         break;
910                 case GPFS_ACL_GROUP_OBJ:
911                         ace->a_type = SMB_ACL_GROUP_OBJ;
912                         break;
913                 case GPFS_ACL_OTHER:
914                         ace->a_type = SMB_ACL_OTHER;
915                         break;
916                 case GPFS_ACL_MASK:
917                         ace->a_type = SMB_ACL_MASK;
918                         break;
919                 default:
920                         DEBUG(10, ("Got invalid ace_type: %d\n",
921                                    g_ace->ace_type));
922                         TALLOC_FREE(result);
923                         errno = EINVAL;
924                         return NULL;
925                 }
926
927                 ace->a_perm = 0;
928                 ace->a_perm |= (g_ace->ace_perm & ACL_PERM_READ) ?
929                         SMB_ACL_READ : 0;
930                 ace->a_perm |= (g_ace->ace_perm & ACL_PERM_WRITE) ?
931                         SMB_ACL_WRITE : 0;
932                 ace->a_perm |= (g_ace->ace_perm & ACL_PERM_EXECUTE) ?
933                         SMB_ACL_EXECUTE : 0;
934
935                 DEBUGADD(10, ("Converted to %d perm %x\n",
936                               ace->a_type, ace->a_perm));
937         }
938
939         return result;
940 }
941
942 static SMB_ACL_T gpfsacl_get_posix_acl(const char *path, gpfs_aclType_t type,
943                                        TALLOC_CTX *mem_ctx)
944 {
945         struct gpfs_acl *pacl;
946         SMB_ACL_T result = NULL;
947
948         pacl = vfs_gpfs_getacl(talloc_tos(), path, false, type);
949
950         if (pacl == NULL) {
951                 DEBUG(10, ("vfs_gpfs_getacl failed for %s with %s\n",
952                            path, strerror(errno)));
953                 if (errno == 0) {
954                         errno = EINVAL;
955                 }
956                 goto done;
957         }
958
959         if (pacl->acl_version != GPFS_ACL_VERSION_POSIX) {
960                 DEBUG(10, ("Got acl version %d, expected %d\n",
961                            pacl->acl_version, GPFS_ACL_VERSION_POSIX));
962                 errno = EINVAL;
963                 goto done;
964         }
965
966         DEBUG(10, ("len: %d, level: %d, version: %d, nace: %d\n",
967                    pacl->acl_len, pacl->acl_level, pacl->acl_version,
968                    pacl->acl_nace));
969
970         result = gpfs2smb_acl(pacl, mem_ctx);
971         if (result != NULL) {
972                 errno = 0;
973         }
974
975  done:
976
977         if (pacl != NULL) {
978                 talloc_free(pacl);
979         }
980         if (errno != 0) {
981                 TALLOC_FREE(result);
982         }
983         return result;
984 }
985
986 static SMB_ACL_T gpfsacl_sys_acl_get_fd(vfs_handle_struct *handle,
987                                         files_struct *fsp,
988                                         SMB_ACL_TYPE_T type,
989                                         TALLOC_CTX *mem_ctx)
990 {
991         gpfs_aclType_t gpfs_type;
992         struct gpfs_config_data *config;
993
994         SMB_VFS_HANDLE_GET_DATA(handle, config,
995                                 struct gpfs_config_data,
996                                 return NULL);
997
998         if (!config->acl) {
999                 return SMB_VFS_NEXT_SYS_ACL_GET_FD(handle, fsp, type, mem_ctx);
1000         }
1001
1002         switch(type) {
1003         case SMB_ACL_TYPE_ACCESS:
1004                 gpfs_type = GPFS_ACL_TYPE_ACCESS;
1005                 break;
1006         case SMB_ACL_TYPE_DEFAULT:
1007                 gpfs_type = GPFS_ACL_TYPE_DEFAULT;
1008                 break;
1009         default:
1010                 DEBUG(0, ("Got invalid type: %d\n", type));
1011                 smb_panic("exiting");
1012         }
1013         return gpfsacl_get_posix_acl(fsp->fsp_name->base_name,
1014                                      gpfs_type, mem_ctx);
1015 }
1016
1017 static int gpfsacl_sys_acl_blob_get_fd(vfs_handle_struct *handle,
1018                                       files_struct *fsp,
1019                                       TALLOC_CTX *mem_ctx,
1020                                       char **blob_description,
1021                                       DATA_BLOB *blob)
1022 {
1023         struct gpfs_config_data *config;
1024         struct gpfs_opaque_acl *acl = NULL;
1025         DATA_BLOB aclblob;
1026         int result;
1027
1028         SMB_VFS_HANDLE_GET_DATA(handle, config,
1029                                 struct gpfs_config_data,
1030                                 return -1);
1031
1032         if (!config->acl) {
1033                 return SMB_VFS_NEXT_SYS_ACL_BLOB_GET_FD(handle, fsp, mem_ctx,
1034                                                         blob_description, blob);
1035         }
1036
1037         errno = 0;
1038         acl = (struct gpfs_opaque_acl *) vfs_gpfs_getacl(mem_ctx,
1039                                                 fsp->fsp_name->base_name,
1040                                                 true,
1041                                                 GPFS_ACL_TYPE_NFS4);
1042
1043         if (errno) {
1044                 DEBUG(5, ("vfs_gpfs_getacl finished with errno %d: %s\n",
1045                                         errno, strerror(errno)));
1046
1047                 /* EINVAL means POSIX ACL, bail out on other cases */
1048                 if (errno != EINVAL) {
1049                         return -1;
1050                 }
1051         }
1052
1053         if (acl != NULL) {
1054                 /*
1055                  * file has NFSv4 ACL
1056                  *
1057                  * we only need the actual ACL blob here
1058                  * acl_version will always be NFS4 because we asked
1059                  * for NFS4
1060                  * acl_type is only used for POSIX ACLs
1061                  */
1062                 aclblob.data = (uint8_t*) acl->acl_var_data;
1063                 aclblob.length = acl->acl_buffer_len;
1064
1065                 *blob_description = talloc_strdup(mem_ctx, "gpfs_nfs4_acl");
1066                 if (!*blob_description) {
1067                         talloc_free(acl);
1068                         errno = ENOMEM;
1069                         return -1;
1070                 }
1071
1072                 result = non_posix_sys_acl_blob_get_fd_helper(handle, fsp,
1073                                                               aclblob, mem_ctx,
1074                                                               blob);
1075
1076                 talloc_free(acl);
1077                 return result;
1078         }
1079
1080         /* fall back to POSIX ACL */
1081         return posix_sys_acl_blob_get_fd(handle, fsp, mem_ctx,
1082                                          blob_description, blob);
1083 }
1084
1085 static struct gpfs_acl *smb2gpfs_acl(const SMB_ACL_T pacl,
1086                                      SMB_ACL_TYPE_T type)
1087 {
1088         gpfs_aclLen_t len;
1089         struct gpfs_acl *result;
1090         int i;
1091
1092         DEBUG(10, ("smb2gpfs_acl: Got ACL with %d entries\n", pacl->count));
1093
1094         len = offsetof(gpfs_acl_t, ace_v1) + (pacl->count) *
1095                 sizeof(gpfs_ace_v1_t);
1096
1097         result = (struct gpfs_acl *)SMB_MALLOC(len);
1098         if (result == NULL) {
1099                 errno = ENOMEM;
1100                 return result;
1101         }
1102
1103         result->acl_len = len;
1104         result->acl_level = 0;
1105         result->acl_version = GPFS_ACL_VERSION_POSIX;
1106         result->acl_type = (type == SMB_ACL_TYPE_DEFAULT) ?
1107                 GPFS_ACL_TYPE_DEFAULT : GPFS_ACL_TYPE_ACCESS;
1108         result->acl_nace = pacl->count;
1109
1110         for (i=0; i<pacl->count; i++) {
1111                 const struct smb_acl_entry *ace = &pacl->acl[i];
1112                 struct gpfs_ace_v1 *g_ace = &result->ace_v1[i];
1113
1114                 DEBUG(10, ("Converting type %d perm %x\n",
1115                            (int)ace->a_type, (int)ace->a_perm));
1116
1117                 g_ace->ace_perm = 0;
1118
1119                 switch(ace->a_type) {
1120                 case SMB_ACL_USER:
1121                         g_ace->ace_type = GPFS_ACL_USER;
1122                         g_ace->ace_who = (gpfs_uid_t)ace->info.user.uid;
1123                         break;
1124                 case SMB_ACL_USER_OBJ:
1125                         g_ace->ace_type = GPFS_ACL_USER_OBJ;
1126                         g_ace->ace_perm |= ACL_PERM_CONTROL;
1127                         g_ace->ace_who = 0;
1128                         break;
1129                 case SMB_ACL_GROUP:
1130                         g_ace->ace_type = GPFS_ACL_GROUP;
1131                         g_ace->ace_who = (gpfs_uid_t)ace->info.group.gid;
1132                         break;
1133                 case SMB_ACL_GROUP_OBJ:
1134                         g_ace->ace_type = GPFS_ACL_GROUP_OBJ;
1135                         g_ace->ace_who = 0;
1136                         break;
1137                 case SMB_ACL_MASK:
1138                         g_ace->ace_type = GPFS_ACL_MASK;
1139                         g_ace->ace_perm = 0x8f;
1140                         g_ace->ace_who = 0;
1141                         break;
1142                 case SMB_ACL_OTHER:
1143                         g_ace->ace_type = GPFS_ACL_OTHER;
1144                         g_ace->ace_who = 0;
1145                         break;
1146                 default:
1147                         DEBUG(10, ("Got invalid ace_type: %d\n", ace->a_type));
1148                         errno = EINVAL;
1149                         SAFE_FREE(result);
1150                         return NULL;
1151                 }
1152
1153                 g_ace->ace_perm |= (ace->a_perm & SMB_ACL_READ) ?
1154                         ACL_PERM_READ : 0;
1155                 g_ace->ace_perm |= (ace->a_perm & SMB_ACL_WRITE) ?
1156                         ACL_PERM_WRITE : 0;
1157                 g_ace->ace_perm |= (ace->a_perm & SMB_ACL_EXECUTE) ?
1158                         ACL_PERM_EXECUTE : 0;
1159
1160                 DEBUGADD(10, ("Converted to %d id %d perm %x\n",
1161                               g_ace->ace_type, g_ace->ace_who, g_ace->ace_perm));
1162         }
1163
1164         return result;
1165 }
1166
1167 static int gpfsacl_sys_acl_set_fd(vfs_handle_struct *handle,
1168                                   files_struct *fsp,
1169                                   SMB_ACL_TYPE_T type,
1170                                   SMB_ACL_T theacl)
1171 {
1172         struct gpfs_config_data *config;
1173         struct gpfs_acl *gpfs_acl = NULL;
1174         int result;
1175
1176         SMB_VFS_HANDLE_GET_DATA(handle, config,
1177                                 struct gpfs_config_data,
1178                                 return -1);
1179
1180         if (!config->acl) {
1181                 return SMB_VFS_NEXT_SYS_ACL_SET_FD(handle, fsp, type, theacl);
1182         }
1183
1184         gpfs_acl = smb2gpfs_acl(theacl, type);
1185         if (gpfs_acl == NULL) {
1186                 return -1;
1187         }
1188
1189         /*
1190          * This is no longer a handle based call.
1191          */
1192         result = gpfswrap_putacl(fsp->fsp_name->base_name,
1193                                  GPFS_PUTACL_STRUCT|GPFS_ACL_SAMBA,
1194                                  gpfs_acl);
1195         SAFE_FREE(gpfs_acl);
1196         return result;
1197 }
1198
1199 static int gpfsacl_sys_acl_delete_def_fd(vfs_handle_struct *handle,
1200                                 files_struct *fsp)
1201 {
1202         struct gpfs_config_data *config;
1203
1204         SMB_VFS_HANDLE_GET_DATA(handle, config,
1205                                 struct gpfs_config_data,
1206                                 return -1);
1207
1208         if (!config->acl) {
1209                 return SMB_VFS_NEXT_SYS_ACL_DELETE_DEF_FD(handle, fsp);
1210         }
1211
1212         errno = ENOTSUP;
1213         return -1;
1214 }
1215
1216
1217 /*
1218  * Assumed: mode bits are shiftable and standard
1219  * Output: the new aceMask field for an smb nfs4 ace
1220  */
1221 static uint32_t gpfsacl_mask_filter(uint32_t aceType, uint32_t aceMask, uint32_t rwx)
1222 {
1223         const uint32_t posix_nfs4map[3] = {
1224                 SMB_ACE4_EXECUTE, /* execute */
1225                 SMB_ACE4_WRITE_DATA | SMB_ACE4_APPEND_DATA, /* write; GPFS specific */
1226                 SMB_ACE4_READ_DATA /* read */
1227         };
1228         int     i;
1229         uint32_t        posix_mask = 0x01;
1230         uint32_t        posix_bit;
1231         uint32_t        nfs4_bits;
1232
1233         for(i=0; i<3; i++) {
1234                 nfs4_bits = posix_nfs4map[i];
1235                 posix_bit = rwx & posix_mask;
1236
1237                 if (aceType==SMB_ACE4_ACCESS_ALLOWED_ACE_TYPE) {
1238                         if (posix_bit)
1239                                 aceMask |= nfs4_bits;
1240                         else
1241                                 aceMask &= ~nfs4_bits;
1242                 } else {
1243                         /* add deny bits when suitable */
1244                         if (!posix_bit)
1245                                 aceMask |= nfs4_bits;
1246                         else
1247                                 aceMask &= ~nfs4_bits;
1248                 } /* other ace types are unexpected */
1249
1250                 posix_mask <<= 1;
1251         }
1252
1253         return aceMask;
1254 }
1255
1256 static int gpfsacl_emu_chmod(vfs_handle_struct *handle,
1257                              const struct smb_filename *fname, mode_t mode)
1258 {
1259         char *path = fname->base_name;
1260         struct SMB4ACL_T *pacl = NULL;
1261         int     result;
1262         bool    haveAllowEntry[SMB_ACE4_WHO_EVERYONE + 1] = {False, False, False, False};
1263         int     i;
1264         files_struct fake_fsp = { 0 }; /* TODO: rationalize parametrization */
1265         struct SMB4ACE_T *smbace;
1266         TALLOC_CTX *frame = talloc_stackframe();
1267
1268         DEBUG(10, ("gpfsacl_emu_chmod invoked for %s mode %o\n", path, mode));
1269
1270         result = gpfs_get_nfs4_acl(frame, path, &pacl);
1271         if (result) {
1272                 TALLOC_FREE(frame);
1273                 return result;
1274         }
1275
1276         if (mode & ~(S_IRWXU | S_IRWXG | S_IRWXO)) {
1277                 DEBUG(2, ("WARNING: cutting extra mode bits %o on %s\n", mode, path));
1278         }
1279
1280         for (smbace=smb_first_ace4(pacl); smbace!=NULL; smbace = smb_next_ace4(smbace)) {
1281                 SMB_ACE4PROP_T  *ace = smb_get_ace4(smbace);
1282                 uint32_t        specid = ace->who.special_id;
1283
1284                 if (ace->flags&SMB_ACE4_ID_SPECIAL &&
1285                     ace->aceType<=SMB_ACE4_ACCESS_DENIED_ACE_TYPE &&
1286                     specid <= SMB_ACE4_WHO_EVERYONE) {
1287
1288                         uint32_t newMask;
1289
1290                         if (ace->aceType==SMB_ACE4_ACCESS_ALLOWED_ACE_TYPE)
1291                                 haveAllowEntry[specid] = True;
1292
1293                         /* mode >> 6 for @owner, mode >> 3 for @group,
1294                          * mode >> 0 for @everyone */
1295                         newMask = gpfsacl_mask_filter(ace->aceType, ace->aceMask,
1296                                                       mode >> ((SMB_ACE4_WHO_EVERYONE - specid) * 3));
1297                         if (ace->aceMask!=newMask) {
1298                                 DEBUG(10, ("ace changed for %s (%o -> %o) id=%d\n",
1299                                            path, ace->aceMask, newMask, specid));
1300                         }
1301                         ace->aceMask = newMask;
1302                 }
1303         }
1304
1305         /* make sure we have at least ALLOW entries
1306          * for all the 3 special ids (@EVERYONE, @OWNER, @GROUP)
1307          * - if necessary
1308          */
1309         for(i = SMB_ACE4_WHO_OWNER; i<=SMB_ACE4_WHO_EVERYONE; i++) {
1310                 SMB_ACE4PROP_T ace = { 0 };
1311
1312                 if (haveAllowEntry[i]==True)
1313                         continue;
1314
1315                 ace.aceType = SMB_ACE4_ACCESS_ALLOWED_ACE_TYPE;
1316                 ace.flags |= SMB_ACE4_ID_SPECIAL;
1317                 ace.who.special_id = i;
1318
1319                 if (i==SMB_ACE4_WHO_GROUP) /* not sure it's necessary... */
1320                         ace.aceFlags |= SMB_ACE4_IDENTIFIER_GROUP;
1321
1322                 ace.aceMask = gpfsacl_mask_filter(ace.aceType, ace.aceMask,
1323                                                   mode >> ((SMB_ACE4_WHO_EVERYONE - i) * 3));
1324
1325                 /* don't add unnecessary aces */
1326                 if (!ace.aceMask)
1327                         continue;
1328
1329                 /* we add it to the END - as windows expects allow aces */
1330                 smb_add_ace4(pacl, &ace);
1331                 DEBUG(10, ("Added ALLOW ace for %s, mode=%o, id=%d, aceMask=%x\n",
1332                            path, mode, i, ace.aceMask));
1333         }
1334
1335         /* don't add complementary DENY ACEs here */
1336         fake_fsp.fsp_name = synthetic_smb_fname(frame,
1337                                                 path,
1338                                                 NULL,
1339                                                 NULL,
1340                                                 fname->twrp,
1341                                                 0);
1342         if (fake_fsp.fsp_name == NULL) {
1343                 errno = ENOMEM;
1344                 TALLOC_FREE(frame);
1345                 return -1;
1346         }
1347         /* put the acl */
1348         if (gpfsacl_process_smbacl(handle, &fake_fsp, pacl) == False) {
1349                 TALLOC_FREE(frame);
1350                 return -1;
1351         }
1352
1353         TALLOC_FREE(frame);
1354         return 0; /* ok for [f]chmod */
1355 }
1356
1357 static int vfs_gpfs_fchmod(vfs_handle_struct *handle, files_struct *fsp, mode_t mode)
1358 {
1359                  SMB_STRUCT_STAT st;
1360                  int rc;
1361
1362                  if (SMB_VFS_NEXT_FSTAT(handle, fsp, &st) != 0) {
1363                          return -1;
1364                  }
1365
1366                  /* avoid chmod() if possible, to preserve acls */
1367                  if ((st.st_ex_mode & ~S_IFMT) == mode) {
1368                          return 0;
1369                  }
1370
1371                  rc = gpfsacl_emu_chmod(handle, fsp->fsp_name,
1372                                         mode);
1373                  if (rc == 1)
1374                          return SMB_VFS_NEXT_FCHMOD(handle, fsp, mode);
1375                  return rc;
1376 }
1377
1378 static uint32_t vfs_gpfs_winattrs_to_dosmode(unsigned int winattrs)
1379 {
1380         uint32_t dosmode = 0;
1381
1382         if (winattrs & GPFS_WINATTR_ARCHIVE){
1383                 dosmode |= FILE_ATTRIBUTE_ARCHIVE;
1384         }
1385         if (winattrs & GPFS_WINATTR_HIDDEN){
1386                 dosmode |= FILE_ATTRIBUTE_HIDDEN;
1387         }
1388         if (winattrs & GPFS_WINATTR_SYSTEM){
1389                 dosmode |= FILE_ATTRIBUTE_SYSTEM;
1390         }
1391         if (winattrs & GPFS_WINATTR_READONLY){
1392                 dosmode |= FILE_ATTRIBUTE_READONLY;
1393         }
1394         if (winattrs & GPFS_WINATTR_SPARSE_FILE) {
1395                 dosmode |= FILE_ATTRIBUTE_SPARSE;
1396         }
1397         if (winattrs & GPFS_WINATTR_OFFLINE) {
1398                 dosmode |= FILE_ATTRIBUTE_OFFLINE;
1399         }
1400
1401         return dosmode;
1402 }
1403
1404 static unsigned int vfs_gpfs_dosmode_to_winattrs(uint32_t dosmode)
1405 {
1406         unsigned int winattrs = 0;
1407
1408         if (dosmode & FILE_ATTRIBUTE_ARCHIVE){
1409                 winattrs |= GPFS_WINATTR_ARCHIVE;
1410         }
1411         if (dosmode & FILE_ATTRIBUTE_HIDDEN){
1412                 winattrs |= GPFS_WINATTR_HIDDEN;
1413         }
1414         if (dosmode & FILE_ATTRIBUTE_SYSTEM){
1415                 winattrs |= GPFS_WINATTR_SYSTEM;
1416         }
1417         if (dosmode & FILE_ATTRIBUTE_READONLY){
1418                 winattrs |= GPFS_WINATTR_READONLY;
1419         }
1420         if (dosmode & FILE_ATTRIBUTE_SPARSE) {
1421                 winattrs |= GPFS_WINATTR_SPARSE_FILE;
1422         }
1423         if (dosmode & FILE_ATTRIBUTE_OFFLINE) {
1424                 winattrs |= GPFS_WINATTR_OFFLINE;
1425         }
1426
1427         return winattrs;
1428 }
1429
1430 static NTSTATUS vfs_gpfs_get_file_id(struct gpfs_iattr64 *iattr,
1431                                      uint64_t *fileid)
1432 {
1433         uint8_t input[sizeof(gpfs_ino64_t) +
1434                       sizeof(gpfs_gen64_t) +
1435                       sizeof(gpfs_snapid64_t)];
1436         uint8_t digest[gnutls_hash_get_len(GNUTLS_DIG_SHA1)];
1437         int rc;
1438
1439         DBG_DEBUG("ia_inode 0x%llx, ia_gen 0x%llx, ia_modsnapid 0x%llx\n",
1440                   iattr->ia_inode, iattr->ia_gen, iattr->ia_modsnapid);
1441
1442         SBVAL(input,
1443               0, iattr->ia_inode);
1444         SBVAL(input,
1445               sizeof(gpfs_ino64_t), iattr->ia_gen);
1446         SBVAL(input,
1447               sizeof(gpfs_ino64_t) + sizeof(gpfs_gen64_t), iattr->ia_modsnapid);
1448
1449         GNUTLS_FIPS140_SET_LAX_MODE();
1450         rc = gnutls_hash_fast(GNUTLS_DIG_SHA1, input, sizeof(input), &digest);
1451         GNUTLS_FIPS140_SET_STRICT_MODE();
1452
1453         if (rc != 0) {
1454                 return gnutls_error_to_ntstatus(rc,
1455                                                 NT_STATUS_HASH_NOT_SUPPORTED);
1456         }
1457
1458         memcpy(fileid, &digest, sizeof(*fileid));
1459         DBG_DEBUG("file_id 0x%" PRIx64 "\n", *fileid);
1460
1461         return NT_STATUS_OK;
1462 }
1463
1464 static struct timespec gpfs_timestruc64_to_timespec(struct gpfs_timestruc64 g)
1465 {
1466         return (struct timespec) { .tv_sec = g.tv_sec, .tv_nsec = g.tv_nsec };
1467 }
1468
1469 static NTSTATUS vfs_gpfs_fget_dos_attributes(struct vfs_handle_struct *handle,
1470                                              struct files_struct *fsp,
1471                                              uint32_t *dosmode)
1472 {
1473         struct gpfs_config_data *config;
1474         struct gpfs_iattr64 iattr = { };
1475         unsigned int litemask;
1476         struct timespec ts;
1477         uint64_t file_id;
1478         NTSTATUS status;
1479         int ret;
1480
1481         SMB_VFS_HANDLE_GET_DATA(handle, config,
1482                                 struct gpfs_config_data,
1483                                 return NT_STATUS_INTERNAL_ERROR);
1484
1485         if (!config->winattr) {
1486                 return SMB_VFS_NEXT_FGET_DOS_ATTRIBUTES(handle, fsp, dosmode);
1487         }
1488
1489         ret = gpfswrap_fstat_x(fsp_get_pathref_fd(fsp), &litemask, &iattr, sizeof(iattr));
1490         if (ret == -1 && errno == ENOSYS) {
1491                 return SMB_VFS_NEXT_FGET_DOS_ATTRIBUTES(handle, fsp, dosmode);
1492         }
1493
1494         if (ret == -1 && errno == EACCES) {
1495                 int saved_errno = 0;
1496
1497                 /*
1498                  * According to MS-FSA 2.1.5.1.2.1 "Algorithm to Check Access to
1499                  * an Existing File" FILE_LIST_DIRECTORY on a directory implies
1500                  * FILE_READ_ATTRIBUTES for directory entries. Being able to
1501                  * open a file implies FILE_LIST_DIRECTORY.
1502                  */
1503
1504                 set_effective_capability(DAC_OVERRIDE_CAPABILITY);
1505
1506                 ret = gpfswrap_fstat_x(fsp_get_pathref_fd(fsp), &litemask,
1507                                        &iattr, sizeof(iattr));
1508                 if (ret == -1) {
1509                         saved_errno = errno;
1510                 }
1511
1512                 drop_effective_capability(DAC_OVERRIDE_CAPABILITY);
1513
1514                 if (saved_errno != 0) {
1515                         errno = saved_errno;
1516                 }
1517         }
1518
1519         if (ret == -1) {
1520                 DBG_WARNING("Getting winattrs failed for %s: %s\n",
1521                             fsp->fsp_name->base_name, strerror(errno));
1522                 return map_nt_error_from_unix(errno);
1523         }
1524
1525         ZERO_STRUCT(file_id);
1526         status = vfs_gpfs_get_file_id(&iattr, &file_id);
1527         if (!NT_STATUS_IS_OK(status)) {
1528                 return status;
1529         }
1530
1531         ts = gpfs_timestruc64_to_timespec(iattr.ia_createtime);
1532
1533         *dosmode |= vfs_gpfs_winattrs_to_dosmode(iattr.ia_winflags);
1534         update_stat_ex_create_time(&fsp->fsp_name->st, ts);
1535         update_stat_ex_file_id(&fsp->fsp_name->st, file_id);
1536
1537         return NT_STATUS_OK;
1538 }
1539
1540 static NTSTATUS vfs_gpfs_fset_dos_attributes(struct vfs_handle_struct *handle,
1541                                              struct files_struct *fsp,
1542                                              uint32_t dosmode)
1543 {
1544         struct gpfs_config_data *config;
1545         struct gpfs_winattr attrs = { };
1546         int ret;
1547
1548         SMB_VFS_HANDLE_GET_DATA(handle, config,
1549                                 struct gpfs_config_data,
1550                                 return NT_STATUS_INTERNAL_ERROR);
1551
1552         if (!config->winattr) {
1553                 return SMB_VFS_NEXT_FSET_DOS_ATTRIBUTES(handle, fsp, dosmode);
1554         }
1555
1556         attrs.winAttrs = vfs_gpfs_dosmode_to_winattrs(dosmode);
1557         ret = gpfswrap_set_winattrs(fsp_get_io_fd(fsp),
1558                                     GPFS_WINATTR_SET_ATTRS, &attrs);
1559
1560         if (ret == -1 && errno == ENOSYS) {
1561                 return SMB_VFS_NEXT_FSET_DOS_ATTRIBUTES(handle, fsp, dosmode);
1562         }
1563
1564         if (ret == -1) {
1565                 DBG_WARNING("Setting winattrs failed for %s: %s\n",
1566                             fsp->fsp_name->base_name, strerror(errno));
1567                 return map_nt_error_from_unix(errno);
1568         }
1569
1570         return NT_STATUS_OK;
1571 }
1572
1573 static int stat_with_capability(struct vfs_handle_struct *handle,
1574                                 struct smb_filename *smb_fname, int flag)
1575 {
1576 #if defined(HAVE_FSTATAT)
1577         int fd = -1;
1578         NTSTATUS status;
1579         struct smb_filename *dir_name = NULL;
1580         struct smb_filename *rel_name = NULL;
1581         struct stat st;
1582         int ret = -1;
1583
1584         status = SMB_VFS_PARENT_PATHNAME(handle->conn,
1585                                          talloc_tos(),
1586                                          smb_fname,
1587                                          &dir_name,
1588                                          &rel_name);
1589         if (!NT_STATUS_IS_OK(status)) {
1590                 errno = map_errno_from_nt_status(status);
1591                 return -1;
1592         }
1593
1594         fd = open(dir_name->base_name, O_RDONLY, 0);
1595         if (fd == -1) {
1596                 TALLOC_FREE(dir_name);
1597                 return -1;
1598         }
1599
1600         set_effective_capability(DAC_OVERRIDE_CAPABILITY);
1601         ret = fstatat(fd, rel_name->base_name, &st, flag);
1602         drop_effective_capability(DAC_OVERRIDE_CAPABILITY);
1603
1604         TALLOC_FREE(dir_name);
1605         close(fd);
1606
1607         if (ret == 0) {
1608                 init_stat_ex_from_stat(
1609                         &smb_fname->st, &st,
1610                         lp_fake_directory_create_times(SNUM(handle->conn)));
1611         }
1612
1613         return ret;
1614 #else
1615         return -1;
1616 #endif
1617 }
1618
1619 static int vfs_gpfs_stat(struct vfs_handle_struct *handle,
1620                          struct smb_filename *smb_fname)
1621 {
1622         int ret;
1623         struct gpfs_config_data *config;
1624
1625         SMB_VFS_HANDLE_GET_DATA(handle, config,
1626                                 struct gpfs_config_data,
1627                                 return -1);
1628
1629         ret = SMB_VFS_NEXT_STAT(handle, smb_fname);
1630         if (ret == -1 && errno == EACCES) {
1631                 DEBUG(10, ("Trying stat with capability for %s\n",
1632                            smb_fname->base_name));
1633                 ret = stat_with_capability(handle, smb_fname, 0);
1634         }
1635         return ret;
1636 }
1637
1638 static int vfs_gpfs_lstat(struct vfs_handle_struct *handle,
1639                           struct smb_filename *smb_fname)
1640 {
1641         int ret;
1642         struct gpfs_config_data *config;
1643
1644         SMB_VFS_HANDLE_GET_DATA(handle, config,
1645                                 struct gpfs_config_data,
1646                                 return -1);
1647
1648         ret = SMB_VFS_NEXT_LSTAT(handle, smb_fname);
1649         if (ret == -1 && errno == EACCES) {
1650                 DEBUG(10, ("Trying lstat with capability for %s\n",
1651                            smb_fname->base_name));
1652                 ret = stat_with_capability(handle, smb_fname,
1653                                            AT_SYMLINK_NOFOLLOW);
1654         }
1655         return ret;
1656 }
1657
1658 static void timespec_to_gpfs_time(struct timespec ts, gpfs_timestruc_t *gt,
1659                                   int idx, int *flags)
1660 {
1661         if (!is_omit_timespec(&ts)) {
1662                 *flags |= 1 << idx;
1663                 gt[idx].tv_sec = ts.tv_sec;
1664                 gt[idx].tv_nsec = ts.tv_nsec;
1665                 DEBUG(10, ("Setting GPFS time %d, flags 0x%x\n", idx, *flags));
1666         }
1667 }
1668
1669 static int smbd_gpfs_set_times(int fd, char *path, struct smb_file_time *ft)
1670 {
1671         gpfs_timestruc_t gpfs_times[4];
1672         int flags = 0;
1673         int rc;
1674
1675         ZERO_ARRAY(gpfs_times);
1676         timespec_to_gpfs_time(ft->atime, gpfs_times, 0, &flags);
1677         timespec_to_gpfs_time(ft->mtime, gpfs_times, 1, &flags);
1678         /* No good mapping from LastChangeTime to ctime, not storing */
1679         timespec_to_gpfs_time(ft->create_time, gpfs_times, 3, &flags);
1680
1681         if (!flags) {
1682                 DBG_DEBUG("nothing to do, return to avoid EINVAL\n");
1683                 return 0;
1684         }
1685
1686         rc = gpfswrap_set_times(fd, flags, gpfs_times);
1687
1688         if (rc != 0 && errno != ENOSYS) {
1689                 DBG_WARNING("gpfs_set_times() returned with error %s for %s\n",
1690                             strerror(errno),
1691                             path);
1692         }
1693
1694         return rc;
1695 }
1696
1697 static int vfs_gpfs_fntimes(struct vfs_handle_struct *handle,
1698                 files_struct *fsp,
1699                 struct smb_file_time *ft)
1700 {
1701
1702         struct gpfs_winattr attrs;
1703         int ret;
1704         struct gpfs_config_data *config;
1705
1706         SMB_VFS_HANDLE_GET_DATA(handle,
1707                                 config,
1708                                 struct gpfs_config_data,
1709                                 return -1);
1710
1711         /* Try to use gpfs_set_times if it is enabled and available */
1712         if (config->settimes) {
1713                 ret = smbd_gpfs_set_times(fsp_get_io_fd(fsp),
1714                                           fsp->fsp_name->base_name,
1715                                           ft);
1716                 if (ret == 0 || (ret == -1 && errno != ENOSYS)) {
1717                         return ret;
1718                 }
1719         }
1720
1721         DBG_DEBUG("gpfs_set_times() not available or disabled, "
1722                   "use ntimes and winattr\n");
1723
1724         ret = SMB_VFS_NEXT_FNTIMES(handle, fsp, ft);
1725         if (ret == -1) {
1726                 /* don't complain if access was denied */
1727                 if (errno != EPERM && errno != EACCES) {
1728                         DBG_WARNING("SMB_VFS_NEXT_FNTIMES failed: %s",
1729                                     strerror(errno));
1730                 }
1731                 return -1;
1732         }
1733
1734         if (is_omit_timespec(&ft->create_time)) {
1735                 DBG_DEBUG("Create Time is NULL\n");
1736                 return 0;
1737         }
1738
1739         if (!config->winattr) {
1740                 return 0;
1741         }
1742
1743         attrs.winAttrs = 0;
1744         attrs.creationTime.tv_sec = ft->create_time.tv_sec;
1745         attrs.creationTime.tv_nsec = ft->create_time.tv_nsec;
1746
1747         ret = gpfswrap_set_winattrs(fsp_get_io_fd(fsp),
1748                                     GPFS_WINATTR_SET_CREATION_TIME,
1749                                     &attrs);
1750         if (ret == -1 && errno != ENOSYS) {
1751                 DBG_WARNING("Set GPFS ntimes failed %d\n", ret);
1752                 return -1;
1753         }
1754
1755         return 0;
1756 }
1757
1758 static int vfs_gpfs_fallocate(struct vfs_handle_struct *handle,
1759                               struct files_struct *fsp, uint32_t mode,
1760                               off_t offset, off_t len)
1761 {
1762         if (mode == (VFS_FALLOCATE_FL_PUNCH_HOLE|VFS_FALLOCATE_FL_KEEP_SIZE) &&
1763             !fsp->fsp_flags.is_sparse &&
1764             lp_strict_allocate(SNUM(fsp->conn))) {
1765                 /*
1766                  * This is from a ZERO_DATA request on a non-sparse
1767                  * file. GPFS does not support FL_KEEP_SIZE and thus
1768                  * cannot fill the whole again in the subsequent
1769                  * fallocate(FL_KEEP_SIZE). Deny this FL_PUNCH_HOLE
1770                  * call to not end up with a hole in a non-sparse
1771                  * file.
1772                  */
1773                 errno = ENOTSUP;
1774                 return -1;
1775         }
1776
1777         return SMB_VFS_NEXT_FALLOCATE(handle, fsp, mode, offset, len);
1778 }
1779
1780 static int vfs_gpfs_ftruncate(vfs_handle_struct *handle, files_struct *fsp,
1781                                 off_t len)
1782 {
1783         int result;
1784         struct gpfs_config_data *config;
1785
1786         SMB_VFS_HANDLE_GET_DATA(handle, config,
1787                                 struct gpfs_config_data,
1788                                 return -1);
1789
1790         if (!config->ftruncate) {
1791                 return SMB_VFS_NEXT_FTRUNCATE(handle, fsp, len);
1792         }
1793
1794         result = gpfswrap_ftruncate(fsp_get_io_fd(fsp), len);
1795         if ((result == -1) && (errno == ENOSYS)) {
1796                 return SMB_VFS_NEXT_FTRUNCATE(handle, fsp, len);
1797         }
1798         return result;
1799 }
1800
1801 static bool vfs_gpfs_is_offline(struct vfs_handle_struct *handle,
1802                                 const struct smb_filename *fname,
1803                                 SMB_STRUCT_STAT *sbuf)
1804 {
1805         struct gpfs_winattr attrs;
1806         struct gpfs_config_data *config;
1807         int ret;
1808
1809         SMB_VFS_HANDLE_GET_DATA(handle, config,
1810                                 struct gpfs_config_data,
1811                                 return false);
1812
1813         if (!config->winattr) {
1814                 return false;
1815         }
1816
1817         ret = gpfswrap_get_winattrs_path(fname->base_name, &attrs);
1818         if (ret == -1) {
1819                 return false;
1820         }
1821
1822         if ((attrs.winAttrs & GPFS_WINATTR_OFFLINE) != 0) {
1823                 DBG_DEBUG("%s is offline\n", fname->base_name);
1824                 return true;
1825         }
1826
1827         DBG_DEBUG("%s is online\n", fname->base_name);
1828         return false;
1829 }
1830
1831 static bool vfs_gpfs_fsp_is_offline(struct vfs_handle_struct *handle,
1832                                     struct files_struct *fsp)
1833 {
1834         struct gpfs_fsp_extension *ext;
1835
1836         ext = VFS_FETCH_FSP_EXTENSION(handle, fsp);
1837         if (ext == NULL) {
1838                 /*
1839                  * Something bad happened, always ask.
1840                  */
1841                 return vfs_gpfs_is_offline(handle, fsp->fsp_name,
1842                                            &fsp->fsp_name->st);
1843         }
1844
1845         if (ext->offline) {
1846                 /*
1847                  * As long as it's offline, ask.
1848                  */
1849                 ext->offline = vfs_gpfs_is_offline(handle, fsp->fsp_name,
1850                                                    &fsp->fsp_name->st);
1851         }
1852
1853         return ext->offline;
1854 }
1855
1856 static bool vfs_gpfs_aio_force(struct vfs_handle_struct *handle,
1857                                struct files_struct *fsp)
1858 {
1859         return vfs_gpfs_fsp_is_offline(handle, fsp);
1860 }
1861
1862 static ssize_t vfs_gpfs_sendfile(vfs_handle_struct *handle, int tofd,
1863                                  files_struct *fsp, const DATA_BLOB *hdr,
1864                                  off_t offset, size_t n)
1865 {
1866         if (vfs_gpfs_fsp_is_offline(handle, fsp)) {
1867                 errno = ENOSYS;
1868                 return -1;
1869         }
1870         return SMB_VFS_NEXT_SENDFILE(handle, tofd, fsp, hdr, offset, n);
1871 }
1872
1873 static int vfs_gpfs_connect(struct vfs_handle_struct *handle,
1874                             const char *service, const char *user)
1875 {
1876         struct gpfs_config_data *config;
1877         int ret;
1878         bool check_fstype;
1879
1880         gpfswrap_lib_init(0);
1881
1882         config = talloc_zero(handle->conn, struct gpfs_config_data);
1883         if (!config) {
1884                 DEBUG(0, ("talloc_zero() failed\n"));
1885                 errno = ENOMEM;
1886                 return -1;
1887         }
1888
1889         ret = SMB_VFS_NEXT_CONNECT(handle, service, user);
1890         if (ret < 0) {
1891                 TALLOC_FREE(config);
1892                 return ret;
1893         }
1894
1895         check_fstype = lp_parm_bool(SNUM(handle->conn), "gpfs",
1896                                     "check_fstype", true);
1897
1898         if (check_fstype && !IS_IPC(handle->conn)) {
1899                 const char *connectpath = handle->conn->connectpath;
1900                 struct statfs buf = { 0 };
1901
1902                 ret = statfs(connectpath, &buf);
1903                 if (ret != 0) {
1904                         DBG_ERR("statfs failed for share %s at path %s: %s\n",
1905                                 service, connectpath, strerror(errno));
1906                         TALLOC_FREE(config);
1907                         return ret;
1908                 }
1909
1910                 if (buf.f_type != GPFS_SUPER_MAGIC) {
1911                         DBG_ERR("SMB share %s, path %s not in GPFS file system."
1912                                 " statfs magic: 0x%jx\n",
1913                                 service,
1914                                 connectpath,
1915                                 (uintmax_t)buf.f_type);
1916                         errno = EINVAL;
1917                         TALLOC_FREE(config);
1918                         return -1;
1919                 }
1920         }
1921
1922         ret = smbacl4_get_vfs_params(handle->conn, &config->nfs4_params);
1923         if (ret < 0) {
1924                 TALLOC_FREE(config);
1925                 return ret;
1926         }
1927
1928         config->sharemodes = lp_parm_bool(SNUM(handle->conn), "gpfs",
1929                                         "sharemodes", true);
1930
1931         config->leases = lp_parm_bool(SNUM(handle->conn), "gpfs",
1932                                         "leases", true);
1933
1934         config->hsm = lp_parm_bool(SNUM(handle->conn), "gpfs",
1935                                    "hsm", false);
1936
1937         config->syncio = lp_parm_bool(SNUM(handle->conn), "gpfs",
1938                                       "syncio", false);
1939
1940         config->winattr = lp_parm_bool(SNUM(handle->conn), "gpfs",
1941                                        "winattr", false);
1942
1943         config->ftruncate = lp_parm_bool(SNUM(handle->conn), "gpfs",
1944                                          "ftruncate", true);
1945
1946         config->getrealfilename = lp_parm_bool(SNUM(handle->conn), "gpfs",
1947                                                "getrealfilename", true);
1948
1949         config->dfreequota = lp_parm_bool(SNUM(handle->conn), "gpfs",
1950                                           "dfreequota", false);
1951
1952         config->acl = lp_parm_bool(SNUM(handle->conn), "gpfs", "acl", true);
1953
1954         config->settimes = lp_parm_bool(SNUM(handle->conn), "gpfs",
1955                                         "settimes", true);
1956         config->recalls = lp_parm_bool(SNUM(handle->conn), "gpfs",
1957                                        "recalls", true);
1958
1959         SMB_VFS_HANDLE_SET_DATA(handle, config,
1960                                 NULL, struct gpfs_config_data,
1961                                 return -1);
1962
1963         if (config->leases) {
1964                 /*
1965                  * GPFS lease code is based on kernel oplock code
1966                  * so make sure it is turned on
1967                  */
1968                 if (!lp_kernel_oplocks(SNUM(handle->conn))) {
1969                         DEBUG(5, ("Enabling kernel oplocks for "
1970                                   "gpfs:leases to work\n"));
1971                         lp_do_parameter(SNUM(handle->conn), "kernel oplocks",
1972                                         "true");
1973                 }
1974
1975                 /*
1976                  * as the kernel does not properly support Level II oplocks
1977                  * and GPFS leases code is based on kernel infrastructure, we
1978                  * need to turn off Level II oplocks if gpfs:leases is enabled
1979                  */
1980                 if (lp_level2_oplocks(SNUM(handle->conn))) {
1981                         DEBUG(5, ("gpfs:leases are enabled, disabling "
1982                                   "Level II oplocks\n"));
1983                         lp_do_parameter(SNUM(handle->conn), "level2 oplocks",
1984                                         "false");
1985                 }
1986         }
1987
1988         /*
1989          * Unless we have an async implementation of get_dos_attributes turn
1990          * this off.
1991          */
1992         lp_do_parameter(SNUM(handle->conn), "smbd async dosmode", "false");
1993
1994         return 0;
1995 }
1996
1997 static int get_gpfs_quota(const char *pathname, int type, int id,
1998                           struct gpfs_quotaInfo *qi)
1999 {
2000         int ret;
2001
2002         ret = gpfswrap_quotactl(pathname, GPFS_QCMD(Q_GETQUOTA, type), id, qi);
2003
2004         if (ret) {
2005                 if (errno == GPFS_E_NO_QUOTA_INST) {
2006                         DEBUG(10, ("Quotas disabled on GPFS filesystem.\n"));
2007                 } else if (errno != ENOSYS) {
2008                         DEBUG(0, ("Get quota failed, type %d, id, %d, "
2009                                   "errno %d.\n", type, id, errno));
2010                 }
2011
2012                 return ret;
2013         }
2014
2015         DEBUG(10, ("quota type %d, id %d, blk u:%lld h:%lld s:%lld gt:%u\n",
2016                    type, id, qi->blockUsage, qi->blockHardLimit,
2017                    qi->blockSoftLimit, qi->blockGraceTime));
2018
2019         return ret;
2020 }
2021
2022 static void vfs_gpfs_disk_free_quota(struct gpfs_quotaInfo qi, time_t cur_time,
2023                                      uint64_t *dfree, uint64_t *dsize)
2024 {
2025         uint64_t usage, limit;
2026
2027         /*
2028          * The quota reporting is done in units of 1024 byte blocks, but
2029          * sys_fsusage uses units of 512 byte blocks, adjust the block number
2030          * accordingly. Also filter possibly negative usage counts from gpfs.
2031          */
2032         usage = qi.blockUsage < 0 ? 0 : (uint64_t)qi.blockUsage * 2;
2033         limit = (uint64_t)qi.blockHardLimit * 2;
2034
2035         /*
2036          * When the grace time for the exceeded soft block quota has been
2037          * exceeded, the soft block quota becomes an additional hard limit.
2038          */
2039         if (qi.blockSoftLimit &&
2040             qi.blockGraceTime && cur_time > qi.blockGraceTime) {
2041                 /* report disk as full */
2042                 *dfree = 0;
2043                 *dsize = MIN(*dsize, usage);
2044         }
2045
2046         if (!qi.blockHardLimit)
2047                 return;
2048
2049         if (usage >= limit) {
2050                 /* report disk as full */
2051                 *dfree = 0;
2052                 *dsize = MIN(*dsize, usage);
2053
2054         } else {
2055                 /* limit has not been reached, determine "free space" */
2056                 *dfree = MIN(*dfree, limit - usage);
2057                 *dsize = MIN(*dsize, limit);
2058         }
2059 }
2060
2061 static uint64_t vfs_gpfs_disk_free(vfs_handle_struct *handle,
2062                                 const struct smb_filename *smb_fname,
2063                                 uint64_t *bsize,
2064                                 uint64_t *dfree,
2065                                 uint64_t *dsize)
2066 {
2067         struct security_unix_token *utok;
2068         struct gpfs_quotaInfo qi_user = { 0 }, qi_group = { 0 };
2069         struct gpfs_config_data *config;
2070         int err;
2071         time_t cur_time;
2072
2073         SMB_VFS_HANDLE_GET_DATA(handle, config, struct gpfs_config_data,
2074                                 return (uint64_t)-1);
2075         if (!config->dfreequota) {
2076                 return SMB_VFS_NEXT_DISK_FREE(handle, smb_fname,
2077                                               bsize, dfree, dsize);
2078         }
2079
2080         err = sys_fsusage(smb_fname->base_name, dfree, dsize);
2081         if (err) {
2082                 DEBUG (0, ("Could not get fs usage, errno %d\n", errno));
2083                 return SMB_VFS_NEXT_DISK_FREE(handle, smb_fname,
2084                                               bsize, dfree, dsize);
2085         }
2086
2087         /* sys_fsusage returns units of 512 bytes */
2088         *bsize = 512;
2089
2090         DEBUG(10, ("fs dfree %llu, dsize %llu\n",
2091                    (unsigned long long)*dfree, (unsigned long long)*dsize));
2092
2093         utok = handle->conn->session_info->unix_token;
2094
2095         err = get_gpfs_quota(smb_fname->base_name,
2096                         GPFS_USRQUOTA, utok->uid, &qi_user);
2097         if (err) {
2098                 return SMB_VFS_NEXT_DISK_FREE(handle, smb_fname,
2099                                               bsize, dfree, dsize);
2100         }
2101
2102         /*
2103          * If new files created under this folder get this folder's
2104          * GID, then available space is governed by the quota of the
2105          * folder's GID, not the primary group of the creating user.
2106          */
2107         if (VALID_STAT(smb_fname->st) &&
2108             S_ISDIR(smb_fname->st.st_ex_mode) &&
2109             smb_fname->st.st_ex_mode & S_ISGID) {
2110                 become_root();
2111                 err = get_gpfs_quota(smb_fname->base_name, GPFS_GRPQUOTA,
2112                                      smb_fname->st.st_ex_gid, &qi_group);
2113                 unbecome_root();
2114
2115         } else {
2116                 err = get_gpfs_quota(smb_fname->base_name, GPFS_GRPQUOTA,
2117                                      utok->gid, &qi_group);
2118         }
2119
2120         if (err) {
2121                 return SMB_VFS_NEXT_DISK_FREE(handle, smb_fname,
2122                                               bsize, dfree, dsize);
2123         }
2124
2125         cur_time = time(NULL);
2126
2127         /* Adjust free space and size according to quota limits. */
2128         vfs_gpfs_disk_free_quota(qi_user, cur_time, dfree, dsize);
2129         vfs_gpfs_disk_free_quota(qi_group, cur_time, dfree, dsize);
2130
2131         return *dfree / 2;
2132 }
2133
2134 static int vfs_gpfs_get_quota(vfs_handle_struct *handle,
2135                                 const struct smb_filename *smb_fname,
2136                                 enum SMB_QUOTA_TYPE qtype,
2137                                 unid_t id,
2138                                 SMB_DISK_QUOTA *dq)
2139 {
2140         switch(qtype) {
2141                 /*
2142                  * User/group quota are being used for disk-free
2143                  * determination, which in this module is done directly
2144                  * by the disk-free function. It's important that this
2145                  * module does not return wrong quota values by mistake,
2146                  * which would modify the correct values set by disk-free.
2147                  * User/group quota are also being used for processing
2148                  * NT_TRANSACT_GET_USER_QUOTA in smb1 protocol, which is
2149                  * currently not supported by this module.
2150                  */
2151                 case SMB_USER_QUOTA_TYPE:
2152                 case SMB_GROUP_QUOTA_TYPE:
2153                         errno = ENOSYS;
2154                         return -1;
2155                 default:
2156                         return SMB_VFS_NEXT_GET_QUOTA(handle, smb_fname,
2157                                         qtype, id, dq);
2158         }
2159 }
2160
2161 static uint32_t vfs_gpfs_capabilities(struct vfs_handle_struct *handle,
2162                                       enum timestamp_set_resolution *p_ts_res)
2163 {
2164         struct gpfs_config_data *config;
2165         uint32_t next;
2166
2167         next = SMB_VFS_NEXT_FS_CAPABILITIES(handle, p_ts_res);
2168
2169         SMB_VFS_HANDLE_GET_DATA(handle, config,
2170                                 struct gpfs_config_data,
2171                                 return next);
2172
2173         if (config->hsm) {
2174                 next |= FILE_SUPPORTS_REMOTE_STORAGE;
2175         }
2176         return next;
2177 }
2178
2179 static int vfs_gpfs_openat(struct vfs_handle_struct *handle,
2180                            const struct files_struct *dirfsp,
2181                            const struct smb_filename *smb_fname,
2182                            files_struct *fsp,
2183                            int flags,
2184                            mode_t mode)
2185 {
2186         struct gpfs_config_data *config = NULL;
2187         struct gpfs_fsp_extension *ext = NULL;
2188         int ret;
2189
2190         SMB_VFS_HANDLE_GET_DATA(handle, config,
2191                                 struct gpfs_config_data,
2192                                 return -1);
2193
2194         if (config->hsm && !config->recalls &&
2195             vfs_gpfs_fsp_is_offline(handle, fsp))
2196         {
2197                 DBG_DEBUG("Refusing access to offline file %s\n",
2198                           fsp_str_dbg(fsp));
2199                 errno = EACCES;
2200                 return -1;
2201         }
2202
2203         if (config->syncio) {
2204                 flags |= O_SYNC;
2205         }
2206
2207         ext = VFS_ADD_FSP_EXTENSION(handle, fsp, struct gpfs_fsp_extension,
2208                                     NULL);
2209         if (ext == NULL) {
2210                 errno = ENOMEM;
2211                 return -1;
2212         }
2213
2214         /*
2215          * Assume the file is offline until gpfs tells us it's online.
2216          */
2217         *ext = (struct gpfs_fsp_extension) { .offline = true };
2218
2219         ret = SMB_VFS_NEXT_OPENAT(handle, dirfsp, smb_fname, fsp, flags, mode);
2220         if (ret == -1) {
2221                 VFS_REMOVE_FSP_EXTENSION(handle, fsp);
2222         }
2223         return ret;
2224 }
2225
2226 static ssize_t vfs_gpfs_pread(vfs_handle_struct *handle, files_struct *fsp,
2227                               void *data, size_t n, off_t offset)
2228 {
2229         ssize_t ret;
2230         bool was_offline;
2231
2232         was_offline = vfs_gpfs_fsp_is_offline(handle, fsp);
2233
2234         ret = SMB_VFS_NEXT_PREAD(handle, fsp, data, n, offset);
2235
2236         if ((ret != -1) && was_offline) {
2237                 notify_fname(handle->conn, NOTIFY_ACTION_MODIFIED,
2238                              FILE_NOTIFY_CHANGE_ATTRIBUTES,
2239                              fsp->fsp_name->base_name);
2240         }
2241
2242         return ret;
2243 }
2244
2245 struct vfs_gpfs_pread_state {
2246         struct files_struct *fsp;
2247         ssize_t ret;
2248         bool was_offline;
2249         struct vfs_aio_state vfs_aio_state;
2250 };
2251
2252 static void vfs_gpfs_pread_done(struct tevent_req *subreq);
2253
2254 static struct tevent_req *vfs_gpfs_pread_send(struct vfs_handle_struct *handle,
2255                                               TALLOC_CTX *mem_ctx,
2256                                               struct tevent_context *ev,
2257                                               struct files_struct *fsp,
2258                                               void *data, size_t n,
2259                                               off_t offset)
2260 {
2261         struct tevent_req *req, *subreq;
2262         struct vfs_gpfs_pread_state *state;
2263
2264         req = tevent_req_create(mem_ctx, &state, struct vfs_gpfs_pread_state);
2265         if (req == NULL) {
2266                 return NULL;
2267         }
2268         state->was_offline = vfs_gpfs_fsp_is_offline(handle, fsp);
2269         state->fsp = fsp;
2270         subreq = SMB_VFS_NEXT_PREAD_SEND(state, ev, handle, fsp, data,
2271                                          n, offset);
2272         if (tevent_req_nomem(subreq, req)) {
2273                 return tevent_req_post(req, ev);
2274         }
2275         tevent_req_set_callback(subreq, vfs_gpfs_pread_done, req);
2276         return req;
2277 }
2278
2279 static void vfs_gpfs_pread_done(struct tevent_req *subreq)
2280 {
2281         struct tevent_req *req = tevent_req_callback_data(
2282                 subreq, struct tevent_req);
2283         struct vfs_gpfs_pread_state *state = tevent_req_data(
2284                 req, struct vfs_gpfs_pread_state);
2285
2286         state->ret = SMB_VFS_PREAD_RECV(subreq, &state->vfs_aio_state);
2287         TALLOC_FREE(subreq);
2288         tevent_req_done(req);
2289 }
2290
2291 static ssize_t vfs_gpfs_pread_recv(struct tevent_req *req,
2292                                    struct vfs_aio_state *vfs_aio_state)
2293 {
2294         struct vfs_gpfs_pread_state *state = tevent_req_data(
2295                 req, struct vfs_gpfs_pread_state);
2296         struct files_struct *fsp = state->fsp;
2297
2298         if (tevent_req_is_unix_error(req, &vfs_aio_state->error)) {
2299                 return -1;
2300         }
2301         *vfs_aio_state = state->vfs_aio_state;
2302
2303         if ((state->ret != -1) && state->was_offline) {
2304                 DEBUG(10, ("sending notify\n"));
2305                 notify_fname(fsp->conn, NOTIFY_ACTION_MODIFIED,
2306                              FILE_NOTIFY_CHANGE_ATTRIBUTES,
2307                              fsp->fsp_name->base_name);
2308         }
2309
2310         return state->ret;
2311 }
2312
2313 static ssize_t vfs_gpfs_pwrite(vfs_handle_struct *handle, files_struct *fsp,
2314                                const void *data, size_t n, off_t offset)
2315 {
2316         ssize_t ret;
2317         bool was_offline;
2318
2319         was_offline = vfs_gpfs_fsp_is_offline(handle, fsp);
2320
2321         ret = SMB_VFS_NEXT_PWRITE(handle, fsp, data, n, offset);
2322
2323         if ((ret != -1) && was_offline) {
2324                 notify_fname(handle->conn, NOTIFY_ACTION_MODIFIED,
2325                              FILE_NOTIFY_CHANGE_ATTRIBUTES,
2326                              fsp->fsp_name->base_name);
2327         }
2328
2329         return ret;
2330 }
2331
2332 struct vfs_gpfs_pwrite_state {
2333         struct files_struct *fsp;
2334         ssize_t ret;
2335         bool was_offline;
2336         struct vfs_aio_state vfs_aio_state;
2337 };
2338
2339 static void vfs_gpfs_pwrite_done(struct tevent_req *subreq);
2340
2341 static struct tevent_req *vfs_gpfs_pwrite_send(
2342         struct vfs_handle_struct *handle,
2343         TALLOC_CTX *mem_ctx,
2344         struct tevent_context *ev,
2345         struct files_struct *fsp,
2346         const void *data, size_t n,
2347         off_t offset)
2348 {
2349         struct tevent_req *req, *subreq;
2350         struct vfs_gpfs_pwrite_state *state;
2351
2352         req = tevent_req_create(mem_ctx, &state, struct vfs_gpfs_pwrite_state);
2353         if (req == NULL) {
2354                 return NULL;
2355         }
2356         state->was_offline = vfs_gpfs_fsp_is_offline(handle, fsp);
2357         state->fsp = fsp;
2358         subreq = SMB_VFS_NEXT_PWRITE_SEND(state, ev, handle, fsp, data,
2359                                          n, offset);
2360         if (tevent_req_nomem(subreq, req)) {
2361                 return tevent_req_post(req, ev);
2362         }
2363         tevent_req_set_callback(subreq, vfs_gpfs_pwrite_done, req);
2364         return req;
2365 }
2366
2367 static void vfs_gpfs_pwrite_done(struct tevent_req *subreq)
2368 {
2369         struct tevent_req *req = tevent_req_callback_data(
2370                 subreq, struct tevent_req);
2371         struct vfs_gpfs_pwrite_state *state = tevent_req_data(
2372                 req, struct vfs_gpfs_pwrite_state);
2373
2374         state->ret = SMB_VFS_PWRITE_RECV(subreq, &state->vfs_aio_state);
2375         TALLOC_FREE(subreq);
2376         tevent_req_done(req);
2377 }
2378
2379 static ssize_t vfs_gpfs_pwrite_recv(struct tevent_req *req,
2380                                     struct vfs_aio_state *vfs_aio_state)
2381 {
2382         struct vfs_gpfs_pwrite_state *state = tevent_req_data(
2383                 req, struct vfs_gpfs_pwrite_state);
2384         struct files_struct *fsp = state->fsp;
2385
2386         if (tevent_req_is_unix_error(req, &vfs_aio_state->error)) {
2387                 return -1;
2388         }
2389         *vfs_aio_state = state->vfs_aio_state;
2390
2391         if ((state->ret != -1) && state->was_offline) {
2392                 DEBUG(10, ("sending notify\n"));
2393                 notify_fname(fsp->conn, NOTIFY_ACTION_MODIFIED,
2394                              FILE_NOTIFY_CHANGE_ATTRIBUTES,
2395                              fsp->fsp_name->base_name);
2396         }
2397
2398         return state->ret;
2399 }
2400
2401
2402 static struct vfs_fn_pointers vfs_gpfs_fns = {
2403         .connect_fn = vfs_gpfs_connect,
2404         .disk_free_fn = vfs_gpfs_disk_free,
2405         .get_quota_fn = vfs_gpfs_get_quota,
2406         .fs_capabilities_fn = vfs_gpfs_capabilities,
2407         .kernel_flock_fn = vfs_gpfs_kernel_flock,
2408         .linux_setlease_fn = vfs_gpfs_setlease,
2409         .get_real_filename_fn = vfs_gpfs_get_real_filename,
2410         .get_dos_attributes_send_fn = vfs_not_implemented_get_dos_attributes_send,
2411         .get_dos_attributes_recv_fn = vfs_not_implemented_get_dos_attributes_recv,
2412         .fget_dos_attributes_fn = vfs_gpfs_fget_dos_attributes,
2413         .fset_dos_attributes_fn = vfs_gpfs_fset_dos_attributes,
2414         .fget_nt_acl_fn = gpfsacl_fget_nt_acl,
2415         .fset_nt_acl_fn = gpfsacl_fset_nt_acl,
2416         .sys_acl_get_fd_fn = gpfsacl_sys_acl_get_fd,
2417         .sys_acl_blob_get_fd_fn = gpfsacl_sys_acl_blob_get_fd,
2418         .sys_acl_set_fd_fn = gpfsacl_sys_acl_set_fd,
2419         .sys_acl_delete_def_fd_fn = gpfsacl_sys_acl_delete_def_fd,
2420         .fchmod_fn = vfs_gpfs_fchmod,
2421         .close_fn = vfs_gpfs_close,
2422         .stat_fn = vfs_gpfs_stat,
2423         .lstat_fn = vfs_gpfs_lstat,
2424         .fntimes_fn = vfs_gpfs_fntimes,
2425         .aio_force_fn = vfs_gpfs_aio_force,
2426         .sendfile_fn = vfs_gpfs_sendfile,
2427         .fallocate_fn = vfs_gpfs_fallocate,
2428         .openat_fn = vfs_gpfs_openat,
2429         .pread_fn = vfs_gpfs_pread,
2430         .pread_send_fn = vfs_gpfs_pread_send,
2431         .pread_recv_fn = vfs_gpfs_pread_recv,
2432         .pwrite_fn = vfs_gpfs_pwrite,
2433         .pwrite_send_fn = vfs_gpfs_pwrite_send,
2434         .pwrite_recv_fn = vfs_gpfs_pwrite_recv,
2435         .ftruncate_fn = vfs_gpfs_ftruncate
2436 };
2437
2438 static_decl_vfs;
2439 NTSTATUS vfs_gpfs_init(TALLOC_CTX *ctx)
2440 {
2441         int ret;
2442
2443         ret = gpfswrap_init();
2444         if (ret != 0) {
2445                 DEBUG(1, ("Could not initialize GPFS library wrapper\n"));
2446         }
2447
2448         return smb_register_vfs(SMB_VFS_INTERFACE_VERSION, "gpfs",
2449                                 &vfs_gpfs_fns);
2450 }