… | |
… | |
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__) |
51 | vector<rxvt_term *> rxvt_term::termlist; |
52 | static uid_t saved_euid; |
52 | |
53 | static gid_t saved_egid; |
53 | // used to tell global functions which terminal instance is "active" |
54 | #endif |
54 | rxvt_t rxvt_current_term; |
|
|
55 | |
|
|
56 | static char curlocale[128], savelocale[128]; |
55 | |
57 | |
56 | bool |
58 | 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; |
|
|
67 | |
|
|
68 | static char curlocale[128], savelocale[128]; |
|
|
69 | |
|
|
70 | bool |
|
|
71 | rxvt_set_locale (const char *locale) |
59 | rxvt_set_locale (const char *locale) NOTHROW |
72 | { |
60 | { |
73 | if (!locale || !strncmp (locale, curlocale, 128)) |
61 | if (!locale || !strncmp (locale, curlocale, 128)) |
74 | return false; |
62 | return false; |
75 | |
63 | |
76 | strncpy (curlocale, locale, 128); |
64 | strncpy (curlocale, locale, 128); |
77 | setlocale (LC_CTYPE, curlocale); |
65 | setlocale (LC_CTYPE, curlocale); |
78 | return true; |
66 | return true; |
79 | } |
67 | } |
80 | |
68 | |
81 | void |
69 | void |
82 | rxvt_push_locale (const char *locale) |
70 | rxvt_push_locale (const char *locale) NOTHROW |
83 | { |
71 | { |
84 | strcpy (savelocale, curlocale); |
72 | strcpy (savelocale, curlocale); |
85 | rxvt_set_locale (locale); |
73 | rxvt_set_locale (locale); |
86 | } |
74 | } |
87 | |
75 | |
88 | void |
76 | void |
89 | rxvt_pop_locale () |
77 | rxvt_pop_locale () NOTHROW |
90 | { |
78 | { |
91 | rxvt_set_locale (savelocale); |
79 | rxvt_set_locale (savelocale); |
92 | } |
80 | } |
93 | |
81 | |
94 | #if ENABLE_COMBINING |
82 | #if ENABLE_COMBINING |
… | |
… | |
184 | #ifdef USE_XIM |
172 | #ifdef USE_XIM |
185 | im_ev (this, &rxvt_term::im_cb), |
173 | im_ev (this, &rxvt_term::im_cb), |
186 | #endif |
174 | #endif |
187 | termwin_ev (this, &rxvt_term::x_cb), |
175 | termwin_ev (this, &rxvt_term::x_cb), |
188 | vt_ev (this, &rxvt_term::x_cb), |
176 | vt_ev (this, &rxvt_term::x_cb), |
|
|
177 | child_ev (this, &rxvt_term::child_cb), |
189 | check_ev (this, &rxvt_term::check_cb), |
178 | check_ev (this, &rxvt_term::check_cb), |
190 | flush_ev (this, &rxvt_term::flush_cb), |
179 | flush_ev (this, &rxvt_term::flush_cb), |
191 | destroy_ev (this, &rxvt_term::destroy_cb), |
180 | destroy_ev (this, &rxvt_term::destroy_cb), |
192 | pty_ev (this, &rxvt_term::pty_cb), |
181 | pty_ev (this, &rxvt_term::pty_cb), |
193 | incr_ev (this, &rxvt_term::incr_cb) |
182 | incr_ev (this, &rxvt_term::incr_cb) |
… | |
… | |
196 | |
185 | |
197 | termlist.push_back (this); |
186 | termlist.push_back (this); |
198 | |
187 | |
199 | #ifdef KEYSYM_RESOURCE |
188 | #ifdef KEYSYM_RESOURCE |
200 | keyboard = new keyboard_manager; |
189 | keyboard = new keyboard_manager; |
201 | |
|
|
202 | if (!keyboard) |
|
|
203 | rxvt_fatal ("out of memory, aborting.\n"); |
|
|
204 | #endif |
190 | #endif |
205 | } |
191 | } |
206 | |
192 | |
207 | // clean up the most important stuff, do *not* call x or free mem etc. |
193 | // clean up the most important stuff, do *not* call x or free mem etc. |
208 | // for use before an emergency exit |
194 | // for use before an emergency exit |
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)); |
|
|
224 | |
|
|
225 | termlist.erase (find (termlist.begin (), termlist.end(), this)); |
205 | termlist.erase (find (termlist.begin (), termlist.end(), this)); |
226 | |
206 | |
227 | emergency_cleanup (); |
207 | emergency_cleanup (); |
228 | |
208 | |
229 | #if ENABLE_STYLES |
209 | #if ENABLE_STYLES |
… | |
… | |
304 | #ifdef KEYSYM_RESOURCE |
284 | #ifdef KEYSYM_RESOURCE |
305 | delete keyboard; |
285 | delete keyboard; |
306 | #endif |
286 | #endif |
307 | } |
287 | } |
308 | |
288 | |
|
|
289 | // child has exited, usually destroys |
309 | void |
290 | void |
310 | rxvt_term::child_exit () |
291 | rxvt_term::child_cb (child_watcher &w, int status) |
311 | { |
292 | { |
|
|
293 | HOOK_INVOKE ((this, HOOK_CHILD_EXIT, DT_INT, status, DT_END)); |
|
|
294 | |
312 | cmd_pid = 0; |
295 | cmd_pid = 0; |
313 | |
296 | |
314 | if (!OPTION (Opt_hold)) |
297 | if (!OPTION (Opt_hold)) |
315 | destroy (); |
298 | destroy (); |
316 | } |
299 | } |
317 | |
300 | |
318 | void |
301 | void |
319 | rxvt_term::destroy () |
302 | rxvt_term::destroy () |
320 | { |
303 | { |
321 | if (destroy_ev.active) |
304 | if (destroy_ev.active) |
|
|
305 | return; |
|
|
306 | |
|
|
307 | if (HOOK_INVOKE ((this, HOOK_DESTROY, DT_END))) |
322 | return; |
308 | return; |
323 | |
309 | |
324 | #if ENABLE_OVERLAY |
310 | #if ENABLE_OVERLAY |
325 | scr_overlay_off (); |
311 | scr_overlay_off (); |
326 | #endif |
312 | #endif |
… | |
… | |
478 | |
464 | |
479 | #ifdef KEYSYM_RESOURCE |
465 | #ifdef KEYSYM_RESOURCE |
480 | keyboard->register_done (); |
466 | keyboard->register_done (); |
481 | #endif |
467 | #endif |
482 | |
468 | |
|
|
469 | #if ENABLE_PERL |
|
|
470 | if (!rs[Rs_perl_ext_1]) |
|
|
471 | rs[Rs_perl_ext_1] = "default"; |
|
|
472 | |
|
|
473 | if ((rs[Rs_perl_ext_1] && *rs[Rs_perl_ext_1]) |
|
|
474 | || (rs[Rs_perl_ext_2] && *rs[Rs_perl_ext_2]) |
|
|
475 | || (rs[Rs_perl_eval] && *rs[Rs_perl_eval])) |
|
|
476 | { |
|
|
477 | rxvt_perl.init (this); |
|
|
478 | HOOK_INVOKE ((this, HOOK_INIT, DT_END)); |
|
|
479 | } |
|
|
480 | #endif |
|
|
481 | |
483 | #ifdef HAVE_SCROLLBARS |
482 | #ifdef HAVE_SCROLLBARS |
484 | if (OPTION (Opt_scrollBar)) |
483 | if (OPTION (Opt_scrollBar)) |
485 | scrollBar.setIdle (); /* set existence for size calculations */ |
484 | scrollBar.setIdle (); /* set existence for size calculations */ |
486 | #endif |
485 | #endif |
487 | |
486 | |
488 | #if ENABLE_PERL |
487 | 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 | |
488 | |
514 | create_windows (argc, argv); |
489 | create_windows (argc, argv); |
515 | |
490 | |
516 | dDisp; |
491 | dDisp; |
517 | |
492 | |
… | |
… | |
534 | check_our_parents (); |
509 | check_our_parents (); |
535 | rootwin_ev.start (display, display->root); |
510 | rootwin_ev.start (display, display->root); |
536 | } |
511 | } |
537 | #endif |
512 | #endif |
538 | |
513 | |
|
|
514 | set_colorfgbg (); |
|
|
515 | |
|
|
516 | init_command (cmd_argv); |
|
|
517 | |
|
|
518 | free (cmd_argv); |
|
|
519 | |
|
|
520 | if (pty->pty >= 0) |
|
|
521 | pty_ev.start (pty->pty, EVENT_READ); |
|
|
522 | |
|
|
523 | check_ev.start (); |
|
|
524 | |
|
|
525 | HOOK_INVOKE ((this, HOOK_START, DT_END)); |
|
|
526 | |
539 | XMapWindow (disp, vt); |
527 | XMapWindow (disp, vt); |
540 | XMapWindow (disp, parent[0]); |
528 | XMapWindow (disp, parent[0]); |
541 | |
529 | |
542 | set_colorfgbg (); |
|
|
543 | |
|
|
544 | init_command (cmd_argv); |
|
|
545 | |
|
|
546 | free (cmd_argv); |
|
|
547 | |
|
|
548 | if (pty.pty >= 0) |
|
|
549 | pty_ev.start (pty.pty, EVENT_READ); |
|
|
550 | |
|
|
551 | check_ev.start (); |
|
|
552 | |
|
|
553 | HOOK_INVOKE ((this, HOOK_START, DT_END)); |
|
|
554 | |
|
|
555 | return true; |
530 | return true; |
556 | } |
531 | } |
557 | |
532 | |
558 | static struct sig_handlers |
533 | static struct sig_handlers |
559 | { |
534 | { |
560 | sig_watcher sw_chld, sw_term, sw_int; |
535 | sig_watcher sw_term, sw_int; |
561 | |
536 | |
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 | /* |
537 | /* |
577 | * Catch a fatal signal and tidy up before quitting |
538 | * Catch a fatal signal and tidy up before quitting |
578 | */ |
539 | */ |
579 | void |
540 | void |
580 | sig_term (sig_watcher &w) |
541 | sig_term (sig_watcher &w) |
… | |
… | |
586 | signal (w.signum, SIG_DFL); |
547 | signal (w.signum, SIG_DFL); |
587 | kill (getpid (), w.signum); |
548 | kill (getpid (), w.signum); |
588 | } |
549 | } |
589 | |
550 | |
590 | sig_handlers () |
551 | sig_handlers () |
591 | : sw_chld (this, &sig_handlers::sig_chld), |
|
|
592 | sw_term (this, &sig_handlers::sig_term), |
552 | : sw_term (this, &sig_handlers::sig_term), |
593 | sw_int (this, &sig_handlers::sig_term) |
553 | sw_int (this, &sig_handlers::sig_term) |
594 | { |
554 | { |
595 | } |
555 | } |
596 | } sig_handlers; |
556 | } sig_handlers; |
597 | |
557 | |
598 | char **rxvt_environ; // startup environment |
558 | char **rxvt_environ; // startup environment |
599 | |
559 | |
600 | void |
560 | void |
601 | rxvt_init () |
561 | rxvt_init () |
602 | { |
562 | { |
|
|
563 | uid_t uid = getuid (); |
|
|
564 | gid_t gid = getgid (); |
|
|
565 | |
|
|
566 | // before doing anything else, check for setuid/setgid operation, |
|
|
567 | // start the helper process and drop privileges |
|
|
568 | if (uid != geteuid () |
|
|
569 | || gid != getegid ()) |
|
|
570 | { |
|
|
571 | #if PTYTTY_HELPER |
|
|
572 | rxvt_ptytty_server (); |
|
|
573 | #else |
|
|
574 | rxvt_warn ("running setuid/setgid without pty helper compiled in, continuing unprivileged.\n"); |
|
|
575 | #endif |
|
|
576 | |
|
|
577 | // drop privileges |
|
|
578 | #if HAVE_SETRESUID |
|
|
579 | setresgid (gid, gid, gid); |
|
|
580 | setresuid (uid, uid, uid); |
|
|
581 | #elif HAVE_SETREUID |
|
|
582 | setregid (gid, gid); |
|
|
583 | setreuid (uid, uid); |
|
|
584 | #elif HAVE_SETUID |
|
|
585 | setgid (gid); |
|
|
586 | setuid (uid); |
|
|
587 | #endif |
|
|
588 | |
|
|
589 | if (uid != geteuid () |
|
|
590 | || gid != getegid ()) |
|
|
591 | rxvt_fatal ("unable to drop privileges, aborting.\n"); |
|
|
592 | } |
|
|
593 | |
603 | rxvt_environ = environ; |
594 | 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 | |
595 | |
615 | signal (SIGHUP, SIG_IGN); |
596 | signal (SIGHUP, SIG_IGN); |
616 | signal (SIGPIPE, SIG_IGN); |
597 | signal (SIGPIPE, SIG_IGN); |
617 | |
598 | |
618 | sig_handlers.sw_chld.start (SIGCHLD); |
|
|
619 | sig_handlers.sw_term.start (SIGTERM); |
599 | sig_handlers.sw_term.start (SIGTERM); |
620 | sig_handlers.sw_int.start (SIGINT); |
600 | sig_handlers.sw_int.start (SIGINT); |
621 | |
601 | |
622 | /* need to trap SIGURG for SVR4 (Unixware) rlogin */ |
602 | /* need to trap SIGURG for SVR4 (Unixware) rlogin */ |
623 | /* signal (SIGURG, SIG_DFL); */ |
603 | /* signal (SIGURG, SIG_DFL); */ |
… | |
… | |
652 | rxvt_fatal ("memory allocation failure. aborting.\n"); |
632 | rxvt_fatal ("memory allocation failure. aborting.\n"); |
653 | |
633 | |
654 | return p; |
634 | return p; |
655 | } |
635 | } |
656 | |
636 | |
657 | void * |
637 | void * |
658 | rxvt_realloc (void *ptr, size_t size) |
638 | rxvt_realloc (void *ptr, size_t size) |
659 | { |
639 | { |
660 | void *p = realloc (ptr, size); |
640 | void *p = realloc (ptr, size); |
661 | |
641 | |
662 | if (!p) |
642 | if (!p) |
663 | rxvt_fatal ("memory allocation failure. aborting.\n"); |
643 | rxvt_fatal ("memory allocation failure. aborting.\n"); |
664 | |
644 | |
665 | return p; |
645 | return p; |
666 | } |
646 | } |
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 | pty.login (cmd_pid, OPTION (Opt_loginShell), rs[Rs_display_name]); |
|
|
730 | else |
|
|
731 | pty.logout (); |
|
|
732 | |
|
|
733 | rxvt_privileges (IGNORE); |
|
|
734 | } |
|
|
735 | #endif |
|
|
736 | |
647 | |
737 | /*----------------------------------------------------------------------*/ |
648 | /*----------------------------------------------------------------------*/ |
738 | /* |
649 | /* |
739 | * window size/position calculcations for XSizeHint and other storage. |
650 | * window size/position calculcations for XSizeHint and other storage. |
740 | * if width/height are non-zero then override calculated width/height |
651 | * if width/height are non-zero then override calculated width/height |
… | |
… | |
874 | * Called after a window size change. |
785 | * Called after a window size change. |
875 | */ |
786 | */ |
876 | void |
787 | void |
877 | rxvt_term::tt_winch () |
788 | rxvt_term::tt_winch () |
878 | { |
789 | { |
879 | if (pty.pty < 0) |
790 | if (pty->pty < 0) |
880 | return; |
791 | return; |
881 | |
792 | |
882 | struct winsize ws; |
793 | struct winsize ws; |
883 | |
794 | |
884 | ws.ws_col = ncol; |
795 | ws.ws_col = ncol; |
885 | ws.ws_row = nrow; |
796 | ws.ws_row = nrow; |
886 | ws.ws_xpixel = width; |
797 | ws.ws_xpixel = width; |
887 | ws.ws_ypixel = height; |
798 | ws.ws_ypixel = height; |
888 | (void)ioctl (pty.pty, TIOCSWINSZ, &ws); |
799 | (void)ioctl (pty->pty, TIOCSWINSZ, &ws); |
889 | |
800 | |
890 | #if 0 |
801 | #if 0 |
891 | // TIOCSWINSZ⎈ is supposed to do this automatically and correctly |
802 | // TIOCSWINSZ⎈ is supposed to do this automatically and correctly |
892 | if (cmd_pid) /* force through to the command */ |
803 | if (cmd_pid) /* force through to the command */ |
893 | kill (-cmd_pid, SIGWINCH); |
804 | kill (-cmd_pid, SIGWINCH); |
… | |
… | |
1679 | XFree (preedit_attr); |
1590 | XFree (preedit_attr); |
1680 | XFree (status_attr); |
1591 | XFree (status_attr); |
1681 | } |
1592 | } |
1682 | #endif /* USE_XIM */ |
1593 | #endif /* USE_XIM */ |
1683 | |
1594 | |
1684 | /*----------------------------------------------------------------------*/ |
|
|
1685 | rxvt_t rxvt_current_term; |
|
|
1686 | |
|
|
1687 | /*----------------------- end-of-file (C source) -----------------------*/ |
1595 | /*----------------------- end-of-file (C source) -----------------------*/ |