Slightly more refined lock() keyword recognition (using %INC).
[p5sagit/p5-mst-13.2.git] / taint.c
1 /*
2  * "...we will have peace, when you and all your works have perished--and
3  * the works of your dark master to whom you would deliver us.  You are a
4  * liar, Saruman, and a corrupter of men's hearts."  --Theoden
5  */
6
7 #include "EXTERN.h"
8 #include "perl.h"
9
10 void
11 taint_proper(f, s)
12 const char *f;
13 char *s;
14 {
15     dTHR;       /* just for taint */
16     char *ug;
17
18     DEBUG_u(PerlIO_printf(Perl_debug_log,
19             "%s %d %d %d\n", s, tainted, uid, euid));
20
21     if (tainted) {
22         if (euid != uid)
23             ug = " while running setuid";
24         else if (egid != gid)
25             ug = " while running setgid";
26         else
27             ug = " while running with -T switch";
28         if (!unsafe)
29             croak(f, s, ug);
30         else if (dowarn)
31             warn(f, s, ug);
32     }
33 }
34
35 void
36 taint_env()
37 {
38     SV** svp;
39     MAGIC* mg;
40     char** e;
41     static char* misc_env[] = {
42         "IFS",          /* most shells' inter-field separators */
43         "CDPATH",       /* ksh dain bramage #1 */
44         "ENV",          /* ksh dain bramage #2 */
45         "BASH_ENV",     /* bash dain bramage -- I guess it's contagious */
46         NULL
47     };
48
49 #ifdef VMS
50     int i = 0;
51     char name[10 + TYPE_DIGITS(int)] = "DCL$PATH";
52
53     while (1) {
54         if (i)
55             (void)sprintf(name,"DCL$PATH;%d", i);
56         svp = hv_fetch(GvHVn(envgv), name, strlen(name), FALSE);
57         if (!svp || *svp == &sv_undef)
58             break;
59         if (SvTAINTED(*svp)) {
60             TAINT;
61             taint_proper("Insecure %s%s", "$ENV{DCL$PATH}");
62         }
63         if ((mg = mg_find(*svp, 'e')) && MgTAINTEDDIR(mg)) {
64             TAINT;
65             taint_proper("Insecure directory in %s%s", "$ENV{DCL$PATH}");
66         }
67         i++;
68     }
69 #endif /* VMS */
70
71     svp = hv_fetch(GvHVn(envgv),"PATH",4,FALSE);
72     if (svp && *svp) {
73         if (SvTAINTED(*svp)) {
74             dTHR;
75             TAINT;
76             taint_proper("Insecure %s%s", "$ENV{PATH}");
77         }
78         if ((mg = mg_find(*svp, 'e')) && MgTAINTEDDIR(mg)) {
79             dTHR;
80             TAINT;
81             taint_proper("Insecure directory in %s%s", "$ENV{PATH}");
82         }
83     }
84
85 #ifndef VMS
86     /* tainted $TERM is okay if it contains no metachars */
87     svp = hv_fetch(GvHVn(envgv),"TERM",4,FALSE);
88     if (svp && *svp && SvTAINTED(*svp)) {
89         dTHR;   /* just for taint */
90         bool was_tainted = tainted;
91         char *t = SvPV(*svp, na);
92         char *e = t + na;
93         tainted = was_tainted;
94         if (t < e && isALNUM(*t))
95             t++;
96         while (t < e && (isALNUM(*t) || *t == '-' || *t == ':'))
97             t++;
98         if (t < e) {
99             TAINT;
100             taint_proper("Insecure $ENV{%s}%s", "TERM");
101         }
102     }
103 #endif /* !VMS */
104
105     for (e = misc_env; *e; e++) {
106         svp = hv_fetch(GvHVn(envgv), *e, strlen(*e), FALSE);
107         if (svp && *svp != &sv_undef && SvTAINTED(*svp)) {
108             dTHR;       /* just for taint */
109             TAINT;
110             taint_proper("Insecure $ENV{%s}%s", *e);
111         }
112     }
113 }