2 * Copyright 2000, International Business Machines Corporation and others.
5 * This software has been released under the terms of the IBM Public
6 * License. For details, see the LICENSE file in the top-level source
7 * directory or online at http://www.openafs.org/dl/license10.html
11 Information Technology Center
12 Carnegie-Mellon University
16 #include <afsconfig.h>
17 #include <afs/param.h>
25 #include <afs/ptclient.h>
26 #include <afs/ptuser.h>
29 #ifdef AFS_PTHREAD_ENV
31 pthread_mutex_t acl_list_mutex;
32 #endif /* AFS_PTHREAD_ENV */
34 struct freeListEntry {
35 struct freeListEntry *next;
40 struct freeListEntry *freeList;
42 static int AddToList(struct freeListEntry **, struct freeListEntry *);
43 static int GetFromList(struct freeListEntry **, struct freeListEntry **,
46 /*todo: for sorting acls - make sure they work with new groups lists 10/5*/
48 CmpPlus(const void *arg1, const void *arg2)
50 const struct acl_accessEntry *a = (struct acl_accessEntry *) arg1;
51 const struct acl_accessEntry *b = (struct acl_accessEntry *) arg2;
60 CmpMinus(const void *arg1, const void *arg2)
62 const struct acl_accessEntry *a = (struct acl_accessEntry *) arg1;
63 const struct acl_accessEntry *b = (struct acl_accessEntry *) arg2;
84 acl_NewACL(int nEntries, struct acl_accessList **acl)
86 /* Creates an access list capable of holding at least nEntries entries.
87 * Returns 0 on success; aborts if we run out of memory. */
90 struct freeListEntry *e;
92 t = sizeof(struct acl_accessList) + (nEntries -
93 1) * sizeof(struct acl_accessEntry);
94 if (GetFromList(&freeList, &e, t) < 0) {
95 e = malloc(t + sizeof(int) + sizeof(struct freeListEntry *));
97 perror("acl_NewACL: malloc() failed");
101 *acl = (struct acl_accessList *)(e->body);
103 *acl = (struct acl_accessList *)(e->body);
105 (*acl)->size = t; /* May be less than actual size of storage */
106 (*acl)->version = ACL_ACLVERSION;
107 (*acl)->total = nEntries;
108 (*acl)->positive = (*acl)->negative = 0;
114 acl_FreeACL(struct acl_accessList **acl)
116 /* Releases the access list defined by acl. Returns 0 always. */
117 struct freeListEntry *x;
119 x = (struct freeListEntry *)
120 ((char *)*acl - sizeof(struct freeListEntry *) - sizeof(int));
122 return (AddToList(&freeList, x));
126 acl_NewExternalACL(int nEntries, char **r)
128 /* Puts an external acl big enough to hold nEntries in r. Returns 0 on success, aborts if insufficient memory. */
131 struct freeListEntry *e;
133 t = 20 + (nEntries) * (PR_MAXNAMELEN + 20);
134 /* Conservative estimate: enough space in each entry for longest
135 * name plus decimal 2**32 (for largest rights mask) plus some formatting */
137 if (GetFromList(&freeList, &e, t)) {
138 e = malloc(t + sizeof(int) + sizeof(struct freeListEntry *));
140 perror("acl_NewExternalACL(): malloc() failed");
147 sprintf(*r, "0\n0\n");
152 acl_FreeExternalACL(char **r)
154 /* Releases the external access list defined by r. Returns 0 always. */
156 struct freeListEntry *x;
158 x = (struct freeListEntry *)
159 ((char *)*r - sizeof(struct freeListEntry *) - sizeof(int));
161 return (AddToList(&freeList, x));
165 acl_Externalize_pr(int (*func)(idlist *ids, namelist *names), struct acl_accessList *acl, char **elist)
167 /* Converts the access list defined by acl into the external access list
168 * in elist. Non-translatable id's are converted to their ASCII string
169 * representations. Returns 0 on success, -1 if number of entries
170 * exceeds ACL_MAXENTRIES, or a failure code from the protection server
171 * if the problem occured there. */
180 if (acl->total > ACL_MAXENTRIES)
182 acl_NewExternalACL(acl->total, elist);
184 lids.idlist_val = calloc(ACL_MAXENTRIES, sizeof(afs_int32));
185 lids.idlist_len = acl->total;
186 lnames.namelist_len = 0;
187 lnames.namelist_val = (prname *) 0;
188 sprintf(nextc, "%d\n%d\n", acl->positive, acl->negative);
189 nextc += strlen(nextc);
190 for (i = 0; i < acl->positive; i++)
191 lids.idlist_val[i] = acl->entries[i].id;
193 for (i = acl->total - 1; i >= acl->total - acl->negative; i--, j++)
194 lids.idlist_val[j] = acl->entries[i].id;
195 code = (*func)(&lids, &lnames);
198 free(lids.idlist_val);
199 if (lnames.namelist_val)
200 free(lnames.namelist_val);
203 for (i = 0; i < acl->positive; i++) {
204 sprintf(nextc, "%s", lnames.namelist_val[i]);
205 nextc += strlen(nextc);
206 sprintf(nextc, "\t%d\n", acl->entries[i].rights);
207 nextc += strlen(nextc);
210 for (i = acl->total - 1; i >= acl->total - acl->negative; i--, j++) {
211 sprintf(nextc, "%s", lnames.namelist_val[j]);
212 nextc += strlen(nextc);
213 sprintf(nextc, "\t%d\n", acl->entries[i].rights);
214 nextc += strlen(nextc);
217 free(lids.idlist_val);
218 if (lnames.namelist_val)
219 free(lnames.namelist_val);
224 acl_Externalize(struct acl_accessList *acl, char **elist)
226 return acl_Externalize_pr(pr_IdToName, acl, elist);
230 acl_Internalize_pr(int (*func)(namelist *names, idlist *ids), char *elist, struct acl_accessList **acl)
232 /* Converts the external access list elist into the access list acl.
233 * Returns 0 on success, -1 if ANY name is not translatable, or if
234 * the number of entries exceeds al_maxExtEntries. */
243 if (sscanf(elist, "%d\n%d\n", &p, &n) != 2)
245 if (p < 0 || n < 0 || p > INT_MAX - n || p + n > ACL_MAXENTRIES)
247 acl_NewACL(p + n, acl);
248 (*acl)->total = p + n;
249 (*acl)->positive = p;
250 (*acl)->negative = n;
251 if ((*acl)->total == 0) {
252 /* Empty acl entry; simply return success */
255 lnames.namelist_len = (*acl)->total;
256 lnames.namelist_val = malloc(lnames.namelist_len * PR_MAXNAMELEN);
257 if (lnames.namelist_val == 0) {
261 while (*nextc && *nextc != '\n')
264 while (*nextc && *nextc != '\n')
266 nextc++; /* now at the beginning of the entry list */
267 for (i = 0; i < (*acl)->positive; i++) {
269 if (sscanf(nextc, "%63s\t%d\n", lnames.namelist_val[i], &k) != 2) {
270 free(lnames.namelist_val);
273 (*acl)->entries[i].rights = k;
274 nextc = strchr(nextc, '\n');
275 nextc++; /* 1 + index can cast ptr to integer */
278 for (i = (*acl)->total - 1; i >= (*acl)->total - (*acl)->negative;
281 (nextc, "%63s\t%d\n", lnames.namelist_val[j],
282 &((*acl)->entries[j].rights)) != 2) {
283 free(lnames.namelist_val);
286 nextc = strchr(nextc, '\n');
292 code = (*func)(&lnames, &lids);
294 free(lnames.namelist_val);
296 free(lids.idlist_val);
299 for (i = 0; i < (*acl)->positive; i++) {
300 if (lids.idlist_val[i] == ANONYMOUSID) {
301 free(lnames.namelist_val);
303 free(lids.idlist_val);
306 (*acl)->entries[i].id = lids.idlist_val[i];
308 for (i = (*acl)->total - 1; i >= (*acl)->total - (*acl)->negative; i--) {
309 if (lids.idlist_val[i] == ANONYMOUSID) {
310 free(lnames.namelist_val);
312 free(lids.idlist_val);
315 (*acl)->entries[i].id = lids.idlist_val[i];
317 /* sort for easier lookup */
318 qsort(&((*acl)->entries[0]), (*acl)->positive,
319 sizeof(struct acl_accessEntry), CmpPlus);
320 qsort(&((*acl)->entries[(*acl)->total - (*acl)->negative]),
321 (*acl)->negative, sizeof(struct acl_accessEntry), CmpMinus);
322 free(lnames.namelist_val);
324 free(lids.idlist_val);
329 acl_Internalize(char *elist, struct acl_accessList **acl)
331 return acl_Internalize_pr(pr_NameToId, elist, acl);
335 acl_CheckRights(struct acl_accessList *acl, prlist *groups, int *rights)
337 /* Returns the rights given by acl to groups */
339 int temprights; /* positive rights accumulated so far */
340 int negrights; /* negative rights accumulated so far */
341 int a; /* index into next entry in acl */
342 int c; /* index into next entry in CPS */
344 /* more sanity checks */
345 if (acl->total > ACL_MAXENTRIES)
350 return 1; /* 192 is the room in a 256 byte vnode reserved for the ACL */
352 if (acl->total <= 0 || groups->prlist_len <= 0) {
356 if (groups->prlist_val[groups->prlist_len - 1] == SYSADMINID) {
361 /* Each iteration eats up exactly one entry from either acl or groups.
362 * Duplicate Entries in access list ==> accumulated rights are obtained.
363 * Duplicate Entries in groups ==> irrelevant */
366 while ((a < acl->positive) && (c < groups->prlist_len))
367 switch (CmpInt(acl->entries[a].id, groups->prlist_val[c])) {
373 temprights |= acl->entries[a].rights;
382 printf("CmpInt() returned bogus value. Aborting ...\n");
388 while ((c < groups->prlist_len) && (a > acl->total - acl->negative - 1))
389 switch (CmpInt(acl->entries[a].id, groups->prlist_val[c])) {
394 negrights |= acl->entries[a].rights;
401 *rights = temprights & (~negrights);
406 acl_Initialize(char *version)
408 /* I'm sure we need to do some initialization, I'm just not quite sure what yet! */
409 if (strcmp(version, ACL_VERSION) != 0) {
410 fprintf(stderr, "Wrong version of acl package!\n");
411 fprintf(stderr, "This is version %s, file server passed in %s.\n",
412 ACL_VERSION, version);
414 #ifdef AFS_PTHREAD_ENV
415 opr_Verify(pthread_mutex_init(&acl_list_mutex, NULL) == 0);
416 #endif /* AFS_PTHREAD_ENV */
421 acl_IsAMember(afs_int32 aid, prlist *cps)
425 for (i = 0; i < cps->prlist_len; i++)
426 if (cps->prlist_val[i] == aid)
433 AddToList(struct freeListEntry **pflist, struct freeListEntry *elem)
435 /* Adds elem to the freelist flist; returns 0 */
436 #ifdef AFS_PTHREAD_ENV
437 opr_Verify(pthread_mutex_lock(&acl_list_mutex) == 0);
438 #endif /* AFS_PTHREAD_ENV */
439 elem->next = *pflist;
441 #ifdef AFS_PTHREAD_ENV
442 opr_Verify(pthread_mutex_unlock(&acl_list_mutex) == 0);
443 #endif /* AFS_PTHREAD_ENV */
448 GetFromList(struct freeListEntry **pflist, struct freeListEntry **elem,
451 /* Looks for an element whose body is at least minsize bytes in the
452 * freelist flist. If found, unlinks it, puts its address in elem,
453 * and returns 0, else returns -1. A trivial first-fit algorithm is
456 struct freeListEntry *y, *z;
458 #ifdef AFS_PTHREAD_ENV
459 opr_Verify(pthread_mutex_lock(&acl_list_mutex) == 0);
460 #endif /* AFS_PTHREAD_ENV */
461 if (*pflist == NULL) {
462 #ifdef AFS_PTHREAD_ENV
463 opr_Verify(pthread_mutex_unlock(&acl_list_mutex) == 0);
464 #endif /* AFS_PTHREAD_ENV */
467 for (y = *pflist, z = NULL; y != NULL; z = y, y = y->next) {
468 if (y->size >= minsize) {
470 if (z == NULL) { /* pulling off the head */
472 #ifdef AFS_PTHREAD_ENV
473 opr_Verify(pthread_mutex_unlock(&acl_list_mutex) == 0);
474 #endif /* AFS_PTHREAD_ENV */
478 #ifdef AFS_PTHREAD_ENV
479 opr_Verify(pthread_mutex_unlock(&acl_list_mutex) == 0);
480 #endif /* AFS_PTHREAD_ENV */
484 #ifdef AFS_PTHREAD_ENV
485 opr_Verify(pthread_mutex_unlock(&acl_list_mutex) == 0);
486 #endif /* AFS_PTHREAD_ENV */