… | |
… | |
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 |
… | |
… | |
214 | delete pty; pty = 0; |
200 | delete pty; pty = 0; |
215 | } |
201 | } |
216 | |
202 | |
217 | rxvt_term::~rxvt_term () |
203 | rxvt_term::~rxvt_term () |
218 | { |
204 | { |
219 | HOOK_INVOKE ((this, HOOK_DESTROY, DT_END)); |
|
|
220 | |
|
|
221 | termlist.erase (find (termlist.begin (), termlist.end(), this)); |
205 | termlist.erase (find (termlist.begin (), termlist.end(), this)); |
222 | |
206 | |
223 | emergency_cleanup (); |
207 | emergency_cleanup (); |
224 | |
208 | |
225 | #if ENABLE_STYLES |
209 | #if ENABLE_STYLES |
… | |
… | |
300 | #ifdef KEYSYM_RESOURCE |
284 | #ifdef KEYSYM_RESOURCE |
301 | delete keyboard; |
285 | delete keyboard; |
302 | #endif |
286 | #endif |
303 | } |
287 | } |
304 | |
288 | |
|
|
289 | // child has exited, usually destroys |
305 | void |
290 | void |
306 | rxvt_term::child_exit () |
291 | rxvt_term::child_cb (child_watcher &w, int status) |
307 | { |
292 | { |
|
|
293 | HOOK_INVOKE ((this, HOOK_CHILD_EXIT, DT_INT, status, DT_END)); |
|
|
294 | |
308 | cmd_pid = 0; |
295 | cmd_pid = 0; |
309 | |
296 | |
310 | if (!OPTION (Opt_hold)) |
297 | if (!OPTION (Opt_hold)) |
311 | destroy (); |
298 | destroy (); |
312 | } |
299 | } |
313 | |
300 | |
314 | void |
301 | void |
315 | rxvt_term::destroy () |
302 | rxvt_term::destroy () |
316 | { |
303 | { |
317 | if (destroy_ev.active) |
304 | if (destroy_ev.active) |
|
|
305 | return; |
|
|
306 | |
|
|
307 | if (HOOK_INVOKE ((this, HOOK_DESTROY, DT_END))) |
318 | return; |
308 | return; |
319 | |
309 | |
320 | #if ENABLE_OVERLAY |
310 | #if ENABLE_OVERLAY |
321 | scr_overlay_off (); |
311 | scr_overlay_off (); |
322 | #endif |
312 | #endif |
… | |
… | |
479 | #ifdef HAVE_SCROLLBARS |
469 | #ifdef HAVE_SCROLLBARS |
480 | if (OPTION (Opt_scrollBar)) |
470 | if (OPTION (Opt_scrollBar)) |
481 | scrollBar.setIdle (); /* set existence for size calculations */ |
471 | scrollBar.setIdle (); /* set existence for size calculations */ |
482 | #endif |
472 | #endif |
483 | |
473 | |
484 | #if ENABLE_PERL |
|
|
485 | if (!rs[Rs_perl_ext_1]) |
|
|
486 | rs[Rs_perl_ext_1] = "default"; |
|
|
487 | |
|
|
488 | if ((rs[Rs_perl_ext_1] && *rs[Rs_perl_ext_1]) |
|
|
489 | || (rs[Rs_perl_ext_2] && *rs[Rs_perl_ext_2]) |
|
|
490 | || (rs[Rs_perl_eval] && *rs[Rs_perl_eval])) |
|
|
491 | { |
|
|
492 | #if (defined(HAVE_SETEUID) || defined(HAVE_SETREUID)) && !defined(__CYGWIN32__) |
|
|
493 | // ignore some perl-related arguments if some bozo installed us set[ug]id |
|
|
494 | if (rxvt_tainted ()) |
|
|
495 | { |
|
|
496 | if ((rs[Rs_perl_lib] && *rs[Rs_perl_lib]) |
|
|
497 | || (rs[Rs_perl_eval] && *rs[Rs_perl_eval])) |
|
|
498 | { |
|
|
499 | rxvt_warn ("running with elevated privileges: ignoring perl-lib and perl-eval.\n"); |
|
|
500 | rs[Rs_perl_lib] = 0; |
|
|
501 | rs[Rs_perl_eval] = 0; |
|
|
502 | } |
|
|
503 | } |
|
|
504 | #endif |
|
|
505 | rxvt_perl.init (this); |
|
|
506 | HOOK_INVOKE ((this, HOOK_INIT, DT_END)); |
|
|
507 | } |
|
|
508 | #endif |
|
|
509 | |
|
|
510 | pty = rxvt_new_ptytty (); |
474 | pty = rxvt_new_ptytty (); |
511 | |
475 | |
512 | create_windows (argc, argv); |
476 | create_windows (argc, argv); |
513 | |
477 | |
514 | dDisp; |
478 | dDisp; |
… | |
… | |
532 | check_our_parents (); |
496 | check_our_parents (); |
533 | rootwin_ev.start (display, display->root); |
497 | rootwin_ev.start (display, display->root); |
534 | } |
498 | } |
535 | #endif |
499 | #endif |
536 | |
500 | |
|
|
501 | set_colorfgbg (); |
|
|
502 | |
|
|
503 | init_command (cmd_argv); |
|
|
504 | |
|
|
505 | free (cmd_argv); |
|
|
506 | |
|
|
507 | if (pty->pty >= 0) |
|
|
508 | pty_ev.start (pty->pty, EVENT_READ); |
|
|
509 | |
|
|
510 | check_ev.start (); |
|
|
511 | |
|
|
512 | HOOK_INVOKE ((this, HOOK_START, DT_END)); |
|
|
513 | |
537 | XMapWindow (disp, vt); |
514 | XMapWindow (disp, vt); |
538 | XMapWindow (disp, parent[0]); |
515 | XMapWindow (disp, parent[0]); |
539 | |
516 | |
540 | set_colorfgbg (); |
|
|
541 | |
|
|
542 | init_command (cmd_argv); |
|
|
543 | |
|
|
544 | free (cmd_argv); |
|
|
545 | |
|
|
546 | if (pty->pty >= 0) |
|
|
547 | pty_ev.start (pty->pty, EVENT_READ); |
|
|
548 | |
|
|
549 | check_ev.start (); |
|
|
550 | |
|
|
551 | HOOK_INVOKE ((this, HOOK_START, DT_END)); |
|
|
552 | |
|
|
553 | return true; |
517 | return true; |
554 | } |
518 | } |
555 | |
519 | |
556 | static struct sig_handlers |
520 | static struct sig_handlers |
557 | { |
521 | { |
558 | sig_watcher sw_chld, sw_term, sw_int; |
522 | sig_watcher sw_term, sw_int; |
559 | |
523 | |
560 | void sig_chld (sig_watcher &w) |
|
|
561 | { |
|
|
562 | // we are being called for every SIGCHLD, find the corresponding term |
|
|
563 | int pid; |
|
|
564 | |
|
|
565 | while ((pid = waitpid (-1, NULL, WNOHANG)) > 0) |
|
|
566 | for (rxvt_term **t = rxvt_term::termlist.begin (); t < rxvt_term::termlist.end (); t++) |
|
|
567 | if (pid == (*t)->cmd_pid) |
|
|
568 | { |
|
|
569 | (*t)->child_exit (); |
|
|
570 | break; |
|
|
571 | } |
|
|
572 | } |
|
|
573 | |
|
|
574 | /* |
524 | /* |
575 | * Catch a fatal signal and tidy up before quitting |
525 | * Catch a fatal signal and tidy up before quitting |
576 | */ |
526 | */ |
577 | void |
527 | void |
578 | sig_term (sig_watcher &w) |
528 | sig_term (sig_watcher &w) |
… | |
… | |
584 | signal (w.signum, SIG_DFL); |
534 | signal (w.signum, SIG_DFL); |
585 | kill (getpid (), w.signum); |
535 | kill (getpid (), w.signum); |
586 | } |
536 | } |
587 | |
537 | |
588 | sig_handlers () |
538 | sig_handlers () |
589 | : sw_chld (this, &sig_handlers::sig_chld), |
|
|
590 | sw_term (this, &sig_handlers::sig_term), |
539 | : sw_term (this, &sig_handlers::sig_term), |
591 | sw_int (this, &sig_handlers::sig_term) |
540 | sw_int (this, &sig_handlers::sig_term) |
592 | { |
541 | { |
593 | } |
542 | } |
594 | } sig_handlers; |
543 | } sig_handlers; |
595 | |
544 | |
… | |
… | |
602 | gid_t gid = getgid (); |
551 | gid_t gid = getgid (); |
603 | |
552 | |
604 | // before doing anything else, check for setuid/setgid operation, |
553 | // before doing anything else, check for setuid/setgid operation, |
605 | // start the helper process and drop privileges |
554 | // start the helper process and drop privileges |
606 | if (uid != geteuid () |
555 | if (uid != geteuid () |
607 | || 1 //D |
|
|
608 | || gid != getegid ()) |
556 | || gid != getegid ()) |
609 | { |
557 | { |
610 | #if PTYTTY_HELPER |
558 | #if PTYTTY_HELPER |
611 | rxvt_ptytty_server (); |
559 | rxvt_ptytty_server (); |
612 | #else |
560 | #else |
… | |
… | |
630 | rxvt_fatal ("unable to drop privileges, aborting.\n"); |
578 | rxvt_fatal ("unable to drop privileges, aborting.\n"); |
631 | } |
579 | } |
632 | |
580 | |
633 | rxvt_environ = environ; |
581 | rxvt_environ = environ; |
634 | |
582 | |
635 | /* |
|
|
636 | * Save and then give up any super-user privileges |
|
|
637 | * If we need privileges in any area then we must specifically request it. |
|
|
638 | * We should only need to be root in these cases: |
|
|
639 | * 1. write utmp entries on some systems |
|
|
640 | * 2. chown tty on some systems |
|
|
641 | */ |
|
|
642 | rxvt_privileges (SAVE); |
|
|
643 | rxvt_privileges (IGNORE); |
|
|
644 | |
|
|
645 | signal (SIGHUP, SIG_IGN); |
583 | signal (SIGHUP, SIG_IGN); |
646 | signal (SIGPIPE, SIG_IGN); |
584 | signal (SIGPIPE, SIG_IGN); |
647 | |
585 | |
648 | sig_handlers.sw_chld.start (SIGCHLD); |
|
|
649 | sig_handlers.sw_term.start (SIGTERM); |
586 | sig_handlers.sw_term.start (SIGTERM); |
650 | sig_handlers.sw_int.start (SIGINT); |
587 | sig_handlers.sw_int.start (SIGINT); |
651 | |
588 | |
652 | /* need to trap SIGURG for SVR4 (Unixware) rlogin */ |
589 | /* need to trap SIGURG for SVR4 (Unixware) rlogin */ |
653 | /* signal (SIGURG, SIG_DFL); */ |
590 | /* signal (SIGURG, SIG_DFL); */ |
… | |
… | |
682 | rxvt_fatal ("memory allocation failure. aborting.\n"); |
619 | rxvt_fatal ("memory allocation failure. aborting.\n"); |
683 | |
620 | |
684 | return p; |
621 | return p; |
685 | } |
622 | } |
686 | |
623 | |
687 | void * |
624 | void * |
688 | rxvt_realloc (void *ptr, size_t size) |
625 | rxvt_realloc (void *ptr, size_t size) |
689 | { |
626 | { |
690 | void *p = realloc (ptr, size); |
627 | void *p = realloc (ptr, size); |
691 | |
628 | |
692 | if (!p) |
629 | if (!p) |
693 | rxvt_fatal ("memory allocation failure. aborting.\n"); |
630 | rxvt_fatal ("memory allocation failure. aborting.\n"); |
694 | |
631 | |
695 | return p; |
632 | return p; |
696 | } |
|
|
697 | |
|
|
698 | /* ------------------------------------------------------------------------- * |
|
|
699 | * PRIVILEGED OPERATIONS * |
|
|
700 | * ------------------------------------------------------------------------- */ |
|
|
701 | /* take care of suid/sgid super-user (root) privileges */ |
|
|
702 | void |
|
|
703 | rxvt_privileges (rxvt_privaction action) |
|
|
704 | { |
|
|
705 | #if ! defined(__CYGWIN32__) |
|
|
706 | # if !defined(HAVE_SETEUID) && defined(HAVE_SETREUID) |
|
|
707 | /* setreuid () is the poor man's setuid (), seteuid () */ |
|
|
708 | # define seteuid(a) setreuid(-1, (a)) |
|
|
709 | # define setegid(a) setregid(-1, (a)) |
|
|
710 | # define HAVE_SETEUID |
|
|
711 | # endif |
|
|
712 | # ifdef HAVE_SETEUID |
|
|
713 | switch (action) |
|
|
714 | { |
|
|
715 | case IGNORE: |
|
|
716 | /* |
|
|
717 | * change effective uid/gid - not real uid/gid - so we can switch |
|
|
718 | * back to root later, as required |
|
|
719 | */ |
|
|
720 | setegid (getgid ()); |
|
|
721 | seteuid (getuid ()); |
|
|
722 | break; |
|
|
723 | case SAVE: |
|
|
724 | saved_egid = getegid (); |
|
|
725 | saved_euid = geteuid (); |
|
|
726 | break; |
|
|
727 | case RESTORE: |
|
|
728 | setegid (saved_egid); |
|
|
729 | seteuid (saved_euid); |
|
|
730 | break; |
|
|
731 | } |
|
|
732 | # else |
|
|
733 | switch (action) |
|
|
734 | { |
|
|
735 | case IGNORE: |
|
|
736 | setgid (getgid ()); |
|
|
737 | setuid (getuid ()); |
|
|
738 | /* FALLTHROUGH */ |
|
|
739 | case SAVE: |
|
|
740 | /* FALLTHROUGH */ |
|
|
741 | case RESTORE: |
|
|
742 | break; |
|
|
743 | } |
|
|
744 | # endif |
|
|
745 | #endif |
|
|
746 | } |
633 | } |
747 | |
634 | |
748 | /*----------------------------------------------------------------------*/ |
635 | /*----------------------------------------------------------------------*/ |
749 | /* |
636 | /* |
750 | * window size/position calculcations for XSizeHint and other storage. |
637 | * window size/position calculcations for XSizeHint and other storage. |
… | |
… | |
1690 | XFree (preedit_attr); |
1577 | XFree (preedit_attr); |
1691 | XFree (status_attr); |
1578 | XFree (status_attr); |
1692 | } |
1579 | } |
1693 | #endif /* USE_XIM */ |
1580 | #endif /* USE_XIM */ |
1694 | |
1581 | |
1695 | /*----------------------------------------------------------------------*/ |
|
|
1696 | rxvt_t rxvt_current_term; |
|
|
1697 | |
|
|
1698 | /*----------------------- end-of-file (C source) -----------------------*/ |
1582 | /*----------------------- end-of-file (C source) -----------------------*/ |