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