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 <afs/param.h>
14 #include <sys/types.h>
17 #include <WINNT/afsevent.h>
19 #include <netinet/in.h>
26 #include <afs/com_err.h>
27 #include <afs/cellconfig.h>
30 #include <afs/afsutil.h>
33 afs_int32 security = 0;
34 char confdir[AFSDIR_PATH_MAX];
38 extern struct ubik_client *pruclient;
40 static int ignoreExist = 0;
41 static char line[256];
42 static char *lineProgress;
44 #define WHITESPACE " \t\n"
46 #ifndef AFS_PTHREAD_ENV
49 /* OK, this REALLY sucks bigtime, but I can't tell who is calling
50 * afsconf_CheckAuth easily, and only *SERVERS* should be calling osi_audit
51 * anyway. It's gonna give somebody fits to debug, I know, I know.
55 #endif /* !AFS_PTHREAD_ENV */
57 int GetToken (format, l)
64 if (lineProgress == 0) lineProgress = line;
65 c = sscanf (lineProgress, format, l);
66 if (c != 1) return -1;
67 /* skip the white space */
68 lineProgress += strspn (lineProgress, WHITESPACE);
69 /* skip to end of token */
70 lineProgress = strpbrk (lineProgress, WHITESPACE);
74 #define GetInt32(l) GetToken ("%d", l)
75 #define GetXInt32(l) GetToken ("%x", l)
77 int GetString (s, slen)
85 if (lineProgress == 0) lineProgress = line;
86 /* skip the white space */
87 lineProgress += strspn (lineProgress, WHITESPACE);
89 /* check for quoted string and find end */
92 l = strcspn (++beg, "\"");
93 if (l == strlen(beg)) return -1; /* unbalanced quotes */
94 lineProgress = beg+l+1;
96 l = strcspn (beg, WHITESPACE);
99 if (l >= slen) { /* don't return too much */
105 s[l] = 0; /* null termination */
112 if (!ignoreExist) return code;
113 return code && (code != PREXIST) && (code != PRIDEXIST);
116 int PrintEntry (ea, e, indent)
121 /* handle screwed up versions of DumpEntry */
122 if (e->flags & PRCONT) {
123 afs_int32 id = *(afs_int32 *)(e->name);
124 if ((id != PRBADID) && ((id > (1<<24)) || (id < -(1<<24)))) {
125 /* assume server incorrectly swapped these bytes... */
127 while (i<sizeof(e->name)) {
130 e->name[i] = e->name[i+3];
133 e->name[i+1] = e->name[i+2];
139 return pr_PrintEntry (stdout, /*host order*/1, ea, e, indent);
142 #ifndef AFS_PTHREAD_ENV
146 #include "AFS_component_version_number.c"
152 register afs_int32 code;
154 char name[PR_MAXNAMELEN];
155 afs_int32 id, oid, gid;
158 struct prentry entry;
169 * The following signal action for AIX is necessary so that in case of a
170 * crash (i.e. core is generated) we can include the user's data section
171 * in the core dump. Unfortunately, by default, only a partial core is
172 * generated which, in many cases, isn't too useful.
174 struct sigaction nsa;
176 sigemptyset(&nsa.sa_mask);
177 nsa.sa_handler = SIG_DFL;
178 nsa.sa_flags = SA_FULLDUMP;
179 sigaction(SIGSEGV, &nsa, NULL);
183 initialize_pt_error_table();
185 strcpy(confdir, AFSDIR_CLIENT_ETC_DIRPATH);
189 int arglen = strlen(argv[i]);
191 lcstring (arg, argv[i], sizeof(arg));
192 #define IsArg(a) (strncmp (arg,a, arglen) == 0)
193 if (IsArg("-testconfdir"))
194 strncpy (confdir, argv[++i], sizeof(confdir));
195 else if (IsArg("client"))
196 strncpy(confdir, AFSDIR_CLIENT_ETC_DIRPATH, sizeof(confdir));
197 else if (IsArg("server"))
198 strncpy(confdir, AFSDIR_SERVER_ETC_DIRPATH, sizeof(confdir));
199 else if (IsArg("0") || IsArg("1") || IsArg("2"))
200 security = atoi(argv[i]);
201 else if (IsArg("-ignoreexist"))
203 else if (IsArg("-cell"))
206 printf ("Usage is: 'prclient [-testconfdir <dir> | server | client] [0 | 1 | 2] [-ignoreExist] [-cell <cellname>]\n");
212 printf ("Using CellServDB file in %s\n", confdir);
214 printf ("Making unauthenticated connection to prserver\n");
216 code = pr_Initialize (security, confdir, cell);
218 com_err (whoami, code, "Couldn't initialize protection library");
226 s = fgets(line, sizeof(line), stdin);
227 if (s == NULL) break;
230 code = GetString (op, sizeof(op));
232 com_err (whoami, PRBADARG,
233 "error reading opcode in line '%s', got '%.*s'",
234 line, sizeof(op), op);
237 if (strlen(op) == 0) continue; /* no input */
239 if (!strcmp(op, "cr")) {
240 if (GetString (name, sizeof(name)) ||
242 GetInt32 (&oid)) code = PRBADARG;
243 /* use ubik_Call to do the work, finding an up server and handling
244 the job of finding a sync site, if need be */
245 else code = ubik_Call(PR_INewEntry, pruclient,0, name, id, oid);
247 com_err (whoami, code, "on %s %s %d %d", op, name, id, oid);
249 else if (!strcmp(op, "sf")) {
250 afs_int32 mask, access, gq, uq;
251 if (GetInt32 (&id) ||
253 GetXInt32 (&access) ||
255 GetInt32 (&uq)) code = PRBADARG;
256 else code = ubik_Call(PR_SetFieldsEntry, pruclient,0,
257 id, mask, access, gq, uq, 0,0);
259 com_err (whoami, code, "on %s %d %x %x %d %d",
260 op, id, mask, access, gq, uq);
262 else if (!strcmp(op, "ce")) {
263 char newname[PR_MAXNAMELEN];
265 if (GetInt32 (&id) ||
266 GetString (newname, sizeof(newname)) ||
268 GetInt32 (&newid)) code = PRBADARG;
269 else code = ubik_Call(PR_ChangeEntry, pruclient,0,
270 id, newname, oid, newid);
272 com_err (whoami, code, "on %s %d %s %d %d",
273 op, id, newname, oid, newid);
275 else if (!strcmp(op,"wh")) {
276 /* scanf("%d",&id); */
277 if (GetInt32 (&id)) code = PRBADARG;
278 else code = ubik_Call(PR_WhereIsIt, pruclient, 0, id, &pos);
279 if (CodeOk(code)) printf("%s\n",pr_ErrorMsg(code));
280 else printf("location %d\n",pos);
282 else if (!strcmp(op,"du")) {
283 bzero(&entry,sizeof(entry));
284 /* scanf("%d",&pos); */
285 if (GetInt32 (&pos)) code = PRBADARG;
286 else code = ubik_Call(PR_DumpEntry, pruclient, 0, pos, &entry);
287 if (CodeOk(code)) printf("%s\n",pr_ErrorMsg(code));
288 if (code == PRSUCCESS) {
289 PrintEntry (pos, &entry, /*indent*/0);
291 printf("The contents of the entry for %d are:\n",entry.id);
292 printf("flags %d next %d\n",entry.flags,entry.next);
293 printf("Groups (or members) \n");
294 for (i=0;i<PRSIZE;i++)
295 printf("%d\n",entry.entries[i]);
296 printf("nextID %d nextname %d name %s\n",entry.nextID,entry.nextName,entry.name);
297 printf("owner %d creator %d\n",entry.owner, entry.creator);
301 else if (!strcmp(op,"add") || !strcmp(op,"au")) {
302 /* scanf("%d %d",&id,&gid); */
303 if (GetInt32 (&id) ||
304 GetInt32 (&gid)) code = PRBADARG;
305 else code = ubik_Call(PR_AddToGroup,pruclient,0,id,gid);
307 com_err (whoami, code, "on %s %d %d", op, id, gid);
309 else if (!strcmp(op,"iton")) {
310 lid.idlist_val = (afs_int32 *)malloc(20*sizeof(afs_int32));
311 ptr = lid.idlist_val;
315 while ((lid.idlist_len < 20) && (sscanf(foo,"%d",ptr) != EOF)) {
321 fprintf (stderr, "too many values specified; max is %d\n", 20);
323 lnames.namelist_val = 0;
324 lnames.namelist_len = 0;
325 code = ubik_Call(PR_IDToName,pruclient,0,&lid,&lnames);
326 if (CodeOk(code)) printf("%s\n",pr_ErrorMsg(code));
327 if (code == PRSUCCESS) {
328 for (i=0;i<lnames.namelist_len;i++) {
329 printf("id %d name %s\n",lid.idlist_val[i],lnames.namelist_val[i]);
331 free(lnames.namelist_val);
333 free(lid.idlist_val);
337 else if (!strcmp(op,"ntoi")) {
338 lnames.namelist_val = (prname *)malloc(PR_MAXLIST*PR_MAXNAMELEN);
339 lnames.namelist_len = 0;
342 for (i=0; ((lnames.namelist_len < PR_MAXLIST) &&
343 (sscanf(foo,"%s",lnames.namelist_val[i]) != EOF));
345 lnames.namelist_len++;
349 fprintf (stderr, "too many values specified; max is %d\n", PR_MAXLIST);
353 code = ubik_Call(PR_NameToID,pruclient,0,&lnames,&lid);
354 if (CodeOk(code)) printf("%s\n",pr_ErrorMsg(code));
355 if (code == PRSUCCESS) {
356 for (i=0;i<lid.idlist_len;i++)
357 printf("name %s id %d\n",lnames.namelist_val[i],lid.idlist_val[i]);
358 free(lid.idlist_val);
360 free(lnames.namelist_val);
361 lnames.namelist_val = 0;
362 lnames.namelist_len = 0;
364 else if (!strcmp(op,"del")) {
365 /* scanf("%d",&id); */
366 if (GetInt32 (&id)) code = PRBADARG;
367 else code = ubik_Call(PR_Delete, pruclient,0,id);
368 if (CodeOk(code)) printf("%s\n",pr_ErrorMsg(code));
370 else if (!strcmp(op,"dg")) {
371 /* scanf("%d",&id); */
372 if (GetInt32 (&id)) code = PRBADARG;
373 else code = ubik_Call(PR_Delete,pruclient,0,id);
374 if (CodeOk(code)) printf("%s\n",pr_ErrorMsg(code));
376 else if (!strcmp(op,"rm")) {
377 /* scanf("%d %d",&id,&gid); */
378 if (GetInt32 (&id) ||
379 GetInt32 (&gid)) code = PRBADARG;
380 else code = ubik_Call(PR_RemoveFromGroup,pruclient,0,id,gid);
381 if (CodeOk(code)) printf("%s\n",pr_ErrorMsg(code));
383 else if (!strcmp(op,"l")) {
384 alist.prlist_len = 0;
385 alist.prlist_val = 0;
386 /* scanf("%d",&id); */
387 if (GetInt32 (&id)) code = PRBADARG;
388 else code = ubik_Call(PR_GetCPS,pruclient,0,id, &alist, &over);
389 if (CodeOk(code)) printf("%s\n",pr_ErrorMsg(code));
390 if (code == PRSUCCESS) {
391 ptr = alist.prlist_val;
393 printf("Number of groups greater than PR_MAXGROUPS!\n");
394 printf("Excess of %d.\n",over);
396 for (i=0;i<alist.prlist_len;i++,ptr++)
398 free(alist.prlist_val);
399 alist.prlist_len = 0;
400 alist.prlist_val = 0;
403 else if (!strcmp(op,"nu")) {
404 /* scanf("%s",name); */
405 if (GetString (name, sizeof(name))) code = PRBADARG;
406 else code = pr_CreateUser(name,&id);
407 if (CodeOk(code)) printf("%s\n",pr_ErrorMsg(code));
408 if (code == PRSUCCESS)
409 printf("Id is %d.\n",id);
411 else if (!strcmp(op,"ng")) {
412 /* scanf("%s",name); */
413 if (GetString (name, sizeof(name))) code = PRBADARG;
414 else code = ubik_Call(PR_NewEntry,pruclient,0,name,1,&id);
415 if (CodeOk(code)) printf("%s\n",pr_ErrorMsg(code));
416 if (code == PRSUCCESS)
417 printf("Id is %d.\n",id);
419 else if (!strcmp(op,"lm")) {
420 code = ubik_Call(PR_ListMax,pruclient,0,&id,&gid);
421 if (CodeOk(code)) printf("%s\n",pr_ErrorMsg(code));
422 if (code == PRSUCCESS)
423 printf("Max user id is %d, max (really min) group is %d.\n",
426 else if (!strcmp(op,"smu")) {
427 /* scanf("%d",&id); */
428 if (GetInt32 (&id)) code = PRBADARG;
429 else code = ubik_Call(PR_SetMax, pruclient, 0,id,0);
430 if (CodeOk(code)) printf("%s\n",pr_ErrorMsg(code));
432 else if (!strcmp(op,"smg")) {
433 /* scanf("%d",&id); */
434 if (GetInt32 (&id)) code = PRBADARG;
435 else code = ubik_Call(PR_SetMax, pruclient, 0,id,1);
436 if (CodeOk(code)) printf("%s\n",pr_ErrorMsg(code));
438 else if (!strcmp(op,"sin")) {
439 /* scanf("%d",&id); */
440 if (GetInt32 (&id)) code = PRBADARG;
441 else code = pr_SIdToName(id,name);
442 if (CodeOk(code)) printf("%s\n",pr_ErrorMsg(code));
443 if (code == PRSUCCESS)
444 printf("id %d name %s\n",id,name);
446 else if (!strcmp(op,"sni")) {
447 /* scanf("%s",name); */
448 if (GetString (name, sizeof(name))) code = PRBADARG;
449 else code = pr_SNameToId(name,&id);
450 if (CodeOk(code)) printf("%s\n",pr_ErrorMsg(code));
451 if (code == PRSUCCESS)
452 printf("name %s id %d\n",name,id);
454 else if (!strcmp(op,"fih")) {
456 struct PrUpdateEntry uentry;
457 bzero(&uentry, sizeof(uentry));
458 /* scanf("%s",name); */
459 if (GetString (name, sizeof(name))) {
463 code = pr_SNameToId(name,&id);
465 printf("%s\n",pr_ErrorMsg(code));
468 code = pr_SIdToName(id, tname);
469 if (code == PRSUCCESS) {
470 printf("Warning: Id hash for %s (id %d) seems correct at the db; rehashing it anyway\n", name, id);
473 uentry.Mask = PRUPDATE_IDHASH;
474 code = ubik_Call(PR_UpdateEntry, pruclient, 0,0,name,&uentry);
476 printf("Failed to update entry %s (err=%d)\n", name, code);
480 else if (!strcmp(op,"fnh")) {
482 struct PrUpdateEntry uentry;
483 bzero(&uentry, sizeof(uentry));
484 /* scanf("%d", &id); */
485 if (GetInt32 (&id)) {
489 code = pr_SIdToName(id, name);
491 printf("%s\n",pr_ErrorMsg(code));
494 code = pr_SNameToId(name, &tid);
495 if (code == PRSUCCESS) {
496 printf("Name hash for %d (name is %s) seems correct at the db; rehashing it anyway\n", id, name);
499 uentry.Mask = PRUPDATE_NAMEHASH;
500 code = ubik_Call(PR_UpdateEntry, pruclient, 0,id,"_foo_",&uentry);
502 printf("Failed to update entry with id %d (err=%d)\n", id, code);
506 else if (!strcmp(op,"?"))
508 else if (!strcmp(op,"q")) exit(0);
509 else printf("Unknown op: '%s'! ? for help\n", op);
516 printf("cr name id owner - create entry with name and id.\n");
517 printf("wh id - what is the offset into database for id?\n");
518 printf("du offset - dump the contents of the entry at offset.\n");
519 printf("add uid gid - add user uid to group gid.\n");
520 printf("iton id* - translate the list of id's to names.\n");
521 printf("ntoi name* - translate the list of names to ids.\n");
522 printf("del id - delete the entry for id.\n");
523 printf("dg gid - delete the entry for group gid.\n");
524 printf("rm id gid - remove user id from group gid.\n");
525 printf("l id - get the CPS for id.\n");
526 printf("nu name - create new user with name - returns an id.\n");
527 printf("ng name - create new group with name - returns an id.\n");
528 printf("lm - list max user id and max (really min) group id.\n");
529 printf("smu - set max user id.\n");
530 printf("smg - set max group id.\n");
531 printf("sin id - single iton.\n");
532 printf("sni name - single ntoi.\n");
533 printf("fih name - fix id hash for <name>.\n");
534 printf("fnh id - fix name hash for <id>.\n");
535 printf("q - quit.\n?- this message.\n");
542 while (**s != ' ' && **s != '\0') (*s)++;
543 while (**s == ' ') (*s)++;
545 #endif /* !AFS_PTHREAD_ENV */