… | |
… | |
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 |
… | |
… | |
184 | #ifdef USE_XIM |
169 | #ifdef USE_XIM |
185 | im_ev (this, &rxvt_term::im_cb), |
170 | im_ev (this, &rxvt_term::im_cb), |
186 | #endif |
171 | #endif |
187 | termwin_ev (this, &rxvt_term::x_cb), |
172 | termwin_ev (this, &rxvt_term::x_cb), |
188 | vt_ev (this, &rxvt_term::x_cb), |
173 | vt_ev (this, &rxvt_term::x_cb), |
|
|
174 | child_ev (this, &rxvt_term::child_cb), |
189 | check_ev (this, &rxvt_term::check_cb), |
175 | check_ev (this, &rxvt_term::check_cb), |
190 | flush_ev (this, &rxvt_term::flush_cb), |
176 | flush_ev (this, &rxvt_term::flush_cb), |
191 | destroy_ev (this, &rxvt_term::destroy_cb), |
177 | destroy_ev (this, &rxvt_term::destroy_cb), |
192 | pty_ev (this, &rxvt_term::pty_cb), |
178 | pty_ev (this, &rxvt_term::pty_cb), |
193 | incr_ev (this, &rxvt_term::incr_cb) |
179 | incr_ev (this, &rxvt_term::incr_cb) |
… | |
… | |
209 | void rxvt_term::emergency_cleanup () |
195 | void rxvt_term::emergency_cleanup () |
210 | { |
196 | { |
211 | if (cmd_pid) |
197 | if (cmd_pid) |
212 | kill (-cmd_pid, SIGHUP); |
198 | kill (-cmd_pid, SIGHUP); |
213 | |
199 | |
214 | #ifdef UTMP_SUPPORT |
200 | delete pty; pty = 0; |
215 | privileged_utmp (RESTORE); |
|
|
216 | #endif |
|
|
217 | |
|
|
218 | pty.put (); |
|
|
219 | } |
201 | } |
220 | |
202 | |
221 | rxvt_term::~rxvt_term () |
203 | rxvt_term::~rxvt_term () |
222 | { |
204 | { |
223 | HOOK_INVOKE ((this, HOOK_DESTROY, DT_END)); |
205 | HOOK_INVOKE ((this, HOOK_DESTROY, DT_END)); |
… | |
… | |
304 | #ifdef KEYSYM_RESOURCE |
286 | #ifdef KEYSYM_RESOURCE |
305 | delete keyboard; |
287 | delete keyboard; |
306 | #endif |
288 | #endif |
307 | } |
289 | } |
308 | |
290 | |
|
|
291 | // child has exited, usually destroys |
309 | void |
292 | void |
310 | rxvt_term::child_exit () |
293 | rxvt_term::child_cb (child_watcher &w, int status) |
311 | { |
294 | { |
|
|
295 | HOOK_INVOKE ((this, HOOK_CHILD_EXIT, DT_INT, status, DT_END)); |
|
|
296 | |
312 | cmd_pid = 0; |
297 | cmd_pid = 0; |
313 | |
298 | |
314 | if (!OPTION (Opt_hold)) |
299 | if (!OPTION (Opt_hold)) |
315 | destroy (); |
300 | destroy (); |
316 | } |
301 | } |
… | |
… | |
478 | |
463 | |
479 | #ifdef KEYSYM_RESOURCE |
464 | #ifdef KEYSYM_RESOURCE |
480 | keyboard->register_done (); |
465 | keyboard->register_done (); |
481 | #endif |
466 | #endif |
482 | |
467 | |
|
|
468 | #if ENABLE_PERL |
|
|
469 | if (!rs[Rs_perl_ext_1]) |
|
|
470 | rs[Rs_perl_ext_1] = "default"; |
|
|
471 | |
|
|
472 | if ((rs[Rs_perl_ext_1] && *rs[Rs_perl_ext_1]) |
|
|
473 | || (rs[Rs_perl_ext_2] && *rs[Rs_perl_ext_2]) |
|
|
474 | || (rs[Rs_perl_eval] && *rs[Rs_perl_eval])) |
|
|
475 | { |
|
|
476 | rxvt_perl.init (this); |
|
|
477 | HOOK_INVOKE ((this, HOOK_INIT, DT_END)); |
|
|
478 | } |
|
|
479 | #endif |
|
|
480 | |
483 | #ifdef HAVE_SCROLLBARS |
481 | #ifdef HAVE_SCROLLBARS |
484 | if (OPTION (Opt_scrollBar)) |
482 | if (OPTION (Opt_scrollBar)) |
485 | scrollBar.setIdle (); /* set existence for size calculations */ |
483 | scrollBar.setIdle (); /* set existence for size calculations */ |
486 | #endif |
484 | #endif |
487 | |
485 | |
488 | #if ENABLE_PERL |
486 | pty = rxvt_new_ptytty (); |
489 | if (!rs[Rs_perl_ext_1]) |
|
|
490 | rs[Rs_perl_ext_1] = "default"; |
|
|
491 | |
|
|
492 | if ((rs[Rs_perl_ext_1] && *rs[Rs_perl_ext_1]) |
|
|
493 | || (rs[Rs_perl_ext_2] && *rs[Rs_perl_ext_2]) |
|
|
494 | || (rs[Rs_perl_eval] && *rs[Rs_perl_eval])) |
|
|
495 | { |
|
|
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); |
|
|
510 | HOOK_INVOKE ((this, HOOK_INIT, DT_END)); |
|
|
511 | } |
|
|
512 | #endif |
|
|
513 | |
487 | |
514 | create_windows (argc, argv); |
488 | create_windows (argc, argv); |
515 | |
489 | |
516 | dDisp; |
490 | dDisp; |
517 | |
491 | |
… | |
… | |
543 | |
517 | |
544 | init_command (cmd_argv); |
518 | init_command (cmd_argv); |
545 | |
519 | |
546 | free (cmd_argv); |
520 | free (cmd_argv); |
547 | |
521 | |
548 | if (pty.pty >= 0) |
522 | if (pty->pty >= 0) |
549 | pty_ev.start (pty.pty, EVENT_READ); |
523 | pty_ev.start (pty->pty, EVENT_READ); |
550 | |
524 | |
551 | check_ev.start (); |
525 | check_ev.start (); |
552 | |
526 | |
553 | HOOK_INVOKE ((this, HOOK_START, DT_END)); |
527 | HOOK_INVOKE ((this, HOOK_START, DT_END)); |
554 | |
528 | |
555 | return true; |
529 | return true; |
556 | } |
530 | } |
557 | |
531 | |
558 | static struct sig_handlers |
532 | static struct sig_handlers |
559 | { |
533 | { |
560 | sig_watcher sw_chld, sw_term, sw_int; |
534 | sig_watcher sw_term, sw_int; |
561 | |
535 | |
562 | void sig_chld (sig_watcher &w) |
|
|
563 | { |
|
|
564 | // we are being called for every SIGCHLD, find the corresponding term |
|
|
565 | int pid; |
|
|
566 | |
|
|
567 | while ((pid = waitpid (-1, NULL, WNOHANG)) > 0) |
|
|
568 | for (rxvt_term **t = rxvt_term::termlist.begin (); t < rxvt_term::termlist.end (); t++) |
|
|
569 | if (pid == (*t)->cmd_pid) |
|
|
570 | { |
|
|
571 | (*t)->child_exit (); |
|
|
572 | break; |
|
|
573 | } |
|
|
574 | } |
|
|
575 | |
|
|
576 | /* |
536 | /* |
577 | * Catch a fatal signal and tidy up before quitting |
537 | * Catch a fatal signal and tidy up before quitting |
578 | */ |
538 | */ |
579 | void |
539 | void |
580 | sig_term (sig_watcher &w) |
540 | sig_term (sig_watcher &w) |
… | |
… | |
586 | signal (w.signum, SIG_DFL); |
546 | signal (w.signum, SIG_DFL); |
587 | kill (getpid (), w.signum); |
547 | kill (getpid (), w.signum); |
588 | } |
548 | } |
589 | |
549 | |
590 | sig_handlers () |
550 | sig_handlers () |
591 | : sw_chld (this, &sig_handlers::sig_chld), |
|
|
592 | sw_term (this, &sig_handlers::sig_term), |
551 | : sw_term (this, &sig_handlers::sig_term), |
593 | sw_int (this, &sig_handlers::sig_term) |
552 | sw_int (this, &sig_handlers::sig_term) |
594 | { |
553 | { |
595 | } |
554 | } |
596 | } sig_handlers; |
555 | } sig_handlers; |
597 | |
556 | |
598 | char **rxvt_environ; // startup environment |
557 | char **rxvt_environ; // startup environment |
599 | |
558 | |
600 | void |
559 | void |
601 | rxvt_init () |
560 | rxvt_init () |
602 | { |
561 | { |
|
|
562 | uid_t uid = getuid (); |
|
|
563 | gid_t gid = getgid (); |
|
|
564 | |
|
|
565 | // before doing anything else, check for setuid/setgid operation, |
|
|
566 | // start the helper process and drop privileges |
|
|
567 | if (uid != geteuid () |
|
|
568 | || gid != getegid ()) |
|
|
569 | { |
|
|
570 | #if PTYTTY_HELPER |
|
|
571 | rxvt_ptytty_server (); |
|
|
572 | #else |
|
|
573 | rxvt_warn ("running setuid/setgid without pty helper compiled in, continuing unprivileged.\n"); |
|
|
574 | #endif |
|
|
575 | |
|
|
576 | // drop privileges |
|
|
577 | #if HAVE_SETRESUID |
|
|
578 | setresgid (gid, gid, gid); |
|
|
579 | setresuid (uid, uid, uid); |
|
|
580 | #elif HAVE_SETREUID |
|
|
581 | setregid (gid, gid); |
|
|
582 | setreuid (uid, uid); |
|
|
583 | #elif HAVE_SETUID |
|
|
584 | setgid (gid); |
|
|
585 | setuid (uid); |
|
|
586 | #endif |
|
|
587 | |
|
|
588 | if (uid != geteuid () |
|
|
589 | || gid != getegid ()) |
|
|
590 | rxvt_fatal ("unable to drop privileges, aborting.\n"); |
|
|
591 | } |
|
|
592 | |
603 | rxvt_environ = environ; |
593 | 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 | |
594 | |
615 | signal (SIGHUP, SIG_IGN); |
595 | signal (SIGHUP, SIG_IGN); |
616 | signal (SIGPIPE, SIG_IGN); |
596 | signal (SIGPIPE, SIG_IGN); |
617 | |
597 | |
618 | sig_handlers.sw_chld.start (SIGCHLD); |
|
|
619 | sig_handlers.sw_term.start (SIGTERM); |
598 | sig_handlers.sw_term.start (SIGTERM); |
620 | sig_handlers.sw_int.start (SIGINT); |
599 | sig_handlers.sw_int.start (SIGINT); |
621 | |
600 | |
622 | /* need to trap SIGURG for SVR4 (Unixware) rlogin */ |
601 | /* need to trap SIGURG for SVR4 (Unixware) rlogin */ |
623 | /* signal (SIGURG, SIG_DFL); */ |
602 | /* signal (SIGURG, SIG_DFL); */ |
… | |
… | |
662 | if (!p) |
641 | if (!p) |
663 | rxvt_fatal ("memory allocation failure. aborting.\n"); |
642 | rxvt_fatal ("memory allocation failure. aborting.\n"); |
664 | |
643 | |
665 | return p; |
644 | return p; |
666 | } |
645 | } |
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 | 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 | |
646 | |
737 | /*----------------------------------------------------------------------*/ |
647 | /*----------------------------------------------------------------------*/ |
738 | /* |
648 | /* |
739 | * window size/position calculcations for XSizeHint and other storage. |
649 | * window size/position calculcations for XSizeHint and other storage. |
740 | * if width/height are non-zero then override calculated width/height |
650 | * if width/height are non-zero then override calculated width/height |
… | |
… | |
874 | * Called after a window size change. |
784 | * Called after a window size change. |
875 | */ |
785 | */ |
876 | void |
786 | void |
877 | rxvt_term::tt_winch () |
787 | rxvt_term::tt_winch () |
878 | { |
788 | { |
879 | if (pty.pty < 0) |
789 | if (pty->pty < 0) |
880 | return; |
790 | return; |
881 | |
791 | |
882 | struct winsize ws; |
792 | struct winsize ws; |
883 | |
793 | |
884 | ws.ws_col = ncol; |
794 | ws.ws_col = ncol; |
885 | ws.ws_row = nrow; |
795 | ws.ws_row = nrow; |
886 | ws.ws_xpixel = width; |
796 | ws.ws_xpixel = width; |
887 | ws.ws_ypixel = height; |
797 | ws.ws_ypixel = height; |
888 | (void)ioctl (pty.pty, TIOCSWINSZ, &ws); |
798 | (void)ioctl (pty->pty, TIOCSWINSZ, &ws); |
889 | |
799 | |
890 | #if 0 |
800 | #if 0 |
891 | // TIOCSWINSZ⎈ is supposed to do this automatically and correctly |
801 | // TIOCSWINSZ⎈ is supposed to do this automatically and correctly |
892 | if (cmd_pid) /* force through to the command */ |
802 | if (cmd_pid) /* force through to the command */ |
893 | kill (-cmd_pid, SIGWINCH); |
803 | kill (-cmd_pid, SIGWINCH); |