summaryrefslogtreecommitdiff
path: root/usr/src/cmd/svr4pkg/libinst/scriptvfy.l
blob: f467b840cc28ea6fb7b702464adce470f20e84a9 (plain)
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
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License (the "License").
 * You may not use this file except in compliance with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */

/*
 * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 */

/*
 * The purpose of this lex specification is to estimate the
 * correctness of the various scripts that accompany packages. It
 * is not flawless, but it is a better review than that of prior
 * package validators. It looks for indications of interaction,
 * root calls and attempts to modify locked files.
 */
%e 1500
%p 3500
%s WHROOT
%{
#undef	input
#undef	unput
FILE *scr_fp;
#define	input()		(((yytchar=yysptr>yysbuf?U(*--yysptr):getc(scr_fp))==10?(yylineno++,yytchar):yytchar)==EOF?0:yytchar)
#define	unput(p)	ungetc(p, scr_fp)

#define	INTERACT_D	0x00000001	/* definitely */
#define	ROOT_D		0x00000002
#define	LOCKED_D	0x00000004
#define	INTERACT_M	0x00010000	/* might be true, or we ... */
#define	ROOT_M		0x00020000	/* ... might be reading it wrong. */
#define	LOCKED_M	0x00040000
#define	WPARM1_M	0x00080000	/* attempt to write to $1 */
#define	USEPARM1_M	0x00100000	/* other attempt to use $1 */
#define	ODDPARM_M	0x00200000	/* use of some other parameter */
#define	PKGDB_M		0x00400000	/* read access to DB */
#define	INITVAL		0x40000000

/* Abbreviations */
#define	INTERACT	(INTERACT_D | INTERACT_M)
#define	ROOT		(ROOT_D | ROOT_M)
#define	LOCKED		(LOCKED_D | LOCKED_M)
#define	HASPARM		(WPARM1_M | USEPARM1_M | ODDPARM_M)

/* Things the preinstall and preremove scripts can't do. */
#define	PRE_MASK	(INTERACT | LOCKED | PKGDB_M | HASPARM)
/*
 * Things the class action script can't do. Don't get the impression that
 * this means the class action script can be interactive; but, it can
 * legitimately read stdin (which is what INTERACT tests for).
 */
#define	CAS_MASK	(LOCKED | PKGDB_M | WPARM1_M | ODDPARM_M)
/* Things the postinstall and postremove scripts can't do. */
#define	POST_MASK	(INTERACT | HASPARM)
/* Things the request script can't do. */
#define	REQ_MASK	(ROOT | ODDPARM_M)
/* Things the checkinstall script can't do. */
#define	CHK_MASK	(INTERACT | ROOT | ODDPARM_M)

/* Nothing definite - not worth returning an error */
#define	MAYBE_ONLY	~(INTERACT_D | ROOT_D | LOCKED_D)

#define	WRN_INST_F	"WARNING: script <%s> uses installf but no " \
			    "installf -f was detected."
#define	WRN_REM_F	"WARNING: script <%s> uses removef but no " \
			    "removef -f was detected."
#define	WRN_INTERACT	"WARNING: script <%s> may require " \
			    "user interaction at line <%d>."
#define	WRN_LOCKED	"WARNING: script <%s> may seek access to the " \
			    "transitional package database at line <%d>. " \
			    "This is safest in the postinstall or " \
			    "postremove script."
#define	WRN_ROOT	"WARNING: script <%s> may not have permission " \
			    "to execute line <%d>."
#define	WRN_FORM_ARG	"WARNING: not sure where script <%s> gets the "\
			    "parameter at line <%d>."
#define	WRN_FORM_USE	"WARNING: script <%s> questionable usage of "\
			    "parameter at line <%d>."
#define	WRN_TRANSDB	"WARNING: script <%s> questionable read " \
			    "of package database at line <%d>. An " \
			    "intermediate buffer may be appropriate."
#define	WRN_SPACEACC	"WARNING: script <%s> updates the package database " \
			    "but provides no space file to account for " \
			    "the additional package object."
#define	ERR_INTERACT	"ERROR: script <%s> requires user " \
			    "interaction at line <%d>."
#define	ERR_LOCKED	"ERROR: script <%s> attempts to modify locked " \
			    "package database at line <%d>."
#define	ERR_ROOT	"ERROR: script <%s> requires root permission at " \
			    "line <%d>."
#define	ERR_FOPEN	"ERROR: Cannot evaluate script <%s>, errno=%d."
#define	ERR_ARGS	"ERROR: scripteval() - no script provided for " \
			    "evaluation."
extern int errno;

static int line_no;	/* current line number */
int pipe_release = 0;	/* loop level for release of pipe */
int loop_depth = 0;	/* current number of nested loops */
int case_depth = 0;	/* same for case ... */
int if_depth = 0;	/* ... and if statements */
int cur_level = 0;	/* current number of nested anything */
int braces = 0;		/* depth into a function */

int lock_level = 0;
int root_level = 0;

struct statstrct {
	unsigned int in_function:1;
	unsigned int in_pipe:1;
	unsigned int in_loop:1;
	unsigned int in_case:1;
	unsigned int in_if:1;
	unsigned int in_awk:1;
	unsigned int allow_int:1;	/* Allow an interactive function. */
	unsigned int pkg_rtn_done:1;
	unsigned int pkgchk_f:1;
	unsigned int instf:1;
	unsigned int instf_f:1;
	unsigned int remf:1;
	unsigned int remf_f:1;
	unsigned int nospacefile:1;
	unsigned int needspacefile:1;
} status = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};

%}
%%
%{
/*
 * Validate a few OK patterns that look like bad patterns. These include:
 *	1. comments
 *	2. quoted strings
 *	3. writes to $1 (request script)
 *	4. reads from $1 (CAS)
 *	5. writes to /dev/null
 */
%}
#.*$ 	{
	if (status.in_awk == 0)
		return INITVAL;
	else
		REJECT; /* No comments in the middle of an awk statement */
	}

\`	unput(' ');	/* No executable matching */

%{
/* Anybody can write to /dev/null and anybody can write to /tmp. */
%}
\>[ \t]*"/dev/null"	return INITVAL;
\>[ \t]*"/tmp"		return INITVAL;

%{
/* If it's escaped, the next entry may as well be a space. */
%}
\\	{
	char ch;

	if ((ch = input()) == '\n')
		line_no++;

	unput(' ');
}

%{
/* In the quotes is OK. */
%}
\"	{
	char ch;
	while ((ch = input()) != '\"') {
		if (ch == '\\') {
			input();	/* Read this into the bit bucket. */
			continue;
		}
		if (ch == '\n')
			line_no++;
		else if (ch == '\0')
			return (0);	/* EOF */
	}
}

%{
/* In the single quotes is OK if they aren't associated with an awk script. */
%}
\'	{
	char ch;

	if (status.in_awk != 0) {
		REJECT;;
	}

	while ((ch = input()) != '\'') {
		if (ch == '\\') {
			input();	/* Read this into the bit bucket. */
			continue;
		}
		if (ch == '\n')
			line_no++;
		else if (ch == '\0')
			return (0);	/* EOF */
	}
}

%{
/*
 * Check for use of parameters passed to the script.
 *	1. writes to $1 as though it were a file
 *	2. use of $1 in any capacity
 *	3. use of other parameters
 * Within a function or an awk script, these parameters aren't
 * the one's of interest.
 */
%}
\>[\t ]*\$1/[\t\n ]	{
	if (status.in_function == 0 && status.in_awk == 0)
		return (WPARM1_M);
}

^$1/[\t\n ]	|
[\t ]$1/[\t\n ]	{
	if (status.in_function == 0 && status.in_awk == 0)
		return (USEPARM1_M);
}

\$[2-9]	|
\$[0-9][0-9]+ {
	if (status.in_function == 0 && status.in_awk == 0)
		return (ODDPARM_M);
}

%{
/*
 * Detect shell function.
 */
%}
"()"[ \t]*\n[ \t]*/\{	{ status.in_function = 1; line_no++; }
"()"[ ]*/\{	status.in_function = 1;

"{" {
	if (status.in_function == 1)
		braces++;
}

"}" {
	if (status.in_function == 1) {
		braces--;
		if (braces == 0)
			status.in_function = 0;
	}
}

%{
/*
 * Detect for or while loop.
 */
%}
^for/[\t\n ]		|
^[\t ]+for/[\t\n ]	|
^while/[\t\n ]		|
^[\t ]+while/[\t\n ] {
	status.in_loop = 1;
	loop_depth++;
	cur_level++;
	REJECT;		/* What's in the argument is important too. */
}

^done/[\t\n ] 	|
^[\t ]+done/[\t\n ]  {
	if (status.in_loop == 1) {
		loop_depth--;
		cur_level--;
		if (loop_depth == 0)
			status.in_loop = 0;
	}
}

%{
/*
 * Detect case.
 */
%}
^case/[\t\n ]	|
^[\t ]+case/[\t\n ] {
	status.in_case = 1;
	case_depth++;
	cur_level++;
	REJECT;		/* What's in the argument is important too. */
}

^esac/[\t\n ] 	|
^[\t ]+esac/[\t\n ] {
	if (status.in_case == 1) {
		case_depth--;
		cur_level--;
		if (case_depth == 0)
			status.in_case = 0;
	}
}

%{
/*
 * Detect if.
 */
%}
^if" "*"["	|
^[\t ]+if" "*"[" {
	status.in_if = 1;
	if_depth++;
	cur_level++;
	REJECT;		/* What's in the argument is important too. */
}

^fi/[\t\n ]	|
^[\t ]+fi/[\t\n ]  {
	if (status.in_if == 1) {
		if_depth--;
		cur_level--;
		if (if_depth == 0)
			status.in_if = 0;
	}
}

%{
/*
 * Detect awk or nawk function. If the function is enclosed in "`"s
 * the entire line will be grabbed., so we check for that possibility.
 */
%}
^n?awk[^\n^']*\' 	|
[\t \\\(\/]n?awk[^\n^']*\'	{status.in_awk = 1;
#ifdef VERBOSE
	printf("open awk statment, line %d\n", line_no);
#endif
}


\' {
	if (status.in_awk == 1) {
#ifdef VERBOSE
		printf("close awk statement, line %d\n", line_no);
#endif
		status.in_awk = 0;
	}
}

%{
/* Detect pipe target. */
%}
[\$A-Za-z]	{
	if (status.in_pipe == 1 && pipe_release == cur_level) {
		status.in_pipe = 0;	/* target located */
		pipe_release = 0;
#ifdef VERBOSE
		printf("end pipe, line %d\n", line_no);
#endif
		status.allow_int = 1;	/* this isn't really interactive. */
		REJECT;	/* put it back */
	}
}

%{
/* If it's a pipe, note that and continue. */
%}
"||"		|
"|"		{
	if (status.in_pipe == 0) {
		status.in_pipe = 1;
#ifdef VERBOSE
		printf("start pipe, line %d\n", line_no);
#endif
		pipe_release = cur_level;
	}
}

%{
/*
 * Test input for admin-type telltale interactive functions. Definite's
 * first, maybe's next.
 */
%}
^ckdate/[\t\n ]		|
[\t \/]ckdate/[\t\n ]	|
^ckint/[\t\n ]		|
[\t \/]ckint/[\t\n ]	|
^ckrange/[\t\n ]	|
[\t \/]ckrange/[\t\n ]	|
^cktime/[\t\n ]		|
[\t \/]cktime/[\t\n ]	|
^ckyorn/[\t\n ]		|
[\t \/]ckyorn/[\t\n ]	|
^ckgid/[\t\n ]		|
[\t \/]ckgid/[\t\n ]	|
^ckpath/[\t\n ]		|
[\t \/]ckpath/[\t\n ]	|
^ckstr/[\t\n ]		|
[\t \/]ckstr/[\t\n ]	|
^ckuid/[\t\n ]		|
[\t \/]ckuid/[\t\n ]		{
	if (status.in_pipe == 1 || status.allow_int == 1)
		return (INITVAL);
	else
		return (INTERACT_M);	/* maybe should be _D */
}

^read/[\t\n ]		|
[\t ]read/[\t\n ]	|
"=[ ]+&<"[\t ]	{
	if (status.in_pipe == 1 || status.allow_int == 1)
		return (INITVAL);
	else
		return (INTERACT_M);
}

%{
/* Scan for root authority commands. Definite's first, maybe's next. */
%}
^mkdir/[\t\n ]		|
[\t \/]mkdir/[\t\n ]	|
^mv/[\t\n ]		|
[\t \/]mv/[\t\n ]	|
^cpio/[\t\n ]		|
[\t \/]cpio/[\t\n ]	|
^tar/[\t\n ]		|
[\t \/]tar/[\t\n ]	|
^(un)?compress/[\t\n ]	|
[\t \/](un)?compress/[\t\n ]	|
^rmdir/[\t\n ]		|
[\t \/]rmdir/[\t\n ]	return (ROOT_D);

^r?cp(dir)?/[\t\n ]	|
[\t \/]r?cp(dir)?/[\t\n ]	|
^rm/[\t\n ]	|
[\t \/]rm/[\t\n ]	|
\>[ \t]*[\$\/a-zA-Z0-9]	return (ROOT_M);

%{
/* These root commands may also be locked. */

/* Here we analyze any pkgchk calls. If it's "pkgchk ... -f ..." then that calls for root authority. We then check for a "-R" argument. */
%}
^pkgchk[^\n^|^>^;]*"-f"	|
[\t \/]pkgchk[^\n^|^>^;]*"-f"	{
	status.pkgchk_f = 1;
	REJECT;		/* We need the intermediate args. */
}

%{
/* If it's "pkgchk ... -R ..." then the local package database is not being tested and no database warning is necessary. */
%}
^pkgchk[^\n^|^>^;]*"-R"[ \t][\/\$]/[^ ^\t^\n]		|
[\t \/]pkgchk[^\n^|^>^;]*"-R"[ \t][\/\$]/[^ ^\t^\n]  {
	if (status.pkgchk_f)
		return (ROOT_D);
	else
		return (INITVAL);
}

%{
/* If it's just "pkgchk ..." then we need to mention something about access to the package database. With Solaris 2.5, an improved locking mechanism is in place, so this message may be something we can drop later. */
%}
^pkgchk/[\t\n ]		|
[\t \/]pkgchk/[\t\n ]  {
	if (status.pkgchk_f) {
		status.pkgchk_f = 0;
		return (ROOT_D | PKGDB_M);
	} else
		return (PKGDB_M);
}

%{
/* The installf and removef utilities require root authority, they modify the package database and they must be invoked at least once with a "-f" argument. */

/* First test for a "-f" argument. */
%}
^installf[^\n^|^>^;]*"-f"	|
[\t \/]installf[^\n^|^>^;]*"-f"	{
	status.instf_f = 1;

	REJECT;		/* The whole line needs to be re-reviewed. */
}

^removef[^\n^|^>^;]*"-f"	|
[\t \/]removef[^\n^|^>^;]*"-f"	{
	status.remf_f = 1;

	REJECT;		/* The whole line needs to be re-reviewed. */
}

^installf/[\t\n ]	|
[\t \/]installf/[\t\n ]	{
	status.instf = 1;
	status.needspacefile = 1;

	root_level = ROOT_D;
	lock_level = LOCKED_M;

	BEGIN WHROOT;
}

^removef/[\t\n ]	|
[\t \/]removef/[\t\n ]	{
	status.remf = 1;

	root_level = ROOT_D;
	lock_level = LOCKED_M;
	BEGIN WHROOT;
}

%{
/* There's no question that use of a pkgadd or pkgrm in a script is bound to cause problems unless it is to a different root. */
%}
^pkgadd/[\t\n ]	|
[\t \/]pkgadd/[\t\n ]	|
^pkgrm/[\t\n ]		|
[\t \/]pkgrm/[\t\n ] {
	root_level = ROOT_D;
	lock_level = LOCKED_D;
	BEGIN WHROOT;
}

%{
/* The only way to get here is if we are in the middle of a pkg command. */
%}
<WHROOT>. {
	if (status.pkg_rtn_done) {
		status.pkg_rtn_done = 0;
		BEGIN 0;
	} else
		REJECT;
}
<WHROOT>[ \t]+"-R"[ \t][\/\$]/[^ ^\t^\n] {
	status.pkg_rtn_done = 1;
	return (root_level);		/* "-R" means locking is unlikely. */
}
<WHROOT>[\n]		{
	if (status.pkg_rtn_done) {
		status.pkg_rtn_done = 0;
		line_no++;
		BEGIN 0;
	} else {
		status.pkg_rtn_done = 1;
		unput('\n');
		return (root_level | lock_level); /* No "-R". */
	}
}
<WHROOT>[;|>]		{
	status.pkg_rtn_done = 1;
	return (root_level | lock_level); /* End of command without a "-R". */
}

\n	{ line_no++; status.allow_int = 0; 
#ifdef VERBOSE
	printf("allow_int = 0\n");
#endif
}

.	{
	/*
	XXX - bug - resets prematurely if we pipe into a while loop or
	other such construct
	status.allow_int = 0;
	*/ 
}

%%
#include <stdio.h>
#include <limits.h>
#include <dirent.h>
#include <unistd.h>
#include <libintl.h>

#ifdef DEBUG
/*
 * Since this is a lex specification twice removed from the binary,
 * I strongly recommend leaving the DEBUG portions in place. When new
 * keywords are added, this will be very important. After modifying
 * the specification, create an executable to test in this way.
 *
 *	lex scriptvfy.l
 *	cc -o scriptvfy -g lex.yy.c $ROOT/usr/lib/libpkg.a \
 *	    -DDEBUG [-DVERBOSE] -ll -lintl
 *	scriptvfy test_directory
 */

main(int argc, char *argv[])
{
	int val;

	line_no = 1;

	if (argc == 1) {
		printf("No directory provided.\n");
		exit(1);
	}

	val = checkscripts(argv[1], 0);

	printf("return code is %d\n", val);
}
#endif

/*
 * This function evaluates the provided script and returns a bit string
 * describing what patterns were located.
 */
static int
scripteval(char *script_name, char *script_path, int mask, int silent)
{
	int val = 0;
	int error = 0;
	line_no = 1;

	if ((script_path == NULL) || (*script_path == NULL) ||
	    (script_name == NULL)) {
		logerr(gettext(ERR_ARGS));
		return (0);
	}

#ifdef VERBOSE
	printf("Evaluating %s\n", script_path);
#endif

	if ((scr_fp = fopen(script_path, "r")) == NULL) {
		logerr(gettext(ERR_FOPEN), script_path, errno);
		return (0);
	}

#ifdef VERBOSE
	printf("Opened script\n");
#endif

	while (val = yylex()) {
#ifdef VERBOSE
		printf("  Match is %s, returned 0x%x at line %d\n",
		    yytext, val, line_no);
		printf("    in_function = %d, in_awk = %d, in_loop = %d, " \
		    "in_case = %d, in_if = %d, in_pipe = %d\n",
		    status.in_function, status.in_awk, status.in_loop,
		    status.in_case, status.in_if, status.in_pipe);
		printf("    loop_depth = %d, case_depth = %d, " \
		    "if_depth = %d, pipe_release = %d, cur_level = %d\n",
		    loop_depth, case_depth, if_depth, pipe_release, cur_level);
#endif

		val &= mask;
		if (val) {
			error |= ((val & MAYBE_ONLY) ? 1 : 2);

			/*
			 * So at this point, val contains all status bits
			 * appropriate to this script.
			 */
			if (!silent) {
				char *msg_ptr;
				if (val & INTERACT_D)
					msg_ptr = gettext(ERR_INTERACT);
				else if (val & ROOT_D)
					msg_ptr = gettext(ERR_ROOT);
				else if (val & LOCKED_D)
					msg_ptr = gettext(ERR_LOCKED);
				else if (val & INTERACT_M)
					msg_ptr = gettext(WRN_INTERACT);
				else if (val & ROOT_M)
					msg_ptr = gettext(WRN_ROOT);
				else if (val & LOCKED_M)
					msg_ptr = gettext(WRN_LOCKED);
				else if (val & WPARM1_M)
					msg_ptr = gettext(WRN_FORM_USE);
				else if (val & USEPARM1_M)
					msg_ptr = gettext(WRN_FORM_USE);
				else if (val &  ODDPARM_M)
					msg_ptr = gettext(WRN_FORM_ARG);
				else if (val &  PKGDB_M)
					msg_ptr = gettext(WRN_TRANSDB);
				else
					msg_ptr = gettext("unknown error");

				logerr(msg_ptr, script_name, line_no);
			}
		}
	}

	/* Warn if required about missing "-f" calls. */
	if (status.instf && !(status.instf_f))
		logerr(gettext(WRN_INST_F), script_name);

	if (status.remf && !(status.remf_f))
		logerr(gettext(WRN_REM_F), script_name);

	status.instf = status.instf_f = status.remf = status.remf_f = 0;

	/* Warn if installf was used but no space file is in place. */
	if (status.nospacefile && status.needspacefile) {
		logerr(gettext(WRN_SPACEACC), script_name);
		status.needspacefile = 0;
	}

	status.in_pipe = 0;	/* Pipes may dangle. */
	fclose(scr_fp);

	if (error == 3)
		error = 2;

	return (error);
}

/* Test a preinstall or preremove script for validity. */
int
pre_valid(char *script_name, char *script_path, int silent)
{
	return (scripteval(script_name, script_path, PRE_MASK, silent));
}

/* Test a class action script for validity. */
int
cas_valid(char *script_name, char *script_path, int silent)
{
	return (scripteval(script_name, script_path, CAS_MASK, silent));
}

/* Test a postinstall or postremove script for validity. */
int
post_valid(char *script_name, char *script_path, int silent)
{
	return (scripteval(script_name, script_path, POST_MASK, silent));
}

/* Test a class action script for validity. */
int
req_valid(char *script_name, char *script_path, int silent)
{
	return (scripteval(script_name, script_path, REQ_MASK, silent));
}


/* Test a class action script for validity. */
int
chk_valid(char *script_name, char *script_path, int silent)
{
	return (scripteval(script_name, script_path, CHK_MASK, silent));
}

/* This tests all of the scripts in the provided directory. */
int
checkscripts(char *inst_dir, int silent)
{
	DIR *dirfp;
	struct dirent *dp;
	char path[PATH_MAX];
	int retval = 0;

	/* For future reference, determine if a space file is present. */
	sprintf(path, "%s/%s", inst_dir, "space");
	if (access(path, F_OK) != 0)
		status.nospacefile = 1;

	if ((dirfp = opendir(inst_dir)) == NULL)
		return (0);

	while ((dp = readdir(dirfp)) != NULL) {
#ifdef VERBOSE
		printf("Looking at file %s\n", dp->d_name);
#endif
#ifdef BUG_DEBUG
		if ((status.in_function != 0)
		    || (status.in_pipe != 0)
		    || (status.in_loop != 0)
		    || (status.in_case != 0)
		    || (status.in_if != 0)
		    || (status.in_awk != 0)
		    || (pipe_release != 0)
		    || (loop_depth != 0)
		    || (case_depth != 0)
		    || (if_depth != 0)
		    || (cur_level != 0)
		    || (braces != 0)) {
			printf("    in_function = %d, in_awk = %d, "
			    "in_loop = %d, in_case = %d, in_if = %d, "
			    "in_pipe = %d\n",
			    status.in_function, status.in_awk, status.in_loop,
			    status.in_case, status.in_if, status.in_pipe);
			printf("    loop_depth = %d, case_depth = %d, "
			    "if_depth = %d, pipe_release = %d, "
			    "cur_level = %d\n",
			    loop_depth, case_depth, if_depth, pipe_release,
			    cur_level);
			printf("ERROR: found a bug: variable still open\n");
			return (0);
		} else {
			printf("SUCCESS: All variables reset.\n");
		}
#endif
		/* Reset all variables before processing the next file */
		status.in_function = status.in_pipe = status.in_loop =
		    status.in_case = status.in_if = status.in_awk = 0;
		pipe_release = loop_depth = case_depth = if_depth =
		    cur_level = braces = 0;

		if (dp->d_name[0] == '.')
			continue;

		if ((strcmp(dp->d_name, "preinstall") == 0) ||
		    (strcmp(dp->d_name, "preremove") == 0)) {
			sprintf(path, "%s/%s", inst_dir, dp->d_name);
			retval |= pre_valid(dp->d_name, path, silent);
			continue;
		}

		if ((strncmp(dp->d_name, "i.", 2) == 0) ||
		    (strncmp(dp->d_name, "r.", 2) == 0)) {
			sprintf(path, "%s/%s", inst_dir, dp->d_name);
			retval |= cas_valid(dp->d_name, path, silent);
			continue;
		}

		if ((strcmp(dp->d_name, "postinstall") == 0) ||
		    (strcmp(dp->d_name, "postremove") == 0)) {
			sprintf(path, "%s/%s", inst_dir, dp->d_name);
			retval |= post_valid(dp->d_name, path, silent);
			continue;
		}

		if (strcmp(dp->d_name, "request") == 0) {
			sprintf(path, "%s/%s", inst_dir, dp->d_name);
			retval |= req_valid(dp->d_name, path, silent);
			continue;
		}
		if (strcmp(dp->d_name, "checkinstall") == 0) {
			sprintf(path, "%s/%s", inst_dir, dp->d_name);
			retval |= chk_valid(dp->d_name, path, silent);
			continue;
		}
	}

	(void) closedir(dirfp);

	return (retval);
}