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>
17 #include <sys/types.h>
21 #include <netinet/in.h>
28 #include <afs/cellconfig.h>
29 #include <afs/afsutil.h>
30 #include <afs/com_err.h>
37 # include "afs_usrops.h"
40 struct ubik_client *pruclient = 0;
41 static afs_int32 lastLevel; /* security level pruclient, if any */
43 static char *whoami = "libprot";
46 #define ID_HASH_SIZE 1024
47 #define ID_STACK_SIZE 1024
50 * Hash table chain of user and group ids.
58 * Hash table of user and group ids.
61 afs_uint32 userEntries; /**< number of user id entries hashed */
62 afs_uint32 groupEntries; /**< number of group id entries hashed */
63 struct idchain *hash[ID_HASH_SIZE];
67 * Allocate a new id hash table.
70 AllocateIdHash(struct idhash **aidhash)
72 struct idhash *idhash;
74 idhash = (struct idhash *)malloc(sizeof(struct idhash));
78 memset((void *)idhash, 0, sizeof(struct idhash));
87 FreeIdHash(struct idhash *idhash)
90 struct idchain *chain;
93 for (index = 0; index < ID_HASH_SIZE; index++) {
94 for (chain = idhash->hash[index]; chain; chain = next) {
103 * Indicate if group/user id is already hashed, and
106 * @returns whether id is present
107 * @retval >0 id is already present in the hash
108 * @retval 0 id was not found and was inserted into the hash
109 * @retval <0 error encountered
112 FindId(struct idhash *idhash, afs_int32 id)
115 struct idchain *chain;
116 struct idchain *newChain;
118 index = abs(id) % ID_HASH_SIZE;
119 for (chain = idhash->hash[index]; chain; chain = chain->next) {
120 if (chain->id == id) {
125 /* Insert this id but return not found. */
126 newChain = (struct idchain *)malloc(sizeof(struct idchain));
131 newChain->next = idhash->hash[index];
132 idhash->hash[index] = newChain;
134 idhash->groupEntries++;
136 idhash->userEntries++;
143 * Create an idlist from the ids in the hash.
146 CreateIdList(struct idhash *idhash, idlist * alist, afs_int32 select)
148 struct idchain *chain;
149 afs_int32 entries = 0;
153 if (select & PRGROUPS) {
154 entries += idhash->groupEntries;
156 if (select & PRUSERS) {
157 entries += idhash->userEntries;
160 alist->idlist_len = entries;
161 alist->idlist_val = (afs_int32 *) malloc(sizeof(afs_int32) * entries);
162 if (!alist->idlist_val) {
166 for (i = 0, index = 0; index < ID_HASH_SIZE; index++) {
167 for (chain = idhash->hash[index]; chain; chain = chain->next) {
169 if (select & PRGROUPS) {
170 alist->idlist_val[i++] = chain->id;
173 if (select & PRUSERS) {
174 alist->idlist_val[i++] = chain->id;
183 pr_Initialize(IN afs_int32 secLevel, IN const char *confDir, IN char *cell)
186 struct rx_connection *serverconns[MAXSERVERS];
187 struct rx_securityClass *sc = NULL;
188 static struct afsconf_dir *tdir = (struct afsconf_dir *)NULL; /* only do this once */
189 static char tconfDir[100] = "";
190 static char tcell[64] = "";
193 static struct afsconf_cell info;
195 #if !defined(UKERNEL)
198 afs_int32 gottdir = 0;
199 afs_int32 refresh = 0;
201 initialize_PT_error_table();
202 initialize_RXK_error_table();
203 initialize_ACFG_error_table();
204 initialize_KTC_error_table();
208 cell = afs_LclCellName;
210 #else /* defined(UKERNEL) */
213 tdir = afsconf_Open(confDir);
215 if (confDir && strcmp(confDir, ""))
217 "%s: Could not open configuration directory: %s.\n",
221 "%s: No configuration directory specified.\n",
227 code = afsconf_GetLocalCell(tdir, cellstr, sizeof(cellstr));
230 "libprot: Could not get local cell. [%d]\n", code);
235 #endif /* defined(UKERNEL) */
237 if (tdir == NULL || strcmp(confDir, tconfDir) || strcmp(cell, tcell)) {
239 * force re-evaluation. we either don't have an afsconf_dir,
240 * the directory has changed or the cell has changed.
242 if (tdir && !gottdir) {
244 tdir = (struct afsconf_dir *)NULL;
246 pruclient = (struct ubik_client *)NULL;
251 strncpy(tconfDir, confDir, sizeof(tconfDir));
252 strncpy(tcell, cell, sizeof(tcell));
256 #else /* defined(UKERNEL) */
258 tdir = afsconf_Open(confDir);
260 if (confDir && strcmp(confDir, ""))
262 "libprot: Could not open configuration directory: %s.\n",
266 "libprot: No configuration directory specified.\n");
269 #endif /* defined(UKERNEL) */
271 code = afsconf_GetCellInfo(tdir, cell, "afsprot", &info);
273 fprintf(stderr, "libprot: Could not locate cell %s in %s/%s\n",
274 cell, confDir, AFSDIR_CELLSERVDB_FILE);
279 /* If we already have a client and it is at the security level we
280 * want, don't get a new one. Unless the security level is 2 in
281 * which case we will get one (and re-read the key file).
283 if (pruclient && (lastLevel == secLevel) && (secLevel != 2)) {
289 fprintf(stderr, "libprot: Could not initialize rx.\n");
293 /* Most callers use secLevel==1, however, the fileserver uses secLevel==2
294 * to force use of the KeyFile. secLevel == 0 implies -noauth was
297 /* If secLevel is two assume we're on a file server and use
298 * ClientAuthSecure if possible. */
299 code = afsconf_ClientAuthSecure(tdir, &sc, &scIndex);
301 afs_com_err(whoami, code, "(calling client secure)\n");
302 } else if (secLevel > 0) {
305 secFlags |= AFSCONF_SECOPTS_ALWAYSENCRYPT;
307 code = afsconf_ClientAuthToken(&info, secFlags, &sc, &scIndex, NULL);
309 afs_com_err(whoami, code, "(getting token)");
316 sc = rxnull_NewClientSecurityObject();
317 scIndex = RX_SECIDX_NULL;
320 if ((scIndex == RX_SECIDX_NULL) && (secLevel != 0))
322 "%s: Could not get afs tokens, running unauthenticated\n",
325 memset(serverconns, 0, sizeof(serverconns)); /* terminate list!!! */
326 for (i = 0; i < info.numServers; i++)
328 rx_NewConnection(info.hostAddr[i].sin_addr.s_addr,
329 info.hostAddr[i].sin_port, PRSRV, sc,
332 code = ubik_ClientInit(serverconns, &pruclient);
334 afs_com_err(whoami, code, "ubik client init failed.");
339 code = rxs_Release(sc);
349 code = ubik_ClientDestroy(pruclient);
358 pr_CreateUser(char name[PR_MAXNAMELEN], afs_int32 *id)
364 code = ubik_PR_INewEntry(pruclient, 0, name, *id, 0);
367 code = ubik_PR_NewEntry(pruclient, 0, name, 0, 0, id);
374 pr_CreateGroup(char name[PR_MAXNAMELEN], char owner[PR_MAXNAMELEN], afs_int32 *id)
382 code = pr_SNameToId(owner, &oid);
385 if (oid == ANONYMOUSID)
390 code = ubik_PR_INewEntry(pruclient, 0, name, *id, oid);
393 code = ubik_PR_NewEntry(pruclient, 0, name, flags, oid, id);
399 pr_Delete(char *name)
405 code = pr_SNameToId(name, &id);
408 if (id == ANONYMOUSID)
410 code = ubik_PR_Delete(pruclient, 0, id);
415 pr_DeleteByID(afs_int32 id)
419 code = ubik_PR_Delete(pruclient, 0, id);
424 pr_AddToGroup(char *user, char *group)
430 lnames.namelist_len = 2;
431 lnames.namelist_val = malloc(2 * PR_MAXNAMELEN);
432 strncpy(lnames.namelist_val[0], user, PR_MAXNAMELEN);
433 strncpy(lnames.namelist_val[1], group, PR_MAXNAMELEN);
436 code = pr_NameToId(&lnames, &lids);
439 /* if here, still could be missing an entry */
440 if (lids.idlist_val[0] == ANONYMOUSID
441 || lids.idlist_val[1] == ANONYMOUSID) {
446 ubik_PR_AddToGroup(pruclient, 0, lids.idlist_val[0],
449 if (lnames.namelist_val)
450 free(lnames.namelist_val);
452 xdr_free((xdrproc_t) xdr_idlist, &lids);
457 pr_RemoveUserFromGroup(char *user, char *group)
463 lnames.namelist_len = 2;
464 lnames.namelist_val = malloc(2 * PR_MAXNAMELEN);
465 strncpy(lnames.namelist_val[0], user, PR_MAXNAMELEN);
466 strncpy(lnames.namelist_val[1], group, PR_MAXNAMELEN);
469 code = pr_NameToId(&lnames, &lids);
473 if (lids.idlist_val[0] == ANONYMOUSID
474 || lids.idlist_val[1] == ANONYMOUSID) {
479 ubik_PR_RemoveFromGroup(pruclient, 0, lids.idlist_val[0],
482 if (lnames.namelist_val)
483 free(lnames.namelist_val);
485 xdr_free((xdrproc_t) xdr_idlist, &lids);
491 pr_NameToId(namelist *names, idlist *ids)
496 for (i = 0; i < names->namelist_len; i++)
497 stolower(names->namelist_val[i]);
498 code = ubik_PR_NameToID(pruclient, 0, names, ids);
503 pr_SNameToId(char name[PR_MAXNAMELEN], afs_int32 *id)
511 lnames.namelist_len = 1;
512 lnames.namelist_val = malloc(PR_MAXNAMELEN);
514 strncpy(lnames.namelist_val[0], name, PR_MAXNAMELEN);
515 code = ubik_PR_NameToID(pruclient, 0, &lnames, &lids);
516 if (lids.idlist_val) {
517 *id = *lids.idlist_val;
518 xdr_free((xdrproc_t) xdr_idlist, &lids);
520 if (lnames.namelist_val)
521 free(lnames.namelist_val);
526 pr_IdToName(idlist *ids, namelist *names)
530 code = ubik_PR_IDToName(pruclient, 0, ids, names);
535 pr_SIdToName(afs_int32 id, char name[PR_MAXNAMELEN])
542 lids.idlist_val = malloc(sizeof(afs_int32));
543 *lids.idlist_val = id;
544 lnames.namelist_len = 0;
545 lnames.namelist_val = 0;
546 code = ubik_PR_IDToName(pruclient, 0, &lids, &lnames);
548 if (lnames.namelist_val)
549 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 = (afs_int32 *) 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 */
751 (afs_int32 *) realloc(stack,
752 maxstack * sizeof(afs_int32));
755 xdr_free((xdrproc_t) xdr_prlist, &alist);
760 stack[n++] = id; /* push group id */
763 xdr_free((xdrproc_t) xdr_prlist, &alist);
766 code = CreateIdList(members, &lids, (aid < 0 ? PRUSERS : PRGROUPS));
770 code = pr_IdToName(&lids, lnames);
772 free(lids.idlist_val);
783 pr_ListEntry(afs_int32 id, struct prcheckentry *aentry)
787 code = ubik_PR_ListEntry(pruclient, 0, id, aentry);
792 pr_ListEntries(int flag, afs_int32 startindex, afs_int32 *nentries, struct prlistentries **entries, afs_int32 *nextstartindex)
795 prentries bulkentries;
799 *nextstartindex = -1;
800 bulkentries.prentries_val = 0;
801 bulkentries.prentries_len = 0;
804 ubik_PR_ListEntries(pruclient, 0, flag, startindex,
805 &bulkentries, nextstartindex);
806 *nentries = bulkentries.prentries_len;
807 *entries = bulkentries.prentries_val;
812 pr_CheckEntryByName(char *name, afs_int32 *id, char *owner, char *creator)
814 /* struct prcheckentry returns other things, which aren't useful to show at this time. */
816 struct prcheckentry aentry;
818 code = pr_SNameToId(name, id);
821 if (*id == ANONYMOUSID)
823 code = ubik_PR_ListEntry(pruclient, 0, *id, &aentry);
826 /* this should be done in one RPC, but I'm lazy. */
827 code = pr_SIdToName(aentry.owner, owner);
830 code = pr_SIdToName(aentry.creator, creator);
837 pr_CheckEntryById(char *name, afs_int32 id, char *owner, char *creator)
839 /* struct prcheckentry returns other things, which aren't useful to show at this time. */
841 struct prcheckentry aentry;
843 code = pr_SIdToName(id, name);
846 if (id == ANONYMOUSID)
848 code = ubik_PR_ListEntry(pruclient, 0, id, &aentry);
851 /* this should be done in one RPC, but I'm lazy. */
852 code = pr_SIdToName(aentry.owner, owner);
855 code = pr_SIdToName(aentry.creator, creator);
862 pr_ChangeEntry(char *oldname, char *newname, afs_int32 *newid, char *newowner)
868 code = pr_SNameToId(oldname, &id);
871 if (id == ANONYMOUSID)
873 if (newowner && *newowner) {
874 code = pr_SNameToId(newowner, &oid);
877 if (oid == ANONYMOUSID)
881 code = ubik_PR_ChangeEntry(pruclient, 0, id, newname, oid, *newid);
883 code = ubik_PR_ChangeEntry(pruclient, 0, id, newname, oid, 0);
888 pr_IsAMemberOf(char *uname, char *gname, afs_int32 *flag)
896 lnames.namelist_len = 2;
897 lnames.namelist_val = malloc(2 * PR_MAXNAMELEN);
898 strncpy(lnames.namelist_val[0], uname, PR_MAXNAMELEN);
899 strncpy(lnames.namelist_val[1], gname, PR_MAXNAMELEN);
902 code = pr_NameToId(&lnames, &lids);
904 if (lnames.namelist_val)
905 free(lnames.namelist_val);
906 xdr_free((xdrproc_t) xdr_idlist, &lids);
910 ubik_PR_IsAMemberOf(pruclient, 0, lids.idlist_val[0],
911 lids.idlist_val[1], flag);
912 if (lnames.namelist_val)
913 free(lnames.namelist_val);
914 xdr_free((xdrproc_t) xdr_idlist, &lids);
919 pr_ListMaxUserId(afs_int32 *mid)
923 code = ubik_PR_ListMax(pruclient, 0, mid, &gid);
928 pr_SetMaxUserId(afs_int32 mid)
932 code = ubik_PR_SetMax(pruclient, 0, mid, flag);
937 pr_ListMaxGroupId(afs_int32 *mid)
941 code = ubik_PR_ListMax(pruclient, 0, &id, mid);
946 pr_SetMaxGroupId(afs_int32 mid)
952 code = ubik_PR_SetMax(pruclient, 0, mid, flag);
957 pr_SetFieldsEntry(afs_int32 id, afs_int32 mask, afs_int32 flags, afs_int32 ngroups, afs_int32 nusers)
962 ubik_PR_SetFieldsEntry(pruclient, 0, id, mask, flags, ngroups,
968 pr_ListSuperGroups(afs_int32 gid, namelist * lnames)
975 alist.prlist_len = 0;
976 alist.prlist_val = 0;
977 code = ubik_PR_ListSuperGroups(pruclient, 0, gid, &alist, &over);
981 fprintf(stderr, "supergroup list for id %d exceeds display limit\n",
984 lids = (idlist *) & alist;
985 code = pr_IdToName(lids, lnames);
987 xdr_free((xdrproc_t) xdr_prlist, &alist);