1 char rcsid[] = "$Header: perly.c,v 3.0.1.3 89/12/21 20:15:41 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.3 89/12/21 20:15:41 lwall
10 * patch7: ANSI strerror() is now supported
11 * patch7: errno may now be a macro with an lvalue
12 * patch7: allowed setuid scripts to have a space after #!
14 * Revision 3.0.1.2 89/11/17 15:34:42 lwall
15 * patch5: fixed possible confusion about current effective gid
17 * Revision 3.0.1.1 89/11/11 04:50:04 lwall
18 * patch2: moved yydebug to where its type didn't matter
20 * Revision 3.0 89/10/18 15:22:21 lwall
28 #include "patchlevel.h"
36 #ifdef SETUID_SCRIPTS_ARE_SECURE_NOW
49 char *index(), *strcpy(), *getenv();
50 bool dosearch = FALSE;
51 char **origargv = argv;
56 #ifdef SETUID_SCRIPTS_ARE_SECURE_NOW
59 fatal("suidperl is no longer needed since the kernel can now execute\n\
60 setuid perl scripts securely.\n");
65 euid = (int)geteuid();
67 egid = (int)getegid();
70 loop_ptr = 0; /* start label stack again */
73 (void)sprintf(index(rcsid,'#'), "%d\n", PATCHLEVEL);
74 linestr = Str_new(65,80);
75 str_nset(linestr,"",0);
76 str = str_make("",0); /* first used for -I flags */
77 curstash = defstash = hnew(0);
78 curstname = str_make("main",4);
79 stab_xhash(stabent("_main",TRUE)) = defstash;
80 incstab = aadd(stabent("INC",TRUE));
81 incstab->str_pok |= SP_MULTI;
82 for (argc--,argv++; argc; argc--,argv++) {
83 if (argv[0][0] != '-' || !argv[0][1])
87 validarg = " PHOOEY ";
100 if (euid != uid || egid != gid)
101 fatal("No -d allowed in setuid scripts");
109 if (euid != uid || egid != gid)
110 fatal("No -D allowed in setuid scripts");
114 warn("Recompile perl with -DDEBUGGING to use -D switch\n");
119 if (euid != uid || egid != gid)
120 fatal("No -e allowed in setuid scripts");
123 e_tmpname = savestr(TMPPATH);
124 (void)mktemp(e_tmpname);
125 e_fp = fopen(e_tmpname,"w");
129 (void)putc('\n', e_fp);
133 inplace = savestr(s+1);
134 argvoutstab = stabent("ARGVOUT",TRUE);
138 if (euid != uid || egid != gid)
139 fatal("No -I allowed in setuid scripts");
145 (void)apush(stab_array(incstab),str_make(s,0));
148 (void)apush(stab_array(incstab),str_make(argv[1],0));
149 str_cat(str,argv[1]);
164 if (euid != uid || egid != gid)
165 fatal("No -P allowed in setuid scripts");
172 if (euid != uid || egid != gid)
173 fatal("No -s allowed in setuid scripts");
192 fputs("\nCopyright (c) 1989, Larry Wall\n\n\
193 Perl may be copied only under the terms of the GNU General Public License,\n\
194 a copy of which can be found with the Perl 3.0 distribution kit.\n",stdout);
206 fatal("Unrecognized switch: -%s",s);
216 #define PRIVLIB "/usr/local/lib/perl"
218 (void)apush(stab_array(incstab),str_make(PRIVLIB,0));
221 str_set(&str_yes,Yes);
225 if (argv[0] == Nullch)
227 if (dosearch && !index(argv[0], '/') && (s = getenv("PATH"))) {
228 char *xfound = Nullch, *xfailed = Nullch;
231 bufend = s + strlen(s);
233 s = cpytill(tokenbuf,s,bufend,':',&len);
237 (void)strcat(tokenbuf+len,"/");
238 (void)strcat(tokenbuf+len,argv[0]);
241 fprintf(stderr,"Looking for %s\n",tokenbuf);
243 if (stat(tokenbuf,&statbuf) < 0) /* not there? */
245 if ((statbuf.st_mode & S_IFMT) == S_IFREG
246 && cando(S_IREAD,TRUE,&statbuf) && cando(S_IEXEC,TRUE,&statbuf)) {
247 xfound = tokenbuf; /* bingo! */
251 xfailed = savestr(tokenbuf);
254 fatal("Can't execute %s", xfailed ? xfailed : argv[0] );
257 argv[0] = savestr(xfound);
260 pidstatary = anew(Nullstab); /* for remembering popen pids, status */
262 filename = savestr(argv[0]);
263 origfilename = savestr(filename);
264 if (strEQ(filename,"-"))
268 str_cat(str,PRIVLIB);
269 (void)sprintf(buf, "\
270 /bin/sed -e '/^[^#]/b' \
271 -e '/^#[ ]*include[ ]/b' \
272 -e '/^#[ ]*define[ ]/b' \
273 -e '/^#[ ]*if[ ]/b' \
274 -e '/^#[ ]*ifdef[ ]/b' \
275 -e '/^#[ ]*ifndef[ ]/b' \
277 -e '/^#[ ]*endif/b' \
280 argv[0], CPPSTDIN, str_get(str), CPPMINUS);
281 #ifdef IAMSUID /* actually, this is caught earlier */
282 if (euid != uid && !euid) /* if running suidperl */
284 (void)seteuid(uid); /* musn't stay setuid root */
287 (void)setreuid(-1, uid);
293 rsfp = mypopen(buf,"r");
298 rsfp = fopen(argv[0],"r");
299 if (rsfp == Nullfp) {
301 #ifndef IAMSUID /* in case script is not readable before setuid */
302 if (euid && stat(filename,&statbuf) >= 0 &&
303 statbuf.st_mode & (S_ISUID|S_ISGID)) {
304 (void)sprintf(buf, "%s/%s", BIN, "suidperl");
305 execv(buf, origargv); /* try again */
306 fatal("Can't do setuid\n");
310 fatal("Can't open perl script \"%s\": %s\n",
311 filename, strerror(errno));
313 str_free(str); /* free -I directories */
315 /* do we need to emulate setuid on scripts? */
317 /* This code is for those BSD systems that have setuid #! scripts disabled
318 * in the kernel because of a security problem. Merely defining DOSUID
319 * in perl will not fix that problem, but if you have disabled setuid
320 * scripts in the kernel, this will attempt to emulate setuid and setgid
321 * on scripts that have those now-otherwise-useless bits set. The setuid
322 * root version must be called suidperl. If regular perl discovers that
323 * it has opened a setuid script, it calls suidperl with the same argv
324 * that it had. If suidperl finds that the script it has just opened
325 * is NOT setuid root, it sets the effective uid back to the uid. We
326 * don't just make perl setuid root because that loses the effective
327 * uid we had before invoking perl, if it was different from the uid.
329 * DOSUID must be defined in both perl and suidperl, and IAMSUID must
330 * be defined in suidperl only. suidperl must be setuid root. The
331 * Configure script will set this up for you if you want it.
333 * There is also the possibility of have a script which is running
334 * set-id due to a C wrapper. We want to do the TAINT checks
335 * on these set-id scripts, but don't want to have the overhead of
336 * them in normal perl, and can't use suidperl because it will lose
337 * the effective uid info, so we have an additional non-setuid root
338 * version called taintperl that just does the TAINT checks.
342 if (fstat(fileno(rsfp),&statbuf) < 0) /* normal stat is insecure */
343 fatal("Can't stat script \"%s\"",filename);
344 if (statbuf.st_mode & (S_ISUID|S_ISGID)) {
349 /* On this access check to make sure the directories are readable,
350 * there is actually a small window that the user could use to make
351 * filename point to an accessible directory. So there is a faint
352 * chance that someone could execute a setuid script down in a
353 * non-accessible directory. I don't know what to do about that.
354 * But I don't think it's too important. The manual lies when
355 * it says access() is useful in setuid programs.
357 if (access(filename,1)) /* as a double check */
358 fatal("Permission denied");
360 /* If we can swap euid and uid, then we can determine access rights
361 * with a simple stat of the file, and then compare device and
362 * inode to make sure we did stat() on the same file we opened.
363 * Then we just have to make sure he or she can execute it.
366 struct stat tmpstatbuf;
368 if (setreuid(euid,uid) < 0 || getuid() != euid || geteuid() != uid)
369 fatal("Can't swap uid and euid"); /* really paranoid */
370 if (stat(filename,&tmpstatbuf) < 0) /* testing full pathname here */
371 fatal("Permission denied");
372 if (tmpstatbuf.st_dev != statbuf.st_dev ||
373 tmpstatbuf.st_ino != statbuf.st_ino) {
375 if (rsfp = mypopen("/bin/mail root","w")) { /* heh, heh */
377 "User %d tried to run dev %d ino %d in place of dev %d ino %d!\n\
378 (Filename of set-id script was %s, uid %d gid %d.)\n\nSincerely,\nperl\n",
379 uid,tmpstatbuf.st_dev, tmpstatbuf.st_ino,
380 statbuf.st_dev, statbuf.st_ino,
381 filename, statbuf.st_uid, statbuf.st_gid);
382 (void)mypclose(rsfp);
384 fatal("Permission denied\n");
386 if (setreuid(uid,euid) < 0 || getuid() != uid || geteuid() != euid)
387 fatal("Can't reswap uid and euid");
388 if (!cando(S_IEXEC,FALSE,&statbuf)) /* can real uid exec? */
389 fatal("Permission denied\n");
391 #endif /* SETREUID */
394 if ((statbuf.st_mode & S_IFMT) != S_IFREG)
395 fatal("Permission denied");
396 if ((statbuf.st_mode >> 6) & S_IWRITE)
397 fatal("Setuid/gid script is writable by world");
398 doswitches = FALSE; /* -s is insecure in suid */
400 if (fgets(tokenbuf,sizeof tokenbuf, rsfp) == Nullch ||
401 strnNE(tokenbuf,"#!",2) ) /* required even on Sys V */
405 while (!isspace(*s)) s++;
406 if (strnNE(s-4,"perl",4)) /* sanity check */
407 fatal("Not a perl script");
408 while (*s == ' ' || *s == '\t') s++;
410 * #! arg must be what we saw above. They can invoke it by
411 * mentioning suidperl explicitly, but they may not add any strange
412 * arguments beyond what #! says if they do invoke suidperl that way.
414 len = strlen(validarg);
415 if (strEQ(validarg," PHOOEY ") ||
416 strnNE(s,validarg,len) || !isspace(s[len]))
417 fatal("Args must match #! line");
420 if (euid != uid && (statbuf.st_mode & S_ISUID) &&
421 euid == statbuf.st_uid)
423 fatal("YOU HAVEN'T DISABLED SET-ID SCRIPTS IN THE KERNEL YET!\n\
424 FIX YOUR KERNEL, PUT A C WRAPPER AROUND THIS SCRIPT, OR USE -u AND UNDUMP!\n");
427 if (euid) { /* oops, we're not the setuid root perl */
430 (void)sprintf(buf, "%s/%s", BIN, "suidperl");
431 execv(buf, origargv); /* try again */
433 fatal("Can't do setuid\n");
436 if (statbuf.st_mode & S_ISGID && statbuf.st_gid != egid)
438 (void)setegid(statbuf.st_gid);
441 (void)setregid((GIDTYPE)-1,statbuf.st_gid);
443 setgid(statbuf.st_gid);
446 if (statbuf.st_mode & S_ISUID) {
447 if (statbuf.st_uid != euid)
449 (void)seteuid(statbuf.st_uid); /* all that for this */
452 (void)setreuid((UIDTYPE)-1,statbuf.st_uid);
454 setuid(statbuf.st_uid);
458 else if (uid) /* oops, mustn't run as root */
460 (void)seteuid((UIDTYPE)uid);
463 (void)setreuid((UIDTYPE)-1,(UIDTYPE)uid);
465 setuid((UIDTYPE)uid);
469 euid = (int)geteuid();
471 egid = (int)getegid();
472 if (!cando(S_IEXEC,TRUE,&statbuf))
473 fatal("Permission denied\n"); /* they can't do this */
477 fatal("-P not allowed for setuid/setgid script\n");
479 fatal("Script is not setuid/setgid in suidperl\n");
481 #ifndef TAINT /* we aren't taintperl or suidperl */
482 /* script has a wrapper--can't run suidperl or we lose euid */
483 else if (euid != uid || egid != gid) {
485 (void)sprintf(buf, "%s/%s", BIN, "taintperl");
486 execv(buf, origargv); /* try again */
487 fatal("Can't run setuid script with taint checks");
492 #ifndef TAINT /* we aren't taintperl or suidperl */
493 if (euid != uid || egid != gid) { /* (suidperl doesn't exist, in fact) */
494 #ifndef SETUID_SCRIPTS_ARE_SECURE_NOW
495 fstat(fileno(rsfp),&statbuf); /* may be either wrapped or real suid */
496 if ((euid != uid && euid == statbuf.st_uid && statbuf.st_mode & S_ISUID)
498 (egid != gid && egid == statbuf.st_gid && statbuf.st_mode & S_ISGID)
501 fatal("YOU HAVEN'T DISABLED SET-ID SCRIPTS IN THE KERNEL YET!\n\
502 FIX YOUR KERNEL, PUT A C WRAPPER AROUND THIS SCRIPT, OR USE -u AND UNDUMP!\n");
503 #endif /* SETUID_SCRIPTS_ARE_SECURE_NOW */
504 /* not set-id, must be wrapped */
506 (void)sprintf(buf, "%s/%s", BIN, "taintperl");
507 execv(buf, origargv); /* try again */
508 fatal("Can't run setuid script with taint checks");
513 defstab = stabent("_",TRUE);
517 stab_xhash(stabent("_DB",TRUE)) = debstash;
519 lineary = stab_xarray(aadd((tmpstab = stabent("line",TRUE))));
520 tmpstab->str_pok |= SP_MULTI;
521 subname = str_make("main",4);
522 DBstab = stabent("DB",TRUE);
523 DBstab->str_pok |= SP_MULTI;
524 DBsub = hadd(tmpstab = stabent("sub",TRUE));
525 tmpstab->str_pok |= SP_MULTI;
526 DBsingle = stab_val((tmpstab = stabent("single",TRUE)));
527 tmpstab->str_pok |= SP_MULTI;
533 bufend = bufptr = str_get(linestr);
535 savestack = anew(Nullstab); /* for saving non-local values */
536 stack = anew(Nullstab); /* for saving non-local values */
537 stack->ary_flags = 0; /* not a real array */
539 /* now parse the script */
542 if (yyparse() || error_count)
543 fatal("Execution aborted due to compilation errors.\n");
545 New(50,loop_stack,128,struct loop);
548 New(51,debname,128,char);
549 New(52,debdelim,128,char);
557 (void)UNLINK(e_tmpname);
560 /* initialize everything that won't change if we undump */
562 if (sigstab = stabent("SIG",allstabs)) {
563 sigstab->str_pok |= SP_MULTI;
567 magicalize("!#?^~=-%0123456789.+&*()<>,\\/[|`':");
569 amperstab = stabent("&",allstabs);
570 leftstab = stabent("`",allstabs);
571 rightstab = stabent("'",allstabs);
572 sawampersand = (amperstab || leftstab || rightstab);
573 if (tmpstab = stabent(":",allstabs))
574 str_set(STAB_STR(tmpstab),chopset);
576 /* these aren't necessarily magical */
577 if (tmpstab = stabent(";",allstabs))
578 str_set(STAB_STR(tmpstab),"\034");
582 if (tmpstab = stabent("0",allstabs))
583 str_set(STAB_STR(tmpstab),origfilename);
587 if (tmpstab = stabent("]",allstabs))
588 str_set(STAB_STR(tmpstab),rcsid);
589 str_nset(stab_val(stabent("\"", TRUE)), " ", 1);
591 stdinstab = stabent("STDIN",TRUE);
592 stdinstab->str_pok |= SP_MULTI;
593 stab_io(stdinstab) = stio_new();
594 stab_io(stdinstab)->ifp = stdin;
595 tmpstab = stabent("stdin",TRUE);
596 stab_io(tmpstab) = stab_io(stdinstab);
597 tmpstab->str_pok |= SP_MULTI;
599 tmpstab = stabent("STDOUT",TRUE);
600 tmpstab->str_pok |= SP_MULTI;
601 stab_io(tmpstab) = stio_new();
602 stab_io(tmpstab)->ofp = stab_io(tmpstab)->ifp = stdout;
603 defoutstab = tmpstab;
604 tmpstab = stabent("stdout",TRUE);
605 stab_io(tmpstab) = stab_io(defoutstab);
606 tmpstab->str_pok |= SP_MULTI;
608 curoutstab = stabent("STDERR",TRUE);
609 curoutstab->str_pok |= SP_MULTI;
610 stab_io(curoutstab) = stio_new();
611 stab_io(curoutstab)->ofp = stab_io(curoutstab)->ifp = stderr;
612 tmpstab = stabent("stderr",TRUE);
613 stab_io(tmpstab) = stab_io(curoutstab);
614 tmpstab->str_pok |= SP_MULTI;
615 curoutstab = defoutstab; /* switch back to STDOUT */
617 statname = Str_new(66,0); /* last filename we did stat on */
619 perldb = FALSE; /* don't try to instrument evals */
629 just_doit: /* come here if running an undumped a.out */
630 argc--,argv++; /* skip name of script */
632 for (; argc > 0 && **argv == '-'; argc--,argv++) {
633 if (argv[0][1] == '-') {
637 str_numset(stab_val(stabent(argv[0]+1,TRUE)),(double)1.0);
643 if (argvstab = stabent("ARGV",allstabs)) {
644 argvstab->str_pok |= SP_MULTI;
645 (void)aadd(argvstab);
646 for (; argc > 0; argc--,argv++) {
647 (void)apush(stab_array(argvstab),str_make(argv[0],0));
651 (void) stabent("ENV",TRUE); /* must test PATH and IFS */
653 if (envstab = stabent("ENV",allstabs)) {
654 envstab->str_pok |= SP_MULTI;
656 for (; *env; env++) {
657 if (!(s = index(*env,'=')))
660 str = str_make(s--,0);
661 str_magic(str, envstab, 'E', *env, s - *env);
662 (void)hstore(stab_hash(envstab), *env, s - *env, str, 0);
669 if (tmpstab = stabent("$",allstabs))
670 str_numset(STAB_STR(tmpstab),(double)getpid());
672 if (setjmp(top_env)) /* sets goto_targ on longjump */
673 loop_ptr = 0; /* start label stack again */
679 fprintf(stderr,"\nEXECUTING...\n\n");
684 (void) cmd_exec(main_root,G_SCALAR,-1);
687 fatal("Can't find label \"%s\"--aborting",goto_targ);
699 while (*sym = *list++) {
700 if (stab = stabent(sym,allstabs)) {
701 stab_flags(stab) = SF_VMAGIC;
702 str_magic(stab_val(stab), stab, 0, Nullch, 0);
707 /* this routine is in perly.c by virtue of being sort of an alternate main() */
710 do_eval(str,optype,stash,gimme,arglast)
717 STR **st = stack->ary_array;
722 char *oldfile = filename;
723 line_t oldline = line;
724 int oldtmps_base = tmps_base;
725 int oldsave = savestack->ary_fill;
726 SPAT *oldspat = curspat;
727 static char *last_eval = Nullch;
728 static CMD *last_root = Nullcmd;
729 VOLATILE int sp = arglast[0];
731 tmps_base = tmps_max;
732 if (curstash != stash) {
733 (void)savehptr(&curstash);
736 str_set(stab_val(stabent("@",TRUE)),"");
737 if (optype != O_DOFILE) { /* normal eval */
740 str_sset(linestr,str);
741 str_cat(linestr,";"); /* be kind to them */
749 filename = savestr(str_get(str)); /* can't free this easily */
751 rsfp = fopen(filename,"r");
752 ar = stab_array(incstab);
753 if (!rsfp && *filename != '/') {
754 for (i = 0; i <= ar->ary_fill; i++) {
755 (void)sprintf(buf,"%s/%s",str_get(afetch(ar,i,TRUE)),filename);
756 rsfp = fopen(buf,"r");
758 filename = savestr(buf);
765 tmps_base = oldtmps_base;
766 if (gimme != G_ARRAY)
767 st[++sp] = &str_undef;
773 oldoldbufptr = oldbufptr = bufptr = str_get(linestr);
774 bufend = bufptr + linestr->str_cur;
775 if (setjmp(eval_env)) {
783 else if (last_root && *bufptr == *last_eval && strEQ(bufptr,last_eval)){
785 eval_root = last_root; /* no point in reparsing */
787 else if (in_eval == 1) {
792 last_eval = savestr(bufptr);
796 last_root = eval_root;
801 myroot = eval_root; /* in case cmd_exec does another eval! */
802 if (retval || error_count) {
804 last_root = Nullcmd; /* can't free on error, for some reason */
811 sp = cmd_exec(eval_root,gimme,sp);
812 st = stack->ary_array;
813 for (i = arglast[0] + 1; i <= sp; i++)
814 st[i] = str_static(st[i]);
815 /* if we don't save result, free zaps it */
816 if (in_eval != 1 && myroot != last_root)
822 tmps_base = oldtmps_base;
824 if (savestack->ary_fill > oldsave) /* let them use local() */
825 restorelist(oldsave);