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>
22 #include <sys/types.h>
26 #include <netinet/in.h>
33 #include <afs/ptclient.h>
36 #ifdef AFS_PTHREAD_ENV
39 pthread_mutex_t acl_list_mutex;
40 #endif /* AFS_PTHREAD_ENV */
42 static int AddToList();
43 static int GetFromList();
45 struct freeListEntry {
46 struct freeListEntry *next;
51 struct freeListEntry *freeList;
53 /*todo: for sorting acls - make sure they work with new groups lists 10/5*/
56 struct acl_accessEntry *a, *b;
67 struct acl_accessEntry *a, *b;
89 acl_NewACL(nEntries, acl)
91 struct acl_accessList **acl;
93 /* Creates an access list capable of holding at least nEntries entries.
94 * Returns 0 on success; aborts if we run out of memory. */
97 struct freeListEntry *e;
99 t = sizeof(struct acl_accessList) + (nEntries -
100 1) * sizeof(struct acl_accessEntry);
101 if (GetFromList(&freeList, &e, t) < 0) {
102 e = (struct freeListEntry *)malloc(t + sizeof(int) +
103 sizeof(struct freeListEntry *));
105 perror("acl_NewACL: malloc() failed");
109 *acl = (struct acl_accessList *)(e->body);
111 *acl = (struct acl_accessList *)(e->body);
113 (*acl)->size = t; /* May be less than actual size of storage */
114 (*acl)->version = ACL_ACLVERSION;
115 (*acl)->total = nEntries;
116 (*acl)->positive = (*acl)->negative = 0;
123 struct acl_accessList **acl;
125 /* Releases the access list defined by acl. Returns 0 always. */
126 struct freeListEntry *x;
128 x = (struct freeListEntry *)
129 ((char *)*acl - sizeof(struct freeListEntry *) - sizeof(int));
131 return (AddToList(&freeList, x));
135 acl_NewExternalACL(nEntries, r)
139 /* Puts an external acl big enough to hold nEntries in r. Returns 0 on success, aborts if insufficient memory. */
142 struct freeListEntry *e;
144 t = 20 + (nEntries) * (PR_MAXNAMELEN + 20);
145 /* Conservative estimate: enough space in each entry for longest
146 * name plus decimal 2**32 (for largest rights mask) plus some formatting */
148 if (GetFromList(&freeList, &e, t)) {
149 e = (struct freeListEntry *)malloc(t + sizeof(int) +
150 sizeof(struct freeListEntry *));
152 perror("acl_NewExternalACL(): malloc() failed");
159 sprintf(*r, "0\n0\n");
164 acl_FreeExternalACL(r)
167 /* Releases the external access list defined by r. Returns 0 always. */
169 struct freeListEntry *x;
171 x = (struct freeListEntry *)
172 ((char *)*r - sizeof(struct freeListEntry *) - sizeof(int));
174 return (AddToList(&freeList, x));
179 acl_Externalize(acl, elist)
180 struct acl_accessList *acl;
183 /* Converts the access list defined by acl into the external access list in elist. Non-translatable id's are converted to their ASCII string representations. Returns 0 on success, -1 if number of entries exceeds ACL_MAXENTRIES, or a failure code from the protection server if the problem occured there. */
188 register char *nextc;
192 if (acl->total > ACL_MAXENTRIES)
194 acl_NewExternalACL(acl->total, elist);
197 (afs_int32 *) malloc(ACL_MAXENTRIES * sizeof(afs_int32));
198 memset(lids.idlist_val, 0, ACL_MAXENTRIES * sizeof(afs_int32));
199 lids.idlist_len = acl->total;
200 lnames.namelist_len = 0;
201 lnames.namelist_val = (prname *) 0;
202 sprintf(nextc, "%d\n%d\n", acl->positive, acl->negative);
203 nextc += strlen(nextc);
204 for (i = 0; i < acl->positive; i++)
205 lids.idlist_val[i] = acl->entries[i].id;
207 for (i = acl->total - 1; i >= acl->total - acl->negative; i--, j++)
208 lids.idlist_val[j] = acl->entries[i].id;
209 code = pr_IdToName(&lids, &lnames);
212 free(lids.idlist_val);
213 if (lnames.namelist_val)
214 free(lnames.namelist_val);
217 for (i = 0; i < acl->positive; i++) {
218 sprintf(nextc, "%s", lnames.namelist_val[i]);
219 nextc += strlen(nextc);
220 sprintf(nextc, "\t%d\n", acl->entries[i].rights);
221 nextc += strlen(nextc);
224 for (i = acl->total - 1; i >= acl->total - acl->negative; i--, j++) {
225 sprintf(nextc, "%s", lnames.namelist_val[j]);
226 nextc += strlen(nextc);
227 sprintf(nextc, "\t%d\n", acl->entries[i].rights);
228 nextc += strlen(nextc);
231 free(lids.idlist_val);
232 if (lnames.namelist_val)
233 free(lnames.namelist_val);
239 acl_Internalize(elist, acl)
241 struct acl_accessList **acl;
243 /* Converts the external access list elist into the access list acl. Returns 0 on success, -1 if ANY name is not translatable, or if the number of entries exceeds al_maxExtEntries. */
246 register char *nextc;
247 register afs_int32 code;
252 if (sscanf(elist, "%d\n%d\n", &p, &n) != 2)
254 if (p + n > ACL_MAXENTRIES)
256 acl_NewACL(p + n, acl);
257 (*acl)->total = p + n;
258 (*acl)->positive = p;
259 (*acl)->negative = n;
260 if ((*acl)->total == 0) {
261 /* Empty acl entry; simply return success */
264 lnames.namelist_len = (*acl)->total;
265 lnames.namelist_val =
266 (prname *) malloc(lnames.namelist_len * PR_MAXNAMELEN);
267 if (lnames.namelist_val == 0) {
271 while (*nextc && *nextc != '\n')
274 while (*nextc && *nextc != '\n')
276 nextc++; /* now at the beginning of the entry list */
277 for (i = 0; i < (*acl)->positive; i++) {
279 if (sscanf(nextc, "%s\t%d\n", lnames.namelist_val[i], &k) != 2) {
280 free(lnames.namelist_val);
283 (*acl)->entries[i].rights = k;
284 nextc = strchr(nextc, '\n');
285 nextc++; /* 1 + index can cast ptr to integer */
288 for (i = (*acl)->total - 1; i >= (*acl)->total - (*acl)->negative;
291 (nextc, "%s\t%d\n", lnames.namelist_val[j],
292 &((*acl)->entries[j].rights)) != 2) {
293 free(lnames.namelist_val);
296 nextc = strchr(nextc, '\n');
302 code = pr_NameToId(&lnames, &lids);
304 free(lnames.namelist_val);
306 free(lids.idlist_val);
309 for (i = 0; i < (*acl)->positive; i++) {
310 if (lids.idlist_val[i] == ANONYMOUSID) {
311 free(lnames.namelist_val);
313 free(lids.idlist_val);
316 (*acl)->entries[i].id = lids.idlist_val[i];
319 for (i = (*acl)->total - 1; i >= (*acl)->total - (*acl)->negative; i--) {
320 if (lids.idlist_val[i] == ANONYMOUSID) {
321 free(lnames.namelist_val);
323 free(lids.idlist_val);
326 (*acl)->entries[i].id = lids.idlist_val[i];
328 /* sort for easier lookup */
329 qsort(&((*acl)->entries[0]), (*acl)->positive,
330 sizeof(struct acl_accessEntry), CmpPlus);
331 qsort(&((*acl)->entries[(*acl)->total - (*acl)->negative]),
332 (*acl)->negative, sizeof(struct acl_accessEntry), CmpMinus);
333 free(lnames.namelist_val);
335 free(lids.idlist_val);
341 acl_CheckRights(acl, groups, rights)
342 struct acl_accessList *acl;
346 /* Returns the rights given by acl to groups */
348 int temprights; /* positive rights accumulated so far */
349 int negrights; /* negative rights accumulated so far */
350 int a; /* index into next entry in acl */
351 int c; /* index into next entry in CPS */
353 /* more sanity checks */
354 if (acl->total > ACL_MAXENTRIES)
359 return 1; /* 192 is the room in a 256 byte vnode reserved for the ACL */
361 if (acl->total <= 0 || groups->prlist_len <= 0) {
365 if (groups->prlist_val[groups->prlist_len - 1] == SYSADMINID) {
370 /* Each iteration eats up exactly one entry from either acl or groups.
371 * Duplicate Entries in access list ==> accumulated rights are obtained.
372 * Duplicate Entries in groups ==> irrelevant */
375 while ((a < acl->positive) && (c < groups->prlist_len))
376 switch (CmpInt(acl->entries[a].id, groups->prlist_val[c])) {
382 temprights |= acl->entries[a].rights;
391 printf("CmpInt() returned bogus value. Aborting ...\n");
397 while ((c < groups->prlist_len) && (a > acl->total - acl->negative - 1))
398 switch (CmpInt(acl->entries[a].id, groups->prlist_val[c])) {
403 negrights |= acl->entries[a].rights;
410 *rights = temprights & (~negrights);
415 acl_Initialize(version)
418 /* I'm sure we need to do some initialization, I'm just not quite sure what yet! */
419 if (strcmp(version, ACL_VERSION) != 0) {
420 fprintf(stderr, "Wrong version of acl package!\n");
421 fprintf(stderr, "This is version %s, file server passed in %s.\n",
422 ACL_VERSION, version);
424 #ifdef AFS_PTHREAD_ENV
425 assert(pthread_mutex_init(&acl_list_mutex, NULL) == 0);
426 #endif /* AFS_PTHREAD_ENV */
431 acl_IsAMember(aid, cps)
437 for (i = 0; i < cps->prlist_len; i++)
438 if (cps->prlist_val[i] == aid)
445 AddToList(pflist, elem)
446 struct freeListEntry **pflist;
447 struct freeListEntry *elem;
449 /* Adds elem to the freelist flist; returns 0 */
450 #ifdef AFS_PTHREAD_ENV
451 assert(pthread_mutex_lock(&acl_list_mutex) == 0);
452 #endif /* AFS_PTHREAD_ENV */
453 elem->next = *pflist;
455 #ifdef AFS_PTHREAD_ENV
456 assert(pthread_mutex_unlock(&acl_list_mutex) == 0);
457 #endif /* AFS_PTHREAD_ENV */
462 GetFromList(pflist, elem, minsize)
463 struct freeListEntry **pflist;
464 struct freeListEntry **elem;
467 /* Looks for an element whose body is at least minsize bytes in the freelist flist. If found, unlinks it, puts its address in elem, and returns 0, else returns -1. A trivial first-fit algorithm is used. */
469 struct freeListEntry *y, *z;
471 #ifdef AFS_PTHREAD_ENV
472 assert(pthread_mutex_lock(&acl_list_mutex) == 0);
473 #endif /* AFS_PTHREAD_ENV */
474 if (*pflist == NULL) {
475 #ifdef AFS_PTHREAD_ENV
476 assert(pthread_mutex_unlock(&acl_list_mutex) == 0);
477 #endif /* AFS_PTHREAD_ENV */
480 for (y = *pflist, z = NULL; y != NULL; z = y, y = y->next) {
481 if (y->size >= minsize) {
483 if (z == NULL) { /* pulling off the head */
485 #ifdef AFS_PTHREAD_ENV
486 assert(pthread_mutex_unlock(&acl_list_mutex) == 0);
487 #endif /* AFS_PTHREAD_ENV */
491 #ifdef AFS_PTHREAD_ENV
492 assert(pthread_mutex_unlock(&acl_list_mutex) == 0);
493 #endif /* AFS_PTHREAD_ENV */
497 #ifdef AFS_PTHREAD_ENV
498 assert(pthread_mutex_unlock(&acl_list_mutex) == 0);
499 #endif /* AFS_PTHREAD_ENV */