1 /* Copyright (C) 1990, 1998 Transarc Corporation - All rights reserved */
3 * (C) COPYRIGHT IBM CORPORATION 1988
4 * LICENSED MATERIALS - PROPERTY OF IBM
11 #include <sys/types.h>
14 #include <WINNT/afsevent.h>
16 #include <netinet/in.h>
23 #include <afs/com_err.h>
24 #include <afs/cellconfig.h>
27 #include <afs/afsutil.h>
30 afs_int32 security = 0;
31 char confdir[AFSDIR_PATH_MAX];
35 extern struct ubik_client *pruclient;
37 static int ignoreExist = 0;
38 static char line[256];
39 static char *lineProgress;
41 #define WHITESPACE " \t\n"
43 #ifndef AFS_PTHREAD_ENV
46 /* OK, this REALLY sucks bigtime, but I can't tell who is calling
47 * afsconf_CheckAuth easily, and only *SERVERS* should be calling osi_audit
48 * anyway. It's gonna give somebody fits to debug, I know, I know.
52 #endif /* !AFS_PTHREAD_ENV */
54 int GetToken (format, l)
61 if (lineProgress == 0) lineProgress = line;
62 c = sscanf (lineProgress, format, l);
63 if (c != 1) return -1;
64 /* skip the white space */
65 lineProgress += strspn (lineProgress, WHITESPACE);
66 /* skip to end of token */
67 lineProgress = strpbrk (lineProgress, WHITESPACE);
71 #define GetInt32(l) GetToken ("%d", l)
72 #define GetXInt32(l) GetToken ("%x", l)
74 int GetString (s, slen)
82 if (lineProgress == 0) lineProgress = line;
83 /* skip the white space */
84 lineProgress += strspn (lineProgress, WHITESPACE);
86 /* check for quoted string and find end */
89 l = strcspn (++beg, "\"");
90 if (l == strlen(beg)) return -1; /* unbalanced quotes */
91 lineProgress = beg+l+1;
93 l = strcspn (beg, WHITESPACE);
96 if (l >= slen) { /* don't return too much */
102 s[l] = 0; /* null termination */
109 if (!ignoreExist) return code;
110 return code && (code != PREXIST) && (code != PRIDEXIST);
113 int PrintEntry (ea, e, indent)
118 /* handle screwed up versions of DumpEntry */
119 if (e->flags & PRCONT) {
120 afs_int32 id = *(afs_int32 *)(e->name);
121 if ((id != PRBADID) && ((id > (1<<24)) || (id < -(1<<24)))) {
122 /* assume server incorrectly swapped these bytes... */
124 while (i<sizeof(e->name)) {
127 e->name[i] = e->name[i+3];
130 e->name[i+1] = e->name[i+2];
136 return pr_PrintEntry (stdout, /*host order*/1, ea, e, indent);
139 #ifndef AFS_PTHREAD_ENV
143 #include "AFS_component_version_number.c"
149 register afs_int32 code;
151 char name[PR_MAXNAMELEN];
152 afs_int32 id, oid, gid;
155 struct prentry entry;
166 * The following signal action for AIX is necessary so that in case of a
167 * crash (i.e. core is generated) we can include the user's data section
168 * in the core dump. Unfortunately, by default, only a partial core is
169 * generated which, in many cases, isn't too useful.
171 struct sigaction nsa;
173 sigemptyset(&nsa.sa_mask);
174 nsa.sa_handler = SIG_DFL;
175 nsa.sa_flags = SA_FULLDUMP;
176 sigaction(SIGSEGV, &nsa, NULL);
180 initialize_pt_error_table();
182 strcpy(confdir, AFSDIR_CLIENT_ETC_DIRPATH);
186 int arglen = strlen(argv[i]);
188 lcstring (arg, argv[i], sizeof(arg));
189 #define IsArg(a) (strncmp (arg,a, arglen) == 0)
190 if (IsArg("-testconfdir"))
191 strncpy (confdir, argv[++i], sizeof(confdir));
192 else if (IsArg("client"))
193 strncpy(confdir, AFSDIR_CLIENT_ETC_DIRPATH, sizeof(confdir));
194 else if (IsArg("server"))
195 strncpy(confdir, AFSDIR_SERVER_ETC_DIRPATH, sizeof(confdir));
196 else if (IsArg("0") || IsArg("1") || IsArg("2"))
197 security = atoi(argv[i]);
198 else if (IsArg("-ignoreexist"))
200 else if (IsArg("-cell"))
203 printf ("Usage is: 'prclient [-testconfdir <dir> | server | client] [0 | 1 | 2] [-ignoreExist] [-cell <cellname>]\n");
209 printf ("Using CellServDB file in %s\n", confdir);
211 printf ("Making unauthenticated connection to prserver\n");
213 code = pr_Initialize (security, confdir, cell);
215 com_err (whoami, code, "Couldn't initialize protection library");
223 s = fgets(line, sizeof(line), stdin);
224 if (s == NULL) break;
227 code = GetString (op, sizeof(op));
229 com_err (whoami, PRBADARG,
230 "error reading opcode in line '%s', got '%.*s'",
231 line, sizeof(op), op);
234 if (strlen(op) == 0) continue; /* no input */
236 if (!strcmp(op, "cr")) {
237 if (GetString (name, sizeof(name)) ||
239 GetInt32 (&oid)) code = PRBADARG;
240 /* use ubik_Call to do the work, finding an up server and handling
241 the job of finding a sync site, if need be */
242 else code = ubik_Call(PR_INewEntry, pruclient,0, name, id, oid);
244 com_err (whoami, code, "on %s %s %d %d", op, name, id, oid);
246 else if (!strcmp(op, "sf")) {
247 afs_int32 mask, access, gq, uq;
248 if (GetInt32 (&id) ||
250 GetXInt32 (&access) ||
252 GetInt32 (&uq)) code = PRBADARG;
253 else code = ubik_Call(PR_SetFieldsEntry, pruclient,0,
254 id, mask, access, gq, uq, 0,0);
256 com_err (whoami, code, "on %s %d %x %x %d %d",
257 op, id, mask, access, gq, uq);
259 else if (!strcmp(op, "ce")) {
260 char newname[PR_MAXNAMELEN];
262 if (GetInt32 (&id) ||
263 GetString (newname, sizeof(newname)) ||
265 GetInt32 (&newid)) code = PRBADARG;
266 else code = ubik_Call(PR_ChangeEntry, pruclient,0,
267 id, newname, oid, newid);
269 com_err (whoami, code, "on %s %d %s %d %d",
270 op, id, newname, oid, newid);
272 else if (!strcmp(op,"wh")) {
273 /* scanf("%d",&id); */
274 if (GetInt32 (&id)) code = PRBADARG;
275 else code = ubik_Call(PR_WhereIsIt, pruclient, 0, id, &pos);
276 if (CodeOk(code)) printf("%s\n",pr_ErrorMsg(code));
277 else printf("location %d\n",pos);
279 else if (!strcmp(op,"du")) {
280 bzero(&entry,sizeof(entry));
281 /* scanf("%d",&pos); */
282 if (GetInt32 (&pos)) code = PRBADARG;
283 else code = ubik_Call(PR_DumpEntry, pruclient, 0, pos, &entry);
284 if (CodeOk(code)) printf("%s\n",pr_ErrorMsg(code));
285 if (code == PRSUCCESS) {
286 PrintEntry (pos, &entry, /*indent*/0);
288 printf("The contents of the entry for %d are:\n",entry.id);
289 printf("flags %d next %d\n",entry.flags,entry.next);
290 printf("Groups (or members) \n");
291 for (i=0;i<PRSIZE;i++)
292 printf("%d\n",entry.entries[i]);
293 printf("nextID %d nextname %d name %s\n",entry.nextID,entry.nextName,entry.name);
294 printf("owner %d creator %d\n",entry.owner, entry.creator);
298 else if (!strcmp(op,"add") || !strcmp(op,"au")) {
299 /* scanf("%d %d",&id,&gid); */
300 if (GetInt32 (&id) ||
301 GetInt32 (&gid)) code = PRBADARG;
302 else code = ubik_Call(PR_AddToGroup,pruclient,0,id,gid);
304 com_err (whoami, code, "on %s %d %d", op, id, gid);
306 else if (!strcmp(op,"iton")) {
307 lid.idlist_val = (afs_int32 *)malloc(20*sizeof(afs_int32));
308 ptr = lid.idlist_val;
312 while ((lid.idlist_len < 20) && (sscanf(foo,"%d",ptr) != EOF)) {
318 fprintf (stderr, "too many values specified; max is %d\n", 20);
320 lnames.namelist_val = 0;
321 lnames.namelist_len = 0;
322 code = ubik_Call(PR_IDToName,pruclient,0,&lid,&lnames);
323 if (CodeOk(code)) printf("%s\n",pr_ErrorMsg(code));
324 if (code == PRSUCCESS) {
325 for (i=0;i<lnames.namelist_len;i++) {
326 printf("id %d name %s\n",lid.idlist_val[i],lnames.namelist_val[i]);
328 free(lnames.namelist_val);
330 free(lid.idlist_val);
334 else if (!strcmp(op,"ntoi")) {
335 lnames.namelist_val = (prname *)malloc(PR_MAXLIST*PR_MAXNAMELEN);
336 lnames.namelist_len = 0;
339 for (i=0; ((lnames.namelist_len < PR_MAXLIST) &&
340 (sscanf(foo,"%s",lnames.namelist_val[i]) != EOF));
342 lnames.namelist_len++;
346 fprintf (stderr, "too many values specified; max is %d\n", PR_MAXLIST);
350 code = ubik_Call(PR_NameToID,pruclient,0,&lnames,&lid);
351 if (CodeOk(code)) printf("%s\n",pr_ErrorMsg(code));
352 if (code == PRSUCCESS) {
353 for (i=0;i<lid.idlist_len;i++)
354 printf("name %s id %d\n",lnames.namelist_val[i],lid.idlist_val[i]);
355 free(lid.idlist_val);
357 free(lnames.namelist_val);
358 lnames.namelist_val = 0;
359 lnames.namelist_len = 0;
361 else if (!strcmp(op,"del")) {
362 /* scanf("%d",&id); */
363 if (GetInt32 (&id)) code = PRBADARG;
364 else code = ubik_Call(PR_Delete, pruclient,0,id);
365 if (CodeOk(code)) printf("%s\n",pr_ErrorMsg(code));
367 else if (!strcmp(op,"dg")) {
368 /* scanf("%d",&id); */
369 if (GetInt32 (&id)) code = PRBADARG;
370 else code = ubik_Call(PR_Delete,pruclient,0,id);
371 if (CodeOk(code)) printf("%s\n",pr_ErrorMsg(code));
373 else if (!strcmp(op,"rm")) {
374 /* scanf("%d %d",&id,&gid); */
375 if (GetInt32 (&id) ||
376 GetInt32 (&gid)) code = PRBADARG;
377 else code = ubik_Call(PR_RemoveFromGroup,pruclient,0,id,gid);
378 if (CodeOk(code)) printf("%s\n",pr_ErrorMsg(code));
380 else if (!strcmp(op,"l")) {
381 alist.prlist_len = 0;
382 alist.prlist_val = 0;
383 /* scanf("%d",&id); */
384 if (GetInt32 (&id)) code = PRBADARG;
385 else code = ubik_Call(PR_GetCPS,pruclient,0,id, &alist, &over);
386 if (CodeOk(code)) printf("%s\n",pr_ErrorMsg(code));
387 if (code == PRSUCCESS) {
388 ptr = alist.prlist_val;
390 printf("Number of groups greater than PR_MAXGROUPS!\n");
391 printf("Excess of %d.\n",over);
393 for (i=0;i<alist.prlist_len;i++,ptr++)
395 free(alist.prlist_val);
396 alist.prlist_len = 0;
397 alist.prlist_val = 0;
400 else if (!strcmp(op,"nu")) {
401 /* scanf("%s",name); */
402 if (GetString (name, sizeof(name))) code = PRBADARG;
403 else code = pr_CreateUser(name,&id);
404 if (CodeOk(code)) printf("%s\n",pr_ErrorMsg(code));
405 if (code == PRSUCCESS)
406 printf("Id is %d.\n",id);
408 else if (!strcmp(op,"ng")) {
409 /* scanf("%s",name); */
410 if (GetString (name, sizeof(name))) code = PRBADARG;
411 else code = ubik_Call(PR_NewEntry,pruclient,0,name,1,&id);
412 if (CodeOk(code)) printf("%s\n",pr_ErrorMsg(code));
413 if (code == PRSUCCESS)
414 printf("Id is %d.\n",id);
416 else if (!strcmp(op,"lm")) {
417 code = ubik_Call(PR_ListMax,pruclient,0,&id,&gid);
418 if (CodeOk(code)) printf("%s\n",pr_ErrorMsg(code));
419 if (code == PRSUCCESS)
420 printf("Max user id is %d, max (really min) group is %d.\n",
423 else if (!strcmp(op,"smu")) {
424 /* scanf("%d",&id); */
425 if (GetInt32 (&id)) code = PRBADARG;
426 else code = ubik_Call(PR_SetMax, pruclient, 0,id,0);
427 if (CodeOk(code)) printf("%s\n",pr_ErrorMsg(code));
429 else if (!strcmp(op,"smg")) {
430 /* scanf("%d",&id); */
431 if (GetInt32 (&id)) code = PRBADARG;
432 else code = ubik_Call(PR_SetMax, pruclient, 0,id,1);
433 if (CodeOk(code)) printf("%s\n",pr_ErrorMsg(code));
435 else if (!strcmp(op,"sin")) {
436 /* scanf("%d",&id); */
437 if (GetInt32 (&id)) code = PRBADARG;
438 else code = pr_SIdToName(id,name);
439 if (CodeOk(code)) printf("%s\n",pr_ErrorMsg(code));
440 if (code == PRSUCCESS)
441 printf("id %d name %s\n",id,name);
443 else if (!strcmp(op,"sni")) {
444 /* scanf("%s",name); */
445 if (GetString (name, sizeof(name))) code = PRBADARG;
446 else code = pr_SNameToId(name,&id);
447 if (CodeOk(code)) printf("%s\n",pr_ErrorMsg(code));
448 if (code == PRSUCCESS)
449 printf("name %s id %d\n",name,id);
451 else if (!strcmp(op,"fih")) {
453 struct PrUpdateEntry uentry;
454 bzero(&uentry, sizeof(uentry));
455 /* scanf("%s",name); */
456 if (GetString (name, sizeof(name))) {
460 code = pr_SNameToId(name,&id);
462 printf("%s\n",pr_ErrorMsg(code));
465 code = pr_SIdToName(id, tname);
466 if (code == PRSUCCESS) {
467 printf("Warning: Id hash for %s (id %d) seems correct at the db; rehashing it anyway\n", name, id);
470 uentry.Mask = PRUPDATE_IDHASH;
471 code = ubik_Call(PR_UpdateEntry, pruclient, 0,0,name,&uentry);
473 printf("Failed to update entry %s (err=%d)\n", name, code);
477 else if (!strcmp(op,"fnh")) {
479 struct PrUpdateEntry uentry;
480 bzero(&uentry, sizeof(uentry));
481 /* scanf("%d", &id); */
482 if (GetInt32 (&id)) {
486 code = pr_SIdToName(id, name);
488 printf("%s\n",pr_ErrorMsg(code));
491 code = pr_SNameToId(name, &tid);
492 if (code == PRSUCCESS) {
493 printf("Name hash for %d (name is %s) seems correct at the db; rehashing it anyway\n", id, name);
496 uentry.Mask = PRUPDATE_NAMEHASH;
497 code = ubik_Call(PR_UpdateEntry, pruclient, 0,id,"_foo_",&uentry);
499 printf("Failed to update entry with id %d (err=%d)\n", id, code);
503 else if (!strcmp(op,"?"))
505 else if (!strcmp(op,"q")) exit(0);
506 else printf("Unknown op: '%s'! ? for help\n", op);
513 printf("cr name id owner - create entry with name and id.\n");
514 printf("wh id - what is the offset into database for id?\n");
515 printf("du offset - dump the contents of the entry at offset.\n");
516 printf("add uid gid - add user uid to group gid.\n");
517 printf("iton id* - translate the list of id's to names.\n");
518 printf("ntoi name* - translate the list of names to ids.\n");
519 printf("del id - delete the entry for id.\n");
520 printf("dg gid - delete the entry for group gid.\n");
521 printf("rm id gid - remove user id from group gid.\n");
522 printf("l id - get the CPS for id.\n");
523 printf("nu name - create new user with name - returns an id.\n");
524 printf("ng name - create new group with name - returns an id.\n");
525 printf("lm - list max user id and max (really min) group id.\n");
526 printf("smu - set max user id.\n");
527 printf("smg - set max group id.\n");
528 printf("sin id - single iton.\n");
529 printf("sni name - single ntoi.\n");
530 printf("fih name - fix id hash for <name>.\n");
531 printf("fnh id - fix name hash for <id>.\n");
532 printf("q - quit.\n?- this message.\n");
539 while (**s != ' ' && **s != '\0') (*s)++;
540 while (**s == ' ') (*s)++;
542 #endif /* !AFS_PTHREAD_ENV */