1 char rcsid[] = "$Header: perly.c,v 3.0 89/10/18 15:22:21 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 89/10/18 15:22:21 lwall
17 #include "patchlevel.h"
25 #ifdef SETUID_SCRIPTS_ARE_SECURE_NOW
38 char *index(), *strcpy(), *getenv();
39 bool dosearch = FALSE;
40 char **origargv = argv;
45 #ifdef SETUID_SCRIPTS_ARE_SECURE_NOW
48 fatal("suidperl is no longer needed since the kernel can now execute\n\
49 setuid perl scripts securely.\n");
54 euid = (int)geteuid();
56 egid = (int)getegid();
59 loop_ptr = 0; /* start label stack again */
62 (void)sprintf(index(rcsid,'#'), "%d\n", PATCHLEVEL);
63 linestr = Str_new(65,80);
64 str_nset(linestr,"",0);
65 str = str_make("",0); /* first used for -I flags */
66 curstash = defstash = hnew(0);
67 curstname = str_make("main",4);
68 stab_xhash(stabent("_main",TRUE)) = defstash;
69 incstab = aadd(stabent("INC",TRUE));
70 incstab->str_pok |= SP_MULTI;
71 for (argc--,argv++; argc; argc--,argv++) {
72 if (argv[0][0] != '-' || !argv[0][1])
76 validarg = " PHOOEY ";
89 if (euid != uid || egid != gid)
90 fatal("No -d allowed in setuid scripts");
98 if (euid != uid || egid != gid)
99 fatal("No -D allowed in setuid scripts");
103 yydebug = (debug & 1);
109 if (euid != uid || egid != gid)
110 fatal("No -e allowed in setuid scripts");
113 e_tmpname = savestr(TMPPATH);
114 (void)mktemp(e_tmpname);
115 e_fp = fopen(e_tmpname,"w");
119 (void)putc('\n', e_fp);
123 inplace = savestr(s+1);
124 argvoutstab = stabent("ARGVOUT",TRUE);
128 if (euid != uid || egid != gid)
129 fatal("No -I allowed in setuid scripts");
135 (void)apush(stab_array(incstab),str_make(s,0));
138 (void)apush(stab_array(incstab),str_make(argv[1],0));
139 str_cat(str,argv[1]);
154 if (euid != uid || egid != gid)
155 fatal("No -P allowed in setuid scripts");
162 if (euid != uid || egid != gid)
163 fatal("No -s allowed in setuid scripts");
182 fputs("\nCopyright (c) 1989, Larry Wall\n\n\
183 Perl may be copied only under the terms of the GNU General Public License,\n\
184 a copy of which can be found with the Perl 3.0 distribution kit.\n",stdout);
196 fatal("Unrecognized switch: -%s",s);
206 #define PRIVLIB "/usr/local/lib/perl"
208 (void)apush(stab_array(incstab),str_make(PRIVLIB,0));
211 str_set(&str_yes,Yes);
215 if (argv[0] == Nullch)
217 if (dosearch && !index(argv[0], '/') && (s = getenv("PATH"))) {
218 char *xfound = Nullch, *xfailed = Nullch;
221 bufend = s + strlen(s);
223 s = cpytill(tokenbuf,s,bufend,':',&len);
227 (void)strcat(tokenbuf+len,"/");
228 (void)strcat(tokenbuf+len,argv[0]);
231 fprintf(stderr,"Looking for %s\n",tokenbuf);
233 if (stat(tokenbuf,&statbuf) < 0) /* not there? */
235 if ((statbuf.st_mode & S_IFMT) == S_IFREG
236 && cando(S_IREAD,TRUE,&statbuf) && cando(S_IEXEC,TRUE,&statbuf)) {
237 xfound = tokenbuf; /* bingo! */
241 xfailed = savestr(tokenbuf);
244 fatal("Can't execute %s", xfailed ? xfailed : argv[0] );
247 argv[0] = savestr(xfound);
250 pidstatary = anew(Nullstab); /* for remembering popen pids, status */
252 filename = savestr(argv[0]);
253 origfilename = savestr(filename);
254 if (strEQ(filename,"-"))
258 str_cat(str,PRIVLIB);
259 (void)sprintf(buf, "\
260 /bin/sed -e '/^[^#]/b' \
261 -e '/^#[ ]*include[ ]/b' \
262 -e '/^#[ ]*define[ ]/b' \
263 -e '/^#[ ]*if[ ]/b' \
264 -e '/^#[ ]*ifdef[ ]/b' \
265 -e '/^#[ ]*ifndef[ ]/b' \
267 -e '/^#[ ]*endif/b' \
270 argv[0], CPPSTDIN, str_get(str), CPPMINUS);
271 #ifdef IAMSUID /* actually, this is caught earlier */
272 if (euid != uid && !euid) /* if running suidperl */
274 (void)seteuid(uid); /* musn't stay setuid root */
277 (void)setreuid(-1, uid);
283 rsfp = mypopen(buf,"r");
288 rsfp = fopen(argv[0],"r");
289 if (rsfp == Nullfp) {
290 extern char *sys_errlist[];
294 #ifndef IAMSUID /* in case script is not readable before setuid */
295 if (euid && stat(filename,&statbuf) >= 0 &&
296 statbuf.st_mode & (S_ISUID|S_ISGID)) {
297 (void)sprintf(buf, "%s/%s", BIN, "suidperl");
298 execv(buf, origargv); /* try again */
299 fatal("Can't do setuid\n");
303 fatal("Can't open perl script \"%s\": %s\n",
304 filename, sys_errlist[errno]);
306 str_free(str); /* free -I directories */
308 /* do we need to emulate setuid on scripts? */
310 /* This code is for those BSD systems that have setuid #! scripts disabled
311 * in the kernel because of a security problem. Merely defining DOSUID
312 * in perl will not fix that problem, but if you have disabled setuid
313 * scripts in the kernel, this will attempt to emulate setuid and setgid
314 * on scripts that have those now-otherwise-useless bits set. The setuid
315 * root version must be called suidperl. If regular perl discovers that
316 * it has opened a setuid script, it calls suidperl with the same argv
317 * that it had. If suidperl finds that the script it has just opened
318 * is NOT setuid root, it sets the effective uid back to the uid. We
319 * don't just make perl setuid root because that loses the effective
320 * uid we had before invoking perl, if it was different from the uid.
322 * DOSUID must be defined in both perl and suidperl, and IAMSUID must
323 * be defined in suidperl only. suidperl must be setuid root. The
324 * Configure script will set this up for you if you want it.
326 * There is also the possibility of have a script which is running
327 * set-id due to a C wrapper. We want to do the TAINT checks
328 * on these set-id scripts, but don't want to have the overhead of
329 * them in normal perl, and can't use suidperl because it will lose
330 * the effective uid info, so we have an additional non-setuid root
331 * version called taintperl that just does the TAINT checks.
335 if (fstat(fileno(rsfp),&statbuf) < 0) /* normal stat is insecure */
336 fatal("Can't stat script \"%s\"",filename);
337 if (statbuf.st_mode & (S_ISUID|S_ISGID)) {
342 /* On this access check to make sure the directories are readable,
343 * there is actually a small window that the user could use to make
344 * filename point to an accessible directory. So there is a faint
345 * chance that someone could execute a setuid script down in a
346 * non-accessible directory. I don't know what to do about that.
347 * But I don't think it's too important. The manual lies when
348 * it says access() is useful in setuid programs.
350 if (access(filename,1)) /* as a double check */
351 fatal("Permission denied");
353 /* If we can swap euid and uid, then we can determine access rights
354 * with a simple stat of the file, and then compare device and
355 * inode to make sure we did stat() on the same file we opened.
356 * Then we just have to make sure he or she can execute it.
359 struct stat tmpstatbuf;
361 if (setreuid(euid,uid) < 0 || getuid() != euid || geteuid() != uid)
362 fatal("Can't swap uid and euid"); /* really paranoid */
363 if (stat(filename,&tmpstatbuf) < 0) /* testing full pathname here */
364 fatal("Permission denied");
365 if (tmpstatbuf.st_dev != statbuf.st_dev ||
366 tmpstatbuf.st_ino != statbuf.st_ino) {
368 if (rsfp = mypopen("/bin/mail root","w")) { /* heh, heh */
370 "User %d tried to run dev %d ino %d in place of dev %d ino %d!\n\
371 (Filename of set-id script was %s, uid %d gid %d.)\n\nSincerely,\nperl\n",
372 uid,tmpstatbuf.st_dev, tmpstatbuf.st_ino,
373 statbuf.st_dev, statbuf.st_ino,
374 filename, statbuf.st_uid, statbuf.st_gid);
375 (void)mypclose(rsfp);
377 fatal("Permission denied\n");
379 if (setreuid(uid,euid) < 0 || getuid() != uid || geteuid() != euid)
380 fatal("Can't reswap uid and euid");
381 if (!cando(S_IEXEC,FALSE,&statbuf)) /* can real uid exec? */
382 fatal("Permission denied\n");
384 #endif /* SETREUID */
387 if ((statbuf.st_mode & S_IFMT) != S_IFREG)
388 fatal("Permission denied");
389 if ((statbuf.st_mode >> 6) & S_IWRITE)
390 fatal("Setuid/gid script is writable by world");
391 doswitches = FALSE; /* -s is insecure in suid */
393 if (fgets(tokenbuf,sizeof tokenbuf, rsfp) == Nullch ||
394 strnNE(tokenbuf,"#!",2) ) /* required even on Sys V */
396 for (s = tokenbuf+2; !isspace(*s); s++) ;
397 if (strnNE(s-4,"perl",4)) /* sanity check */
398 fatal("Not a perl script");
399 while (*s == ' ' || *s == '\t') s++;
401 * #! arg must be what we saw above. They can invoke it by
402 * mentioning suidperl explicitly, but they may not add any strange
403 * arguments beyond what #! says if they do invoke suidperl that way.
405 len = strlen(validarg);
406 if (strEQ(validarg," PHOOEY ") ||
407 strnNE(s,validarg,len) || !isspace(s[len]))
408 fatal("Args must match #! line");
411 if (euid != uid && (statbuf.st_mode & S_ISUID) &&
412 euid == statbuf.st_uid)
414 fatal("YOU HAVEN'T DISABLED SET-ID SCRIPTS IN THE KERNEL YET!\n\
415 FIX YOUR KERNEL, PUT A C WRAPPER AROUND THIS SCRIPT, OR USE -u AND UNDUMP!\n");
418 if (euid) { /* oops, we're not the setuid root perl */
421 (void)sprintf(buf, "%s/%s", BIN, "suidperl");
422 execv(buf, origargv); /* try again */
424 fatal("Can't do setuid\n");
427 if (statbuf.st_mode & S_ISGID && statbuf.st_gid != getegid())
429 (void)setegid(statbuf.st_gid);
432 (void)setregid((GIDTYPE)-1,statbuf.st_gid);
434 setgid(statbuf.st_gid);
437 if (statbuf.st_mode & S_ISUID) {
438 if (statbuf.st_uid != euid)
440 (void)seteuid(statbuf.st_uid); /* all that for this */
443 (void)setreuid((UIDTYPE)-1,statbuf.st_uid);
445 setuid(statbuf.st_uid);
449 else if (uid) /* oops, mustn't run as root */
451 (void)seteuid((UIDTYPE)uid);
454 (void)setreuid((UIDTYPE)-1,(UIDTYPE)uid);
456 setuid((UIDTYPE)uid);
459 euid = (int)geteuid();
460 if (!cando(S_IEXEC,TRUE,&statbuf))
461 fatal("Permission denied\n"); /* they can't do this */
465 fatal("-P not allowed for setuid/setgid script\n");
467 fatal("Script is not setuid/setgid in suidperl\n");
469 #ifndef TAINT /* we aren't taintperl or suidperl */
470 /* script has a wrapper--can't run suidperl or we lose euid */
471 else if (euid != uid || egid != gid) {
473 (void)sprintf(buf, "%s/%s", BIN, "taintperl");
474 execv(buf, origargv); /* try again */
475 fatal("Can't run setuid script with taint checks");
480 #ifndef TAINT /* we aren't taintperl or suidperl */
481 if (euid != uid || egid != gid) { /* (suidperl doesn't exist, in fact) */
482 #ifndef SETUID_SCRIPTS_ARE_SECURE_NOW
483 fstat(fileno(rsfp),&statbuf); /* may be either wrapped or real suid */
484 if ((euid != uid && euid == statbuf.st_uid && statbuf.st_mode & S_ISUID)
486 (egid != gid && egid == statbuf.st_gid && statbuf.st_mode & S_ISGID)
489 fatal("YOU HAVEN'T DISABLED SET-ID SCRIPTS IN THE KERNEL YET!\n\
490 FIX YOUR KERNEL, PUT A C WRAPPER AROUND THIS SCRIPT, OR USE -u AND UNDUMP!\n");
491 #endif /* SETUID_SCRIPTS_ARE_SECURE_NOW */
492 /* not set-id, must be wrapped */
494 (void)sprintf(buf, "%s/%s", BIN, "taintperl");
495 execv(buf, origargv); /* try again */
496 fatal("Can't run setuid script with taint checks");
501 defstab = stabent("_",TRUE);
505 stab_xhash(stabent("_DB",TRUE)) = debstash;
507 lineary = stab_xarray(aadd((tmpstab = stabent("line",TRUE))));
508 tmpstab->str_pok |= SP_MULTI;
509 subname = str_make("main",4);
510 DBstab = stabent("DB",TRUE);
511 DBstab->str_pok |= SP_MULTI;
512 DBsub = hadd(tmpstab = stabent("sub",TRUE));
513 tmpstab->str_pok |= SP_MULTI;
514 DBsingle = stab_val((tmpstab = stabent("single",TRUE)));
515 tmpstab->str_pok |= SP_MULTI;
521 bufend = bufptr = str_get(linestr);
523 savestack = anew(Nullstab); /* for saving non-local values */
524 stack = anew(Nullstab); /* for saving non-local values */
525 stack->ary_flags = 0; /* not a real array */
527 /* now parse the script */
530 if (yyparse() || error_count)
531 fatal("Execution aborted due to compilation errors.\n");
533 New(50,loop_stack,128,struct loop);
534 New(51,debname,128,char);
535 New(52,debdelim,128,char);
541 (void)UNLINK(e_tmpname);
544 /* initialize everything that won't change if we undump */
546 if (sigstab = stabent("SIG",allstabs)) {
547 sigstab->str_pok |= SP_MULTI;
551 magicalize("!#?^~=-%0123456789.+&*()<>,\\/[|`':");
553 amperstab = stabent("&",allstabs);
554 leftstab = stabent("`",allstabs);
555 rightstab = stabent("'",allstabs);
556 sawampersand = (amperstab || leftstab || rightstab);
557 if (tmpstab = stabent(":",allstabs))
558 str_set(STAB_STR(tmpstab),chopset);
560 /* these aren't necessarily magical */
561 if (tmpstab = stabent(";",allstabs))
562 str_set(STAB_STR(tmpstab),"\034");
566 if (tmpstab = stabent("0",allstabs))
567 str_set(STAB_STR(tmpstab),origfilename);
571 if (tmpstab = stabent("]",allstabs))
572 str_set(STAB_STR(tmpstab),rcsid);
573 str_nset(stab_val(stabent("\"", TRUE)), " ", 1);
575 stdinstab = stabent("STDIN",TRUE);
576 stdinstab->str_pok |= SP_MULTI;
577 stab_io(stdinstab) = stio_new();
578 stab_io(stdinstab)->ifp = stdin;
579 tmpstab = stabent("stdin",TRUE);
580 stab_io(tmpstab) = stab_io(stdinstab);
581 tmpstab->str_pok |= SP_MULTI;
583 tmpstab = stabent("STDOUT",TRUE);
584 tmpstab->str_pok |= SP_MULTI;
585 stab_io(tmpstab) = stio_new();
586 stab_io(tmpstab)->ofp = stab_io(tmpstab)->ifp = stdout;
587 defoutstab = tmpstab;
588 tmpstab = stabent("stdout",TRUE);
589 stab_io(tmpstab) = stab_io(defoutstab);
590 tmpstab->str_pok |= SP_MULTI;
592 curoutstab = stabent("STDERR",TRUE);
593 curoutstab->str_pok |= SP_MULTI;
594 stab_io(curoutstab) = stio_new();
595 stab_io(curoutstab)->ofp = stab_io(curoutstab)->ifp = stderr;
596 tmpstab = stabent("stderr",TRUE);
597 stab_io(tmpstab) = stab_io(curoutstab);
598 tmpstab->str_pok |= SP_MULTI;
599 curoutstab = defoutstab; /* switch back to STDOUT */
601 statname = Str_new(66,0); /* last filename we did stat on */
603 perldb = FALSE; /* don't try to instrument evals */
613 just_doit: /* come here if running an undumped a.out */
614 argc--,argv++; /* skip name of script */
616 for (; argc > 0 && **argv == '-'; argc--,argv++) {
617 if (argv[0][1] == '-') {
621 str_numset(stab_val(stabent(argv[0]+1,TRUE)),(double)1.0);
627 if (argvstab = stabent("ARGV",allstabs)) {
628 argvstab->str_pok |= SP_MULTI;
629 (void)aadd(argvstab);
630 for (; argc > 0; argc--,argv++) {
631 (void)apush(stab_array(argvstab),str_make(argv[0],0));
635 (void) stabent("ENV",TRUE); /* must test PATH and IFS */
637 if (envstab = stabent("ENV",allstabs)) {
638 envstab->str_pok |= SP_MULTI;
640 for (; *env; env++) {
641 if (!(s = index(*env,'=')))
644 str = str_make(s--,0);
645 str_magic(str, envstab, 'E', *env, s - *env);
646 (void)hstore(stab_hash(envstab), *env, s - *env, str, 0);
653 if (tmpstab = stabent("$",allstabs))
654 str_numset(STAB_STR(tmpstab),(double)getpid());
656 if (setjmp(top_env)) /* sets goto_targ on longjump */
657 loop_ptr = 0; /* start label stack again */
663 fprintf(stderr,"\nEXECUTING...\n\n");
668 (void) cmd_exec(main_root,G_SCALAR,-1);
671 fatal("Can't find label \"%s\"--aborting",goto_targ);
683 while (*sym = *list++) {
684 if (stab = stabent(sym,allstabs)) {
685 stab_flags(stab) = SF_VMAGIC;
686 str_magic(stab_val(stab), stab, 0, Nullch, 0);
691 /* this routine is in perly.c by virtue of being sort of an alternate main() */
694 do_eval(str,optype,stash,gimme,arglast)
701 STR **st = stack->ary_array;
706 char *oldfile = filename;
707 line_t oldline = line;
708 int oldtmps_base = tmps_base;
709 int oldsave = savestack->ary_fill;
710 SPAT *oldspat = curspat;
711 static char *last_eval = Nullch;
712 static CMD *last_root = Nullcmd;
715 tmps_base = tmps_max;
716 if (curstash != stash) {
717 (void)savehptr(&curstash);
720 str_set(stab_val(stabent("@",TRUE)),"");
721 if (optype != O_DOFILE) { /* normal eval */
724 str_sset(linestr,str);
725 str_cat(linestr,";"); /* be kind to them */
733 filename = savestr(str_get(str)); /* can't free this easily */
735 rsfp = fopen(filename,"r");
736 ar = stab_array(incstab);
737 if (!rsfp && *filename != '/') {
738 for (i = 0; i <= ar->ary_fill; i++) {
739 (void)sprintf(buf,"%s/%s",str_get(afetch(ar,i,TRUE)),filename);
740 rsfp = fopen(buf,"r");
742 filename = savestr(buf);
749 tmps_base = oldtmps_base;
750 if (gimme != G_ARRAY)
751 st[++sp] = &str_undef;
757 oldoldbufptr = oldbufptr = bufptr = str_get(linestr);
758 bufend = bufptr + linestr->str_cur;
759 if (setjmp(eval_env)) {
767 else if (last_root && *bufptr == *last_eval && strEQ(bufptr,last_eval)){
769 eval_root = last_root; /* no point in reparsing */
771 else if (in_eval == 1) {
776 last_eval = savestr(bufptr);
780 last_root = eval_root;
785 myroot = eval_root; /* in case cmd_exec does another eval! */
786 if (retval || error_count) {
788 last_root = Nullcmd; /* can't free on error, for some reason */
795 sp = cmd_exec(eval_root,gimme,sp);
796 st = stack->ary_array;
797 for (i = arglast[0] + 1; i <= sp; i++)
798 st[i] = str_static(st[i]);
799 /* if we don't save result, free zaps it */
800 if (in_eval != 1 && myroot != last_root)
806 tmps_base = oldtmps_base;
808 if (savestack->ary_fill > oldsave) /* let them use local() */
809 restorelist(oldsave);