ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/rxvt-unicode/src/main.C
(Generate patch)

Comparing rxvt-unicode/src/main.C (file contents):
Revision 1.181 by root, Tue Jan 17 06:06:42 2006 UTC vs.
Revision 1.187 by root, Tue Jan 17 16:22:41 2006 UTC

46 46
47#ifdef HAVE_TERMIOS_H 47#ifdef HAVE_TERMIOS_H
48# include <termios.h> 48# include <termios.h>
49#endif 49#endif
50 50
51#if (defined(HAVE_SETEUID) || defined(HAVE_SETREUID)) && !defined(__CYGWIN32__)
52static uid_t saved_euid;
53static gid_t saved_egid;
54#endif
55
56bool
57rxvt_tainted ()
58{
59#if (defined(HAVE_SETEUID) || defined(HAVE_SETREUID)) && !defined(__CYGWIN32__)
60 return getuid () != saved_euid || getgid () != saved_egid;
61#else
62 return false;
63#endif
64}
65
66vector<rxvt_term *> rxvt_term::termlist; 51vector<rxvt_term *> rxvt_term::termlist;
67 52
68static char curlocale[128], savelocale[128]; 53static char curlocale[128], savelocale[128];
69 54
70bool 55bool
209void rxvt_term::emergency_cleanup () 194void rxvt_term::emergency_cleanup ()
210{ 195{
211 if (cmd_pid) 196 if (cmd_pid)
212 kill (-cmd_pid, SIGHUP); 197 kill (-cmd_pid, SIGHUP);
213 198
214#ifdef UTMP_SUPPORT 199 delete pty; pty = 0;
215 privileged_utmp (RESTORE);
216#endif
217
218 pty.put ();
219} 200}
220 201
221rxvt_term::~rxvt_term () 202rxvt_term::~rxvt_term ()
222{ 203{
223 HOOK_INVOKE ((this, HOOK_DESTROY, DT_END)); 204 HOOK_INVOKE ((this, HOOK_DESTROY, DT_END));
491 472
492 if ((rs[Rs_perl_ext_1] && *rs[Rs_perl_ext_1]) 473 if ((rs[Rs_perl_ext_1] && *rs[Rs_perl_ext_1])
493 || (rs[Rs_perl_ext_2] && *rs[Rs_perl_ext_2]) 474 || (rs[Rs_perl_ext_2] && *rs[Rs_perl_ext_2])
494 || (rs[Rs_perl_eval] && *rs[Rs_perl_eval])) 475 || (rs[Rs_perl_eval] && *rs[Rs_perl_eval]))
495 { 476 {
496#if (defined(HAVE_SETEUID) || defined(HAVE_SETREUID)) && !defined(__CYGWIN32__)
497 // ignore some perl-related arguments if some bozo installed us set[ug]id
498 if (rxvt_tainted ())
499 {
500 if ((rs[Rs_perl_lib] && *rs[Rs_perl_lib])
501 || (rs[Rs_perl_eval] && *rs[Rs_perl_eval]))
502 {
503 rxvt_warn ("running with elevated privileges: ignoring perl-lib and perl-eval.\n");
504 rs[Rs_perl_lib] = 0;
505 rs[Rs_perl_eval] = 0;
506 }
507 }
508#endif
509 rxvt_perl.init (this); 477 rxvt_perl.init (this);
510 HOOK_INVOKE ((this, HOOK_INIT, DT_END)); 478 HOOK_INVOKE ((this, HOOK_INIT, DT_END));
511 } 479 }
512#endif 480#endif
481
482 pty = rxvt_new_ptytty ();
513 483
514 create_windows (argc, argv); 484 create_windows (argc, argv);
515 485
516 dDisp; 486 dDisp;
517 487
543 513
544 init_command (cmd_argv); 514 init_command (cmd_argv);
545 515
546 free (cmd_argv); 516 free (cmd_argv);
547 517
548 if (pty.pty >= 0) 518 if (pty->pty >= 0)
549 pty_ev.start (pty.pty, EVENT_READ); 519 pty_ev.start (pty->pty, EVENT_READ);
550 520
551 check_ev.start (); 521 check_ev.start ();
552 522
553 HOOK_INVOKE ((this, HOOK_START, DT_END)); 523 HOOK_INVOKE ((this, HOOK_START, DT_END));
554 524
598char **rxvt_environ; // startup environment 568char **rxvt_environ; // startup environment
599 569
600void 570void
601rxvt_init () 571rxvt_init ()
602{ 572{
573 uid_t uid = getuid ();
574 gid_t gid = getgid ();
575
576 // before doing anything else, check for setuid/setgid operation,
577 // start the helper process and drop privileges
578 if (uid != geteuid ()
579 || 1 //D
580 || gid != getegid ())
581 {
582#if PTYTTY_HELPER
583 rxvt_ptytty_server ();
584#else
585 rxvt_warn ("running setuid/setgid without pty helper compiled in, continuing unprivileged.\n");
586#endif
587
588 // drop privileges
589#if HAVE_SETRESUID
590 setresgid (gid, gid, gid);
591 setresuid (uid, uid, uid);
592#elif HAVE_SETREUID
593 setregid (gid, gid);
594 setreuid (uid, uid);
595#elif HAVE_SETUID
596 setgid (gid);
597 setuid (uid);
598#endif
599
600 if (uid != geteuid ()
601 || gid != getegid ())
602 rxvt_fatal ("unable to drop privileges, aborting.\n");
603 }
604
603 rxvt_environ = environ; 605 rxvt_environ = environ;
604
605 /*
606 * Save and then give up any super-user privileges
607 * If we need privileges in any area then we must specifically request it.
608 * We should only need to be root in these cases:
609 * 1. write utmp entries on some systems
610 * 2. chown tty on some systems
611 */
612 rxvt_privileges (SAVE);
613 rxvt_privileges (IGNORE);
614 606
615 signal (SIGHUP, SIG_IGN); 607 signal (SIGHUP, SIG_IGN);
616 signal (SIGPIPE, SIG_IGN); 608 signal (SIGPIPE, SIG_IGN);
617 609
618 sig_handlers.sw_chld.start (SIGCHLD); 610 sig_handlers.sw_chld.start (SIGCHLD);
662 if (!p) 654 if (!p)
663 rxvt_fatal ("memory allocation failure. aborting.\n"); 655 rxvt_fatal ("memory allocation failure. aborting.\n");
664 656
665 return p; 657 return p;
666} 658}
667
668/* ------------------------------------------------------------------------- *
669 * PRIVILEGED OPERATIONS *
670 * ------------------------------------------------------------------------- */
671/* take care of suid/sgid super-user (root) privileges */
672void
673rxvt_privileges (rxvt_privaction action)
674{
675#if ! defined(__CYGWIN32__)
676# if !defined(HAVE_SETEUID) && defined(HAVE_SETREUID)
677 /* setreuid () is the poor man's setuid (), seteuid () */
678# define seteuid(a) setreuid(-1, (a))
679# define setegid(a) setregid(-1, (a))
680# define HAVE_SETEUID
681# endif
682# ifdef HAVE_SETEUID
683 switch (action)
684 {
685 case IGNORE:
686 /*
687 * change effective uid/gid - not real uid/gid - so we can switch
688 * back to root later, as required
689 */
690 setegid (getgid ());
691 seteuid (getuid ());
692 break;
693 case SAVE:
694 saved_egid = getegid ();
695 saved_euid = geteuid ();
696 break;
697 case RESTORE:
698 setegid (saved_egid);
699 seteuid (saved_euid);
700 break;
701 }
702# else
703 switch (action)
704 {
705 case IGNORE:
706 setgid (getgid ());
707 setuid (getuid ());
708 /* FALLTHROUGH */
709 case SAVE:
710 /* FALLTHROUGH */
711 case RESTORE:
712 break;
713 }
714# endif
715#endif
716}
717
718#ifdef UTMP_SUPPORT
719void
720rxvt_term::privileged_utmp (rxvt_privaction action)
721{
722 if (OPTION (Opt_utmpInhibit)
723 || !pty.name || !*pty.name)
724 return;
725
726 rxvt_privileges (RESTORE);
727
728 if (action == SAVE)
729 session.login (pty.name, cmd_pid, OPTION (Opt_loginShell), rs[Rs_display_name]);
730 else
731 session.logout ();
732
733 rxvt_privileges (IGNORE);
734}
735#endif
736 659
737/*----------------------------------------------------------------------*/ 660/*----------------------------------------------------------------------*/
738/* 661/*
739 * window size/position calculcations for XSizeHint and other storage. 662 * window size/position calculcations for XSizeHint and other storage.
740 * if width/height are non-zero then override calculated width/height 663 * if width/height are non-zero then override calculated width/height
874 * Called after a window size change. 797 * Called after a window size change.
875 */ 798 */
876void 799void
877rxvt_term::tt_winch () 800rxvt_term::tt_winch ()
878{ 801{
879 if (pty.pty < 0) 802 if (pty->pty < 0)
880 return; 803 return;
881 804
882 struct winsize ws; 805 struct winsize ws;
883 806
884 ws.ws_col = ncol; 807 ws.ws_col = ncol;
885 ws.ws_row = nrow; 808 ws.ws_row = nrow;
886 ws.ws_xpixel = width; 809 ws.ws_xpixel = width;
887 ws.ws_ypixel = height; 810 ws.ws_ypixel = height;
888 (void)ioctl (pty.pty, TIOCSWINSZ, &ws); 811 (void)ioctl (pty->pty, TIOCSWINSZ, &ws);
889 812
890#if 0 813#if 0
891 // TIOCSWINSZ⎈ is supposed to do this automatically and correctly 814 // TIOCSWINSZ⎈ is supposed to do this automatically and correctly
892 if (cmd_pid) /* force through to the command */ 815 if (cmd_pid) /* force through to the command */
893 kill (-cmd_pid, SIGWINCH); 816 kill (-cmd_pid, SIGWINCH);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines