Fix bug #Bug 6090 renaming or deleting a "not matching/resolving" symlink is failing.
[Samba.git] / source3 / modules / vfs_gpfs.c
blob1d7cdba01412c975a8635c176bdce0da0b146a92
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_setlease(vfs_handle_struct *handle, files_struct *fsp,
53 int leasetype)
55 int ret;
57 START_PROFILE(syscall_linux_setlease);
59 if ( linux_set_lease_sighandler(fsp->fh->fd) == -1)
60 return -1;
62 ret = set_gpfs_lease(fsp->fh->fd,leasetype);
64 if ( ret < 0 ) {
65 /* This must have come from GPFS not being available */
66 /* or some other error, hence call the default */
67 ret = linux_setlease(fsp->fh->fd, leasetype);
70 END_PROFILE(syscall_linux_setlease);
72 return ret;
75 static int vfs_gpfs_get_real_filename(struct vfs_handle_struct *handle,
76 const char *path,
77 const char *name,
78 TALLOC_CTX *mem_ctx,
79 char **found_name)
81 int result;
82 char *full_path;
83 char real_pathname[PATH_MAX+1];
84 int buflen;
86 full_path = talloc_asprintf(talloc_tos(), "%s/%s", path, name);
87 if (full_path == NULL) {
88 errno = ENOMEM;
89 return -1;
92 buflen = sizeof(real_pathname) - 1;
94 result = smbd_gpfs_get_realfilename_path(full_path, real_pathname,
95 &buflen);
97 TALLOC_FREE(full_path);
99 if (result == -1) {
100 DEBUG(10, ("smbd_gpfs_get_realfilename_path returned %s\n",
101 strerror(errno)));
102 return -1;
106 * GPFS does not necessarily null-terminate the returned path
107 * but instead returns the buffer length in buflen.
110 if (buflen < sizeof(real_pathname)) {
111 real_pathname[buflen] = '\0';
112 } else {
113 real_pathname[sizeof(real_pathname)-1] = '\0';
116 DEBUG(10, ("smbd_gpfs_get_realfilename_path: %s/%s -> %s\n",
117 path, name, real_pathname));
119 name = strrchr_m(real_pathname, '/');
120 if (name == NULL) {
121 errno = ENOENT;
122 return -1;
125 *found_name = talloc_strdup(mem_ctx, name+1);
126 if (*found_name == NULL) {
127 errno = ENOMEM;
128 return -1;
131 return 0;
134 static void gpfs_dumpacl(int level, struct gpfs_acl *gacl)
136 int i;
137 if (gacl==NULL)
139 DEBUG(0, ("gpfs acl is NULL\n"));
140 return;
143 DEBUG(level, ("gpfs acl: nace: %d, type:%d, version:%d, level:%d, len:%d\n",
144 gacl->acl_nace, gacl->acl_type, gacl->acl_version, gacl->acl_level, gacl->acl_len));
145 for(i=0; i<gacl->acl_nace; i++)
147 struct gpfs_ace_v4 *gace = gacl->ace_v4 + i;
148 DEBUG(level, ("\tace[%d]: type:%d, flags:0x%x, mask:0x%x, iflags:0x%x, who:%u\n",
149 i, gace->aceType, gace->aceFlags, gace->aceMask,
150 gace->aceIFlags, gace->aceWho));
154 static struct gpfs_acl *gpfs_getacl_alloc(const char *fname, gpfs_aclType_t type)
156 struct gpfs_acl *acl;
157 size_t len = 200;
158 int ret;
159 TALLOC_CTX *mem_ctx = talloc_tos();
161 acl = (struct gpfs_acl *)TALLOC_SIZE(mem_ctx, len);
162 if (acl == NULL) {
163 errno = ENOMEM;
164 return NULL;
167 acl->acl_len = len;
168 acl->acl_level = 0;
169 acl->acl_version = 0;
170 acl->acl_type = type;
172 ret = smbd_gpfs_getacl((char *)fname, GPFS_GETACL_STRUCT | GPFS_ACL_SAMBA, acl);
173 if ((ret != 0) && (errno == ENOSPC)) {
174 struct gpfs_acl *new_acl = (struct gpfs_acl *)TALLOC_SIZE(
175 mem_ctx, acl->acl_len + sizeof(struct gpfs_acl));
176 if (new_acl == NULL) {
177 errno = ENOMEM;
178 return NULL;
181 new_acl->acl_len = acl->acl_len;
182 new_acl->acl_level = acl->acl_level;
183 new_acl->acl_version = acl->acl_version;
184 new_acl->acl_type = acl->acl_type;
185 acl = new_acl;
187 ret = smbd_gpfs_getacl((char *)fname, GPFS_GETACL_STRUCT | GPFS_ACL_SAMBA, acl);
189 if (ret != 0)
191 DEBUG(8, ("smbd_gpfs_getacl failed with %s\n",strerror(errno)));
192 return NULL;
195 return acl;
198 /* Tries to get nfs4 acls and returns SMB ACL allocated.
199 * On failure returns 1 if it got non-NFSv4 ACL to prompt
200 * retry with POSIX ACL checks.
201 * On failure returns -1 if there is system (GPFS) error, check errno.
202 * Returns 0 on success
204 static int gpfs_get_nfs4_acl(const char *fname, SMB4ACL_T **ppacl)
206 int i;
207 struct gpfs_acl *gacl = NULL;
208 DEBUG(10, ("gpfs_get_nfs4_acl invoked for %s\n", fname));
210 /* First get the real acl length */
211 gacl = gpfs_getacl_alloc(fname, 0);
212 if (gacl == NULL) {
213 DEBUG(9, ("gpfs_getacl failed for %s with %s\n",
214 fname, strerror(errno)));
215 return -1;
218 if (gacl->acl_type != GPFS_ACL_TYPE_NFS4) {
219 DEBUG(10, ("Got non-nfsv4 acl\n"));
220 /* Retry with POSIX ACLs check */
221 return 1;
224 *ppacl = smb_create_smb4acl();
226 DEBUG(10, ("len: %d, level: %d, version: %d, nace: %d\n",
227 gacl->acl_len, gacl->acl_level, gacl->acl_version,
228 gacl->acl_nace));
230 for (i=0; i<gacl->acl_nace; i++) {
231 struct gpfs_ace_v4 *gace = &gacl->ace_v4[i];
232 SMB_ACE4PROP_T smbace;
233 DEBUG(10, ("type: %d, iflags: %x, flags: %x, mask: %x, "
234 "who: %d\n", gace->aceType, gace->aceIFlags,
235 gace->aceFlags, gace->aceMask, gace->aceWho));
237 ZERO_STRUCT(smbace);
238 if (gace->aceIFlags & ACE4_IFLAG_SPECIAL_ID) {
239 smbace.flags |= SMB_ACE4_ID_SPECIAL;
240 switch (gace->aceWho) {
241 case ACE4_SPECIAL_OWNER:
242 smbace.who.special_id = SMB_ACE4_WHO_OWNER;
243 break;
244 case ACE4_SPECIAL_GROUP:
245 smbace.who.special_id = SMB_ACE4_WHO_GROUP;
246 break;
247 case ACE4_SPECIAL_EVERYONE:
248 smbace.who.special_id = SMB_ACE4_WHO_EVERYONE;
249 break;
250 default:
251 DEBUG(8, ("invalid special gpfs id %d "
252 "ignored\n", gace->aceWho));
253 continue; /* don't add it */
255 } else {
256 if (gace->aceFlags & ACE4_FLAG_GROUP_ID)
257 smbace.who.gid = gace->aceWho;
258 else
259 smbace.who.uid = gace->aceWho;
262 /* remove redundent deny entries */
263 if (i > 0 && gace->aceType == SMB_ACE4_ACCESS_DENIED_ACE_TYPE) {
264 struct gpfs_ace_v4 *prev = &gacl->ace_v4[i-1];
265 if (prev->aceType == SMB_ACE4_ACCESS_ALLOWED_ACE_TYPE &&
266 prev->aceFlags == gace->aceFlags &&
267 prev->aceIFlags == gace->aceIFlags &&
268 (gace->aceMask & prev->aceMask) == 0 &&
269 gace->aceWho == prev->aceWho) {
270 /* its redundent - skip it */
271 continue;
275 smbace.aceType = gace->aceType;
276 smbace.aceFlags = gace->aceFlags;
277 smbace.aceMask = gace->aceMask;
278 smb_add_ace4(*ppacl, &smbace);
281 return 0;
284 static NTSTATUS gpfsacl_fget_nt_acl(vfs_handle_struct *handle,
285 files_struct *fsp, uint32 security_info,
286 SEC_DESC **ppdesc)
288 SMB4ACL_T *pacl = NULL;
289 int result;
291 *ppdesc = NULL;
292 result = gpfs_get_nfs4_acl(fsp->fsp_name, &pacl);
294 if (result == 0)
295 return smb_fget_nt_acl_nfs4(fsp, security_info, ppdesc, pacl);
297 if (result > 0) {
298 DEBUG(10, ("retrying with posix acl...\n"));
299 return posix_fget_nt_acl(fsp, security_info, ppdesc);
302 /* GPFS ACL was not read, something wrong happened, error code is set in errno */
303 return map_nt_error_from_unix(errno);
306 static NTSTATUS gpfsacl_get_nt_acl(vfs_handle_struct *handle,
307 const char *name,
308 uint32 security_info, SEC_DESC **ppdesc)
310 SMB4ACL_T *pacl = NULL;
311 int result;
313 *ppdesc = NULL;
314 result = gpfs_get_nfs4_acl(name, &pacl);
316 if (result == 0)
317 return smb_get_nt_acl_nfs4(handle->conn, name, security_info, ppdesc, pacl);
319 if (result > 0) {
320 DEBUG(10, ("retrying with posix acl...\n"));
321 return posix_get_nt_acl(handle->conn, name, security_info, ppdesc);
324 /* GPFS ACL was not read, something wrong happened, error code is set in errno */
325 return map_nt_error_from_unix(errno);
328 static bool gpfsacl_process_smbacl(files_struct *fsp, SMB4ACL_T *smbacl)
330 int ret;
331 gpfs_aclLen_t gacl_len;
332 SMB4ACE_T *smbace;
333 struct gpfs_acl *gacl;
334 TALLOC_CTX *mem_ctx = talloc_tos();
336 gacl_len = sizeof(struct gpfs_acl) +
337 (smb_get_naces(smbacl)-1)*sizeof(gpfs_ace_v4_t);
339 gacl = TALLOC_SIZE(mem_ctx, gacl_len);
340 if (gacl == NULL) {
341 DEBUG(0, ("talloc failed\n"));
342 errno = ENOMEM;
343 return False;
346 gacl->acl_len = gacl_len;
347 gacl->acl_level = 0;
348 gacl->acl_version = GPFS_ACL_VERSION_NFS4;
349 gacl->acl_type = GPFS_ACL_TYPE_NFS4;
350 gacl->acl_nace = 0; /* change later... */
352 for (smbace=smb_first_ace4(smbacl); smbace!=NULL; smbace = smb_next_ace4(smbace)) {
353 struct gpfs_ace_v4 *gace = &gacl->ace_v4[gacl->acl_nace];
354 SMB_ACE4PROP_T *aceprop = smb_get_ace4(smbace);
356 gace->aceType = aceprop->aceType;
357 gace->aceFlags = aceprop->aceFlags;
358 gace->aceMask = aceprop->aceMask;
361 * GPFS can't distinguish between WRITE and APPEND on
362 * files, so one being set without the other is an
363 * error. Sorry for the many ()'s :-)
366 if (!fsp->is_directory
368 ((((gace->aceMask & ACE4_MASK_WRITE) == 0)
369 && ((gace->aceMask & ACE4_MASK_APPEND) != 0))
371 (((gace->aceMask & ACE4_MASK_WRITE) != 0)
372 && ((gace->aceMask & ACE4_MASK_APPEND) == 0)))
374 lp_parm_bool(fsp->conn->params->service, "gpfs",
375 "merge_writeappend", True)) {
376 DEBUG(2, ("vfs_gpfs.c: file [%s]: ACE contains "
377 "WRITE^APPEND, setting WRITE|APPEND\n",
378 fsp->fsp_name));
379 gace->aceMask |= ACE4_MASK_WRITE|ACE4_MASK_APPEND;
382 gace->aceIFlags = (aceprop->flags&SMB_ACE4_ID_SPECIAL) ? ACE4_IFLAG_SPECIAL_ID : 0;
384 if (aceprop->flags&SMB_ACE4_ID_SPECIAL)
386 switch(aceprop->who.special_id)
388 case SMB_ACE4_WHO_EVERYONE:
389 gace->aceWho = ACE4_SPECIAL_EVERYONE;
390 break;
391 case SMB_ACE4_WHO_OWNER:
392 gace->aceWho = ACE4_SPECIAL_OWNER;
393 break;
394 case SMB_ACE4_WHO_GROUP:
395 gace->aceWho = ACE4_SPECIAL_GROUP;
396 break;
397 default:
398 DEBUG(8, ("unsupported special_id %d\n", aceprop->who.special_id));
399 continue; /* don't add it !!! */
401 } else {
402 /* just only for the type safety... */
403 if (aceprop->aceFlags&SMB_ACE4_IDENTIFIER_GROUP)
404 gace->aceWho = aceprop->who.gid;
405 else
406 gace->aceWho = aceprop->who.uid;
409 gacl->acl_nace++;
412 ret = smbd_gpfs_putacl(fsp->fsp_name, GPFS_PUTACL_STRUCT | GPFS_ACL_SAMBA, gacl);
413 if (ret != 0) {
414 DEBUG(8, ("gpfs_putacl failed with %s\n", strerror(errno)));
415 gpfs_dumpacl(8, gacl);
416 return False;
419 DEBUG(10, ("gpfs_putacl succeeded\n"));
420 return True;
423 static NTSTATUS gpfsacl_set_nt_acl_internal(files_struct *fsp, uint32 security_info_sent, const SEC_DESC *psd)
425 struct gpfs_acl *acl;
426 NTSTATUS result = NT_STATUS_ACCESS_DENIED;
428 acl = gpfs_getacl_alloc(fsp->fsp_name, 0);
429 if (acl == NULL)
430 return result;
432 if (acl->acl_version&GPFS_ACL_VERSION_NFS4)
434 result = smb_set_nt_acl_nfs4(
435 fsp, security_info_sent, psd,
436 gpfsacl_process_smbacl);
437 } else { /* assume POSIX ACL - by default... */
438 result = set_nt_acl(fsp, security_info_sent, psd);
441 return result;
444 static NTSTATUS gpfsacl_fset_nt_acl(vfs_handle_struct *handle, files_struct *fsp, uint32 security_info_sent, const SEC_DESC *psd)
446 return gpfsacl_set_nt_acl_internal(fsp, security_info_sent, psd);
449 static SMB_ACL_T gpfs2smb_acl(const struct gpfs_acl *pacl)
451 SMB_ACL_T result;
452 int i;
454 result = sys_acl_init(pacl->acl_nace);
455 if (result == NULL) {
456 errno = ENOMEM;
457 return NULL;
460 result->count = pacl->acl_nace;
462 for (i=0; i<pacl->acl_nace; i++) {
463 struct smb_acl_entry *ace = &result->acl[i];
464 const struct gpfs_ace_v1 *g_ace = &pacl->ace_v1[i];
466 DEBUG(10, ("Converting type %d id %lu perm %x\n",
467 (int)g_ace->ace_type, (unsigned long)g_ace->ace_who,
468 (int)g_ace->ace_perm));
470 switch (g_ace->ace_type) {
471 case GPFS_ACL_USER:
472 ace->a_type = SMB_ACL_USER;
473 ace->uid = (uid_t)g_ace->ace_who;
474 break;
475 case GPFS_ACL_USER_OBJ:
476 ace->a_type = SMB_ACL_USER_OBJ;
477 break;
478 case GPFS_ACL_GROUP:
479 ace->a_type = SMB_ACL_GROUP;
480 ace->gid = (gid_t)g_ace->ace_who;
481 break;
482 case GPFS_ACL_GROUP_OBJ:
483 ace->a_type = SMB_ACL_GROUP_OBJ;
484 break;
485 case GPFS_ACL_OTHER:
486 ace->a_type = SMB_ACL_OTHER;
487 break;
488 case GPFS_ACL_MASK:
489 ace->a_type = SMB_ACL_MASK;
490 break;
491 default:
492 DEBUG(10, ("Got invalid ace_type: %d\n",
493 g_ace->ace_type));
494 errno = EINVAL;
495 SAFE_FREE(result);
496 return NULL;
499 ace->a_perm = 0;
500 ace->a_perm |= (g_ace->ace_perm & ACL_PERM_READ) ?
501 SMB_ACL_READ : 0;
502 ace->a_perm |= (g_ace->ace_perm & ACL_PERM_WRITE) ?
503 SMB_ACL_WRITE : 0;
504 ace->a_perm |= (g_ace->ace_perm & ACL_PERM_EXECUTE) ?
505 SMB_ACL_EXECUTE : 0;
507 DEBUGADD(10, ("Converted to %d perm %x\n",
508 ace->a_type, ace->a_perm));
511 return result;
514 static SMB_ACL_T gpfsacl_get_posix_acl(const char *path, gpfs_aclType_t type)
516 struct gpfs_acl *pacl;
517 SMB_ACL_T result = NULL;
519 pacl = gpfs_getacl_alloc(path, type);
521 if (pacl == NULL) {
522 DEBUG(10, ("gpfs_getacl failed for %s with %s\n",
523 path, strerror(errno)));
524 if (errno == 0) {
525 errno = EINVAL;
527 goto done;
530 if (pacl->acl_version != GPFS_ACL_VERSION_POSIX) {
531 DEBUG(10, ("Got acl version %d, expected %d\n",
532 pacl->acl_version, GPFS_ACL_VERSION_POSIX));
533 errno = EINVAL;
534 goto done;
537 DEBUG(10, ("len: %d, level: %d, version: %d, nace: %d\n",
538 pacl->acl_len, pacl->acl_level, pacl->acl_version,
539 pacl->acl_nace));
541 result = gpfs2smb_acl(pacl);
542 if (result == NULL) {
543 goto done;
546 done:
548 if (errno != 0) {
549 SAFE_FREE(result);
551 return result;
554 SMB_ACL_T gpfsacl_sys_acl_get_file(vfs_handle_struct *handle,
555 const char *path_p,
556 SMB_ACL_TYPE_T type)
558 gpfs_aclType_t gpfs_type;
560 switch(type) {
561 case SMB_ACL_TYPE_ACCESS:
562 gpfs_type = GPFS_ACL_TYPE_ACCESS;
563 break;
564 case SMB_ACL_TYPE_DEFAULT:
565 gpfs_type = GPFS_ACL_TYPE_DEFAULT;
566 break;
567 default:
568 DEBUG(0, ("Got invalid type: %d\n", type));
569 smb_panic("exiting");
572 return gpfsacl_get_posix_acl(path_p, gpfs_type);
575 SMB_ACL_T gpfsacl_sys_acl_get_fd(vfs_handle_struct *handle,
576 files_struct *fsp)
578 return gpfsacl_get_posix_acl(fsp->fsp_name, GPFS_ACL_TYPE_ACCESS);
581 static struct gpfs_acl *smb2gpfs_acl(const SMB_ACL_T pacl,
582 SMB_ACL_TYPE_T type)
584 gpfs_aclLen_t len;
585 struct gpfs_acl *result;
586 int i;
587 union gpfs_ace_union
589 gpfs_ace_v1_t ace_v1[1]; /* when GPFS_ACL_VERSION_POSIX */
590 gpfs_ace_v4_t ace_v4[1]; /* when GPFS_ACL_VERSION_NFS4 */
593 DEBUG(10, ("smb2gpfs_acl: Got ACL with %d entries\n", pacl->count));
595 len = sizeof(struct gpfs_acl) - sizeof(union gpfs_ace_union) +
596 (pacl->count)*sizeof(gpfs_ace_v1_t);
598 result = SMB_MALLOC(len);
599 if (result == NULL) {
600 errno = ENOMEM;
601 return result;
604 result->acl_len = len;
605 result->acl_level = 0;
606 result->acl_version = GPFS_ACL_VERSION_POSIX;
607 result->acl_type = (type == SMB_ACL_TYPE_DEFAULT) ?
608 GPFS_ACL_TYPE_DEFAULT : GPFS_ACL_TYPE_ACCESS;
609 result->acl_nace = pacl->count;
611 for (i=0; i<pacl->count; i++) {
612 const struct smb_acl_entry *ace = &pacl->acl[i];
613 struct gpfs_ace_v1 *g_ace = &result->ace_v1[i];
615 DEBUG(10, ("Converting type %d perm %x\n",
616 (int)ace->a_type, (int)ace->a_perm));
618 g_ace->ace_perm = 0;
620 switch(ace->a_type) {
621 case SMB_ACL_USER:
622 g_ace->ace_type = GPFS_ACL_USER;
623 g_ace->ace_who = (gpfs_uid_t)ace->uid;
624 break;
625 case SMB_ACL_USER_OBJ:
626 g_ace->ace_type = GPFS_ACL_USER_OBJ;
627 g_ace->ace_perm |= ACL_PERM_CONTROL;
628 g_ace->ace_who = 0;
629 break;
630 case SMB_ACL_GROUP:
631 g_ace->ace_type = GPFS_ACL_GROUP;
632 g_ace->ace_who = (gpfs_uid_t)ace->gid;
633 break;
634 case SMB_ACL_GROUP_OBJ:
635 g_ace->ace_type = GPFS_ACL_GROUP_OBJ;
636 g_ace->ace_who = 0;
637 break;
638 case SMB_ACL_MASK:
639 g_ace->ace_type = GPFS_ACL_MASK;
640 g_ace->ace_perm = 0x8f;
641 g_ace->ace_who = 0;
642 break;
643 case SMB_ACL_OTHER:
644 g_ace->ace_type = GPFS_ACL_OTHER;
645 g_ace->ace_who = 0;
646 break;
647 default:
648 DEBUG(10, ("Got invalid ace_type: %d\n", ace->a_type));
649 errno = EINVAL;
650 SAFE_FREE(result);
651 return NULL;
654 g_ace->ace_perm |= (ace->a_perm & SMB_ACL_READ) ?
655 ACL_PERM_READ : 0;
656 g_ace->ace_perm |= (ace->a_perm & SMB_ACL_WRITE) ?
657 ACL_PERM_WRITE : 0;
658 g_ace->ace_perm |= (ace->a_perm & SMB_ACL_EXECUTE) ?
659 ACL_PERM_EXECUTE : 0;
661 DEBUGADD(10, ("Converted to %d id %d perm %x\n",
662 g_ace->ace_type, g_ace->ace_who, g_ace->ace_perm));
665 return result;
668 int gpfsacl_sys_acl_set_file(vfs_handle_struct *handle,
669 const char *name,
670 SMB_ACL_TYPE_T type,
671 SMB_ACL_T theacl)
673 struct gpfs_acl *gpfs_acl;
674 int result;
676 gpfs_acl = smb2gpfs_acl(theacl, type);
677 if (gpfs_acl == NULL) {
678 return -1;
681 result = smbd_gpfs_putacl((char *)name, GPFS_PUTACL_STRUCT | GPFS_ACL_SAMBA, gpfs_acl);
683 SAFE_FREE(gpfs_acl);
684 return result;
687 int gpfsacl_sys_acl_set_fd(vfs_handle_struct *handle,
688 files_struct *fsp,
689 SMB_ACL_T theacl)
691 return gpfsacl_sys_acl_set_file(handle, fsp->fsp_name, SMB_ACL_TYPE_ACCESS, theacl);
694 int gpfsacl_sys_acl_delete_def_file(vfs_handle_struct *handle,
695 const char *path)
697 errno = ENOTSUP;
698 return -1;
702 * Assumed: mode bits are shiftable and standard
703 * Output: the new aceMask field for an smb nfs4 ace
705 static uint32 gpfsacl_mask_filter(uint32 aceType, uint32 aceMask, uint32 rwx)
707 const uint32 posix_nfs4map[3] = {
708 SMB_ACE4_EXECUTE, /* execute */
709 SMB_ACE4_WRITE_DATA | SMB_ACE4_APPEND_DATA, /* write; GPFS specific */
710 SMB_ACE4_READ_DATA /* read */
712 int i;
713 uint32_t posix_mask = 0x01;
714 uint32_t posix_bit;
715 uint32_t nfs4_bits;
717 for(i=0; i<3; i++) {
718 nfs4_bits = posix_nfs4map[i];
719 posix_bit = rwx & posix_mask;
721 if (aceType==SMB_ACE4_ACCESS_ALLOWED_ACE_TYPE) {
722 if (posix_bit)
723 aceMask |= nfs4_bits;
724 else
725 aceMask &= ~nfs4_bits;
726 } else {
727 /* add deny bits when suitable */
728 if (!posix_bit)
729 aceMask |= nfs4_bits;
730 else
731 aceMask &= ~nfs4_bits;
732 } /* other ace types are unexpected */
734 posix_mask <<= 1;
737 return aceMask;
740 static int gpfsacl_emu_chmod(const char *path, mode_t mode)
742 SMB4ACL_T *pacl = NULL;
743 int result;
744 bool haveAllowEntry[SMB_ACE4_WHO_EVERYONE + 1] = {False, False, False, False};
745 int i;
746 files_struct fake_fsp; /* TODO: rationalize parametrization */
747 SMB4ACE_T *smbace;
749 DEBUG(10, ("gpfsacl_emu_chmod invoked for %s mode %o\n", path, mode));
751 result = gpfs_get_nfs4_acl(path, &pacl);
752 if (result)
753 return result;
755 if (mode & ~(S_IRWXU | S_IRWXG | S_IRWXO)) {
756 DEBUG(2, ("WARNING: cutting extra mode bits %o on %s\n", mode, path));
759 for (smbace=smb_first_ace4(pacl); smbace!=NULL; smbace = smb_next_ace4(smbace)) {
760 SMB_ACE4PROP_T *ace = smb_get_ace4(smbace);
761 uint32_t specid = ace->who.special_id;
763 if (ace->flags&SMB_ACE4_ID_SPECIAL &&
764 ace->aceType<=SMB_ACE4_ACCESS_DENIED_ACE_TYPE &&
765 specid <= SMB_ACE4_WHO_EVERYONE) {
767 uint32_t newMask;
769 if (ace->aceType==SMB_ACE4_ACCESS_ALLOWED_ACE_TYPE)
770 haveAllowEntry[specid] = True;
772 /* mode >> 6 for @owner, mode >> 3 for @group,
773 * mode >> 0 for @everyone */
774 newMask = gpfsacl_mask_filter(ace->aceType, ace->aceMask,
775 mode >> ((SMB_ACE4_WHO_EVERYONE - specid) * 3));
776 if (ace->aceMask!=newMask) {
777 DEBUG(10, ("ace changed for %s (%o -> %o) id=%d\n",
778 path, ace->aceMask, newMask, specid));
780 ace->aceMask = newMask;
784 /* make sure we have at least ALLOW entries
785 * for all the 3 special ids (@EVERYONE, @OWNER, @GROUP)
786 * - if necessary
788 for(i = SMB_ACE4_WHO_OWNER; i<=SMB_ACE4_WHO_EVERYONE; i++) {
789 SMB_ACE4PROP_T ace;
791 if (haveAllowEntry[i]==True)
792 continue;
794 ZERO_STRUCT(ace);
795 ace.aceType = SMB_ACE4_ACCESS_ALLOWED_ACE_TYPE;
796 ace.flags |= SMB_ACE4_ID_SPECIAL;
797 ace.who.special_id = i;
799 if (i==SMB_ACE4_WHO_GROUP) /* not sure it's necessary... */
800 ace.aceFlags |= SMB_ACE4_IDENTIFIER_GROUP;
802 ace.aceMask = gpfsacl_mask_filter(ace.aceType, ace.aceMask,
803 mode >> ((SMB_ACE4_WHO_EVERYONE - i) * 3));
805 /* don't add unnecessary aces */
806 if (!ace.aceMask)
807 continue;
809 /* we add it to the END - as windows expects allow aces */
810 smb_add_ace4(pacl, &ace);
811 DEBUG(10, ("Added ALLOW ace for %s, mode=%o, id=%d, aceMask=%x\n",
812 path, mode, i, ace.aceMask));
815 /* don't add complementary DENY ACEs here */
816 ZERO_STRUCT(fake_fsp);
817 fake_fsp.fsp_name = (char *)path; /* no file_new is needed here */
819 /* put the acl */
820 if (gpfsacl_process_smbacl(&fake_fsp, pacl) == False)
821 return -1;
822 return 0; /* ok for [f]chmod */
825 static int vfs_gpfs_chmod(vfs_handle_struct *handle, const char *path, mode_t mode)
827 SMB_STRUCT_STAT st;
828 int rc;
830 if (SMB_VFS_NEXT_STAT(handle, path, &st) != 0) {
831 return -1;
834 /* avoid chmod() if possible, to preserve acls */
835 if ((st.st_mode & ~S_IFMT) == mode) {
836 return 0;
839 rc = gpfsacl_emu_chmod(path, mode);
840 if (rc == 1)
841 return SMB_VFS_NEXT_CHMOD(handle, path, mode);
842 return rc;
845 static int vfs_gpfs_fchmod(vfs_handle_struct *handle, files_struct *fsp, mode_t mode)
847 SMB_STRUCT_STAT st;
848 int rc;
850 if (SMB_VFS_NEXT_FSTAT(handle, fsp, &st) != 0) {
851 return -1;
854 /* avoid chmod() if possible, to preserve acls */
855 if ((st.st_mode & ~S_IFMT) == mode) {
856 return 0;
859 rc = gpfsacl_emu_chmod(fsp->fsp_name, mode);
860 if (rc == 1)
861 return SMB_VFS_NEXT_FCHMOD(handle, fsp, mode);
862 return rc;
865 /* VFS operations structure */
867 static vfs_op_tuple gpfs_op_tuples[] = {
869 { SMB_VFS_OP(vfs_gpfs_kernel_flock),
870 SMB_VFS_OP_KERNEL_FLOCK,
871 SMB_VFS_LAYER_OPAQUE },
873 { SMB_VFS_OP(vfs_gpfs_setlease),
874 SMB_VFS_OP_LINUX_SETLEASE,
875 SMB_VFS_LAYER_OPAQUE },
877 { SMB_VFS_OP(vfs_gpfs_get_real_filename),
878 SMB_VFS_OP_GET_REAL_FILENAME,
879 SMB_VFS_LAYER_OPAQUE },
881 { SMB_VFS_OP(gpfsacl_fget_nt_acl),
882 SMB_VFS_OP_FGET_NT_ACL,
883 SMB_VFS_LAYER_TRANSPARENT },
885 { SMB_VFS_OP(gpfsacl_get_nt_acl),
886 SMB_VFS_OP_GET_NT_ACL,
887 SMB_VFS_LAYER_TRANSPARENT },
889 { SMB_VFS_OP(gpfsacl_fset_nt_acl),
890 SMB_VFS_OP_FSET_NT_ACL,
891 SMB_VFS_LAYER_TRANSPARENT },
893 { SMB_VFS_OP(gpfsacl_sys_acl_get_file),
894 SMB_VFS_OP_SYS_ACL_GET_FILE,
895 SMB_VFS_LAYER_TRANSPARENT },
897 { SMB_VFS_OP(gpfsacl_sys_acl_get_fd),
898 SMB_VFS_OP_SYS_ACL_GET_FD,
899 SMB_VFS_LAYER_TRANSPARENT },
901 { SMB_VFS_OP(gpfsacl_sys_acl_set_file),
902 SMB_VFS_OP_SYS_ACL_SET_FILE,
903 SMB_VFS_LAYER_TRANSPARENT },
905 { SMB_VFS_OP(gpfsacl_sys_acl_set_fd),
906 SMB_VFS_OP_SYS_ACL_SET_FD,
907 SMB_VFS_LAYER_TRANSPARENT },
909 { SMB_VFS_OP(gpfsacl_sys_acl_delete_def_file),
910 SMB_VFS_OP_SYS_ACL_DELETE_DEF_FILE,
911 SMB_VFS_LAYER_TRANSPARENT },
913 { SMB_VFS_OP(vfs_gpfs_chmod),
914 SMB_VFS_OP_CHMOD,
915 SMB_VFS_LAYER_TRANSPARENT },
917 { SMB_VFS_OP(vfs_gpfs_fchmod),
918 SMB_VFS_OP_FCHMOD,
919 SMB_VFS_LAYER_TRANSPARENT },
921 { SMB_VFS_OP(NULL), SMB_VFS_OP_NOOP, SMB_VFS_LAYER_NOOP }
926 NTSTATUS vfs_gpfs_init(void);
927 NTSTATUS vfs_gpfs_init(void)
929 init_gpfs();
931 return smb_register_vfs(SMB_VFS_INTERFACE_VERSION, "gpfs",
932 gpfs_op_tuples);