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
10 #include <afsconfig.h>
11 #include <afs/param.h>
20 #include <afs/cellconfig.h>
21 #include <afs/afsutil.h>
22 #include <afs/com_err.h>
29 # include "afs_usrops.h"
32 struct ubik_client *pruclient = 0;
33 static afs_int32 lastLevel; /* security level pruclient, if any */
35 static char *whoami = "libprot";
38 #define ID_HASH_SIZE 1024
39 #define ID_STACK_SIZE 1024
42 * Hash table chain of user and group ids.
50 * Hash table of user and group ids.
53 afs_uint32 userEntries; /**< number of user id entries hashed */
54 afs_uint32 groupEntries; /**< number of group id entries hashed */
55 struct idchain *hash[ID_HASH_SIZE];
59 * Allocate a new id hash table.
62 AllocateIdHash(struct idhash **aidhash)
64 struct idhash *idhash;
66 idhash = calloc(1, sizeof(struct idhash));
78 FreeIdHash(struct idhash *idhash)
81 struct idchain *chain;
84 for (index = 0; index < ID_HASH_SIZE; index++) {
85 for (chain = idhash->hash[index]; chain; chain = next) {
94 * Indicate if group/user id is already hashed, and
97 * @returns whether id is present
98 * @retval >0 id is already present in the hash
99 * @retval 0 id was not found and was inserted into the hash
100 * @retval <0 error encountered
103 FindId(struct idhash *idhash, afs_int32 id)
106 struct idchain *chain;
107 struct idchain *newChain;
109 index = abs(id) % ID_HASH_SIZE;
110 for (chain = idhash->hash[index]; chain; chain = chain->next) {
111 if (chain->id == id) {
116 /* Insert this id but return not found. */
117 newChain = malloc(sizeof(struct idchain));
122 newChain->next = idhash->hash[index];
123 idhash->hash[index] = newChain;
125 idhash->groupEntries++;
127 idhash->userEntries++;
134 * Create an idlist from the ids in the hash.
137 CreateIdList(struct idhash *idhash, idlist * alist, afs_int32 select)
139 struct idchain *chain;
140 afs_int32 entries = 0;
144 if (select & PRGROUPS) {
145 entries += idhash->groupEntries;
147 if (select & PRUSERS) {
148 entries += idhash->userEntries;
151 alist->idlist_len = 0;
152 alist->idlist_val = NULL;
156 alist->idlist_len = entries;
157 alist->idlist_val = malloc(sizeof(afs_int32) * entries);
158 if (!alist->idlist_val) {
162 for (i = 0, index = 0; index < ID_HASH_SIZE; index++) {
163 for (chain = idhash->hash[index]; chain; chain = chain->next) {
165 if (select & PRGROUPS) {
166 alist->idlist_val[i++] = chain->id;
169 if (select & PRUSERS) {
170 alist->idlist_val[i++] = chain->id;
179 pr_Initialize(IN afs_int32 secLevel, IN const char *confDir, IN char *cell)
182 struct rx_connection *serverconns[MAXSERVERS];
183 struct rx_securityClass *sc = NULL;
184 static struct afsconf_dir *tdir = NULL; /* only do this once */
185 static char tconfDir[100] = "";
186 static char tcell[64] = "";
189 static struct afsconf_cell info;
191 #if !defined(UKERNEL)
194 afs_int32 gottdir = 0;
195 afs_int32 refresh = 0;
197 initialize_PT_error_table();
198 initialize_RXK_error_table();
199 initialize_ACFG_error_table();
200 initialize_KTC_error_table();
204 cell = afs_LclCellName;
206 #else /* defined(UKERNEL) */
209 tdir = afsconf_Open(confDir);
211 if (confDir && strcmp(confDir, ""))
213 "%s: Could not open configuration directory: %s.\n",
217 "%s: No configuration directory specified.\n",
223 code = afsconf_GetLocalCell(tdir, cellstr, sizeof(cellstr));
226 "libprot: Could not get local cell. [%d]\n", code);
231 #endif /* defined(UKERNEL) */
233 if (tdir == NULL || strcmp(confDir, tconfDir) || strcmp(cell, tcell)) {
235 * force re-evaluation. we either don't have an afsconf_dir,
236 * the directory has changed or the cell has changed.
238 if (tdir && !gottdir) {
247 strncpy(tconfDir, confDir, sizeof(tconfDir));
248 strncpy(tcell, cell, sizeof(tcell));
252 #else /* defined(UKERNEL) */
254 tdir = afsconf_Open(confDir);
256 if (confDir && strcmp(confDir, ""))
258 "libprot: Could not open configuration directory: %s.\n",
262 "libprot: No configuration directory specified.\n");
265 #endif /* defined(UKERNEL) */
267 code = afsconf_GetCellInfo(tdir, cell, "afsprot", &info);
269 fprintf(stderr, "libprot: Could not locate cell %s in %s/%s\n",
270 cell, confDir, AFSDIR_CELLSERVDB_FILE);
275 /* If we already have a client and it is at the security level we
276 * want, don't get a new one. Unless the security level is 2 in
277 * which case we will get one (and re-read the key file).
279 if (pruclient && (lastLevel == secLevel) && (secLevel != 2)) {
285 fprintf(stderr, "libprot: Could not initialize rx.\n");
289 /* Most callers use secLevel==1, however, the fileserver uses secLevel==2
290 * to force use of the KeyFile. secLevel == 0 implies -noauth was
293 /* If secLevel is two assume we're on a file server and use
294 * ClientAuthSecure if possible. */
295 code = afsconf_ClientAuthSecure(tdir, &sc, &scIndex);
297 afs_com_err(whoami, code, "(calling client secure)\n");
298 } else if (secLevel > 0) {
301 secFlags |= AFSCONF_SECOPTS_ALWAYSENCRYPT;
303 code = afsconf_ClientAuthToken(&info, secFlags, &sc, &scIndex, NULL);
305 afs_com_err(whoami, code, "(getting token)");
312 sc = rxnull_NewClientSecurityObject();
313 scIndex = RX_SECIDX_NULL;
316 if ((scIndex == RX_SECIDX_NULL) && (secLevel != 0))
318 "%s: Could not get afs tokens, running unauthenticated\n",
321 memset(serverconns, 0, sizeof(serverconns)); /* terminate list!!! */
322 for (i = 0; i < info.numServers; i++)
324 rx_NewConnection(info.hostAddr[i].sin_addr.s_addr,
325 info.hostAddr[i].sin_port, PRSRV, sc,
328 code = ubik_ClientInit(serverconns, &pruclient);
330 afs_com_err(whoami, code, "ubik client init failed.");
335 code = rxs_Release(sc);
345 code = ubik_ClientDestroy(pruclient);
354 pr_CreateUser(prname name, afs_int32 *id)
360 code = ubik_PR_INewEntry(pruclient, 0, name, *id, 0);
363 code = ubik_PR_NewEntry(pruclient, 0, name, 0, 0, id);
370 pr_CreateGroup(prname name, prname owner, afs_int32 *id)
378 code = pr_SNameToId(owner, &oid);
381 if (oid == ANONYMOUSID)
386 code = ubik_PR_INewEntry(pruclient, 0, name, *id, oid);
389 code = ubik_PR_NewEntry(pruclient, 0, name, flags, oid, id);
395 pr_Delete(char *name)
401 code = pr_SNameToId(name, &id);
404 if (id == ANONYMOUSID)
406 code = ubik_PR_Delete(pruclient, 0, id);
411 pr_DeleteByID(afs_int32 id)
415 code = ubik_PR_Delete(pruclient, 0, id);
420 pr_AddToGroup(char *user, char *group)
426 lnames.namelist_len = 2;
427 lnames.namelist_val = malloc(2 * PR_MAXNAMELEN);
428 strncpy(lnames.namelist_val[0], user, PR_MAXNAMELEN);
429 strncpy(lnames.namelist_val[1], group, PR_MAXNAMELEN);
432 code = pr_NameToId(&lnames, &lids);
435 /* if here, still could be missing an entry */
436 if (lids.idlist_val[0] == ANONYMOUSID
437 || lids.idlist_val[1] == ANONYMOUSID) {
442 ubik_PR_AddToGroup(pruclient, 0, lids.idlist_val[0],
445 if (lnames.namelist_val)
446 free(lnames.namelist_val);
448 xdr_free((xdrproc_t) xdr_idlist, &lids);
453 pr_RemoveUserFromGroup(char *user, char *group)
459 lnames.namelist_len = 2;
460 lnames.namelist_val = malloc(2 * PR_MAXNAMELEN);
461 strncpy(lnames.namelist_val[0], user, PR_MAXNAMELEN);
462 strncpy(lnames.namelist_val[1], group, PR_MAXNAMELEN);
465 code = pr_NameToId(&lnames, &lids);
469 if (lids.idlist_val[0] == ANONYMOUSID
470 || lids.idlist_val[1] == ANONYMOUSID) {
475 ubik_PR_RemoveFromGroup(pruclient, 0, lids.idlist_val[0],
478 if (lnames.namelist_val)
479 free(lnames.namelist_val);
481 xdr_free((xdrproc_t) xdr_idlist, &lids);
487 pr_NameToId(namelist *names, idlist *ids)
492 for (i = 0; i < names->namelist_len; i++)
493 stolower(names->namelist_val[i]);
494 code = ubik_PR_NameToID(pruclient, 0, names, ids);
499 pr_SNameToId(prname name, afs_int32 *id)
507 lnames.namelist_len = 1;
508 lnames.namelist_val = malloc(PR_MAXNAMELEN);
510 strncpy(lnames.namelist_val[0], name, PR_MAXNAMELEN);
511 code = ubik_PR_NameToID(pruclient, 0, &lnames, &lids);
512 if (lids.idlist_val) {
513 *id = *lids.idlist_val;
514 xdr_free((xdrproc_t) xdr_idlist, &lids);
515 } else if (code == 0) {
518 if (lnames.namelist_val)
519 free(lnames.namelist_val);
524 pr_IdToName(idlist *ids, namelist *names)
528 code = ubik_PR_IDToName(pruclient, 0, ids, names);
533 pr_SIdToName(afs_int32 id, prname name)
540 lids.idlist_val = malloc(sizeof(afs_int32));
541 *lids.idlist_val = id;
542 lnames.namelist_len = 0;
543 lnames.namelist_val = 0;
544 code = ubik_PR_IDToName(pruclient, 0, &lids, &lnames);
546 if (lnames.namelist_val)
547 strncpy(name, lnames.namelist_val[0], PR_MAXNAMELEN);
552 free(lids.idlist_val);
554 xdr_free((xdrproc_t) xdr_namelist, &lnames);
560 pr_GetCPS(afs_int32 id, prlist *CPS)
566 code = ubik_PR_GetCPS(pruclient, 0, id, CPS, &over);
567 if (code != PRSUCCESS)
570 /* do something about this, probably make a new call */
571 /* don't forget there's a hard limit in the interface */
572 fprintf(stderr, "membership list for id %d exceeds display limit\n",
579 pr_GetCPS2(afs_int32 id, afs_uint32 host, prlist *CPS)
585 code = ubik_PR_GetCPS2(pruclient, 0, id, host, CPS, &over);
586 if (code != PRSUCCESS)
589 /* do something about this, probably make a new call */
590 /* don't forget there's a hard limit in the interface */
591 fprintf(stderr, "membership list for id %d exceeds display limit\n",
598 pr_GetHostCPS(afs_uint32 host, prlist *CPS)
604 code = ubik_PR_GetHostCPS(pruclient, 0, host, CPS, &over);
605 if (code != PRSUCCESS)
608 /* do something about this, probably make a new call */
609 /* don't forget there's a hard limit in the interface */
611 "membership list for host id %d exceeds display limit\n",
618 pr_ListMembers(char *group, namelist *lnames)
623 code = pr_SNameToId(group, &gid);
626 if (gid == ANONYMOUSID)
628 code = pr_IDListMembers(gid, lnames);
633 pr_ListOwned(afs_int32 oid, namelist *lnames, afs_int32 *moreP)
639 alist.prlist_len = 0;
640 alist.prlist_val = 0;
641 code = ubik_PR_ListOwned(pruclient, 0, oid, &alist, moreP);
645 /* Remain backwards compatible when moreP was a T/F bit */
646 fprintf(stderr, "membership list for id %d exceeds display limit\n",
650 lids = (idlist *) &alist;
651 code = pr_IdToName(lids, lnames);
653 xdr_free((xdrproc_t) xdr_prlist, &alist);
662 pr_IDListMembers(afs_int32 gid, namelist *lnames)
669 alist.prlist_len = 0;
670 alist.prlist_val = 0;
671 code = ubik_PR_ListElements(pruclient, 0, gid, &alist, &over);
675 fprintf(stderr, "membership list for id %d exceeds display limit\n",
678 lids = (idlist *) &alist;
679 code = pr_IdToName(lids, lnames);
681 xdr_free((xdrproc_t) xdr_prlist, &alist);
689 pr_IDListExpandedMembers(afs_int32 aid, namelist * lnames)
696 struct idhash *members = NULL;
697 afs_int32 *stack = NULL;
698 afs_int32 maxstack = ID_STACK_SIZE;
699 int n = 0; /* number of ids stacked */
703 code = AllocateIdHash(&members);
707 stack = malloc(sizeof(afs_int32) * maxstack);
715 gid = stack[--n]; /* pop next group id */
716 alist.prlist_len = 0;
717 alist.prlist_val = NULL;
718 if (firstpass || aid < 0) {
720 code = ubik_PR_ListElements(pruclient, 0, gid, &alist, &over);
722 code = ubik_PR_ListSuperGroups(pruclient, 0, gid, &alist, &over);
723 if (code == RXGEN_OPCODE) {
724 alist.prlist_len = 0;
725 alist.prlist_val = NULL;
726 code = 0; /* server does not support supergroups. */
733 "membership list for id %d exceeds display limit\n", gid);
735 for (i = 0; i < alist.prlist_len; i++) {
739 id = alist.prlist_val[i];
740 found = FindId(members, id);
743 xdr_free((xdrproc_t) xdr_prlist, &alist);
746 if (found == 0 && id < 0) {
747 if (n == maxstack) { /* need more stack space */
750 tmp = realloc(stack, maxstack * sizeof(afs_int32));
753 xdr_free((xdrproc_t) xdr_prlist, &alist);
758 stack[n++] = id; /* push group id */
761 xdr_free((xdrproc_t) xdr_prlist, &alist);
764 code = CreateIdList(members, &lids, (aid < 0 ? PRUSERS : PRGROUPS));
767 } else if (lids.idlist_len == 0) {
768 /* Avoid the RPC when there's nothing to look up. */
769 lnames->namelist_len = 0;
770 lnames->namelist_val = NULL;
773 code = pr_IdToName(&lids, lnames);
774 free(lids.idlist_val);
785 pr_ListEntry(afs_int32 id, struct prcheckentry *aentry)
789 code = ubik_PR_ListEntry(pruclient, 0, id, aentry);
794 pr_ListEntries(int flag, afs_int32 startindex, afs_int32 *nentries, struct prlistentries **entries, afs_int32 *nextstartindex)
797 prentries bulkentries;
801 *nextstartindex = -1;
802 bulkentries.prentries_val = 0;
803 bulkentries.prentries_len = 0;
806 ubik_PR_ListEntries(pruclient, 0, flag, startindex,
807 &bulkentries, nextstartindex);
808 *nentries = bulkentries.prentries_len;
809 *entries = bulkentries.prentries_val;
814 pr_CheckEntryByName(char *name, afs_int32 *id, char *owner, char *creator)
816 /* struct prcheckentry returns other things, which aren't useful to show at this time. */
818 struct prcheckentry aentry;
820 code = pr_SNameToId(name, id);
823 if (*id == ANONYMOUSID)
825 code = ubik_PR_ListEntry(pruclient, 0, *id, &aentry);
828 /* this should be done in one RPC, but I'm lazy. */
829 code = pr_SIdToName(aentry.owner, owner);
832 code = pr_SIdToName(aentry.creator, creator);
839 pr_CheckEntryById(char *name, afs_int32 id, char *owner, char *creator)
841 /* struct prcheckentry returns other things, which aren't useful to show at this time. */
843 struct prcheckentry aentry;
845 code = pr_SIdToName(id, name);
848 if (id == ANONYMOUSID)
850 code = ubik_PR_ListEntry(pruclient, 0, id, &aentry);
853 /* this should be done in one RPC, but I'm lazy. */
854 code = pr_SIdToName(aentry.owner, owner);
857 code = pr_SIdToName(aentry.creator, creator);
864 pr_ChangeEntry(char *oldname, char *newname, afs_int32 *newid, char *newowner)
870 code = pr_SNameToId(oldname, &id);
873 if (id == ANONYMOUSID)
875 if (newowner && *newowner) {
876 code = pr_SNameToId(newowner, &oid);
879 if (oid == ANONYMOUSID)
883 code = ubik_PR_ChangeEntry(pruclient, 0, id, newname, oid, *newid);
885 code = ubik_PR_ChangeEntry(pruclient, 0, id, newname, oid, 0);
890 pr_IsAMemberOf(char *uname, char *gname, afs_int32 *flag)
898 lnames.namelist_len = 2;
899 lnames.namelist_val = malloc(2 * PR_MAXNAMELEN);
900 strncpy(lnames.namelist_val[0], uname, PR_MAXNAMELEN);
901 strncpy(lnames.namelist_val[1], gname, PR_MAXNAMELEN);
904 code = pr_NameToId(&lnames, &lids);
906 if (lnames.namelist_val)
907 free(lnames.namelist_val);
908 xdr_free((xdrproc_t) xdr_idlist, &lids);
912 ubik_PR_IsAMemberOf(pruclient, 0, lids.idlist_val[0],
913 lids.idlist_val[1], flag);
914 if (lnames.namelist_val)
915 free(lnames.namelist_val);
916 xdr_free((xdrproc_t) xdr_idlist, &lids);
921 pr_ListMaxUserId(afs_int32 *mid)
925 code = ubik_PR_ListMax(pruclient, 0, mid, &gid);
930 pr_SetMaxUserId(afs_int32 mid)
934 code = ubik_PR_SetMax(pruclient, 0, mid, flag);
939 pr_ListMaxGroupId(afs_int32 *mid)
943 code = ubik_PR_ListMax(pruclient, 0, &id, mid);
948 pr_SetMaxGroupId(afs_int32 mid)
954 code = ubik_PR_SetMax(pruclient, 0, mid, flag);
959 pr_SetFieldsEntry(afs_int32 id, afs_int32 mask, afs_int32 flags, afs_int32 ngroups, afs_int32 nusers)
964 ubik_PR_SetFieldsEntry(pruclient, 0, id, mask, flags, ngroups,
970 pr_ListSuperGroups(afs_int32 gid, namelist * lnames)
977 alist.prlist_len = 0;
978 alist.prlist_val = 0;
979 code = ubik_PR_ListSuperGroups(pruclient, 0, gid, &alist, &over);
983 fprintf(stderr, "supergroup list for id %d exceeds display limit\n",
986 lids = (idlist *) & alist;
987 code = pr_IdToName(lids, lnames);
989 xdr_free((xdrproc_t) xdr_prlist, &alist);