openafs-void-star-pointers-20071031
[openafs.git] / src / ptserver / testpt.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
13 RCSID
14     ("$Header$");
15
16 #include <ctype.h>
17 #include <errno.h>
18 #include <sys/types.h>
19 #ifdef AFS_NT40_ENV
20 #include <winsock2.h>
21 #include <WINNT/afsevent.h>
22 #else
23 #include <sys/socket.h>
24 #include <netdb.h>
25 #include <netinet/in.h>
26 #endif
27 #include <string.h>
28 #ifdef HAVE_FCNTL_H
29 #include <fcntl.h>
30 #endif
31 #ifdef HAVE_SYS_STAT_H
32 #include <sys/stat.h>
33 #endif
34 #ifdef HAVE_SYS_TYPES_H
35 #include <sys/types.h>
36 #endif
37 #include <rx/rx.h>
38 #include <rx/xdr.h>
39 #include <afs/rxgen_consts.h>
40 #include <afs/cmd.h>
41 #include <afs/auth.h>
42 #include <afs/cellconfig.h>
43 #include "ptclient.h"
44 #include "pterror.h"
45 #include <afs/afsutil.h>
46 #include <afs/com_err.h>
47
48 static char *whoami = "testpr";
49 static struct afsconf_dir *conf;        /* cell info, set by MyBeforeProc */
50 static char conf_dir[100];
51 static char lcell[MAXCELLCHARS];
52
53 int
54 ListUsedIds(struct cmd_syndesc *as, void *arock)
55 {
56     afs_int32 code;
57     namelist lnames;
58     idlist lids;
59     int i, j;
60     int group = 0;              /* check groups */
61     int unused = 0;             /* print unused */
62     int number = 100;           /* check 100 ids */
63     afs_int32 startId = 1;
64     afs_int32 maxId;
65     int range;
66
67     if (as->parms[0].items)
68         startId = atoi(as->parms[0].items->data);
69     if (as->parms[1].items)
70         number = atoi(as->parms[1].items->data);
71     if (as->parms[2].items)
72         unused = 1;
73
74     code = pr_Initialize(1, conf_dir, NULL);
75     if (code) {
76         afs_com_err(whoami, code, "initializing pruser");
77         exit(1);
78     }
79     if (startId < 0) {
80         group = 1;
81         code = pr_ListMaxGroupId(&maxId);
82         if (code) {
83           bad_max:
84             afs_com_err(whoami, code, "getting maximum id");
85             exit(2);
86         }
87         if (startId < maxId) {
88             fprintf(stderr, "Max group id is only %d.\n", maxId);
89             exit(3);
90         }
91     } else {
92         code = pr_ListMaxUserId(&maxId);
93         if (code)
94             goto bad_max;
95         if (startId > maxId) {
96             fprintf(stderr, "Max user id is only %d.\n", maxId);
97             exit(3);
98         }
99     }
100     range = abs(startId - maxId);
101     if (range < 0)
102         range = -range;
103     range++;                    /* number that can be printed */
104     if (range < number) {
105         fprintf(stderr, "Only %d ids to be checked.\n", range);
106         number = range;
107     }
108
109     printf("Checking for %d %sused ids starting at %d.\n", number,
110            (unused ? "un" : ""), startId);
111 #define NUM 100
112     lids.idlist_val = (afs_int32 *) malloc(sizeof(afs_int32) * NUM);
113     lnames.namelist_len = 0;
114     lnames.namelist_val = 0;
115     while (number) {
116         if (number < NUM)
117             i = number;
118         else
119             i = NUM;
120         for (j = 0; j < i; j++) {
121             lids.idlist_val[j] = startId;
122             if (group)
123                 startId--;
124             else
125                 startId++;
126         }
127         lids.idlist_len = i;
128         code = pr_IdToName(&lids, &lnames);
129         if (code) {
130             afs_com_err(whoami, code, "converting id to name");
131             exit(2);
132         }
133         for (j = 0; j < lnames.namelist_len; j++) {
134             if (lids.idlist_val[j] == atoi(lnames.namelist_val[j])) {
135                 if (unused)
136                     printf("%s is free\n", lnames.namelist_val[j]);
137             } else {
138                 if (!unused)
139                     printf("%s is id %d\n", lnames.namelist_val[j],
140                            lids.idlist_val[j]);
141             }
142         }
143         number -= i;
144     }
145     if (lids.idlist_val)
146         free(lids.idlist_val);
147     if (lnames.namelist_val)
148         free(lnames.namelist_val);
149     return 0;
150 }
151
152 /* TestManyMembers - called with a number N.  Try creating N users and N groups
153  * and put all the users on one of the groups and one of the users on all the
154  * groups.  Also put many users on many groups.
155  *
156  * To keep track of this create an NxN matrix of membership and fill it in with
157  * a function that looks like a quarter of a circle.  That makes the first
158  * group contain every user and the first user be a member of every group. */
159
160 int verbose;
161 char callerName[PR_MAXNAMELEN];
162 afs_int32 callerId;
163 afs_int32 lastGroup;            /* id of last group created */
164 afs_int32 ownerUser;            /* first created user */
165 char ownerUserName[PR_MAXNAMELEN];      /*  " " " name */
166 int steepDropOff;               /* precentage decreate in GroupLimit */
167 char *createPrefix;             /* prefix for naming users&groups */
168 extern struct ubik_client *pruclient;   /* initialized by pr_Initialize */
169
170 /* These variables form the state if this test */
171 int number;                     /* max number of members */
172 char *population;               /* matrix of memberships */
173 afs_int32 *users;               /* ids of users */
174 afs_int32 *groups;              /* ids of groups */
175 afs_int32 *groupOwners;         /* ids of owners of groups */
176
177 /* statistics */
178 int nUsers, nGroups, nAdds, nRems, nUDels, nGDels;
179
180 int
181 IdCmp(afs_int32 *a, afs_int32 *b)
182 {
183     if (*a > *b) {
184         return 1;
185     } else if (*a == *b) {
186         return 0;
187     } else /* (*a < *b) */ {
188         return -1;
189     }
190 }
191
192 static int
193 sqr(int n)
194 {
195     return n * n;
196 }
197
198 static int
199 GetGroupLimit(int N, int x)
200 {
201     int y;
202     double sqrt();
203
204     if ((x >= N) || (x < 0)) {
205         printf("GetGroupLimit: input value out of range %d (%d)\n", x, N);
206         exit(10);
207     }
208     if (steepDropOff) {         /* Use exponential decrease */
209         int i;
210         y = N;
211         for (i = 0; i < x; i++) {
212             y = (y * steepDropOff) / 100;       /* parameter is a percentage */
213             if (y == 0) {
214                 y = 1;          /* with a floor of 1 */
215                 break;
216             }
217         }
218     } else {                    /* Use a circle's third quadrant */
219         y = sqr(N - 1) - sqr(N - 1 - x);
220         y = (int)(sqrt((double)y) + 0.5);       /* round off */
221         y = N - y;
222     }
223     if ((y > N) || (y < 1)) {
224         printf("filling value out of range: %d (%d) => %d\n", x, N, y);
225         exit(11);
226     }
227     return y;
228 }
229
230 void
231 CreateUser(int u)
232 {
233     afs_int32 code;
234     char name[16];
235     afs_int32 id;
236
237     sprintf(name, "%s%d", createPrefix, u);
238     id = 0;
239     code = pr_CreateUser(name, &id);
240     if (code) {
241         if (code == PREXIST) {
242             code = pr_Delete(name);
243             if (code == 0) {
244                 nUDels++;
245                 code = pr_CreateUser(name, &id);
246                 if (code == 0) {
247                     if (verbose)
248                         printf("RE-");
249                     goto done;
250                 }
251             }
252         }
253         afs_com_err(whoami, code, "couldn't create %s", name);
254         exit(12);
255     }
256   done:
257     if (verbose)
258         printf("Creating user %s (%di)\n", name, id);
259     users[u] = id;
260     nUsers++;
261
262     if (ownerUser == 0) {
263         ownerUser = id;
264         strcpy(ownerUserName, name);
265     }
266 }
267
268 void
269 CreateGroup(int g)
270 {
271     afs_int32 code;
272     char name[16];
273     afs_int32 id = 0;
274     afs_int32 flags = PRGRP;
275     afs_int32 owner;
276     char *ownerName;
277     int ownerType;              /* type of ownership */
278     static char *lastGroupPrefix;       /* prefix used for type==2 */
279
280     /* At least 50 groups should be owned by another group to test long owner
281      * chains during deletion.  Also let's create some long owners of owners
282      * lists.  */
283     ownerType = random() % 3;
284
285     if (!ownerUser)
286         ownerType = 0;
287     if (!lastGroup)
288         ownerType = 0;
289     switch (ownerType) {
290     case 0:
291         owner = callerId;
292         ownerName = callerName;
293         break;
294     case 1:
295         owner = ownerUser;
296         ownerName = ownerUserName;
297         break;
298     case 2:
299         owner = lastGroup;
300         ownerName = lastGroupPrefix;
301         break;
302     }
303
304     sprintf(name, "%s:%s%d", ownerName, createPrefix, g);
305     code = ubik_PR_NewEntry(pruclient, 0, name, PRGRP, owner, &id);
306     if (code) {
307         if (code == PREXIST) {
308             code = pr_Delete(name);
309             if (code == 0) {
310                 nGDels++;
311                 code =
312                     ubik_PR_NewEntry(pruclient, 0, name, PRGRP, owner,
313                               &id);
314                 if (code == 0) {
315                     if (verbose)
316                         printf("RE-");
317                     goto done;
318                 }
319             }
320         }
321         afs_com_err(whoami, code, "couldn't create %s w/ owner=%d", name, owner);
322         exit(13);
323     }
324   done:
325     if (verbose)
326         printf("Creating group %s (%di)\n", name, id);
327     groups[g] = id;
328     groupOwners[g] = owner;
329     nGroups++;
330     if (!lastGroup || (ownerType == 2)) {
331         lastGroup = id;
332         lastGroupPrefix = ownerName;
333     }
334 }
335
336 int
337 DeleteRandomId(afs_int32 *list)
338 {
339     afs_int32 code;
340     afs_int32 id;
341     int j, k;
342     int m;
343
344     k = random();               /* random starting point */
345     for (j = 0; j < number; j++) {      /* find an undeleted id */
346         m = (k + j) % number;
347         if (id = list[m]) {
348             code = ubik_PR_Delete(pruclient, 0, id);
349             if (code) {
350                 afs_com_err(whoami, code, "Couldn't delete %di", id);
351                 exit(22);
352             }
353             list[m] = 0;
354             if (list == users)
355                 nUDels++;
356             else
357                 nGDels++;
358             return 0;
359         }
360     }
361     return -1;                  /* none left */
362 }
363
364 void
365 AddUser(int u, int g)
366 {
367     afs_int32 code;
368     afs_int32 ui, gi;
369
370     if (users[u] == 0)          /* create if necessary */
371         CreateUser(u);
372     if (groups[g] == 0)         /* create group if necessary */
373         CreateGroup(g);
374     ui = users[u];
375     gi = groups[g];
376     code = ubik_PR_AddToGroup(pruclient, 0, ui, gi);
377     if (code) {
378         afs_com_err(whoami, code, "couldn't add %d to %d", ui, gi);
379         exit(14);
380     }
381     if (verbose)
382         printf("Adding user (%di) to group (%di)\n", ui, gi);
383     population[u * number + g]++;
384     nAdds++;
385 }
386
387 void
388 RemUser(int u, int g)
389 {
390     afs_int32 code;
391     afs_int32 ui, gi;
392
393     ui = users[u];
394     gi = groups[g];
395     code = ubik_PR_RemoveFromGroup(pruclient, 0, ui, gi);
396     if (code) {
397         afs_com_err(whoami, code, "couldn't remove %d from %d", ui, gi);
398         exit(14);
399     }
400     if (verbose)
401         printf("Removing user (%di) from group (%di)\n", ui, gi);
402     population[u * number + g]--;
403     nRems++;
404 }
405
406 int
407 TestManyMembers(struct cmd_syndesc *as, void *arock)
408 {
409     char *filled;               /* users filled up */
410     char *cleaned;              /* users cleaned up */
411
412     int nFilled, nCleaned;
413     int u, g, i, j, n;
414     int seed;                   /* random number generator seed */
415
416     afs_int32 *glist;           /* membership list */
417
418     afs_int32 code;
419
420     code = pr_Initialize(1, conf_dir, NULL);
421     if (code) {
422         afs_com_err(whoami, code, "initializing pruser");
423         exit(1);
424     }
425     /* get name of person running command */
426     {
427         struct ktc_principal afs, user;
428         struct ktc_token token;
429
430         strcpy(afs.name, "afs");
431         strcpy(afs.instance, "");
432         code = afsconf_GetLocalCell(conf, afs.cell, sizeof(afs.cell));
433         if (code)
434             exit(2);
435         code = ktc_GetToken(&afs, &token, sizeof(token), &user);
436         if (code) {
437             afs_com_err(whoami, code, "getting afs tokens");
438             exit(3);
439         }
440         if (strlen(user.instance) > 0) {
441             fprintf(stderr, "can't handle non-null instance %s.%s\n",
442                     user.name, user.cell);
443             exit(4);
444         }
445         if (strncmp(user.name, "AFS ID ", 7) == 0) {
446             callerId = atoi(user.name + 7);
447             code = pr_SIdToName(callerId, callerName);
448             if (code) {
449                 afs_com_err(whoami, code, "call get name for id %d", callerId);
450                 exit(6);
451             }
452         } else {
453             strcpy(callerName, user.name);
454             code = pr_SNameToId(callerName, &callerId);
455             if ((code == 0) && (callerId == ANONYMOUSID))
456                 code = PRNOENT;
457         }
458 #if 0                           /* don't create user */
459         if (code == PRNOENT) {
460             callerId = 0;
461             code = pr_CreateUser(callerName, &callerId);
462             if (code) {
463                 afs_com_err(whoami, code, "can't create caller %s", callerName);
464                 exit(5);
465             }
466             printf("Creating caller %s (%di)\n", callerName, callerId);
467         }
468         /* else */
469 #endif
470         if (code) {
471             afs_com_err(whoami, code, "can't find caller %s", callerName);
472             exit(6);
473         } else
474             printf("Assuming caller is %s (%di)\n", callerName, callerId);
475     }
476
477     /* Parse arguments */
478     if (as->parms[0].items)
479         number = atoi(as->parms[0].items->data);
480     if (as->parms[1].items) {
481         steepDropOff = atoi(as->parms[1].items->data);
482         if ((steepDropOff < 0) || (steepDropOff > 100)) {
483             fprintf(stderr,
484                     "Illegal value for dropoff: %d, must be between 0 and 100, inclusive.\n",
485                     steepDropOff);
486             exit(7);
487         }
488     } else
489         steepDropOff = 0;       /* use quadratic dropoff */
490     if (as->parms[2].items)
491         createPrefix = as->parms[2].items->data;
492     else
493         createPrefix = "u";
494     if (as->parms[3].items)
495         verbose = 1;
496     else
497         verbose = 0;
498     if (as->parms[4].items)
499         seed = atoi(as->parms[4].items->data);
500     else
501         seed = 1;
502
503     srandom(seed);
504
505     users = (afs_int32 *) malloc(number * sizeof(afs_int32));
506     groups = (afs_int32 *) malloc(number * sizeof(afs_int32));
507     filled = (char *)malloc(number * sizeof(char));
508     cleaned = (char *)malloc(number * sizeof(char));
509     population = (char *)malloc(sqr(number) * sizeof(char));
510
511     nFilled = 0;
512     memset(filled, 0, number);
513     nCleaned = 0;
514     memset(cleaned, 0, number);
515     memset(population, 0, sqr(number));
516     memset(users, 0, number * sizeof(afs_int32));
517     memset(groups, 0, number * sizeof(afs_int32));
518
519     ownerUser = lastGroup = 0;
520     groupOwners = (afs_int32 *) malloc(number * sizeof(afs_int32));
521     nUsers = nGroups = nAdds = nRems = nUDels = nGDels = 0;
522
523     while ((nFilled < number) || (nCleaned < number)) {
524         /* pick a user at random, using  */
525         u = random() % number;
526         if (!filled[u]) {
527             n = GetGroupLimit(number, u);       /* get group limit for that user */
528             g = random() % (n + 1);     /* pick a random group */
529             if (g == n) {       /* in a few cases create any user */
530                 n = number;     /* in the whole range */
531                 g = random() % n;
532             }
533             for (i = 0; i < n; i++) {   /* rotate until unused one found */
534                 j = (g + i) % n;
535                 if (!population[u * number + j]) {
536                     /* add this user/group membership */
537                     AddUser(u, j);
538                     goto added;
539                 }
540             }
541             filled[u]++;
542             nFilled++;
543           added:;
544         }
545         if (!cleaned[u]) {
546             int base;
547             if (filled[u]) {    /* only clean above GroupLimit */
548                 base = GetGroupLimit(number, u);
549                 n = number - base;
550                 if (n == 0)
551                     goto iscleaned;
552                 g = random() % n;
553             } else {
554                 base = 0;
555                 n = number;     /* pick a group from the whole range */
556                 g = random() % 2 * n;   /* at random for removal */
557                 if (g >= n)
558                     goto remed; /* but half the time do nothing */
559             }
560             for (i = 0; i < n; i++) {   /* rotate until used one found */
561                 j = (g + i) % n + base;
562                 if (population[u * number + j]) {
563                     /* remove this user/group membership */
564                     RemUser(u, j);
565                     goto remed;
566                 }
567             }
568             if (filled[u]) {    /* track finished ones */
569               iscleaned:
570                 cleaned[u]++;
571                 nCleaned++;
572             }
573           remed:;
574         }
575     }
576
577     /* check the membership list of all users for correctness */
578     printf("Starting check of memberships\n");
579     glist = (afs_int32 *) malloc(number * sizeof(afs_int32));
580     for (u = 0; u < number; u++) {
581         afs_int32 ui = users[u];
582         if (ui) {
583             int i;
584             int ng;             /* number groups */
585             int (*proc) ();     /* membership listing procedure */
586             int over;
587             prlist alist;
588
589             alist.prlist_len = 0;
590             alist.prlist_val = 0;
591             if (random() & 4)
592                 proc = PR_ListElements;
593             else
594                 proc = PR_GetCPS;
595             code = ubik_Call(proc, pruclient, 0, ui, &alist, &over);
596             if (code) {
597                 afs_com_err(whoami, code,
598                         "getting membership list of (%di) using %s", ui,
599                         ((proc == PR_GetCPS) ? "GetCPR" : "ListElements"));
600                 exit(24);
601             }
602             if (over) {
603                 fprintf(stderr, "membership list for id %di too long\n", ui);
604             }
605             ng = 0;
606             for (i = 0; i < number; i++)
607                 if (population[u * number + i])
608                     glist[ng++] = groups[i];
609             qsort(glist, ng, sizeof(afs_int32), IdCmp);
610             if (ng != (alist.prlist_len - ((proc == PR_GetCPS) ? 3 : 0))) {
611                 fprintf(stderr,
612                         "Membership list for %di of unexpected length: was %d but expected %d\n",
613                         ui, alist.prlist_len, ng);
614                 exit(20);
615             }
616             /* all the extra entries for the CPS should be at the end. */
617             code = 0;
618             for (i = 0; i < ng; i++)
619                 if (alist.prlist_val[i] != glist[i]) {
620                     fprintf(stderr,
621                             "membership for %di not correct: was %di but expected %di\n",
622                             ui, alist.prlist_val[i], glist[i]);
623                     code++;
624                 }
625             if (code)
626                 exit(21);
627             if (proc == PR_GetCPS) {
628                 if ((alist.prlist_val[i /* =ng */ ] != AUTHUSERID) ||
629                     (alist.prlist_val[++i] != ANYUSERID)
630                     || (alist.prlist_val[++i] != ui)) {
631                     fprintf(stderr, "CPS doesn't have extra entries\n");
632                     exit(27);
633                 }
634             }
635             if (alist.prlist_val)
636                 free(alist.prlist_val);
637
638             /* User 0 is a member of all groups all of which should also be on
639              * the owner list of the caller or the ownerUser, although there
640              * may also be others.  Check this. */
641             if (u == 0) {
642                 prlist callerList;
643                 prlist ownerList;
644                 prlist lastGroupList;
645                 int i, j, k, l;
646
647                 if (ng != number) {
648                     fprintf(stderr, "User 0 not a member of all groups\n");
649                     exit(26);
650                 }
651 #define GETOWNED(xlist,xid) \
652   (xlist).prlist_val = 0; (xlist).prlist_len = 0; \
653   code = ubik_PR_ListOwned(pruclient, 0, (xid), &(xlist), &over); \
654   if (code) { \
655       afs_com_err (whoami, code, "getting owner list of (%di)", (xid)); \
656       exit (23); } \
657   if (over) \
658       { fprintf (stderr, "membership of id %di too long\n", (xid)); }
659
660                 GETOWNED(callerList, callerId);
661                 GETOWNED(ownerList, ownerUser);
662
663                 /* look for every entry in glist, in all the owner lists */
664                 for (i = j = k = l = 0; i < number; i++) {
665                     while ((j < callerList.prlist_len)
666                            && (callerList.prlist_val[j] < glist[i]))
667                         j++;
668                     while ((k < ownerList.prlist_len)
669                            && (ownerList.prlist_val[k] < glist[i]))
670                         k++;
671 #define PRLISTCMP(l,i) \
672   (((l).prlist_len == 0) || (glist[i] != (l).prlist_val[(i)]))
673                     if (PRLISTCMP(callerList, j) && PRLISTCMP(ownerList, k)) {
674                         for (l = 0; l < number; l++) {
675                             if (groups[l] == glist[i]) {
676                                 if ((groupOwners[l] != callerId)
677                                     && (groupOwners[l] != ownerUser)) {
678                                     GETOWNED(lastGroupList, groupOwners[l]);
679                                     if ((lastGroupList.prlist_len != 1)
680                                         || (lastGroupList.prlist_val[0] !=
681                                             glist[i])) {
682                                         fprintf(stderr,
683                                                 "Group (%di) not on any owner list\n",
684                                                 glist[i]);
685                                         exit(25);
686                                     }
687                                 }
688                                 goto foundLast;
689                             }
690                         }
691                         fprintf(stderr, "unexpected group %di\n", glist[i]);
692                       foundLast:;
693                     }
694                 }
695                 if (callerList.prlist_val)
696                     free(callerList.prlist_val);
697                 if (ownerList.prlist_val)
698                     free(ownerList.prlist_val);
699                 if (lastGroupList.prlist_val)
700                     free(lastGroupList.prlist_val);
701             }
702         }
703     }
704
705     /* cleanup by deleting all the users and groups */
706     printf("Starting deletion of users and groups\n");
707     for (i = 0; i < number; i++) {
708         DeleteRandomId(users);
709         DeleteRandomId(groups);
710     }
711
712     printf
713         ("Created/deleted %d/%d users and %d/%d groups; added %d and removed %d.\n",
714          nUsers, nUDels, nGroups, nGDels, nAdds, nRems);
715     return 0;
716 }
717
718 /* from ka_ConvertBytes included here to avoid circularity */
719 /* Converts a byte string to ascii.  Return the number of unconverted bytes. */
720
721 static int
722 ka_ConvertBytes(char *ascii,            /* output buffer */
723                 int alen,               /* buffer length */
724                 char bs[],              /* byte string */
725                 int bl)                 /* number of bytes */
726 {
727     int i;
728     unsigned char c;
729
730     alen--;                     /* make room for termination */
731     for (i = 0; i < bl; i++) {
732         c = bs[i];
733         if (alen <= 0)
734             return bl - i;
735         if (isalnum(c) || ispunct(c))
736             (*ascii++ = c), alen--;
737         else {
738             if (alen <= 3)
739                 return bl - i;
740             *ascii++ = '\\';
741             *ascii++ = (c >> 6) + '0';
742             *ascii++ = (c >> 3 & 7) + '0';
743             *ascii++ = (c & 7) + '0';
744             alen -= 4;
745         }
746     }
747     *ascii = 0;                 /* terminate string */
748     return 0;
749 }
750
751 /* This runs various tests on the server.  It creates, then deletes, a bunch of
752  * users and groups, so it would be safest to run it on a test database.
753  *
754  * These are the things I check for:
755  *   User names longer than PR_MAXNAMELEN - strlen(cellname).
756  *   Group names longer than PR_MAXNAMELEN.
757  *   User names containing all legal 8-bit ascii characters.  This excludes
758  *     only ':', '@', and '\n'.
759  *   Group names as above, but at least one colon is required, and the owner
760  *     must be correct.
761  */
762
763 int
764 TestPrServ(struct cmd_syndesc *as, void *arock)
765 {
766     afs_int32 id;
767     char name[PR_MAXNAMELEN + 1];
768     char creator[PR_MAXNAMELEN];        /* our name */
769     struct prcheckentry ent;
770     afs_int32 code;
771     int i, j;
772     int maxLen = PR_MAXNAMELEN - 1 - strlen(lcell) - 1;
773
774     code = pr_Initialize(1, conf_dir, NULL);
775     if (code) {
776         afs_com_err(whoami, code, "initializing pruser");
777         exit(1);
778     }
779
780     for (i = 0; i < maxLen; i++)
781         name[i] = 'a';
782     name[i] = 'a';              /* too long a name... */
783     name[i + 1] = 0;
784     id = 0;
785     code = pr_CreateUser(name, &id);
786     if ((code != RXGEN_CC_MARSHAL) && (code != PRBADNAM)) {
787         afs_com_err(whoami, code, "succeeded creating %s", name);
788         exit(2);
789     }
790     name[i] = 0;
791     id = 0;
792     code = pr_CreateUser(name, &id);
793     if (code == PREXIST) {
794         fprintf(stderr, "group already exists, skipping\n");
795         pr_SNameToId(name, &id);
796     } else if (code) {
797         afs_com_err(whoami, code, "failed creating %s", name);
798         exit(3);
799     }
800     if ((code = pr_ListEntry(id, &ent))
801         || (code = pr_SIdToName(ent.creator, creator))) {
802         afs_com_err(whoami, code, "getting creator's name");
803         exit(5);
804     }
805     code = pr_DeleteByID(id);
806     if (code) {
807         afs_com_err(whoami, code, "deleting %s", name);
808         exit(6);
809     }
810     /* now make sure the illegal chars are detected */
811     {
812         char *illegalChars;
813         for (illegalChars = "@:\n"; *illegalChars; illegalChars++) {
814             name[10] = *illegalChars;
815             id = 0;
816             code = pr_CreateUser(name, &id);
817             if (code != PRBADNAM) {
818                 afs_com_err(whoami, code, "succeeded creating %s", name);
819                 exit(8);
820             }
821         }
822     }
823
824     for (i = 1; i <= 255;) {    /* for all 8-bit ascii... */
825         j = 0;                  /* build a new name */
826         while ((j < maxLen) && (i <= 255)) {
827             if (!((i == ':') || (i == '@') || (i == '\n')))
828                 name[j++] = i;
829             i++;
830         }
831         name[j] = 0;            /* terminate string */
832         id = 0;
833         code = pr_CreateUser(name, &id);
834         if (code == PREXIST) {
835             fprintf(stderr, "user already exists, skipping\n");
836             pr_SNameToId(name, &id);
837         } else if (code) {
838             char ascii[BUFSIZ];
839             ka_ConvertBytes(ascii, sizeof(ascii), name, strlen(name));
840             afs_com_err(whoami, code, "failed creating %s", ascii);
841             exit(4);
842         }
843         code = pr_DeleteByID(id);
844         if (code) {
845             afs_com_err(whoami, code, "deleting %s", name);
846             exit(7);
847         }
848     }
849
850     /* now check group names */
851     strcpy(name, creator);
852     strcat(name, ":abcdefghijklmnopqrstuvwxyz");
853     name[0] = 1;                /* bash the owner name */
854     id = 0;
855     code = pr_CreateGroup(name, &id);
856     if (code != PRNOENT) {      /* owner doesn't exist */
857         afs_com_err(whoami, code, "succeeded creating %s", name);
858         exit(9);
859     }
860     name[0] = creator[0];       /* fix owner */
861     /* Make sure the illegal chars are detected */
862     {
863         char *illegalChars;
864         for (illegalChars = ":@\n"; *illegalChars; illegalChars++) {
865             name[strlen(creator) + 10] = *illegalChars;
866             id = 0;
867             code = pr_CreateGroup(name, creator, &id);
868             if (code != PRBADNAM) {
869                 afs_com_err(whoami, code, "succeeded creating %s", name);
870                 exit(10);
871             }
872         }
873     }
874     for (i = 1; i <= 255;) {    /* for all 8-bit ascii... */
875         j = strlen(creator) + 1;        /* build a new name */
876         while ((j < PR_MAXNAMELEN - 1) && (i <= 255)) {
877             if (!((i == ':') || (i == '@') || (i == '\n')))
878                 name[j++] = i;
879             i++;
880         }
881         name[j] = 0;            /* terminate string */
882         id = 0;
883         code = pr_CreateGroup(name, creator, &id);
884         if (code == PREXIST) {
885             fprintf(stderr, "group already exists, skipping\n");
886             pr_SNameToId(name, &id);
887         } else if (code) {
888             char ascii[BUFSIZ];
889             ka_ConvertBytes(ascii, sizeof(ascii), name, strlen(name));
890             afs_com_err(whoami, code, "failed creating %s", ascii);
891             exit(4);
892         }
893         code = pr_DeleteByID(id);
894         if (code) {
895             afs_com_err(whoami, code, "deleting %s", name);
896             exit(7);
897         }
898     }
899
900     printf("All OK\n");
901     return 0;
902 }
903
904 static char tmp_conf_dir[128] = "";
905 static char tmp_conf_file[128] = "";
906 static char tmp_cell_file[128] = "";
907 static char tmp_noauth_file[128] = "";
908
909 static int
910 MyAfterProc(struct cmd_syndesc *as, void *arock)
911 {
912     if (strlen(tmp_conf_file))
913         unlink(tmp_conf_file);
914     if (strlen(tmp_cell_file))
915         unlink(tmp_cell_file);
916     if (strlen(tmp_noauth_file))
917         unlink(tmp_noauth_file);
918     if (strlen(tmp_conf_dir))
919         rmdir(tmp_conf_dir);
920     return 0;
921 }
922
923 static int
924 MyBeforeProc(struct cmd_syndesc *as, void *arock)
925 {
926     afs_int32 code;
927     int i;
928     char *cdir = 0;
929     int noAuth = 0;
930     struct cmd_item *serverList = 0;
931     struct afsconf_dir *local_conf = 0; /* so we can default stuff nicely */
932     struct afsconf_cell cellinfo;
933
934     if (as->parms[12].items) {  /* if conf dir specified */
935         cdir = as->parms[12].items->data;
936         if (as->parms[13].items || as->parms[14].items || as->parms[15].items) {
937             printf("Can't specify conf dir and other cell parameters\n");
938             return AFSCONF_SYNTAX;
939         }
940     }
941
942     /* if we need to default cell name or cell servers, get local conf info */
943
944     if (!(local_conf = afsconf_Open(AFSDIR_CLIENT_ETC_DIRPATH))
945         && !(local_conf = afsconf_Open(AFSDIR_SERVER_ETC_DIRPATH))) {
946         printf("** Can't local configuration!\n");
947         return AFSCONF_NOCELL;
948     }
949
950     if (as->parms[13].items) {  /* if cell name specified */
951         lcstring(lcell, as->parms[13].items->data, sizeof(lcell));
952         code = afsconf_GetCellInfo(local_conf, lcell, 0, &cellinfo);
953         if (code == 0)
954             strncpy(lcell, cellinfo.name, sizeof(lcell));
955     } else {
956         code = afsconf_GetLocalCell(local_conf, lcell, sizeof(lcell));
957         if (code)
958             return code;
959     }
960
961     if (as->parms[14].items) {  /* noauth flag */
962         noAuth = 1;
963     }
964
965     if (as->parms[15].items) {  /* servers list */
966         serverList = as->parms[15].items;
967         for (i = 0; serverList; i++, serverList = serverList->next) {
968             struct hostent *th;
969             if (i >= MAXHOSTSPERCELL)
970                 return AFSCONF_FULL;
971             strncpy(cellinfo.hostName[i], serverList->data, MAXHOSTCHARS);
972             th = gethostbyname(cellinfo.hostName[i]);
973             if (!th)
974                 return UBADHOST;
975             memcpy(&cellinfo.hostAddr[i].sin_addr, th->h_addr,
976                    sizeof(afs_int32));
977             cellinfo.hostAddr[i].sin_family = AF_INET;
978             cellinfo.hostAddr[i].sin_port = 0;
979 #ifdef STRUCT_SOCKADDR_HAS_SA_LEN
980             cellinfo.hostAddr[i].sin_len = sizeof(struct sockaddr_in);
981 #endif
982         }
983         cellinfo.numServers = i;
984         strcpy(cellinfo.name, lcell);
985     } else {
986         code = afsconf_GetCellInfo(local_conf, lcell, 0, &cellinfo);
987         if (code)
988             return code;
989     }
990
991     if (local_conf)
992         afsconf_Close(local_conf);
993
994     if (cdir == 0) {
995         FILE *f;
996
997         sprintf(tmp_conf_dir, "%s/afsconf.%lu", gettmpdir(),
998                 (unsigned long)getpid());
999         code = mkdir(tmp_conf_dir, 0777);
1000         if ((code < 0) && (errno != EEXIST)) {
1001             afs_com_err(whoami, errno, "can't create temporary afsconf dir: %s",
1002                     cdir);
1003             return errno;
1004         }
1005
1006         strcompose(tmp_conf_file, 128, tmp_conf_dir, "/",
1007                    AFSDIR_CELLSERVDB_FILE, NULL);
1008         f = fopen(tmp_conf_file, "w");
1009         if (f == 0) {
1010           cantcreate:
1011             afs_com_err(whoami, errno, "can't create conf file %s",
1012                     tmp_conf_file);
1013             return errno;
1014         }
1015         fprintf(f, ">%s\n", lcell);
1016         for (i = 0; i < cellinfo.numServers; i++) {
1017             register unsigned char *tp =
1018                 (unsigned char *)&cellinfo.hostAddr[i].sin_addr;
1019             fprintf(f, "%d.%d.%d.%d\t#%s\n", tp[0], tp[1], tp[2], tp[3],
1020                     cellinfo.hostName[i]);
1021         }
1022         if (fclose(f) == EOF) {
1023           cantclose:
1024             afs_com_err(whoami, errno, "can't write to conf file %s",
1025                     tmp_conf_file);
1026             return errno;
1027         }
1028
1029         strcompose(tmp_cell_file, 128, tmp_conf_dir, "/",
1030                    AFSDIR_THISCELL_FILE, NULL);
1031         f = fopen(tmp_cell_file, "w");
1032         if (f == 0)
1033             goto cantcreate;
1034         fprintf(f, "%s", lcell);
1035         if (fclose(f) == EOF)
1036             goto cantclose;
1037
1038         strcompose(tmp_noauth_file, 128, tmp_conf_dir, "/",
1039                    AFSDIR_NOAUTH_FILE, NULL);
1040         if (noAuth) {
1041             code = creat(tmp_noauth_file, 0777);
1042             if (code && (errno != EEXIST))
1043                 return errno;
1044         } else {                /* make sure file doesn't exist */
1045             code = unlink(tmp_noauth_file);
1046             if (code && (errno != ENOENT))
1047                 return errno;
1048         }
1049     }
1050
1051     strncpy(conf_dir, tmp_conf_dir, sizeof(conf_dir));
1052     conf = afsconf_Open(conf_dir);
1053     if (conf == 0)
1054         return AFSCONF_NOTFOUND;
1055     return 0;
1056 }
1057
1058 static void
1059 add_std_args(register struct cmd_syndesc *ts)
1060 {
1061     cmd_Seek(ts, 12);
1062     cmd_AddParm(ts, "-confdir", CMD_SINGLE, CMD_OPTIONAL,
1063                 "AFS Conf dir pathname");
1064     cmd_AddParm(ts, "-cell", CMD_SINGLE, CMD_OPTIONAL, "Cell name");
1065     cmd_AddParm(ts, "-noauth", CMD_FLAG, CMD_OPTIONAL, "Don't authenticate");
1066     cmd_AddParm(ts, "-servers", CMD_LIST, CMD_OPTIONAL, "Server config");
1067 }
1068
1069 int
1070 osi_audit()
1071 {
1072 /* OK, this REALLY sucks bigtime, but I can't tell who is calling
1073  * afsconf_CheckAuth easily, and only *SERVERS* should be calling osi_audit
1074  * anyway.  It's gonna give somebody fits to debug, I know, I know.
1075  */
1076     return 0;
1077 }
1078
1079 #include "AFS_component_version_number.c"
1080
1081 int 
1082 main(int argc, char *argv[])
1083 {
1084     afs_int32 code;
1085     struct cmd_syndesc *ts;     /* ptr to parsed command line syntax */
1086
1087     whoami = argv[0];
1088     initialize_CMD_error_table();
1089     initialize_ACFG_error_table();
1090     initialize_KTC_error_table();
1091     initialize_U_error_table();
1092     initialize_PT_error_table();
1093     initialize_RXK_error_table();
1094
1095 #ifdef AFS_NT40_ENV
1096     /* initialize winsock */
1097     if (afs_winsockInit() < 0) {
1098         fprintf(stderr, "%s: couldn't initialize winsock. \n", whoami);
1099         exit(1);
1100     }
1101 #endif
1102
1103     cmd_SetBeforeProc(MyBeforeProc, NULL);
1104     cmd_SetAfterProc(MyAfterProc, NULL);
1105
1106     ts = cmd_CreateSyntax("usedIds", ListUsedIds, NULL,
1107                           "Find used (or unused) user (or group) ids");
1108     cmd_AddParm(ts, "-startId", CMD_SINGLE, CMD_OPTIONAL,
1109                 "id to start checking");
1110     cmd_AddParm(ts, "-number", CMD_SINGLE, CMD_OPTIONAL,
1111                 "number of ids to check");
1112     cmd_AddParm(ts, "-unused", CMD_FLAG, CMD_OPTIONAL, "print unused ids");
1113     add_std_args(ts);
1114
1115     ts = cmd_CreateSyntax("initcmd", TestPrServ, NULL, "test the prserver");
1116     add_std_args(ts);
1117
1118     ts = cmd_CreateSyntax("testmanymembers", TestManyMembers, NULL,
1119                           "test creating users and groups w/ many members");
1120     cmd_AddParm(ts, "-number", CMD_SINGLE, 0,
1121                 "number of users/groups to create");
1122     cmd_AddParm(ts, "-dropoff", CMD_SINGLE, CMD_OPTIONAL,
1123                 "precentage for exponential dropoff");
1124     cmd_AddParm(ts, "-prefix", CMD_SINGLE, CMD_OPTIONAL, "naming prefix");
1125     cmd_AddParm(ts, "-long", CMD_FLAG, CMD_OPTIONAL, "show progress");
1126     cmd_AddParm(ts, "-seed", CMD_SINGLE, CMD_OPTIONAL, "random number seed");
1127     add_std_args(ts);
1128     cmd_CreateAlias(ts, "mm");
1129
1130
1131     code = cmd_Dispatch(argc, argv);
1132     if (code)
1133         afs_com_err(whoami, code, "calling cmd_Dispatch");
1134     exit(code);
1135 }