55ed4905cb230ef8741c20f25f1be8967df84e3b
[openafs.git] / src / ptserver / ptprocs.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 /*
11  *                      (3) function addToGroup
12  *
13  *                          1. Eliminate the code that tests for adding groups
14  *                             to groups. This is an error in normal AFS.
15  *                          2. If adding a group to a group call AddToSGEntry
16  *                             to add the id of the group it's a member of.
17  *
18  *                      (4) function Delete
19  *
20  *                          1. Print a messsage if an error is returned from
21  *                             FindByID() and PTDEBUG is defined.
22  *                          2. If removing a group from a group call
23  *                             RemoveFromSGEntry to remove the id of the
24  *                             group it's a member of.
25  *                          3. Remove supergroup continuation records.
26  *
27  *                      (5) function RemoveFromGroup
28  *
29  *                          1. Eliminate the code that tests for adding groups
30  *                             to groups. This is an error in normal AFS.
31  *                          2. If removing a group from a group call
32  *                             RemoveFromSGEntry to remove the id of the
33  *                             group it's a member of.
34  *
35  *                      (6) Add new functions PR_ListSuperGroups and
36  *                          listSuperGroups.
37  *
38  *                      (7) function isAMemberOf
39  *
40  *                          1. Allow groups to be members of groups.
41  *
42  *                      Transarc does not currently use opcodes past 520, but
43  *                      they *could* decide at any time to use more opcodes.
44  *                      If they did, then one part of our local mods,
45  *                      ListSupergroups, would break.  I've therefore
46  *                      renumbered it to 530, and put logic in to enable the
47  *                      old opcode to work (for now).
48  */
49
50 #include <afsconfig.h>
51 #include <afs/param.h>
52 #include <afs/stds.h>
53
54 #include <roken.h>
55 #include <afs/opr.h>
56
57 #include <ctype.h>
58
59 #include <lock.h>
60 #include <afs/afsutil.h>
61 #include <ubik.h>
62 #include <rx/xdr.h>
63 #include <rx/rx.h>
64 #include <rx/rxkad.h>
65 #include <afs/auth.h>
66 #include <afs/cellconfig.h>
67
68 #include "ptserver.h"
69 #include "pterror.h"
70 #include "ptprototypes.h"
71 #include "afs/audit.h"
72
73 extern int restricted;
74 extern struct ubik_dbase *dbase;
75 extern int pr_noAuth;
76 extern int prp_group_default;
77 extern int prp_user_default;
78 extern struct afsconf_dir *prdir;
79
80 static afs_int32 iNewEntry(struct rx_call *call, char aname[], afs_int32 aid,
81                            afs_int32 oid, afs_int32 *cid);
82 static afs_int32 newEntry(struct rx_call *call, char aname[], afs_int32 flag,
83                           afs_int32 oid, afs_int32 *aid, afs_int32 *cid);
84 static afs_int32 whereIsIt(struct rx_call *call, afs_int32 aid, afs_int32 *apos,
85                            afs_int32 *cid);
86 static afs_int32 dumpEntry(struct rx_call *call, afs_int32 apos,
87                            struct prdebugentry *aentry, afs_int32 *cid);
88 static afs_int32 addToGroup(struct rx_call *call, afs_int32 aid, afs_int32 gid,
89                             afs_int32 *cid);
90 static afs_int32 nameToID(struct rx_call *call, namelist *aname, idlist *aid);
91 static afs_int32 idToName(struct rx_call *call, idlist *aid, namelist *aname);
92 static afs_int32 Delete(struct rx_call *call, afs_int32 aid, afs_int32 *cid);
93 static afs_int32 UpdateEntry(struct rx_call *call, afs_int32 aid, char *name,
94                              struct PrUpdateEntry *uentry, afs_int32 *cid);
95 static afs_int32 removeFromGroup(struct rx_call *call, afs_int32 aid,
96                                  afs_int32 gid, afs_int32 *cid);
97 static afs_int32 getCPS(struct rx_call *call, afs_int32 aid, prlist *alist,
98                         afs_int32 *over, afs_int32 *cid);
99 static afs_int32 getCPS2(struct rx_call *call, afs_int32 aid, afs_uint32 ahost,
100                          prlist *alist, afs_int32 *over, afs_int32 *cid);
101 static afs_int32 getHostCPS(struct rx_call *call, afs_uint32 ahost,
102                             prlist *alist, afs_int32 *over);
103 static afs_int32 listMax(struct rx_call *call, afs_int32 *uid, afs_int32 *gid);
104 static afs_int32 setMax(struct rx_call *call, afs_int32 aid, afs_int32 gflag,
105                         afs_int32 *cid);
106 static afs_int32 listEntry(struct rx_call *call, afs_int32 aid,
107                            struct prcheckentry *aentry, afs_int32 *cid);
108 static afs_int32 listEntries(struct rx_call *call, afs_int32 flag,
109                              afs_int32 startindex, prentries *bulkentries,
110                              afs_int32 *nextstartindex, afs_int32 *cid);
111 static afs_int32 put_prentries(struct prentry *tentry, prentries *bulkentries);
112 static afs_int32 changeEntry(struct rx_call *call, afs_int32 aid, char *name,
113                              afs_int32 oid, afs_int32 newid, afs_int32 *cid);
114 static afs_int32 setFieldsEntry(struct rx_call *call, afs_int32 id,
115                                 afs_int32 mask, afs_int32 flags,
116                                 afs_int32 ngroups, afs_int32 nusers,
117                                 afs_int32 spare1, afs_int32 spare2,
118                                 afs_int32 *cid);
119 static afs_int32 listElements(struct rx_call *call, afs_int32 aid,
120                               prlist *alist, afs_int32 *over, afs_int32 *cid);
121 #if defined(SUPERGROUPS)
122 static afs_int32 listSuperGroups(struct rx_call *call, afs_int32 aid,
123                                  prlist *alist, afs_int32 *over,
124                                  afs_int32 *cid);
125 #endif
126 static afs_int32 listOwned(struct rx_call *call, afs_int32 aid, prlist *alist,
127                            afs_int32 *lastP, afs_int32 *cid);
128 static afs_int32 isAMemberOf(struct rx_call *call, afs_int32 uid, afs_int32 gid,
129                              afs_int32 *flag, afs_int32 *cid);
130 static afs_int32 addWildCards(struct ubik_trans *tt, prlist *alist,
131                               afs_uint32 host);
132 static afs_int32 WhoIsThisWithName(struct rx_call *acall,
133                                    struct ubik_trans *at, afs_int32 *aid,
134                                    char *aname);
135
136 /* when we abort, the ubik cachedVersion will be reset, so we'll read in the
137  * header on the next call.
138  * Abort the transaction and return the code.
139  */
140 #define ABORT_WITH(tt,code) return(ubik_AbortTrans(tt),code)
141
142 static int
143 CreateOK(struct ubik_trans *ut, afs_int32 cid, afs_int32 oid, afs_int32 flag,
144          int admin)
145 {
146     if (restricted && !admin)
147         return 0;
148
149     if (flag & PRFOREIGN) {
150         /* Foreign users are recognized by the '@' sign and
151          * not by the PRFOREIGN flag.
152          */
153         return 0;
154     } else if (flag & PRGRP) {
155         /* Allow anonymous group creation only if owner specified
156          * and running noAuth.
157          */
158         if (cid == ANONYMOUSID) {
159             if ((oid == 0) || !pr_noAuth)
160                 return 0;
161         }
162     } else {                    /* creating a user */
163         if (!admin && !pr_noAuth)
164             return 0;
165     }
166     return 1;                   /* OK! */
167 }
168
169 afs_int32
170 WhoIsThis(struct rx_call *acall, struct ubik_trans *at, afs_int32 *aid)
171 {
172     int code = WhoIsThisWithName(acall, at, aid, NULL);
173     if (code == 2 && *aid == ANONYMOUSID)
174         return PRNOENT;
175     return code;
176 }
177
178 static int
179 WritePreamble(struct ubik_trans **tt)
180 {
181     int code;
182
183     code = Initdb();
184     if (code)
185         return code;
186
187     code = ubik_BeginTrans(dbase, UBIK_WRITETRANS, tt);
188     if (code)
189         return code;
190
191     code = ubik_SetLock(*tt, 1, 1, LOCKWRITE);
192     if (code)
193         goto out;
194
195     code = read_DbHeader(*tt);
196
197 out:
198     if (code)
199         ubik_AbortTrans(*tt);
200
201     return code;
202 }
203
204 static int
205 ReadPreamble(struct ubik_trans **tt)
206 {
207     int code;
208
209     code = Initdb();
210     if (code)
211         return code;
212
213     code = ubik_BeginTransReadAny(dbase, UBIK_READTRANS, tt);
214     if (code)
215         return code;
216
217     code = ubik_SetLock(*tt, 1, 1, LOCKREAD);
218     if (code)
219         goto out;
220
221     code = read_DbHeader(*tt);
222
223 out:
224     if (code)
225         ubik_AbortTrans(*tt);
226
227     return code;
228 }
229
230 afs_int32
231 SPR_INewEntry(struct rx_call *call, char aname[], afs_int32 aid, afs_int32 oid)
232 {
233     afs_int32 code;
234     afs_int32 cid = ANONYMOUSID;
235
236     code = iNewEntry(call, aname, aid, oid, &cid);
237     osi_auditU(call, PTS_INewEntEvent, code, AUD_ID, aid, AUD_STR, aname,
238                AUD_ID, oid, AUD_END);
239     ViceLog(5, ("PTS_INewEntry: code %d cid %d aid %d aname %s oid %d\n", code, cid, aid, aname, oid));
240     return code;
241 }
242
243 static afs_int32
244 iNewEntry(struct rx_call *call, char aname[], afs_int32 aid, afs_int32 oid,
245           afs_int32 *cid)
246 {
247     /* used primarily for conversion - not intended to be used as usual means
248      * of entering people into the database. */
249     struct ubik_trans *tt;
250     afs_int32 code;
251     afs_int32 gflag = 0;
252     int admin;
253
254     stolower(aname);
255
256     code = WritePreamble(&tt);
257     if (code)
258         return code;
259
260     code = WhoIsThis(call, tt, cid);
261     if (code)
262         ABORT_WITH(tt, PRPERM);
263     admin = IsAMemberOf(tt, *cid, SYSADMINID);
264
265     /* first verify the id is good */
266     if (aid == 0)
267         ABORT_WITH(tt, PRPERM);
268     if (aid < 0) {
269         gflag |= PRGRP;
270         /* only sysadmin can reuse a group id */
271         if (!admin && !pr_noAuth && (aid != ntohl(cheader.maxGroup) - 1))
272             ABORT_WITH(tt, PRPERM);
273     }
274     if (FindByID(tt, aid))
275         ABORT_WITH(tt, PRIDEXIST);
276
277     /* check a few other things */
278     if (!CreateOK(tt, *cid, oid, gflag, admin))
279         ABORT_WITH(tt, PRPERM);
280
281     code = CreateEntry(tt, aname, &aid, 1, gflag, oid, *cid);
282     if (code != PRSUCCESS)
283         ABORT_WITH(tt, code);
284
285     /* finally, commit transaction */
286     code = ubik_EndTrans(tt);
287     if (code)
288         return code;
289     return PRSUCCESS;
290 }
291
292
293 afs_int32
294 SPR_NewEntry(struct rx_call *call, char aname[], afs_int32 flag, afs_int32 oid,
295              afs_int32 *aid)
296 {
297     afs_int32 code;
298     afs_int32 cid = ANONYMOUSID;
299
300     code = newEntry(call, aname, flag, oid, aid, &cid);
301     osi_auditU(call, PTS_NewEntEvent, code, AUD_ID, *aid, AUD_STR, aname,
302                AUD_ID, oid, AUD_END);
303     ViceLog(5, ("PTS_NewEntry: code %d cid %d aid %d aname %s oid %d\n", code, cid, *aid, aname, oid));
304     return code;
305 }
306
307 static afs_int32
308 newEntry(struct rx_call *call, char aname[], afs_int32 flag, afs_int32 oid,
309          afs_int32 *aid, afs_int32 *cid)
310 {
311     afs_int32 code;
312     struct ubik_trans *tt;
313     int admin;
314     char cname[PR_MAXNAMELEN];
315     stolower(aname);
316
317     code = WritePreamble(&tt);
318     if (code)
319         return code;
320
321     /* this is for cross-cell self registration. It is not added in the
322      * SPR_INewEntry because we want self-registration to only do
323      * automatic id assignment.
324      */
325     code = WhoIsThisWithName(call, tt, cid, cname);
326     if (code != 2) {            /* 2 specifies that this is a foreign cell request */
327         if (code)
328             ABORT_WITH(tt, PRPERM);
329         admin = IsAMemberOf(tt, *cid, SYSADMINID);
330     } else {
331         admin = ((!restricted && !strcmp(aname, cname))) || IsAMemberOf(tt, *cid, SYSADMINID);
332         oid = *cid = SYSADMINID;
333     }
334     if (!CreateOK(tt, *cid, oid, flag, admin))
335         ABORT_WITH(tt, PRPERM);
336
337     code = CreateEntry(tt, aname, aid, 0, flag, oid, *cid);
338     if (code != PRSUCCESS)
339         ABORT_WITH(tt, code);
340
341     code = ubik_EndTrans(tt);
342     if (code)
343         return code;
344     return PRSUCCESS;
345 }
346
347
348
349 afs_int32
350 SPR_WhereIsIt(struct rx_call *call, afs_int32 aid, afs_int32 *apos)
351 {
352     afs_int32 code;
353     afs_int32 cid = ANONYMOUSID;
354
355     code = whereIsIt(call, aid, apos, &cid);
356     osi_auditU(call, PTS_WheIsItEvent, code, AUD_ID, aid, AUD_LONG, *apos,
357                AUD_END);
358     ViceLog(125, ("PTS_WhereIsIt: code %d cid %d aid %d apos %d\n", code, cid, aid, *apos));
359     return code;
360 }
361
362 static afs_int32
363 whereIsIt(struct rx_call *call, afs_int32 aid, afs_int32 *apos, afs_int32 *cid)
364 {
365     afs_int32 code;
366     struct ubik_trans *tt;
367     afs_int32 temp;
368
369     code = ReadPreamble(&tt);
370     if (code)
371         return code;
372
373     code = WhoIsThis(call, tt, cid);
374     if (code)
375         ABORT_WITH(tt, PRPERM);
376
377     temp = FindByID(tt, aid);
378     if (!temp)
379         ABORT_WITH(tt, PRNOENT);
380     *apos = temp;
381     code = ubik_EndTrans(tt);
382     if (code)
383         return code;
384     return PRSUCCESS;
385 }
386
387
388 afs_int32
389 SPR_DumpEntry(struct rx_call *call, afs_int32 apos,
390               struct prdebugentry *aentry)
391 {
392     afs_int32 code;
393     afs_int32 cid = ANONYMOUSID;
394
395     code = dumpEntry(call, apos, aentry, &cid);
396     osi_auditU(call, PTS_DmpEntEvent, code, AUD_LONG, apos, AUD_END);
397     ViceLog(125, ("PTS_DumpEntry: code %d cid %d apos %d\n", code, cid, apos));
398     return code;
399 }
400
401 static afs_int32
402 dumpEntry(struct rx_call *call, afs_int32 apos, struct prdebugentry *aentry,
403           afs_int32 *cid)
404 {
405     afs_int32 code;
406     struct ubik_trans *tt;
407
408     code = ReadPreamble(&tt);
409     if (code)
410         return code;
411
412     code = WhoIsThis(call, tt, cid);
413     if (code)
414         ABORT_WITH(tt, PRPERM);
415     code = pr_ReadEntry(tt, 0, apos, (struct prentry *)aentry);
416     if (code)
417         ABORT_WITH(tt, code);
418
419     if (!AccessOK(tt, *cid, 0, PRP_STATUS_MEM, 0))
420         ABORT_WITH(tt, PRPERM);
421
422     /* Since prdebugentry is in the form of a prentry not a coentry, we will
423      * return the coentry slots in network order where the string is. */
424 #if 0
425     if (aentry->flags & PRCONT) {       /* wrong type, get coentry instead */
426         code = pr_ReadCoEntry(tt, 0, apos, aentry);
427         if (code)
428             ABORT_WITH(tt, code);
429     }
430 #endif
431     code = ubik_EndTrans(tt);
432     if (code)
433         return code;
434     return PRSUCCESS;
435 }
436
437 afs_int32
438 SPR_AddToGroup(struct rx_call *call, afs_int32 aid, afs_int32 gid)
439 {
440     afs_int32 code;
441     afs_int32 cid = ANONYMOUSID;
442
443     code = addToGroup(call, aid, gid, &cid);
444     osi_auditU(call, PTS_AdToGrpEvent, code, AUD_ID, gid, AUD_ID, aid,
445                AUD_END);
446     ViceLog(5, ("PTS_AddToGroup: code %d cid %d gid %d aid %d\n", code, cid, gid, aid));
447     return code;
448 }
449
450 static afs_int32
451 addToGroup(struct rx_call *call, afs_int32 aid, afs_int32 gid, afs_int32 *cid)
452 {
453     afs_int32 code;
454     struct ubik_trans *tt;
455     afs_int32 tempu;
456     afs_int32 tempg;
457     struct prentry tentry;
458     struct prentry uentry;
459
460     if (gid == ANYUSERID || gid == AUTHUSERID)
461         return PRPERM;
462     if (aid == ANONYMOUSID)
463         return PRPERM;
464
465     code = WritePreamble(&tt);
466     if (code)
467         return code;
468
469     code = WhoIsThis(call, tt, cid);
470     if (code)
471         ABORT_WITH(tt, PRPERM);
472     tempu = FindByID(tt, aid);
473     if (!tempu)
474         ABORT_WITH(tt, PRNOENT);
475     memset(&uentry, 0, sizeof(uentry));
476     code = pr_ReadEntry(tt, 0, tempu, &uentry);
477     if (code != 0)
478         ABORT_WITH(tt, code);
479
480 #if !defined(SUPERGROUPS)
481     /* we don't allow groups as members of groups at present */
482     if (uentry.flags & PRGRP)
483         ABORT_WITH(tt, PRNOTUSER);
484 #endif
485
486     tempg = FindByID(tt, gid);
487     if (!tempg)
488         ABORT_WITH(tt, PRNOENT);
489     code = pr_ReadEntry(tt, 0, tempg, &tentry);
490     if (code != 0)
491         ABORT_WITH(tt, code);
492     /* make sure that this is a group */
493     if (!(tentry.flags & PRGRP))
494         ABORT_WITH(tt, PRNOTGROUP);
495     if (!AccessOK(tt, *cid, &tentry, PRP_ADD_MEM, PRP_ADD_ANY))
496         ABORT_WITH(tt, PRPERM);
497
498     code = AddToEntry(tt, &tentry, tempg, aid);
499     if (code != PRSUCCESS)
500         ABORT_WITH(tt, code);
501
502 #if defined(SUPERGROUPS)
503     if (uentry.flags & PRGRP)
504         code = AddToSGEntry(tt, &uentry, tempu, gid);   /* mod group to be in sg */
505     else
506 #endif
507         /* now, modify the user's entry as well */
508         code = AddToEntry(tt, &uentry, tempu, gid);
509     if (code != PRSUCCESS)
510         ABORT_WITH(tt, code);
511     code = ubik_EndTrans(tt);
512     if (code)
513         return code;
514     return PRSUCCESS;
515 }
516
517 afs_int32
518 SPR_NameToID(struct rx_call *call, namelist *aname, idlist *aid)
519 {
520     afs_int32 code;
521
522     code = nameToID(call, aname, aid);
523     osi_auditU(call, PTS_NmToIdEvent, code, AUD_END);
524     ViceLog(125, ("PTS_NameToID: code %d\n", code));
525     return code;
526 }
527
528 static afs_int32
529 nameToID(struct rx_call *call, namelist *aname, idlist *aid)
530 {
531     afs_int32 code;
532     struct ubik_trans *tt;
533     afs_int32 i;
534     int size;
535     int count = 0;
536
537     /* Initialize return struct */
538     aid->idlist_len = 0;
539     aid->idlist_val = NULL;
540
541     size = aname->namelist_len;
542     if (size == 0)
543         return 0;
544     if (size < 0)
545         return PRTOOMANY;
546
547     aid->idlist_val = (afs_int32 *) malloc(size * sizeof(afs_int32));
548     if (!aid->idlist_val)
549         return PRNOMEM;
550
551     code = ReadPreamble(&tt);
552     if (code)
553         return code;
554
555     for (i = 0; i < aname->namelist_len; i++) {
556         char vname[256];
557         char *nameinst, *cell;
558         afs_int32 islocal = 1;
559
560         strncpy(vname, aname->namelist_val[i], sizeof(vname));
561         vname[sizeof(vname)-1] ='\0';
562
563         nameinst = vname;
564         cell = strchr(vname, '@');
565         if (cell) {
566             *cell = '\0';
567             cell++;
568         }
569
570         if (cell && *cell) {
571             code = afsconf_IsLocalRealmMatch(prdir, &islocal, nameinst, NULL, cell);
572             ViceLog(0,
573                     ("PTS_NameToID: afsconf_IsLocalRealmMatch(); code=%d, nameinst=%s, cell=%s\n",
574                      code, nameinst, cell));
575         }
576         if (islocal)
577             code = NameToID(tt, nameinst, &aid->idlist_val[i]);
578         else
579             code = NameToID(tt, aname->namelist_val[i], &aid->idlist_val[i]);
580
581         if (code != PRSUCCESS)
582             aid->idlist_val[i] = ANONYMOUSID;
583         osi_audit(PTS_NmToIdEvent, code, AUD_STR,
584                    aname->namelist_val[i], AUD_ID, aid->idlist_val[i],
585                    AUD_END);
586         ViceLog(125, ("PTS_NameToID: code %d aname %s aid %d\n", code,
587                       aname->namelist_val[i], aid->idlist_val[i]));
588         if (count++ > 50) {
589 #ifndef AFS_PTHREAD_ENV
590             IOMGR_Poll();
591 #endif
592             count = 0;
593         }
594     }
595     aid->idlist_len = aname->namelist_len;
596
597     code = ubik_EndTrans(tt);
598     if (code)
599         return code;
600     return PRSUCCESS;
601 }
602
603 /*
604  * SPR_IDToName
605  * Given an array of ids, find the name for each of them.
606  * The array of ids and names is unlimited.
607  */
608 afs_int32
609 SPR_IDToName(struct rx_call *call, idlist *aid, namelist *aname)
610 {
611     afs_int32 code;
612
613     code = idToName(call, aid, aname);
614     osi_auditU(call, PTS_IdToNmEvent, code, AUD_END);
615     ViceLog(125, ("PTS_IDToName: code %d\n", code));
616     return code;
617 }
618
619 static afs_int32
620 idToName(struct rx_call *call, idlist *aid, namelist *aname)
621 {
622     afs_int32 code;
623     struct ubik_trans *tt;
624     afs_int32 i;
625     int size;
626     int count = 0;
627
628     /* leave this first for rpc stub */
629     size = aid->idlist_len;
630     if (size == 0)
631         return 0;
632     if (size < 0)
633         return PRTOOMANY;
634     aname->namelist_val = (prname *) malloc(size * PR_MAXNAMELEN);
635     aname->namelist_len = 0;
636     if (aname->namelist_val == 0)
637         return PRNOMEM;
638     if (aid->idlist_len == 0)
639         return 0;
640     if (size == 0)
641         return PRTOOMANY;       /* rxgen will probably handle this */
642
643     code = ReadPreamble(&tt);
644     if (code)
645         return code;
646
647     for (i = 0; i < aid->idlist_len; i++) {
648         code = IDToName(tt, aid->idlist_val[i], aname->namelist_val[i]);
649         if (code != PRSUCCESS)
650             sprintf(aname->namelist_val[i], "%d", aid->idlist_val[i]);
651         osi_audit(PTS_IdToNmEvent, code, AUD_ID, aid->idlist_val[i],
652                   AUD_STR, aname->namelist_val[i], AUD_END);
653         ViceLog(125, ("PTS_idToName: code %d aid %d aname %s\n", code,
654                       aid->idlist_val[i], aname->namelist_val[i]));
655         if (count++ > 50) {
656 #ifndef AFS_PTHREAD_ENV
657             IOMGR_Poll();
658 #endif
659             count = 0;
660         }
661     }
662     aname->namelist_len = aid->idlist_len;
663
664     code = ubik_EndTrans(tt);
665     if (code)
666         return code;
667     return PRSUCCESS;
668 }
669
670 afs_int32
671 SPR_Delete(struct rx_call *call, afs_int32 aid)
672 {
673     afs_int32 code;
674     afs_int32 cid = ANONYMOUSID;
675
676     code = Delete(call, aid, &cid);
677     osi_auditU(call, PTS_DelEvent, code, AUD_ID, aid, AUD_END);
678     ViceLog(5, ("PTS_Delete: code %d cid %d aid %d\n", code, cid, aid));
679     return code;
680 }
681
682 static afs_int32
683 Delete(struct rx_call *call, afs_int32 aid, afs_int32 *cid)
684 {
685     afs_int32 code;
686     struct ubik_trans *tt;
687     struct prentry tentry;
688     afs_int32 loc, nptr;
689     int count;
690
691     if (aid == SYSADMINID || aid == ANYUSERID || aid == AUTHUSERID
692         || aid == ANONYMOUSID)
693         return PRPERM;
694
695     code = WritePreamble(&tt);
696     if (code)
697         return code;
698
699     code = WhoIsThis(call, tt, cid);
700     if (code)
701         ABORT_WITH(tt, PRPERM);
702
703     /* Read in entry to be deleted */
704     loc = FindByID(tt, aid);
705     if (loc == 0)
706         ABORT_WITH(tt, PRNOENT);
707     code = pr_ReadEntry(tt, 0, loc, &tentry);
708     if (code)
709         ABORT_WITH(tt, PRDBFAIL);
710
711     /* Do some access checking */
712     if (tentry.owner != *cid && !IsAMemberOf(tt, *cid, SYSADMINID)
713         && !IsAMemberOf(tt, *cid, tentry.owner) && !pr_noAuth)
714         ABORT_WITH(tt, PRPERM);
715
716     /* Delete each continuation block as a separate transaction so that no one
717      * transaction become to large to complete. */
718     nptr = tentry.next;
719     while (nptr != 0) {
720         struct contentry centry;
721         int i;
722
723         code = pr_ReadCoEntry(tt, 0, nptr, &centry);
724         if (code != 0)
725             ABORT_WITH(tt, PRDBFAIL);
726         for (i = 0; i < COSIZE; i++) {
727             if (centry.entries[i] == PRBADID)
728                 continue;
729             if (centry.entries[i] == 0)
730                 break;
731 #if defined(SUPERGROUPS)
732             if (aid < 0 && centry.entries[i] < 0)       /* Supergroup */
733                 code = RemoveFromSGEntry(tt, aid, centry.entries[i]);
734             else
735 #endif
736                 code = RemoveFromEntry(tt, aid, centry.entries[i]);
737             if (code)
738                 ABORT_WITH(tt, code);
739             tentry.count--;     /* maintain count */
740 #ifndef AFS_PTHREAD_ENV
741             if ((i & 3) == 0)
742                 IOMGR_Poll();
743 #endif
744         }
745         tentry.next = centry.next;      /* thread out this block */
746         code = FreeBlock(tt, nptr);     /* free continuation block */
747         if (code)
748             ABORT_WITH(tt, code);
749         code = pr_WriteEntry(tt, 0, loc, &tentry);      /* update main entry */
750         if (code)
751             ABORT_WITH(tt, code);
752
753         /* end this trans and start a new one */
754         code = ubik_EndTrans(tt);
755         if (code)
756             return code;
757 #ifndef AFS_PTHREAD_ENV
758         IOMGR_Poll();           /* just to keep the connection alive */
759 #endif
760         code = ubik_BeginTrans(dbase, UBIK_WRITETRANS, &tt);
761         if (code)
762             return code;
763         code = ubik_SetLock(tt, 1, 1, LOCKWRITE);
764         if (code)
765             ABORT_WITH(tt, code);
766
767         /* re-read entry to get consistent uptodate info */
768         loc = FindByID(tt, aid);
769         if (loc == 0)
770             ABORT_WITH(tt, PRNOENT);
771         code = pr_ReadEntry(tt, 0, loc, &tentry);
772         if (code)
773             ABORT_WITH(tt, PRDBFAIL);
774
775         nptr = tentry.next;
776     }
777
778 #if defined(SUPERGROUPS)
779     /* Delete each continuation block as a separate transaction
780      * so that no one transaction become too large to complete. */
781     {
782         struct prentryg *tentryg = (struct prentryg *)&tentry;
783         nptr = tentryg->nextsg;
784         while (nptr != 0) {
785             struct contentry centry;
786             int i;
787
788             code = pr_ReadCoEntry(tt, 0, nptr, &centry);
789             if (code != 0)
790                 ABORT_WITH(tt, PRDBFAIL);
791             for (i = 0; i < COSIZE; i++) {
792                 if (centry.entries[i] == PRBADID)
793                     continue;
794                 if (centry.entries[i] == 0)
795                     break;
796                 code = RemoveFromEntry(tt, aid, centry.entries[i]);
797                 if (code)
798                     ABORT_WITH(tt, code);
799                 tentryg->countsg--;     /* maintain count */
800 #ifndef AFS_PTHREAD_ENV
801                 if ((i & 3) == 0)
802                     IOMGR_Poll();
803 #endif
804             }
805             tentryg->nextsg = centry.next;      /* thread out this block */
806             code = FreeBlock(tt, nptr); /* free continuation block */
807             if (code)
808                 ABORT_WITH(tt, code);
809             code = pr_WriteEntry(tt, 0, loc, &tentry);  /* update main entry */
810             if (code)
811                 ABORT_WITH(tt, code);
812
813             /* end this trans and start a new one */
814             code = ubik_EndTrans(tt);
815             if (code)
816                 return code;
817 #ifndef AFS_PTHREAD_ENV
818             IOMGR_Poll();       /* just to keep the connection alive */
819 #endif
820
821             code = ubik_BeginTrans(dbase, UBIK_WRITETRANS, &tt);
822             if (code)
823                 return code;
824             code = ubik_SetLock(tt, 1, 1, LOCKWRITE);
825             if (code)
826                 ABORT_WITH(tt, code);
827
828             /* re-read entry to get consistent uptodate info */
829             loc = FindByID(tt, aid);
830             if (loc == 0)
831                 ABORT_WITH(tt, PRNOENT);
832             code = pr_ReadEntry(tt, 0, loc, &tentry);
833             if (code)
834                 ABORT_WITH(tt, PRDBFAIL);
835
836             nptr = tentryg->nextsg;
837         }
838     }
839
840 #endif /* SUPERGROUPS */
841
842     /* Then move the owned chain, except possibly ourself to the orphan list.
843      * Because this list can be very long and so exceed the size of a ubik
844      * transaction, we start a new transaction every 50 entries. */
845     count = 0;
846     nptr = tentry.owned;
847     while (nptr != 0) {
848         struct prentry nentry;
849
850         code = pr_ReadEntry(tt, 0, nptr, &nentry);
851         if (code)
852             ABORT_WITH(tt, PRDBFAIL);
853         nptr = tentry.owned = nentry.nextOwned; /* thread out */
854
855         if (nentry.id != tentry.id) {   /* don't add us to orphan chain! */
856             code = AddToOrphan(tt, nentry.id);
857             if (code)
858                 ABORT_WITH(tt, code);
859             count++;
860 #ifndef AFS_PTHREAD_ENV
861             if ((count & 3) == 0)
862                 IOMGR_Poll();
863 #endif
864         }
865         if (count < 50)
866             continue;
867         code = pr_WriteEntry(tt, 0, loc, &tentry);      /* update main entry */
868         if (code)
869             ABORT_WITH(tt, code);
870
871         /* end this trans and start a new one */
872         code = ubik_EndTrans(tt);
873         if (code)
874             return code;
875 #ifndef AFS_PTHREAD_ENV
876         IOMGR_Poll();           /* just to keep the connection alive */
877 #endif
878         code = ubik_BeginTrans(dbase, UBIK_WRITETRANS, &tt);
879         if (code)
880             return code;
881         code = ubik_SetLock(tt, 1, 1, LOCKWRITE);
882         if (code)
883             ABORT_WITH(tt, code);
884
885         /* re-read entry to get consistent uptodate info */
886         loc = FindByID(tt, aid);
887         if (loc == 0)
888             ABORT_WITH(tt, PRNOENT);
889         code = pr_ReadEntry(tt, 0, loc, &tentry);
890         if (code)
891             ABORT_WITH(tt, PRDBFAIL);
892
893         nptr = tentry.owned;
894     }
895
896     /* now do what's left of the deletion stuff */
897     code = DeleteEntry(tt, &tentry, loc);
898     if (code != PRSUCCESS)
899         ABORT_WITH(tt, code);
900
901     code = ubik_EndTrans(tt);
902     if (code)
903         return code;
904     return PRSUCCESS;
905 }
906
907 afs_int32
908 SPR_UpdateEntry(struct rx_call *call, afs_int32 aid, char *name,
909                 struct PrUpdateEntry *uentry)
910 {
911     afs_int32 code;
912     afs_int32 cid = ANONYMOUSID;
913
914     code = UpdateEntry(call, aid, name, uentry, &cid);
915     osi_auditU(call, PTS_UpdEntEvent, code, AUD_ID, aid, AUD_STR, name, AUD_END);
916     ViceLog(5, ("PTS_UpdateEntry: code %d cid %d aid %d name %s\n", code, cid, aid, name));
917     return code;
918 }
919
920 afs_int32
921 UpdateEntry(struct rx_call *call, afs_int32 aid, char *name,
922             struct PrUpdateEntry *uentry, afs_int32 *cid)
923 {
924     afs_int32 code;
925     struct ubik_trans *tt;
926     struct prentry tentry;
927     afs_int32 loc;
928     int id = 0;
929
930     if (aid) {
931         id = aid;
932         if (aid == SYSADMINID || aid == ANYUSERID || aid == AUTHUSERID
933             || aid == ANONYMOUSID)
934             return PRPERM;
935     }
936
937     code = WritePreamble(&tt);
938     if (code)
939         return code;
940
941     code = WhoIsThis(call, tt, cid);
942     if (code)
943         ABORT_WITH(tt, PRPERM);
944     code = IsAMemberOf(tt, *cid, SYSADMINID);
945     if (!code && !pr_noAuth)
946         ABORT_WITH(tt, PRPERM);
947
948     /* Read in entry to be deleted */
949     if (id) {
950         loc = FindByID(tt, aid);
951     } else {
952         loc = FindByName(tt, name, &tentry);
953     }
954     if (loc == 0)
955         ABORT_WITH(tt, PRNOENT);
956     code = pr_ReadEntry(tt, 0, loc, &tentry);
957     if (code)
958         ABORT_WITH(tt, PRDBFAIL);
959
960     if (uentry->Mask & PRUPDATE_NAMEHASH) {
961         int tloc;
962         code = RemoveFromNameHash(tt, tentry.name, &tloc);
963         if (code != PRSUCCESS)
964             ABORT_WITH(tt, PRDBFAIL);
965         code = AddToNameHash(tt, tentry.name, loc);
966         if (code)
967             ABORT_WITH(tt, code);
968     }
969
970     if (uentry->Mask & PRUPDATE_IDHASH) {
971         int tloc;
972         if (!id)
973             id = tentry.id;
974         code = RemoveFromIDHash(tt, id, &tloc);
975         if (code != PRSUCCESS)
976             ABORT_WITH(tt, PRDBFAIL);
977         code = AddToIDHash(tt, id, loc);
978         if (code)
979             ABORT_WITH(tt, code);
980     }
981
982     code = ubik_EndTrans(tt);
983     if (code)
984         return code;
985     return PRSUCCESS;
986 }
987
988 afs_int32
989 SPR_RemoveFromGroup(struct rx_call *call, afs_int32 aid, afs_int32 gid)
990 {
991     afs_int32 code;
992     afs_int32 cid = ANONYMOUSID;
993
994     code = removeFromGroup(call, aid, gid, &cid);
995     osi_auditU(call, PTS_RmFmGrpEvent, code, AUD_ID, gid, AUD_ID, aid,
996                AUD_END);
997     ViceLog(5, ("PTS_RemoveFromGroup: code %d cid %d gid %d aid %d\n", code, cid, gid, aid));
998     return code;
999 }
1000
1001 static afs_int32
1002 removeFromGroup(struct rx_call *call, afs_int32 aid, afs_int32 gid,
1003                 afs_int32 *cid)
1004 {
1005     afs_int32 code;
1006     struct ubik_trans *tt;
1007     afs_int32 tempu;
1008     afs_int32 tempg;
1009     struct prentry uentry;
1010     struct prentry gentry;
1011
1012     code = WritePreamble(&tt);
1013     if (code)
1014         return code;
1015
1016     code = WhoIsThis(call, tt, cid);
1017     if (code)
1018         ABORT_WITH(tt, PRPERM);
1019     tempu = FindByID(tt, aid);
1020     if (!tempu)
1021         ABORT_WITH(tt, PRNOENT);
1022     tempg = FindByID(tt, gid);
1023     if (!tempg)
1024         ABORT_WITH(tt, PRNOENT);
1025     memset(&uentry, 0, sizeof(uentry));
1026     memset(&gentry, 0, sizeof(gentry));
1027     code = pr_ReadEntry(tt, 0, tempu, &uentry);
1028     if (code != 0)
1029         ABORT_WITH(tt, code);
1030     code = pr_ReadEntry(tt, 0, tempg, &gentry);
1031     if (code != 0)
1032         ABORT_WITH(tt, code);
1033     if (!(gentry.flags & PRGRP))
1034         ABORT_WITH(tt, PRNOTGROUP);
1035 #if !defined(SUPERGROUPS)
1036     if (uentry.flags & PRGRP)
1037         ABORT_WITH(tt, PRNOTUSER);
1038 #endif
1039     if (!AccessOK(tt, *cid, &gentry, PRP_REMOVE_MEM, 0))
1040         ABORT_WITH(tt, PRPERM);
1041     code = RemoveFromEntry(tt, aid, gid);
1042     if (code != PRSUCCESS)
1043         ABORT_WITH(tt, code);
1044 #if defined(SUPERGROUPS)
1045     if (!(uentry.flags & PRGRP))
1046 #endif
1047         code = RemoveFromEntry(tt, gid, aid);
1048 #if defined(SUPERGROUPS)
1049     else
1050         code = RemoveFromSGEntry(tt, gid, aid);
1051 #endif
1052     if (code != PRSUCCESS)
1053         ABORT_WITH(tt, code);
1054
1055     code = ubik_EndTrans(tt);
1056     if (code)
1057         return code;
1058     return PRSUCCESS;
1059 }
1060
1061
1062 afs_int32
1063 SPR_GetCPS(struct rx_call *call, afs_int32 aid, prlist *alist, afs_int32 *over)
1064 {
1065     afs_int32 code;
1066     afs_int32 cid = ANONYMOUSID;
1067
1068     code = getCPS(call, aid, alist, over, &cid);
1069     osi_auditU(call, PTS_GetCPSEvent, code, AUD_ID, aid, AUD_END);
1070     ViceLog(125, ("PTS_GetCPS: code %d cid %d aid %d\n", code, cid, aid));
1071     return code;
1072 }
1073
1074 static afs_int32
1075 getCPS(struct rx_call *call, afs_int32 aid, prlist *alist, afs_int32 *over,
1076        afs_int32 *cid)
1077 {
1078     afs_int32 code;
1079     struct ubik_trans *tt;
1080     afs_int32 temp;
1081     struct prentry tentry;
1082
1083     *over = 0;
1084     alist->prlist_len = 0;
1085     alist->prlist_val = NULL;
1086
1087     code = ReadPreamble(&tt);
1088     if (code)
1089         return code;
1090
1091     temp = FindByID(tt, aid);
1092     if (!temp)
1093         ABORT_WITH(tt, PRNOENT);
1094     code = pr_ReadEntry(tt, 0, temp, &tentry);
1095     if (code)
1096         ABORT_WITH(tt, code);
1097
1098     /* afs does authenticate now */
1099     code = WhoIsThis(call, tt, cid);
1100     if (code || !AccessOK(tt, *cid, &tentry, PRP_MEMBER_MEM, PRP_MEMBER_ANY))
1101         ABORT_WITH(tt, PRPERM);
1102
1103     code = GetList(tt, &tentry, alist, 1);
1104     if (code != PRSUCCESS)
1105         ABORT_WITH(tt, code);
1106
1107     code = ubik_EndTrans(tt);
1108     return code;
1109 }
1110
1111
1112 int
1113 inCPS(prlist CPS, afs_int32 id)
1114 {
1115     int i;
1116
1117     for (i = (CPS.prlist_len - 1); i >= 0; i--) {
1118         if (CPS.prlist_val[i] == id)
1119             return (1);
1120     }
1121     return (0);
1122 }
1123
1124
1125 afs_int32
1126 SPR_GetCPS2(struct rx_call *call, afs_int32 aid, afs_int32 ahost,
1127             prlist *alist, afs_int32 *over)
1128 {
1129     afs_int32 code;
1130     afs_int32 cid = ANONYMOUSID;
1131
1132     code = getCPS2(call, aid, ahost, alist, over, &cid);
1133     osi_auditU(call, PTS_GetCPS2Event, code, AUD_ID, aid, AUD_HOST, htonl(ahost),
1134                AUD_END);
1135     ViceLog(125, ("PTS_GetCPS2: code %d cid %d aid %d ahost %d\n", code, cid, aid, ahost));
1136     return code;
1137 }
1138
1139 static afs_int32
1140 getCPS2(struct rx_call *call, afs_int32 aid, afs_uint32 ahost, prlist *alist,
1141         afs_int32 *over, afs_int32 *cid)
1142 {
1143     afs_int32 code;
1144     struct ubik_trans *tt;
1145     afs_int32 temp;
1146     struct prentry tentry;
1147     struct prentry host_tentry;
1148     afs_int32 hostid;
1149     int host_list = 0;
1150     struct in_addr iaddr;
1151
1152     *over = 0;
1153     iaddr.s_addr = ntohl(ahost);
1154     alist->prlist_len = 0;
1155     alist->prlist_val = NULL;
1156
1157     code = ReadPreamble(&tt);
1158     if (code)
1159         return code;
1160
1161     if (aid != PRBADID) {
1162         temp = FindByID(tt, aid);
1163         if (!temp)
1164             ABORT_WITH(tt, PRNOENT);
1165         code = pr_ReadEntry(tt, 0, temp, &tentry);
1166         if (code)
1167             ABORT_WITH(tt, code);
1168
1169         /* afs does authenticate now */
1170         code = WhoIsThis(call, tt, cid);
1171         if (code
1172             || !AccessOK(tt, *cid, &tentry, PRP_MEMBER_MEM, PRP_MEMBER_ANY))
1173             ABORT_WITH(tt, PRPERM);
1174     }
1175     code = NameToID(tt, inet_ntoa(iaddr), &hostid);
1176     if (code == PRSUCCESS && hostid != 0) {
1177         temp = FindByID(tt, hostid);
1178         if (temp) {
1179             code = pr_ReadEntry(tt, 0, temp, &host_tentry);
1180             if (code == PRSUCCESS)
1181                 host_list = 1;
1182             else
1183                 fprintf(stderr, "pr_ReadEntry returned %d\n", code);
1184         } else
1185             fprintf(stderr, "FindByID Failed -- Not found\n");
1186     }
1187     if (host_list)
1188         code = GetList2(tt, &tentry, &host_tentry, alist, 1);
1189     else
1190         code = GetList(tt, &tentry, alist, 1);
1191     if (!code)
1192         code = addWildCards(tt, alist, ntohl(ahost));
1193     if (code != PRSUCCESS)
1194         ABORT_WITH(tt, code);
1195
1196     code = ubik_EndTrans(tt);
1197     return code;
1198 }
1199
1200
1201 afs_int32
1202 SPR_GetHostCPS(struct rx_call *call, afs_int32 ahost, prlist *alist,
1203                afs_int32 *over)
1204 {
1205     afs_int32 code;
1206
1207     code = getHostCPS(call, ahost, alist, over);
1208     osi_auditU(call, PTS_GetHCPSEvent, code, AUD_HOST, htonl(ahost), AUD_END);
1209     ViceLog(125, ("PTS_GetHostCPS: code %d ahost %d\n", code, ahost));
1210     return code;
1211 }
1212
1213 afs_int32
1214 getHostCPS(struct rx_call *call, afs_uint32 ahost, prlist *alist,
1215            afs_int32 *over)
1216 {
1217     afs_int32 code, temp;
1218     struct ubik_trans *tt;
1219     struct prentry host_tentry;
1220     afs_int32 hostid;
1221     struct in_addr iaddr;
1222
1223     *over = 0;
1224     iaddr.s_addr = ntohl(ahost);
1225     alist->prlist_len = 0;
1226     alist->prlist_val = NULL;
1227
1228     code = ReadPreamble(&tt);
1229     if (code)
1230         return code;
1231
1232     code = NameToID(tt, inet_ntoa(iaddr), &hostid);
1233     if (code == PRSUCCESS && hostid != 0) {
1234         temp = FindByID(tt, hostid);
1235         if (temp) {
1236             code = pr_ReadEntry(tt, 0, temp, &host_tentry);
1237             if (code == PRSUCCESS) {
1238                 code = GetList(tt, &host_tentry, alist, 0);
1239                 if (code)
1240                     goto bad;
1241             } else
1242                 fprintf(stderr, "pr_ReadEntry returned %d\n", code);
1243         } else
1244             fprintf(stderr, "FindByID Failed -- Not found\n");
1245     }
1246     code = addWildCards(tt, alist, ntohl(ahost));
1247   bad:
1248     if (code != PRSUCCESS)
1249         ABORT_WITH(tt, code);
1250
1251     code = ubik_EndTrans(tt);
1252     return code;
1253 }
1254
1255
1256 afs_int32
1257 SPR_ListMax(struct rx_call *call, afs_int32 *uid, afs_int32 *gid)
1258 {
1259     afs_int32 code;
1260
1261     code = listMax(call, uid, gid);
1262     osi_auditU(call, PTS_LstMaxEvent, code, AUD_END);
1263     ViceLog(125, ("PTS_ListMax: code %d\n", code));
1264     return code;
1265 }
1266
1267 afs_int32
1268 listMax(struct rx_call *call, afs_int32 *uid, afs_int32 *gid)
1269 {
1270     afs_int32 code;
1271     struct ubik_trans *tt;
1272
1273     code = ReadPreamble(&tt);
1274     if (code)
1275         return code;
1276
1277     code = GetMax(tt, uid, gid);
1278     if (code != PRSUCCESS)
1279         ABORT_WITH(tt, code);
1280
1281     code = ubik_EndTrans(tt);
1282     if (code)
1283         return code;
1284     return PRSUCCESS;
1285 }
1286
1287 afs_int32
1288 SPR_SetMax(struct rx_call *call, afs_int32 aid, afs_int32 gflag)
1289 {
1290     afs_int32 code;
1291     afs_int32 cid = ANONYMOUSID;
1292
1293     code = setMax(call, aid, gflag, &cid);
1294     osi_auditU(call, PTS_SetMaxEvent, code, AUD_ID, aid, AUD_LONG, gflag,
1295                AUD_END);
1296     ViceLog(125, ("PTS_SetMax: code %d cid %d aid %d gflag %d\n", code, cid, aid, gflag));
1297     return code;
1298 }
1299
1300 static afs_int32
1301 setMax(struct rx_call *call, afs_int32 aid, afs_int32 gflag, afs_int32 *cid)
1302 {
1303     afs_int32 code;
1304     struct ubik_trans *tt;
1305
1306     code = WritePreamble(&tt);
1307     if (code)
1308         return code;
1309
1310     code = WhoIsThis(call, tt, cid);
1311     if (code)
1312         ABORT_WITH(tt, PRPERM);
1313     if (!AccessOK(tt, *cid, 0, 0, 0))
1314         ABORT_WITH(tt, PRPERM);
1315     if (((gflag & PRGRP) && (aid > 0)) || (!(gflag & PRGRP) && (aid < 0)))
1316         ABORT_WITH(tt, PRBADARG);
1317
1318     code = SetMax(tt, aid, gflag);
1319     if (code != PRSUCCESS)
1320         ABORT_WITH(tt, code);
1321
1322     code = ubik_EndTrans(tt);
1323     if (code)
1324         return code;
1325     return PRSUCCESS;
1326 }
1327
1328 afs_int32
1329 SPR_ListEntry(struct rx_call *call, afs_int32 aid, struct prcheckentry *aentry)
1330 {
1331     afs_int32 code;
1332     afs_int32 cid = ANONYMOUSID;
1333
1334     code = listEntry(call, aid, aentry, &cid);
1335     osi_auditU(call, PTS_LstEntEvent, code, AUD_ID, aid, AUD_END);
1336     ViceLog(125, ("PTS_ListEntry: code %d cid %d aid %d\n", code, cid, aid));
1337     return code;
1338 }
1339
1340 static afs_int32
1341 listEntry(struct rx_call *call, afs_int32 aid, struct prcheckentry *aentry,
1342           afs_int32 *cid)
1343 {
1344     afs_int32 code;
1345     struct ubik_trans *tt;
1346     afs_int32 temp;
1347     struct prentry tentry;
1348
1349     code = ReadPreamble(&tt);
1350     if (code)
1351         return code;
1352
1353     code = WhoIsThis(call, tt, cid);
1354     if (code)
1355         ABORT_WITH(tt, PRPERM);
1356     temp = FindByID(tt, aid);
1357     if (!temp)
1358         ABORT_WITH(tt, PRNOENT);
1359     code = pr_ReadEntry(tt, 0, temp, &tentry);
1360     if (code != 0)
1361         ABORT_WITH(tt, code);
1362     if (!AccessOK(tt, *cid, &tentry, PRP_STATUS_MEM, PRP_STATUS_ANY))
1363         ABORT_WITH(tt, PRPERM);
1364
1365     aentry->flags = tentry.flags >> PRIVATE_SHIFT;
1366     if (aentry->flags == 0) {
1367         if (tentry.flags & PRGRP)
1368             aentry->flags = prp_group_default >> PRIVATE_SHIFT;
1369         else
1370             aentry->flags = prp_user_default >> PRIVATE_SHIFT;
1371     }
1372     aentry->owner = tentry.owner;
1373     aentry->id = tentry.id;
1374     strncpy(aentry->name, tentry.name, PR_MAXNAMELEN);
1375     aentry->creator = tentry.creator;
1376     aentry->ngroups = tentry.ngroups;
1377     aentry->nusers = tentry.nusers;
1378     aentry->count = tentry.count;
1379     memset(aentry->reserved, 0, sizeof(aentry->reserved));
1380     code = ubik_EndTrans(tt);
1381     if (code)
1382         return code;
1383     return PRSUCCESS;
1384 }
1385
1386 afs_int32
1387 SPR_ListEntries(struct rx_call *call, afs_int32 flag, afs_int32 startindex,
1388                 prentries *bulkentries, afs_int32 *nextstartindex)
1389 {
1390     afs_int32 code;
1391     afs_int32 cid = ANONYMOUSID;
1392
1393     code = listEntries(call, flag, startindex, bulkentries, nextstartindex, &cid);
1394     osi_auditU(call, PTS_LstEntsEvent, code, AUD_LONG, flag, AUD_END);
1395     ViceLog(125, ("PTS_ListEntries: code %d cid %d flag %d\n", code, cid, flag));
1396     return code;
1397 }
1398
1399 static afs_int32
1400 listEntries(struct rx_call *call, afs_int32 flag, afs_int32 startindex,
1401             prentries *bulkentries, afs_int32 *nextstartindex, afs_int32 *cid)
1402 {
1403     afs_int32 code;
1404     struct ubik_trans *tt;
1405     afs_int32 i, eof, pos, maxentries, f;
1406     struct prentry tentry;
1407     afs_int32 pollcount = 0;
1408
1409     *nextstartindex = -1;
1410     bulkentries->prentries_val = 0;
1411     bulkentries->prentries_len = 0;
1412
1413     code = ReadPreamble(&tt);
1414     if (code)
1415         return code;
1416
1417     /* Make sure we are an authenticated caller and that we are on the
1418      * SYSADMIN list.
1419      */
1420     code = WhoIsThis(call, tt, cid);
1421     if (code)
1422         ABORT_WITH(tt, PRPERM);
1423     code = IsAMemberOf(tt, *cid, SYSADMINID);
1424     if (!code && !pr_noAuth)
1425         ABORT_WITH(tt, PRPERM);
1426
1427     eof = ntohl(cheader.eofPtr) - sizeof(cheader);
1428     maxentries = eof / sizeof(struct prentry);
1429     for (i = startindex; i < maxentries; i++) {
1430         pos = i * sizeof(struct prentry) + sizeof(cheader);
1431         code = pr_ReadEntry(tt, 0, pos, &tentry);
1432         if (code)
1433             goto done;
1434
1435         if (++pollcount > 50) {
1436 #ifndef AFS_PTHREAD_ENV
1437             IOMGR_Poll();
1438 #endif
1439             pollcount = 0;
1440         }
1441
1442         f = (tentry.flags & PRTYPE);
1443         if (((flag & PRUSERS) && (f == 0)) ||   /* User  entry */
1444             ((flag & PRGROUPS) && (f & PRGRP))) {       /* Group entry */
1445             code = put_prentries(&tentry, bulkentries);
1446             if (code == -1)
1447                 break;          /* Filled return array */
1448             if (code)
1449                 goto done;
1450         }
1451     }
1452     code = 0;
1453     if (i < maxentries)
1454         *nextstartindex = i;
1455
1456   done:
1457     if (code) {
1458         if (bulkentries->prentries_val)
1459             free(bulkentries->prentries_val);
1460         bulkentries->prentries_val = 0;
1461         bulkentries->prentries_len = 0;
1462         ABORT_WITH(tt, code);
1463     } else {
1464         code = ubik_EndTrans(tt);
1465     }
1466     if (code)
1467         return code;
1468     return PRSUCCESS;
1469 }
1470
1471 #define PR_MAXENTRIES 500
1472 static afs_int32
1473 put_prentries(struct prentry *tentry, prentries *bulkentries)
1474 {
1475     struct prlistentries *entry;
1476
1477     if (bulkentries->prentries_val == 0) {
1478         bulkentries->prentries_len = 0;
1479         bulkentries->prentries_val =
1480             (struct prlistentries *)malloc(PR_MAXENTRIES *
1481                                            sizeof(struct prentry));
1482         if (!bulkentries->prentries_val) {
1483             return (PRNOMEM);
1484         }
1485     }
1486
1487     if (bulkentries->prentries_len >= PR_MAXENTRIES) {
1488         return (-1);
1489     }
1490
1491     entry = (struct prlistentries *)bulkentries->prentries_val;
1492     entry += bulkentries->prentries_len;
1493
1494     entry->flags = tentry->flags >> PRIVATE_SHIFT;
1495     if (entry->flags == 0) {
1496         entry->flags =
1497             ((tentry->
1498               flags & PRGRP) ? prp_group_default : prp_user_default) >>
1499             PRIVATE_SHIFT;
1500     }
1501     entry->owner = tentry->owner;
1502     entry->id = tentry->id;
1503     entry->creator = tentry->creator;
1504     entry->ngroups = tentry->ngroups;
1505     entry->nusers = tentry->nusers;
1506     entry->count = tentry->count;
1507     strncpy(entry->name, tentry->name, PR_MAXNAMELEN);
1508     memset(entry->reserved, 0, sizeof(entry->reserved));
1509     bulkentries->prentries_len++;
1510     return 0;
1511 }
1512
1513 afs_int32
1514 SPR_ChangeEntry(struct rx_call *call, afs_int32 aid, char *name, afs_int32 oid,
1515                 afs_int32 newid)
1516 {
1517     afs_int32 code;
1518     afs_int32 cid = ANONYMOUSID;
1519
1520     code = changeEntry(call, aid, name, oid, newid, &cid);
1521     osi_auditU(call, PTS_ChgEntEvent, code, AUD_ID, aid, AUD_STR, name,
1522                AUD_LONG, oid, AUD_LONG, newid, AUD_END);
1523     ViceLog(5, ("PTS_ChangeEntry: code %d cid %d aid %d name %s oid %d newid %d\n", code, cid, aid, name, oid, newid));
1524     return code;
1525 }
1526
1527 static afs_int32
1528 changeEntry(struct rx_call *call, afs_int32 aid, char *name, afs_int32 oid,
1529             afs_int32 newid, afs_int32 *cid)
1530 {
1531     afs_int32 code;
1532     struct ubik_trans *tt;
1533     afs_int32 pos;
1534
1535     if (!name)
1536         return PRPERM;
1537     stolower(name);
1538
1539     if (aid == ANYUSERID || aid == AUTHUSERID || aid == ANONYMOUSID
1540         || aid == SYSADMINID)
1541         return PRPERM;
1542
1543     code = WritePreamble(&tt);
1544     if (code)
1545         return code;
1546
1547     code = WhoIsThis(call, tt, cid);
1548     if (code)
1549         ABORT_WITH(tt, PRPERM);
1550     pos = FindByID(tt, aid);
1551     if (!pos)
1552         ABORT_WITH(tt, PRNOENT);
1553     /* protection check in changeentry */
1554     code = ChangeEntry(tt, aid, *cid, name, oid, newid);
1555     if (code != PRSUCCESS)
1556         ABORT_WITH(tt, code);
1557
1558     code = ubik_EndTrans(tt);
1559     return code;
1560 }
1561
1562 afs_int32
1563 SPR_SetFieldsEntry(struct rx_call *call,
1564                    afs_int32 id,
1565                    afs_int32 mask, /* specify which fields to update */
1566                    afs_int32 flags, afs_int32 ngroups, afs_int32 nusers,
1567                    afs_int32 spare1, afs_int32 spare2)
1568 {
1569     afs_int32 code;
1570     afs_int32 cid = ANONYMOUSID;
1571
1572     code =
1573         setFieldsEntry(call, id, mask, flags, ngroups, nusers, spare1,
1574                        spare2, &cid);
1575     osi_auditU(call, PTS_SetFldEntEvent, code, AUD_ID, id, AUD_END);
1576     ViceLog(5, ("PTS_SetFieldsEntry: code %d cid %d id %d\n", code, cid, id));
1577     return code;
1578 }
1579
1580 static afs_int32
1581 setFieldsEntry(struct rx_call *call,
1582                afs_int32 id,
1583                afs_int32 mask, /* specify which fields to update */
1584                afs_int32 flags, afs_int32 ngroups, afs_int32 nusers,
1585                afs_int32 spare1, afs_int32 spare2, afs_int32 *cid)
1586 {
1587     afs_int32 code;
1588     struct ubik_trans *tt;
1589     afs_int32 pos;
1590     struct prentry tentry;
1591     afs_int32 tflags;
1592
1593     if (mask == 0)
1594         return 0;               /* no-op */
1595
1596     if (id == ANYUSERID || id == AUTHUSERID || id == ANONYMOUSID)
1597         return PRPERM;
1598
1599     code = WritePreamble(&tt);
1600     if (code)
1601         return code;
1602
1603     code = WhoIsThis(call, tt, cid);
1604     if (code)
1605         ABORT_WITH(tt, PRPERM);
1606     pos = FindByID(tt, id);
1607     if (!pos)
1608         ABORT_WITH(tt, PRNOENT);
1609     code = pr_ReadEntry(tt, 0, pos, &tentry);
1610     if (code)
1611         ABORT_WITH(tt, code);
1612     tflags = tentry.flags;
1613
1614     if (mask & (PR_SF_NGROUPS | PR_SF_NUSERS)) {
1615         if (!AccessOK(tt, *cid, 0, 0, 0))
1616             ABORT_WITH(tt, PRPERM);
1617         if ((tflags & PRQUOTA) == 0) {  /* default if only setting one */
1618             tentry.ngroups = tentry.nusers = 20;
1619         }
1620     } else {
1621         if (!AccessOK(tt, *cid, &tentry, 0, 0))
1622             ABORT_WITH(tt, PRPERM);
1623     }
1624
1625     if (mask & 0xffff) {        /* if setting flag bits */
1626         afs_int32 flagsMask = mask & 0xffff;
1627         tflags &= ~(flagsMask << PRIVATE_SHIFT);
1628         tflags |= (flags & flagsMask) << PRIVATE_SHIFT;
1629         tflags |= PRACCESS;
1630     }
1631
1632     if (mask & PR_SF_NGROUPS) { /* setting group limit */
1633         if (ngroups < 0)
1634             ABORT_WITH(tt, PRBADARG);
1635         tentry.ngroups = ngroups;
1636         tflags |= PRQUOTA;
1637     }
1638
1639     if (mask & PR_SF_NUSERS) {  /* setting foreign user limit */
1640         if (nusers < 0)
1641             ABORT_WITH(tt, PRBADARG);
1642         tentry.nusers = nusers;
1643         tflags |= PRQUOTA;
1644     }
1645     tentry.flags = tflags;
1646
1647     code = pr_WriteEntry(tt, 0, pos, &tentry);
1648     if (code)
1649         ABORT_WITH(tt, code);
1650
1651     code = ubik_EndTrans(tt);
1652     return code;
1653 }
1654
1655 afs_int32
1656 SPR_ListElements(struct rx_call *call, afs_int32 aid, prlist *alist,
1657                  afs_int32 *over)
1658 {
1659     afs_int32 code;
1660     afs_int32 cid = ANONYMOUSID;
1661
1662     code = listElements(call, aid, alist, over, &cid);
1663     osi_auditU(call, PTS_LstEleEvent, code, AUD_ID, aid, AUD_END);
1664     ViceLog(125, ("PTS_ListElements: code %d cid %d aid %d\n", code, cid, aid));
1665     return code;
1666 }
1667
1668 static afs_int32
1669 listElements(struct rx_call *call, afs_int32 aid, prlist *alist,
1670              afs_int32 *over, afs_int32 *cid)
1671 {
1672     afs_int32 code;
1673     struct ubik_trans *tt;
1674     afs_int32 temp;
1675     struct prentry tentry;
1676
1677     *over = 0;
1678     alist->prlist_len = 0;
1679     alist->prlist_val = NULL;
1680
1681     code = ReadPreamble(&tt);
1682     if (code)
1683         return code;
1684
1685     code = WhoIsThis(call, tt, cid);
1686     if (code)
1687         ABORT_WITH(tt, PRPERM);
1688
1689     temp = FindByID(tt, aid);
1690     if (!temp)
1691         ABORT_WITH(tt, PRNOENT);
1692     code = pr_ReadEntry(tt, 0, temp, &tentry);
1693     if (code)
1694         ABORT_WITH(tt, code);
1695     if (!AccessOK(tt, *cid, &tentry, PRP_MEMBER_MEM, PRP_MEMBER_ANY))
1696         ABORT_WITH(tt, PRPERM);
1697
1698     code = GetList(tt, &tentry, alist, 0);
1699     if (code != PRSUCCESS)
1700         ABORT_WITH(tt, code);
1701
1702     code = ubik_EndTrans(tt);
1703     return code;
1704 }
1705
1706
1707 afs_int32
1708 SPR_ListSuperGroups(struct rx_call *call, afs_int32 aid, prlist *alist,
1709                     afs_int32 *over)
1710 {
1711 #if defined(SUPERGROUPS)
1712     afs_int32 code;
1713     afs_int32 cid = ANONYMOUSID;
1714
1715     code = listSuperGroups(call, aid, alist, over, &cid);
1716     osi_auditU(call, PTS_LstSGrps, code, AUD_ID, aid, AUD_END);
1717     ViceLog(125, ("PTS_ListSuperGroups: code %d cid %d aid %d\n", code, cid, aid));
1718     return code;
1719 #else
1720     return RXGEN_OPCODE;
1721 #endif
1722 }
1723
1724 #if defined(SUPERGROUPS)
1725 static afs_int32
1726 listSuperGroups(struct rx_call *call, afs_int32 aid, prlist *alist,
1727                 afs_int32 *over, afs_int32 *cid)
1728 {
1729     afs_int32 code;
1730     struct ubik_trans *tt;
1731     afs_int32 temp;
1732     struct prentry tentry;
1733
1734     alist->prlist_len = 0;
1735     alist->prlist_val = (afs_int32 *) 0;
1736
1737     code = ReadPreamble(&tt);
1738     if (code)
1739         return code;
1740
1741     code = ubik_SetLock(tt, 1, 1, LOCKREAD);
1742     if (code)
1743         ABORT_WITH(tt, code);
1744     code = WhoIsThis(call, tt, cid);
1745     if (code)
1746         ABORT_WITH(tt, PRPERM);
1747
1748     temp = FindByID(tt, aid);
1749     if (!temp)
1750         ABORT_WITH(tt, PRNOENT);
1751     code = pr_ReadEntry(tt, 0, temp, &tentry);
1752     if (code)
1753         ABORT_WITH(tt, code);
1754     if (!AccessOK(tt, *cid, &tentry, PRP_MEMBER_MEM, PRP_MEMBER_ANY))
1755         ABORT_WITH(tt, PRPERM);
1756
1757     code = GetSGList(tt, &tentry, alist);
1758     *over = 0;
1759     if (code == PRTOOMANY)
1760         *over = 1;
1761     else if (code != PRSUCCESS)
1762         ABORT_WITH(tt, code);
1763
1764     code = ubik_EndTrans(tt);
1765
1766     return code;
1767 }
1768
1769 #endif /* SUPERGROUPS */
1770
1771 /*
1772  * SPR_ListOwned
1773  * List the entries owned by this id.  If the id is zero,
1774  * return the orphans list. This will return up to PR_MAXGROUPS
1775  * at a time with the lastP available to get the rest. The
1776  * maximum value is enforced in GetOwnedChain().
1777  */
1778 afs_int32
1779 SPR_ListOwned(struct rx_call *call, afs_int32 aid, prlist *alist,
1780               afs_int32 *lastP)
1781 {
1782     afs_int32 code;
1783     afs_int32 cid = ANONYMOUSID;
1784
1785     code = listOwned(call, aid, alist, lastP, &cid);
1786     osi_auditU(call, PTS_LstOwnEvent, code, AUD_ID, aid, AUD_END);
1787     ViceLog(125, ("PTS_ListOwned: code %d cid %d aid %d\n", code, cid, aid));
1788     return code;
1789 }
1790
1791 afs_int32
1792 listOwned(struct rx_call *call, afs_int32 aid, prlist *alist, afs_int32 *lastP,
1793           afs_int32 *cid)
1794 {
1795     afs_int32 code;
1796     struct ubik_trans *tt;
1797     struct prentry tentry;
1798     afs_int32 head = 0;
1799     afs_int32 start;
1800
1801     alist->prlist_len = 0;
1802     alist->prlist_val = NULL;
1803
1804     if (!lastP)
1805         return PRBADARG;
1806     start = *lastP;
1807     *lastP = 0;
1808
1809     code = ReadPreamble(&tt);
1810     if (code)
1811         return code;
1812
1813     code = WhoIsThis(call, tt, cid);
1814     if (code)
1815         ABORT_WITH(tt, PRPERM);
1816
1817     if (start) {
1818         code = pr_ReadEntry(tt, 0, start, &tentry);
1819         if (!code && (tentry.owner == aid))
1820             head = start;       /* pick up where we left off */
1821     }
1822
1823     if (!head) {
1824         if (aid) {
1825             afs_int32 loc = FindByID(tt, aid);
1826             if (loc == 0)
1827                 ABORT_WITH(tt, PRNOENT);
1828             code = pr_ReadEntry(tt, 0, loc, &tentry);
1829             if (code)
1830                 ABORT_WITH(tt, code);
1831
1832             if (!AccessOK(tt, *cid, &tentry, -1, PRP_OWNED_ANY))
1833                 ABORT_WITH(tt, PRPERM);
1834             head = tentry.owned;
1835         } else {
1836             if (!AccessOK(tt, *cid, 0, 0, 0))
1837                 ABORT_WITH(tt, PRPERM);
1838             head = ntohl(cheader.orphan);
1839         }
1840     }
1841
1842     code = GetOwnedChain(tt, &head, alist);
1843     if (code) {
1844         if (code == PRTOOMANY)
1845             *lastP = head;
1846         else
1847             ABORT_WITH(tt, code);
1848     }
1849
1850     code = ubik_EndTrans(tt);
1851     return code;
1852 }
1853
1854 afs_int32
1855 SPR_IsAMemberOf(struct rx_call *call, afs_int32 uid, afs_int32 gid,
1856                 afs_int32 *flag)
1857 {
1858     afs_int32 code;
1859     afs_int32 cid = ANONYMOUSID;
1860
1861     code = isAMemberOf(call, uid, gid, flag, &cid);
1862     osi_auditU(call, PTS_IsMemOfEvent, code, AUD_LONG, uid, AUD_LONG, gid,
1863                AUD_END);
1864     ViceLog(125, ("PTS_IsAMemberOf: code %d cid %d uid %d gid %d\n", code, cid, uid, gid));
1865     return code;
1866 }
1867
1868 static afs_int32
1869 isAMemberOf(struct rx_call *call, afs_int32 uid, afs_int32 gid, afs_int32 *flag,
1870             afs_int32 *cid)
1871 {
1872     afs_int32 code;
1873     struct ubik_trans *tt;
1874
1875     code = ReadPreamble(&tt);
1876     if (code)
1877         return code;
1878
1879     {
1880         afs_int32 uloc = FindByID(tt, uid);
1881         afs_int32 gloc = FindByID(tt, gid);
1882         struct prentry uentry, gentry;
1883
1884         if (!uloc || !gloc)
1885             ABORT_WITH(tt, PRNOENT);
1886         code = WhoIsThis(call, tt, cid);
1887         if (code)
1888             ABORT_WITH(tt, PRPERM);
1889         code = pr_ReadEntry(tt, 0, uloc, &uentry);
1890         if (code)
1891             ABORT_WITH(tt, code);
1892         code = pr_ReadEntry(tt, 0, gloc, &gentry);
1893         if (code)
1894             ABORT_WITH(tt, code);
1895 #if !defined(SUPERGROUPS)
1896         if ((uentry.flags & PRGRP) || !(gentry.flags & PRGRP))
1897             ABORT_WITH(tt, PRBADARG);
1898 #else
1899         if (!(gentry.flags & PRGRP))
1900             ABORT_WITH(tt, PRBADARG);
1901 #endif
1902         if (!AccessOK(tt, *cid, &uentry, 0, PRP_MEMBER_ANY)
1903             && !AccessOK(tt, *cid, &gentry, PRP_MEMBER_MEM, PRP_MEMBER_ANY))
1904             ABORT_WITH(tt, PRPERM);
1905     }
1906
1907     *flag = IsAMemberOf(tt, uid, gid);
1908     code = ubik_EndTrans(tt);
1909     return code;
1910 }
1911
1912 static afs_int32
1913 addWildCards(struct ubik_trans *tt, prlist *alist, afs_uint32 host)
1914 {
1915     afs_int32 temp;
1916     struct prentry tentry;
1917     prlist wlist;
1918     unsigned wild = htonl(0xffffff00);
1919     struct in_addr iaddr;
1920     afs_int32 hostid;
1921     int size = 0, i, code;
1922     int added = 0;
1923
1924     while ((host = (host & wild))) {
1925         wild = htonl(ntohl(wild) << 8);
1926         iaddr.s_addr = host;
1927         code = NameToID(tt, inet_ntoa(iaddr), &hostid);
1928         if (code == PRSUCCESS && hostid != 0) {
1929             temp = FindByID(tt, hostid);
1930             if (temp) {
1931                 code = pr_ReadEntry(tt, 0, temp, &tentry);
1932                 if (code != PRSUCCESS)
1933                     continue;
1934             } else
1935                 continue;
1936         } else
1937             continue;
1938         wlist.prlist_len = 0;
1939         wlist.prlist_val = NULL;
1940
1941         code = GetList(tt, &tentry, &wlist, 0);
1942         if (code)
1943             return code;
1944         added += wlist.prlist_len;
1945         for (i = 0; i < wlist.prlist_len; i++) {
1946             if (!inCPS(*alist, wlist.prlist_val[i]))
1947                 if ((code = AddToPRList(alist, &size, wlist.prlist_val[i]))) {
1948                     free(wlist.prlist_val);
1949                     return (code);
1950                 }
1951         }
1952         if (wlist.prlist_val)
1953             free(wlist.prlist_val);
1954     }
1955     if (added)
1956         qsort(alist->prlist_val, alist->prlist_len, sizeof(afs_int32), IDCmp);
1957     return 0;
1958 }
1959
1960 static afs_int32
1961 WhoIsThisWithName(struct rx_call *acall, struct ubik_trans *at, afs_int32 *aid,
1962                   char *aname)
1963 {
1964     afs_int32 islocal = 1;
1965     /* aid is set to the identity of the caller, if known, else ANONYMOUSID */
1966     /* returns -1 and sets aid to ANONYMOUSID on any failure */
1967     struct rx_connection *tconn;
1968     afs_int32 code;
1969     char tcell[MAXKTCREALMLEN];
1970     char name[MAXKTCNAMELEN];
1971     char inst[MAXKTCNAMELEN];
1972     int ilen;
1973     char vname[256];
1974
1975     *aid = ANONYMOUSID;
1976     tconn = rx_ConnectionOf(acall);
1977     code = rx_SecurityClassOf(tconn);
1978     if (code == 0)
1979         return 0;
1980     else if (code == 1) {       /* vab class */
1981         goto done;              /* no longer supported */
1982     } else if (code == 2) {     /* kad class */
1983         if ((code = rxkad_GetServerInfo(rx_ConnectionOf(acall), NULL, NULL,
1984                                         name, inst, tcell, NULL)))
1985             goto done;
1986
1987         if (tcell[0]) {
1988             code = afsconf_IsLocalRealmMatch(prdir, &islocal, name, inst, tcell);
1989             if (code)
1990                 goto done;
1991         }
1992         strncpy(vname, name, sizeof(vname));
1993         if ((ilen = strlen(inst))) {
1994             if (strlen(vname) + 1 + ilen >= sizeof(vname))
1995                 goto done;
1996             strcat(vname, ".");
1997             strcat(vname, inst);
1998         }
1999         if (!islocal) {
2000              if (strlen(vname) + strlen(tcell) + 1  >= sizeof(vname))
2001                 goto done;
2002              strcat(vname, "@");
2003              strcat(vname, tcell);
2004              lcstring(vname, vname, sizeof(vname));
2005              code = NameToID(at, vname, aid);
2006              if (aname)
2007                 strcpy(aname, vname);
2008              return 2;
2009         }
2010
2011         if (strcmp(AUTH_SUPERUSER, vname) == 0)
2012             *aid = SYSADMINID;  /* special case for the fileserver */
2013         else {
2014             lcstring(vname, vname, sizeof(vname));
2015             code = NameToID(at, vname, aid);
2016         }
2017     }
2018   done:
2019     if (code && !pr_noAuth)
2020         return -1;
2021     return 0;
2022 }