File: approve.c

package info (click to toggle)
super 3.11.6-1
  • links: PTS
  • area: main
  • in suites: hamm
  • size: 660 kB
  • ctags: 592
  • sloc: ansic: 7,338; sh: 183; makefile: 175
file content (894 lines) | stat: -rw-r--r-- 28,268 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
static char rcsid[] = "$Id: approve.c,v 1.103 1998/04/07 07:18:31 will Exp $";

/* The code should compile with either ANSI C or K&R compilers. */

/*
 *      Copyright (c) 1993 by California Institute of Technology.
 *      Written by William Deich.  Not derived from licensed software.

 *    You may distribute under the terms of either the GNU General Public
 *    License or the Artistic License, as specified in the README file.
 */

#include "super.h"
#include "version.h"

/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
/* Look up command "usrcmd" in file "superfile".  Return path to execute
 * if approved, empty string if no action should be taken, NULL ptr
 * if error.  As a ``side effect'': Sets the fields in the superinfo struct.
 * approve() returns:
 *	- NULL ptr if error:
 *		a) username not found;
 *		b) superfile can't be opened for reading;
 *		c) no such command as usrcmd in superfile;
 *		d) user not allowed to execute this command;
 *		e) invalid superfile contents.
 *	- ptr to empty string if all ok, but no program should be executed.
 *	- ptr to path of file to exec, if user allowed to do so.
 *	Any error also generates a message to stderr.

 * New calls to approve() overwrite the buffer containing the returned path.
 */
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
char *
approve(usrcmd, givehelp, verbosity)
char *usrcmd;		/* command we're to check on. Should NOT be a
			 * command-line option!  Can be a NULL pointer
			 * or empty string, in which case we give help,
			 * print version info, or whatever.
			 */
int givehelp;		/* Don't execute command; give help instead. */
int verbosity;		/* HELP_BASIC, HELP_FULL, or HELP_FACTS */
{
    char *buf, *bbuf;
    char *do_variables();
    int indentok;

    /* Do we (1) give help, or (2) match a command with this user? */
    if (givehelp && !check_syntax)
	printhelp_hello(verbosity, usrcmd);
    if (givehelp && verbosity == HELP_USAGE)
	return "";

    currfile = file_open(currfile, superfile, 0);
    if (!currfile)
	return NULL;

    for (localinfo.progs.commandfound=NULL, currfile->line=1;
	(buf=fillbuffer(currfile->fp, &indentok, &currfile->nl)) ||
	    ( (check_syntax || indentok) && currfile->prev != NULL);
					     currfile->line += currfile->nl) {
	if (!buf) {
	    /* Reached EOF on this file -- close it, continue with prev file */
	    currfile = file_close(currfile);
	    continue;
	}
	error_srcfile = currfile->fullname;
	error_line = currfile->line;
	error_nl = currfile->nl;

	if (!indentok) {
	     Error(0, 0, "%t\n\tformat error in super.tab file:\n\
\tContinued line not indented.\n");
	    if (check_syntax)
		continue;
	    else
		return NULL;
	}


	/* Discard empty lines and pure-comment lines */
	bbuf = buf + strspn(buf, SEP);	/* skip leading whitespace */
	if (!*bbuf || *bbuf == '#')
	    continue;

	if (bbuf != buf) {
	    Error(0, 0, "%t\n\tformat error in super(1) file: \n\
\tNon-comments must begin in column 1.\n");
	    if (check_syntax)
		continue;
	    else
		return NULL;
	}

	/* Clean out embedded comments, whitespace-newline-whitespace. */
	buf = clean_buf(buf, NULL);
	if (!buf)
	    return NULL;
	if (debug) {
	    fprintf(stderr,
	    "==============================================================\n");
	    fprintf(stderr, "Input text: %s", buf);
	}
    
	/* Do variable expansion */
	buf = do_variables(buf);
	if (!buf) {
	    if (check_syntax)
		continue;
	    else
		return NULL;
	}

	/* Initialize localinfo */
	if (handle_option(NULL, NULL, 0) == -1)
	    if (check_syntax)
		continue;
	    else
		return NULL;

	if (check_syntax) {
	    parseline(0, 1, buf, usrcmd);

	} else if (givehelp) {
	    switch (parseline(1, 0, buf, usrcmd)) {
	    case -1:
		return NULL;
		break;
	    
	    case 0:
		break;
	    
	    default:
		printhelp(verbosity);
	    }

	} else {
	    switch(parseline(0, 0, buf, usrcmd)) {
	    case -1:
		return NULL;
		break;

	    case 0:
		break;

	    default:
		/* Have an acceptable line, unless die=xxx was used */
		return localinfo.progs.cmd_file[localinfo.progs.match].File;
	    }
	}
    }
    file_close(currfile);

    if (!check_syntax && !indentok) {
	return NULL;
    } else if (givehelp || check_syntax) {
	return "";
    } else if (!localinfo.progs.evermatched) {
	Error(0, 0, "No such super command as `%-.500s'.\n", usrcmd);
	return NULL;
    } else {
	Error(0, 0, "%-.500s - Permission denied to user %s\n",
					    usrcmd, userinfo.caller.pw_name);
	return NULL;
    }
    /* NOTREACHED */
}

/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
/* Parse one control line (may contain several text lines).
 * Side effect: prints help information if givehelp is set.
 */
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
/*	Return -1 if caller should give up parsing the file;
 *	return 0 if caller should loop to next chunk (because we
 *		are doing help processing, because of nomatch, because
 *		of ":"-command, etc.);
 *	return 1 if success.
 *	The cmd/file list and which one matched (if any) are returned
 *	through the localinfo.progs struct.
 */
int
parseline(givehelp, checksyntax, buf, usrcmd)
int givehelp;		/* request is to give help, not check valid command.  */
int checksyntax;	/* request is to check syntax of super.tab file,
			 * not check valid command.
			 */
char *buf;		/* Input chunk.  Gets modified by strqtokS(). */
char *usrcmd;		/* Command user wants to execute -- can be
			 * NULL if givehelp_or_checksyntax!=0.
			 */
{
    char *word1;
    char *cond_or_opt_wd;
    char *bbuf;
    int i, continue_after_if;
    int condition_match;
    /* FullPath is twice MAXPATHLEN, because this allows room for
     * MAXPATHLEN characters of arguments as well as MAXPATHLEN
     * characters for the filename itself.
     */
    static char FullPath[2*MAXPATHLEN];

    localinfo.progs.commandfound = NULL;

    bbuf = buf;
    buf += strspn(buf, SEP);	/* skip leading whitespace */
    if (!*buf || *buf == '#')
	return 0;		/* Discard empty lines and pure-comment lines */
    
    if (bbuf != buf) {
	return Error(0, 0, "%t\n\tformat error in super(1) file: \n\
\tCommandPatterns must begin in column 1.\n");
    }

    /* Get the list of command/file pairs */
    strqS_qm = my_qm;
    strqS_cc = my_cc;

    if (localinfo.progs.nalloc == 0) {
	int nalloc = 10;
	if ( !(localinfo.progs.cmd_file = (ProgList *)
				malloc(nalloc * sizeof(ProgList))) )
	    return Error(1, 0, "%tFailed to malloc space for Cmd+File list\n");
	localinfo.progs.nalloc = 10;
    }
    localinfo.progs.n = 0;	/* Initialize to 0 cmd/file pairs */

    /* Start by inspecting the first word.  If it begins ":", it is
     * a special builtin command.  Otherwise, if it contains "::", it
     * must be a command/file pair.  Otherwise it's a single command.
     */
    word1 = strqtokS(buf, SEP, NULL, NULL, 1);
    if (!word1)
	return Error(0, 0, "%t\n\tformat error in super.tab file: \
CmdPattern or FullPathName missing.\n");

    do {
	continue_after_if = 0;
	if (*word1 == ':' || strcmp(word1, "/") == 0) {
	    /* It's a special builtin command beginning ":" (or the obsolete
	     * special command name "/".)
	     */
	    int i;

	    i = process_colon_cmds(word1, &continue_after_if);
	    if (!continue_after_if) {
		return (i < 0) ? -1 : 0;
	    } else {
		/* A :if  evaluated to true; replace "word1" with the next
		 * word from this line, and process the rest of the line.
		 */
		word1 = strqtokS(NULL, SEP, NULL, NULL, 1);
		if (!word1)
		    return 0;	/* no body for conditional */
	    }
	}
    } while (continue_after_if);

    /* Build lists of cmd::file entries */
    if ((i=build_cmd_file(usrcmd, givehelp || checksyntax,
					word1, &cond_or_opt_wd)) <= 0)
	return i;


    if (!(givehelp || checksyntax)) {
	int indx = (localinfo.progs.match >= 0) ? localinfo.progs.match : 0;

	/* Check if we are supposed to generate a modified FullPath */
	char *path = localinfo.progs.cmd_file[indx].File;

	/* Adjust fullpath as necessary */
	if (fixup_fullpath(indx, usrcmd, path,
					FullPath, sizeof(FullPath)) == -1)
	    return -1;

    } else {
	/* givehelp or check syntax */
	int indx = (localinfo.progs.match >= 0) ? localinfo.progs.match : 0;
	localinfo.progs.commandfound = localinfo.progs.cmd_file[indx].Cmd;
    }

    if (!debug && it_came_from_cmdline) {
	/* Not debug, but -F / -T / -U / -G / -M flag was given -- so, let's
	 * print a separator line to help out the reader.
	 */
	fprintf(stderr,
	"==============================================================\n");
    }
    if (!givehelp && (debug || it_came_from_cmdline)) {
	fprintf(stderr, "\tCommandfound = `%s' path = `%s'\n",
	    localinfo.progs.commandfound, localinfo.progs.match >= 0 ?
		localinfo.progs.cmd_file[localinfo.progs.match].File : "-");
    }

    /* Now process command-line conditions and options */
    if ((condition_match=conditions_and_options(cond_or_opt_wd)) == -1)
	return condition_match;


    if (condition_match && localinfo.die) {
	if (checksyntax)
	    return 0;
	else if (givehelp)
	    return -1;
	else
	    return Error(0, 0, "User's command: %s. %s\n",
				    usrcmd, localinfo.die);
    }
    return condition_match;
}

/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
/* Builds list of cmd::file pairs for a control line.
 *	Return -1 if caller should give up parsing the file;
 *	return 0 if caller should loop to next chunk (because we
 *		are doing help processing, because of nomatch, etc.
 *	return 1 if success.
 */
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
int
build_cmd_file(usrcmd, givehelp_or_checksyntax, word1, cond_or_opt_wd)
char *usrcmd;			/* User's typed command */
int givehelp_or_checksyntax;	/* Just giving help or checking syntax
				 * of super.tab file?
				 */
char *word1;			/* First wd to process; get rest w/ strqtokS */
char **cond_or_opt_wd;
{
    /* Expect to see either
     *		cmd::file ...
     *	or
     *		cmd   file
     */

    char *s, *coloncolon, *pair;
    extern char *taglines();

    if ((s = strchr(word1, ':')) && *(s+1) == ':') {
	/* It's a Cmd::Filename pair.  Go through the list of
	 * command/filenames, until they are all used up, and record
	 * the matching filename.
	 */
	pair = word1;
	localinfo.progs.match = -1;	/* initialize to no match */
	do {
	    s = strchr(pair, ':');
	    coloncolon = (s && *(s+1) == ':') ? s : NULL;
	    if (!coloncolon) {
		/* Reached end of cmd::file list. */
		*cond_or_opt_wd = pair;
		break;
	    }
	    if (debug)
		fprintf(stderr, "\tCommand::file = `%s'\n", pair);

	    if (coloncolon == pair)
		return Error(0, 0, "%t\n\tFormat error in super.tab file:\n\
\tCmd::File used, but the Cmd part is missing.\n");
	    *coloncolon = '\0';
	    localinfo.progs.n++;	/* Have a new cmd/file pair */
	    if (localinfo.progs.n == localinfo.progs.nalloc) {
		localinfo.progs.nalloc *= 2;
		localinfo.progs.cmd_file =
			    (ProgList *) realloc(localinfo.progs.cmd_file,
				localinfo.progs.nalloc * sizeof(ProgList));
		if (!localinfo.progs.cmd_file)
		    return Error(1, 0,
			"%tFailed to malloc space for Cmd+File list\n");
	    }

	    localinfo.progs.cmd_file[localinfo.progs.n - 1].Cmd = pair;
	    localinfo.progs.cmd_file[localinfo.progs.n - 1].File =
							coloncolon + 2;

	    if ((givehelp_or_checksyntax && !usrcmd) ||
				    match_pattern(0, 2, usrcmd, pair) == 1) {
		/* Matched the command */
		localinfo.progs.match = localinfo.progs.n - 1;
		localinfo.progs.evermatched = 1;
		if (localinfo.progs.cmd_file[localinfo.progs.match].File[0]
				== '\0')
		    return Error(0, 0, "%t\n\tFormat error in super.tab file:\n\
\tCmd::File used, but the File part is missing.\n");

		if (globalinfo.relative_path == 0 &&
		    localinfo.progs.cmd_file[localinfo.progs.match].File[0]
				!= '/')
		    return Error(0, 0, "%t\n\tFormat error in super.tab file:\n\
\tfilename `%s' is not an absolute path.\n\
\tUse global option relative_path=y if you really want such foolishness!\n",
		    localinfo.progs.cmd_file[localinfo.progs.match].File);
	    }

	} while ( (pair = strqtokS(NULL, SEP, NULL, NULL, 1)) );
	if (!pair)
	    *cond_or_opt_wd = NULL;

	if (!(givehelp_or_checksyntax && !usrcmd) &&
				localinfo.progs.match == -1)
	    return 0;		/* no match */

    } else if (s && *s == ':') {
	/* Embedded `:' in command name! */

	return Error(0, 0, "%t\n\tFormat error in super.tab file: \
The CmdPattern (%s) may not contain an embedded colon\n", word1);

    } else {
	/* It was a plain command name.  Get the full path. */
	localinfo.progs.n = 1;
	localinfo.progs.match = -1;	/* initialize to no match */
	localinfo.progs.cmd_file[0].Cmd = word1;
	localinfo.progs.cmd_file[0].File = strqtokS(NULL, SEP, NULL, NULL, 1);
	if (localinfo.progs.cmd_file[0].File == NULL ||
				localinfo.progs.cmd_file[0].File[0] == '\0')
	    return Error(0, 0, "%t\n\tformat error in super.tab file: \
CmdPattern or FullPathName missing.\n");

	    if (globalinfo.relative_path == 0 &&
			localinfo.progs.cmd_file[0].File[0] != '/')
		return Error(0, 0, "%t\n\tFormat error in super.tab file: \
filename `%s' is not an absolute path.\n\
\tUse global option relative_path=y if you really want such foolishness!\n",
		localinfo.progs.cmd_file[0].File);

	if (debug)
	    fprintf(stderr, "\tCmdPattern = `%s' %s\n",
		localinfo.progs.cmd_file[0].Cmd, taglines(1));

	if ((givehelp_or_checksyntax && !usrcmd)  ||
	    match_pattern(0, 2, usrcmd, localinfo.progs.cmd_file[0].Cmd) == 1) {
	    localinfo.progs.match = 0;	/* success */
	    localinfo.progs.evermatched = 1;
	}

	if (!(givehelp_or_checksyntax && !usrcmd) &&
					localinfo.progs.match == -1)
	    return 0;		/* Skip non-matching commands */

	/* The next word has to be ready in advance of the next loop: */
	*cond_or_opt_wd = strqtokS(NULL, SEP, NULL, NULL, 1);
    }
    return 1;
}

/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
/* Take user's command, and the path from the control line, and generate
 * the full path that should be executed.
 * Returns -1 on error, indicating file should be parsed no further.
 */
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
int
fixup_fullpath(indx, usrcmd, path, FullPath, l_FullPath)
int indx;		/* Index into cmd_file array whence comes the path */
char *usrcmd;		/* User's entered command */
char *path;		/* Matched path from control line */
char *FullPath;		/* output: the generated full path */
int l_FullPath;		/* length of the FullPath buffer */
{
    register char *p, *q;
    int lpath, n;

    /* Watch out for sneaky users */
    if (strcspn(usrcmd, SEP) != strlen(usrcmd) || strchr(usrcmd, '\\'))
	return Error(0, 0,
	    "You entered `%s',\nbut you may NOT include whitespace or \
backslashes in the cmd!\n", usrcmd);

    /* Determine the length of the filename part of the path -- remember
     * that it may contain whitespace-separated arguments following the
     * filename.
     */
    lpath = strcspn(path, SEP);
    p = strchr(path, '*');	/* Check for asterisk in filename part */
    n = p ? p - path : 0;	/* number of characters before asterisk */
    if (p && n < lpath) {
	/* Wildcard command -- replace asterisk with the usrcmd */

	if ((strlen(path) + strlen(usrcmd)) > l_FullPath)
	    return Error(0, 0,
	    "%t\n\tRidiculously long path would be formed from Cmd \
and FullPathName:\n<%s> + <%s>", path, usrcmd);
	if ((q=strchr(p+1, '*')) && q < (path + lpath))
	    return Error(0, 0,
		"%t\n\tFullPathName can only have 1 asterisk, \
but the superfile entry\nreads `%s'\n", path);
	if (n > 0)
	    strncpy(FullPath, path, n);
	strcpy(FullPath + n, usrcmd);
	strcat(FullPath, p+1);
	localinfo.progs.commandfound = usrcmd;
	localinfo.progs.cmd_file[indx].File = FullPath;
    } else {
	/* Regular command */
	localinfo.progs.commandfound = usrcmd;
    }
    return 0;
}

/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
/* Process command-line conditions and options.
 * Returns -1 on syntax and other errors;
 * otherwise returns 0 if conditions fail to match;
 * otherwise conditions match, and returns 1;
 */
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
int
conditions_and_options(cond_or_opt_wd)
char *cond_or_opt_wd;	/* First word to process; rest we get with strqtokS */
{
    int condition_match;
    int invert;
    register char *p;

    strqS_qm = my_qm;
    strqS_cc = my_cc;
    if (cond_or_opt_wd) {
	do {

	    p = cond_or_opt_wd;
	    if (*p == CONDITION_SEP || *p == OPTION_SEP)
		return Error(0, 0, "%t\n\tBad syntax: <%s>\n", cond_or_opt_wd);

	    for (p++; *p && ((*p != CONDITION_SEP && *p != OPTION_SEP) ||
						    (*(p-1) == '\\')); p++)
		    ;

	    if (*p == OPTION_SEP) {
		/* ************** */
		/* It's an option */
		/* ************** */
		if (handle_option(cond_or_opt_wd, p+1, 0) == -1)
		    return -1;
		continue;

	    } else if (*p == CONDITION_SEP) {
		/* **************** */
		/* It's a condition */
		/* **************** */
		if (InsertCondition(cond_or_opt_wd, p+1, 0) == -1)
		    return -1;

	    } else {
		/* Default is to treat it as user~ pattern.
		 * Insert in user list.
		 */
		int i;
		char *s, **glob;

		invert = *cond_or_opt_wd == '!';
		s = invert ? cond_or_opt_wd+1 : cond_or_opt_wd;

		/* Do brace globbing */
		if ((i=globbraces(s, 1, &glob)) != 0)
		    return Error(0, 0, "%tMissing `%c'.\n", i);

		if (InsertUserList(s, glob,
		    &localinfo.userpats, &localinfo.origtext, invert) == -1)
		    return -1;
	    }
	} while ( (cond_or_opt_wd = strqtokS(NULL, SEP, NULL, NULL, 1)) );
    }

    condition_match = 1;

    /* Look at condition lists in reverse order, and stop as soon
     * as there is a result.
     */
    if (globalinfo.userafter.next ||
	    localinfo.userpats.next ||
	    globalinfo.userbefore.next) {
	/* We have user fields to match */
	matches.user = 0;	/* default is to disallow user */
	match_ugh_user(&globalinfo.userafter, 1);
	if (!matches.user) {
	    match_ugh_user(&localinfo.userpats, 0);
	    if (!matches.user) {
		match_ugh_user(&globalinfo.userbefore, 1);
	    }
	}
    } else {
	return Error(0, 0,
	"%t\n\tNo user patterns given on control line or in :global line.\n");
    }
    if (!matches.user)
	condition_match = 0;

    if (condition_match != 0) {
	if (globalinfo.timeafter.next ||
		localinfo.time.next ||
		globalinfo.timebefore.next) {
	    /* We have time fields to match */

	    /* Default depends on whether all time fields are inverted */
	    matches.time = -1;
	    matches.allinverted = 1;
	    matchtime(&userinfo.ourtime, &globalinfo.timeafter);
	    if (matches.time == -1) {
		matchtime(&userinfo.ourtime, &localinfo.time);
		if (matches.time == -1) {
		    matchtime(&userinfo.ourtime, &globalinfo.timebefore);
		}
	    }
	    if (matches.time == -1) {
		/* Default is permit execution if all patterns were inverting;
		 * deny execution if there were some non-inverting patterns.
		 */
		condition_match = matches.allinverted ? 1 : 0;
		if (debug || it_came_from_cmdline) {
		    if (matches.allinverted)
			fputs("\tPermission allowed (default for non-match \
when all times are !time~xxx)\n", stderr);
		    else
			fputs("\tPermission denied (default for non-match \
when >=1 time is non-negated)\n", stderr);
		}
	    } else {
		condition_match = matches.time;
	    }
	}
    }
    return condition_match;
}

/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
/* Prints help information title */
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
void
printhelp_hello(verbosity, usrcmd)
int verbosity;
char *usrcmd;
{
    static char *usage[] = {
"    %s command [args...]	\texecutes command [args]",
"    %s -h			\tprint this usage info, then exits",
"    %s -V			\tprint version number, then exits",
"    %s [-H | -f] [MasqueradeOptions]",
"    				\tlist ok cmds: -H=verbose,-f=just-facts",
"    %s [-d | -D] [MasqueradeOptions]  cmd [args...]",
"    				\t...debug mode",
"    %s -b			\tlists builtin variables, then exits",
"    %s -c [superfile]	\tjust check syntax of the superfile",
"",
"Notes on options:",
"    -d    ...basic debug mode: show what would be done,",
"		but don't actually execute a command.",
"    -D    ...same as -d, plus tell more about variables, plus other details.",
"    -f -- (just the facts, ma'm) lists the commands you can execute, as:",
"		    Cmd FullPath [initial args]",
"	      Useful for scripts that want a list of what you may execute.",
"    MasqueradeOptions:",
"	** No command is ever executed when any MasqueradeOption is used.**",
"	Additionally, some debugging output is enabled when these are used:",
"	-F superfile -- names the super file to use.",
"	-G gid -- act as if the invoking user was group gid.",
"	-M mach -- act as if the command was executed from host mach.",
"	-T hh[:mm]/day -- act as if this is execution time.",
"	-U uid -- act as if the invoking user was user uid.",
};

    if (verbosity == HELP_FACTS)
	return;

    if (verbosity == HELP_USAGE) {
	int i;
	for (i = 0; i < NELEM(usage); i++) {
	    fprintf(stderr, usage[i], prog);
	    putc('\n', stderr);
	}
	printf("\nSuper.tab file: `%s'\n\n", superfile);
	return;
    }

    if (verbosity == HELP_FULL) {
	printf("(Use %s -h for general usage information.)\n", prog);
    }

    if (verbosity != HELP_BASIC) {
	printf("Super.tab file: `%s'\n\n", superfile);
    }

    if (usrcmd) {
	if (verbosity == HELP_BASIC)
	    (void) printf("Terse help on command `%s' for user %s:\n\n",
				usrcmd, userinfo.caller.pw_name);
	else if (verbosity == HELP_FULL)
	    (void) printf("Long help on command `%s' for user %s:\n\n",
				usrcmd, userinfo.caller.pw_name);
    } else {
	if (verbosity) {
	    (void) printf("===============================================\n");
	}
	(void) printf("Commands available to user %s%s:\n\n",
		userinfo.caller.pw_name,
		verbosity ? " (use option `-h' for a general usage listing)" :
		" (use option `-H' for a long-winded listing)");
	if (!verbosity) {
	    (void) printf("Command Name    Comments\n");
	    (void) printf("or Pattern              \n");
	    (void) printf("------------    --------\n");
	}
    }
}

/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
/* Prints help information for a command. */
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
void
printhelp(verbosity)
int verbosity;
{
    char **p, *s;
    int i, j, n;

    if (verbosity == HELP_BASIC) {
	if (localinfo.progs.n < 1) {
	    ;
	} else if (localinfo.progs.n == 1) {
	    /* The entry contains a single CmdPattern/FullPath pairs */

	    /* If the command contains a wildcard, print the full filename,
	     * otherwise just the command name.
	     */
	    if (strchr(localinfo.progs.cmd_file[0].Cmd, '*')) {
		printf("%s -> %s:\n                -- %s\n",
			localinfo.progs.cmd_file[0].Cmd,
			localinfo.progs.cmd_file[0].File,
			localinfo.info);
	    } else {
		printf("%-15s %s\n", localinfo.progs.cmd_file[0].Cmd,
			localinfo.info ? localinfo.info : "");
	    }
	} else {
	    /* The entry contains multiple CmdPattern/FullPath pairs */

	    /* If any command contains a wildcard, print the
	     * full filenames, otherwise just the command name.
	     */
	    int havewildcard=0;
	    for (i=0; havewildcard==0 && i<localinfo.progs.n; i++) {
		havewildcard =
			(strchr(localinfo.progs.cmd_file[0].Cmd, '*') != NULL);
	    }
	    if (havewildcard) {
		for (i=0; i<localinfo.progs.n-1; i++) {
		    printf("%s -> %s;\n", prog,
			localinfo.progs.cmd_file[i].Cmd,
			localinfo.progs.cmd_file[i].File);
		}
		i = localinfo.progs.n - 1;
		printf("%s -> %s:\n", prog,
		    localinfo.progs.cmd_file[i].Cmd,
		    localinfo.progs.cmd_file[i].File);
		if (localinfo.info && *localinfo.info)
		    (void) printf("                -- %s\n", localinfo.info);

	    } else {
		/* Now wildcards in names */
		printf("%s", localinfo.progs.cmd_file[0].Cmd);
		for (i=1; i<localinfo.progs.n; i++)
		    printf(", %s", localinfo.progs.cmd_file[i].Cmd);
		if (localinfo.info && *localinfo.info)
		    (void) printf(":\n                -- %s\n",
			localinfo.info);
		else
		    (void) putchar('\n');
	    }
	}
	return;

    } else if (verbosity == HELP_FACTS) {
	for (i=0; i<localinfo.progs.n; i++)
	    (void) printf("%s %s\n", localinfo.progs.cmd_file[i].Cmd,
					localinfo.progs.cmd_file[i].File);
	return;

    } else if (verbosity == HELP_FULL) {
	for (i=0; i<localinfo.progs.n; i++)
	    (void) printf("%s %s -> %s\n", prog,
			localinfo.progs.cmd_file[i].Cmd,
			localinfo.progs.cmd_file[i].File);
    } else {
	(void) Error(0, 0, "%s: printhelp(): Unknown verbosity level %d\n",
			verbosity);
	return;
    }

    /* Assert verbosity == HELP_FULL */
    if (localinfo.info)
	printf("\t## %s\n", localinfo.info);

    if (localinfo.die) {
	printf("\tCommand forces exit using 'die' option:\n\t\tdie=\"%s\"",
		localinfo.die);
	return;
    }

    if (localinfo.print)
	printf("\tCommand prints msg before execution:\n\t\t\"%s\"",
		localinfo.print);

    if (globalinfo.timebefore.next ||
	    globalinfo.timeafter.next ||
	    localinfo.time.next) {
	printf("\tPermitted times for execution (in reverse input order):\n");
	for (j = 0; j<3; j++) {
	    TimeList *tl;
	    switch (j) {
	    case 0: tl = globalinfo.timebefore.next; break;
	    case 1: tl = localinfo.time.next; break;
	    default: tl = globalinfo.timeafter.next; break;
	    }
	    for ( ; tl; tl=tl->next) {
		printf("\t\t%stime~%d:%02d-%d:%02d/%s\n",
		    tl->te.invert ? "!" : "",
		    tl->te.begin / 60, tl->te.begin % 60,
		    tl->te.end / 60, tl->te.end % 60, dayname(tl->te.day));
	    }
	}
    }
    if (*localinfo.user ||
	    *localinfo.group ||
	    *localinfo.u_g ||
	    localinfo.fdlist ||
	    *localinfo.owner ||
	    localinfo.env) {
	(void) fputs("\t(Executes with:", stdout);
	if (*localinfo.user) (void) printf(" uid=%s", localinfo.user);
	if (*localinfo.group) (void) printf(" gid=%s", localinfo.group);
	if (*localinfo.u_g) (void) printf(" u+g=%s", localinfo.u_g);
	if (localinfo.env) {
	    for (p=localinfo.env; *p; p++)
		(void) printf("%s%s", p==localinfo.env ? " env=" : ",", *p);
	}
	if (localinfo.setenv) {
	    for (p=localinfo.setenv; *p; p++)
		(void) printf(" setenv=%s", *p);
	}
	if (localinfo.fdlist) (void) printf(" fdlist=%s", localinfo.fdlist);
	if (*localinfo.owner) (void) printf(" owner=%s", localinfo.owner);
	(void) fputs(")\n", stdout);
    }

    if (localinfo.usr_args[0] < 0)
	;
    else if (localinfo.usr_args[0] == localinfo.usr_args[1] &&
					    localinfo.usr_args[0] == 0)
	(void) printf("\tNo user-entered args are allowed.\n");
    else if (localinfo.usr_args[0] == localinfo.usr_args[1])
	(void) printf("\t%d user-entered arg%s required.\n",
	    localinfo.usr_args[0], localinfo.usr_args[0] == 1? " is" : "s are");
    else
	(void) printf("\t%d - %d user-entered args are required.\n",
	    localinfo.usr_args[0], localinfo.usr_args[1]);

    if (localinfo.maxlen1arg < 0)
	(void) printf("\tNo per-arg length limit; ");
    else
	(void) printf("\tMax per-arg length: %d chars; ", localinfo.maxlen1arg);

    if (localinfo.maxlenargs < 0)
	(void) printf("no limit on sum of all args.\n");
    else
	(void) printf("max over all args: %d chars.\n", localinfo.maxlenargs);

    n = StrLastInUse(&localinfo.argpats);
    if (n >= 0) {
	printf("\tUser-entered arguments must match following patterns:\n");
	for (i = 0; i<=n; i++) {
	    s = StrEltGetPtr(&localinfo.argpats, i);
	    if (s)
		printf("\t\tArg %d must match pattern: %s\n", i, s);
	}
    }

    if (rcl_nice_incr() != 0)
	printf("\tCommand executes with nice increment = %d.\n",
		    rcl_nice_incr());
    if (rcl_umask() != userinfo.orig_mask)
	printf("\tCommand executes with umask set to 0%o.\n", rcl_umask());


    if (localinfo.passinfo.required) {
	if (localinfo.passinfo.timeout == 0)
	    (void) printf("\tPassword is required on each use.\n");
	else
	    (void) printf("\tPassword is required; good for %d minutes%s.\n",
		localinfo.passinfo.timeout, localinfo.passinfo.renewtime ? 
		    ", extended with each use" : "");
    }
    (void) putchar('\n');
}