4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
23 * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
28 * The purpose of this lex specification is to estimate the
29 * correctness of the various scripts that accompany packages. It
30 * is not flawless, but it is a better review than that of prior
31 * package validators. It looks for indications of interaction,
32 * root calls and attempts to modify locked files.
41 #define input() (((yytchar=yysptr>yysbuf?U(*--yysptr):getc(scr_fp))==10?(yylineno++,yytchar):yytchar)==EOF?0:yytchar)
42 #define unput(p) ungetc(p, scr_fp)
44 #define INTERACT_D 0x00000001 /* definitely */
45 #define ROOT_D 0x00000002
46 #define LOCKED_D 0x00000004
47 #define INTERACT_M 0x00010000 /* might be true, or we ... */
48 #define ROOT_M 0x00020000 /* ... might be reading it wrong. */
49 #define LOCKED_M 0x00040000
50 #define WPARM1_M 0x00080000 /* attempt to write to $1 */
51 #define USEPARM1_M 0x00100000 /* other attempt to use $1 */
52 #define ODDPARM_M 0x00200000 /* use of some other parameter */
53 #define PKGDB_M 0x00400000 /* read access to DB */
54 #define INITVAL 0x40000000
57 #define INTERACT (INTERACT_D | INTERACT_M)
58 #define ROOT (ROOT_D | ROOT_M)
59 #define LOCKED (LOCKED_D | LOCKED_M)
60 #define HASPARM (WPARM1_M | USEPARM1_M | ODDPARM_M)
62 /* Things the preinstall and preremove scripts can't do. */
63 #define PRE_MASK (INTERACT | LOCKED | PKGDB_M | HASPARM)
65 * Things the class action script can't do. Don't get the impression that
66 * this means the class action script can be interactive; but, it can
67 * legitimately read stdin (which is what INTERACT tests for).
69 #define CAS_MASK (LOCKED | PKGDB_M | WPARM1_M | ODDPARM_M)
70 /* Things the postinstall and postremove scripts can't do. */
71 #define POST_MASK (INTERACT | HASPARM)
72 /* Things the request script can't do. */
73 #define REQ_MASK (ROOT | ODDPARM_M)
74 /* Things the checkinstall script can't do. */
75 #define CHK_MASK (INTERACT | ROOT | ODDPARM_M)
77 /* Nothing definite - not worth returning an error */
78 #define MAYBE_ONLY ~(INTERACT_D | ROOT_D | LOCKED_D)
80 #define WRN_INST_F "WARNING: script <%s> uses installf but no " \
81 "installf -f was detected."
82 #define WRN_REM_F "WARNING: script <%s> uses removef but no " \
83 "removef -f was detected."
84 #define WRN_INTERACT "WARNING: script <%s> may require " \
85 "user interaction at line <%d>."
86 #define WRN_LOCKED "WARNING: script <%s> may seek access to the " \
87 "transitional package database at line <%d>. " \
88 "This is safest in the postinstall or " \
90 #define WRN_ROOT "WARNING: script <%s> may not have permission " \
91 "to execute line <%d>."
92 #define WRN_FORM_ARG "WARNING: not sure where script <%s> gets the "\
93 "parameter at line <%d>."
94 #define WRN_FORM_USE "WARNING: script <%s> questionable usage of "\
95 "parameter at line <%d>."
96 #define WRN_TRANSDB "WARNING: script <%s> questionable read " \
97 "of package database at line <%d>. An " \
98 "intermediate buffer may be appropriate."
99 #define WRN_SPACEACC "WARNING: script <%s> updates the package database " \
100 "but provides no space file to account for " \
101 "the additional package object."
102 #define ERR_INTERACT "ERROR: script <%s> requires user " \
103 "interaction at line <%d>."
104 #define ERR_LOCKED "ERROR: script <%s> attempts to modify locked " \
105 "package database at line <%d>."
106 #define ERR_ROOT "ERROR: script <%s> requires root permission at " \
108 #define ERR_FOPEN "ERROR: Cannot evaluate script <%s>, errno=%d."
109 #define ERR_ARGS "ERROR: scripteval() - no script provided for " \
113 static int line_no; /* current line number */
114 int pipe_release = 0; /* loop level for release of pipe */
115 int loop_depth = 0; /* current number of nested loops */
116 int case_depth = 0; /* same for case ... */
117 int if_depth = 0; /* ... and if statements */
118 int cur_level = 0; /* current number of nested anything */
119 int braces = 0; /* depth into a function */
125 unsigned int in_function:1;
126 unsigned int in_pipe:1;
127 unsigned int in_loop:1;
128 unsigned int in_case:1;
129 unsigned int in_if:1;
130 unsigned int in_awk:1;
131 unsigned int allow_int:1; /* Allow an interactive function. */
132 unsigned int pkg_rtn_done:1;
133 unsigned int pkgchk_f:1;
134 unsigned int instf:1;
135 unsigned int instf_f:1;
137 unsigned int remf_f:1;
138 unsigned int nospacefile:1;
139 unsigned int needspacefile:1;
140 } status = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
146 * Validate a few OK patterns that look like bad patterns. These include:
149 * 3. writes to $1 (request script)
150 * 4. reads from $1 (CAS)
151 * 5. writes to /dev/null
155 if (status.in_awk == 0)
158 REJECT; /* No comments in the middle of an awk statement */
161 \` unput(' '); /* No executable matching */
164 /* Anybody can write to /dev/null and anybody can write to /tmp. */
166 \>[ \t]*"/dev/null" return INITVAL;
167 \>[ \t]*"/tmp" return INITVAL;
170 /* If it's escaped, the next entry may as well be a space. */
175 if ((ch = input()) == '\n')
182 /* In the quotes is OK. */
186 while ((ch = input()) != '\"') {
188 input(); /* Read this into the bit bucket. */
194 return (0); /* EOF */
199 /* In the single quotes is OK if they aren't associated with an awk script. */
204 if (status.in_awk != 0) {
208 while ((ch = input()) != '\'') {
210 input(); /* Read this into the bit bucket. */
216 return (0); /* EOF */
222 * Check for use of parameters passed to the script.
223 * 1. writes to $1 as though it were a file
224 * 2. use of $1 in any capacity
225 * 3. use of other parameters
226 * Within a function or an awk script, these parameters aren't
227 * the one's of interest.
230 \>[\t ]*\$1/[\t\n ] {
231 if (status.in_function == 0 && status.in_awk == 0)
237 if (status.in_function == 0 && status.in_awk == 0)
243 if (status.in_function == 0 && status.in_awk == 0)
249 * Detect shell function.
252 "()"[ \t]*\n[ \t]*/\{ { status.in_function = 1; line_no++; }
253 "()"[ ]*/\{ status.in_function = 1;
256 if (status.in_function == 1)
261 if (status.in_function == 1) {
264 status.in_function = 0;
270 * Detect for or while loop.
276 ^[\t ]+while/[\t\n ] {
280 REJECT; /* What's in the argument is important too. */
284 ^[\t ]+done/[\t\n ] {
285 if (status.in_loop == 1) {
299 ^[\t ]+case/[\t\n ] {
303 REJECT; /* What's in the argument is important too. */
307 ^[\t ]+esac/[\t\n ] {
308 if (status.in_case == 1) {
326 REJECT; /* What's in the argument is important too. */
331 if (status.in_if == 1) {
341 * Detect awk or nawk function. If the function is enclosed in "`"s
342 * the entire line will be grabbed., so we check for that possibility.
346 [\t \\\(\/]n?awk[^\n^']*\' {status.in_awk = 1;
348 printf("open awk statment, line %d\n", line_no);
354 if (status.in_awk == 1) {
356 printf("close awk statement, line %d\n", line_no);
363 /* Detect pipe target. */
366 if (status.in_pipe == 1 && pipe_release == cur_level) {
367 status.in_pipe = 0; /* target located */
370 printf("end pipe, line %d\n", line_no);
372 status.allow_int = 1; /* this isn't really interactive. */
373 REJECT; /* put it back */
378 /* If it's a pipe, note that and continue. */
382 if (status.in_pipe == 0) {
385 printf("start pipe, line %d\n", line_no);
387 pipe_release = cur_level;
393 * Test input for admin-type telltale interactive functions. Definite's
394 * first, maybe's next.
398 [\t \/]ckdate/[\t\n ] |
400 [\t \/]ckint/[\t\n ] |
402 [\t \/]ckrange/[\t\n ] |
404 [\t \/]cktime/[\t\n ] |
406 [\t \/]ckyorn/[\t\n ] |
408 [\t \/]ckgid/[\t\n ] |
410 [\t \/]ckpath/[\t\n ] |
412 [\t \/]ckstr/[\t\n ] |
414 [\t \/]ckuid/[\t\n ] {
415 if (status.in_pipe == 1 || status.allow_int == 1)
418 return (INTERACT_M); /* maybe should be _D */
424 if (status.in_pipe == 1 || status.allow_int == 1)
431 /* Scan for root authority commands. Definite's first, maybe's next. */
434 [\t \/]mkdir/[\t\n ] |
438 [\t \/]cpio/[\t\n ] |
441 ^(un)?compress/[\t\n ] |
442 [\t \/](un)?compress/[\t\n ] |
444 [\t \/]rmdir/[\t\n ] return (ROOT_D);
446 ^r?cp(dir)?/[\t\n ] |
447 [\t \/]r?cp(dir)?/[\t\n ] |
450 \>[ \t]*[\$\/a-zA-Z0-9] return (ROOT_M);
453 /* These root commands may also be locked. */
455 /* Here we analyze any pkgchk calls. If it's "pkgchk ... -f ..." then that calls for root authority. We then check for a "-R" argument. */
457 ^pkgchk[^\n^|^>^;]*"-f" |
458 [\t \/]pkgchk[^\n^|^>^;]*"-f" {
460 REJECT; /* We need the intermediate args. */
464 /* If it's "pkgchk ... -R ..." then the local package database is not being tested and no database warning is necessary. */
466 ^pkgchk[^\n^|^>^;]*"-R"[ \t][\/\$]/[^ ^\t^\n] |
467 [\t \/]pkgchk[^\n^|^>^;]*"-R"[ \t][\/\$]/[^ ^\t^\n] {
475 /* 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. */
478 [\t \/]pkgchk/[\t\n ] {
479 if (status.pkgchk_f) {
481 return (ROOT_D | PKGDB_M);
487 /* 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. */
489 /* First test for a "-f" argument. */
491 ^installf[^\n^|^>^;]*"-f" |
492 [\t \/]installf[^\n^|^>^;]*"-f" {
495 REJECT; /* The whole line needs to be re-reviewed. */
498 ^removef[^\n^|^>^;]*"-f" |
499 [\t \/]removef[^\n^|^>^;]*"-f" {
502 REJECT; /* The whole line needs to be re-reviewed. */
506 [\t \/]installf/[\t\n ] {
508 status.needspacefile = 1;
511 lock_level = LOCKED_M;
517 [\t \/]removef/[\t\n ] {
521 lock_level = LOCKED_M;
526 /* 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. */
529 [\t \/]pkgadd/[\t\n ] |
531 [\t \/]pkgrm/[\t\n ] {
533 lock_level = LOCKED_D;
538 /* The only way to get here is if we are in the middle of a pkg command. */
541 if (status.pkg_rtn_done) {
542 status.pkg_rtn_done = 0;
547 <WHROOT>[ \t]+"-R"[ \t][\/\$]/[^ ^\t^\n] {
548 status.pkg_rtn_done = 1;
549 return (root_level); /* "-R" means locking is unlikely. */
552 if (status.pkg_rtn_done) {
553 status.pkg_rtn_done = 0;
557 status.pkg_rtn_done = 1;
559 return (root_level | lock_level); /* No "-R". */
563 status.pkg_rtn_done = 1;
564 return (root_level | lock_level); /* End of command without a "-R". */
567 \n { line_no++; status.allow_int = 0;
569 printf("allow_int = 0\n");
575 XXX - bug - resets prematurely if we pipe into a while loop or
577 status.allow_int = 0;
590 * Since this is a lex specification twice removed from the binary,
591 * I strongly recommend leaving the DEBUG portions in place. When new
592 * keywords are added, this will be very important. After modifying
593 * the specification, create an executable to test in this way.
596 * cc -o scriptvfy -g lex.yy.c $ROOT/usr/lib/libpkg.a \
597 * -DDEBUG [-DVERBOSE] -ll -lintl
598 * scriptvfy test_directory
601 main(int argc, char *argv[])
608 printf("No directory provided.\n");
612 val = checkscripts(argv[1], 0);
614 printf("return code is %d\n", val);
619 * This function evaluates the provided script and returns a bit string
620 * describing what patterns were located.
623 scripteval(char *script_name, char *script_path, int mask, int silent)
629 if ((script_path == NULL) || (*script_path == NULL) ||
630 (script_name == NULL)) {
631 logerr(gettext(ERR_ARGS));
636 printf("Evaluating %s\n", script_path);
639 if ((scr_fp = fopen(script_path, "r")) == NULL) {
640 logerr(gettext(ERR_FOPEN), script_path, errno);
645 printf("Opened script\n");
648 while (val = yylex()) {
650 printf(" Match is %s, returned 0x%x at line %d\n",
651 yytext, val, line_no);
652 printf(" in_function = %d, in_awk = %d, in_loop = %d, " \
653 "in_case = %d, in_if = %d, in_pipe = %d\n",
654 status.in_function, status.in_awk, status.in_loop,
655 status.in_case, status.in_if, status.in_pipe);
656 printf(" loop_depth = %d, case_depth = %d, " \
657 "if_depth = %d, pipe_release = %d, cur_level = %d\n",
658 loop_depth, case_depth, if_depth, pipe_release, cur_level);
663 error |= ((val & MAYBE_ONLY) ? 1 : 2);
666 * So at this point, val contains all status bits
667 * appropriate to this script.
671 if (val & INTERACT_D)
672 msg_ptr = gettext(ERR_INTERACT);
673 else if (val & ROOT_D)
674 msg_ptr = gettext(ERR_ROOT);
675 else if (val & LOCKED_D)
676 msg_ptr = gettext(ERR_LOCKED);
677 else if (val & INTERACT_M)
678 msg_ptr = gettext(WRN_INTERACT);
679 else if (val & ROOT_M)
680 msg_ptr = gettext(WRN_ROOT);
681 else if (val & LOCKED_M)
682 msg_ptr = gettext(WRN_LOCKED);
683 else if (val & WPARM1_M)
684 msg_ptr = gettext(WRN_FORM_USE);
685 else if (val & USEPARM1_M)
686 msg_ptr = gettext(WRN_FORM_USE);
687 else if (val & ODDPARM_M)
688 msg_ptr = gettext(WRN_FORM_ARG);
689 else if (val & PKGDB_M)
690 msg_ptr = gettext(WRN_TRANSDB);
692 msg_ptr = gettext("unknown error");
694 logerr(msg_ptr, script_name, line_no);
699 /* Warn if required about missing "-f" calls. */
700 if (status.instf && !(status.instf_f))
701 logerr(gettext(WRN_INST_F), script_name);
703 if (status.remf && !(status.remf_f))
704 logerr(gettext(WRN_REM_F), script_name);
706 status.instf = status.instf_f = status.remf = status.remf_f = 0;
708 /* Warn if installf was used but no space file is in place. */
709 if (status.nospacefile && status.needspacefile) {
710 logerr(gettext(WRN_SPACEACC), script_name);
711 status.needspacefile = 0;
714 status.in_pipe = 0; /* Pipes may dangle. */
723 /* Test a preinstall or preremove script for validity. */
725 pre_valid(char *script_name, char *script_path, int silent)
727 return (scripteval(script_name, script_path, PRE_MASK, silent));
730 /* Test a class action script for validity. */
732 cas_valid(char *script_name, char *script_path, int silent)
734 return (scripteval(script_name, script_path, CAS_MASK, silent));
737 /* Test a postinstall or postremove script for validity. */
739 post_valid(char *script_name, char *script_path, int silent)
741 return (scripteval(script_name, script_path, POST_MASK, silent));
744 /* Test a class action script for validity. */
746 req_valid(char *script_name, char *script_path, int silent)
748 return (scripteval(script_name, script_path, REQ_MASK, silent));
752 /* Test a class action script for validity. */
754 chk_valid(char *script_name, char *script_path, int silent)
756 return (scripteval(script_name, script_path, CHK_MASK, silent));
759 /* This tests all of the scripts in the provided directory. */
761 checkscripts(char *inst_dir, int silent)
768 /* For future reference, determine if a space file is present. */
769 sprintf(path, "%s/%s", inst_dir, "space");
770 if (access(path, F_OK) != 0)
771 status.nospacefile = 1;
773 if ((dirfp = opendir(inst_dir)) == NULL)
776 while ((dp = readdir(dirfp)) != NULL) {
778 printf("Looking at file %s\n", dp->d_name);
781 if ((status.in_function != 0)
782 || (status.in_pipe != 0)
783 || (status.in_loop != 0)
784 || (status.in_case != 0)
785 || (status.in_if != 0)
786 || (status.in_awk != 0)
787 || (pipe_release != 0)
793 printf(" in_function = %d, in_awk = %d, "
794 "in_loop = %d, in_case = %d, in_if = %d, "
796 status.in_function, status.in_awk, status.in_loop,
797 status.in_case, status.in_if, status.in_pipe);
798 printf(" loop_depth = %d, case_depth = %d, "
799 "if_depth = %d, pipe_release = %d, "
801 loop_depth, case_depth, if_depth, pipe_release,
803 printf("ERROR: found a bug: variable still open\n");
806 printf("SUCCESS: All variables reset.\n");
809 /* Reset all variables before processing the next file */
810 status.in_function = status.in_pipe = status.in_loop =
811 status.in_case = status.in_if = status.in_awk = 0;
812 pipe_release = loop_depth = case_depth = if_depth =
813 cur_level = braces = 0;
815 if (dp->d_name[0] == '.')
818 if ((strcmp(dp->d_name, "preinstall") == 0) ||
819 (strcmp(dp->d_name, "preremove") == 0)) {
820 sprintf(path, "%s/%s", inst_dir, dp->d_name);
821 retval |= pre_valid(dp->d_name, path, silent);
825 if ((strncmp(dp->d_name, "i.", 2) == 0) ||
826 (strncmp(dp->d_name, "r.", 2) == 0)) {
827 sprintf(path, "%s/%s", inst_dir, dp->d_name);
828 retval |= cas_valid(dp->d_name, path, silent);
832 if ((strcmp(dp->d_name, "postinstall") == 0) ||
833 (strcmp(dp->d_name, "postremove") == 0)) {
834 sprintf(path, "%s/%s", inst_dir, dp->d_name);
835 retval |= post_valid(dp->d_name, path, silent);
839 if (strcmp(dp->d_name, "request") == 0) {
840 sprintf(path, "%s/%s", inst_dir, dp->d_name);
841 retval |= req_valid(dp->d_name, path, silent);
844 if (strcmp(dp->d_name, "checkinstall") == 0) {
845 sprintf(path, "%s/%s", inst_dir, dp->d_name);
846 retval |= chk_valid(dp->d_name, path, silent);
851 (void) closedir(dirfp);