Make use of smbd_gpfs_get_realfilename_path in unix_convert
[kai/samba.git] / source3 / modules / vfs_gpfs.c
1 /*
2    Unix SMB/CIFS implementation.
3    Wrap gpfs calls in vfs functions.
4
5    Copyright (C) Christian Ambach <cambach1@de.ibm.com> 2006
6
7    Major code contributions by Chetan Shringarpure <chetan.sh@in.ibm.com>
8                             and Gomati Mohanan <gomati.mohanan@in.ibm.com>
9
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.
14
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.
19
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/>.
22 */
23
24 #include "includes.h"
25
26 #undef DBGC_CLASS
27 #define DBGC_CLASS DBGC_VFS
28
29 #include <gpfs_gpl.h>
30 #include "nfs4_acls.h"
31 #include "vfs_gpfs.h"
32
33 static int vfs_gpfs_kernel_flock(vfs_handle_struct *handle, files_struct *fsp, 
34                                  uint32 share_mode)
35 {
36
37         START_PROFILE(syscall_kernel_flock);
38
39         kernel_flock(fsp->fh->fd, share_mode);
40
41         if (!set_gpfs_sharemode(fsp, fsp->access_mask, fsp->share_access)) {
42
43                 return -1;
44
45         }
46
47         END_PROFILE(syscall_kernel_flock);
48
49         return 0;
50 }
51
52 static int vfs_gpfs_setlease(vfs_handle_struct *handle, files_struct *fsp, 
53                              int leasetype)
54 {
55         int ret;
56
57         START_PROFILE(syscall_linux_setlease);
58
59         if ( linux_set_lease_sighandler(fsp->fh->fd) == -1)
60                 return -1;
61
62         ret = set_gpfs_lease(fsp->fh->fd,leasetype);
63
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);
68         }
69
70         END_PROFILE(syscall_linux_setlease);
71
72         return ret;
73 }
74
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)
80 {
81         int result;
82         char *full_path;
83         char real_pathname[PATH_MAX+1];
84         int buflen;
85
86         full_path = talloc_asprintf(talloc_tos(), "%s/%s", path, name);
87         if (full_path == NULL) {
88                 errno = ENOMEM;
89                 return -1;
90         }
91
92         buflen = sizeof(real_pathname) - 1;
93
94         result = smbd_gpfs_get_realfilename_path(full_path, real_pathname,
95                                                  &buflen);
96
97         TALLOC_FREE(full_path);
98
99         if (result == -1) {
100                 DEBUG(10, ("smbd_gpfs_get_realfilename_path returned %s\n",
101                            strerror(errno)));
102                 return -1;
103         }
104
105         /*
106          * GPFS does not necessarily null-terminate the returned path
107          * but instead returns the buffer length in buflen.
108          */
109
110         if (buflen < sizeof(real_pathname)) {
111                 real_pathname[buflen] = '\0';
112         } else {
113                 real_pathname[sizeof(real_pathname)-1] = '\0';
114         }
115
116         DEBUG(10, ("smbd_gpfs_get_realfilename_path: %s/%s -> %s\n",
117                    path, name, real_pathname));
118
119         name = strrchr_m(real_pathname, '/');
120         if (name == NULL) {
121                 errno = ENOENT;
122                 return -1;
123         }
124
125         *found_name = talloc_strdup(mem_ctx, name+1);
126         if (*found_name == NULL) {
127                 errno = ENOMEM;
128                 return -1;
129         }
130
131         return 0;
132 }
133
134 static void gpfs_dumpacl(int level, struct gpfs_acl *gacl)
135 {
136         int     i;
137         if (gacl==NULL)
138         {
139                 DEBUG(0, ("gpfs acl is NULL\n"));
140                 return;
141         }
142
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++)
146         {
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));
151         }
152 }
153
154 static struct gpfs_acl *gpfs_getacl_alloc(const char *fname, gpfs_aclType_t type)
155 {
156         struct gpfs_acl *acl;
157         size_t len = 200;
158         int ret;
159         TALLOC_CTX *mem_ctx = talloc_tos();
160
161         acl = (struct gpfs_acl *)TALLOC_SIZE(mem_ctx, len);
162         if (acl == NULL) {
163                 errno = ENOMEM;
164                 return NULL;
165         }
166
167         acl->acl_len = len;
168         acl->acl_level = 0;
169         acl->acl_version = 0;
170         acl->acl_type = type;
171
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;
179                 }
180
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;
186
187                 ret = smbd_gpfs_getacl((char *)fname, GPFS_GETACL_STRUCT | GPFS_ACL_SAMBA, acl);
188         }
189         if (ret != 0)
190         {
191                 DEBUG(8, ("smbd_gpfs_getacl failed with %s\n",strerror(errno)));
192                 return NULL;
193         }
194
195         return acl;
196 }
197
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
203  */
204 static int gpfs_get_nfs4_acl(const char *fname, SMB4ACL_T **ppacl)
205 {
206         int i;
207         struct gpfs_acl *gacl = NULL;
208         DEBUG(10, ("gpfs_get_nfs4_acl invoked for %s\n", fname));
209
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;
216         }
217
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;
222         }
223
224         *ppacl = smb_create_smb4acl();
225
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));
229
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));
236
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 */
254                         }
255                 } else {
256                         if (gace->aceFlags & ACE4_FLAG_GROUP_ID)
257                                 smbace.who.gid = gace->aceWho;
258                         else
259                                 smbace.who.uid = gace->aceWho;
260                 }
261
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;
272                         }                                                
273                 }
274
275                 smbace.aceType = gace->aceType;
276                 smbace.aceFlags = gace->aceFlags;
277                 smbace.aceMask = gace->aceMask;
278                 smb_add_ace4(*ppacl, &smbace);
279         }
280
281         return 0;
282 }
283
284 static NTSTATUS gpfsacl_fget_nt_acl(vfs_handle_struct *handle,
285         files_struct *fsp, uint32 security_info,
286         SEC_DESC **ppdesc)
287 {
288         SMB4ACL_T *pacl = NULL;
289         int     result;
290
291         *ppdesc = NULL;
292         result = gpfs_get_nfs4_acl(fsp->fsp_name, &pacl);
293
294         if (result == 0)
295                 return smb_fget_nt_acl_nfs4(fsp, security_info, ppdesc, pacl);
296
297         if (result > 0) {
298                 DEBUG(10, ("retrying with posix acl...\n"));
299                 return posix_fget_nt_acl(fsp, security_info, ppdesc);
300         }
301
302         /* GPFS ACL was not read, something wrong happened, error code is set in errno */
303         return map_nt_error_from_unix(errno);
304 }
305
306 static NTSTATUS gpfsacl_get_nt_acl(vfs_handle_struct *handle,
307         const char *name,
308         uint32 security_info, SEC_DESC **ppdesc)
309 {
310         SMB4ACL_T *pacl = NULL;
311         int     result;
312
313         *ppdesc = NULL;
314         result = gpfs_get_nfs4_acl(name, &pacl);
315
316         if (result == 0)
317                 return smb_get_nt_acl_nfs4(handle->conn, name, security_info, ppdesc, pacl);
318
319         if (result > 0) {
320                 DEBUG(10, ("retrying with posix acl...\n"));
321                 return posix_get_nt_acl(handle->conn, name, security_info, ppdesc);
322         }
323
324         /* GPFS ACL was not read, something wrong happened, error code is set in errno */
325         return map_nt_error_from_unix(errno);
326 }
327
328 static bool gpfsacl_process_smbacl(files_struct *fsp, SMB4ACL_T *smbacl)
329 {
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();
335
336         gacl_len = sizeof(struct gpfs_acl) +
337                 (smb_get_naces(smbacl)-1)*sizeof(gpfs_ace_v4_t);
338
339         gacl = TALLOC_SIZE(mem_ctx, gacl_len);
340         if (gacl == NULL) {
341                 DEBUG(0, ("talloc failed\n"));
342                 errno = ENOMEM;
343                 return False;
344         }
345
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... */
351
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);
355
356                 gace->aceType = aceprop->aceType;
357                 gace->aceFlags = aceprop->aceFlags;
358                 gace->aceMask = aceprop->aceMask;
359
360                 /*
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 :-)
364                  */
365
366                 if (!fsp->is_directory
367                     &&
368                     ((((gace->aceMask & ACE4_MASK_WRITE) == 0)
369                       && ((gace->aceMask & ACE4_MASK_APPEND) != 0))
370                      ||
371                      (((gace->aceMask & ACE4_MASK_WRITE) != 0)
372                       && ((gace->aceMask & ACE4_MASK_APPEND) == 0)))
373                     &&
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;
380                 }
381
382                 gace->aceIFlags = (aceprop->flags&SMB_ACE4_ID_SPECIAL) ? ACE4_IFLAG_SPECIAL_ID : 0;
383
384                 if (aceprop->flags&SMB_ACE4_ID_SPECIAL)
385                 {
386                         switch(aceprop->who.special_id)
387                         {
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 !!! */
400                         }
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;
407                 }
408
409                 gacl->acl_nace++;
410         }
411
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;
417         }
418
419         DEBUG(10, ("gpfs_putacl succeeded\n"));
420         return True;
421 }
422
423 static NTSTATUS gpfsacl_set_nt_acl_internal(files_struct *fsp, uint32 security_info_sent, const SEC_DESC *psd)
424 {
425         struct gpfs_acl *acl;
426         NTSTATUS result = NT_STATUS_ACCESS_DENIED;
427
428         acl = gpfs_getacl_alloc(fsp->fsp_name, 0);
429         if (acl == NULL)
430                 return result;
431
432         if (acl->acl_version&GPFS_ACL_VERSION_NFS4)
433         {
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);
439         }
440
441         return result;
442 }
443
444 static NTSTATUS gpfsacl_fset_nt_acl(vfs_handle_struct *handle, files_struct *fsp, uint32 security_info_sent, const SEC_DESC *psd)
445 {
446         return gpfsacl_set_nt_acl_internal(fsp, security_info_sent, psd);
447 }
448
449 static SMB_ACL_T gpfs2smb_acl(const struct gpfs_acl *pacl)
450 {
451         SMB_ACL_T result;
452         int i;
453
454         result = sys_acl_init(pacl->acl_nace);
455         if (result == NULL) {
456                 errno = ENOMEM;
457                 return NULL;
458         }
459
460         result->count = pacl->acl_nace;
461
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];
465
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));
469
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;
497                 }
498
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;
506
507                 DEBUGADD(10, ("Converted to %d perm %x\n",
508                               ace->a_type, ace->a_perm));
509         }
510
511         return result;
512 }
513
514 static SMB_ACL_T gpfsacl_get_posix_acl(const char *path, gpfs_aclType_t type)
515 {
516         struct gpfs_acl *pacl;
517         SMB_ACL_T result = NULL;
518
519         pacl = gpfs_getacl_alloc(path, type);
520
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;
526                 }
527                 goto done;
528         }
529
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;
535         }
536
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));
540
541         result = gpfs2smb_acl(pacl);
542         if (result == NULL) {
543                 goto done;
544         }
545
546  done:
547
548         if (errno != 0) {
549                 SAFE_FREE(result);
550         }
551         return result;  
552 }
553
554 SMB_ACL_T gpfsacl_sys_acl_get_file(vfs_handle_struct *handle,
555                                     const char *path_p,
556                                     SMB_ACL_TYPE_T type)
557 {
558         gpfs_aclType_t gpfs_type;
559
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");
570         }
571
572         return gpfsacl_get_posix_acl(path_p, gpfs_type);
573 }
574
575 SMB_ACL_T gpfsacl_sys_acl_get_fd(vfs_handle_struct *handle,
576                                  files_struct *fsp)
577 {
578         return gpfsacl_get_posix_acl(fsp->fsp_name, GPFS_ACL_TYPE_ACCESS);
579 }
580
581 static struct gpfs_acl *smb2gpfs_acl(const SMB_ACL_T pacl,
582                                      SMB_ACL_TYPE_T type)
583 {
584         gpfs_aclLen_t len;
585         struct gpfs_acl *result;
586         int i;
587         union gpfs_ace_union
588         {
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  */
591         };
592
593         DEBUG(10, ("smb2gpfs_acl: Got ACL with %d entries\n", pacl->count));
594
595         len = sizeof(struct gpfs_acl) - sizeof(union gpfs_ace_union) +
596                 (pacl->count)*sizeof(gpfs_ace_v1_t);
597
598         result = SMB_MALLOC(len);
599         if (result == NULL) {
600                 errno = ENOMEM;
601                 return result;
602         }
603
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;
610
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];
614
615                 DEBUG(10, ("Converting type %d perm %x\n",
616                            (int)ace->a_type, (int)ace->a_perm));
617
618                 g_ace->ace_perm = 0;
619
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;
652                 }
653
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;
660
661                 DEBUGADD(10, ("Converted to %d id %d perm %x\n",
662                               g_ace->ace_type, g_ace->ace_who, g_ace->ace_perm));
663         }
664
665         return result;
666 }
667
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)
672 {
673         struct gpfs_acl *gpfs_acl;
674         int result;
675
676         gpfs_acl = smb2gpfs_acl(theacl, type);
677         if (gpfs_acl == NULL) {
678                 return -1;
679         }
680
681         result = smbd_gpfs_putacl((char *)name, GPFS_PUTACL_STRUCT | GPFS_ACL_SAMBA, gpfs_acl);
682
683         SAFE_FREE(gpfs_acl);
684         return result;
685 }
686
687 int gpfsacl_sys_acl_set_fd(vfs_handle_struct *handle,
688                             files_struct *fsp,
689                             SMB_ACL_T theacl)
690 {
691         return gpfsacl_sys_acl_set_file(handle, fsp->fsp_name, SMB_ACL_TYPE_ACCESS, theacl);
692 }
693
694 int gpfsacl_sys_acl_delete_def_file(vfs_handle_struct *handle,
695                                      const char *path)
696 {
697         errno = ENOTSUP;
698         return -1;
699 }
700
701 /*
702  * Assumed: mode bits are shiftable and standard
703  * Output: the new aceMask field for an smb nfs4 ace
704  */
705 static uint32 gpfsacl_mask_filter(uint32 aceType, uint32 aceMask, uint32 rwx)
706 {
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 */
711         };
712         int     i;
713         uint32_t        posix_mask = 0x01;
714         uint32_t        posix_bit;
715         uint32_t        nfs4_bits;
716
717         for(i=0; i<3; i++) {
718                 nfs4_bits = posix_nfs4map[i];
719                 posix_bit = rwx & posix_mask;
720
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 */
733
734                 posix_mask <<= 1;
735         }
736
737         return aceMask;
738 }
739
740 static int gpfsacl_emu_chmod(const char *path, mode_t mode)
741 {
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;
748
749         DEBUG(10, ("gpfsacl_emu_chmod invoked for %s mode %o\n", path, mode));
750
751         result = gpfs_get_nfs4_acl(path, &pacl);
752         if (result)
753                 return result;
754
755         if (mode & ~(S_IRWXU | S_IRWXG | S_IRWXO)) {
756                 DEBUG(2, ("WARNING: cutting extra mode bits %o on %s\n", mode, path));
757         }
758
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;
762
763                 if (ace->flags&SMB_ACE4_ID_SPECIAL &&
764                     ace->aceType<=SMB_ACE4_ACCESS_DENIED_ACE_TYPE &&
765                     specid <= SMB_ACE4_WHO_EVERYONE) {
766
767                         uint32_t newMask;
768
769                         if (ace->aceType==SMB_ACE4_ACCESS_ALLOWED_ACE_TYPE)
770                                 haveAllowEntry[specid] = True;
771
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));
779                         }
780                         ace->aceMask = newMask;
781                 }
782         }
783
784         /* make sure we have at least ALLOW entries
785          * for all the 3 special ids (@EVERYONE, @OWNER, @GROUP)
786          * - if necessary
787          */
788         for(i = SMB_ACE4_WHO_OWNER; i<=SMB_ACE4_WHO_EVERYONE; i++) {
789                 SMB_ACE4PROP_T  ace;
790
791                 if (haveAllowEntry[i]==True)
792                         continue;
793
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;
798
799                 if (i==SMB_ACE4_WHO_GROUP) /* not sure it's necessary... */
800                         ace.aceFlags |= SMB_ACE4_IDENTIFIER_GROUP;
801
802                 ace.aceMask = gpfsacl_mask_filter(ace.aceType, ace.aceMask,
803                                                   mode >> ((SMB_ACE4_WHO_EVERYONE - i) * 3));
804
805                 /* don't add unnecessary aces */
806                 if (!ace.aceMask)
807                         continue;
808
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));
813         }
814
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 */
818
819         /* put the acl */
820         if (gpfsacl_process_smbacl(&fake_fsp, pacl) == False)
821                 return -1;
822         return 0; /* ok for [f]chmod */
823 }
824
825 static int vfs_gpfs_chmod(vfs_handle_struct *handle, const char *path, mode_t mode)
826 {
827                  SMB_STRUCT_STAT st;
828                  int rc;
829
830                  if (SMB_VFS_NEXT_STAT(handle, path, &st) != 0) {
831                          return -1;
832                  }
833
834                  /* avoid chmod() if possible, to preserve acls */
835                  if ((st.st_mode & ~S_IFMT) == mode) {
836                          return 0;
837                  }
838
839                  rc = gpfsacl_emu_chmod(path, mode);
840                  if (rc == 1)
841                          return SMB_VFS_NEXT_CHMOD(handle, path, mode);
842                  return rc;
843 }
844
845 static int vfs_gpfs_fchmod(vfs_handle_struct *handle, files_struct *fsp, mode_t mode)
846 {
847                  SMB_STRUCT_STAT st;
848                  int rc;
849
850                  if (SMB_VFS_NEXT_FSTAT(handle, fsp, &st) != 0) {
851                          return -1;
852                  }
853
854                  /* avoid chmod() if possible, to preserve acls */
855                  if ((st.st_mode & ~S_IFMT) == mode) {
856                          return 0;
857                  }
858
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;
863 }
864
865 /* VFS operations structure */
866
867 static vfs_op_tuple gpfs_op_tuples[] = {
868
869         { SMB_VFS_OP(vfs_gpfs_kernel_flock), 
870           SMB_VFS_OP_KERNEL_FLOCK,
871           SMB_VFS_LAYER_OPAQUE },
872
873         { SMB_VFS_OP(vfs_gpfs_setlease), 
874           SMB_VFS_OP_LINUX_SETLEASE,
875           SMB_VFS_LAYER_OPAQUE },
876
877         { SMB_VFS_OP(vfs_gpfs_get_real_filename),
878           SMB_VFS_OP_GET_REAL_FILENAME,
879           SMB_VFS_LAYER_OPAQUE },
880
881         { SMB_VFS_OP(gpfsacl_fget_nt_acl), 
882           SMB_VFS_OP_FGET_NT_ACL,
883           SMB_VFS_LAYER_TRANSPARENT },
884
885         { SMB_VFS_OP(gpfsacl_get_nt_acl), 
886           SMB_VFS_OP_GET_NT_ACL,
887           SMB_VFS_LAYER_TRANSPARENT },
888
889         { SMB_VFS_OP(gpfsacl_fset_nt_acl), 
890           SMB_VFS_OP_FSET_NT_ACL,
891           SMB_VFS_LAYER_TRANSPARENT },
892
893         { SMB_VFS_OP(gpfsacl_sys_acl_get_file), 
894           SMB_VFS_OP_SYS_ACL_GET_FILE,
895           SMB_VFS_LAYER_TRANSPARENT },
896
897         { SMB_VFS_OP(gpfsacl_sys_acl_get_fd), 
898           SMB_VFS_OP_SYS_ACL_GET_FD,
899           SMB_VFS_LAYER_TRANSPARENT },
900
901         { SMB_VFS_OP(gpfsacl_sys_acl_set_file), 
902           SMB_VFS_OP_SYS_ACL_SET_FILE,
903           SMB_VFS_LAYER_TRANSPARENT },
904
905         { SMB_VFS_OP(gpfsacl_sys_acl_set_fd), 
906           SMB_VFS_OP_SYS_ACL_SET_FD,
907           SMB_VFS_LAYER_TRANSPARENT },
908
909         { SMB_VFS_OP(gpfsacl_sys_acl_delete_def_file),
910           SMB_VFS_OP_SYS_ACL_DELETE_DEF_FILE,
911           SMB_VFS_LAYER_TRANSPARENT },
912
913         { SMB_VFS_OP(vfs_gpfs_chmod), 
914           SMB_VFS_OP_CHMOD,
915           SMB_VFS_LAYER_TRANSPARENT },
916
917         { SMB_VFS_OP(vfs_gpfs_fchmod), 
918           SMB_VFS_OP_FCHMOD,
919           SMB_VFS_LAYER_TRANSPARENT },
920
921         { SMB_VFS_OP(NULL), SMB_VFS_OP_NOOP, SMB_VFS_LAYER_NOOP }
922
923 };
924
925
926 NTSTATUS vfs_gpfs_init(void);
927 NTSTATUS vfs_gpfs_init(void)
928 {
929         init_gpfs();
930
931         return smb_register_vfs(SMB_VFS_INTERFACE_VERSION, "gpfs",
932                                 gpfs_op_tuples);
933 }