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>
12 #include "afs/param.h"
14 #include <afs/param.h>
21 #include "afs/sysincludes.h"
22 #include "afs_usrops.h"
23 #include "afsincludes.h"
29 #include "afs/cellconfig.h"
30 #include "afs/afsutil.h"
31 #include "afs/ptclient.h"
32 #include "afs/ptuser.h"
33 #include "afs/pterror.h"
34 #else /* defined(UKERNEL) */
37 #include <sys/types.h>
41 #include <netinet/in.h>
55 #include <afs/cellconfig.h>
56 #include <afs/afsutil.h>
60 #endif /* defined(UKERNEL) */
63 struct ubik_client *pruclient = 0;
64 static afs_int32 lastLevel; /* security level pruclient, if any */
66 static char *whoami = "libprot";
69 pr_Initialize(IN afs_int32 secLevel, IN const char *confDir, IN char *cell)
72 struct rx_connection *serverconns[MAXSERVERS];
73 struct rx_securityClass *sc[3];
74 static struct afsconf_dir *tdir = (struct afsconf_dir *)NULL; /* only do this once */
75 static char tconfDir[100] = "";
76 static char tcell[64] = "";
77 struct ktc_token ttoken;
79 static struct afsconf_cell info;
82 afs_int32 gottdir = 0;
83 afs_int32 refresh = 0;
85 initialize_PT_error_table();
86 initialize_RXK_error_table();
87 initialize_ACFG_error_table();
88 initialize_KTC_error_table();
92 cell = afs_LclCellName;
94 #else /* defined(UKERNEL) */
97 tdir = afsconf_Open(confDir);
99 if (confDir && strcmp(confDir, ""))
101 "libprot: Could not open configuration directory: %s.\n",
105 "libprot: No configuration directory specified.\n");
110 code = afsconf_GetLocalCell(tdir, cellstr, sizeof(cellstr));
114 "libprot: Could not get local cell. [%d]\n", code);
119 #endif /* defined(UKERNEL) */
121 if (tdir == NULL || strcmp(confDir, tconfDir) || strcmp(cell, tcell)) {
123 * force re-evaluation. we either don't have an afsconf_dir,
124 * the directory has changed or the cell has changed.
126 if (tdir && !gottdir) {
128 tdir = (struct afsconf_dir *)NULL;
130 pruclient = (struct ubik_client *)NULL;
135 strncpy(tconfDir, confDir, sizeof(tconfDir));
136 strncpy(tcell, cell, sizeof(tcell));
140 #else /* defined(UKERNEL) */
142 tdir = afsconf_Open(confDir);
144 if (confDir && strcmp(confDir, ""))
146 "libprot: Could not open configuration directory: %s.\n",
150 "libprot: No configuration directory specified.\n");
153 #endif /* defined(UKERNEL) */
155 code = afsconf_GetCellInfo(tdir, cell, "afsprot", &info);
158 fprintf(stderr, "libprot: Could not locate cell %s in %s/%s\n",
159 cell, confDir, AFSDIR_CELLSERVDB_FILE);
164 /* If we already have a client and it is at the security level we
165 * want, don't get a new one. Unless the security level is 2 in
166 * which case we will get one (and re-read the key file).
168 if (pruclient && (lastLevel == secLevel) && (secLevel != 2)) {
176 fprintf(stderr, "libprot: Could not initialize rx.\n");
184 /* Most callers use secLevel==1, however, the fileserver uses secLevel==2
185 * to force use of the KeyFile. secLevel == 0 implies -noauth was
187 if ((secLevel == 2) && (afsconf_GetLatestKey(tdir, 0, 0) == 0)) {
188 /* If secLevel is two assume we're on a file server and use
189 * ClientAuthSecure if possible. */
190 code = afsconf_ClientAuthSecure(tdir, &sc[2], &scIndex);
193 "libprot: clientauthsecure returns %d %s"
194 " (so trying noauth)\n", code, error_message(code));
196 scIndex = 0; /* use noauth */
198 /* if there was a problem, an unauthenticated conn is returned */
200 } else if (secLevel > 0) {
201 struct ktc_principal sname;
202 strcpy(sname.cell, info.name);
203 sname.instance[0] = 0;
204 strcpy(sname.name, "afs");
205 code = ktc_GetToken(&sname, &ttoken, sizeof(ttoken), NULL);
209 if (ttoken.kvno >= 0 && ttoken.kvno <= 256)
210 /* this is a kerberos ticket, set scIndex accordingly */
214 "libprot: funny kvno (%d) in ticket, proceeding\n",
219 rxkad_NewClientSecurityObject(rxkad_clear, &ttoken.sessionKey,
220 ttoken.kvno, ttoken.ticketLen,
229 if ((scIndex == 0) && (sc[0] == 0))
230 sc[0] = rxnull_NewClientSecurityObject();
231 if ((scIndex == 0) && (secLevel != 0))
232 com_err(whoami, code,
233 "Could not get afs tokens, running unauthenticated.");
235 memset(serverconns, 0, sizeof(serverconns)); /* terminate list!!! */
236 for (i = 0; i < info.numServers; i++)
238 rx_NewConnection(info.hostAddr[i].sin_addr.s_addr,
239 info.hostAddr[i].sin_port, PRSRV, sc[scIndex],
242 code = ubik_ClientInit(serverconns, &pruclient);
244 com_err(whoami, code, "ubik client init failed.");
249 code = rxs_Release(sc[scIndex]);
259 code = ubik_ClientDestroy(pruclient);
268 pr_CreateUser(char name[PR_MAXNAMELEN], afs_int32 *id)
270 register afs_int32 code;
274 code = ubik_PR_INewEntry(pruclient, 0, name, *id, 0);
277 code = ubik_PR_NewEntry(pruclient, 0, name, 0, 0, id);
284 pr_CreateGroup(char name[PR_MAXNAMELEN], char owner[PR_MAXNAMELEN], afs_int32 *id)
286 register afs_int32 code;
292 code = pr_SNameToId(owner, &oid);
295 if (oid == ANONYMOUSID)
300 code = ubik_PR_INewEntry(pruclient, 0, name, *id, oid);
303 code = ubik_PR_NewEntry(pruclient, 0, name, flags, oid, id);
309 pr_Delete(char *name)
311 register afs_int32 code;
315 code = pr_SNameToId(name, &id);
318 if (id == ANONYMOUSID)
320 code = ubik_PR_Delete(pruclient, 0, id);
325 pr_DeleteByID(afs_int32 id)
327 register afs_int32 code;
329 code = ubik_PR_Delete(pruclient, 0, id);
334 pr_AddToGroup(char *user, char *group)
336 register afs_int32 code;
340 lnames.namelist_len = 2;
341 lnames.namelist_val = (prname *) malloc(2 * PR_MAXNAMELEN);
342 strncpy(lnames.namelist_val[0], user, PR_MAXNAMELEN);
343 strncpy(lnames.namelist_val[1], group, PR_MAXNAMELEN);
346 code = pr_NameToId(&lnames, &lids);
349 /* if here, still could be missing an entry */
350 if (lids.idlist_val[0] == ANONYMOUSID
351 || lids.idlist_val[1] == ANONYMOUSID) {
356 ubik_PR_AddToGroup(pruclient, 0, lids.idlist_val[0],
359 if (lnames.namelist_val)
360 free(lnames.namelist_val);
362 free(lids.idlist_val);
367 pr_RemoveUserFromGroup(char *user, char *group)
369 register afs_int32 code;
373 lnames.namelist_len = 2;
374 lnames.namelist_val = (prname *) malloc(2 * PR_MAXNAMELEN);
375 strncpy(lnames.namelist_val[0], user, PR_MAXNAMELEN);
376 strncpy(lnames.namelist_val[1], group, PR_MAXNAMELEN);
379 code = pr_NameToId(&lnames, &lids);
383 if (lids.idlist_val[0] == ANONYMOUSID
384 || lids.idlist_val[1] == ANONYMOUSID) {
389 ubik_PR_RemoveFromGroup(pruclient, 0, lids.idlist_val[0],
392 if (lnames.namelist_val)
393 free(lnames.namelist_val);
395 free(lids.idlist_val);
401 pr_NameToId(namelist *names, idlist *ids)
403 register afs_int32 code;
404 register afs_int32 i;
406 for (i = 0; i < names->namelist_len; i++)
407 stolower(names->namelist_val[i]);
408 code = ubik_PR_NameToID(pruclient, 0, names, ids);
413 pr_SNameToId(char name[PR_MAXNAMELEN], afs_int32 *id)
417 register afs_int32 code;
421 lnames.namelist_len = 1;
422 lnames.namelist_val = (prname *) malloc(PR_MAXNAMELEN);
424 strncpy(lnames.namelist_val[0], name, PR_MAXNAMELEN);
425 code = ubik_PR_NameToID(pruclient, 0, &lnames, &lids);
426 if (lids.idlist_val) {
427 *id = *lids.idlist_val;
428 free(lids.idlist_val);
430 if (lnames.namelist_val)
431 free(lnames.namelist_val);
436 pr_IdToName(idlist *ids, namelist *names)
438 register afs_int32 code;
440 code = ubik_PR_IDToName(pruclient, 0, ids, names);
445 pr_SIdToName(afs_int32 id, char name[PR_MAXNAMELEN])
449 register afs_int32 code;
452 lids.idlist_val = (afs_int32 *) malloc(sizeof(afs_int32));
453 *lids.idlist_val = id;
454 lnames.namelist_len = 0;
455 lnames.namelist_val = 0;
456 code = ubik_PR_IDToName(pruclient, 0, &lids, &lnames);
457 if (lnames.namelist_val) {
458 strncpy(name, lnames.namelist_val[0], PR_MAXNAMELEN);
459 free(lnames.namelist_val);
462 free(lids.idlist_val);
467 pr_GetCPS(afs_int32 id, prlist *CPS)
469 register afs_int32 code;
473 code = ubik_PR_GetCPS(pruclient, 0, id, CPS, &over);
474 if (code != PRSUCCESS)
477 /* do something about this, probably make a new call */
478 /* don't forget there's a hard limit in the interface */
479 fprintf(stderr, "membership list for id %d exceeds display limit\n",
486 pr_GetCPS2(afs_int32 id, afs_int32 host, prlist *CPS)
488 register afs_int32 code;
492 code = ubik_PR_GetCPS2(pruclient, 0, id, host, CPS, &over);
493 if (code != PRSUCCESS)
496 /* do something about this, probably make a new call */
497 /* don't forget there's a hard limit in the interface */
498 fprintf(stderr, "membership list for id %d exceeds display limit\n",
505 pr_GetHostCPS(afs_int32 host, prlist *CPS)
507 register afs_int32 code;
511 code = ubik_PR_GetHostCPS(pruclient, 0, host, CPS, &over);
512 if (code != PRSUCCESS)
515 /* do something about this, probably make a new call */
516 /* don't forget there's a hard limit in the interface */
518 "membership list for host id %d exceeds display limit\n",
525 pr_ListMembers(char *group, namelist *lnames)
527 register afs_int32 code;
530 code = pr_SNameToId(group, &gid);
533 if (gid == ANONYMOUSID)
535 code = pr_IDListMembers(gid, lnames);
540 pr_ListOwned(afs_int32 oid, namelist *lnames, afs_int32 *moreP)
542 register afs_int32 code;
546 alist.prlist_len = 0;
547 alist.prlist_val = 0;
548 code = ubik_PR_ListOwned(pruclient, 0, oid, &alist, moreP);
552 /* Remain backwards compatible when moreP was a T/F bit */
553 fprintf(stderr, "membership list for id %d exceeds display limit\n",
557 lids = (idlist *) & alist;
558 code = pr_IdToName(lids, lnames);
561 if (alist.prlist_val)
562 free(alist.prlist_val);
567 pr_IDListMembers(afs_int32 gid, namelist *lnames)
569 register afs_int32 code;
574 alist.prlist_len = 0;
575 alist.prlist_val = 0;
576 code = ubik_PR_ListElements(pruclient, 0, gid, &alist, &over);
580 fprintf(stderr, "membership list for id %d exceeds display limit\n",
583 lids = (idlist *) & alist;
584 code = pr_IdToName(lids, lnames);
587 if (alist.prlist_val)
588 free(alist.prlist_val);
593 pr_ListEntry(afs_int32 id, struct prcheckentry *aentry)
595 register afs_int32 code;
597 code = ubik_PR_ListEntry(pruclient, 0, id, aentry);
602 pr_ListEntries(int flag, afs_int32 startindex, afs_int32 *nentries, struct prlistentries **entries, afs_int32 *nextstartindex)
605 prentries bulkentries;
609 *nextstartindex = -1;
610 bulkentries.prentries_val = 0;
611 bulkentries.prentries_len = 0;
614 ubik_PR_ListEntries(pruclient, 0, flag, startindex,
615 &bulkentries, nextstartindex);
616 *nentries = bulkentries.prentries_len;
617 *entries = bulkentries.prentries_val;
622 pr_CheckEntryByName(char *name, afs_int32 *id, char *owner, char *creator)
624 /* struct prcheckentry returns other things, which aren't useful to show at this time. */
625 register afs_int32 code;
626 struct prcheckentry aentry;
628 code = pr_SNameToId(name, id);
631 if (*id == ANONYMOUSID)
633 code = ubik_PR_ListEntry(pruclient, 0, *id, &aentry);
634 //code = ubik_PR_ListEntry(pruclient, 0, *id, &aentry);
637 /* this should be done in one RPC, but I'm lazy. */
638 code = pr_SIdToName(aentry.owner, owner);
641 code = pr_SIdToName(aentry.creator, creator);
648 pr_CheckEntryById(char *name, afs_int32 id, char *owner, char *creator)
650 /* struct prcheckentry returns other things, which aren't useful to show at this time. */
651 register afs_int32 code;
652 struct prcheckentry aentry;
654 code = pr_SIdToName(id, name);
657 if (id == ANONYMOUSID)
659 code = ubik_PR_ListEntry(pruclient, 0, id, &aentry);
662 /* this should be done in one RPC, but I'm lazy. */
663 code = pr_SIdToName(aentry.owner, owner);
666 code = pr_SIdToName(aentry.creator, creator);
673 pr_ChangeEntry(char *oldname, char *newname, afs_int32 *newid, char *newowner)
675 register afs_int32 code;
679 code = pr_SNameToId(oldname, &id);
682 if (id == ANONYMOUSID)
684 if (newowner && *newowner) {
685 code = pr_SNameToId(newowner, &oid);
688 if (oid == ANONYMOUSID)
691 code = ubik_PR_ChangeEntry(pruclient, 0, id, newname, oid, newid);
696 pr_IsAMemberOf(char *uname, char *gname, afs_int32 *flag)
698 register afs_int32 code;
704 lnames.namelist_len = 2;
705 lnames.namelist_val = (prname *) malloc(2 * PR_MAXNAMELEN);
706 strncpy(lnames.namelist_val[0], uname, PR_MAXNAMELEN);
707 strncpy(lnames.namelist_val[1], gname, PR_MAXNAMELEN);
710 code = pr_NameToId(&lnames, &lids);
712 if (lnames.namelist_val)
713 free(lnames.namelist_val);
715 free(lids.idlist_val);
719 ubik_PR_IsAMemberOf(pruclient, 0, lids.idlist_val[0],
720 lids.idlist_val[1], flag);
721 if (lnames.namelist_val)
722 free(lnames.namelist_val);
724 free(lids.idlist_val);
729 pr_ListMaxUserId(afs_int32 *mid)
731 register afs_int32 code;
733 code = ubik_PR_ListMax(pruclient, 0, mid, &gid);
738 pr_SetMaxUserId(afs_int32 mid)
740 register afs_int32 code;
742 code = ubik_PR_SetMax(pruclient, 0, mid, flag);
747 pr_ListMaxGroupId(afs_int32 *mid)
749 register afs_int32 code;
751 code = ubik_PR_ListMax(pruclient, 0, &id, mid);
756 pr_SetMaxGroupId(afs_int32 mid)
758 register afs_int32 code;
762 code = ubik_PR_SetMax(pruclient, 0, mid, flag);
767 pr_SetFieldsEntry(afs_int32 id, afs_int32 mask, afs_int32 flags, afs_int32 ngroups, afs_int32 nusers)
769 register afs_int32 code;
772 ubik_PR_SetFieldsEntry(pruclient, 0, id, mask, flags, ngroups,