Integrate mainline @ 18:15 CST 31 Oct 1997
[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(const char *f, char *s)
12 {
13     dTHR;       /* just for taint */
14     char *ug;
15
16     DEBUG_u(PerlIO_printf(Perl_debug_log,
17             "%s %d %d %d\n", s, tainted, uid, euid));
18
19     if (tainted) {
20         if (euid != uid)
21             ug = " while running setuid";
22         else if (egid != gid)
23             ug = " while running setgid";
24         else
25             ug = " while running with -T switch";
26         if (!unsafe)
27             croak(f, s, ug);
28         else if (dowarn)
29             warn(f, s, ug);
30     }
31 }
32
33 void
34 taint_env(void)
35 {
36     SV** svp;
37     MAGIC* mg;
38     char** e;
39     static char* misc_env[] = {
40         "IFS",          /* most shells' inter-field separators */
41         "CDPATH",       /* ksh dain bramage #1 */
42         "ENV",          /* ksh dain bramage #2 */
43         "BASH_ENV",     /* bash dain bramage -- I guess it's contagious */
44         NULL
45     };
46
47 #ifdef VMS
48     int i = 0;
49     char name[10 + TYPE_DIGITS(int)] = "DCL$PATH";
50
51     while (1) {
52         if (i)
53             (void)sprintf(name,"DCL$PATH;%d", i);
54         svp = hv_fetch(GvHVn(envgv), name, strlen(name), FALSE);
55         if (!svp || *svp == &sv_undef)
56             break;
57         if (SvTAINTED(*svp)) {
58             TAINT;
59             taint_proper("Insecure %s%s", "$ENV{DCL$PATH}");
60         }
61         if ((mg = mg_find(*svp, 'e')) && MgTAINTEDDIR(mg)) {
62             TAINT;
63             taint_proper("Insecure directory in %s%s", "$ENV{DCL$PATH}");
64         }
65         i++;
66     }
67 #endif /* VMS */
68
69     svp = hv_fetch(GvHVn(envgv),"PATH",4,FALSE);
70     if (svp && *svp) {
71         if (SvTAINTED(*svp)) {
72             dTHR;
73             TAINT;
74             taint_proper("Insecure %s%s", "$ENV{PATH}");
75         }
76         if ((mg = mg_find(*svp, 'e')) && MgTAINTEDDIR(mg)) {
77             dTHR;
78             TAINT;
79             taint_proper("Insecure directory in %s%s", "$ENV{PATH}");
80         }
81     }
82
83 #ifndef VMS
84     /* tainted $TERM is okay if it contains no metachars */
85     svp = hv_fetch(GvHVn(envgv),"TERM",4,FALSE);
86     if (svp && *svp && SvTAINTED(*svp)) {
87         dTHR;   /* just for taint */
88         bool was_tainted = tainted;
89         char *t = SvPV(*svp, na);
90         char *e = t + na;
91         tainted = was_tainted;
92         if (t < e && isALNUM(*t))
93             t++;
94         while (t < e && (isALNUM(*t) || *t == '-' || *t == ':'))
95             t++;
96         if (t < e) {
97             TAINT;
98             taint_proper("Insecure $ENV{%s}%s", "TERM");
99         }
100     }
101 #endif /* !VMS */
102
103     for (e = misc_env; *e; e++) {
104         svp = hv_fetch(GvHVn(envgv), *e, strlen(*e), FALSE);
105         if (svp && *svp != &sv_undef && SvTAINTED(*svp)) {
106             dTHR;       /* just for taint */
107             TAINT;
108             taint_proper("Insecure $ENV{%s}%s", *e);
109         }
110     }
111 }