smbd-posix_acls: Use a IDL union to store the ACL entry
[kai/samba.git] / source3 / modules / vfs_aixacl_util.c
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
5
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.
10
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.
15
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/>.
18 */
19
20 #include "includes.h"
21 #include "system/filesys.h"
22 #include "smbd/smbd.h"
23 #include "vfs_aixacl_util.h"
24
25 SMB_ACL_T aixacl_to_smbacl(struct acl *file_acl)
26 {
27         struct acl_entry *acl_entry;
28         struct ace_id *idp;
29         
30         struct smb_acl_t *result = sys_acl_init(0);
31         struct smb_acl_entry *ace;
32         int i;
33         
34         if (result == NULL) {
35                 return NULL;
36         }
37         
38         /* Point to the first acl entry in the acl */
39         acl_entry =  file_acl->acl_ext;
40
41
42         
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)));
45
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     */
50
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;
61                         }
62
63                         idp = acl_entry->ace_id;
64                         DEBUG(10,("idp->id_data is %d\n",idp->id_data[0]));
65                         
66                         result->acl = talloc_realloc(result, result->acl, struct smb_acl_entry, result->count+1);
67                         if (result == NULL) {
68                                 DEBUG(0, ("talloc_realloc failed\n"));
69                                 errno = ENOMEM;
70                                 return NULL;
71                         }
72                         
73
74                         DEBUG(10,("idp->id_type is %d\n",idp->id_type));
75                         ace = &result->acl[result->count];
76                         
77                         ace->a_type = idp->id_type;
78                                                         
79                         switch(ace->a_type) {
80                         case ACEID_USER: {
81                         ace->uid = idp->id_data[0];
82                         DEBUG(10,("case ACEID_USER ace->uid is %d\n",ace->uid));
83                         ace->a_type = SMB_ACL_USER;
84                         break;
85                         }
86                 
87                         case ACEID_GROUP: {
88                         ace->gid = idp->id_data[0];
89                         DEBUG(10,("case ACEID_GROUP ace->gid is %d\n",ace->gid));
90                         ace->a_type = SMB_ACL_GROUP;
91                         break;
92                         }
93                         default:
94                                 break;
95                         }
96                         /* The access in the acl entries must be left shifted by *
97                          * three bites, because they will ultimately be compared *
98                          * to S_IRUSR, S_IWUSR, and S_IXUSR.                  */
99
100                         switch(acl_entry->ace_type){
101                         case ACC_PERMIT:
102                         case ACC_SPECIFY:
103                                 ace->a_perm = acl_entry->ace_access;
104                                 ace->a_perm <<= 6;
105                                 DEBUG(10,("ace->a_perm is %d\n",ace->a_perm));
106                                 break;
107                         case ACC_DENY:
108                                 /* Since there is no way to return a DENY acl entry *
109                                  * change to PERMIT and then shift.                 */
110                                 DEBUG(10,("acl_entry->ace_access is %d\n",acl_entry->ace_access));
111                                 ace->a_perm = ~acl_entry->ace_access & 7;
112                                 DEBUG(10,("ace->a_perm is %d\n",ace->a_perm));
113                                 ace->a_perm <<= 6;
114                                 break;
115                         default:
116                                 DEBUG(0, ("unknown ace->type\n"));
117                                 TALLOC_FREE(result);
118                                 return(0);
119                         }
120                 
121                         result->count++;
122                         ace->a_perm |= (ace->a_perm & S_IRUSR) ? SMB_ACL_READ : 0;
123                         ace->a_perm |= (ace->a_perm & S_IWUSR) ? SMB_ACL_WRITE : 0;
124                         ace->a_perm |= (ace->a_perm & S_IXUSR) ? SMB_ACL_EXECUTE : 0;
125                         DEBUG(10,("ace->a_perm is %d\n",ace->a_perm));
126                         
127                         DEBUG(10,("acl_entry = %p\n",(void *)acl_entry));
128                         DEBUG(10,("The ace_type is %d\n",acl_entry->ace_type));
129  
130                         acl_entry = acl_nxt(acl_entry);
131                 }
132         } /* end of if enabled */
133
134         /* Since owner, group, other acl entries are not *
135          * part of the acl entries in an acl, they must  *
136          * be dummied up to become part of the list.     */
137
138         for( i = 1; i < 4; i++) {
139                 DEBUG(10,("i is %d\n",i));
140
141                 result->acl = talloc_realloc(result, result->acl, struct smb_acl_entry, result->count+1);
142                 if (result->acl == NULL) {
143                         TALLOC_FREE(result);
144                         DEBUG(0, ("talloc_realloc failed\n"));
145                         errno = ENOMEM;
146                         DEBUG(0,("Error in AIX sys_acl_get_file is %d\n",errno));
147                         return NULL;
148                 }
149                         
150                 ace = &result->acl[result->count];
151                 
152                 ace->uid = 0;
153                 ace->gid = 0;
154                 DEBUG(10,("ace->uid = %d\n",ace->uid));
155                 
156                 switch(i) {
157                 case 2:
158                         ace->a_perm = file_acl->g_access << 6;
159                         ace->a_type = SMB_ACL_GROUP_OBJ;
160                         break;
161
162                 case 3:
163                         ace->a_perm = file_acl->o_access << 6;
164                         ace->a_type = SMB_ACL_OTHER;
165                         break;
166  
167                 case 1:
168                         ace->a_perm = file_acl->u_access << 6;
169                         ace->a_type = SMB_ACL_USER_OBJ;
170                         break;
171  
172                 default:
173                         return(NULL);
174
175                 }
176                 ace->a_perm |= ((ace->a_perm & S_IRUSR) ? SMB_ACL_READ : 0);
177                 ace->a_perm |= ((ace->a_perm & S_IWUSR) ? SMB_ACL_WRITE : 0);
178                 ace->a_perm |= ((ace->a_perm & S_IXUSR) ? SMB_ACL_EXECUTE : 0);
179                 
180                 memcpy(&result->acl[result->count],ace,sizeof(struct smb_acl_entry));
181                 result->count++;
182                 DEBUG(10,("ace->a_perm = %d\n",ace->a_perm));
183                 DEBUG(10,("ace->a_type = %d\n",ace->a_type));
184         }
185
186
187         return result;
188
189
190 }
191
192 static ushort aixacl_smb_to_aixperm(SMB_ACL_PERM_T a_perm)
193 {
194         ushort ret = (ushort)0;
195         if (a_perm & SMB_ACL_READ)
196                 ret |= R_ACC;
197         if (a_perm & SMB_ACL_WRITE)
198                 ret |= W_ACC;
199         if (a_perm & SMB_ACL_EXECUTE)
200                 ret |= X_ACC;
201         return ret;
202 }
203
204 struct acl *aixacl_smb_to_aixacl(SMB_ACL_TYPE_T acltype, SMB_ACL_T theacl)
205 {
206         struct smb_acl_entry *smb_entry = NULL;
207         struct acl *file_acl = NULL;
208         struct acl *file_acl_temp = NULL;
209         struct acl_entry *acl_entry = NULL;
210         struct ace_id *ace_id = NULL;
211         unsigned int id_type;
212         unsigned int acl_length;
213         int     i;
214  
215         DEBUG(10,("Entering aixacl_smb_to_aixacl\n"));
216         /* AIX has no default ACL */
217         if(acltype == SMB_ACL_TYPE_DEFAULT)
218                 return NULL;
219
220         acl_length = BUFSIZ;
221         file_acl = (struct acl *)SMB_MALLOC(BUFSIZ);
222         if(file_acl == NULL) {
223                 errno = ENOMEM;
224                 DEBUG(0,("Error in aixacl_smb_to_aixacl is %d\n",errno));
225                 return NULL;
226         }
227
228         memset(file_acl,0,BUFSIZ);
229  
230         file_acl->acl_len = ACL_SIZ;
231         file_acl->acl_mode = S_IXACL;
232
233         for(i=0; i<theacl->count; i++ ) {
234                 smb_entry = &(theacl->acl[i]);
235                 id_type = smb_entry->a_type;
236                 DEBUG(10,("The id_type is %d\n",id_type));
237
238                 switch(id_type) {
239                         case SMB_ACL_USER_OBJ:
240                                 file_acl->u_access = aixacl_smb_to_aixperm(smb_entry->a_perm);
241                                 continue;
242                         case SMB_ACL_GROUP_OBJ:
243                                 file_acl->g_access = aixacl_smb_to_aixperm(smb_entry->a_perm);
244                                 continue;
245                         case SMB_ACL_OTHER:
246                                 file_acl->o_access = aixacl_smb_to_aixperm(smb_entry->a_perm);
247                                 continue;
248                         case SMB_ACL_MASK:
249                                 continue;
250                         case SMB_ACL_GROUP:
251                                 break; /* process this */
252                         case SMB_ACL_USER:
253                                 break; /* process this */
254                         default: /* abnormal case */
255                                 DEBUG(10,("The id_type is unknown !\n"));
256                                 continue;
257                 }
258
259                 if((file_acl->acl_len + sizeof(struct acl_entry)) > acl_length) {
260                         acl_length += sizeof(struct acl_entry);
261                         file_acl_temp = (struct acl *)SMB_MALLOC(acl_length);
262                         if(file_acl_temp == NULL) {
263                                 SAFE_FREE(file_acl);
264                                 errno = ENOMEM;
265                                 DEBUG(0,("Error in aixacl_smb_to_aixacl is %d\n",errno));
266                                 return NULL;
267                         }
268
269                         memcpy(file_acl_temp,file_acl,file_acl->acl_len);
270                         SAFE_FREE(file_acl);
271                         file_acl = file_acl_temp;
272                 }
273
274                 acl_entry = (struct acl_entry *)((char *)file_acl + file_acl->acl_len);
275                 file_acl->acl_len += sizeof(struct acl_entry);
276                 acl_entry->ace_len = sizeof(struct acl_entry); /* contains 1 ace_id */
277                 acl_entry->ace_access = aixacl_smb_to_aixperm(smb_entry->a_perm);
278
279                 /* In order to use this, we'll need to wait until we can get denies */
280                 /* if(!acl_entry->ace_access && acl_entry->ace_type == ACC_PERMIT)
281                 acl_entry->ace_type = ACC_SPECIFY; */
282
283                 acl_entry->ace_type = ACC_SPECIFY;
284
285                 ace_id = acl_entry->ace_id;
286
287                 ace_id->id_type = (smb_entry->a_type==SMB_ACL_GROUP) ? ACEID_GROUP : ACEID_USER;
288                 DEBUG(10,("The id type is %d\n",ace_id->id_type));
289                 ace_id->id_len = sizeof(struct ace_id); /* contains 1 id_data */
290                 ace_id->id_data[0] = (smb_entry->a_type==SMB_ACL_GROUP) ? smb_entry->gid : smb_entry->uid;
291         }
292
293         return file_acl;
294 }