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/pterror.h"
33 #else /* defined(UKERNEL) */
36 #include <sys/types.h>
40 #include <netinet/in.h>
54 #include <afs/cellconfig.h>
55 #include <afs/afsutil.h>
58 #endif /* defined(UKERNEL) */
61 struct ubik_client *pruclient = 0;
62 static afs_int32 lastLevel; /* security level pruclient, if any */
64 static char *whoami = "libprot";
67 pr_Initialize(IN afs_int32 secLevel, IN char *confDir, IN char *cell)
70 struct rx_connection *serverconns[MAXSERVERS];
71 struct rx_securityClass *sc[3];
72 static struct afsconf_dir *tdir = 0; /* only do this once */
73 static char tconfDir[100];
74 struct ktc_token ttoken;
76 static struct afsconf_cell info;
80 initialize_PT_error_table();
81 initialize_RXK_error_table();
82 initialize_ACFG_error_table();
83 initialize_KTC_error_table();
85 if (strcmp(confDir, tconfDir)) {
87 * Different conf dir; force re-evaluation.
91 tdir = (struct afsconf_dir *)0;
92 pruclient = (struct ubik_client *)0;
95 strncpy(tconfDir, confDir, sizeof(tconfDir));
99 cell = afs_LclCellName;
101 #else /* defined(UKERNEL) */
102 tdir = afsconf_Open(confDir);
104 if (confDir && strcmp(confDir, ""))
106 "libprot: Could not open configuration directory: %s.\n",
112 code = afsconf_GetLocalCell(tdir, cellstr, sizeof(cellstr));
115 "vos: can't get local cell name - check %s/%s\n",
116 confDir, AFSDIR_THISCELL_FILE);
121 #endif /* defined(UKERNEL) */
123 code = afsconf_GetCellInfo(tdir, cell, "afsprot", &info);
125 fprintf(stderr, "libprot: Could not locate cell %s in %s/%s\n",
126 cell, confDir, AFSDIR_CELLSERVDB_FILE);
131 /* If we already have a client and it is at the security level we
132 * want, don't get a new one. Unless the security level is 2 in
133 * which case we will get one (and re-read the key file).
135 if (pruclient && (lastLevel == secLevel) && (secLevel != 2))
140 fprintf(stderr, "libprot: Could not initialize rx.\n");
148 /* Most callers use secLevel==1, however, the fileserver uses secLevel==2
149 * to force use of the KeyFile. secLevel == 0 implies -noauth was
151 if ((secLevel == 2) && (afsconf_GetLatestKey(tdir, 0, 0) == 0)) {
152 /* If secLevel is two assume we're on a file server and use
153 * ClientAuthSecure if possible. */
154 code = afsconf_ClientAuthSecure(tdir, &sc[2], &scIndex);
157 "libprot: clientauthsecure returns %d %s"
158 " (so trying noauth)\n", code, error_message(code));
160 scIndex = 0; /* use noauth */
162 /* if there was a problem, an unauthenticated conn is returned */
164 } else if (secLevel > 0) {
165 struct ktc_principal sname;
166 strcpy(sname.cell, info.name);
167 sname.instance[0] = 0;
168 strcpy(sname.name, "afs");
169 code = ktc_GetToken(&sname, &ttoken, sizeof(ttoken), NULL);
173 if (ttoken.kvno >= 0 && ttoken.kvno <= 256)
174 /* this is a kerberos ticket, set scIndex accordingly */
178 "libprot: funny kvno (%d) in ticket, proceeding\n",
183 rxkad_NewClientSecurityObject(rxkad_clear, &ttoken.sessionKey,
184 ttoken.kvno, ttoken.ticketLen,
190 if ((scIndex == 0) && (sc[0] == 0))
191 sc[0] = rxnull_NewClientSecurityObject();
192 if ((scIndex == 0) && (secLevel != 0))
193 com_err(whoami, code,
194 "Could not get afs tokens, running unauthenticated.");
196 memset(serverconns, 0, sizeof(serverconns)); /* terminate list!!! */
197 for (i = 0; i < info.numServers; i++)
199 rx_NewConnection(info.hostAddr[i].sin_addr.s_addr,
200 info.hostAddr[i].sin_port, PRSRV, sc[scIndex],
203 code = ubik_ClientInit(serverconns, &pruclient);
205 com_err(whoami, code, "ubik client init failed.");
210 code = rxs_Release(sc[scIndex]);
220 code = ubik_ClientDestroy(pruclient);
229 pr_CreateUser(char name[PR_MAXNAMELEN], afs_int32 *id)
231 register afs_int32 code;
235 code = ubik_Call(PR_INewEntry, pruclient, 0, name, *id, 0);
238 code = ubik_Call(PR_NewEntry, pruclient, 0, name, 0, 0, id);
245 pr_CreateGroup(char name[PR_MAXNAMELEN], char owner[PR_MAXNAMELEN], afs_int32 *id)
247 register afs_int32 code;
253 code = pr_SNameToId(owner, &oid);
256 if (oid == ANONYMOUSID)
261 code = ubik_Call(PR_INewEntry, pruclient, 0, name, *id, oid);
264 code = ubik_Call(PR_NewEntry, pruclient, 0, name, flags, oid, id);
270 pr_Delete(char *name)
272 register afs_int32 code;
276 code = pr_SNameToId(name, &id);
279 if (id == ANONYMOUSID)
281 code = ubik_Call(PR_Delete, pruclient, 0, id);
286 pr_DeleteByID(afs_int32 id)
288 register afs_int32 code;
290 code = ubik_Call(PR_Delete, pruclient, 0, id);
295 pr_AddToGroup(char *user, char *group)
297 register afs_int32 code;
301 lnames.namelist_len = 2;
302 lnames.namelist_val = (prname *) malloc(2 * PR_MAXNAMELEN);
303 strncpy(lnames.namelist_val[0], user, PR_MAXNAMELEN);
304 strncpy(lnames.namelist_val[1], group, PR_MAXNAMELEN);
307 code = pr_NameToId(&lnames, &lids);
310 /* if here, still could be missing an entry */
311 if (lids.idlist_val[0] == ANONYMOUSID
312 || lids.idlist_val[1] == ANONYMOUSID) {
317 ubik_Call(PR_AddToGroup, pruclient, 0, lids.idlist_val[0],
320 if (lnames.namelist_val)
321 free(lnames.namelist_val);
323 free(lids.idlist_val);
328 pr_RemoveUserFromGroup(char *user, char *group)
330 register afs_int32 code;
334 lnames.namelist_len = 2;
335 lnames.namelist_val = (prname *) malloc(2 * PR_MAXNAMELEN);
336 strncpy(lnames.namelist_val[0], user, PR_MAXNAMELEN);
337 strncpy(lnames.namelist_val[1], group, PR_MAXNAMELEN);
340 code = pr_NameToId(&lnames, &lids);
344 if (lids.idlist_val[0] == ANONYMOUSID
345 || lids.idlist_val[1] == ANONYMOUSID) {
350 ubik_Call(PR_RemoveFromGroup, pruclient, 0, lids.idlist_val[0],
353 if (lnames.namelist_val)
354 free(lnames.namelist_val);
356 free(lids.idlist_val);
362 pr_NameToId(namelist *names, idlist *ids)
364 register afs_int32 code;
365 register afs_int32 i;
367 for (i = 0; i < names->namelist_len; i++)
368 stolower(names->namelist_val[i]);
369 code = ubik_Call(PR_NameToID, pruclient, 0, names, ids);
374 pr_SNameToId(char name[PR_MAXNAMELEN], afs_int32 *id)
378 register afs_int32 code;
382 lnames.namelist_len = 1;
383 lnames.namelist_val = (prname *) malloc(PR_MAXNAMELEN);
385 strncpy(lnames.namelist_val[0], name, PR_MAXNAMELEN);
386 code = ubik_Call(PR_NameToID, pruclient, 0, &lnames, &lids);
387 if (lids.idlist_val) {
388 *id = *lids.idlist_val;
389 free(lids.idlist_val);
391 if (lnames.namelist_val)
392 free(lnames.namelist_val);
397 pr_IdToName(idlist *ids, namelist *names)
399 register afs_int32 code;
401 code = ubik_Call(PR_IDToName, pruclient, 0, ids, names);
406 pr_SIdToName(afs_int32 id, char name[PR_MAXNAMELEN])
410 register afs_int32 code;
413 lids.idlist_val = (afs_int32 *) malloc(sizeof(afs_int32));
414 *lids.idlist_val = id;
415 lnames.namelist_len = 0;
416 lnames.namelist_val = 0;
417 code = ubik_Call(PR_IDToName, pruclient, 0, &lids, &lnames);
418 if (lnames.namelist_val) {
419 strncpy(name, lnames.namelist_val[0], PR_MAXNAMELEN);
420 free(lnames.namelist_val);
423 free(lids.idlist_val);
428 pr_GetCPS(afs_int32 id, prlist *CPS)
430 register afs_int32 code;
434 code = ubik_Call(PR_GetCPS, pruclient, 0, id, CPS, &over);
435 if (code != PRSUCCESS)
438 /* do something about this, probably make a new call */
439 /* don't forget there's a hard limit in the interface */
440 fprintf(stderr, "membership list for id %d exceeds display limit\n",
447 pr_GetCPS2(afs_int32 id, afs_int32 host, prlist *CPS)
449 register afs_int32 code;
453 code = ubik_Call(PR_GetCPS2, pruclient, 0, id, host, CPS, &over);
454 if (code != PRSUCCESS)
457 /* do something about this, probably make a new call */
458 /* don't forget there's a hard limit in the interface */
459 fprintf(stderr, "membership list for id %d exceeds display limit\n",
466 pr_GetHostCPS(afs_int32 host, prlist *CPS)
468 register afs_int32 code;
472 code = ubik_Call(PR_GetHostCPS, pruclient, 0, host, CPS, &over);
473 if (code != PRSUCCESS)
476 /* do something about this, probably make a new call */
477 /* don't forget there's a hard limit in the interface */
479 "membership list for host id %d exceeds display limit\n",
486 pr_ListMembers(char *group, namelist *lnames)
488 register afs_int32 code;
491 code = pr_SNameToId(group, &gid);
494 if (gid == ANONYMOUSID)
496 code = pr_IDListMembers(gid, lnames);
501 pr_ListOwned(afs_int32 oid, namelist *lnames, afs_int32 *moreP)
503 register afs_int32 code;
507 alist.prlist_len = 0;
508 alist.prlist_val = 0;
509 code = ubik_Call(PR_ListOwned, pruclient, 0, oid, &alist, moreP);
513 /* Remain backwards compatible when moreP was a T/F bit */
514 fprintf(stderr, "membership list for id %d exceeds display limit\n",
518 lids = (idlist *) & alist;
519 code = pr_IdToName(lids, lnames);
522 if (alist.prlist_val)
523 free(alist.prlist_val);
528 pr_IDListMembers(afs_int32 gid, namelist *lnames)
530 register afs_int32 code;
535 alist.prlist_len = 0;
536 alist.prlist_val = 0;
537 code = ubik_Call(PR_ListElements, pruclient, 0, gid, &alist, &over);
541 fprintf(stderr, "membership list for id %d exceeds display limit\n",
544 lids = (idlist *) & alist;
545 code = pr_IdToName(lids, lnames);
548 if (alist.prlist_val)
549 free(alist.prlist_val);
554 pr_ListEntry(afs_int32 id, struct prcheckentry *aentry)
556 register afs_int32 code;
558 code = ubik_Call(PR_ListEntry, pruclient, 0, id, aentry);
563 pr_ListEntries(int flag, afs_int32 startindex, afs_int32 *nentries, struct prlistentries **entries, afs_int32 *nextstartindex)
566 prentries bulkentries;
570 *nextstartindex = -1;
571 bulkentries.prentries_val = 0;
572 bulkentries.prentries_len = 0;
575 ubik_Call(PR_ListEntries, pruclient, 0, flag, startindex,
576 &bulkentries, nextstartindex);
577 *nentries = bulkentries.prentries_len;
578 *entries = bulkentries.prentries_val;
583 pr_CheckEntryByName(char *name, afs_int32 *id, char *owner, char *creator)
585 /* struct prcheckentry returns other things, which aren't useful to show at this time. */
586 register afs_int32 code;
587 struct prcheckentry aentry;
589 code = pr_SNameToId(name, id);
592 if (*id == ANONYMOUSID)
594 code = ubik_Call(PR_ListEntry, pruclient, 0, *id, &aentry);
597 /* this should be done in one RPC, but I'm lazy. */
598 code = pr_SIdToName(aentry.owner, owner);
601 code = pr_SIdToName(aentry.creator, creator);
608 pr_CheckEntryById(char *name, afs_int32 id, char *owner, char *creator)
610 /* struct prcheckentry returns other things, which aren't useful to show at this time. */
611 register afs_int32 code;
612 struct prcheckentry aentry;
614 code = pr_SIdToName(id, name);
617 if (id == ANONYMOUSID)
619 code = ubik_Call(PR_ListEntry, pruclient, 0, id, &aentry);
622 /* this should be done in one RPC, but I'm lazy. */
623 code = pr_SIdToName(aentry.owner, owner);
626 code = pr_SIdToName(aentry.creator, creator);
633 pr_ChangeEntry(char *oldname, char *newname, afs_int32 *newid, char *newowner)
635 register afs_int32 code;
639 code = pr_SNameToId(oldname, &id);
642 if (id == ANONYMOUSID)
644 if (newowner && *newowner) {
645 code = pr_SNameToId(newowner, &oid);
648 if (oid == ANONYMOUSID)
651 code = ubik_Call(PR_ChangeEntry, pruclient, 0, id, newname, oid, newid);
656 pr_IsAMemberOf(char *uname, char *gname, afs_int32 *flag)
658 register afs_int32 code;
664 lnames.namelist_len = 2;
665 lnames.namelist_val = (prname *) malloc(2 * PR_MAXNAMELEN);
666 strncpy(lnames.namelist_val[0], uname, PR_MAXNAMELEN);
667 strncpy(lnames.namelist_val[1], gname, PR_MAXNAMELEN);
670 code = pr_NameToId(&lnames, &lids);
672 if (lnames.namelist_val)
673 free(lnames.namelist_val);
675 free(lids.idlist_val);
679 ubik_Call(PR_IsAMemberOf, pruclient, 0, lids.idlist_val[0],
680 lids.idlist_val[1], flag);
681 if (lnames.namelist_val)
682 free(lnames.namelist_val);
684 free(lids.idlist_val);
689 pr_ListMaxUserId(afs_int32 *mid)
691 register afs_int32 code;
693 code = ubik_Call(PR_ListMax, pruclient, 0, mid, &gid);
698 pr_SetMaxUserId(afs_int32 mid)
700 register afs_int32 code;
702 code = ubik_Call(PR_SetMax, pruclient, 0, mid, flag);
707 pr_ListMaxGroupId(afs_int32 *mid)
709 register afs_int32 code;
711 code = ubik_Call(PR_ListMax, pruclient, 0, &id, mid);
716 pr_SetMaxGroupId(afs_int32 mid)
718 register afs_int32 code;
722 code = ubik_Call(PR_SetMax, pruclient, 0, mid, flag);
727 pr_SetFieldsEntry(afs_int32 id, afs_int32 mask, afs_int32 flags, afs_int32 ngroups, afs_int32 nusers)
729 register afs_int32 code;
732 ubik_Call(PR_SetFieldsEntry, pruclient, 0, id, mask, flags, ngroups,