s3-vfs: Put vfs_aixacl_util.c helper functions into a header file
[Samba/gebeck_regimport.git] / source3 / modules / vfs_aixacl_util.c
blobb359c401efee2c651f4c7f4474417ccb280fc0d6
1 /*
2 Unix SMB/Netbios implementation.
3 VFS module to get and set posix acls
4 Copyright (C) Jim McDonough <jmcd@us.ibm.com> 2006
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>.
20 #include "includes.h"
21 #include "system/filesys.h"
22 #include "smbd/smbd.h"
23 #include "vfs_aixacl_util.h"
25 SMB_ACL_T aixacl_to_smbacl(struct acl *file_acl)
27 struct acl_entry *acl_entry;
28 struct ace_id *idp;
30 struct smb_acl_t *result = SMB_MALLOC_P(struct smb_acl_t);
31 struct smb_acl_entry *ace;
32 int i;
34 if (result == NULL) {
35 return NULL;
37 ZERO_STRUCTP(result);
39 /* Point to the first acl entry in the acl */
40 acl_entry = file_acl->acl_ext;
44 DEBUG(10,("acl_entry is %p\n",(void *)acl_entry));
45 DEBUG(10,("acl_last(file_acl) id %p\n",(void *)acl_last(file_acl)));
47 /* Check if the extended acl bit is on. *
48 * If it isn't, do not show the *
49 * contents of the acl since AIX intends *
50 * the extended info to remain unused */
52 if(file_acl->acl_mode & S_IXACL){
53 /* while we are not pointing to the very end */
54 while(acl_entry < acl_last(file_acl)) {
55 /* before we malloc anything, make sure this is */
56 /* a valid acl entry and one that we want to map */
57 idp = id_nxt(acl_entry->ace_id);
58 if((acl_entry->ace_type == ACC_SPECIFY ||
59 (acl_entry->ace_type == ACC_PERMIT)) && (idp != id_last(acl_entry))) {
60 acl_entry = acl_nxt(acl_entry);
61 continue;
64 idp = acl_entry->ace_id;
65 DEBUG(10,("idp->id_data is %d\n",idp->id_data[0]));
67 result = SMB_REALLOC(result, sizeof(struct smb_acl_t) +
68 (sizeof(struct smb_acl_entry) *
69 (result->count+1)));
70 if (result == NULL) {
71 DEBUG(0, ("SMB_REALLOC failed\n"));
72 errno = ENOMEM;
73 return NULL;
77 DEBUG(10,("idp->id_type is %d\n",idp->id_type));
78 ace = &result->acl[result->count];
80 ace->a_type = idp->id_type;
82 switch(ace->a_type) {
83 case ACEID_USER: {
84 ace->uid = idp->id_data[0];
85 DEBUG(10,("case ACEID_USER ace->uid is %d\n",ace->uid));
86 ace->a_type = SMB_ACL_USER;
87 break;
90 case ACEID_GROUP: {
91 ace->gid = idp->id_data[0];
92 DEBUG(10,("case ACEID_GROUP ace->gid is %d\n",ace->gid));
93 ace->a_type = SMB_ACL_GROUP;
94 break;
96 default:
97 break;
99 /* The access in the acl entries must be left shifted by *
100 * three bites, because they will ultimately be compared *
101 * to S_IRUSR, S_IWUSR, and S_IXUSR. */
103 switch(acl_entry->ace_type){
104 case ACC_PERMIT:
105 case ACC_SPECIFY:
106 ace->a_perm = acl_entry->ace_access;
107 ace->a_perm <<= 6;
108 DEBUG(10,("ace->a_perm is %d\n",ace->a_perm));
109 break;
110 case ACC_DENY:
111 /* Since there is no way to return a DENY acl entry *
112 * change to PERMIT and then shift. */
113 DEBUG(10,("acl_entry->ace_access is %d\n",acl_entry->ace_access));
114 ace->a_perm = ~acl_entry->ace_access & 7;
115 DEBUG(10,("ace->a_perm is %d\n",ace->a_perm));
116 ace->a_perm <<= 6;
117 break;
118 default:
119 DEBUG(0, ("unknown ace->type\n"));
120 SAFE_FREE(result);
121 return(0);
124 result->count++;
125 ace->a_perm |= (ace->a_perm & S_IRUSR) ? SMB_ACL_READ : 0;
126 ace->a_perm |= (ace->a_perm & S_IWUSR) ? SMB_ACL_WRITE : 0;
127 ace->a_perm |= (ace->a_perm & S_IXUSR) ? SMB_ACL_EXECUTE : 0;
128 DEBUG(10,("ace->a_perm is %d\n",ace->a_perm));
130 DEBUG(10,("acl_entry = %p\n",(void *)acl_entry));
131 DEBUG(10,("The ace_type is %d\n",acl_entry->ace_type));
133 acl_entry = acl_nxt(acl_entry);
135 } /* end of if enabled */
137 /* Since owner, group, other acl entries are not *
138 * part of the acl entries in an acl, they must *
139 * be dummied up to become part of the list. */
141 for( i = 1; i < 4; i++) {
142 DEBUG(10,("i is %d\n",i));
144 result = SMB_REALLOC(result, sizeof(struct smb_acl_t) +
145 (sizeof(struct smb_acl_entry) *
146 (result->count+1)));
147 if (result == NULL) {
148 DEBUG(0, ("SMB_REALLOC failed\n"));
149 errno = ENOMEM;
150 DEBUG(0,("Error in AIX sys_acl_get_file is %d\n",errno));
151 return NULL;
154 ace = &result->acl[result->count];
156 ace->uid = 0;
157 ace->gid = 0;
158 DEBUG(10,("ace->uid = %d\n",ace->uid));
160 switch(i) {
161 case 2:
162 ace->a_perm = file_acl->g_access << 6;
163 ace->a_type = SMB_ACL_GROUP_OBJ;
164 break;
166 case 3:
167 ace->a_perm = file_acl->o_access << 6;
168 ace->a_type = SMB_ACL_OTHER;
169 break;
171 case 1:
172 ace->a_perm = file_acl->u_access << 6;
173 ace->a_type = SMB_ACL_USER_OBJ;
174 break;
176 default:
177 return(NULL);
180 ace->a_perm |= ((ace->a_perm & S_IRUSR) ? SMB_ACL_READ : 0);
181 ace->a_perm |= ((ace->a_perm & S_IWUSR) ? SMB_ACL_WRITE : 0);
182 ace->a_perm |= ((ace->a_perm & S_IXUSR) ? SMB_ACL_EXECUTE : 0);
184 memcpy(&result->acl[result->count],ace,sizeof(struct smb_acl_entry));
185 result->count++;
186 DEBUG(10,("ace->a_perm = %d\n",ace->a_perm));
187 DEBUG(10,("ace->a_type = %d\n",ace->a_type));
191 return result;
196 static ushort aixacl_smb_to_aixperm(SMB_ACL_PERM_T a_perm)
198 ushort ret = (ushort)0;
199 if (a_perm & SMB_ACL_READ)
200 ret |= R_ACC;
201 if (a_perm & SMB_ACL_WRITE)
202 ret |= W_ACC;
203 if (a_perm & SMB_ACL_EXECUTE)
204 ret |= X_ACC;
205 return ret;
208 struct acl *aixacl_smb_to_aixacl(SMB_ACL_TYPE_T acltype, SMB_ACL_T theacl)
210 struct smb_acl_entry *smb_entry = NULL;
211 struct acl *file_acl = NULL;
212 struct acl *file_acl_temp = NULL;
213 struct acl_entry *acl_entry = NULL;
214 struct ace_id *ace_id = NULL;
215 unsigned int id_type;
216 unsigned int acl_length;
217 int i;
219 DEBUG(10,("Entering aixacl_smb_to_aixacl\n"));
220 /* AIX has no default ACL */
221 if(acltype == SMB_ACL_TYPE_DEFAULT)
222 return NULL;
224 acl_length = BUFSIZ;
225 file_acl = (struct acl *)SMB_MALLOC(BUFSIZ);
226 if(file_acl == NULL) {
227 errno = ENOMEM;
228 DEBUG(0,("Error in aixacl_smb_to_aixacl is %d\n",errno));
229 return NULL;
232 memset(file_acl,0,BUFSIZ);
234 file_acl->acl_len = ACL_SIZ;
235 file_acl->acl_mode = S_IXACL;
237 for(i=0; i<theacl->count; i++ ) {
238 smb_entry = &(theacl->acl[i]);
239 id_type = smb_entry->a_type;
240 DEBUG(10,("The id_type is %d\n",id_type));
242 switch(id_type) {
243 case SMB_ACL_USER_OBJ:
244 file_acl->u_access = aixacl_smb_to_aixperm(smb_entry->a_perm);
245 continue;
246 case SMB_ACL_GROUP_OBJ:
247 file_acl->g_access = aixacl_smb_to_aixperm(smb_entry->a_perm);
248 continue;
249 case SMB_ACL_OTHER:
250 file_acl->o_access = aixacl_smb_to_aixperm(smb_entry->a_perm);
251 continue;
252 case SMB_ACL_MASK:
253 continue;
254 case SMB_ACL_GROUP:
255 break; /* process this */
256 case SMB_ACL_USER:
257 break; /* process this */
258 default: /* abnormal case */
259 DEBUG(10,("The id_type is unknown !\n"));
260 continue;
263 if((file_acl->acl_len + sizeof(struct acl_entry)) > acl_length) {
264 acl_length += sizeof(struct acl_entry);
265 file_acl_temp = (struct acl *)SMB_MALLOC(acl_length);
266 if(file_acl_temp == NULL) {
267 SAFE_FREE(file_acl);
268 errno = ENOMEM;
269 DEBUG(0,("Error in aixacl_smb_to_aixacl is %d\n",errno));
270 return NULL;
273 memcpy(file_acl_temp,file_acl,file_acl->acl_len);
274 SAFE_FREE(file_acl);
275 file_acl = file_acl_temp;
278 acl_entry = (struct acl_entry *)((char *)file_acl + file_acl->acl_len);
279 file_acl->acl_len += sizeof(struct acl_entry);
280 acl_entry->ace_len = sizeof(struct acl_entry); /* contains 1 ace_id */
281 acl_entry->ace_access = aixacl_smb_to_aixperm(smb_entry->a_perm);
283 /* In order to use this, we'll need to wait until we can get denies */
284 /* if(!acl_entry->ace_access && acl_entry->ace_type == ACC_PERMIT)
285 acl_entry->ace_type = ACC_SPECIFY; */
287 acl_entry->ace_type = ACC_SPECIFY;
289 ace_id = acl_entry->ace_id;
291 ace_id->id_type = (smb_entry->a_type==SMB_ACL_GROUP) ? ACEID_GROUP : ACEID_USER;
292 DEBUG(10,("The id type is %d\n",ace_id->id_type));
293 ace_id->id_len = sizeof(struct ace_id); /* contains 1 id_data */
294 ace_id->id_data[0] = (smb_entry->a_type==SMB_ACL_GROUP) ? smb_entry->gid : smb_entry->uid;
297 return file_acl;