librpc: remove nbt dependency to svcctl.
[Samba.git] / source3 / modules / vfs_aixacl_util.c
blobd867f9d92fcf07e4bae65e1dbe61a1a19512d429
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"
24 SMB_ACL_T aixacl_to_smbacl(struct acl *file_acl)
26 struct acl_entry *acl_entry;
27 struct ace_id *idp;
29 struct smb_acl_t *result = SMB_MALLOC_P(struct smb_acl_t);
30 struct smb_acl_entry *ace;
31 int i;
33 if (result == NULL) {
34 return NULL;
36 ZERO_STRUCTP(result);
38 /* Point to the first acl entry in the acl */
39 acl_entry = file_acl->acl_ext;
43 DEBUG(10,("acl_entry is %p\n",(void *)acl_entry));
44 DEBUG(10,("acl_last(file_acl) id %p\n",(void *)acl_last(file_acl)));
46 /* Check if the extended acl bit is on. *
47 * If it isn't, do not show the *
48 * contents of the acl since AIX intends *
49 * the extended info to remain unused */
51 if(file_acl->acl_mode & S_IXACL){
52 /* while we are not pointing to the very end */
53 while(acl_entry < acl_last(file_acl)) {
54 /* before we malloc anything, make sure this is */
55 /* a valid acl entry and one that we want to map */
56 idp = id_nxt(acl_entry->ace_id);
57 if((acl_entry->ace_type == ACC_SPECIFY ||
58 (acl_entry->ace_type == ACC_PERMIT)) && (idp != id_last(acl_entry))) {
59 acl_entry = acl_nxt(acl_entry);
60 continue;
63 idp = acl_entry->ace_id;
64 DEBUG(10,("idp->id_data is %d\n",idp->id_data[0]));
66 result = SMB_REALLOC(result, sizeof(struct smb_acl_t) +
67 (sizeof(struct smb_acl_entry) *
68 (result->count+1)));
69 if (result == NULL) {
70 DEBUG(0, ("SMB_REALLOC failed\n"));
71 errno = ENOMEM;
72 return NULL;
76 DEBUG(10,("idp->id_type is %d\n",idp->id_type));
77 ace = &result->acl[result->count];
79 ace->a_type = idp->id_type;
81 switch(ace->a_type) {
82 case ACEID_USER: {
83 ace->uid = idp->id_data[0];
84 DEBUG(10,("case ACEID_USER ace->uid is %d\n",ace->uid));
85 ace->a_type = SMB_ACL_USER;
86 break;
89 case ACEID_GROUP: {
90 ace->gid = idp->id_data[0];
91 DEBUG(10,("case ACEID_GROUP ace->gid is %d\n",ace->gid));
92 ace->a_type = SMB_ACL_GROUP;
93 break;
95 default:
96 break;
98 /* The access in the acl entries must be left shifted by *
99 * three bites, because they will ultimately be compared *
100 * to S_IRUSR, S_IWUSR, and S_IXUSR. */
102 switch(acl_entry->ace_type){
103 case ACC_PERMIT:
104 case ACC_SPECIFY:
105 ace->a_perm = acl_entry->ace_access;
106 ace->a_perm <<= 6;
107 DEBUG(10,("ace->a_perm is %d\n",ace->a_perm));
108 break;
109 case ACC_DENY:
110 /* Since there is no way to return a DENY acl entry *
111 * change to PERMIT and then shift. */
112 DEBUG(10,("acl_entry->ace_access is %d\n",acl_entry->ace_access));
113 ace->a_perm = ~acl_entry->ace_access & 7;
114 DEBUG(10,("ace->a_perm is %d\n",ace->a_perm));
115 ace->a_perm <<= 6;
116 break;
117 default:
118 DEBUG(0, ("unknown ace->type\n"));
119 SAFE_FREE(result);
120 return(0);
123 result->count++;
124 ace->a_perm |= (ace->a_perm & S_IRUSR) ? SMB_ACL_READ : 0;
125 ace->a_perm |= (ace->a_perm & S_IWUSR) ? SMB_ACL_WRITE : 0;
126 ace->a_perm |= (ace->a_perm & S_IXUSR) ? SMB_ACL_EXECUTE : 0;
127 DEBUG(10,("ace->a_perm is %d\n",ace->a_perm));
129 DEBUG(10,("acl_entry = %p\n",(void *)acl_entry));
130 DEBUG(10,("The ace_type is %d\n",acl_entry->ace_type));
132 acl_entry = acl_nxt(acl_entry);
134 } /* end of if enabled */
136 /* Since owner, group, other acl entries are not *
137 * part of the acl entries in an acl, they must *
138 * be dummied up to become part of the list. */
140 for( i = 1; i < 4; i++) {
141 DEBUG(10,("i is %d\n",i));
143 result = SMB_REALLOC(result, sizeof(struct smb_acl_t) +
144 (sizeof(struct smb_acl_entry) *
145 (result->count+1)));
146 if (result == NULL) {
147 DEBUG(0, ("SMB_REALLOC failed\n"));
148 errno = ENOMEM;
149 DEBUG(0,("Error in AIX sys_acl_get_file is %d\n",errno));
150 return NULL;
153 ace = &result->acl[result->count];
155 ace->uid = 0;
156 ace->gid = 0;
157 DEBUG(10,("ace->uid = %d\n",ace->uid));
159 switch(i) {
160 case 2:
161 ace->a_perm = file_acl->g_access << 6;
162 ace->a_type = SMB_ACL_GROUP_OBJ;
163 break;
165 case 3:
166 ace->a_perm = file_acl->o_access << 6;
167 ace->a_type = SMB_ACL_OTHER;
168 break;
170 case 1:
171 ace->a_perm = file_acl->u_access << 6;
172 ace->a_type = SMB_ACL_USER_OBJ;
173 break;
175 default:
176 return(NULL);
179 ace->a_perm |= ((ace->a_perm & S_IRUSR) ? SMB_ACL_READ : 0);
180 ace->a_perm |= ((ace->a_perm & S_IWUSR) ? SMB_ACL_WRITE : 0);
181 ace->a_perm |= ((ace->a_perm & S_IXUSR) ? SMB_ACL_EXECUTE : 0);
183 memcpy(&result->acl[result->count],ace,sizeof(struct smb_acl_entry));
184 result->count++;
185 DEBUG(10,("ace->a_perm = %d\n",ace->a_perm));
186 DEBUG(10,("ace->a_type = %d\n",ace->a_type));
190 return result;
195 static ushort aixacl_smb_to_aixperm(SMB_ACL_PERM_T a_perm)
197 ushort ret = (ushort)0;
198 if (a_perm & SMB_ACL_READ)
199 ret |= R_ACC;
200 if (a_perm & SMB_ACL_WRITE)
201 ret |= W_ACC;
202 if (a_perm & SMB_ACL_EXECUTE)
203 ret |= X_ACC;
204 return ret;
207 struct acl *aixacl_smb_to_aixacl(SMB_ACL_TYPE_T acltype, SMB_ACL_T theacl)
209 struct smb_acl_entry *smb_entry = NULL;
210 struct acl *file_acl = NULL;
211 struct acl *file_acl_temp = NULL;
212 struct acl_entry *acl_entry = NULL;
213 struct ace_id *ace_id = NULL;
214 unsigned int id_type;
215 unsigned int acl_length;
216 int i;
218 DEBUG(10,("Entering aixacl_smb_to_aixacl\n"));
219 /* AIX has no default ACL */
220 if(acltype == SMB_ACL_TYPE_DEFAULT)
221 return NULL;
223 acl_length = BUFSIZ;
224 file_acl = (struct acl *)SMB_MALLOC(BUFSIZ);
225 if(file_acl == NULL) {
226 errno = ENOMEM;
227 DEBUG(0,("Error in aixacl_smb_to_aixacl is %d\n",errno));
228 return NULL;
231 memset(file_acl,0,BUFSIZ);
233 file_acl->acl_len = ACL_SIZ;
234 file_acl->acl_mode = S_IXACL;
236 for(i=0; i<theacl->count; i++ ) {
237 smb_entry = &(theacl->acl[i]);
238 id_type = smb_entry->a_type;
239 DEBUG(10,("The id_type is %d\n",id_type));
241 switch(id_type) {
242 case SMB_ACL_USER_OBJ:
243 file_acl->u_access = aixacl_smb_to_aixperm(smb_entry->a_perm);
244 continue;
245 case SMB_ACL_GROUP_OBJ:
246 file_acl->g_access = aixacl_smb_to_aixperm(smb_entry->a_perm);
247 continue;
248 case SMB_ACL_OTHER:
249 file_acl->o_access = aixacl_smb_to_aixperm(smb_entry->a_perm);
250 continue;
251 case SMB_ACL_MASK:
252 continue;
253 case SMB_ACL_GROUP:
254 break; /* process this */
255 case SMB_ACL_USER:
256 break; /* process this */
257 default: /* abnormal case */
258 DEBUG(10,("The id_type is unknown !\n"));
259 continue;
262 if((file_acl->acl_len + sizeof(struct acl_entry)) > acl_length) {
263 acl_length += sizeof(struct acl_entry);
264 file_acl_temp = (struct acl *)SMB_MALLOC(acl_length);
265 if(file_acl_temp == NULL) {
266 SAFE_FREE(file_acl);
267 errno = ENOMEM;
268 DEBUG(0,("Error in aixacl_smb_to_aixacl is %d\n",errno));
269 return NULL;
272 memcpy(file_acl_temp,file_acl,file_acl->acl_len);
273 SAFE_FREE(file_acl);
274 file_acl = file_acl_temp;
277 acl_entry = (struct acl_entry *)((char *)file_acl + file_acl->acl_len);
278 file_acl->acl_len += sizeof(struct acl_entry);
279 acl_entry->ace_len = sizeof(struct acl_entry); /* contains 1 ace_id */
280 acl_entry->ace_access = aixacl_smb_to_aixperm(smb_entry->a_perm);
282 /* In order to use this, we'll need to wait until we can get denies */
283 /* if(!acl_entry->ace_access && acl_entry->ace_type == ACC_PERMIT)
284 acl_entry->ace_type = ACC_SPECIFY; */
286 acl_entry->ace_type = ACC_SPECIFY;
288 ace_id = acl_entry->ace_id;
290 ace_id->id_type = (smb_entry->a_type==SMB_ACL_GROUP) ? ACEID_GROUP : ACEID_USER;
291 DEBUG(10,("The id type is %d\n",ace_id->id_type));
292 ace_id->id_len = sizeof(struct ace_id); /* contains 1 id_data */
293 ace_id->id_data[0] = (smb_entry->a_type==SMB_ACL_GROUP) ? smb_entry->gid : smb_entry->uid;
296 return file_acl;