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 printf("The contents of the entry for %d are:\n", entry.id);
308 printf("flags %d next %d\n", entry.flags, entry.next);
309 printf("Groups (or members) \n");
310 for (i = 0; i < PRSIZE; i++)
311 printf("%d\n", entry.entries[i]);
312 printf("nextID %d nextname %d name %s\n", entry.nextID,
313 entry.nextName, entry.name);
314 printf("owner %d creator %d\n", entry.owner, entry.creator);
317 } else if (!strcmp(op, "add") || !strcmp(op, "au")) {
318 /* scanf("%d %d",&id,&gid); */
319 if (GetInt32(&id) || GetInt32(&gid))
322 code = ubik_PR_AddToGroup(pruclient, 0, id, gid);
324 afs_com_err(whoami, code, "on %s %d %d", op, id, gid);
325 } else if (!strcmp(op, "iton")) {
326 lid.idlist_val = malloc(20 * sizeof(afs_int32));
327 ptr = lid.idlist_val;
331 while ((lid.idlist_len < 20) && (sscanf(foo, "%d", ptr) != EOF)) {
337 fprintf(stderr, "too many values specified; max is %d\n", 20);
339 lnames.namelist_val = 0;
340 lnames.namelist_len = 0;
341 code = ubik_PR_IDToName(pruclient, 0, &lid, &lnames);
343 printf("%s\n", pr_ErrorMsg(code));
344 if (code == PRSUCCESS) {
345 for (i = 0; i < lnames.namelist_len; i++) {
346 printf("id %d name %s\n", lid.idlist_val[i],
347 lnames.namelist_val[i]);
349 free(lnames.namelist_val);
351 free(lid.idlist_val);
354 } else if (!strcmp(op, "ntoi")) {
355 lnames.namelist_val = malloc(PR_MAXLIST * PR_MAXNAMELEN);
356 lnames.namelist_len = 0;
359 for (i = 0; ((lnames.namelist_len < PR_MAXLIST)
360 && (sscanf(foo, "%63s", lnames.namelist_val[i]) !=
362 lnames.namelist_len++;
366 fprintf(stderr, "too many values specified; max is %d\n",
371 code = ubik_PR_NameToID(pruclient, 0, &lnames, &lid);
373 printf("%s\n", pr_ErrorMsg(code));
374 if (code == PRSUCCESS) {
375 for (i = 0; i < lid.idlist_len; i++)
376 printf("name %s id %d\n", lnames.namelist_val[i],
378 free(lid.idlist_val);
380 free(lnames.namelist_val);
381 lnames.namelist_val = 0;
382 lnames.namelist_len = 0;
383 } else if (!strcmp(op, "del")) {
384 /* scanf("%d",&id); */
388 code = ubik_PR_Delete(pruclient, 0, id);
390 printf("%s\n", pr_ErrorMsg(code));
391 } else if (!strcmp(op, "dg")) {
392 /* scanf("%d",&id); */
396 code = ubik_PR_Delete(pruclient, 0, id);
398 printf("%s\n", pr_ErrorMsg(code));
399 } else if (!strcmp(op, "rm")) {
400 /* scanf("%d %d",&id,&gid); */
401 if (GetInt32(&id) || GetInt32(&gid))
404 code = ubik_PR_RemoveFromGroup(pruclient, 0, id, gid);
406 printf("%s\n", pr_ErrorMsg(code));
408 #if defined(SUPERGROUPS)
409 else if (!strcmp(op, "lsg")) {
410 alist.prlist_len = 0;
411 alist.prlist_val = 0;
412 /* scanf("%d",&id); */
417 ubik_PR_ListSuperGroups(pruclient, 0, id, &alist,
420 printf("%s\n", pr_ErrorMsg(code));
421 if (code == PRSUCCESS) {
422 ptr = alist.prlist_val;
424 printf("Number of groups greater than PR_MAXGROUPS!\n");
425 printf("Excess of %d.\n", over);
427 for (i = 0; i < alist.prlist_len; i++, ptr++)
428 printf("%d\n", *ptr);
429 free(alist.prlist_val);
430 alist.prlist_len = 0;
431 alist.prlist_val = 0;
434 #endif /* SUPERGROUPS */
435 else if (!strcmp(op, "l")) {
436 alist.prlist_len = 0;
437 alist.prlist_val = 0;
438 /* scanf("%d",&id); */
442 code = ubik_PR_GetCPS(pruclient, 0, id, &alist, &over);
444 printf("%s\n", pr_ErrorMsg(code));
445 if (code == PRSUCCESS) {
446 ptr = alist.prlist_val;
448 printf("Number of groups greater than PR_MAXGROUPS!\n");
449 printf("Excess of %d.\n", over);
451 for (i = 0; i < alist.prlist_len; i++, ptr++)
452 printf("%d\n", *ptr);
453 free(alist.prlist_val);
454 alist.prlist_len = 0;
455 alist.prlist_val = 0;
457 } else if (!strcmp(op, "lh")) {
458 alist.prlist_len = 0;
459 alist.prlist_val = 0;
460 /* scanf("%d",&id); */
461 if (GetString(name, sizeof(name)))
463 else if (!(hostinfo = gethostbyname(name)))
466 hostaddr = (struct in_addr *)hostinfo->h_addr_list[0];
467 id = ntohl(hostaddr->s_addr);
469 ubik_PR_GetHostCPS(pruclient, 0, id, &alist, &over);
472 printf("%s\n", pr_ErrorMsg(code));
473 if (code == PRSUCCESS) {
474 ptr = alist.prlist_val;
476 printf("Number of groups greater than PR_MAXGROUPS!\n");
477 printf("Excess of %d.\n", over);
479 for (i = 0; i < alist.prlist_len; i++, ptr++)
480 printf("%d\n", *ptr);
481 free(alist.prlist_val);
482 alist.prlist_len = 0;
483 alist.prlist_val = 0;
486 #if defined(SUPERGROUPS)
487 else if (!strcmp(op, "m")) {
488 alist.prlist_len = 0;
489 alist.prlist_val = 0;
490 /* scanf("%d",&id); */
495 ubik_PR_ListElements(pruclient, 0, id, &alist,
498 printf("%s\n", pr_ErrorMsg(code));
499 if (code == PRSUCCESS) {
500 ptr = alist.prlist_val;
502 printf("Number of groups greater than PR_MAXGROUPS!\n");
503 printf("Excess of %d.\n", over);
505 for (i = 0; i < alist.prlist_len; i++, ptr++)
506 printf("%d\n", *ptr);
507 free(alist.prlist_val);
508 alist.prlist_len = 0;
509 alist.prlist_val = 0;
512 #endif /* SUPERGROUPS */
513 else if (!strcmp(op, "nu")) {
514 /* scanf("%s",name); */
515 if (GetString(name, sizeof(name)))
518 code = pr_CreateUser(name, &id);
520 printf("%s\n", pr_ErrorMsg(code));
521 if (code == PRSUCCESS)
522 printf("Id is %d.\n", id);
523 } else if (!strcmp(op, "ng")) {
524 /* scanf("%s",name); */
525 if (GetString(name, sizeof(name)))
528 code = ubik_PR_NewEntry(pruclient, 0, name, 1, oid, &id);
530 printf("%s\n", pr_ErrorMsg(code));
531 if (code == PRSUCCESS)
532 printf("Id is %d.\n", id);
533 } else if (!strcmp(op, "lm")) {
534 code = ubik_PR_ListMax(pruclient, 0, &id, &gid);
536 printf("%s\n", pr_ErrorMsg(code));
537 if (code == PRSUCCESS)
538 printf("Max user id is %d, max (really min) group is %d.\n",
540 } else if (!strcmp(op, "smu")) {
541 /* scanf("%d",&id); */
545 code = ubik_PR_SetMax(pruclient, 0, id, 0);
547 printf("%s\n", pr_ErrorMsg(code));
548 } else if (!strcmp(op, "smg")) {
549 /* scanf("%d",&id); */
553 code = ubik_PR_SetMax(pruclient, 0, id, 1);
555 printf("%s\n", pr_ErrorMsg(code));
556 } else if (!strcmp(op, "sin")) {
557 /* scanf("%d",&id); */
561 code = pr_SIdToName(id, name);
563 printf("%s\n", pr_ErrorMsg(code));
564 if (code == PRSUCCESS)
565 printf("id %d name %s\n", id, name);
566 } else if (!strcmp(op, "sni")) {
567 /* scanf("%s",name); */
568 if (GetString(name, sizeof(name)))
571 code = pr_SNameToId(name, &id);
573 printf("%s\n", pr_ErrorMsg(code));
574 if (code == PRSUCCESS)
575 printf("name %s id %d\n", name, id);
576 } else if (!strcmp(op, "fih")) {
578 struct PrUpdateEntry uentry;
579 memset(&uentry, 0, sizeof(uentry));
580 /* scanf("%s",name); */
581 if (GetString(name, sizeof(name))) {
585 code = pr_SNameToId(name, &id);
587 printf("%s\n", pr_ErrorMsg(code));
590 code = pr_SIdToName(id, tname);
591 if (code == PRSUCCESS) {
593 ("Warning: Id hash for %s (id %d) seems correct at the db; rehashing it anyway\n",
597 uentry.Mask = PRUPDATE_IDHASH;
598 code = ubik_PR_UpdateEntry(pruclient, 0, 0, name, &uentry);
600 printf("Failed to update entry %s (err=%d)\n", name, code);
603 } else if (!strcmp(op, "fnh")) {
605 struct PrUpdateEntry uentry;
606 memset(&uentry, 0, sizeof(uentry));
607 /* scanf("%d", &id); */
612 code = pr_SIdToName(id, name);
614 printf("%s\n", pr_ErrorMsg(code));
617 code = pr_SNameToId(name, &tid);
618 if (code == PRSUCCESS) {
620 ("Name hash for %d (name is %s) seems correct at the db; rehashing it anyway\n",
624 uentry.Mask = PRUPDATE_NAMEHASH;
626 ubik_PR_UpdateEntry(pruclient, 0, id, "_foo_", &uentry);
628 printf("Failed to update entry with id %d (err=%d)\n", id,
633 #if defined(SUPERGROUPS)
634 else if (!strcmp(op, "fih")) {
636 struct PrUpdateEntry uentry;
637 memset(&uentry, 0, sizeof(uentry));
638 /* scanf("%s",name); */
639 if (GetString(name, sizeof(name))) {
643 code = pr_SNameToId(name, &id);
645 printf("%s\n", pr_ErrorMsg(code));
648 code = pr_SIdToName(id, tname);
649 if (code == PRSUCCESS) {
651 ("Warning: Id hash for %s (id %d) seems correct at the db; rehashing it anyway\n",
655 uentry.Mask = PRUPDATE_IDHASH;
656 code = ubik_PR_UpdateEntry(pruclient, 0, 0, name, &uentry);
658 printf("Failed to update entry %s (err=%d)\n", name, code);
661 } else if (!strcmp(op, "fnh")) {
663 struct PrUpdateEntry uentry;
664 memset(&uentry, 0, sizeof(uentry));
665 /* scanf("%d", &id); */
670 code = pr_SIdToName(id, name);
672 printf("%s\n", pr_ErrorMsg(code));
675 code = pr_SNameToId(name, &tid);
676 if (code == PRSUCCESS) {
678 ("Name hash for %d (name is %s) seems correct at the db; rehashing it anyway\n",
682 uentry.Mask = PRUPDATE_NAMEHASH;
684 ubik_PR_UpdateEntry(pruclient, 0, id, "_foo_", &uentry);
686 printf("Failed to update entry with id %d (err=%d)\n", id,
691 #endif /* SUPERGROUPS */
692 else if (!strcmp(op, "?"))
694 else if (!strcmp(op, "q"))
697 printf("Unknown op: '%s'! ? for help\n", op);
706 printf("cr name id owner - create entry with name and id.\n");
707 printf("wh id - what is the offset into database for id?\n");
708 printf("du offset - dump the contents of the entry at offset.\n");
709 printf("add uid gid - add user uid to group gid.\n");
710 printf("iton id* - translate the list of id's to names.\n");
711 printf("ntoi name* - translate the list of names to ids.\n");
712 printf("del id - delete the entry for id.\n");
713 printf("dg gid - delete the entry for group gid.\n");
714 printf("rm id gid - remove user id from group gid.\n");
715 printf("l id - get the CPS for id.\n");
716 printf("lh host - get the host CPS for host.\n");
717 #if defined(SUPERGROUPS)
718 printf("lsg id - get the supergroups for id.\n");
719 printf("m id - list elements for id.\n");
721 printf("nu name - create new user with name - returns an id.\n");
722 printf("ng name - create new group with name - returns an id.\n");
723 printf("lm - list max user id and max (really min) group id.\n");
724 printf("smu - set max user id.\n");
725 printf("smg - set max group id.\n");
726 printf("sin id - single iton.\n");
727 printf("sni name - single ntoi.\n");
728 printf("fih name - fix id hash for <name>.\n");
729 printf("fnh id - fix name hash for <id>.\n");
730 printf("q - quit.\n?- this message.\n");
736 while (**s != ' ' && **s != '\0')