… | |
… | |
22 | *---------------------------------------------------------------------*/ |
22 | *---------------------------------------------------------------------*/ |
23 | |
23 | |
24 | #include "../config.h" /* NECESSARY */ |
24 | #include "../config.h" /* NECESSARY */ |
25 | #include "rxvt.h" |
25 | #include "rxvt.h" |
26 | |
26 | |
27 | # include <cstdlib> |
27 | #include <cstdlib> |
28 | # include <cstring> |
28 | #include <cstring> |
29 | |
29 | |
30 | #ifdef HAVE_SYS_TYPES_H |
|
|
31 | # include <sys/types.h> |
30 | #include <sys/types.h> |
32 | #endif |
31 | #include <sys/socket.h> |
33 | #ifdef HAVE_UNISTD_H |
|
|
34 | # include <unistd.h> |
32 | #include <unistd.h> |
35 | #endif |
|
|
36 | #ifdef HAVE_FCNTL_H |
|
|
37 | # include <fcntl.h> |
33 | #include <fcntl.h> |
38 | #endif |
34 | |
39 | #ifdef HAVE_SYS_IOCTL_H |
35 | #ifdef HAVE_SYS_IOCTL_H |
40 | # include <sys/ioctl.h> |
36 | # include <sys/ioctl.h> |
41 | #endif |
37 | #endif |
42 | #if defined(PTYS_ARE_PTMX) && defined(HAVE_SYS_STROPTS_H) |
38 | #if defined(PTYS_ARE_PTMX) && defined(HAVE_SYS_STROPTS_H) |
43 | # include <sys/stropts.h> /* for I_PUSH */ |
39 | # include <sys/stropts.h> /* for I_PUSH */ |
… | |
… | |
54 | #endif |
50 | #endif |
55 | |
51 | |
56 | #include <cstdio> |
52 | #include <cstdio> |
57 | #include <grp.h> |
53 | #include <grp.h> |
58 | |
54 | |
|
|
55 | #include "rxvtutil.h" |
|
|
56 | #include "fdpass.h" |
59 | #include "ptytty.h" |
57 | #include "ptytty.h" |
60 | |
58 | |
61 | ///////////////////////////////////////////////////////////////////////////// |
59 | ///////////////////////////////////////////////////////////////////////////// |
62 | |
60 | |
63 | /* ------------------------------------------------------------------------- * |
61 | /* ------------------------------------------------------------------------- * |
… | |
… | |
74 | int pfd; |
72 | int pfd; |
75 | |
73 | |
76 | #ifdef PTYS_ARE_OPENPTY |
74 | #ifdef PTYS_ARE_OPENPTY |
77 | char tty_name[sizeof "/dev/pts/????\0"]; |
75 | char tty_name[sizeof "/dev/pts/????\0"]; |
78 | |
76 | |
79 | rxvt_privileges(RESTORE); |
|
|
80 | int res = openpty (&pfd, fd_tty, tty_name, NULL, NULL); |
77 | int res = openpty (&pfd, fd_tty, tty_name, NULL, NULL); |
81 | rxvt_privileges(IGNORE); |
|
|
82 | |
78 | |
83 | if (res != -1) |
79 | if (res != -1) |
84 | { |
80 | { |
85 | *ttydev = strdup (tty_name); |
81 | *ttydev = strdup (tty_name); |
86 | return pfd; |
82 | return pfd; |
… | |
… | |
346 | rxvt_ptytty_unix::privileges (rxvt_privaction action) |
342 | rxvt_ptytty_unix::privileges (rxvt_privaction action) |
347 | { |
343 | { |
348 | if (!name || !*name) |
344 | if (!name || !*name) |
349 | return; |
345 | return; |
350 | |
346 | |
351 | rxvt_privileges (RESTORE); |
|
|
352 | |
|
|
353 | if (action == SAVE) |
347 | if (action == SAVE) |
354 | { |
348 | { |
355 | # ifndef RESET_TTY_TO_COMMON_DEFAULTS |
349 | # ifndef RESET_TTY_TO_COMMON_DEFAULTS |
356 | /* store original tty status for restoration rxvt_clean_exit () -- rgg 04/12/95 */ |
350 | /* store original tty status for restoration rxvt_clean_exit () -- rgg 04/12/95 */ |
357 | if (lstat (name, &savestat) < 0) /* you lose out */ |
351 | if (lstat (name, &savestat) < 0) /* you lose out */ |
… | |
… | |
379 | chmod (name, (S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH)); |
373 | chmod (name, (S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH)); |
380 | chown (name, 0, 0); |
374 | chown (name, 0, 0); |
381 | # endif |
375 | # endif |
382 | |
376 | |
383 | } |
377 | } |
384 | |
|
|
385 | rxvt_privileges (IGNORE); |
|
|
386 | } |
378 | } |
387 | #endif |
379 | #endif |
388 | |
380 | |
389 | rxvt_ptytty_unix::rxvt_ptytty_unix () |
381 | rxvt_ptytty_unix::rxvt_ptytty_unix () |
390 | { |
382 | { |
… | |
… | |
398 | #endif |
390 | #endif |
399 | } |
391 | } |
400 | |
392 | |
401 | rxvt_ptytty_unix::~rxvt_ptytty_unix () |
393 | rxvt_ptytty_unix::~rxvt_ptytty_unix () |
402 | { |
394 | { |
|
|
395 | #if UTMP_SUPPORT |
403 | logout (); |
396 | logout (); |
|
|
397 | #endif |
404 | put (); |
398 | put (); |
405 | } |
399 | } |
406 | |
400 | |
407 | void |
401 | void |
408 | rxvt_ptytty_unix::put () |
402 | rxvt_ptytty_unix::put () |
… | |
… | |
443 | } |
437 | } |
444 | |
438 | |
445 | return true; |
439 | return true; |
446 | } |
440 | } |
447 | |
441 | |
|
|
442 | #if PTYTTY_HELPER |
|
|
443 | |
|
|
444 | static int sock_fd; |
|
|
445 | static int pid; |
|
|
446 | |
|
|
447 | struct command |
|
|
448 | { |
|
|
449 | enum { get, login, destroy } type; |
|
|
450 | |
|
|
451 | rxvt_ptytty *id; |
|
|
452 | |
|
|
453 | bool login_shell; |
|
|
454 | int cmd_pid; |
|
|
455 | char hostname[512]; // arbitrary, but should be plenty |
|
|
456 | }; |
|
|
457 | |
|
|
458 | struct rxvt_ptytty_proxy : zero_initialized, rxvt_ptytty |
|
|
459 | { |
|
|
460 | rxvt_ptytty *id; |
|
|
461 | |
|
|
462 | ~rxvt_ptytty_proxy (); |
|
|
463 | |
|
|
464 | bool get (); |
|
|
465 | void login (int cmd_pid, bool login_shell, const char *hostname); |
|
|
466 | }; |
|
|
467 | |
|
|
468 | bool |
|
|
469 | rxvt_ptytty_proxy::get () |
|
|
470 | { |
|
|
471 | command cmd; |
|
|
472 | |
|
|
473 | cmd.type = command::get; |
|
|
474 | |
|
|
475 | write (sock_fd, &cmd, sizeof (cmd)); |
|
|
476 | |
|
|
477 | if (read (sock_fd, &id, sizeof (id)) != sizeof (id)) |
|
|
478 | rxvt_fatal ("protocol error while creating pty using helper process, aborting.\n"); |
|
|
479 | |
|
|
480 | if (!id) |
|
|
481 | return false; |
|
|
482 | |
|
|
483 | if ((pty = rxvt_recv_fd (sock_fd)) < 0 |
|
|
484 | || (tty = rxvt_recv_fd (sock_fd)) < 0) |
|
|
485 | rxvt_fatal ("protocol error while reading pty/tty fds from helper process, aborting.\n"); |
|
|
486 | |
|
|
487 | return true; |
|
|
488 | } |
|
|
489 | |
|
|
490 | void |
|
|
491 | rxvt_ptytty_proxy::login (int cmd_pid, bool login_shell, const char *hostname) |
|
|
492 | { |
|
|
493 | command cmd; |
|
|
494 | |
|
|
495 | cmd.type = command::login; |
|
|
496 | cmd.id = id; |
|
|
497 | cmd.cmd_pid = cmd_pid; |
|
|
498 | cmd.login_shell = login_shell; |
|
|
499 | strncpy (cmd.hostname, hostname, sizeof (cmd.hostname)); |
|
|
500 | |
|
|
501 | write (sock_fd, &cmd, sizeof (cmd)); |
|
|
502 | } |
|
|
503 | |
|
|
504 | rxvt_ptytty_proxy::~rxvt_ptytty_proxy () |
|
|
505 | { |
|
|
506 | command cmd; |
|
|
507 | |
|
|
508 | cmd.type = command::destroy; |
|
|
509 | cmd.id = id; |
|
|
510 | |
|
|
511 | write (sock_fd, &cmd, sizeof (cmd)); |
|
|
512 | } |
|
|
513 | |
|
|
514 | static |
|
|
515 | void serve () |
|
|
516 | { |
|
|
517 | command cmd; |
|
|
518 | vector<rxvt_ptytty *> ptys; |
|
|
519 | |
|
|
520 | while (read (sock_fd, &cmd, sizeof (command)) == sizeof (command)) |
|
|
521 | { |
|
|
522 | if (cmd.type == command::get) |
|
|
523 | { |
|
|
524 | // -> id ptyfd ttyfd |
|
|
525 | cmd.id = new rxvt_ptytty_unix; |
|
|
526 | |
|
|
527 | if (cmd.id->get ()) |
|
|
528 | { |
|
|
529 | write (sock_fd, &cmd.id, sizeof (cmd.id)); |
|
|
530 | ptys.push_back (cmd.id); |
|
|
531 | |
|
|
532 | rxvt_send_fd (sock_fd, cmd.id->pty); |
|
|
533 | rxvt_send_fd (sock_fd, cmd.id->tty); |
|
|
534 | } |
|
|
535 | else |
|
|
536 | { |
|
|
537 | delete cmd.id; |
|
|
538 | cmd.id = 0; |
|
|
539 | write (sock_fd, &cmd.id, sizeof (cmd.id)); |
|
|
540 | } |
|
|
541 | } |
|
|
542 | else if (cmd.type == command::login) |
|
|
543 | { |
|
|
544 | #if UTMP_SUPPORT |
|
|
545 | if (find (ptys.begin (), ptys.end (), cmd.id)) |
|
|
546 | { |
|
|
547 | cmd.hostname[sizeof (cmd.hostname) - 1] = 0; |
|
|
548 | cmd.id->login (cmd.cmd_pid, cmd.login_shell, cmd.hostname); |
|
|
549 | } |
|
|
550 | #endif |
|
|
551 | } |
|
|
552 | else if (cmd.type == command::destroy) |
|
|
553 | { |
|
|
554 | rxvt_ptytty **pty = find (ptys.begin (), ptys.end (), cmd.id); |
|
|
555 | |
|
|
556 | if (pty) |
|
|
557 | { |
|
|
558 | delete *pty; |
|
|
559 | ptys.erase (pty); |
|
|
560 | } |
|
|
561 | } |
|
|
562 | else |
|
|
563 | break; |
|
|
564 | } |
|
|
565 | |
|
|
566 | // destroy all ptys |
|
|
567 | for (rxvt_ptytty **i = ptys.end (); i-- > ptys.begin (); ) |
|
|
568 | delete *i; |
|
|
569 | } |
|
|
570 | |
|
|
571 | void rxvt_ptytty_server () |
|
|
572 | { |
|
|
573 | int sv[2]; |
|
|
574 | |
|
|
575 | if (socketpair (AF_UNIX, SOCK_STREAM, 0, sv)) |
|
|
576 | rxvt_fatal ("could not create socket to communicate with pty/sessiondb helper, aborting.\n"); |
|
|
577 | |
|
|
578 | pid = fork (); |
|
|
579 | |
|
|
580 | if (pid < 0) |
|
|
581 | rxvt_fatal ("could not create pty/sessiondb helper process, aborting.\n"); |
|
|
582 | |
|
|
583 | if (pid) |
|
|
584 | { |
|
|
585 | // client, urxvt |
|
|
586 | sock_fd = sv[0]; |
|
|
587 | close (sv[1]); |
|
|
588 | fcntl (sock_fd, F_SETFD, FD_CLOEXEC); |
|
|
589 | } |
|
|
590 | else |
|
|
591 | { |
|
|
592 | setgid (getegid ()); |
|
|
593 | setuid (geteuid ()); |
|
|
594 | |
|
|
595 | // server, pty-helper |
|
|
596 | sock_fd = sv[1]; |
|
|
597 | |
|
|
598 | for (int fd = 0; fd < 1023; fd++) |
|
|
599 | if (fd != sock_fd && fd != 1) |
|
|
600 | close (fd); |
|
|
601 | |
|
|
602 | serve (); |
|
|
603 | _exit (EXIT_SUCCESS); |
|
|
604 | } |
|
|
605 | } |
|
|
606 | #endif |
|
|
607 | |
448 | // a "factory" *g* |
608 | // a "factory" *g* |
449 | rxvt_ptytty *rxvt_new_ptytty () |
609 | rxvt_ptytty * |
|
|
610 | rxvt_new_ptytty () |
450 | { |
611 | { |
|
|
612 | #if PTYTTY_HELPER |
|
|
613 | if (pid > 0) |
|
|
614 | // use helper process |
|
|
615 | return new rxvt_ptytty_proxy; |
|
|
616 | else |
|
|
617 | #endif |
451 | return new rxvt_ptytty_unix; |
618 | return new rxvt_ptytty_unix; |
452 | } |
619 | } |
453 | |
620 | |
454 | /*----------------------- end-of-file (C source) -----------------------*/ |
621 | /*----------------------- end-of-file (C source) -----------------------*/ |
455 | |
622 | |