… | |
… | |
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__) |
|
|
52 | static uid_t saved_euid; |
|
|
53 | static gid_t saved_egid; |
|
|
54 | #endif |
|
|
55 | |
|
|
56 | bool |
|
|
57 | rxvt_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 | |
|
|
66 | vector<rxvt_term *> rxvt_term::termlist; |
51 | vector<rxvt_term *> rxvt_term::termlist; |
67 | |
52 | |
68 | static char curlocale[128], savelocale[128]; |
53 | static char curlocale[128], savelocale[128]; |
69 | |
54 | |
70 | bool |
55 | bool |
… | |
… | |
209 | void rxvt_term::emergency_cleanup () |
194 | void 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 | |
221 | rxvt_term::~rxvt_term () |
202 | rxvt_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 | |
… | |
… | |
598 | char **rxvt_environ; // startup environment |
568 | char **rxvt_environ; // startup environment |
599 | |
569 | |
600 | void |
570 | void |
601 | rxvt_init () |
571 | rxvt_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 | || gid != getegid ()) |
|
|
580 | { |
|
|
581 | #if PTYTTY_HELPER |
|
|
582 | rxvt_ptytty_server (); |
|
|
583 | #else |
|
|
584 | rxvt_warn ("running setuid/setgid without pty helper compiled in, continuing unprivileged.\n"); |
|
|
585 | #endif |
|
|
586 | |
|
|
587 | // drop privileges |
|
|
588 | #if HAVE_SETRESUID |
|
|
589 | setresgid (gid, gid, gid); |
|
|
590 | setresuid (uid, uid, uid); |
|
|
591 | #elif HAVE_SETREUID |
|
|
592 | setregid (gid, gid); |
|
|
593 | setreuid (uid, uid); |
|
|
594 | #elif HAVE_SETUID |
|
|
595 | setgid (gid); |
|
|
596 | setuid (uid); |
|
|
597 | #endif |
|
|
598 | |
|
|
599 | if (uid != geteuid () |
|
|
600 | || gid != getegid ()) |
|
|
601 | rxvt_fatal ("unable to drop privileges, aborting.\n"); |
|
|
602 | } |
|
|
603 | |
603 | rxvt_environ = environ; |
604 | 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 | |
605 | |
615 | signal (SIGHUP, SIG_IGN); |
606 | signal (SIGHUP, SIG_IGN); |
616 | signal (SIGPIPE, SIG_IGN); |
607 | signal (SIGPIPE, SIG_IGN); |
617 | |
608 | |
618 | sig_handlers.sw_chld.start (SIGCHLD); |
609 | sig_handlers.sw_chld.start (SIGCHLD); |
… | |
… | |
662 | if (!p) |
653 | if (!p) |
663 | rxvt_fatal ("memory allocation failure. aborting.\n"); |
654 | rxvt_fatal ("memory allocation failure. aborting.\n"); |
664 | |
655 | |
665 | return p; |
656 | return p; |
666 | } |
657 | } |
667 | |
|
|
668 | /* ------------------------------------------------------------------------- * |
|
|
669 | * PRIVILEGED OPERATIONS * |
|
|
670 | * ------------------------------------------------------------------------- */ |
|
|
671 | /* take care of suid/sgid super-user (root) privileges */ |
|
|
672 | void |
|
|
673 | rxvt_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 |
|
|
719 | void |
|
|
720 | rxvt_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 | makeutent (pty.name, rs[Rs_display_name]); |
|
|
730 | else |
|
|
731 | cleanutent (); |
|
|
732 | |
|
|
733 | rxvt_privileges (IGNORE); |
|
|
734 | } |
|
|
735 | #endif |
|
|
736 | |
658 | |
737 | /*----------------------------------------------------------------------*/ |
659 | /*----------------------------------------------------------------------*/ |
738 | /* |
660 | /* |
739 | * window size/position calculcations for XSizeHint and other storage. |
661 | * window size/position calculcations for XSizeHint and other storage. |
740 | * if width/height are non-zero then override calculated width/height |
662 | * if width/height are non-zero then override calculated width/height |
… | |
… | |
874 | * Called after a window size change. |
796 | * Called after a window size change. |
875 | */ |
797 | */ |
876 | void |
798 | void |
877 | rxvt_term::tt_winch () |
799 | rxvt_term::tt_winch () |
878 | { |
800 | { |
879 | if (pty.pty < 0) |
801 | if (pty->pty < 0) |
880 | return; |
802 | return; |
881 | |
803 | |
882 | struct winsize ws; |
804 | struct winsize ws; |
883 | |
805 | |
884 | ws.ws_col = ncol; |
806 | ws.ws_col = ncol; |
885 | ws.ws_row = nrow; |
807 | ws.ws_row = nrow; |
886 | ws.ws_xpixel = width; |
808 | ws.ws_xpixel = width; |
887 | ws.ws_ypixel = height; |
809 | ws.ws_ypixel = height; |
888 | (void)ioctl (pty.pty, TIOCSWINSZ, &ws); |
810 | (void)ioctl (pty->pty, TIOCSWINSZ, &ws); |
889 | |
811 | |
890 | #if 0 |
812 | #if 0 |
891 | // TIOCSWINSZ⎈ is supposed to do this automatically and correctly |
813 | // TIOCSWINSZ⎈ is supposed to do this automatically and correctly |
892 | if (cmd_pid) /* force through to the command */ |
814 | if (cmd_pid) /* force through to the command */ |
893 | kill (-cmd_pid, SIGWINCH); |
815 | kill (-cmd_pid, SIGWINCH); |