ptuser: use the "prname" typedef rather than "char[PR_MAXNAMELEN]"
[openafs.git] / src / ptserver / ptuser.c
1 /*
2  * Copyright 2000, International Business Machines Corporation and others.
3  * All Rights Reserved.
4  *
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
8  */
9
10 #include <afsconfig.h>
11 #include <afs/param.h>
12 #include <afs/stds.h>
13
14 #include <roken.h>
15 #include <afs/opr.h>
16
17 #include <rx/rx.h>
18 #include <rx/xdr.h>
19 #include <afs/auth.h>
20 #include <afs/cellconfig.h>
21 #include <afs/afsutil.h>
22 #include <afs/com_err.h>
23
24 #include "ptclient.h"
25 #include "ptuser.h"
26 #include "pterror.h"
27
28 #ifdef UKERNEL
29 # include "afs_usrops.h"
30 #endif
31
32 struct ubik_client *pruclient = 0;
33 static afs_int32 lastLevel;     /* security level pruclient, if any */
34
35 static char *whoami = "libprot";
36
37
38 #define ID_HASH_SIZE 1024
39 #define ID_STACK_SIZE 1024
40
41 /**
42  * Hash table chain of user and group ids.
43  */
44 struct idchain {
45     struct idchain *next;
46     afs_int32 id;
47 };
48
49 /**
50  * Hash table of user and group ids.
51  */
52 struct idhash {
53     afs_uint32 userEntries;         /**< number of user id entries hashed */
54     afs_uint32 groupEntries;        /**< number of group id entries hashed */
55     struct idchain *hash[ID_HASH_SIZE];
56 };
57
58 /**
59  * Allocate a new id hash table.
60  */
61 static afs_int32
62 AllocateIdHash(struct idhash **aidhash)
63 {
64     struct idhash *idhash;
65
66     idhash = calloc(1, sizeof(struct idhash));
67     if (!idhash) {
68        return ENOMEM;
69     }
70     *aidhash = idhash;
71     return 0;
72 }
73
74 /**
75  * Free the id hash.
76  */
77 static void
78 FreeIdHash(struct idhash *idhash)
79 {
80     int index;
81     struct idchain *chain;
82     struct idchain *next;
83
84     for (index = 0; index < ID_HASH_SIZE; index++) {
85        for (chain = idhash->hash[index]; chain; chain = next) {
86            next = chain->next;
87            free(chain);
88        }
89     }
90     free(idhash);
91 }
92
93 /**
94  * Indicate if group/user id is already hashed, and
95  * if not insert it.
96  *
97  * @returns whether id is present
98  *   @retval >0 id is already present in the hash
99  *   @retval 0  id was not found and was inserted into the hash
100  *   @retval <0 error encountered
101  */
102 static afs_int32
103 FindId(struct idhash *idhash, afs_int32 id)
104 {
105     afs_int32 index;
106     struct idchain *chain;
107     struct idchain *newChain;
108
109     index = abs(id) % ID_HASH_SIZE;
110     for (chain = idhash->hash[index]; chain; chain = chain->next) {
111         if (chain->id == id) {
112             return 1;
113         }
114     }
115
116     /* Insert this id but return not found. */
117     newChain = malloc(sizeof(struct idchain));
118     if (!newChain) {
119         return ENOMEM;
120     } else {
121         newChain->id = id;
122         newChain->next = idhash->hash[index];
123         idhash->hash[index] = newChain;
124         if (id < 0) {
125             idhash->groupEntries++;
126         } else {
127             idhash->userEntries++;
128         }
129     }
130     return 0;
131 }
132
133 /**
134  * Create an idlist from the ids in the hash.
135  */
136 static afs_int32
137 CreateIdList(struct idhash *idhash, idlist * alist, afs_int32 select)
138 {
139     struct idchain *chain;
140     afs_int32 entries = 0;
141     int index;
142     int i;
143
144     if (select & PRGROUPS) {
145         entries += idhash->groupEntries;
146     }
147     if (select & PRUSERS) {
148         entries += idhash->userEntries;
149     }
150
151     alist->idlist_len = entries;
152     alist->idlist_val = malloc(sizeof(afs_int32) * entries);
153     if (!alist->idlist_val) {
154         return ENOMEM;
155     }
156
157     for (i = 0, index = 0; index < ID_HASH_SIZE; index++) {
158         for (chain = idhash->hash[index]; chain; chain = chain->next) {
159             if (chain->id < 0) {
160                 if (select & PRGROUPS) {
161                     alist->idlist_val[i++] = chain->id;
162                 }
163             } else {
164                 if (select & PRUSERS) {
165                     alist->idlist_val[i++] = chain->id;
166                 }
167             }
168         }
169     }
170     return 0;
171 }
172
173 afs_int32
174 pr_Initialize(IN afs_int32 secLevel, IN const char *confDir, IN char *cell)
175 {
176     afs_int32 code;
177     struct rx_connection *serverconns[MAXSERVERS];
178     struct rx_securityClass *sc = NULL;
179     static struct afsconf_dir *tdir = (struct afsconf_dir *)NULL;       /* only do this once */
180     static char tconfDir[100] = "";
181     static char tcell[64] = "";
182     afs_int32 scIndex;
183     afs_int32 secFlags;
184     static struct afsconf_cell info;
185     afs_int32 i;
186 #if !defined(UKERNEL)
187     char cellstr[64];
188 #endif
189     afs_int32 gottdir = 0;
190     afs_int32 refresh = 0;
191
192     initialize_PT_error_table();
193     initialize_RXK_error_table();
194     initialize_ACFG_error_table();
195     initialize_KTC_error_table();
196
197 #if defined(UKERNEL)
198     if (!cell) {
199         cell = afs_LclCellName;
200     }
201 #else /* defined(UKERNEL) */
202     if (!cell) {
203         if (!tdir)
204             tdir = afsconf_Open(confDir);
205         if (!tdir) {
206             if (confDir && strcmp(confDir, ""))
207                 fprintf(stderr,
208                         "%s: Could not open configuration directory: %s.\n",
209                         whoami, confDir);
210             else
211                 fprintf(stderr,
212                         "%s: No configuration directory specified.\n",
213                         whoami);
214             return -1;
215         }
216         gottdir = 1;
217
218         code = afsconf_GetLocalCell(tdir, cellstr, sizeof(cellstr));
219         if (code) {
220             fprintf(stderr,
221                      "libprot: Could not get local cell. [%d]\n", code);
222             return code;
223         }
224         cell = cellstr;
225     }
226 #endif /* defined(UKERNEL) */
227
228     if (tdir == NULL || strcmp(confDir, tconfDir) || strcmp(cell, tcell)) {
229         /*
230          * force re-evaluation.  we either don't have an afsconf_dir,
231          * the directory has changed or the cell has changed.
232          */
233         if (tdir && !gottdir) {
234             afsconf_Close(tdir);
235             tdir = (struct afsconf_dir *)NULL;
236         }
237         pruclient = (struct ubik_client *)NULL;
238         refresh = 1;
239     }
240
241     if (refresh) {
242         strncpy(tconfDir, confDir, sizeof(tconfDir));
243         strncpy(tcell, cell, sizeof(tcell));
244
245 #if defined(UKERNEL)
246         tdir = afs_cdir;
247 #else /* defined(UKERNEL) */
248         if (!gottdir)
249             tdir = afsconf_Open(confDir);
250         if (!tdir) {
251             if (confDir && strcmp(confDir, ""))
252                 fprintf(stderr,
253                         "libprot: Could not open configuration directory: %s.\n",
254                         confDir);
255             else
256                 fprintf(stderr,
257                         "libprot: No configuration directory specified.\n");
258             return -1;
259         }
260 #endif /* defined(UKERNEL) */
261
262         code = afsconf_GetCellInfo(tdir, cell, "afsprot", &info);
263         if (code) {
264             fprintf(stderr, "libprot: Could not locate cell %s in %s/%s\n",
265                     cell, confDir, AFSDIR_CELLSERVDB_FILE);
266             return code;
267         }
268     }
269
270     /* If we already have a client and it is at the security level we
271      * want, don't get a new one. Unless the security level is 2 in
272      * which case we will get one (and re-read the key file).
273      */
274     if (pruclient && (lastLevel == secLevel) && (secLevel != 2)) {
275         return 0;
276     }
277
278     code = rx_Init(0);
279     if (code) {
280         fprintf(stderr, "libprot:  Could not initialize rx.\n");
281         return code;
282     }
283
284     /* Most callers use secLevel==1, however, the fileserver uses secLevel==2
285      * to force use of the KeyFile.  secLevel == 0 implies -noauth was
286      * specified. */
287     if (secLevel == 2) {
288         /* If secLevel is two assume we're on a file server and use
289          * ClientAuthSecure if possible. */
290         code = afsconf_ClientAuthSecure(tdir, &sc, &scIndex);
291         if (code)
292             afs_com_err(whoami, code, "(calling client secure)\n");
293     } else if (secLevel > 0) {
294         secFlags = 0;
295         if (secLevel > 1)
296             secFlags |= AFSCONF_SECOPTS_ALWAYSENCRYPT;
297
298         code = afsconf_ClientAuthToken(&info, secFlags, &sc, &scIndex, NULL);
299         if (code) {
300             afs_com_err(whoami, code, "(getting token)");
301             if (secLevel > 1)
302                 return code;
303         }
304     }
305
306     if (sc == NULL) {
307         sc = rxnull_NewClientSecurityObject();
308         scIndex = RX_SECIDX_NULL;
309     }
310
311     if ((scIndex == RX_SECIDX_NULL) && (secLevel != 0))
312         fprintf(stderr,
313                 "%s: Could not get afs tokens, running unauthenticated\n",
314                 whoami);
315
316     memset(serverconns, 0, sizeof(serverconns));        /* terminate list!!! */
317     for (i = 0; i < info.numServers; i++)
318         serverconns[i] =
319             rx_NewConnection(info.hostAddr[i].sin_addr.s_addr,
320                              info.hostAddr[i].sin_port, PRSRV, sc,
321                              scIndex);
322
323     code = ubik_ClientInit(serverconns, &pruclient);
324     if (code) {
325         afs_com_err(whoami, code, "ubik client init failed.");
326         return code;
327     }
328     lastLevel = scIndex;
329
330     code = rxs_Release(sc);
331     return code;
332 }
333
334 int
335 pr_End(void)
336 {
337     int code = 0;
338
339     if (pruclient) {
340         code = ubik_ClientDestroy(pruclient);
341         pruclient = 0;
342     }
343     return code;
344 }
345
346
347
348 int
349 pr_CreateUser(prname name, afs_int32 *id)
350 {
351     afs_int32 code;
352
353     stolower(name);
354     if (*id) {
355         code = ubik_PR_INewEntry(pruclient, 0, name, *id, 0);
356         return code;
357     } else {
358         code = ubik_PR_NewEntry(pruclient, 0, name, 0, 0, id);
359         return code;
360     }
361
362 }
363
364 int
365 pr_CreateGroup(prname name, prname owner, afs_int32 *id)
366 {
367     afs_int32 code;
368     afs_int32 oid = 0;
369     afs_int32 flags = 0;
370
371     stolower(name);
372     if (owner) {
373         code = pr_SNameToId(owner, &oid);
374         if (code)
375             return code;
376         if (oid == ANONYMOUSID)
377             return PRNOENT;
378     }
379     flags |= PRGRP;
380     if (*id) {
381         code = ubik_PR_INewEntry(pruclient, 0, name, *id, oid);
382         return code;
383     } else {
384         code = ubik_PR_NewEntry(pruclient, 0, name, flags, oid, id);
385         return code;
386     }
387 }
388
389 int
390 pr_Delete(char *name)
391 {
392     afs_int32 code;
393     afs_int32 id;
394
395     stolower(name);
396     code = pr_SNameToId(name, &id);
397     if (code)
398         return code;
399     if (id == ANONYMOUSID)
400         return PRNOENT;
401     code = ubik_PR_Delete(pruclient, 0, id);
402     return code;
403 }
404
405 int
406 pr_DeleteByID(afs_int32 id)
407 {
408     afs_int32 code;
409
410     code = ubik_PR_Delete(pruclient, 0, id);
411     return code;
412 }
413
414 int
415 pr_AddToGroup(char *user, char *group)
416 {
417     afs_int32 code;
418     namelist lnames;
419     idlist lids;
420
421     lnames.namelist_len = 2;
422     lnames.namelist_val = malloc(2 * PR_MAXNAMELEN);
423     strncpy(lnames.namelist_val[0], user, PR_MAXNAMELEN);
424     strncpy(lnames.namelist_val[1], group, PR_MAXNAMELEN);
425     lids.idlist_val = 0;
426     lids.idlist_len = 0;
427     code = pr_NameToId(&lnames, &lids);
428     if (code)
429         goto done;
430     /* if here, still could be missing an entry */
431     if (lids.idlist_val[0] == ANONYMOUSID
432         || lids.idlist_val[1] == ANONYMOUSID) {
433         code = PRNOENT;
434         goto done;
435     }
436     code =
437         ubik_PR_AddToGroup(pruclient, 0, lids.idlist_val[0],
438                   lids.idlist_val[1]);
439   done:
440     if (lnames.namelist_val)
441         free(lnames.namelist_val);
442
443     xdr_free((xdrproc_t) xdr_idlist, &lids);
444     return code;
445 }
446
447 int
448 pr_RemoveUserFromGroup(char *user, char *group)
449 {
450     afs_int32 code;
451     namelist lnames;
452     idlist lids;
453
454     lnames.namelist_len = 2;
455     lnames.namelist_val = malloc(2 * PR_MAXNAMELEN);
456     strncpy(lnames.namelist_val[0], user, PR_MAXNAMELEN);
457     strncpy(lnames.namelist_val[1], group, PR_MAXNAMELEN);
458     lids.idlist_val = 0;
459     lids.idlist_len = 0;
460     code = pr_NameToId(&lnames, &lids);
461     if (code)
462         goto done;
463
464     if (lids.idlist_val[0] == ANONYMOUSID
465         || lids.idlist_val[1] == ANONYMOUSID) {
466         code = PRNOENT;
467         goto done;
468     }
469     code =
470         ubik_PR_RemoveFromGroup(pruclient, 0, lids.idlist_val[0],
471                   lids.idlist_val[1]);
472   done:
473     if (lnames.namelist_val)
474         free(lnames.namelist_val);
475
476     xdr_free((xdrproc_t) xdr_idlist, &lids);
477
478     return code;
479 }
480
481 int
482 pr_NameToId(namelist *names, idlist *ids)
483 {
484     afs_int32 code;
485     afs_int32 i;
486
487     for (i = 0; i < names->namelist_len; i++)
488         stolower(names->namelist_val[i]);
489     code = ubik_PR_NameToID(pruclient, 0, names, ids);
490     return code;
491 }
492
493 int
494 pr_SNameToId(prname name, afs_int32 *id)
495 {
496     namelist lnames;
497     idlist lids;
498     afs_int32 code;
499
500     lids.idlist_len = 0;
501     lids.idlist_val = 0;
502     lnames.namelist_len = 1;
503     lnames.namelist_val = malloc(PR_MAXNAMELEN);
504     stolower(name);
505     strncpy(lnames.namelist_val[0], name, PR_MAXNAMELEN);
506     code = ubik_PR_NameToID(pruclient, 0, &lnames, &lids);
507     if (lids.idlist_val) {
508         *id = *lids.idlist_val;
509         xdr_free((xdrproc_t) xdr_idlist, &lids);
510     } else if (code == 0) {
511         code = PRINTERNAL;
512     }
513     if (lnames.namelist_val)
514         free(lnames.namelist_val);
515     return code;
516 }
517
518 int
519 pr_IdToName(idlist *ids, namelist *names)
520 {
521     afs_int32 code;
522
523     code = ubik_PR_IDToName(pruclient, 0, ids, names);
524     return code;
525 }
526
527 int
528 pr_SIdToName(afs_int32 id, prname name)
529 {
530     namelist lnames;
531     idlist lids;
532     afs_int32 code;
533
534     lids.idlist_len = 1;
535     lids.idlist_val = malloc(sizeof(afs_int32));
536     *lids.idlist_val = id;
537     lnames.namelist_len = 0;
538     lnames.namelist_val = 0;
539     code = ubik_PR_IDToName(pruclient, 0, &lids, &lnames);
540
541     if (lnames.namelist_val)
542         strncpy(name, lnames.namelist_val[0], PR_MAXNAMELEN);
543     else if (code == 0)
544         code = PRINTERNAL;
545
546     if (lids.idlist_val)
547         free(lids.idlist_val);
548
549     xdr_free((xdrproc_t) xdr_namelist, &lnames);
550
551     return code;
552 }
553
554 int
555 pr_GetCPS(afs_int32 id, prlist *CPS)
556 {
557     afs_int32 code;
558     afs_int32 over;
559
560     over = 0;
561     code = ubik_PR_GetCPS(pruclient, 0, id, CPS, &over);
562     if (code != PRSUCCESS)
563         return code;
564     if (over) {
565         /* do something about this, probably make a new call */
566         /* don't forget there's a hard limit in the interface */
567         fprintf(stderr, "membership list for id %d exceeds display limit\n",
568                 id);
569     }
570     return 0;
571 }
572
573 int
574 pr_GetCPS2(afs_int32 id, afs_uint32 host, prlist *CPS)
575 {
576     afs_int32 code;
577     afs_int32 over;
578
579     over = 0;
580     code = ubik_PR_GetCPS2(pruclient, 0, id, host, CPS, &over);
581     if (code != PRSUCCESS)
582         return code;
583     if (over) {
584         /* do something about this, probably make a new call */
585         /* don't forget there's a hard limit in the interface */
586         fprintf(stderr, "membership list for id %d exceeds display limit\n",
587                 id);
588     }
589     return 0;
590 }
591
592 int
593 pr_GetHostCPS(afs_uint32 host, prlist *CPS)
594 {
595     afs_int32 code;
596     afs_int32 over;
597
598     over = 0;
599     code = ubik_PR_GetHostCPS(pruclient, 0, host, CPS, &over);
600     if (code != PRSUCCESS)
601         return code;
602     if (over) {
603         /* do something about this, probably make a new call */
604         /* don't forget there's a hard limit in the interface */
605         fprintf(stderr,
606                 "membership list for host id %d exceeds display limit\n",
607                 host);
608     }
609     return 0;
610 }
611
612 int
613 pr_ListMembers(char *group, namelist *lnames)
614 {
615     afs_int32 code;
616     afs_int32 gid;
617
618     code = pr_SNameToId(group, &gid);
619     if (code)
620         return code;
621     if (gid == ANONYMOUSID)
622         return PRNOENT;
623     code = pr_IDListMembers(gid, lnames);
624     return code;
625 }
626
627 int
628 pr_ListOwned(afs_int32 oid, namelist *lnames, afs_int32 *moreP)
629 {
630     afs_int32 code;
631     prlist alist;
632     idlist *lids;
633
634     alist.prlist_len = 0;
635     alist.prlist_val = 0;
636     code = ubik_PR_ListOwned(pruclient, 0, oid, &alist, moreP);
637     if (code)
638         return code;
639     if (*moreP == 1) {
640         /* Remain backwards compatible when moreP was a T/F bit */
641         fprintf(stderr, "membership list for id %d exceeds display limit\n",
642                 oid);
643         *moreP = 0;
644     }
645     lids = (idlist *) &alist;
646     code = pr_IdToName(lids, lnames);
647
648     xdr_free((xdrproc_t) xdr_prlist, &alist);
649
650     if (code)
651         return code;
652
653     return PRSUCCESS;
654 }
655
656 int
657 pr_IDListMembers(afs_int32 gid, namelist *lnames)
658 {
659     afs_int32 code;
660     prlist alist;
661     idlist *lids;
662     afs_int32 over;
663
664     alist.prlist_len = 0;
665     alist.prlist_val = 0;
666     code = ubik_PR_ListElements(pruclient, 0, gid, &alist, &over);
667     if (code)
668         return code;
669     if (over) {
670         fprintf(stderr, "membership list for id %d exceeds display limit\n",
671                 gid);
672     }
673     lids = (idlist *) &alist;
674     code = pr_IdToName(lids, lnames);
675
676     xdr_free((xdrproc_t) xdr_prlist, &alist);
677
678     if (code)
679         return code;
680     return PRSUCCESS;
681 }
682
683 int
684 pr_IDListExpandedMembers(afs_int32 aid, namelist * lnames)
685 {
686     afs_int32 code;
687     afs_int32 gid;
688     idlist lids;
689     prlist alist;
690     afs_int32 over;
691     struct idhash *members = NULL;
692     afs_int32 *stack = NULL;
693     afs_int32 maxstack = ID_STACK_SIZE;
694     int n = 0;                  /* number of ids stacked */
695     int i;
696     int firstpass = 1;
697
698     code = AllocateIdHash(&members);
699     if (code) {
700         return code;
701     }
702     stack = malloc(sizeof(afs_int32) * maxstack);
703     if (!stack) {
704         code = ENOMEM;
705         goto done;
706     }
707
708     stack[n++] = aid;
709     while (n) {
710         gid = stack[--n];       /* pop next group id */
711         alist.prlist_len = 0;
712         alist.prlist_val = NULL;
713         if (firstpass || aid < 0) {
714             firstpass = 0;
715             code = ubik_PR_ListElements(pruclient, 0, gid, &alist, &over);
716         } else {
717             code = ubik_PR_ListSuperGroups(pruclient, 0, gid, &alist, &over);
718             if (code == RXGEN_OPCODE) {
719                 alist.prlist_len = 0;
720                 alist.prlist_val = NULL;
721                 code = 0; /* server does not support supergroups. */
722             }
723         }
724         if (code)
725             goto done;
726         if (over) {
727             fprintf(stderr,
728                     "membership list for id %d exceeds display limit\n", gid);
729         }
730         for (i = 0; i < alist.prlist_len; i++) {
731             afs_int32 found;
732             afs_int32 id;
733
734             id = alist.prlist_val[i];
735             found = FindId(members, id);
736             if (found < 0) {
737                 code = found;
738                 xdr_free((xdrproc_t) xdr_prlist, &alist);
739                 goto done;
740             }
741             if (found == 0 && id < 0) {
742                 if (n == maxstack) {    /* need more stack space */
743                     afs_int32 *tmp;
744                     maxstack += n;
745                     tmp = realloc(stack, maxstack * sizeof(afs_int32));
746                     if (!tmp) {
747                         code = ENOMEM;
748                         xdr_free((xdrproc_t) xdr_prlist, &alist);
749                         goto done;
750                     }
751                     stack = tmp;
752                 }
753                 stack[n++] = id;        /* push group id */
754             }
755         }
756         xdr_free((xdrproc_t) xdr_prlist, &alist);
757     }
758
759     code = CreateIdList(members, &lids, (aid < 0 ? PRUSERS : PRGROUPS));
760     if (code) {
761         goto done;
762     }
763     code = pr_IdToName(&lids, lnames);
764     if (lids.idlist_len)
765         free(lids.idlist_val);
766
767   done:
768     if (stack)
769         free(stack);
770     if (members)
771         FreeIdHash(members);
772     return code;
773 }
774
775 int
776 pr_ListEntry(afs_int32 id, struct prcheckentry *aentry)
777 {
778     afs_int32 code;
779
780     code = ubik_PR_ListEntry(pruclient, 0, id, aentry);
781     return code;
782 }
783
784 afs_int32
785 pr_ListEntries(int flag, afs_int32 startindex, afs_int32 *nentries, struct prlistentries **entries, afs_int32 *nextstartindex)
786 {
787     afs_int32 code;
788     prentries bulkentries;
789
790     *nentries = 0;
791     *entries = NULL;
792     *nextstartindex = -1;
793     bulkentries.prentries_val = 0;
794     bulkentries.prentries_len = 0;
795
796     code =
797         ubik_PR_ListEntries(pruclient, 0, flag, startindex,
798                   &bulkentries, nextstartindex);
799     *nentries = bulkentries.prentries_len;
800     *entries = bulkentries.prentries_val;
801     return code;
802 }
803
804 int
805 pr_CheckEntryByName(char *name, afs_int32 *id, char *owner, char *creator)
806 {
807     /* struct prcheckentry returns other things, which aren't useful to show at this time. */
808     afs_int32 code;
809     struct prcheckentry aentry;
810
811     code = pr_SNameToId(name, id);
812     if (code)
813         return code;
814     if (*id == ANONYMOUSID)
815         return PRNOENT;
816     code = ubik_PR_ListEntry(pruclient, 0, *id, &aentry);
817     if (code)
818         return code;
819     /* this should be done in one RPC, but I'm lazy. */
820     code = pr_SIdToName(aentry.owner, owner);
821     if (code)
822         return code;
823     code = pr_SIdToName(aentry.creator, creator);
824     if (code)
825         return code;
826     return PRSUCCESS;
827 }
828
829 int
830 pr_CheckEntryById(char *name, afs_int32 id, char *owner, char *creator)
831 {
832     /* struct prcheckentry returns other things, which aren't useful to show at this time. */
833     afs_int32 code;
834     struct prcheckentry aentry;
835
836     code = pr_SIdToName(id, name);
837     if (code)
838         return code;
839     if (id == ANONYMOUSID)
840         return PRNOENT;
841     code = ubik_PR_ListEntry(pruclient, 0, id, &aentry);
842     if (code)
843         return code;
844     /* this should be done in one RPC, but I'm lazy. */
845     code = pr_SIdToName(aentry.owner, owner);
846     if (code)
847         return code;
848     code = pr_SIdToName(aentry.creator, creator);
849     if (code)
850         return code;
851     return PRSUCCESS;
852 }
853
854 int
855 pr_ChangeEntry(char *oldname, char *newname, afs_int32 *newid, char *newowner)
856 {
857     afs_int32 code;
858     afs_int32 id;
859     afs_int32 oid = 0;
860
861     code = pr_SNameToId(oldname, &id);
862     if (code)
863         return code;
864     if (id == ANONYMOUSID)
865         return PRNOENT;
866     if (newowner && *newowner) {
867         code = pr_SNameToId(newowner, &oid);
868         if (code)
869             return code;
870         if (oid == ANONYMOUSID)
871             return PRNOENT;
872     }
873     if (newid)
874         code = ubik_PR_ChangeEntry(pruclient, 0, id, newname, oid, *newid);
875     else
876         code = ubik_PR_ChangeEntry(pruclient, 0, id, newname, oid, 0);
877     return code;
878 }
879
880 int
881 pr_IsAMemberOf(char *uname, char *gname, afs_int32 *flag)
882 {
883     afs_int32 code;
884     namelist lnames;
885     idlist lids;
886
887     stolower(uname);
888     stolower(gname);
889     lnames.namelist_len = 2;
890     lnames.namelist_val = malloc(2 * PR_MAXNAMELEN);
891     strncpy(lnames.namelist_val[0], uname, PR_MAXNAMELEN);
892     strncpy(lnames.namelist_val[1], gname, PR_MAXNAMELEN);
893     lids.idlist_val = 0;
894     lids.idlist_len = 0;
895     code = pr_NameToId(&lnames, &lids);
896     if (code) {
897         if (lnames.namelist_val)
898             free(lnames.namelist_val);
899         xdr_free((xdrproc_t) xdr_idlist, &lids);
900         return code;
901     }
902     code =
903         ubik_PR_IsAMemberOf(pruclient, 0, lids.idlist_val[0],
904                   lids.idlist_val[1], flag);
905     if (lnames.namelist_val)
906         free(lnames.namelist_val);
907     xdr_free((xdrproc_t) xdr_idlist, &lids);
908     return code;
909 }
910
911 int
912 pr_ListMaxUserId(afs_int32 *mid)
913 {
914     afs_int32 code;
915     afs_int32 gid;
916     code = ubik_PR_ListMax(pruclient, 0, mid, &gid);
917     return code;
918 }
919
920 int
921 pr_SetMaxUserId(afs_int32 mid)
922 {
923     afs_int32 code;
924     afs_int32 flag = 0;
925     code = ubik_PR_SetMax(pruclient, 0, mid, flag);
926     return code;
927 }
928
929 int
930 pr_ListMaxGroupId(afs_int32 *mid)
931 {
932     afs_int32 code;
933     afs_int32 id;
934     code = ubik_PR_ListMax(pruclient, 0, &id, mid);
935     return code;
936 }
937
938 int
939 pr_SetMaxGroupId(afs_int32 mid)
940 {
941     afs_int32 code;
942     afs_int32 flag = 0;
943
944     flag |= PRGRP;
945     code = ubik_PR_SetMax(pruclient, 0, mid, flag);
946     return code;
947 }
948
949 afs_int32
950 pr_SetFieldsEntry(afs_int32 id, afs_int32 mask, afs_int32 flags, afs_int32 ngroups, afs_int32 nusers)
951 {
952     afs_int32 code;
953
954     code =
955         ubik_PR_SetFieldsEntry(pruclient, 0, id, mask, flags, ngroups,
956                   nusers, 0, 0);
957     return code;
958 }
959
960 int
961 pr_ListSuperGroups(afs_int32 gid, namelist * lnames)
962 {
963     afs_int32 code;
964     prlist alist;
965     idlist *lids;
966     afs_int32 over;
967
968     alist.prlist_len = 0;
969     alist.prlist_val = 0;
970     code = ubik_PR_ListSuperGroups(pruclient, 0, gid, &alist, &over);
971     if (code)
972         return code;
973     if (over) {
974         fprintf(stderr, "supergroup list for id %d exceeds display limit\n",
975                 gid);
976     }
977     lids = (idlist *) & alist;
978     code = pr_IdToName(lids, lnames);
979
980     xdr_free((xdrproc_t) xdr_prlist, &alist);
981     return code;
982 }