Revert "Fix the build"
[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, 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, SEC_DESC *psd)
445 {
446         return gpfsacl_set_nt_acl_internal(fsp, security_info_sent, psd);
447 }
448
449 static NTSTATUS gpfsacl_set_nt_acl(vfs_handle_struct *handle, files_struct *fsp, char *name, uint32 security_info_sent, SEC_DESC *psd)
450 {
451         return gpfsacl_set_nt_acl_internal(fsp, security_info_sent, psd);
452 }
453
454 static SMB_ACL_T gpfs2smb_acl(const struct gpfs_acl *pacl)
455 {
456         SMB_ACL_T result;
457         int i;
458
459         result = sys_acl_init(pacl->acl_nace);
460         if (result == NULL) {
461                 errno = ENOMEM;
462                 return NULL;
463         }
464
465         result->count = pacl->acl_nace;
466
467         for (i=0; i<pacl->acl_nace; i++) {
468                 struct smb_acl_entry *ace = &result->acl[i];
469                 const struct gpfs_ace_v1 *g_ace = &pacl->ace_v1[i];
470
471                 DEBUG(10, ("Converting type %d id %lu perm %x\n",
472                            (int)g_ace->ace_type, (unsigned long)g_ace->ace_who,
473                            (int)g_ace->ace_perm));
474
475                 switch (g_ace->ace_type) {
476                 case GPFS_ACL_USER:
477                         ace->a_type = SMB_ACL_USER;
478                         ace->uid = (uid_t)g_ace->ace_who;
479                         break;
480                 case GPFS_ACL_USER_OBJ:
481                         ace->a_type = SMB_ACL_USER_OBJ;
482                         break;
483                 case GPFS_ACL_GROUP:
484                         ace->a_type = SMB_ACL_GROUP;
485                         ace->gid = (gid_t)g_ace->ace_who;
486                         break;
487                 case GPFS_ACL_GROUP_OBJ:
488                         ace->a_type = SMB_ACL_GROUP_OBJ;
489                         break;
490                 case GPFS_ACL_OTHER:
491                         ace->a_type = SMB_ACL_OTHER;
492                         break;
493                 case GPFS_ACL_MASK:
494                         ace->a_type = SMB_ACL_MASK;
495                         break;
496                 default:
497                         DEBUG(10, ("Got invalid ace_type: %d\n",
498                                    g_ace->ace_type));
499                         errno = EINVAL;
500                         SAFE_FREE(result);
501                         return NULL;
502                 }
503
504                 ace->a_perm = 0;
505                 ace->a_perm |= (g_ace->ace_perm & ACL_PERM_READ) ?
506                         SMB_ACL_READ : 0;
507                 ace->a_perm |= (g_ace->ace_perm & ACL_PERM_WRITE) ?
508                         SMB_ACL_WRITE : 0;
509                 ace->a_perm |= (g_ace->ace_perm & ACL_PERM_EXECUTE) ?
510                         SMB_ACL_EXECUTE : 0;
511
512                 DEBUGADD(10, ("Converted to %d perm %x\n",
513                               ace->a_type, ace->a_perm));
514         }
515
516         return result;
517 }
518
519 static SMB_ACL_T gpfsacl_get_posix_acl(const char *path, gpfs_aclType_t type)
520 {
521         struct gpfs_acl *pacl;
522         SMB_ACL_T result = NULL;
523
524         pacl = gpfs_getacl_alloc(path, type);
525
526         if (pacl == NULL) {
527                 DEBUG(10, ("gpfs_getacl failed for %s with %s\n",
528                            path, strerror(errno)));
529                 if (errno == 0) {
530                         errno = EINVAL;
531                 }
532                 goto done;
533         }
534
535         if (pacl->acl_version != GPFS_ACL_VERSION_POSIX) {
536                 DEBUG(10, ("Got acl version %d, expected %d\n",
537                            pacl->acl_version, GPFS_ACL_VERSION_POSIX));
538                 errno = EINVAL;
539                 goto done;
540         }
541
542         DEBUG(10, ("len: %d, level: %d, version: %d, nace: %d\n",
543                    pacl->acl_len, pacl->acl_level, pacl->acl_version,
544                    pacl->acl_nace));
545
546         result = gpfs2smb_acl(pacl);
547         if (result == NULL) {
548                 goto done;
549         }
550
551  done:
552
553         if (errno != 0) {
554                 SAFE_FREE(result);
555         }
556         return result;  
557 }
558
559 SMB_ACL_T gpfsacl_sys_acl_get_file(vfs_handle_struct *handle,
560                                     const char *path_p,
561                                     SMB_ACL_TYPE_T type)
562 {
563         gpfs_aclType_t gpfs_type;
564
565         switch(type) {
566         case SMB_ACL_TYPE_ACCESS:
567                 gpfs_type = GPFS_ACL_TYPE_ACCESS;
568                 break;
569         case SMB_ACL_TYPE_DEFAULT:
570                 gpfs_type = GPFS_ACL_TYPE_DEFAULT;
571                 break;
572         default:
573                 DEBUG(0, ("Got invalid type: %d\n", type));
574                 smb_panic("exiting");
575         }
576
577         return gpfsacl_get_posix_acl(path_p, gpfs_type);
578 }
579
580 SMB_ACL_T gpfsacl_sys_acl_get_fd(vfs_handle_struct *handle,
581                                  files_struct *fsp)
582 {
583         return gpfsacl_get_posix_acl(fsp->fsp_name, GPFS_ACL_TYPE_ACCESS);
584 }
585
586 static struct gpfs_acl *smb2gpfs_acl(const SMB_ACL_T pacl,
587                                      SMB_ACL_TYPE_T type)
588 {
589         gpfs_aclLen_t len;
590         struct gpfs_acl *result;
591         int i;
592         union gpfs_ace_union
593         {
594                 gpfs_ace_v1_t  ace_v1[1]; /* when GPFS_ACL_VERSION_POSIX */
595                 gpfs_ace_v4_t  ace_v4[1]; /* when GPFS_ACL_VERSION_NFS4  */
596         };
597
598         DEBUG(10, ("smb2gpfs_acl: Got ACL with %d entries\n", pacl->count));
599
600         len = sizeof(struct gpfs_acl) - sizeof(union gpfs_ace_union) +
601                 (pacl->count)*sizeof(gpfs_ace_v1_t);
602
603         result = SMB_MALLOC(len);
604         if (result == NULL) {
605                 errno = ENOMEM;
606                 return result;
607         }
608
609         result->acl_len = len;
610         result->acl_level = 0;
611         result->acl_version = GPFS_ACL_VERSION_POSIX;
612         result->acl_type = (type == SMB_ACL_TYPE_DEFAULT) ?
613                 GPFS_ACL_TYPE_DEFAULT : GPFS_ACL_TYPE_ACCESS;
614         result->acl_nace = pacl->count;
615
616         for (i=0; i<pacl->count; i++) {
617                 const struct smb_acl_entry *ace = &pacl->acl[i];
618                 struct gpfs_ace_v1 *g_ace = &result->ace_v1[i];
619
620                 DEBUG(10, ("Converting type %d perm %x\n",
621                            (int)ace->a_type, (int)ace->a_perm));
622
623                 g_ace->ace_perm = 0;
624
625                 switch(ace->a_type) {
626                 case SMB_ACL_USER:
627                         g_ace->ace_type = GPFS_ACL_USER;
628                         g_ace->ace_who = (gpfs_uid_t)ace->uid;
629                         break;
630                 case SMB_ACL_USER_OBJ:
631                         g_ace->ace_type = GPFS_ACL_USER_OBJ;
632                         g_ace->ace_perm |= ACL_PERM_CONTROL;
633                         g_ace->ace_who = 0;
634                         break;
635                 case SMB_ACL_GROUP:
636                         g_ace->ace_type = GPFS_ACL_GROUP;
637                         g_ace->ace_who = (gpfs_uid_t)ace->gid;
638                         break;
639                 case SMB_ACL_GROUP_OBJ:
640                         g_ace->ace_type = GPFS_ACL_GROUP_OBJ;
641                         g_ace->ace_who = 0;
642                         break;
643                 case SMB_ACL_MASK:
644                         g_ace->ace_type = GPFS_ACL_MASK;
645                         g_ace->ace_perm = 0x8f;
646                         g_ace->ace_who = 0;
647                         break;
648                 case SMB_ACL_OTHER:
649                         g_ace->ace_type = GPFS_ACL_OTHER;
650                         g_ace->ace_who = 0;
651                         break;
652                 default:
653                         DEBUG(10, ("Got invalid ace_type: %d\n", ace->a_type));
654                         errno = EINVAL;
655                         SAFE_FREE(result);
656                         return NULL;
657                 }
658
659                 g_ace->ace_perm |= (ace->a_perm & SMB_ACL_READ) ?
660                         ACL_PERM_READ : 0;
661                 g_ace->ace_perm |= (ace->a_perm & SMB_ACL_WRITE) ?
662                         ACL_PERM_WRITE : 0;
663                 g_ace->ace_perm |= (ace->a_perm & SMB_ACL_EXECUTE) ?
664                         ACL_PERM_EXECUTE : 0;
665
666                 DEBUGADD(10, ("Converted to %d id %d perm %x\n",
667                               g_ace->ace_type, g_ace->ace_who, g_ace->ace_perm));
668         }
669
670         return result;
671 }
672
673 int gpfsacl_sys_acl_set_file(vfs_handle_struct *handle,
674                               const char *name,
675                               SMB_ACL_TYPE_T type,
676                               SMB_ACL_T theacl)
677 {
678         struct gpfs_acl *gpfs_acl;
679         int result;
680
681         gpfs_acl = smb2gpfs_acl(theacl, type);
682         if (gpfs_acl == NULL) {
683                 return -1;
684         }
685
686         result = smbd_gpfs_putacl((char *)name, GPFS_PUTACL_STRUCT | GPFS_ACL_SAMBA, gpfs_acl);
687
688         SAFE_FREE(gpfs_acl);
689         return result;
690 }
691
692 int gpfsacl_sys_acl_set_fd(vfs_handle_struct *handle,
693                             files_struct *fsp,
694                             SMB_ACL_T theacl)
695 {
696         return gpfsacl_sys_acl_set_file(handle, fsp->fsp_name, SMB_ACL_TYPE_ACCESS, theacl);
697 }
698
699 int gpfsacl_sys_acl_delete_def_file(vfs_handle_struct *handle,
700                                      const char *path)
701 {
702         errno = ENOTSUP;
703         return -1;
704 }
705
706 /*
707  * Assumed: mode bits are shiftable and standard
708  * Output: the new aceMask field for an smb nfs4 ace
709  */
710 static uint32 gpfsacl_mask_filter(uint32 aceType, uint32 aceMask, uint32 rwx)
711 {
712         const uint32 posix_nfs4map[3] = {
713                 SMB_ACE4_EXECUTE, /* execute */
714                 SMB_ACE4_WRITE_DATA | SMB_ACE4_APPEND_DATA, /* write; GPFS specific */
715                 SMB_ACE4_READ_DATA /* read */
716         };
717         int     i;
718         uint32_t        posix_mask = 0x01;
719         uint32_t        posix_bit;
720         uint32_t        nfs4_bits;
721
722         for(i=0; i<3; i++) {
723                 nfs4_bits = posix_nfs4map[i];
724                 posix_bit = rwx & posix_mask;
725
726                 if (aceType==SMB_ACE4_ACCESS_ALLOWED_ACE_TYPE) {
727                         if (posix_bit)
728                                 aceMask |= nfs4_bits;
729                         else
730                                 aceMask &= ~nfs4_bits;
731                 } else {
732                         /* add deny bits when suitable */
733                         if (!posix_bit)
734                                 aceMask |= nfs4_bits;
735                         else
736                                 aceMask &= ~nfs4_bits;
737                 } /* other ace types are unexpected */
738
739                 posix_mask <<= 1;
740         }
741
742         return aceMask;
743 }
744
745 static int gpfsacl_emu_chmod(const char *path, mode_t mode)
746 {
747         SMB4ACL_T *pacl = NULL;
748         int     result;
749         bool    haveAllowEntry[SMB_ACE4_WHO_EVERYONE + 1] = {False, False, False, False};
750         int     i;
751         files_struct    fake_fsp; /* TODO: rationalize parametrization */
752         SMB4ACE_T       *smbace;
753
754         DEBUG(10, ("gpfsacl_emu_chmod invoked for %s mode %o\n", path, mode));
755
756         result = gpfs_get_nfs4_acl(path, &pacl);
757         if (result)
758                 return result;
759
760         if (mode & ~(S_IRWXU | S_IRWXG | S_IRWXO)) {
761                 DEBUG(2, ("WARNING: cutting extra mode bits %o on %s\n", mode, path));
762         }
763
764         for (smbace=smb_first_ace4(pacl); smbace!=NULL; smbace = smb_next_ace4(smbace)) {
765                 SMB_ACE4PROP_T  *ace = smb_get_ace4(smbace);
766                 uint32_t        specid = ace->who.special_id;
767
768                 if (ace->flags&SMB_ACE4_ID_SPECIAL &&
769                     ace->aceType<=SMB_ACE4_ACCESS_DENIED_ACE_TYPE &&
770                     specid <= SMB_ACE4_WHO_EVERYONE) {
771
772                         uint32_t newMask;
773
774                         if (ace->aceType==SMB_ACE4_ACCESS_ALLOWED_ACE_TYPE)
775                                 haveAllowEntry[specid] = True;
776
777                         /* mode >> 6 for @owner, mode >> 3 for @group,
778                          * mode >> 0 for @everyone */
779                         newMask = gpfsacl_mask_filter(ace->aceType, ace->aceMask,
780                                                       mode >> ((SMB_ACE4_WHO_EVERYONE - specid) * 3));
781                         if (ace->aceMask!=newMask) {
782                                 DEBUG(10, ("ace changed for %s (%o -> %o) id=%d\n",
783                                            path, ace->aceMask, newMask, specid));
784                         }
785                         ace->aceMask = newMask;
786                 }
787         }
788
789         /* make sure we have at least ALLOW entries
790          * for all the 3 special ids (@EVERYONE, @OWNER, @GROUP)
791          * - if necessary
792          */
793         for(i = SMB_ACE4_WHO_OWNER; i<=SMB_ACE4_WHO_EVERYONE; i++) {
794                 SMB_ACE4PROP_T  ace;
795
796                 if (haveAllowEntry[i]==True)
797                         continue;
798
799                 ZERO_STRUCT(ace);
800                 ace.aceType = SMB_ACE4_ACCESS_ALLOWED_ACE_TYPE;
801                 ace.flags |= SMB_ACE4_ID_SPECIAL;
802                 ace.who.special_id = i;
803
804                 if (i==SMB_ACE4_WHO_GROUP) /* not sure it's necessary... */
805                         ace.aceFlags |= SMB_ACE4_IDENTIFIER_GROUP;
806
807                 ace.aceMask = gpfsacl_mask_filter(ace.aceType, ace.aceMask,
808                                                   mode >> ((SMB_ACE4_WHO_EVERYONE - i) * 3));
809
810                 /* don't add unnecessary aces */
811                 if (!ace.aceMask)
812                         continue;
813
814                 /* we add it to the END - as windows expects allow aces */
815                 smb_add_ace4(pacl, &ace);
816                 DEBUG(10, ("Added ALLOW ace for %s, mode=%o, id=%d, aceMask=%x\n",
817                            path, mode, i, ace.aceMask));
818         }
819
820         /* don't add complementary DENY ACEs here */
821         ZERO_STRUCT(fake_fsp);
822         fake_fsp.fsp_name = (char *)path; /* no file_new is needed here */
823
824         /* put the acl */
825         if (gpfsacl_process_smbacl(&fake_fsp, pacl) == False)
826                 return -1;
827         return 0; /* ok for [f]chmod */
828 }
829
830 static int vfs_gpfs_chmod(vfs_handle_struct *handle, const char *path, mode_t mode)
831 {
832                  SMB_STRUCT_STAT st;
833                  int rc;
834
835                  if (SMB_VFS_NEXT_STAT(handle, path, &st) != 0) {
836                          return -1;
837                  }
838
839                  /* avoid chmod() if possible, to preserve acls */
840                  if ((st.st_mode & ~S_IFMT) == mode) {
841                          return 0;
842                  }
843
844                  rc = gpfsacl_emu_chmod(path, mode);
845                  if (rc == 1)
846                          return SMB_VFS_NEXT_CHMOD(handle, path, mode);
847                  return rc;
848 }
849
850 static int vfs_gpfs_fchmod(vfs_handle_struct *handle, files_struct *fsp, mode_t mode)
851 {
852                  SMB_STRUCT_STAT st;
853                  int rc;
854
855                  if (SMB_VFS_NEXT_FSTAT(handle, fsp, &st) != 0) {
856                          return -1;
857                  }
858
859                  /* avoid chmod() if possible, to preserve acls */
860                  if ((st.st_mode & ~S_IFMT) == mode) {
861                          return 0;
862                  }
863
864                  rc = gpfsacl_emu_chmod(fsp->fsp_name, mode);
865                  if (rc == 1)
866                          return SMB_VFS_NEXT_FCHMOD(handle, fsp, mode);
867                  return rc;
868 }
869
870 /* VFS operations structure */
871
872 static vfs_op_tuple gpfs_op_tuples[] = {
873
874         { SMB_VFS_OP(vfs_gpfs_kernel_flock), 
875           SMB_VFS_OP_KERNEL_FLOCK,
876           SMB_VFS_LAYER_OPAQUE },
877
878         { SMB_VFS_OP(vfs_gpfs_setlease), 
879           SMB_VFS_OP_LINUX_SETLEASE,
880           SMB_VFS_LAYER_OPAQUE },
881
882         { SMB_VFS_OP(vfs_gpfs_get_real_filename),
883           SMB_VFS_OP_GET_REAL_FILENAME,
884           SMB_VFS_LAYER_OPAQUE },
885
886         { SMB_VFS_OP(gpfsacl_fget_nt_acl), 
887           SMB_VFS_OP_FGET_NT_ACL,
888           SMB_VFS_LAYER_TRANSPARENT },
889
890         { SMB_VFS_OP(gpfsacl_get_nt_acl), 
891           SMB_VFS_OP_GET_NT_ACL,
892           SMB_VFS_LAYER_TRANSPARENT },
893
894         { SMB_VFS_OP(gpfsacl_fset_nt_acl), 
895           SMB_VFS_OP_FSET_NT_ACL,
896           SMB_VFS_LAYER_TRANSPARENT },
897
898         { SMB_VFS_OP(gpfsacl_set_nt_acl),
899           SMB_VFS_OP_SET_NT_ACL,
900           SMB_VFS_LAYER_TRANSPARENT },
901
902         { SMB_VFS_OP(gpfsacl_sys_acl_get_file), 
903           SMB_VFS_OP_SYS_ACL_GET_FILE,
904           SMB_VFS_LAYER_TRANSPARENT },
905
906         { SMB_VFS_OP(gpfsacl_sys_acl_get_fd), 
907           SMB_VFS_OP_SYS_ACL_GET_FD,
908           SMB_VFS_LAYER_TRANSPARENT },
909
910         { SMB_VFS_OP(gpfsacl_sys_acl_set_file), 
911           SMB_VFS_OP_SYS_ACL_SET_FILE,
912           SMB_VFS_LAYER_TRANSPARENT },
913
914         { SMB_VFS_OP(gpfsacl_sys_acl_set_fd), 
915           SMB_VFS_OP_SYS_ACL_SET_FD,
916           SMB_VFS_LAYER_TRANSPARENT },
917
918         { SMB_VFS_OP(gpfsacl_sys_acl_delete_def_file),
919           SMB_VFS_OP_SYS_ACL_DELETE_DEF_FILE,
920           SMB_VFS_LAYER_TRANSPARENT },
921
922         { SMB_VFS_OP(vfs_gpfs_chmod), 
923           SMB_VFS_OP_CHMOD,
924           SMB_VFS_LAYER_TRANSPARENT },
925
926         { SMB_VFS_OP(vfs_gpfs_fchmod), 
927           SMB_VFS_OP_FCHMOD,
928           SMB_VFS_LAYER_TRANSPARENT },
929
930         { SMB_VFS_OP(NULL), SMB_VFS_OP_NOOP, SMB_VFS_LAYER_NOOP }
931
932 };
933
934
935 NTSTATUS vfs_gpfs_init(void);
936 NTSTATUS vfs_gpfs_init(void)
937 {
938         init_gpfs();
939
940         return smb_register_vfs(SMB_VFS_INTERFACE_VERSION, "gpfs",
941                                 gpfs_op_tuples);
942 }