rpc_server3: Use fdopen_keepfd()
[Samba.git] / source3 / modules / vfs_aixacl_util.c
blob38b53eb3f535a8911c669626f4c91efa7185468a
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, TALLOC_CTX *mem_ctx)
27 struct acl_entry *acl_entry;
28 struct ace_id *idp;
30 struct smb_acl_t *result = sys_acl_init(mem_ctx);
31 struct smb_acl_entry *ace;
32 int i;
34 if (result == NULL) {
35 return NULL;
38 /* Point to the first acl entry in the acl */
39 acl_entry = file_acl->acl_ext;
41 DEBUG(10,("acl_entry is %p\n",(void *)acl_entry));
42 DEBUG(10,("acl_last(file_acl) id %p\n",(void *)acl_last(file_acl)));
44 /* Check if the extended acl bit is on. *
45 * If it isn't, do not show the *
46 * contents of the acl since AIX intends *
47 * the extended info to remain unused */
49 if(file_acl->acl_mode & S_IXACL){
50 /* while we are not pointing to the very end */
51 while(acl_entry < acl_last(file_acl)) {
52 /* before we malloc anything, make sure this is */
53 /* a valid acl entry and one that we want to map */
54 idp = id_nxt(acl_entry->ace_id);
55 if((acl_entry->ace_type == ACC_SPECIFY ||
56 (acl_entry->ace_type == ACC_PERMIT)) && (idp != id_last(acl_entry))) {
57 acl_entry = acl_nxt(acl_entry);
58 continue;
61 idp = acl_entry->ace_id;
62 DEBUG(10,("idp->id_data is %d\n",idp->id_data[0]));
64 result->acl = talloc_realloc(result, result->acl, struct smb_acl_entry, result->count+1);
65 if (result == NULL) {
66 DEBUG(0, ("talloc_realloc failed\n"));
67 errno = ENOMEM;
68 return NULL;
71 DEBUG(10,("idp->id_type is %d\n",idp->id_type));
72 ace = &result->acl[result->count];
74 ace->a_type = idp->id_type;
76 switch(ace->a_type) {
77 case ACEID_USER: {
78 ace->info.user.uid = idp->id_data[0];
79 DEBUG(10,("case ACEID_USER ace->info.user.uid is %d\n",ace->info.user.uid));
80 ace->a_type = SMB_ACL_USER;
81 break;
84 case ACEID_GROUP: {
85 ace->info.group.gid = idp->id_data[0];
86 DEBUG(10,("case ACEID_GROUP ace->info.group.gid is %d\n",ace->info.group.gid));
87 ace->a_type = SMB_ACL_GROUP;
88 break;
90 default:
91 break;
93 /* The access in the acl entries must be left shifted by *
94 * three bites, because they will ultimately be compared *
95 * to S_IRUSR, S_IWUSR, and S_IXUSR. */
97 switch(acl_entry->ace_type){
98 case ACC_PERMIT:
99 case ACC_SPECIFY:
100 ace->a_perm = acl_entry->ace_access;
101 ace->a_perm <<= 6;
102 DEBUG(10,("ace->a_perm is %d\n",ace->a_perm));
103 break;
104 case ACC_DENY:
105 /* Since there is no way to return a DENY acl entry *
106 * change to PERMIT and then shift. */
107 DEBUG(10,("acl_entry->ace_access is %d\n",acl_entry->ace_access));
108 ace->a_perm = ~acl_entry->ace_access & 7;
109 DEBUG(10,("ace->a_perm is %d\n",ace->a_perm));
110 ace->a_perm <<= 6;
111 break;
112 default:
113 DEBUG(0, ("unknown ace->type\n"));
114 TALLOC_FREE(result);
115 return(0);
118 result->count++;
119 ace->a_perm |= (ace->a_perm & S_IRUSR) ? SMB_ACL_READ : 0;
120 ace->a_perm |= (ace->a_perm & S_IWUSR) ? SMB_ACL_WRITE : 0;
121 ace->a_perm |= (ace->a_perm & S_IXUSR) ? SMB_ACL_EXECUTE : 0;
122 DEBUG(10,("ace->a_perm is %d\n",ace->a_perm));
124 DEBUG(10,("acl_entry = %p\n",(void *)acl_entry));
125 DEBUG(10,("The ace_type is %d\n",acl_entry->ace_type));
127 acl_entry = acl_nxt(acl_entry);
129 } /* end of if enabled */
131 /* Since owner, group, other acl entries are not *
132 * part of the acl entries in an acl, they must *
133 * be dummied up to become part of the list. */
135 for( i = 1; i < 4; i++) {
136 DEBUG(10,("i is %d\n",i));
138 result->acl = talloc_realloc(result, result->acl, struct smb_acl_entry, result->count+1);
139 if (result->acl == NULL) {
140 TALLOC_FREE(result);
141 DEBUG(0, ("talloc_realloc failed\n"));
142 errno = ENOMEM;
143 DEBUG(0,("Error in AIX sys_acl_get_file is %d\n",errno));
144 return NULL;
147 ace = &result->acl[result->count];
149 ace->info.user.uid = 0;
150 ace->info.group.gid = 0;
151 DEBUG(10,("ace->info.user.uid = %d\n",ace->info.user.uid));
153 switch(i) {
154 case 2:
155 ace->a_perm = file_acl->g_access << 6;
156 ace->a_type = SMB_ACL_GROUP_OBJ;
157 break;
159 case 3:
160 ace->a_perm = file_acl->o_access << 6;
161 ace->a_type = SMB_ACL_OTHER;
162 break;
164 case 1:
165 ace->a_perm = file_acl->u_access << 6;
166 ace->a_type = SMB_ACL_USER_OBJ;
167 break;
169 default:
170 return(NULL);
173 ace->a_perm |= ((ace->a_perm & S_IRUSR) ? SMB_ACL_READ : 0);
174 ace->a_perm |= ((ace->a_perm & S_IWUSR) ? SMB_ACL_WRITE : 0);
175 ace->a_perm |= ((ace->a_perm & S_IXUSR) ? SMB_ACL_EXECUTE : 0);
177 memcpy(&result->acl[result->count],ace,sizeof(struct smb_acl_entry));
178 result->count++;
179 DEBUG(10,("ace->a_perm = %d\n",ace->a_perm));
180 DEBUG(10,("ace->a_type = %d\n",ace->a_type));
183 return result;
186 static ushort aixacl_smb_to_aixperm(SMB_ACL_PERM_T a_perm)
188 ushort ret = (ushort)0;
189 if (a_perm & SMB_ACL_READ)
190 ret |= R_ACC;
191 if (a_perm & SMB_ACL_WRITE)
192 ret |= W_ACC;
193 if (a_perm & SMB_ACL_EXECUTE)
194 ret |= X_ACC;
195 return ret;
198 struct acl *aixacl_smb_to_aixacl(SMB_ACL_TYPE_T acltype, SMB_ACL_T theacl)
200 struct smb_acl_entry *smb_entry = NULL;
201 struct acl *file_acl = NULL;
202 struct acl *file_acl_temp = NULL;
203 struct acl_entry *acl_entry = NULL;
204 struct ace_id *ace_id = NULL;
205 unsigned int id_type;
206 unsigned int acl_length;
207 int i;
209 DEBUG(10,("Entering aixacl_smb_to_aixacl\n"));
210 /* AIX has no default ACL */
211 if(acltype == SMB_ACL_TYPE_DEFAULT)
212 return NULL;
214 acl_length = BUFSIZ;
215 file_acl = (struct acl *)SMB_MALLOC(BUFSIZ);
216 if(file_acl == NULL) {
217 errno = ENOMEM;
218 DEBUG(0,("Error in aixacl_smb_to_aixacl is %d\n",errno));
219 return NULL;
222 memset(file_acl,0,BUFSIZ);
224 file_acl->acl_len = ACL_SIZ;
225 file_acl->acl_mode = S_IXACL;
227 for(i=0; i<theacl->count; i++ ) {
228 smb_entry = &(theacl->acl[i]);
229 id_type = smb_entry->a_type;
230 DEBUG(10,("The id_type is %d\n",id_type));
232 switch(id_type) {
233 case SMB_ACL_USER_OBJ:
234 file_acl->u_access = aixacl_smb_to_aixperm(smb_entry->a_perm);
235 continue;
236 case SMB_ACL_GROUP_OBJ:
237 file_acl->g_access = aixacl_smb_to_aixperm(smb_entry->a_perm);
238 continue;
239 case SMB_ACL_OTHER:
240 file_acl->o_access = aixacl_smb_to_aixperm(smb_entry->a_perm);
241 continue;
242 case SMB_ACL_MASK:
243 continue;
244 case SMB_ACL_GROUP:
245 break; /* process this */
246 case SMB_ACL_USER:
247 break; /* process this */
248 default: /* abnormal case */
249 DEBUG(10,("The id_type is unknown !\n"));
250 continue;
253 if((file_acl->acl_len + sizeof(struct acl_entry)) > acl_length) {
254 acl_length += sizeof(struct acl_entry);
255 file_acl_temp = (struct acl *)SMB_MALLOC(acl_length);
256 if(file_acl_temp == NULL) {
257 SAFE_FREE(file_acl);
258 errno = ENOMEM;
259 DEBUG(0,("Error in aixacl_smb_to_aixacl is %d\n",errno));
260 return NULL;
263 memcpy(file_acl_temp,file_acl,file_acl->acl_len);
264 SAFE_FREE(file_acl);
265 file_acl = file_acl_temp;
268 acl_entry = (struct acl_entry *)((char *)file_acl + file_acl->acl_len);
269 file_acl->acl_len += sizeof(struct acl_entry);
270 acl_entry->ace_len = sizeof(struct acl_entry); /* contains 1 ace_id */
271 acl_entry->ace_access = aixacl_smb_to_aixperm(smb_entry->a_perm);
273 /* In order to use this, we'll need to wait until we can get denies */
274 /* if(!acl_entry->ace_access && acl_entry->ace_type == ACC_PERMIT)
275 acl_entry->ace_type = ACC_SPECIFY; */
277 acl_entry->ace_type = ACC_SPECIFY;
279 ace_id = acl_entry->ace_id;
281 ace_id->id_type = (smb_entry->a_type==SMB_ACL_GROUP) ? ACEID_GROUP : ACEID_USER;
282 DEBUG(10,("The id type is %d\n",ace_id->id_type));
283 ace_id->id_len = sizeof(struct ace_id); /* contains 1 id_data */
284 ace_id->id_data[0] = (smb_entry->a_type==SMB_ACL_GROUP) ? smb_entry->info.group.gid : smb_entry->info.user.uid;
287 return file_acl;