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 char *confDir, IN char *cell)
72 struct rx_connection *serverconns[MAXSERVERS];
73 struct rx_securityClass *sc[3];
74 static struct afsconf_dir *tdir = 0; /* only do this once */
75 static char tconfDir[100];
76 struct ktc_token ttoken;
78 static struct afsconf_cell info;
82 initialize_PT_error_table();
83 initialize_RXK_error_table();
84 initialize_ACFG_error_table();
85 initialize_KTC_error_table();
87 if (strcmp(confDir, tconfDir)) {
89 * Different conf dir; force re-evaluation.
93 tdir = (struct afsconf_dir *)0;
94 pruclient = (struct ubik_client *)0;
97 strncpy(tconfDir, confDir, sizeof(tconfDir));
101 cell = afs_LclCellName;
103 #else /* defined(UKERNEL) */
104 tdir = afsconf_Open(confDir);
106 if (confDir && strcmp(confDir, ""))
108 "libprot: Could not open configuration directory: %s.\n",
114 code = afsconf_GetLocalCell(tdir, cellstr, sizeof(cellstr));
117 "vos: can't get local cell name - check %s/%s\n",
118 confDir, AFSDIR_THISCELL_FILE);
123 #endif /* defined(UKERNEL) */
125 code = afsconf_GetCellInfo(tdir, cell, "afsprot", &info);
127 fprintf(stderr, "libprot: Could not locate cell %s in %s/%s\n",
128 cell, confDir, AFSDIR_CELLSERVDB_FILE);
133 /* If we already have a client and it is at the security level we
134 * want, don't get a new one. Unless the security level is 2 in
135 * which case we will get one (and re-read the key file).
137 if (pruclient && (lastLevel == secLevel) && (secLevel != 2))
142 fprintf(stderr, "libprot: Could not initialize rx.\n");
150 /* Most callers use secLevel==1, however, the fileserver uses secLevel==2
151 * to force use of the KeyFile. secLevel == 0 implies -noauth was
153 if ((secLevel == 2) && (afsconf_GetLatestKey(tdir, 0, 0) == 0)) {
154 /* If secLevel is two assume we're on a file server and use
155 * ClientAuthSecure if possible. */
156 code = afsconf_ClientAuthSecure(tdir, &sc[2], &scIndex);
159 "libprot: clientauthsecure returns %d %s"
160 " (so trying noauth)\n", code, error_message(code));
162 scIndex = 0; /* use noauth */
164 /* if there was a problem, an unauthenticated conn is returned */
166 } else if (secLevel > 0) {
167 struct ktc_principal sname;
168 strcpy(sname.cell, info.name);
169 sname.instance[0] = 0;
170 strcpy(sname.name, "afs");
171 code = ktc_GetToken(&sname, &ttoken, sizeof(ttoken), NULL);
175 if (ttoken.kvno >= 0 && ttoken.kvno <= 256)
176 /* this is a kerberos ticket, set scIndex accordingly */
180 "libprot: funny kvno (%d) in ticket, proceeding\n",
185 rxkad_NewClientSecurityObject(rxkad_clear, &ttoken.sessionKey,
186 ttoken.kvno, ttoken.ticketLen,
192 if ((scIndex == 0) && (sc[0] == 0))
193 sc[0] = rxnull_NewClientSecurityObject();
194 if ((scIndex == 0) && (secLevel != 0))
195 com_err(whoami, code,
196 "Could not get afs tokens, running unauthenticated.");
198 memset(serverconns, 0, sizeof(serverconns)); /* terminate list!!! */
199 for (i = 0; i < info.numServers; i++)
201 rx_NewConnection(info.hostAddr[i].sin_addr.s_addr,
202 info.hostAddr[i].sin_port, PRSRV, sc[scIndex],
205 code = ubik_ClientInit(serverconns, &pruclient);
207 com_err(whoami, code, "ubik client init failed.");
212 code = rxs_Release(sc[scIndex]);
222 code = ubik_ClientDestroy(pruclient);
231 pr_CreateUser(char name[PR_MAXNAMELEN], afs_int32 *id)
233 register afs_int32 code;
237 code = ubik_Call(PR_INewEntry, pruclient, 0, name, *id, 0);
240 code = ubik_Call(PR_NewEntry, pruclient, 0, name, 0, 0, id);
247 pr_CreateGroup(char name[PR_MAXNAMELEN], char owner[PR_MAXNAMELEN], afs_int32 *id)
249 register afs_int32 code;
255 code = pr_SNameToId(owner, &oid);
258 if (oid == ANONYMOUSID)
263 code = ubik_Call(PR_INewEntry, pruclient, 0, name, *id, oid);
266 code = ubik_Call(PR_NewEntry, pruclient, 0, name, flags, oid, id);
272 pr_Delete(char *name)
274 register afs_int32 code;
278 code = pr_SNameToId(name, &id);
281 if (id == ANONYMOUSID)
283 code = ubik_Call(PR_Delete, pruclient, 0, id);
288 pr_DeleteByID(afs_int32 id)
290 register afs_int32 code;
292 code = ubik_Call(PR_Delete, pruclient, 0, id);
297 pr_AddToGroup(char *user, char *group)
299 register afs_int32 code;
303 lnames.namelist_len = 2;
304 lnames.namelist_val = (prname *) malloc(2 * PR_MAXNAMELEN);
305 strncpy(lnames.namelist_val[0], user, PR_MAXNAMELEN);
306 strncpy(lnames.namelist_val[1], group, PR_MAXNAMELEN);
309 code = pr_NameToId(&lnames, &lids);
312 /* if here, still could be missing an entry */
313 if (lids.idlist_val[0] == ANONYMOUSID
314 || lids.idlist_val[1] == ANONYMOUSID) {
319 ubik_Call(PR_AddToGroup, pruclient, 0, lids.idlist_val[0],
322 if (lnames.namelist_val)
323 free(lnames.namelist_val);
325 free(lids.idlist_val);
330 pr_RemoveUserFromGroup(char *user, char *group)
332 register afs_int32 code;
336 lnames.namelist_len = 2;
337 lnames.namelist_val = (prname *) malloc(2 * PR_MAXNAMELEN);
338 strncpy(lnames.namelist_val[0], user, PR_MAXNAMELEN);
339 strncpy(lnames.namelist_val[1], group, PR_MAXNAMELEN);
342 code = pr_NameToId(&lnames, &lids);
346 if (lids.idlist_val[0] == ANONYMOUSID
347 || lids.idlist_val[1] == ANONYMOUSID) {
352 ubik_Call(PR_RemoveFromGroup, pruclient, 0, lids.idlist_val[0],
355 if (lnames.namelist_val)
356 free(lnames.namelist_val);
358 free(lids.idlist_val);
364 pr_NameToId(namelist *names, idlist *ids)
366 register afs_int32 code;
367 register afs_int32 i;
369 for (i = 0; i < names->namelist_len; i++)
370 stolower(names->namelist_val[i]);
371 code = ubik_Call(PR_NameToID, pruclient, 0, names, ids);
376 pr_SNameToId(char name[PR_MAXNAMELEN], afs_int32 *id)
380 register afs_int32 code;
384 lnames.namelist_len = 1;
385 lnames.namelist_val = (prname *) malloc(PR_MAXNAMELEN);
387 strncpy(lnames.namelist_val[0], name, PR_MAXNAMELEN);
388 code = ubik_Call(PR_NameToID, pruclient, 0, &lnames, &lids);
389 if (lids.idlist_val) {
390 *id = *lids.idlist_val;
391 free(lids.idlist_val);
393 if (lnames.namelist_val)
394 free(lnames.namelist_val);
399 pr_IdToName(idlist *ids, namelist *names)
401 register afs_int32 code;
403 code = ubik_Call(PR_IDToName, pruclient, 0, ids, names);
408 pr_SIdToName(afs_int32 id, char name[PR_MAXNAMELEN])
412 register afs_int32 code;
415 lids.idlist_val = (afs_int32 *) malloc(sizeof(afs_int32));
416 *lids.idlist_val = id;
417 lnames.namelist_len = 0;
418 lnames.namelist_val = 0;
419 code = ubik_Call(PR_IDToName, pruclient, 0, &lids, &lnames);
420 if (lnames.namelist_val) {
421 strncpy(name, lnames.namelist_val[0], PR_MAXNAMELEN);
422 free(lnames.namelist_val);
425 free(lids.idlist_val);
430 pr_GetCPS(afs_int32 id, prlist *CPS)
432 register afs_int32 code;
436 code = ubik_Call(PR_GetCPS, pruclient, 0, id, CPS, &over);
437 if (code != PRSUCCESS)
440 /* do something about this, probably make a new call */
441 /* don't forget there's a hard limit in the interface */
442 fprintf(stderr, "membership list for id %d exceeds display limit\n",
449 pr_GetCPS2(afs_int32 id, afs_int32 host, prlist *CPS)
451 register afs_int32 code;
455 code = ubik_Call(PR_GetCPS2, pruclient, 0, id, host, CPS, &over);
456 if (code != PRSUCCESS)
459 /* do something about this, probably make a new call */
460 /* don't forget there's a hard limit in the interface */
461 fprintf(stderr, "membership list for id %d exceeds display limit\n",
468 pr_GetHostCPS(afs_int32 host, prlist *CPS)
470 register afs_int32 code;
474 code = ubik_Call(PR_GetHostCPS, pruclient, 0, host, CPS, &over);
475 if (code != PRSUCCESS)
478 /* do something about this, probably make a new call */
479 /* don't forget there's a hard limit in the interface */
481 "membership list for host id %d exceeds display limit\n",
488 pr_ListMembers(char *group, namelist *lnames)
490 register afs_int32 code;
493 code = pr_SNameToId(group, &gid);
496 if (gid == ANONYMOUSID)
498 code = pr_IDListMembers(gid, lnames);
503 pr_ListOwned(afs_int32 oid, namelist *lnames, afs_int32 *moreP)
505 register afs_int32 code;
509 alist.prlist_len = 0;
510 alist.prlist_val = 0;
511 code = ubik_Call(PR_ListOwned, pruclient, 0, oid, &alist, moreP);
515 /* Remain backwards compatible when moreP was a T/F bit */
516 fprintf(stderr, "membership list for id %d exceeds display limit\n",
520 lids = (idlist *) & alist;
521 code = pr_IdToName(lids, lnames);
524 if (alist.prlist_val)
525 free(alist.prlist_val);
530 pr_IDListMembers(afs_int32 gid, namelist *lnames)
532 register afs_int32 code;
537 alist.prlist_len = 0;
538 alist.prlist_val = 0;
539 code = ubik_Call(PR_ListElements, pruclient, 0, gid, &alist, &over);
543 fprintf(stderr, "membership list for id %d exceeds display limit\n",
546 lids = (idlist *) & alist;
547 code = pr_IdToName(lids, lnames);
550 if (alist.prlist_val)
551 free(alist.prlist_val);
556 pr_ListEntry(afs_int32 id, struct prcheckentry *aentry)
558 register afs_int32 code;
560 code = ubik_Call(PR_ListEntry, pruclient, 0, id, aentry);
565 pr_ListEntries(int flag, afs_int32 startindex, afs_int32 *nentries, struct prlistentries **entries, afs_int32 *nextstartindex)
568 prentries bulkentries;
572 *nextstartindex = -1;
573 bulkentries.prentries_val = 0;
574 bulkentries.prentries_len = 0;
577 ubik_Call(PR_ListEntries, pruclient, 0, flag, startindex,
578 &bulkentries, nextstartindex);
579 *nentries = bulkentries.prentries_len;
580 *entries = bulkentries.prentries_val;
585 pr_CheckEntryByName(char *name, afs_int32 *id, char *owner, char *creator)
587 /* struct prcheckentry returns other things, which aren't useful to show at this time. */
588 register afs_int32 code;
589 struct prcheckentry aentry;
591 code = pr_SNameToId(name, id);
594 if (*id == ANONYMOUSID)
596 code = ubik_Call(PR_ListEntry, pruclient, 0, *id, &aentry);
599 /* this should be done in one RPC, but I'm lazy. */
600 code = pr_SIdToName(aentry.owner, owner);
603 code = pr_SIdToName(aentry.creator, creator);
610 pr_CheckEntryById(char *name, afs_int32 id, char *owner, char *creator)
612 /* struct prcheckentry returns other things, which aren't useful to show at this time. */
613 register afs_int32 code;
614 struct prcheckentry aentry;
616 code = pr_SIdToName(id, name);
619 if (id == ANONYMOUSID)
621 code = ubik_Call(PR_ListEntry, pruclient, 0, id, &aentry);
624 /* this should be done in one RPC, but I'm lazy. */
625 code = pr_SIdToName(aentry.owner, owner);
628 code = pr_SIdToName(aentry.creator, creator);
635 pr_ChangeEntry(char *oldname, char *newname, afs_int32 *newid, char *newowner)
637 register afs_int32 code;
641 code = pr_SNameToId(oldname, &id);
644 if (id == ANONYMOUSID)
646 if (newowner && *newowner) {
647 code = pr_SNameToId(newowner, &oid);
650 if (oid == ANONYMOUSID)
653 code = ubik_Call(PR_ChangeEntry, pruclient, 0, id, newname, oid, newid);
658 pr_IsAMemberOf(char *uname, char *gname, afs_int32 *flag)
660 register afs_int32 code;
666 lnames.namelist_len = 2;
667 lnames.namelist_val = (prname *) malloc(2 * PR_MAXNAMELEN);
668 strncpy(lnames.namelist_val[0], uname, PR_MAXNAMELEN);
669 strncpy(lnames.namelist_val[1], gname, PR_MAXNAMELEN);
672 code = pr_NameToId(&lnames, &lids);
674 if (lnames.namelist_val)
675 free(lnames.namelist_val);
677 free(lids.idlist_val);
681 ubik_Call(PR_IsAMemberOf, pruclient, 0, lids.idlist_val[0],
682 lids.idlist_val[1], flag);
683 if (lnames.namelist_val)
684 free(lnames.namelist_val);
686 free(lids.idlist_val);
691 pr_ListMaxUserId(afs_int32 *mid)
693 register afs_int32 code;
695 code = ubik_Call(PR_ListMax, pruclient, 0, mid, &gid);
700 pr_SetMaxUserId(afs_int32 mid)
702 register afs_int32 code;
704 code = ubik_Call(PR_SetMax, pruclient, 0, mid, flag);
709 pr_ListMaxGroupId(afs_int32 *mid)
711 register afs_int32 code;
713 code = ubik_Call(PR_ListMax, pruclient, 0, &id, mid);
718 pr_SetMaxGroupId(afs_int32 mid)
720 register afs_int32 code;
724 code = ubik_Call(PR_SetMax, pruclient, 0, mid, flag);
729 pr_SetFieldsEntry(afs_int32 id, afs_int32 mask, afs_int32 flags, afs_int32 ngroups, afs_int32 nusers)
731 register afs_int32 code;
734 ubik_Call(PR_SetFieldsEntry, pruclient, 0, id, mask, flags, ngroups,