2 * Copyright 2000, International Business Machines Corporation and others.
5 * This software has been released under the terms of the IBM Public
6 * License. For details, see the LICENSE file in the top-level source
7 * directory or online at http://www.openafs.org/dl/license10.html
10 #include <afsconfig.h>
11 #include <afs/param.h>
18 #include <WINNT/afsevent.h>
23 #include <afs/com_err.h>
24 #include <afs/cellconfig.h>
25 #include <afs/afsutil.h>
32 afs_int32 security = 0;
33 char confdir[AFSDIR_PATH_MAX];
37 extern struct ubik_client *pruclient;
38 static void skip(char **);
39 static void PrintHelp(void);
41 static int ignoreExist = 0;
42 static char line[256];
43 static char *lineProgress;
45 #define WHITESPACE " \t\n"
50 /* OK, this REALLY sucks bigtime, but I can't tell who is calling
51 * afsconf_CheckAuth easily, and only *SERVERS* should be calling osi_audit
52 * anyway. It's gonna give somebody fits to debug, I know, I know.
58 GetToken(char *format, afs_int32 *l)
63 if (lineProgress == 0)
65 c = sscanf(lineProgress, format, l);
68 /* skip the white space */
69 lineProgress += strspn(lineProgress, WHITESPACE);
70 /* skip to end of token */
71 lineProgress = strpbrk(lineProgress, WHITESPACE);
75 #define GetInt32(l) GetToken ("%d", l)
76 #define GetXInt32(l) GetToken ("%x", l)
79 GetString(char *s, int slen)
85 if (lineProgress == 0)
87 /* skip the white space */
88 lineProgress += strspn(lineProgress, WHITESPACE);
90 /* check for quoted string and find end */
93 l = strcspn(++beg, "\"");
95 return -1; /* unbalanced quotes */
96 lineProgress = beg + l + 1;
98 l = strcspn(beg, WHITESPACE);
101 if (l >= slen) { /* don't return too much */
108 s[l] = 0; /* null termination */
113 CodeOk(afs_int32 code)
117 return code && (code != PREXIST) && (code != PRIDEXIST);
121 PrintEntry(afs_int32 ea, struct prentry *e, int indent)
123 /* handle screwed up versions of DumpEntry */
124 if (e->flags & PRCONT) {
127 memcpy(&id, e->name, sizeof(id));
128 if ((id != PRBADID) && ((id > (1 << 24)) || (id < -(1 << 24)))) {
129 /* assume server incorrectly swapped these bytes... */
131 while (i < sizeof(e->name)) {
134 e->name[i] = e->name[i + 3];
135 e->name[i + 3] = temp;
136 temp = e->name[i + 1];
137 e->name[i + 1] = e->name[i + 2];
138 e->name[i + 2] = temp;
143 return pr_PrintEntry(stdout, /*host order */ 1, ea, e, indent);
149 #include "AFS_component_version_number.c"
152 main(int argc, char **argv)
156 char name[PR_MAXNAMELEN];
157 afs_int32 id, oid = ANONYMOUSID, gid;
161 struct prentry entry;
165 struct hostent *hostinfo;
166 struct in_addr *hostaddr;
171 afs_int32 rxgk_level = 0;
175 * The following signal action for AIX is necessary so that in case of a
176 * crash (i.e. core is generated) we can include the user's data section
177 * in the core dump. Unfortunately, by default, only a partial core is
178 * generated which, in many cases, isn't too useful.
180 struct sigaction nsa;
182 sigemptyset(&nsa.sa_mask);
183 nsa.sa_handler = SIG_DFL;
184 nsa.sa_flags = SA_FULLDUMP;
185 sigaction(SIGSEGV, &nsa, NULL);
189 initialize_PT_error_table();
191 strcpy(confdir, AFSDIR_CLIENT_ETC_DIRPATH);
195 int arglen = strlen(argv[n]);
197 lcstring(arg, argv[n], sizeof(arg));
198 #define IsArg(a) (strncmp (arg,a, arglen) == 0)
199 if (IsArg("-testconfdir"))
200 strncpy(confdir, argv[++n], sizeof(confdir));
201 else if (IsArg("client"))
202 strncpy(confdir, AFSDIR_CLIENT_ETC_DIRPATH, sizeof(confdir));
203 else if (IsArg("server"))
204 strncpy(confdir, AFSDIR_SERVER_ETC_DIRPATH, sizeof(confdir));
205 else if (IsArg("0") || IsArg("1") || IsArg("2") || IsArg("3"))
206 security = atoi(argv[n]);
207 else if (IsArg("-rxgk"))
208 rxgk_level = atoi(argv[++n]);
209 else if (IsArg("-ignoreexist"))
211 else if (IsArg("-cell"))
215 ("Usage is: 'prclient [-testconfdir <dir> | server | client] [0 | 1 | 2 | 3] [-rxgk 0 | 1 | 2] [-ignoreExist] [-cell <cellname>]\n");
221 printf("Using CellServDB file in %s\n", confdir);
223 printf("Making unauthenticated connection to prserver\n");
225 code = pr_Initialize2(security, confdir, cell, rxgk_level);
227 afs_com_err(whoami, code, "Couldn't initialize protection library");
235 s = fgets(line, sizeof(line), stdin);
240 code = GetString(op, sizeof(op));
242 afs_com_err(whoami, PRBADARG,
243 "error reading opcode in line '%s', got '%.*s'", line,
244 (int) sizeof(op), op);
248 continue; /* no input */
250 if (!strcmp(op, "cr")) {
251 if (GetString(name, sizeof(name)) || GetInt32(&id)
254 /* use ubik_Call to do the work, finding an up server and handling
255 * the job of finding a sync site, if need be */
257 code = ubik_PR_INewEntry(pruclient, 0, name, id, oid);
259 afs_com_err(whoami, code, "on %s %s %d %d", op, name, id, oid);
260 } else if (!strcmp(op, "sf")) {
261 afs_int32 mask, access, gq, uq;
262 if (GetInt32(&id) || GetXInt32(&mask) || GetXInt32(&access)
263 || GetInt32(&gq) || GetInt32(&uq))
267 ubik_PR_SetFieldsEntry(pruclient, 0, id, mask,
268 access, gq, uq, 0, 0);
270 afs_com_err(whoami, code, "on %s %d %x %x %d %d", op, id, mask,
272 } else if (!strcmp(op, "ce")) {
273 char newname[PR_MAXNAMELEN];
275 if (GetInt32(&id) || GetString(newname, sizeof(newname))
276 || GetInt32(&oid) || GetInt32(&newid))
280 ubik_PR_ChangeEntry(pruclient, 0, id, newname, oid,
283 afs_com_err(whoami, code, "on %s %d %s %d %d", op, id, newname,
285 } else if (!strcmp(op, "wh")) {
286 /* scanf("%d",&id); */
290 code = ubik_PR_WhereIsIt(pruclient, 0, id, &pos);
292 printf("%s\n", pr_ErrorMsg(code));
294 printf("location %d\n", pos);
295 } else if (!strcmp(op, "du")) {
296 memset(&entry, 0, sizeof(entry));
297 /* scanf("%d",&pos); */
301 code = ubik_PR_DumpEntry(pruclient, 0, pos, (struct prdebugentry *)&entry);
303 printf("%s\n", pr_ErrorMsg(code));
304 if (code == PRSUCCESS) {
305 PrintEntry(pos, &entry, /*indent */ 0);
307 } else if (!strcmp(op, "add") || !strcmp(op, "au")) {
308 /* scanf("%d %d",&id,&gid); */
309 if (GetInt32(&id) || GetInt32(&gid))
312 code = ubik_PR_AddToGroup(pruclient, 0, id, gid);
314 afs_com_err(whoami, code, "on %s %d %d", op, id, gid);
315 } else if (!strcmp(op, "iton")) {
316 lid.idlist_val = malloc(20 * sizeof(afs_int32));
317 ptr = lid.idlist_val;
321 while ((lid.idlist_len < 20) && (sscanf(foo, "%d", ptr) != EOF)) {
327 fprintf(stderr, "too many values specified; max is %d\n", 20);
329 lnames.namelist_val = 0;
330 lnames.namelist_len = 0;
331 code = ubik_PR_IDToName(pruclient, 0, &lid, &lnames);
333 printf("%s\n", pr_ErrorMsg(code));
334 if (code == PRSUCCESS) {
335 for (i = 0; i < lnames.namelist_len; i++) {
336 printf("id %d name %s\n", lid.idlist_val[i],
337 lnames.namelist_val[i]);
339 free(lnames.namelist_val);
341 free(lid.idlist_val);
344 } else if (!strcmp(op, "ntoi")) {
345 lnames.namelist_val = malloc(PR_MAXLIST * PR_MAXNAMELEN);
346 lnames.namelist_len = 0;
349 for (i = 0; ((lnames.namelist_len < PR_MAXLIST)
350 && (sscanf(foo, "%63s", lnames.namelist_val[i]) !=
352 lnames.namelist_len++;
356 fprintf(stderr, "too many values specified; max is %d\n",
361 code = ubik_PR_NameToID(pruclient, 0, &lnames, &lid);
363 printf("%s\n", pr_ErrorMsg(code));
364 if (code == PRSUCCESS) {
365 for (i = 0; i < lid.idlist_len; i++)
366 printf("name %s id %d\n", lnames.namelist_val[i],
368 free(lid.idlist_val);
370 free(lnames.namelist_val);
371 lnames.namelist_val = 0;
372 lnames.namelist_len = 0;
373 } else if (!strcmp(op, "del")) {
374 /* scanf("%d",&id); */
378 code = ubik_PR_Delete(pruclient, 0, id);
380 printf("%s\n", pr_ErrorMsg(code));
381 } else if (!strcmp(op, "dg")) {
382 /* scanf("%d",&id); */
386 code = ubik_PR_Delete(pruclient, 0, id);
388 printf("%s\n", pr_ErrorMsg(code));
389 } else if (!strcmp(op, "rm")) {
390 /* scanf("%d %d",&id,&gid); */
391 if (GetInt32(&id) || GetInt32(&gid))
394 code = ubik_PR_RemoveFromGroup(pruclient, 0, id, gid);
396 printf("%s\n", pr_ErrorMsg(code));
398 #if defined(SUPERGROUPS)
399 else if (!strcmp(op, "lsg")) {
400 alist.prlist_len = 0;
401 alist.prlist_val = 0;
402 /* scanf("%d",&id); */
407 ubik_PR_ListSuperGroups(pruclient, 0, id, &alist,
410 printf("%s\n", pr_ErrorMsg(code));
411 if (code == PRSUCCESS) {
412 ptr = alist.prlist_val;
414 printf("Number of groups greater than PR_MAXGROUPS!\n");
415 printf("Excess of %d.\n", over);
417 for (i = 0; i < alist.prlist_len; i++, ptr++)
418 printf("%d\n", *ptr);
419 free(alist.prlist_val);
420 alist.prlist_len = 0;
421 alist.prlist_val = 0;
424 #endif /* SUPERGROUPS */
425 else if (!strcmp(op, "l")) {
426 alist.prlist_len = 0;
427 alist.prlist_val = 0;
428 /* scanf("%d",&id); */
432 code = ubik_PR_GetCPS(pruclient, 0, id, &alist, &over);
434 printf("%s\n", pr_ErrorMsg(code));
435 if (code == PRSUCCESS) {
436 ptr = alist.prlist_val;
438 printf("Number of groups greater than PR_MAXGROUPS!\n");
439 printf("Excess of %d.\n", over);
441 for (i = 0; i < alist.prlist_len; i++, ptr++)
442 printf("%d\n", *ptr);
443 free(alist.prlist_val);
444 alist.prlist_len = 0;
445 alist.prlist_val = 0;
447 } else if (!strcmp(op, "lh")) {
448 alist.prlist_len = 0;
449 alist.prlist_val = 0;
450 /* scanf("%d",&id); */
451 if (GetString(name, sizeof(name)))
453 else if (!(hostinfo = gethostbyname(name)))
456 hostaddr = (struct in_addr *)hostinfo->h_addr_list[0];
457 id = ntohl(hostaddr->s_addr);
459 ubik_PR_GetHostCPS(pruclient, 0, id, &alist, &over);
462 printf("%s\n", pr_ErrorMsg(code));
463 if (code == PRSUCCESS) {
464 ptr = alist.prlist_val;
466 printf("Number of groups greater than PR_MAXGROUPS!\n");
467 printf("Excess of %d.\n", over);
469 for (i = 0; i < alist.prlist_len; i++, ptr++)
470 printf("%d\n", *ptr);
471 free(alist.prlist_val);
472 alist.prlist_len = 0;
473 alist.prlist_val = 0;
476 #if defined(SUPERGROUPS)
477 else if (!strcmp(op, "m")) {
478 alist.prlist_len = 0;
479 alist.prlist_val = 0;
480 /* scanf("%d",&id); */
485 ubik_PR_ListElements(pruclient, 0, id, &alist,
488 printf("%s\n", pr_ErrorMsg(code));
489 if (code == PRSUCCESS) {
490 ptr = alist.prlist_val;
492 printf("Number of groups greater than PR_MAXGROUPS!\n");
493 printf("Excess of %d.\n", over);
495 for (i = 0; i < alist.prlist_len; i++, ptr++)
496 printf("%d\n", *ptr);
497 free(alist.prlist_val);
498 alist.prlist_len = 0;
499 alist.prlist_val = 0;
502 #endif /* SUPERGROUPS */
503 else if (!strcmp(op, "nu")) {
504 /* scanf("%s",name); */
505 if (GetString(name, sizeof(name)))
508 code = pr_CreateUser(name, &id);
510 printf("%s\n", pr_ErrorMsg(code));
511 if (code == PRSUCCESS)
512 printf("Id is %d.\n", id);
513 } else if (!strcmp(op, "ng")) {
514 /* scanf("%s",name); */
515 if (GetString(name, sizeof(name)))
518 code = ubik_PR_NewEntry(pruclient, 0, name, 1, oid, &id);
520 printf("%s\n", pr_ErrorMsg(code));
521 if (code == PRSUCCESS)
522 printf("Id is %d.\n", id);
523 } else if (!strcmp(op, "lm")) {
524 code = ubik_PR_ListMax(pruclient, 0, &id, &gid);
526 printf("%s\n", pr_ErrorMsg(code));
527 if (code == PRSUCCESS)
528 printf("Max user id is %d, max (really min) group is %d.\n",
530 } else if (!strcmp(op, "smu")) {
531 /* scanf("%d",&id); */
535 code = ubik_PR_SetMax(pruclient, 0, id, 0);
537 printf("%s\n", pr_ErrorMsg(code));
538 } else if (!strcmp(op, "smg")) {
539 /* scanf("%d",&id); */
543 code = ubik_PR_SetMax(pruclient, 0, id, 1);
545 printf("%s\n", pr_ErrorMsg(code));
546 } else if (!strcmp(op, "sin")) {
547 /* scanf("%d",&id); */
551 code = pr_SIdToName(id, name);
553 printf("%s\n", pr_ErrorMsg(code));
554 if (code == PRSUCCESS)
555 printf("id %d name %s\n", id, name);
556 } else if (!strcmp(op, "sni")) {
557 /* scanf("%s",name); */
558 if (GetString(name, sizeof(name)))
561 code = pr_SNameToId(name, &id);
563 printf("%s\n", pr_ErrorMsg(code));
564 if (code == PRSUCCESS)
565 printf("name %s id %d\n", name, id);
566 } else if (!strcmp(op, "fih")) {
568 struct PrUpdateEntry uentry;
569 memset(&uentry, 0, sizeof(uentry));
570 /* scanf("%s",name); */
571 if (GetString(name, sizeof(name))) {
575 code = pr_SNameToId(name, &id);
577 printf("%s\n", pr_ErrorMsg(code));
580 code = pr_SIdToName(id, tname);
581 if (code == PRSUCCESS) {
583 ("Warning: Id hash for %s (id %d) seems correct at the db; rehashing it anyway\n",
587 uentry.Mask = PRUPDATE_IDHASH;
588 code = ubik_PR_UpdateEntry(pruclient, 0, 0, name, &uentry);
590 printf("Failed to update entry %s (err=%d)\n", name, code);
593 } else if (!strcmp(op, "fnh")) {
595 struct PrUpdateEntry uentry;
596 memset(&uentry, 0, sizeof(uentry));
597 /* scanf("%d", &id); */
602 code = pr_SIdToName(id, name);
604 printf("%s\n", pr_ErrorMsg(code));
607 code = pr_SNameToId(name, &tid);
608 if (code == PRSUCCESS) {
610 ("Name hash for %d (name is %s) seems correct at the db; rehashing it anyway\n",
614 uentry.Mask = PRUPDATE_NAMEHASH;
616 ubik_PR_UpdateEntry(pruclient, 0, id, "_foo_", &uentry);
618 printf("Failed to update entry with id %d (err=%d)\n", id,
623 #if defined(SUPERGROUPS)
624 else if (!strcmp(op, "fih")) {
626 struct PrUpdateEntry uentry;
627 memset(&uentry, 0, sizeof(uentry));
628 /* scanf("%s",name); */
629 if (GetString(name, sizeof(name))) {
633 code = pr_SNameToId(name, &id);
635 printf("%s\n", pr_ErrorMsg(code));
638 code = pr_SIdToName(id, tname);
639 if (code == PRSUCCESS) {
641 ("Warning: Id hash for %s (id %d) seems correct at the db; rehashing it anyway\n",
645 uentry.Mask = PRUPDATE_IDHASH;
646 code = ubik_PR_UpdateEntry(pruclient, 0, 0, name, &uentry);
648 printf("Failed to update entry %s (err=%d)\n", name, code);
651 } else if (!strcmp(op, "fnh")) {
653 struct PrUpdateEntry uentry;
654 memset(&uentry, 0, sizeof(uentry));
655 /* scanf("%d", &id); */
660 code = pr_SIdToName(id, name);
662 printf("%s\n", pr_ErrorMsg(code));
665 code = pr_SNameToId(name, &tid);
666 if (code == PRSUCCESS) {
668 ("Name hash for %d (name is %s) seems correct at the db; rehashing it anyway\n",
672 uentry.Mask = PRUPDATE_NAMEHASH;
674 ubik_PR_UpdateEntry(pruclient, 0, id, "_foo_", &uentry);
676 printf("Failed to update entry with id %d (err=%d)\n", id,
681 #endif /* SUPERGROUPS */
682 else if (!strcmp(op, "?"))
684 else if (!strcmp(op, "q"))
687 printf("Unknown op: '%s'! ? for help\n", op);
696 printf("cr name id owner - create entry with name and id.\n");
697 printf("wh id - what is the offset into database for id?\n");
698 printf("du offset - dump the contents of the entry at offset.\n");
699 printf("add uid gid - add user uid to group gid.\n");
700 printf("iton id* - translate the list of id's to names.\n");
701 printf("ntoi name* - translate the list of names to ids.\n");
702 printf("del id - delete the entry for id.\n");
703 printf("dg gid - delete the entry for group gid.\n");
704 printf("rm id gid - remove user id from group gid.\n");
705 printf("l id - get the CPS for id.\n");
706 printf("lh host - get the host CPS for host.\n");
707 #if defined(SUPERGROUPS)
708 printf("lsg id - get the supergroups for id.\n");
709 printf("m id - list elements for id.\n");
711 printf("nu name - create new user with name - returns an id.\n");
712 printf("ng name - create new group with name - returns an id.\n");
713 printf("lm - list max user id and max (really min) group id.\n");
714 printf("smu - set max user id.\n");
715 printf("smg - set max group id.\n");
716 printf("sin id - single iton.\n");
717 printf("sni name - single ntoi.\n");
718 printf("fih name - fix id hash for <name>.\n");
719 printf("fnh id - fix name hash for <id>.\n");
720 printf("q - quit.\n?- this message.\n");
726 while (**s != ' ' && **s != '\0')