clarify what a "line" is
[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{
aeea060c 13 dTHR; /* just for taint */
bbce6d69 14 char *ug;
15
fb73857a 16 DEBUG_u(PerlIO_printf(Perl_debug_log,
3280af22 17 "%s %d %d %d\n", s, PL_tainted, PL_uid, PL_euid));
1e422769 18
3280af22 19 if (PL_tainted) {
22c35a8c 20 if (!f)
21 f = PL_no_security;
3280af22 22 if (PL_euid != PL_uid)
bbce6d69 23 ug = " while running setuid";
3280af22 24 else if (PL_egid != PL_gid)
bbce6d69 25 ug = " while running setgid";
26 else
27 ug = " while running with -T switch";
3280af22 28 if (!PL_unsafe)
bbce6d69 29 croak(f, s, ug);
599cee73 30 else if (ckWARN(WARN_TAINT))
31 warner(WARN_TAINT, f, s, ug);
79072805 32 }
33}
34
35void
8ac85365 36taint_env(void)
79072805 37{
38 SV** svp;
7bac28a0 39 MAGIC* mg;
40 char** e;
41 static char* misc_env[] = {
42 "IFS", /* most shells' inter-field separators */
c90c0ff4 43 "CDPATH", /* ksh dain bramage #1 */
44 "ENV", /* ksh dain bramage #2 */
45 "BASH_ENV", /* bash dain bramage -- I guess it's contagious */
7bac28a0 46 NULL
47 };
1e422769 48
142393a6 49 if (!PL_envgv)
50 return;
51
1e422769 52#ifdef VMS
82f1aded 53 {
1e422769 54 int i = 0;
0dc443ab 55 char name[10 + TYPE_DIGITS(int)] = "DCL$PATH";
1e422769 56
57 while (1) {
58 if (i)
59 (void)sprintf(name,"DCL$PATH;%d", i);
6b88bc9c 60 svp = hv_fetch(GvHVn(PL_envgv), name, strlen(name), FALSE);
61 if (!svp || *svp == &PL_sv_undef)
1e422769 62 break;
63 if (SvTAINTED(*svp)) {
61bb5906 64 dTHR;
1e422769 65 TAINT;
66 taint_proper("Insecure %s%s", "$ENV{DCL$PATH}");
67 }
68 if ((mg = mg_find(*svp, 'e')) && MgTAINTEDDIR(mg)) {
61bb5906 69 dTHR;
1e422769 70 TAINT;
71 taint_proper("Insecure directory in %s%s", "$ENV{DCL$PATH}");
72 }
73 i++;
74 }
82f1aded 75 }
1e422769 76#endif /* VMS */
79072805 77
3280af22 78 svp = hv_fetch(GvHVn(PL_envgv),"PATH",4,FALSE);
1e422769 79 if (svp && *svp) {
80 if (SvTAINTED(*svp)) {
aeea060c 81 dTHR;
1e422769 82 TAINT;
bbce6d69 83 taint_proper("Insecure %s%s", "$ENV{PATH}");
1e422769 84 }
85 if ((mg = mg_find(*svp, 'e')) && MgTAINTEDDIR(mg)) {
aeea060c 86 dTHR;
1e422769 87 TAINT;
88 taint_proper("Insecure directory in %s%s", "$ENV{PATH}");
89 }
79072805 90 }
79072805 91
c90c0ff4 92#ifndef VMS
93 /* tainted $TERM is okay if it contains no metachars */
3280af22 94 svp = hv_fetch(GvHVn(PL_envgv),"TERM",4,FALSE);
c90c0ff4 95 if (svp && *svp && SvTAINTED(*svp)) {
aeea060c 96 dTHR; /* just for taint */
2d8e6c8d 97 STRLEN n_a;
3280af22 98 bool was_tainted = PL_tainted;
2d8e6c8d 99 char *t = SvPV(*svp, n_a);
100 char *e = t + n_a;
3280af22 101 PL_tainted = was_tainted;
c90c0ff4 102 if (t < e && isALNUM(*t))
103 t++;
104 while (t < e && (isALNUM(*t) || *t == '-' || *t == ':'))
105 t++;
106 if (t < e) {
107 TAINT;
108 taint_proper("Insecure $ENV{%s}%s", "TERM");
109 }
110 }
111#endif /* !VMS */
112
7bac28a0 113 for (e = misc_env; *e; e++) {
3280af22 114 svp = hv_fetch(GvHVn(PL_envgv), *e, strlen(*e), FALSE);
115 if (svp && *svp != &PL_sv_undef && SvTAINTED(*svp)) {
aeea060c 116 dTHR; /* just for taint */
7bac28a0 117 TAINT;
118 taint_proper("Insecure $ENV{%s}%s", *e);
119 }
bbce6d69 120 }
121}