1 char rcsid[] = "$Header: perly.c,v 3.0.1.2 89/11/17 15:34:42 lwall Locked $\nPatch level: ###\n";
3 * Copyright (c) 1989, Larry Wall
5 * You may distribute under the terms of the GNU General Public License
6 * as specified in the README file that comes with the perl 3.0 kit.
9 * Revision 3.0.1.2 89/11/17 15:34:42 lwall
10 * patch5: fixed possible confusion about current effective gid
12 * Revision 3.0.1.1 89/11/11 04:50:04 lwall
13 * patch2: moved yydebug to where its type didn't matter
15 * Revision 3.0 89/10/18 15:22:21 lwall
23 #include "patchlevel.h"
31 #ifdef SETUID_SCRIPTS_ARE_SECURE_NOW
44 char *index(), *strcpy(), *getenv();
45 bool dosearch = FALSE;
46 char **origargv = argv;
51 #ifdef SETUID_SCRIPTS_ARE_SECURE_NOW
54 fatal("suidperl is no longer needed since the kernel can now execute\n\
55 setuid perl scripts securely.\n");
60 euid = (int)geteuid();
62 egid = (int)getegid();
65 loop_ptr = 0; /* start label stack again */
68 (void)sprintf(index(rcsid,'#'), "%d\n", PATCHLEVEL);
69 linestr = Str_new(65,80);
70 str_nset(linestr,"",0);
71 str = str_make("",0); /* first used for -I flags */
72 curstash = defstash = hnew(0);
73 curstname = str_make("main",4);
74 stab_xhash(stabent("_main",TRUE)) = defstash;
75 incstab = aadd(stabent("INC",TRUE));
76 incstab->str_pok |= SP_MULTI;
77 for (argc--,argv++; argc; argc--,argv++) {
78 if (argv[0][0] != '-' || !argv[0][1])
82 validarg = " PHOOEY ";
95 if (euid != uid || egid != gid)
96 fatal("No -d allowed in setuid scripts");
104 if (euid != uid || egid != gid)
105 fatal("No -D allowed in setuid scripts");
109 warn("Recompile perl with -DDEBUGGING to use -D switch\n");
114 if (euid != uid || egid != gid)
115 fatal("No -e allowed in setuid scripts");
118 e_tmpname = savestr(TMPPATH);
119 (void)mktemp(e_tmpname);
120 e_fp = fopen(e_tmpname,"w");
124 (void)putc('\n', e_fp);
128 inplace = savestr(s+1);
129 argvoutstab = stabent("ARGVOUT",TRUE);
133 if (euid != uid || egid != gid)
134 fatal("No -I allowed in setuid scripts");
140 (void)apush(stab_array(incstab),str_make(s,0));
143 (void)apush(stab_array(incstab),str_make(argv[1],0));
144 str_cat(str,argv[1]);
159 if (euid != uid || egid != gid)
160 fatal("No -P allowed in setuid scripts");
167 if (euid != uid || egid != gid)
168 fatal("No -s allowed in setuid scripts");
187 fputs("\nCopyright (c) 1989, Larry Wall\n\n\
188 Perl may be copied only under the terms of the GNU General Public License,\n\
189 a copy of which can be found with the Perl 3.0 distribution kit.\n",stdout);
201 fatal("Unrecognized switch: -%s",s);
211 #define PRIVLIB "/usr/local/lib/perl"
213 (void)apush(stab_array(incstab),str_make(PRIVLIB,0));
216 str_set(&str_yes,Yes);
220 if (argv[0] == Nullch)
222 if (dosearch && !index(argv[0], '/') && (s = getenv("PATH"))) {
223 char *xfound = Nullch, *xfailed = Nullch;
226 bufend = s + strlen(s);
228 s = cpytill(tokenbuf,s,bufend,':',&len);
232 (void)strcat(tokenbuf+len,"/");
233 (void)strcat(tokenbuf+len,argv[0]);
236 fprintf(stderr,"Looking for %s\n",tokenbuf);
238 if (stat(tokenbuf,&statbuf) < 0) /* not there? */
240 if ((statbuf.st_mode & S_IFMT) == S_IFREG
241 && cando(S_IREAD,TRUE,&statbuf) && cando(S_IEXEC,TRUE,&statbuf)) {
242 xfound = tokenbuf; /* bingo! */
246 xfailed = savestr(tokenbuf);
249 fatal("Can't execute %s", xfailed ? xfailed : argv[0] );
252 argv[0] = savestr(xfound);
255 pidstatary = anew(Nullstab); /* for remembering popen pids, status */
257 filename = savestr(argv[0]);
258 origfilename = savestr(filename);
259 if (strEQ(filename,"-"))
263 str_cat(str,PRIVLIB);
264 (void)sprintf(buf, "\
265 /bin/sed -e '/^[^#]/b' \
266 -e '/^#[ ]*include[ ]/b' \
267 -e '/^#[ ]*define[ ]/b' \
268 -e '/^#[ ]*if[ ]/b' \
269 -e '/^#[ ]*ifdef[ ]/b' \
270 -e '/^#[ ]*ifndef[ ]/b' \
272 -e '/^#[ ]*endif/b' \
275 argv[0], CPPSTDIN, str_get(str), CPPMINUS);
276 #ifdef IAMSUID /* actually, this is caught earlier */
277 if (euid != uid && !euid) /* if running suidperl */
279 (void)seteuid(uid); /* musn't stay setuid root */
282 (void)setreuid(-1, uid);
288 rsfp = mypopen(buf,"r");
293 rsfp = fopen(argv[0],"r");
294 if (rsfp == Nullfp) {
295 extern char *sys_errlist[];
299 #ifndef IAMSUID /* in case script is not readable before setuid */
300 if (euid && stat(filename,&statbuf) >= 0 &&
301 statbuf.st_mode & (S_ISUID|S_ISGID)) {
302 (void)sprintf(buf, "%s/%s", BIN, "suidperl");
303 execv(buf, origargv); /* try again */
304 fatal("Can't do setuid\n");
308 fatal("Can't open perl script \"%s\": %s\n",
309 filename, sys_errlist[errno]);
311 str_free(str); /* free -I directories */
313 /* do we need to emulate setuid on scripts? */
315 /* This code is for those BSD systems that have setuid #! scripts disabled
316 * in the kernel because of a security problem. Merely defining DOSUID
317 * in perl will not fix that problem, but if you have disabled setuid
318 * scripts in the kernel, this will attempt to emulate setuid and setgid
319 * on scripts that have those now-otherwise-useless bits set. The setuid
320 * root version must be called suidperl. If regular perl discovers that
321 * it has opened a setuid script, it calls suidperl with the same argv
322 * that it had. If suidperl finds that the script it has just opened
323 * is NOT setuid root, it sets the effective uid back to the uid. We
324 * don't just make perl setuid root because that loses the effective
325 * uid we had before invoking perl, if it was different from the uid.
327 * DOSUID must be defined in both perl and suidperl, and IAMSUID must
328 * be defined in suidperl only. suidperl must be setuid root. The
329 * Configure script will set this up for you if you want it.
331 * There is also the possibility of have a script which is running
332 * set-id due to a C wrapper. We want to do the TAINT checks
333 * on these set-id scripts, but don't want to have the overhead of
334 * them in normal perl, and can't use suidperl because it will lose
335 * the effective uid info, so we have an additional non-setuid root
336 * version called taintperl that just does the TAINT checks.
340 if (fstat(fileno(rsfp),&statbuf) < 0) /* normal stat is insecure */
341 fatal("Can't stat script \"%s\"",filename);
342 if (statbuf.st_mode & (S_ISUID|S_ISGID)) {
347 /* On this access check to make sure the directories are readable,
348 * there is actually a small window that the user could use to make
349 * filename point to an accessible directory. So there is a faint
350 * chance that someone could execute a setuid script down in a
351 * non-accessible directory. I don't know what to do about that.
352 * But I don't think it's too important. The manual lies when
353 * it says access() is useful in setuid programs.
355 if (access(filename,1)) /* as a double check */
356 fatal("Permission denied");
358 /* If we can swap euid and uid, then we can determine access rights
359 * with a simple stat of the file, and then compare device and
360 * inode to make sure we did stat() on the same file we opened.
361 * Then we just have to make sure he or she can execute it.
364 struct stat tmpstatbuf;
366 if (setreuid(euid,uid) < 0 || getuid() != euid || geteuid() != uid)
367 fatal("Can't swap uid and euid"); /* really paranoid */
368 if (stat(filename,&tmpstatbuf) < 0) /* testing full pathname here */
369 fatal("Permission denied");
370 if (tmpstatbuf.st_dev != statbuf.st_dev ||
371 tmpstatbuf.st_ino != statbuf.st_ino) {
373 if (rsfp = mypopen("/bin/mail root","w")) { /* heh, heh */
375 "User %d tried to run dev %d ino %d in place of dev %d ino %d!\n\
376 (Filename of set-id script was %s, uid %d gid %d.)\n\nSincerely,\nperl\n",
377 uid,tmpstatbuf.st_dev, tmpstatbuf.st_ino,
378 statbuf.st_dev, statbuf.st_ino,
379 filename, statbuf.st_uid, statbuf.st_gid);
380 (void)mypclose(rsfp);
382 fatal("Permission denied\n");
384 if (setreuid(uid,euid) < 0 || getuid() != uid || geteuid() != euid)
385 fatal("Can't reswap uid and euid");
386 if (!cando(S_IEXEC,FALSE,&statbuf)) /* can real uid exec? */
387 fatal("Permission denied\n");
389 #endif /* SETREUID */
392 if ((statbuf.st_mode & S_IFMT) != S_IFREG)
393 fatal("Permission denied");
394 if ((statbuf.st_mode >> 6) & S_IWRITE)
395 fatal("Setuid/gid script is writable by world");
396 doswitches = FALSE; /* -s is insecure in suid */
398 if (fgets(tokenbuf,sizeof tokenbuf, rsfp) == Nullch ||
399 strnNE(tokenbuf,"#!",2) ) /* required even on Sys V */
401 for (s = tokenbuf+2; !isspace(*s); s++) ;
402 if (strnNE(s-4,"perl",4)) /* sanity check */
403 fatal("Not a perl script");
404 while (*s == ' ' || *s == '\t') s++;
406 * #! arg must be what we saw above. They can invoke it by
407 * mentioning suidperl explicitly, but they may not add any strange
408 * arguments beyond what #! says if they do invoke suidperl that way.
410 len = strlen(validarg);
411 if (strEQ(validarg," PHOOEY ") ||
412 strnNE(s,validarg,len) || !isspace(s[len]))
413 fatal("Args must match #! line");
416 if (euid != uid && (statbuf.st_mode & S_ISUID) &&
417 euid == statbuf.st_uid)
419 fatal("YOU HAVEN'T DISABLED SET-ID SCRIPTS IN THE KERNEL YET!\n\
420 FIX YOUR KERNEL, PUT A C WRAPPER AROUND THIS SCRIPT, OR USE -u AND UNDUMP!\n");
423 if (euid) { /* oops, we're not the setuid root perl */
426 (void)sprintf(buf, "%s/%s", BIN, "suidperl");
427 execv(buf, origargv); /* try again */
429 fatal("Can't do setuid\n");
432 if (statbuf.st_mode & S_ISGID && statbuf.st_gid != egid)
434 (void)setegid(statbuf.st_gid);
437 (void)setregid((GIDTYPE)-1,statbuf.st_gid);
439 setgid(statbuf.st_gid);
442 if (statbuf.st_mode & S_ISUID) {
443 if (statbuf.st_uid != euid)
445 (void)seteuid(statbuf.st_uid); /* all that for this */
448 (void)setreuid((UIDTYPE)-1,statbuf.st_uid);
450 setuid(statbuf.st_uid);
454 else if (uid) /* oops, mustn't run as root */
456 (void)seteuid((UIDTYPE)uid);
459 (void)setreuid((UIDTYPE)-1,(UIDTYPE)uid);
461 setuid((UIDTYPE)uid);
465 euid = (int)geteuid();
467 egid = (int)getegid();
468 if (!cando(S_IEXEC,TRUE,&statbuf))
469 fatal("Permission denied\n"); /* they can't do this */
473 fatal("-P not allowed for setuid/setgid script\n");
475 fatal("Script is not setuid/setgid in suidperl\n");
477 #ifndef TAINT /* we aren't taintperl or suidperl */
478 /* script has a wrapper--can't run suidperl or we lose euid */
479 else if (euid != uid || egid != gid) {
481 (void)sprintf(buf, "%s/%s", BIN, "taintperl");
482 execv(buf, origargv); /* try again */
483 fatal("Can't run setuid script with taint checks");
488 #ifndef TAINT /* we aren't taintperl or suidperl */
489 if (euid != uid || egid != gid) { /* (suidperl doesn't exist, in fact) */
490 #ifndef SETUID_SCRIPTS_ARE_SECURE_NOW
491 fstat(fileno(rsfp),&statbuf); /* may be either wrapped or real suid */
492 if ((euid != uid && euid == statbuf.st_uid && statbuf.st_mode & S_ISUID)
494 (egid != gid && egid == statbuf.st_gid && statbuf.st_mode & S_ISGID)
497 fatal("YOU HAVEN'T DISABLED SET-ID SCRIPTS IN THE KERNEL YET!\n\
498 FIX YOUR KERNEL, PUT A C WRAPPER AROUND THIS SCRIPT, OR USE -u AND UNDUMP!\n");
499 #endif /* SETUID_SCRIPTS_ARE_SECURE_NOW */
500 /* not set-id, must be wrapped */
502 (void)sprintf(buf, "%s/%s", BIN, "taintperl");
503 execv(buf, origargv); /* try again */
504 fatal("Can't run setuid script with taint checks");
509 defstab = stabent("_",TRUE);
513 stab_xhash(stabent("_DB",TRUE)) = debstash;
515 lineary = stab_xarray(aadd((tmpstab = stabent("line",TRUE))));
516 tmpstab->str_pok |= SP_MULTI;
517 subname = str_make("main",4);
518 DBstab = stabent("DB",TRUE);
519 DBstab->str_pok |= SP_MULTI;
520 DBsub = hadd(tmpstab = stabent("sub",TRUE));
521 tmpstab->str_pok |= SP_MULTI;
522 DBsingle = stab_val((tmpstab = stabent("single",TRUE)));
523 tmpstab->str_pok |= SP_MULTI;
529 bufend = bufptr = str_get(linestr);
531 savestack = anew(Nullstab); /* for saving non-local values */
532 stack = anew(Nullstab); /* for saving non-local values */
533 stack->ary_flags = 0; /* not a real array */
535 /* now parse the script */
538 if (yyparse() || error_count)
539 fatal("Execution aborted due to compilation errors.\n");
541 New(50,loop_stack,128,struct loop);
544 New(51,debname,128,char);
545 New(52,debdelim,128,char);
553 (void)UNLINK(e_tmpname);
556 /* initialize everything that won't change if we undump */
558 if (sigstab = stabent("SIG",allstabs)) {
559 sigstab->str_pok |= SP_MULTI;
563 magicalize("!#?^~=-%0123456789.+&*()<>,\\/[|`':");
565 amperstab = stabent("&",allstabs);
566 leftstab = stabent("`",allstabs);
567 rightstab = stabent("'",allstabs);
568 sawampersand = (amperstab || leftstab || rightstab);
569 if (tmpstab = stabent(":",allstabs))
570 str_set(STAB_STR(tmpstab),chopset);
572 /* these aren't necessarily magical */
573 if (tmpstab = stabent(";",allstabs))
574 str_set(STAB_STR(tmpstab),"\034");
578 if (tmpstab = stabent("0",allstabs))
579 str_set(STAB_STR(tmpstab),origfilename);
583 if (tmpstab = stabent("]",allstabs))
584 str_set(STAB_STR(tmpstab),rcsid);
585 str_nset(stab_val(stabent("\"", TRUE)), " ", 1);
587 stdinstab = stabent("STDIN",TRUE);
588 stdinstab->str_pok |= SP_MULTI;
589 stab_io(stdinstab) = stio_new();
590 stab_io(stdinstab)->ifp = stdin;
591 tmpstab = stabent("stdin",TRUE);
592 stab_io(tmpstab) = stab_io(stdinstab);
593 tmpstab->str_pok |= SP_MULTI;
595 tmpstab = stabent("STDOUT",TRUE);
596 tmpstab->str_pok |= SP_MULTI;
597 stab_io(tmpstab) = stio_new();
598 stab_io(tmpstab)->ofp = stab_io(tmpstab)->ifp = stdout;
599 defoutstab = tmpstab;
600 tmpstab = stabent("stdout",TRUE);
601 stab_io(tmpstab) = stab_io(defoutstab);
602 tmpstab->str_pok |= SP_MULTI;
604 curoutstab = stabent("STDERR",TRUE);
605 curoutstab->str_pok |= SP_MULTI;
606 stab_io(curoutstab) = stio_new();
607 stab_io(curoutstab)->ofp = stab_io(curoutstab)->ifp = stderr;
608 tmpstab = stabent("stderr",TRUE);
609 stab_io(tmpstab) = stab_io(curoutstab);
610 tmpstab->str_pok |= SP_MULTI;
611 curoutstab = defoutstab; /* switch back to STDOUT */
613 statname = Str_new(66,0); /* last filename we did stat on */
615 perldb = FALSE; /* don't try to instrument evals */
625 just_doit: /* come here if running an undumped a.out */
626 argc--,argv++; /* skip name of script */
628 for (; argc > 0 && **argv == '-'; argc--,argv++) {
629 if (argv[0][1] == '-') {
633 str_numset(stab_val(stabent(argv[0]+1,TRUE)),(double)1.0);
639 if (argvstab = stabent("ARGV",allstabs)) {
640 argvstab->str_pok |= SP_MULTI;
641 (void)aadd(argvstab);
642 for (; argc > 0; argc--,argv++) {
643 (void)apush(stab_array(argvstab),str_make(argv[0],0));
647 (void) stabent("ENV",TRUE); /* must test PATH and IFS */
649 if (envstab = stabent("ENV",allstabs)) {
650 envstab->str_pok |= SP_MULTI;
652 for (; *env; env++) {
653 if (!(s = index(*env,'=')))
656 str = str_make(s--,0);
657 str_magic(str, envstab, 'E', *env, s - *env);
658 (void)hstore(stab_hash(envstab), *env, s - *env, str, 0);
665 if (tmpstab = stabent("$",allstabs))
666 str_numset(STAB_STR(tmpstab),(double)getpid());
668 if (setjmp(top_env)) /* sets goto_targ on longjump */
669 loop_ptr = 0; /* start label stack again */
675 fprintf(stderr,"\nEXECUTING...\n\n");
680 (void) cmd_exec(main_root,G_SCALAR,-1);
683 fatal("Can't find label \"%s\"--aborting",goto_targ);
695 while (*sym = *list++) {
696 if (stab = stabent(sym,allstabs)) {
697 stab_flags(stab) = SF_VMAGIC;
698 str_magic(stab_val(stab), stab, 0, Nullch, 0);
703 /* this routine is in perly.c by virtue of being sort of an alternate main() */
706 do_eval(str,optype,stash,gimme,arglast)
713 STR **st = stack->ary_array;
718 char *oldfile = filename;
719 line_t oldline = line;
720 int oldtmps_base = tmps_base;
721 int oldsave = savestack->ary_fill;
722 SPAT *oldspat = curspat;
723 static char *last_eval = Nullch;
724 static CMD *last_root = Nullcmd;
727 tmps_base = tmps_max;
728 if (curstash != stash) {
729 (void)savehptr(&curstash);
732 str_set(stab_val(stabent("@",TRUE)),"");
733 if (optype != O_DOFILE) { /* normal eval */
736 str_sset(linestr,str);
737 str_cat(linestr,";"); /* be kind to them */
745 filename = savestr(str_get(str)); /* can't free this easily */
747 rsfp = fopen(filename,"r");
748 ar = stab_array(incstab);
749 if (!rsfp && *filename != '/') {
750 for (i = 0; i <= ar->ary_fill; i++) {
751 (void)sprintf(buf,"%s/%s",str_get(afetch(ar,i,TRUE)),filename);
752 rsfp = fopen(buf,"r");
754 filename = savestr(buf);
761 tmps_base = oldtmps_base;
762 if (gimme != G_ARRAY)
763 st[++sp] = &str_undef;
769 oldoldbufptr = oldbufptr = bufptr = str_get(linestr);
770 bufend = bufptr + linestr->str_cur;
771 if (setjmp(eval_env)) {
779 else if (last_root && *bufptr == *last_eval && strEQ(bufptr,last_eval)){
781 eval_root = last_root; /* no point in reparsing */
783 else if (in_eval == 1) {
788 last_eval = savestr(bufptr);
792 last_root = eval_root;
797 myroot = eval_root; /* in case cmd_exec does another eval! */
798 if (retval || error_count) {
800 last_root = Nullcmd; /* can't free on error, for some reason */
807 sp = cmd_exec(eval_root,gimme,sp);
808 st = stack->ary_array;
809 for (i = arglast[0] + 1; i <= sp; i++)
810 st[i] = str_static(st[i]);
811 /* if we don't save result, free zaps it */
812 if (in_eval != 1 && myroot != last_root)
818 tmps_base = oldtmps_base;
820 if (savestack->ary_fill > oldsave) /* let them use local() */
821 restorelist(oldsave);