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(int nEntries, struct acl_accessList **acl)
91 /* Creates an access list capable of holding at least nEntries entries.
92 * Returns 0 on success; aborts if we run out of memory. */
95 struct freeListEntry *e;
97 t = sizeof(struct acl_accessList) + (nEntries -
98 1) * sizeof(struct acl_accessEntry);
99 if (GetFromList(&freeList, &e, t) < 0) {
100 e = (struct freeListEntry *)malloc(t + sizeof(int) +
101 sizeof(struct freeListEntry *));
103 perror("acl_NewACL: malloc() failed");
107 *acl = (struct acl_accessList *)(e->body);
109 *acl = (struct acl_accessList *)(e->body);
111 (*acl)->size = t; /* May be less than actual size of storage */
112 (*acl)->version = ACL_ACLVERSION;
113 (*acl)->total = nEntries;
114 (*acl)->positive = (*acl)->negative = 0;
120 acl_FreeACL(struct acl_accessList **acl)
122 /* Releases the access list defined by acl. Returns 0 always. */
123 struct freeListEntry *x;
125 x = (struct freeListEntry *)
126 ((char *)*acl - sizeof(struct freeListEntry *) - sizeof(int));
128 return (AddToList(&freeList, x));
132 acl_NewExternalACL(int nEntries, char **r)
134 /* Puts an external acl big enough to hold nEntries in r. Returns 0 on success, aborts if insufficient memory. */
137 struct freeListEntry *e;
139 t = 20 + (nEntries) * (PR_MAXNAMELEN + 20);
140 /* Conservative estimate: enough space in each entry for longest
141 * name plus decimal 2**32 (for largest rights mask) plus some formatting */
143 if (GetFromList(&freeList, &e, t)) {
144 e = (struct freeListEntry *)malloc(t + sizeof(int) +
145 sizeof(struct freeListEntry *));
147 perror("acl_NewExternalACL(): malloc() failed");
154 sprintf(*r, "0\n0\n");
159 acl_FreeExternalACL(char **r)
161 /* Releases the external access list defined by r. Returns 0 always. */
163 struct freeListEntry *x;
165 x = (struct freeListEntry *)
166 ((char *)*r - sizeof(struct freeListEntry *) - sizeof(int));
168 return (AddToList(&freeList, x));
173 acl_Externalize(struct acl_accessList *acl, char **elist)
175 /* 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. */
180 register char *nextc;
184 if (acl->total > ACL_MAXENTRIES)
186 acl_NewExternalACL(acl->total, elist);
189 (afs_int32 *) malloc(ACL_MAXENTRIES * sizeof(afs_int32));
190 memset(lids.idlist_val, 0, ACL_MAXENTRIES * sizeof(afs_int32));
191 lids.idlist_len = acl->total;
192 lnames.namelist_len = 0;
193 lnames.namelist_val = (prname *) 0;
194 sprintf(nextc, "%d\n%d\n", acl->positive, acl->negative);
195 nextc += strlen(nextc);
196 for (i = 0; i < acl->positive; i++)
197 lids.idlist_val[i] = acl->entries[i].id;
199 for (i = acl->total - 1; i >= acl->total - acl->negative; i--, j++)
200 lids.idlist_val[j] = acl->entries[i].id;
201 code = pr_IdToName(&lids, &lnames);
204 free(lids.idlist_val);
205 if (lnames.namelist_val)
206 free(lnames.namelist_val);
209 for (i = 0; i < acl->positive; i++) {
210 sprintf(nextc, "%s", lnames.namelist_val[i]);
211 nextc += strlen(nextc);
212 sprintf(nextc, "\t%d\n", acl->entries[i].rights);
213 nextc += strlen(nextc);
216 for (i = acl->total - 1; i >= acl->total - acl->negative; i--, j++) {
217 sprintf(nextc, "%s", lnames.namelist_val[j]);
218 nextc += strlen(nextc);
219 sprintf(nextc, "\t%d\n", acl->entries[i].rights);
220 nextc += strlen(nextc);
223 free(lids.idlist_val);
224 if (lnames.namelist_val)
225 free(lnames.namelist_val);
231 acl_Internalize(char *elist, struct acl_accessList **acl)
233 /* 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. */
236 register char *nextc;
237 register afs_int32 code;
242 if (sscanf(elist, "%d\n%d\n", &p, &n) != 2)
244 if (p + n > ACL_MAXENTRIES)
246 acl_NewACL(p + n, acl);
247 (*acl)->total = p + n;
248 (*acl)->positive = p;
249 (*acl)->negative = n;
250 if ((*acl)->total == 0) {
251 /* Empty acl entry; simply return success */
254 lnames.namelist_len = (*acl)->total;
255 lnames.namelist_val =
256 (prname *) 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, "%s\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, "%s\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 = pr_NameToId(&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];
309 for (i = (*acl)->total - 1; i >= (*acl)->total - (*acl)->negative; 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];
318 /* sort for easier lookup */
319 qsort(&((*acl)->entries[0]), (*acl)->positive,
320 sizeof(struct acl_accessEntry), CmpPlus);
321 qsort(&((*acl)->entries[(*acl)->total - (*acl)->negative]),
322 (*acl)->negative, sizeof(struct acl_accessEntry), CmpMinus);
323 free(lnames.namelist_val);
325 free(lids.idlist_val);
331 acl_CheckRights(struct acl_accessList *acl, prlist *groups, int *rights)
333 /* Returns the rights given by acl to groups */
335 int temprights; /* positive rights accumulated so far */
336 int negrights; /* negative rights accumulated so far */
337 int a; /* index into next entry in acl */
338 int c; /* index into next entry in CPS */
340 /* more sanity checks */
341 if (acl->total > ACL_MAXENTRIES)
346 return 1; /* 192 is the room in a 256 byte vnode reserved for the ACL */
348 if (acl->total <= 0 || groups->prlist_len <= 0) {
352 if (groups->prlist_val[groups->prlist_len - 1] == SYSADMINID) {
357 /* Each iteration eats up exactly one entry from either acl or groups.
358 * Duplicate Entries in access list ==> accumulated rights are obtained.
359 * Duplicate Entries in groups ==> irrelevant */
362 while ((a < acl->positive) && (c < groups->prlist_len))
363 switch (CmpInt(acl->entries[a].id, groups->prlist_val[c])) {
369 temprights |= acl->entries[a].rights;
378 printf("CmpInt() returned bogus value. Aborting ...\n");
384 while ((c < groups->prlist_len) && (a > acl->total - acl->negative - 1))
385 switch (CmpInt(acl->entries[a].id, groups->prlist_val[c])) {
390 negrights |= acl->entries[a].rights;
397 *rights = temprights & (~negrights);
402 acl_Initialize(char *version)
404 /* I'm sure we need to do some initialization, I'm just not quite sure what yet! */
405 if (strcmp(version, ACL_VERSION) != 0) {
406 fprintf(stderr, "Wrong version of acl package!\n");
407 fprintf(stderr, "This is version %s, file server passed in %s.\n",
408 ACL_VERSION, version);
410 #ifdef AFS_PTHREAD_ENV
411 assert(pthread_mutex_init(&acl_list_mutex, NULL) == 0);
412 #endif /* AFS_PTHREAD_ENV */
417 acl_IsAMember(afs_int32 aid, prlist *cps)
421 for (i = 0; i < cps->prlist_len; i++)
422 if (cps->prlist_val[i] == aid)
429 AddToList(pflist, elem)
430 struct freeListEntry **pflist;
431 struct freeListEntry *elem;
433 /* Adds elem to the freelist flist; returns 0 */
434 #ifdef AFS_PTHREAD_ENV
435 assert(pthread_mutex_lock(&acl_list_mutex) == 0);
436 #endif /* AFS_PTHREAD_ENV */
437 elem->next = *pflist;
439 #ifdef AFS_PTHREAD_ENV
440 assert(pthread_mutex_unlock(&acl_list_mutex) == 0);
441 #endif /* AFS_PTHREAD_ENV */
446 GetFromList(pflist, elem, minsize)
447 struct freeListEntry **pflist;
448 struct freeListEntry **elem;
451 /* 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. */
453 struct freeListEntry *y, *z;
455 #ifdef AFS_PTHREAD_ENV
456 assert(pthread_mutex_lock(&acl_list_mutex) == 0);
457 #endif /* AFS_PTHREAD_ENV */
458 if (*pflist == NULL) {
459 #ifdef AFS_PTHREAD_ENV
460 assert(pthread_mutex_unlock(&acl_list_mutex) == 0);
461 #endif /* AFS_PTHREAD_ENV */
464 for (y = *pflist, z = NULL; y != NULL; z = y, y = y->next) {
465 if (y->size >= minsize) {
467 if (z == NULL) { /* pulling off the head */
469 #ifdef AFS_PTHREAD_ENV
470 assert(pthread_mutex_unlock(&acl_list_mutex) == 0);
471 #endif /* AFS_PTHREAD_ENV */
475 #ifdef AFS_PTHREAD_ENV
476 assert(pthread_mutex_unlock(&acl_list_mutex) == 0);
477 #endif /* AFS_PTHREAD_ENV */
481 #ifdef AFS_PTHREAD_ENV
482 assert(pthread_mutex_unlock(&acl_list_mutex) == 0);
483 #endif /* AFS_PTHREAD_ENV */