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>
23 #include <rx/rxgk_int.h>
29 struct ubik_client *pruclient = 0;
30 static afs_int32 lastLevel; /* security level pruclient, if any */
32 static char *whoami = "libprot";
35 #define ID_HASH_SIZE 1024
36 #define ID_STACK_SIZE 1024
39 * Hash table chain of user and group ids.
47 * Hash table of user and group ids.
50 afs_uint32 userEntries; /**< number of user id entries hashed */
51 afs_uint32 groupEntries; /**< number of group id entries hashed */
52 struct idchain *hash[ID_HASH_SIZE];
56 * Allocate a new id hash table.
59 AllocateIdHash(struct idhash **aidhash)
61 struct idhash *idhash;
63 idhash = calloc(1, sizeof(struct idhash));
75 FreeIdHash(struct idhash *idhash)
78 struct idchain *chain;
81 for (index = 0; index < ID_HASH_SIZE; index++) {
82 for (chain = idhash->hash[index]; chain; chain = next) {
91 * Indicate if group/user id is already hashed, and
94 * @returns whether id is present
95 * @retval >0 id is already present in the hash
96 * @retval 0 id was not found and was inserted into the hash
97 * @retval <0 error encountered
100 FindId(struct idhash *idhash, afs_int32 id)
103 struct idchain *chain;
104 struct idchain *newChain;
106 index = abs(id) % ID_HASH_SIZE;
107 for (chain = idhash->hash[index]; chain; chain = chain->next) {
108 if (chain->id == id) {
113 /* Insert this id but return not found. */
114 newChain = malloc(sizeof(struct idchain));
119 newChain->next = idhash->hash[index];
120 idhash->hash[index] = newChain;
122 idhash->groupEntries++;
124 idhash->userEntries++;
131 * Create an idlist from the ids in the hash.
134 CreateIdList(struct idhash *idhash, idlist * alist, afs_int32 select)
136 struct idchain *chain;
137 afs_int32 entries = 0;
141 if (select & PRGROUPS) {
142 entries += idhash->groupEntries;
144 if (select & PRUSERS) {
145 entries += idhash->userEntries;
148 alist->idlist_len = 0;
149 alist->idlist_val = NULL;
153 alist->idlist_len = entries;
154 alist->idlist_val = malloc(sizeof(afs_int32) * entries);
155 if (!alist->idlist_val) {
159 for (i = 0, index = 0; index < ID_HASH_SIZE; index++) {
160 for (chain = idhash->hash[index]; chain; chain = chain->next) {
162 if (select & PRGROUPS) {
163 alist->idlist_val[i++] = chain->id;
166 if (select & PRUSERS) {
167 alist->idlist_val[i++] = chain->id;
176 pr_Initialize(IN afs_int32 secLevel, IN const char *confDir, IN char *cell)
178 return pr_Initialize2(secLevel, confDir, cell, RXGK_LEVEL_BOGUS);
182 pr_Initialize2(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 = NULL; /* only do this once */
189 static char tconfDir[100] = "";
190 static char tcell[64] = "";
193 static struct afsconf_cell info;
196 afs_int32 gottdir = 0;
197 afs_int32 refresh = 0;
200 initialize_PT_error_table();
201 initialize_RXK_error_table();
202 initialize_ACFG_error_table();
203 initialize_KTC_error_table();
207 tdir = afsconf_Open(confDir);
209 if (confDir && strcmp(confDir, ""))
211 "%s: Could not open configuration directory: %s.\n",
215 "%s: No configuration directory specified.\n",
221 code = afsconf_GetLocalCell(tdir, cellstr, sizeof(cellstr));
224 "libprot: Could not get local cell. [%d]\n", code);
230 if (tdir == NULL || strcmp(confDir, tconfDir) || strcmp(cell, tcell)) {
232 * force re-evaluation. we either don't have an afsconf_dir,
233 * the directory has changed or the cell has changed.
235 if (tdir && !gottdir) {
244 strncpy(tconfDir, confDir, sizeof(tconfDir));
245 strncpy(tcell, cell, sizeof(tcell));
248 tdir = afsconf_Open(confDir);
250 if (confDir && strcmp(confDir, ""))
252 "libprot: Could not open configuration directory: %s.\n",
256 "libprot: No configuration directory specified.\n");
260 code = afsconf_GetCellInfo(tdir, cell, "afsprot", &info);
262 fprintf(stderr, "libprot: Could not locate cell %s in %s\n",
263 cell, tdir->cellservDB);
268 /* If we already have a client and it is at the security level we
269 * want, don't get a new one. Unless the security level is 2 in
270 * which case we will get one (and re-read the key file).
272 if (pruclient && (lastLevel == secLevel) && (secLevel != 2)) {
278 fprintf(stderr, "libprot: Could not initialize rx.\n");
282 switch (rxgk_level) {
283 case RXGK_LEVEL_CLEAR:
284 case RXGK_LEVEL_AUTH:
285 case RXGK_LEVEL_CRYPT:
288 fprintf(stderr, "libprot: Cannot use rxgk with non-localauth right now\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 switch (rxgk_level) {
298 case RXGK_LEVEL_CLEAR: code = afsconf_ClientAuthRXGKClear(tdir, &sc, &scIndex);
300 case RXGK_LEVEL_AUTH: code = afsconf_ClientAuthRXGKAuth(tdir, &sc, &scIndex);
302 case RXGK_LEVEL_CRYPT: code = afsconf_ClientAuthRXGKCrypt(tdir, &sc, &scIndex);
305 afs_com_err(whoami, code, "(calling client rxgk)");
306 } else if (secLevel == 2) {
307 /* If secLevel is two assume we're on a file server and use
308 * ClientAuthSecure if possible. */
309 code = afsconf_ClientAuthSecure(tdir, &sc, &scIndex);
311 afs_com_err(whoami, code, "(calling client secure)\n");
312 } else if (secLevel > 0) {
315 secFlags |= AFSCONF_SECOPTS_ALWAYSENCRYPT;
317 code = afsconf_ClientAuthToken(&info, secFlags, &sc, &scIndex, NULL);
319 afs_com_err(whoami, code, "(getting token)");
326 sc = rxnull_NewClientSecurityObject();
327 scIndex = RX_SECIDX_NULL;
330 if ((scIndex == RX_SECIDX_NULL) && (secLevel != 0))
332 "%s: Could not get afs tokens, running unauthenticated\n",
335 memset(serverconns, 0, sizeof(serverconns)); /* terminate list!!! */
336 for (i = 0; i < info.numServers; i++)
338 rx_NewConnection(info.hostAddr[i].sin_addr.s_addr,
339 info.hostAddr[i].sin_port, PRSRV, sc,
342 code = ubik_ClientInit(serverconns, &pruclient);
344 afs_com_err(whoami, code, "ubik client init failed.");
349 code = rxs_Release(sc);
359 code = ubik_ClientDestroy(pruclient);
366 * Make sure that arg is a proper C string that fits in a prname.
367 * If strnlen(arg, PR_MAXNAMELEN) == PR_MAXNAMELEN, then arg either
368 * doesn't have a terminating NUL or is too long, and we can't tell
369 * which one in the current API. This code has always assumed that
370 * the names presented to it are valid C strings, but for robustness
371 * we can't depend on the server side guaranteeing that. Unfortunately,
372 * the wire protocol uses a vector[PR_MAXNAMELEN] of char, so XDR will
373 * not automatically fix up strings generated by the server.
375 * The inequality is just belt-and-suspenders and should be impossible.
377 static_inline int check_length(prname arg)
379 if (strnlen(arg, PR_MAXNAMELEN) >= PR_MAXNAMELEN)
380 return PRNAMETOOLONG;
385 pr_CreateUser(prname name, afs_int32 *id)
389 code = check_length(name);
394 code = ubik_PR_INewEntry(pruclient, 0, name, *id, 0);
396 code = ubik_PR_NewEntry(pruclient, 0, name, 0, 0, id);
402 pr_CreateGroup(prname name, prname owner, afs_int32 *id)
408 code = check_length(name);
411 /* pr_SNameToId will check owner's length. */
414 code = pr_SNameToId(owner, &oid);
417 if (oid == ANONYMOUSID)
422 code = ubik_PR_INewEntry(pruclient, 0, name, *id, oid);
424 code = ubik_PR_NewEntry(pruclient, 0, name, flags, oid, id);
430 pr_Delete(prname name)
435 /* pr_SNameToId both checks the length of name and lowercases it. */
436 code = pr_SNameToId(name, &id);
439 if (id == ANONYMOUSID)
441 code = ubik_PR_Delete(pruclient, 0, id);
446 pr_DeleteByID(afs_int32 id)
450 code = ubik_PR_Delete(pruclient, 0, id);
455 pr_AddToGroup(prname user, prname group)
461 code = check_length(user);
464 code = check_length(group);
467 lnames.namelist_len = 2;
468 lnames.namelist_val = malloc(2 * PR_MAXNAMELEN);
469 strncpy(lnames.namelist_val[0], user, PR_MAXNAMELEN);
470 strncpy(lnames.namelist_val[1], group, PR_MAXNAMELEN);
473 code = pr_NameToId(&lnames, &lids);
476 /* if here, still could be missing an entry */
477 if (lids.idlist_len != 2) {
481 if (lids.idlist_val[0] == ANONYMOUSID
482 || lids.idlist_val[1] == ANONYMOUSID) {
487 ubik_PR_AddToGroup(pruclient, 0, lids.idlist_val[0],
490 if (lnames.namelist_val)
491 free(lnames.namelist_val);
493 xdr_free((xdrproc_t) xdr_idlist, &lids);
498 pr_RemoveUserFromGroup(prname user, prname group)
504 code = check_length(user);
507 code = check_length(group);
510 lnames.namelist_len = 2;
511 lnames.namelist_val = malloc(2 * PR_MAXNAMELEN);
512 strncpy(lnames.namelist_val[0], user, PR_MAXNAMELEN);
513 strncpy(lnames.namelist_val[1], group, PR_MAXNAMELEN);
516 code = pr_NameToId(&lnames, &lids);
520 if (lids.idlist_len != 2) {
524 if (lids.idlist_val[0] == ANONYMOUSID
525 || lids.idlist_val[1] == ANONYMOUSID) {
530 ubik_PR_RemoveFromGroup(pruclient, 0, lids.idlist_val[0],
533 if (lnames.namelist_val)
534 free(lnames.namelist_val);
536 xdr_free((xdrproc_t) xdr_idlist, &lids);
542 pr_NameToId(namelist *names, idlist *ids)
547 for (i = 0; i < names->namelist_len; i++) {
548 code = check_length(names->namelist_val[i]);
551 stolower(names->namelist_val[i]);
553 code = ubik_PR_NameToID(pruclient, 0, names, ids);
558 pr_SNameToId(prname name, afs_int32 *id)
564 code = check_length(name);
569 lnames.namelist_len = 1;
570 lnames.namelist_val = malloc(PR_MAXNAMELEN);
572 strncpy(lnames.namelist_val[0], name, PR_MAXNAMELEN);
573 code = ubik_PR_NameToID(pruclient, 0, &lnames, &lids);
574 if (lids.idlist_val) {
575 *id = *lids.idlist_val;
576 xdr_free((xdrproc_t) xdr_idlist, &lids);
577 } else if (code == 0) {
580 if (lnames.namelist_val)
581 free(lnames.namelist_val);
586 * Like ubik_PR_IDToName, but enforces that the output prnames are
587 * interpretable as C strings (i.e., NUL-terminated).
590 string_PR_IDToName(struct ubik_client *client, afs_int32 flags,
591 idlist *ids, namelist *names)
596 code = ubik_PR_IDToName(client, flags, ids, names);
599 for (i = 0; i < names->namelist_len; i++) {
600 code = check_length(names->namelist_val[i]);
609 pr_IdToName(idlist *ids, namelist *names)
611 return string_PR_IDToName(pruclient, 0, ids, names);
615 pr_SIdToName(afs_int32 id, prname name)
622 lids.idlist_val = malloc(sizeof(afs_int32));
623 *lids.idlist_val = id;
624 lnames.namelist_len = 0;
625 lnames.namelist_val = 0;
626 code = pr_IdToName(&lids, &lnames);
627 if (lnames.namelist_val)
628 strncpy(name, lnames.namelist_val[0], PR_MAXNAMELEN);
633 free(lids.idlist_val);
635 xdr_free((xdrproc_t) xdr_namelist, &lnames);
641 pr_GetCPS(afs_int32 id, prlist *CPS)
647 code = ubik_PR_GetCPS(pruclient, 0, id, CPS, &over);
648 if (code != PRSUCCESS)
651 /* do something about this, probably make a new call */
652 /* don't forget there's a hard limit in the interface */
653 fprintf(stderr, "membership list for id %d exceeds display limit\n",
660 pr_GetCPS2(afs_int32 id, afs_uint32 host, prlist *CPS)
666 code = ubik_PR_GetCPS2(pruclient, 0, id, host, CPS, &over);
667 if (code != PRSUCCESS)
670 /* do something about this, probably make a new call */
671 /* don't forget there's a hard limit in the interface */
672 fprintf(stderr, "membership list for id %d exceeds display limit\n",
679 pr_GetHostCPS(afs_uint32 host, prlist *CPS)
685 code = ubik_PR_GetHostCPS(pruclient, 0, host, CPS, &over);
686 if (code != PRSUCCESS)
689 /* do something about this, probably make a new call */
690 /* don't forget there's a hard limit in the interface */
692 "membership list for host id %d exceeds display limit\n",
699 pr_ListMembers(prname group, namelist *lnames)
705 memset(lnames, 0, sizeof(namelist));
707 /* pr_SNameToId checks the length of group. */
708 code = pr_SNameToId(group, &gid);
711 if (gid == ANONYMOUSID)
713 code = pr_IDListMembers(gid, lnames);
716 for (i = 0; i < lnames->namelist_len; i++) {
717 code = check_length(lnames->namelist_val[i]);
725 pr_ListOwned(afs_int32 oid, namelist *lnames, afs_int32 *moreP)
731 alist.prlist_len = 0;
732 alist.prlist_val = 0;
733 code = ubik_PR_ListOwned(pruclient, 0, oid, &alist, moreP);
737 /* Remain backwards compatible when moreP was a T/F bit */
738 fprintf(stderr, "membership list for id %d exceeds display limit\n",
742 lids = (idlist *) &alist;
743 code = pr_IdToName(lids, lnames);
745 xdr_free((xdrproc_t) xdr_prlist, &alist);
754 pr_IDListMembers(afs_int32 gid, namelist *lnames)
761 alist.prlist_len = 0;
762 alist.prlist_val = 0;
763 code = ubik_PR_ListElements(pruclient, 0, gid, &alist, &over);
767 fprintf(stderr, "membership list for id %d exceeds display limit\n",
770 lids = (idlist *) &alist;
771 code = pr_IdToName(lids, lnames);
773 xdr_free((xdrproc_t) xdr_prlist, &alist);
781 pr_IDListExpandedMembers(afs_int32 aid, namelist * lnames)
788 struct idhash *members = NULL;
789 afs_int32 *stack = NULL;
790 afs_int32 maxstack = ID_STACK_SIZE;
791 int n = 0; /* number of ids stacked */
795 code = AllocateIdHash(&members);
799 stack = malloc(sizeof(afs_int32) * maxstack);
807 gid = stack[--n]; /* pop next group id */
808 alist.prlist_len = 0;
809 alist.prlist_val = NULL;
810 if (firstpass || aid < 0) {
812 code = ubik_PR_ListElements(pruclient, 0, gid, &alist, &over);
814 code = ubik_PR_ListSuperGroups(pruclient, 0, gid, &alist, &over);
815 if (code == RXGEN_OPCODE) {
816 alist.prlist_len = 0;
817 alist.prlist_val = NULL;
818 code = 0; /* server does not support supergroups. */
825 "membership list for id %d exceeds display limit\n", gid);
827 for (i = 0; i < alist.prlist_len; i++) {
831 id = alist.prlist_val[i];
832 found = FindId(members, id);
835 xdr_free((xdrproc_t) xdr_prlist, &alist);
838 if (found == 0 && id < 0) {
839 if (n == maxstack) { /* need more stack space */
842 tmp = realloc(stack, maxstack * sizeof(afs_int32));
845 xdr_free((xdrproc_t) xdr_prlist, &alist);
850 stack[n++] = id; /* push group id */
853 xdr_free((xdrproc_t) xdr_prlist, &alist);
856 code = CreateIdList(members, &lids, (aid < 0 ? PRUSERS : PRGROUPS));
859 } else if (lids.idlist_len == 0) {
860 /* Avoid the RPC when there's nothing to look up. */
861 lnames->namelist_len = 0;
862 lnames->namelist_val = NULL;
865 code = pr_IdToName(&lids, lnames);
866 free(lids.idlist_val);
877 pr_ListEntry(afs_int32 id, struct prcheckentry *aentry)
881 code = ubik_PR_ListEntry(pruclient, 0, id, aentry);
884 return check_length(aentry->name);
888 pr_ListEntries(int flag, afs_int32 startindex, afs_int32 *nentries, struct prlistentries **entries, afs_int32 *nextstartindex)
892 prentries bulkentries;
896 *nextstartindex = -1;
897 bulkentries.prentries_val = 0;
898 bulkentries.prentries_len = 0;
901 ubik_PR_ListEntries(pruclient, 0, flag, startindex,
902 &bulkentries, nextstartindex);
905 for (i = 0; i < bulkentries.prentries_len; i++) {
906 /* XXX should we try to return all the other entries? */
907 code = check_length(bulkentries.prentries_val[i].name);
914 xdr_free((xdrproc_t)xdr_prentries, &bulkentries);
916 *nentries = bulkentries.prentries_len;
917 *entries = bulkentries.prentries_val;
923 pr_CheckEntryByName(prname name, afs_int32 *id, prname owner, prname creator)
925 /* struct prcheckentry returns other things, which aren't useful to show at this time. */
927 struct prcheckentry aentry;
929 /* pr_SNameToId will check name's length. */
930 code = pr_SNameToId(name, id);
933 if (*id == ANONYMOUSID)
935 code = ubik_PR_ListEntry(pruclient, 0, *id, &aentry);
938 /* this should be done in one RPC, but I'm lazy. */
939 code = pr_SIdToName(aentry.owner, owner);
942 code = pr_SIdToName(aentry.creator, creator);
949 pr_CheckEntryById(prname name, afs_int32 id, prname owner, prname creator)
951 /* struct prcheckentry returns other things, which aren't useful to show at this time. */
953 struct prcheckentry aentry;
955 /* XXX ListEntry RPC gives us the name back so should avoid extra RPC */
956 code = pr_SIdToName(id, name);
959 if (id == ANONYMOUSID)
961 code = ubik_PR_ListEntry(pruclient, 0, id, &aentry);
964 /* this should be done in one RPC, but I'm lazy. */
965 code = pr_SIdToName(aentry.owner, owner);
968 code = pr_SIdToName(aentry.creator, creator);
975 pr_ChangeEntry(prname oldname, prname newname, afs_int32 *newid, prname newowner)
981 /* pr_SNameToId takes care of length checks for us. */
982 code = pr_SNameToId(oldname, &id);
985 if (id == ANONYMOUSID)
987 if (newowner && *newowner) {
988 code = pr_SNameToId(newowner, &oid);
991 if (oid == ANONYMOUSID)
995 code = ubik_PR_ChangeEntry(pruclient, 0, id, newname, oid, *newid);
997 code = ubik_PR_ChangeEntry(pruclient, 0, id, newname, oid, 0);
1002 pr_IsAMemberOf(prname uname, prname gname, afs_int32 *flag)
1008 code = check_length(uname);
1011 code = check_length(gname);
1016 lnames.namelist_len = 2;
1017 lnames.namelist_val = malloc(2 * PR_MAXNAMELEN);
1018 strncpy(lnames.namelist_val[0], uname, PR_MAXNAMELEN);
1019 strncpy(lnames.namelist_val[1], gname, PR_MAXNAMELEN);
1020 lids.idlist_val = 0;
1021 lids.idlist_len = 0;
1022 code = pr_NameToId(&lnames, &lids);
1024 if (lnames.namelist_val)
1025 free(lnames.namelist_val);
1026 xdr_free((xdrproc_t) xdr_idlist, &lids);
1029 if (lids.idlist_len != 2) {
1030 free(lnames.namelist_val);
1031 xdr_free((xdrproc_t) xdr_idlist, &lids);
1035 ubik_PR_IsAMemberOf(pruclient, 0, lids.idlist_val[0],
1036 lids.idlist_val[1], flag);
1037 if (lnames.namelist_val)
1038 free(lnames.namelist_val);
1039 xdr_free((xdrproc_t) xdr_idlist, &lids);
1044 pr_ListMaxUserId(afs_int32 *mid)
1048 code = ubik_PR_ListMax(pruclient, 0, mid, &gid);
1053 pr_SetMaxUserId(afs_int32 mid)
1057 code = ubik_PR_SetMax(pruclient, 0, mid, flag);
1062 pr_ListMaxGroupId(afs_int32 *mid)
1066 code = ubik_PR_ListMax(pruclient, 0, &id, mid);
1071 pr_SetMaxGroupId(afs_int32 mid)
1077 code = ubik_PR_SetMax(pruclient, 0, mid, flag);
1082 pr_SetFieldsEntry(afs_int32 id, afs_int32 mask, afs_int32 flags, afs_int32 ngroups, afs_int32 nusers)
1087 ubik_PR_SetFieldsEntry(pruclient, 0, id, mask, flags, ngroups,
1093 pr_ListSuperGroups(afs_int32 gid, namelist * lnames)
1100 alist.prlist_len = 0;
1101 alist.prlist_val = 0;
1102 code = ubik_PR_ListSuperGroups(pruclient, 0, gid, &alist, &over);
1106 fprintf(stderr, "supergroup list for id %d exceeds display limit\n",
1109 lids = (idlist *) & alist;
1110 code = pr_IdToName(lids, lnames);
1112 xdr_free((xdrproc_t) xdr_prlist, &alist);