This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
perl 5.002_01: pp_ctl.c
[perl5.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_not(s)
12 char *s;
13 {
14     if (euid != uid)
15         croak("No %s allowed while running setuid", s);
16     if (egid != gid)
17         croak("No %s allowed while running setgid", s);
18 }
19
20 void
21 taint_proper(f, s)
22 char *f;
23 char *s;
24 {
25     if (tainting) {
26         DEBUG_u(fprintf(stderr,"%s %d %d %d\n",s,tainted,uid, euid));
27         if (tainted) {
28             char *ug = 0;
29             if (euid != uid)
30                 ug = " while running setuid";
31             else if (egid != gid)
32                 ug = " while running setgid";
33             else if (tainting)
34                 ug = " while running with -T switch";
35             if (ug) {
36                 if (!unsafe)
37                     croak(f, s, ug);
38                 else if (dowarn)
39                     warn(f, s, ug);
40             }
41         }
42     }
43 }
44
45 void
46 taint_env()
47 {
48     SV** svp;
49
50     if (tainting) {
51         MAGIC *mg = 0;
52         svp = hv_fetch(GvHVn(envgv),"PATH",4,FALSE);
53         if (!svp || *svp == &sv_undef ||
54           ((mg = mg_find(*svp, 't')) && mg->mg_len & 1))
55         {
56             tainted = TRUE;
57             if (mg && MgTAINTEDDIR(mg))
58                 taint_proper("Insecure directory in %s%s", "$ENV{PATH}");
59             else
60                 taint_proper("Insecure %s%s", "$ENV{PATH}");
61         }
62         svp = hv_fetch(GvHVn(envgv),"IFS",3,FALSE);
63         if (svp && *svp != &sv_undef &&
64           (mg = mg_find(*svp, 't')) && mg->mg_len & 1)
65         {
66             tainted = TRUE;
67             taint_proper("Insecure %s%s", "$ENV{IFS}");
68         }
69     }
70 }
71