Fix bug disclosed by lock8 torture test
[Samba/gebeck_regimport.git] / source3 / modules / vfs_gpfs.c
blob778f4a00fd7cabf5ee69bd66af269869dcb4e821
1 /*
2 Unix SMB/CIFS implementation.
3 Wrap gpfs calls in vfs functions.
5 Copyright (C) Christian Ambach <cambach1@de.ibm.com> 2006
7 Major code contributions by Chetan Shringarpure <chetan.sh@in.ibm.com>
8 and Gomati Mohanan <gomati.mohanan@in.ibm.com>
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 3 of the License, or
13 (at your option) any later version.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program. If not, see <http://www.gnu.org/licenses/>.
24 #include "includes.h"
26 #undef DBGC_CLASS
27 #define DBGC_CLASS DBGC_VFS
29 #include <gpfs_gpl.h>
30 #include "nfs4_acls.h"
31 #include "vfs_gpfs.h"
33 static int vfs_gpfs_kernel_flock(vfs_handle_struct *handle, files_struct *fsp,
34 uint32 share_mode)
37 START_PROFILE(syscall_kernel_flock);
39 kernel_flock(fsp->fh->fd, share_mode);
41 if (!set_gpfs_sharemode(fsp, fsp->access_mask, fsp->share_access)) {
43 return -1;
47 END_PROFILE(syscall_kernel_flock);
49 return 0;
52 static int vfs_gpfs_close(vfs_handle_struct *handle, files_struct *fsp)
54 int result;
56 if ((fsp->fh != NULL) && (fsp->fh->fd != -1)) {
57 set_gpfs_sharemode(fsp, 0, 0);
60 result = fd_close_posix(fsp);
62 return result;
65 static int vfs_gpfs_setlease(vfs_handle_struct *handle, files_struct *fsp,
66 int leasetype)
68 int ret;
70 START_PROFILE(syscall_linux_setlease);
72 if ( linux_set_lease_sighandler(fsp->fh->fd) == -1)
73 return -1;
75 ret = set_gpfs_lease(fsp->fh->fd,leasetype);
77 if ( ret < 0 ) {
78 /* This must have come from GPFS not being available */
79 /* or some other error, hence call the default */
80 ret = linux_setlease(fsp->fh->fd, leasetype);
83 END_PROFILE(syscall_linux_setlease);
85 return ret;
88 static int vfs_gpfs_get_real_filename(struct vfs_handle_struct *handle,
89 const char *path,
90 const char *name,
91 TALLOC_CTX *mem_ctx,
92 char **found_name)
94 int result;
95 char *full_path;
96 char real_pathname[PATH_MAX+1];
97 int buflen;
99 full_path = talloc_asprintf(talloc_tos(), "%s/%s", path, name);
100 if (full_path == NULL) {
101 errno = ENOMEM;
102 return -1;
105 buflen = sizeof(real_pathname) - 1;
107 result = smbd_gpfs_get_realfilename_path(full_path, real_pathname,
108 &buflen);
110 TALLOC_FREE(full_path);
112 if ((result == -1) && (errno == ENOSYS)) {
113 return SMB_VFS_NEXT_GET_REAL_FILENAME(
114 handle, path, name, mem_ctx, found_name);
117 if (result == -1) {
118 DEBUG(10, ("smbd_gpfs_get_realfilename_path returned %s\n",
119 strerror(errno)));
120 return -1;
124 * GPFS does not necessarily null-terminate the returned path
125 * but instead returns the buffer length in buflen.
128 if (buflen < sizeof(real_pathname)) {
129 real_pathname[buflen] = '\0';
130 } else {
131 real_pathname[sizeof(real_pathname)-1] = '\0';
134 DEBUG(10, ("smbd_gpfs_get_realfilename_path: %s/%s -> %s\n",
135 path, name, real_pathname));
137 name = strrchr_m(real_pathname, '/');
138 if (name == NULL) {
139 errno = ENOENT;
140 return -1;
143 *found_name = talloc_strdup(mem_ctx, name+1);
144 if (*found_name == NULL) {
145 errno = ENOMEM;
146 return -1;
149 return 0;
152 static void gpfs_dumpacl(int level, struct gpfs_acl *gacl)
154 int i;
155 if (gacl==NULL)
157 DEBUG(0, ("gpfs acl is NULL\n"));
158 return;
161 DEBUG(level, ("gpfs acl: nace: %d, type:%d, version:%d, level:%d, len:%d\n",
162 gacl->acl_nace, gacl->acl_type, gacl->acl_version, gacl->acl_level, gacl->acl_len));
163 for(i=0; i<gacl->acl_nace; i++)
165 struct gpfs_ace_v4 *gace = gacl->ace_v4 + i;
166 DEBUG(level, ("\tace[%d]: type:%d, flags:0x%x, mask:0x%x, iflags:0x%x, who:%u\n",
167 i, gace->aceType, gace->aceFlags, gace->aceMask,
168 gace->aceIFlags, gace->aceWho));
172 static struct gpfs_acl *gpfs_getacl_alloc(const char *fname, gpfs_aclType_t type)
174 struct gpfs_acl *acl;
175 size_t len = 200;
176 int ret;
177 TALLOC_CTX *mem_ctx = talloc_tos();
179 acl = (struct gpfs_acl *)TALLOC_SIZE(mem_ctx, len);
180 if (acl == NULL) {
181 errno = ENOMEM;
182 return NULL;
185 acl->acl_len = len;
186 acl->acl_level = 0;
187 acl->acl_version = 0;
188 acl->acl_type = type;
190 ret = smbd_gpfs_getacl((char *)fname, GPFS_GETACL_STRUCT | GPFS_ACL_SAMBA, acl);
191 if ((ret != 0) && (errno == ENOSPC)) {
192 struct gpfs_acl *new_acl = (struct gpfs_acl *)TALLOC_SIZE(
193 mem_ctx, acl->acl_len + sizeof(struct gpfs_acl));
194 if (new_acl == NULL) {
195 errno = ENOMEM;
196 return NULL;
199 new_acl->acl_len = acl->acl_len;
200 new_acl->acl_level = acl->acl_level;
201 new_acl->acl_version = acl->acl_version;
202 new_acl->acl_type = acl->acl_type;
203 acl = new_acl;
205 ret = smbd_gpfs_getacl((char *)fname, GPFS_GETACL_STRUCT | GPFS_ACL_SAMBA, acl);
207 if (ret != 0)
209 DEBUG(8, ("smbd_gpfs_getacl failed with %s\n",strerror(errno)));
210 return NULL;
213 return acl;
216 /* Tries to get nfs4 acls and returns SMB ACL allocated.
217 * On failure returns 1 if it got non-NFSv4 ACL to prompt
218 * retry with POSIX ACL checks.
219 * On failure returns -1 if there is system (GPFS) error, check errno.
220 * Returns 0 on success
222 static int gpfs_get_nfs4_acl(const char *fname, SMB4ACL_T **ppacl)
224 int i;
225 struct gpfs_acl *gacl = NULL;
226 DEBUG(10, ("gpfs_get_nfs4_acl invoked for %s\n", fname));
228 /* First get the real acl length */
229 gacl = gpfs_getacl_alloc(fname, 0);
230 if (gacl == NULL) {
231 DEBUG(9, ("gpfs_getacl failed for %s with %s\n",
232 fname, strerror(errno)));
233 return -1;
236 if (gacl->acl_type != GPFS_ACL_TYPE_NFS4) {
237 DEBUG(10, ("Got non-nfsv4 acl\n"));
238 /* Retry with POSIX ACLs check */
239 return 1;
242 *ppacl = smb_create_smb4acl();
244 DEBUG(10, ("len: %d, level: %d, version: %d, nace: %d\n",
245 gacl->acl_len, gacl->acl_level, gacl->acl_version,
246 gacl->acl_nace));
248 for (i=0; i<gacl->acl_nace; i++) {
249 struct gpfs_ace_v4 *gace = &gacl->ace_v4[i];
250 SMB_ACE4PROP_T smbace;
251 DEBUG(10, ("type: %d, iflags: %x, flags: %x, mask: %x, "
252 "who: %d\n", gace->aceType, gace->aceIFlags,
253 gace->aceFlags, gace->aceMask, gace->aceWho));
255 ZERO_STRUCT(smbace);
256 if (gace->aceIFlags & ACE4_IFLAG_SPECIAL_ID) {
257 smbace.flags |= SMB_ACE4_ID_SPECIAL;
258 switch (gace->aceWho) {
259 case ACE4_SPECIAL_OWNER:
260 smbace.who.special_id = SMB_ACE4_WHO_OWNER;
261 break;
262 case ACE4_SPECIAL_GROUP:
263 smbace.who.special_id = SMB_ACE4_WHO_GROUP;
264 break;
265 case ACE4_SPECIAL_EVERYONE:
266 smbace.who.special_id = SMB_ACE4_WHO_EVERYONE;
267 break;
268 default:
269 DEBUG(8, ("invalid special gpfs id %d "
270 "ignored\n", gace->aceWho));
271 continue; /* don't add it */
273 } else {
274 if (gace->aceFlags & ACE4_FLAG_GROUP_ID)
275 smbace.who.gid = gace->aceWho;
276 else
277 smbace.who.uid = gace->aceWho;
280 /* remove redundent deny entries */
281 if (i > 0 && gace->aceType == SMB_ACE4_ACCESS_DENIED_ACE_TYPE) {
282 struct gpfs_ace_v4 *prev = &gacl->ace_v4[i-1];
283 if (prev->aceType == SMB_ACE4_ACCESS_ALLOWED_ACE_TYPE &&
284 prev->aceFlags == gace->aceFlags &&
285 prev->aceIFlags == gace->aceIFlags &&
286 (gace->aceMask & prev->aceMask) == 0 &&
287 gace->aceWho == prev->aceWho) {
288 /* its redundent - skip it */
289 continue;
293 smbace.aceType = gace->aceType;
294 smbace.aceFlags = gace->aceFlags;
295 smbace.aceMask = gace->aceMask;
296 smb_add_ace4(*ppacl, &smbace);
299 return 0;
302 static NTSTATUS gpfsacl_fget_nt_acl(vfs_handle_struct *handle,
303 files_struct *fsp, uint32 security_info,
304 SEC_DESC **ppdesc)
306 SMB4ACL_T *pacl = NULL;
307 int result;
309 *ppdesc = NULL;
310 result = gpfs_get_nfs4_acl(fsp->fsp_name, &pacl);
312 if (result == 0)
313 return smb_fget_nt_acl_nfs4(fsp, security_info, ppdesc, pacl);
315 if (result > 0) {
316 DEBUG(10, ("retrying with posix acl...\n"));
317 return posix_fget_nt_acl(fsp, security_info, ppdesc);
320 /* GPFS ACL was not read, something wrong happened, error code is set in errno */
321 return map_nt_error_from_unix(errno);
324 static NTSTATUS gpfsacl_get_nt_acl(vfs_handle_struct *handle,
325 const char *name,
326 uint32 security_info, SEC_DESC **ppdesc)
328 SMB4ACL_T *pacl = NULL;
329 int result;
331 *ppdesc = NULL;
332 result = gpfs_get_nfs4_acl(name, &pacl);
334 if (result == 0)
335 return smb_get_nt_acl_nfs4(handle->conn, name, security_info, ppdesc, pacl);
337 if (result > 0) {
338 DEBUG(10, ("retrying with posix acl...\n"));
339 return posix_get_nt_acl(handle->conn, name, security_info, ppdesc);
342 /* GPFS ACL was not read, something wrong happened, error code is set in errno */
343 return map_nt_error_from_unix(errno);
346 static bool gpfsacl_process_smbacl(files_struct *fsp, SMB4ACL_T *smbacl)
348 int ret;
349 gpfs_aclLen_t gacl_len;
350 SMB4ACE_T *smbace;
351 struct gpfs_acl *gacl;
352 TALLOC_CTX *mem_ctx = talloc_tos();
354 gacl_len = sizeof(struct gpfs_acl) +
355 (smb_get_naces(smbacl)-1)*sizeof(gpfs_ace_v4_t);
357 gacl = (struct gpfs_acl *)TALLOC_SIZE(mem_ctx, gacl_len);
358 if (gacl == NULL) {
359 DEBUG(0, ("talloc failed\n"));
360 errno = ENOMEM;
361 return False;
364 gacl->acl_len = gacl_len;
365 gacl->acl_level = 0;
366 gacl->acl_version = GPFS_ACL_VERSION_NFS4;
367 gacl->acl_type = GPFS_ACL_TYPE_NFS4;
368 gacl->acl_nace = 0; /* change later... */
370 for (smbace=smb_first_ace4(smbacl); smbace!=NULL; smbace = smb_next_ace4(smbace)) {
371 struct gpfs_ace_v4 *gace = &gacl->ace_v4[gacl->acl_nace];
372 SMB_ACE4PROP_T *aceprop = smb_get_ace4(smbace);
374 gace->aceType = aceprop->aceType;
375 gace->aceFlags = aceprop->aceFlags;
376 gace->aceMask = aceprop->aceMask;
379 * GPFS can't distinguish between WRITE and APPEND on
380 * files, so one being set without the other is an
381 * error. Sorry for the many ()'s :-)
384 if (!fsp->is_directory
386 ((((gace->aceMask & ACE4_MASK_WRITE) == 0)
387 && ((gace->aceMask & ACE4_MASK_APPEND) != 0))
389 (((gace->aceMask & ACE4_MASK_WRITE) != 0)
390 && ((gace->aceMask & ACE4_MASK_APPEND) == 0)))
392 lp_parm_bool(fsp->conn->params->service, "gpfs",
393 "merge_writeappend", True)) {
394 DEBUG(2, ("vfs_gpfs.c: file [%s]: ACE contains "
395 "WRITE^APPEND, setting WRITE|APPEND\n",
396 fsp->fsp_name));
397 gace->aceMask |= ACE4_MASK_WRITE|ACE4_MASK_APPEND;
400 gace->aceIFlags = (aceprop->flags&SMB_ACE4_ID_SPECIAL) ? ACE4_IFLAG_SPECIAL_ID : 0;
402 if (aceprop->flags&SMB_ACE4_ID_SPECIAL)
404 switch(aceprop->who.special_id)
406 case SMB_ACE4_WHO_EVERYONE:
407 gace->aceWho = ACE4_SPECIAL_EVERYONE;
408 break;
409 case SMB_ACE4_WHO_OWNER:
410 gace->aceWho = ACE4_SPECIAL_OWNER;
411 break;
412 case SMB_ACE4_WHO_GROUP:
413 gace->aceWho = ACE4_SPECIAL_GROUP;
414 break;
415 default:
416 DEBUG(8, ("unsupported special_id %d\n", aceprop->who.special_id));
417 continue; /* don't add it !!! */
419 } else {
420 /* just only for the type safety... */
421 if (aceprop->aceFlags&SMB_ACE4_IDENTIFIER_GROUP)
422 gace->aceWho = aceprop->who.gid;
423 else
424 gace->aceWho = aceprop->who.uid;
427 gacl->acl_nace++;
430 ret = smbd_gpfs_putacl(fsp->fsp_name, GPFS_PUTACL_STRUCT | GPFS_ACL_SAMBA, gacl);
431 if (ret != 0) {
432 DEBUG(8, ("gpfs_putacl failed with %s\n", strerror(errno)));
433 gpfs_dumpacl(8, gacl);
434 return False;
437 DEBUG(10, ("gpfs_putacl succeeded\n"));
438 return True;
441 static NTSTATUS gpfsacl_set_nt_acl_internal(files_struct *fsp, uint32 security_info_sent, const SEC_DESC *psd)
443 struct gpfs_acl *acl;
444 NTSTATUS result = NT_STATUS_ACCESS_DENIED;
446 acl = gpfs_getacl_alloc(fsp->fsp_name, 0);
447 if (acl == NULL)
448 return result;
450 if (acl->acl_version&GPFS_ACL_VERSION_NFS4)
452 result = smb_set_nt_acl_nfs4(
453 fsp, security_info_sent, psd,
454 gpfsacl_process_smbacl);
455 } else { /* assume POSIX ACL - by default... */
456 result = set_nt_acl(fsp, security_info_sent, psd);
459 return result;
462 static NTSTATUS gpfsacl_fset_nt_acl(vfs_handle_struct *handle, files_struct *fsp, uint32 security_info_sent, const SEC_DESC *psd)
464 return gpfsacl_set_nt_acl_internal(fsp, security_info_sent, psd);
467 static SMB_ACL_T gpfs2smb_acl(const struct gpfs_acl *pacl)
469 SMB_ACL_T result;
470 int i;
472 result = sys_acl_init(pacl->acl_nace);
473 if (result == NULL) {
474 errno = ENOMEM;
475 return NULL;
478 result->count = pacl->acl_nace;
480 for (i=0; i<pacl->acl_nace; i++) {
481 struct smb_acl_entry *ace = &result->acl[i];
482 const struct gpfs_ace_v1 *g_ace = &pacl->ace_v1[i];
484 DEBUG(10, ("Converting type %d id %lu perm %x\n",
485 (int)g_ace->ace_type, (unsigned long)g_ace->ace_who,
486 (int)g_ace->ace_perm));
488 switch (g_ace->ace_type) {
489 case GPFS_ACL_USER:
490 ace->a_type = SMB_ACL_USER;
491 ace->uid = (uid_t)g_ace->ace_who;
492 break;
493 case GPFS_ACL_USER_OBJ:
494 ace->a_type = SMB_ACL_USER_OBJ;
495 break;
496 case GPFS_ACL_GROUP:
497 ace->a_type = SMB_ACL_GROUP;
498 ace->gid = (gid_t)g_ace->ace_who;
499 break;
500 case GPFS_ACL_GROUP_OBJ:
501 ace->a_type = SMB_ACL_GROUP_OBJ;
502 break;
503 case GPFS_ACL_OTHER:
504 ace->a_type = SMB_ACL_OTHER;
505 break;
506 case GPFS_ACL_MASK:
507 ace->a_type = SMB_ACL_MASK;
508 break;
509 default:
510 DEBUG(10, ("Got invalid ace_type: %d\n",
511 g_ace->ace_type));
512 errno = EINVAL;
513 SAFE_FREE(result);
514 return NULL;
517 ace->a_perm = 0;
518 ace->a_perm |= (g_ace->ace_perm & ACL_PERM_READ) ?
519 SMB_ACL_READ : 0;
520 ace->a_perm |= (g_ace->ace_perm & ACL_PERM_WRITE) ?
521 SMB_ACL_WRITE : 0;
522 ace->a_perm |= (g_ace->ace_perm & ACL_PERM_EXECUTE) ?
523 SMB_ACL_EXECUTE : 0;
525 DEBUGADD(10, ("Converted to %d perm %x\n",
526 ace->a_type, ace->a_perm));
529 return result;
532 static SMB_ACL_T gpfsacl_get_posix_acl(const char *path, gpfs_aclType_t type)
534 struct gpfs_acl *pacl;
535 SMB_ACL_T result = NULL;
537 pacl = gpfs_getacl_alloc(path, type);
539 if (pacl == NULL) {
540 DEBUG(10, ("gpfs_getacl failed for %s with %s\n",
541 path, strerror(errno)));
542 if (errno == 0) {
543 errno = EINVAL;
545 goto done;
548 if (pacl->acl_version != GPFS_ACL_VERSION_POSIX) {
549 DEBUG(10, ("Got acl version %d, expected %d\n",
550 pacl->acl_version, GPFS_ACL_VERSION_POSIX));
551 errno = EINVAL;
552 goto done;
555 DEBUG(10, ("len: %d, level: %d, version: %d, nace: %d\n",
556 pacl->acl_len, pacl->acl_level, pacl->acl_version,
557 pacl->acl_nace));
559 result = gpfs2smb_acl(pacl);
560 if (result == NULL) {
561 goto done;
564 done:
566 if (errno != 0) {
567 SAFE_FREE(result);
569 return result;
572 static SMB_ACL_T gpfsacl_sys_acl_get_file(vfs_handle_struct *handle,
573 const char *path_p,
574 SMB_ACL_TYPE_T type)
576 gpfs_aclType_t gpfs_type;
578 switch(type) {
579 case SMB_ACL_TYPE_ACCESS:
580 gpfs_type = GPFS_ACL_TYPE_ACCESS;
581 break;
582 case SMB_ACL_TYPE_DEFAULT:
583 gpfs_type = GPFS_ACL_TYPE_DEFAULT;
584 break;
585 default:
586 DEBUG(0, ("Got invalid type: %d\n", type));
587 smb_panic("exiting");
590 return gpfsacl_get_posix_acl(path_p, gpfs_type);
593 static SMB_ACL_T gpfsacl_sys_acl_get_fd(vfs_handle_struct *handle,
594 files_struct *fsp)
596 return gpfsacl_get_posix_acl(fsp->fsp_name, GPFS_ACL_TYPE_ACCESS);
599 static struct gpfs_acl *smb2gpfs_acl(const SMB_ACL_T pacl,
600 SMB_ACL_TYPE_T type)
602 gpfs_aclLen_t len;
603 struct gpfs_acl *result;
604 int i;
605 union gpfs_ace_union
607 gpfs_ace_v1_t ace_v1[1]; /* when GPFS_ACL_VERSION_POSIX */
608 gpfs_ace_v4_t ace_v4[1]; /* when GPFS_ACL_VERSION_NFS4 */
611 DEBUG(10, ("smb2gpfs_acl: Got ACL with %d entries\n", pacl->count));
613 len = sizeof(struct gpfs_acl) - sizeof(union gpfs_ace_union) +
614 (pacl->count)*sizeof(gpfs_ace_v1_t);
616 result = (struct gpfs_acl *)SMB_MALLOC(len);
617 if (result == NULL) {
618 errno = ENOMEM;
619 return result;
622 result->acl_len = len;
623 result->acl_level = 0;
624 result->acl_version = GPFS_ACL_VERSION_POSIX;
625 result->acl_type = (type == SMB_ACL_TYPE_DEFAULT) ?
626 GPFS_ACL_TYPE_DEFAULT : GPFS_ACL_TYPE_ACCESS;
627 result->acl_nace = pacl->count;
629 for (i=0; i<pacl->count; i++) {
630 const struct smb_acl_entry *ace = &pacl->acl[i];
631 struct gpfs_ace_v1 *g_ace = &result->ace_v1[i];
633 DEBUG(10, ("Converting type %d perm %x\n",
634 (int)ace->a_type, (int)ace->a_perm));
636 g_ace->ace_perm = 0;
638 switch(ace->a_type) {
639 case SMB_ACL_USER:
640 g_ace->ace_type = GPFS_ACL_USER;
641 g_ace->ace_who = (gpfs_uid_t)ace->uid;
642 break;
643 case SMB_ACL_USER_OBJ:
644 g_ace->ace_type = GPFS_ACL_USER_OBJ;
645 g_ace->ace_perm |= ACL_PERM_CONTROL;
646 g_ace->ace_who = 0;
647 break;
648 case SMB_ACL_GROUP:
649 g_ace->ace_type = GPFS_ACL_GROUP;
650 g_ace->ace_who = (gpfs_uid_t)ace->gid;
651 break;
652 case SMB_ACL_GROUP_OBJ:
653 g_ace->ace_type = GPFS_ACL_GROUP_OBJ;
654 g_ace->ace_who = 0;
655 break;
656 case SMB_ACL_MASK:
657 g_ace->ace_type = GPFS_ACL_MASK;
658 g_ace->ace_perm = 0x8f;
659 g_ace->ace_who = 0;
660 break;
661 case SMB_ACL_OTHER:
662 g_ace->ace_type = GPFS_ACL_OTHER;
663 g_ace->ace_who = 0;
664 break;
665 default:
666 DEBUG(10, ("Got invalid ace_type: %d\n", ace->a_type));
667 errno = EINVAL;
668 SAFE_FREE(result);
669 return NULL;
672 g_ace->ace_perm |= (ace->a_perm & SMB_ACL_READ) ?
673 ACL_PERM_READ : 0;
674 g_ace->ace_perm |= (ace->a_perm & SMB_ACL_WRITE) ?
675 ACL_PERM_WRITE : 0;
676 g_ace->ace_perm |= (ace->a_perm & SMB_ACL_EXECUTE) ?
677 ACL_PERM_EXECUTE : 0;
679 DEBUGADD(10, ("Converted to %d id %d perm %x\n",
680 g_ace->ace_type, g_ace->ace_who, g_ace->ace_perm));
683 return result;
686 static int gpfsacl_sys_acl_set_file(vfs_handle_struct *handle,
687 const char *name,
688 SMB_ACL_TYPE_T type,
689 SMB_ACL_T theacl)
691 struct gpfs_acl *gpfs_acl;
692 int result;
694 gpfs_acl = smb2gpfs_acl(theacl, type);
695 if (gpfs_acl == NULL) {
696 return -1;
699 result = smbd_gpfs_putacl((char *)name, GPFS_PUTACL_STRUCT | GPFS_ACL_SAMBA, gpfs_acl);
701 SAFE_FREE(gpfs_acl);
702 return result;
705 static int gpfsacl_sys_acl_set_fd(vfs_handle_struct *handle,
706 files_struct *fsp,
707 SMB_ACL_T theacl)
709 return gpfsacl_sys_acl_set_file(handle, fsp->fsp_name, SMB_ACL_TYPE_ACCESS, theacl);
712 static int gpfsacl_sys_acl_delete_def_file(vfs_handle_struct *handle,
713 const char *path)
715 errno = ENOTSUP;
716 return -1;
720 * Assumed: mode bits are shiftable and standard
721 * Output: the new aceMask field for an smb nfs4 ace
723 static uint32 gpfsacl_mask_filter(uint32 aceType, uint32 aceMask, uint32 rwx)
725 const uint32 posix_nfs4map[3] = {
726 SMB_ACE4_EXECUTE, /* execute */
727 SMB_ACE4_WRITE_DATA | SMB_ACE4_APPEND_DATA, /* write; GPFS specific */
728 SMB_ACE4_READ_DATA /* read */
730 int i;
731 uint32_t posix_mask = 0x01;
732 uint32_t posix_bit;
733 uint32_t nfs4_bits;
735 for(i=0; i<3; i++) {
736 nfs4_bits = posix_nfs4map[i];
737 posix_bit = rwx & posix_mask;
739 if (aceType==SMB_ACE4_ACCESS_ALLOWED_ACE_TYPE) {
740 if (posix_bit)
741 aceMask |= nfs4_bits;
742 else
743 aceMask &= ~nfs4_bits;
744 } else {
745 /* add deny bits when suitable */
746 if (!posix_bit)
747 aceMask |= nfs4_bits;
748 else
749 aceMask &= ~nfs4_bits;
750 } /* other ace types are unexpected */
752 posix_mask <<= 1;
755 return aceMask;
758 static int gpfsacl_emu_chmod(const char *path, mode_t mode)
760 SMB4ACL_T *pacl = NULL;
761 int result;
762 bool haveAllowEntry[SMB_ACE4_WHO_EVERYONE + 1] = {False, False, False, False};
763 int i;
764 files_struct fake_fsp; /* TODO: rationalize parametrization */
765 SMB4ACE_T *smbace;
767 DEBUG(10, ("gpfsacl_emu_chmod invoked for %s mode %o\n", path, mode));
769 result = gpfs_get_nfs4_acl(path, &pacl);
770 if (result)
771 return result;
773 if (mode & ~(S_IRWXU | S_IRWXG | S_IRWXO)) {
774 DEBUG(2, ("WARNING: cutting extra mode bits %o on %s\n", mode, path));
777 for (smbace=smb_first_ace4(pacl); smbace!=NULL; smbace = smb_next_ace4(smbace)) {
778 SMB_ACE4PROP_T *ace = smb_get_ace4(smbace);
779 uint32_t specid = ace->who.special_id;
781 if (ace->flags&SMB_ACE4_ID_SPECIAL &&
782 ace->aceType<=SMB_ACE4_ACCESS_DENIED_ACE_TYPE &&
783 specid <= SMB_ACE4_WHO_EVERYONE) {
785 uint32_t newMask;
787 if (ace->aceType==SMB_ACE4_ACCESS_ALLOWED_ACE_TYPE)
788 haveAllowEntry[specid] = True;
790 /* mode >> 6 for @owner, mode >> 3 for @group,
791 * mode >> 0 for @everyone */
792 newMask = gpfsacl_mask_filter(ace->aceType, ace->aceMask,
793 mode >> ((SMB_ACE4_WHO_EVERYONE - specid) * 3));
794 if (ace->aceMask!=newMask) {
795 DEBUG(10, ("ace changed for %s (%o -> %o) id=%d\n",
796 path, ace->aceMask, newMask, specid));
798 ace->aceMask = newMask;
802 /* make sure we have at least ALLOW entries
803 * for all the 3 special ids (@EVERYONE, @OWNER, @GROUP)
804 * - if necessary
806 for(i = SMB_ACE4_WHO_OWNER; i<=SMB_ACE4_WHO_EVERYONE; i++) {
807 SMB_ACE4PROP_T ace;
809 if (haveAllowEntry[i]==True)
810 continue;
812 ZERO_STRUCT(ace);
813 ace.aceType = SMB_ACE4_ACCESS_ALLOWED_ACE_TYPE;
814 ace.flags |= SMB_ACE4_ID_SPECIAL;
815 ace.who.special_id = i;
817 if (i==SMB_ACE4_WHO_GROUP) /* not sure it's necessary... */
818 ace.aceFlags |= SMB_ACE4_IDENTIFIER_GROUP;
820 ace.aceMask = gpfsacl_mask_filter(ace.aceType, ace.aceMask,
821 mode >> ((SMB_ACE4_WHO_EVERYONE - i) * 3));
823 /* don't add unnecessary aces */
824 if (!ace.aceMask)
825 continue;
827 /* we add it to the END - as windows expects allow aces */
828 smb_add_ace4(pacl, &ace);
829 DEBUG(10, ("Added ALLOW ace for %s, mode=%o, id=%d, aceMask=%x\n",
830 path, mode, i, ace.aceMask));
833 /* don't add complementary DENY ACEs here */
834 ZERO_STRUCT(fake_fsp);
835 fake_fsp.fsp_name = (char *)path; /* no file_new is needed here */
837 /* put the acl */
838 if (gpfsacl_process_smbacl(&fake_fsp, pacl) == False)
839 return -1;
840 return 0; /* ok for [f]chmod */
843 static int vfs_gpfs_chmod(vfs_handle_struct *handle, const char *path, mode_t mode)
845 SMB_STRUCT_STAT st;
846 int rc;
848 if (SMB_VFS_NEXT_STAT(handle, path, &st) != 0) {
849 return -1;
852 /* avoid chmod() if possible, to preserve acls */
853 if ((st.st_mode & ~S_IFMT) == mode) {
854 return 0;
857 rc = gpfsacl_emu_chmod(path, mode);
858 if (rc == 1)
859 return SMB_VFS_NEXT_CHMOD(handle, path, mode);
860 return rc;
863 static int vfs_gpfs_fchmod(vfs_handle_struct *handle, files_struct *fsp, mode_t mode)
865 SMB_STRUCT_STAT st;
866 int rc;
868 if (SMB_VFS_NEXT_FSTAT(handle, fsp, &st) != 0) {
869 return -1;
872 /* avoid chmod() if possible, to preserve acls */
873 if ((st.st_mode & ~S_IFMT) == mode) {
874 return 0;
877 rc = gpfsacl_emu_chmod(fsp->fsp_name, mode);
878 if (rc == 1)
879 return SMB_VFS_NEXT_FCHMOD(handle, fsp, mode);
880 return rc;
883 /* VFS operations structure */
885 static vfs_op_tuple gpfs_op_tuples[] = {
887 { SMB_VFS_OP(vfs_gpfs_kernel_flock),
888 SMB_VFS_OP_KERNEL_FLOCK,
889 SMB_VFS_LAYER_OPAQUE },
891 { SMB_VFS_OP(vfs_gpfs_setlease),
892 SMB_VFS_OP_LINUX_SETLEASE,
893 SMB_VFS_LAYER_OPAQUE },
895 { SMB_VFS_OP(vfs_gpfs_get_real_filename),
896 SMB_VFS_OP_GET_REAL_FILENAME,
897 SMB_VFS_LAYER_OPAQUE },
899 { SMB_VFS_OP(gpfsacl_fget_nt_acl),
900 SMB_VFS_OP_FGET_NT_ACL,
901 SMB_VFS_LAYER_TRANSPARENT },
903 { SMB_VFS_OP(gpfsacl_get_nt_acl),
904 SMB_VFS_OP_GET_NT_ACL,
905 SMB_VFS_LAYER_TRANSPARENT },
907 { SMB_VFS_OP(gpfsacl_fset_nt_acl),
908 SMB_VFS_OP_FSET_NT_ACL,
909 SMB_VFS_LAYER_TRANSPARENT },
911 { SMB_VFS_OP(gpfsacl_sys_acl_get_file),
912 SMB_VFS_OP_SYS_ACL_GET_FILE,
913 SMB_VFS_LAYER_TRANSPARENT },
915 { SMB_VFS_OP(gpfsacl_sys_acl_get_fd),
916 SMB_VFS_OP_SYS_ACL_GET_FD,
917 SMB_VFS_LAYER_TRANSPARENT },
919 { SMB_VFS_OP(gpfsacl_sys_acl_set_file),
920 SMB_VFS_OP_SYS_ACL_SET_FILE,
921 SMB_VFS_LAYER_TRANSPARENT },
923 { SMB_VFS_OP(gpfsacl_sys_acl_set_fd),
924 SMB_VFS_OP_SYS_ACL_SET_FD,
925 SMB_VFS_LAYER_TRANSPARENT },
927 { SMB_VFS_OP(gpfsacl_sys_acl_delete_def_file),
928 SMB_VFS_OP_SYS_ACL_DELETE_DEF_FILE,
929 SMB_VFS_LAYER_TRANSPARENT },
931 { SMB_VFS_OP(vfs_gpfs_chmod),
932 SMB_VFS_OP_CHMOD,
933 SMB_VFS_LAYER_TRANSPARENT },
935 { SMB_VFS_OP(vfs_gpfs_fchmod),
936 SMB_VFS_OP_FCHMOD,
937 SMB_VFS_LAYER_TRANSPARENT },
939 { SMB_VFS_OP(vfs_gpfs_close),
940 SMB_VFS_OP_CLOSE,
941 SMB_VFS_LAYER_TRANSPARENT },
943 { SMB_VFS_OP(NULL), SMB_VFS_OP_NOOP, SMB_VFS_LAYER_NOOP }
948 NTSTATUS vfs_gpfs_init(void);
949 NTSTATUS vfs_gpfs_init(void)
951 init_gpfs();
953 return smb_register_vfs(SMB_VFS_INTERFACE_VERSION, "gpfs",
954 gpfs_op_tuples);