… | |
… | |
6 | *----------------------------------------------------------------------* |
6 | *----------------------------------------------------------------------* |
7 | * |
7 | * |
8 | * All portions of code are copyright by their respective author/s. |
8 | * All portions of code are copyright by their respective author/s. |
9 | * Copyright (c) 1999-2001 Geoff Wing <gcw@pobox.com> |
9 | * Copyright (c) 1999-2001 Geoff Wing <gcw@pobox.com> |
10 | * Copyright (c) 2004-2006 Marc Lehmann <pcg@goof.com> |
10 | * Copyright (c) 2004-2006 Marc Lehmann <pcg@goof.com> |
|
|
11 | * Copyright (c) 2006 Emanuele Giaquinta <e.giaquinta@glauco.it> |
11 | * |
12 | * |
12 | * This program is free software; you can redistribute it and/or modify |
13 | * This program is free software; you can redistribute it and/or modify |
13 | * it under the terms of the GNU General Public License as published by |
14 | * it under the terms of the GNU General Public License as published by |
14 | * the Free Software Foundation; either version 2 of the License, or |
15 | * the Free Software Foundation; either version 2 of the License, or |
15 | * (at your option) any later version. |
16 | * (at your option) any later version. |
… | |
… | |
24 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
25 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
25 | *---------------------------------------------------------------------*/ |
26 | *---------------------------------------------------------------------*/ |
26 | |
27 | |
27 | #include "../config.h" |
28 | #include "../config.h" |
28 | |
29 | |
29 | #include "fdpass.h" |
|
|
30 | #include "ptytty.h" |
30 | #include "ptytty.h" |
31 | |
31 | |
32 | #include <cstdlib> |
32 | #include <cstdlib> |
33 | #include <cstring> |
33 | #include <cstring> |
|
|
34 | #include <csignal> |
34 | |
35 | |
35 | #include <sys/types.h> |
36 | #include <sys/types.h> |
36 | #include <sys/socket.h> |
37 | #include <sys/socket.h> |
37 | #include <unistd.h> |
38 | #include <unistd.h> |
38 | #include <fcntl.h> |
39 | #include <fcntl.h> |
… | |
… | |
235 | |
236 | |
236 | ioctl (fd_tty, TIOCSCTTY, NULL); |
237 | ioctl (fd_tty, TIOCSCTTY, NULL); |
237 | |
238 | |
238 | int fd = open ("/dev/tty", O_WRONLY); |
239 | int fd = open ("/dev/tty", O_WRONLY); |
239 | if (fd < 0) |
240 | if (fd < 0) |
240 | return -1; /* fatal */ |
241 | return -1; /* fatal */ |
241 | |
242 | |
242 | close (fd); |
243 | close (fd); |
243 | |
244 | |
244 | return 0; |
245 | return 0; |
245 | } |
246 | } |
… | |
… | |
371 | } |
372 | } |
372 | |
373 | |
373 | return true; |
374 | return true; |
374 | } |
375 | } |
375 | |
376 | |
|
|
377 | ///////////////////////////////////////////////////////////////////////////// |
|
|
378 | // helper/proxy support |
|
|
379 | |
376 | #if PTYTTY_HELPER |
380 | #if PTYTTY_HELPER |
377 | |
381 | |
378 | static int sock_fd; |
382 | static int sock_fd = -1, lock_fd = -1; |
379 | static int pid; |
383 | static int helper_pid, owner_pid; |
380 | |
384 | |
381 | struct command |
385 | struct command |
382 | { |
386 | { |
383 | enum { get, login, destroy } type; |
387 | enum { get, login, destroy } type; |
384 | |
388 | |
… | |
… | |
412 | cmd.type = command::get; |
416 | cmd.type = command::get; |
413 | |
417 | |
414 | write (sock_fd, &cmd, sizeof (cmd)); |
418 | write (sock_fd, &cmd, sizeof (cmd)); |
415 | |
419 | |
416 | if (read (sock_fd, &id, sizeof (id)) != sizeof (id)) |
420 | if (read (sock_fd, &id, sizeof (id)) != sizeof (id)) |
417 | fatal ("protocol error while creating pty using helper process, aborting.\n"); |
421 | ptytty_fatal ("protocol error while creating pty using helper process, aborting.\n"); |
418 | |
422 | |
419 | if (!id) |
423 | if (!id) |
420 | return false; |
424 | return false; |
421 | |
425 | |
422 | if ((pty = ptytty_recv_fd (sock_fd)) < 0 |
426 | if ((pty = recv_fd (sock_fd)) < 0 |
423 | || (tty = ptytty_recv_fd (sock_fd)) < 0) |
427 | || (tty = recv_fd (sock_fd)) < 0) |
424 | fatal ("protocol error while reading pty/tty fds from helper process, aborting.\n"); |
428 | ptytty_fatal ("protocol error while reading pty/tty fds from helper process, aborting.\n"); |
425 | |
429 | |
426 | return true; |
430 | return true; |
427 | } |
431 | } |
428 | |
432 | |
429 | void |
433 | void |
… | |
… | |
469 | if (cmd.id->get ()) |
473 | if (cmd.id->get ()) |
470 | { |
474 | { |
471 | write (sock_fd, &cmd.id, sizeof (cmd.id)); |
475 | write (sock_fd, &cmd.id, sizeof (cmd.id)); |
472 | ptys.push_back (cmd.id); |
476 | ptys.push_back (cmd.id); |
473 | |
477 | |
474 | ptytty_send_fd (sock_fd, cmd.id->pty); |
478 | ptytty::send_fd (sock_fd, cmd.id->pty); |
475 | ptytty_send_fd (sock_fd, cmd.id->tty); |
479 | ptytty::send_fd (sock_fd, cmd.id->tty); |
476 | } |
480 | } |
477 | else |
481 | else |
478 | { |
482 | { |
479 | delete cmd.id; |
483 | delete cmd.id; |
480 | cmd.id = 0; |
484 | cmd.id = 0; |
… | |
… | |
482 | } |
486 | } |
483 | } |
487 | } |
484 | else if (cmd.type == command::login) |
488 | else if (cmd.type == command::login) |
485 | { |
489 | { |
486 | #if UTMP_SUPPORT |
490 | #if UTMP_SUPPORT |
487 | if (find (ptys.begin (), ptys.end (), cmd.id)) |
491 | if (find (ptys.begin (), ptys.end (), cmd.id) != ptys.end ()) |
488 | { |
492 | { |
489 | cmd.hostname[sizeof (cmd.hostname) - 1] = 0; |
493 | cmd.hostname[sizeof (cmd.hostname) - 1] = 0; |
490 | cmd.id->login (cmd.cmd_pid, cmd.login_shell, cmd.hostname); |
494 | cmd.id->login (cmd.cmd_pid, cmd.login_shell, cmd.hostname); |
491 | } |
495 | } |
492 | #endif |
496 | #endif |
493 | } |
497 | } |
494 | else if (cmd.type == command::destroy) |
498 | else if (cmd.type == command::destroy) |
495 | { |
499 | { |
496 | ptytty **pty = find (ptys.begin (), ptys.end (), cmd.id); |
500 | vector<ptytty *>::iterator pty = find (ptys.begin (), ptys.end (), cmd.id); |
497 | |
501 | |
498 | if (pty) |
502 | if (pty != ptys.end ()) |
499 | { |
503 | { |
500 | delete *pty; |
504 | delete *pty; |
501 | ptys.erase (pty); |
505 | ptys.erase (pty); |
502 | } |
506 | } |
503 | } |
507 | } |
504 | else |
508 | else |
505 | break; |
509 | break; |
506 | } |
510 | } |
507 | |
511 | |
508 | // destroy all ptys |
512 | // destroy all ptys |
509 | for (ptytty **i = ptys.end (); i-- > ptys.begin (); ) |
513 | for (vector<ptytty *>::iterator i = ptys.end (); i-- > ptys.begin (); ) |
510 | delete *i; |
514 | delete *i; |
511 | } |
515 | } |
512 | |
516 | |
513 | void ptytty_server () |
517 | void |
|
|
518 | ptytty::use_helper () |
514 | { |
519 | { |
|
|
520 | int pid = getpid (); |
|
|
521 | |
|
|
522 | if (sock_fd >= 0 && pid == owner_pid) |
|
|
523 | return; |
|
|
524 | |
|
|
525 | owner_pid = pid; |
|
|
526 | |
515 | int sv[2]; |
527 | int sv[2]; |
516 | |
528 | |
517 | if (socketpair (AF_UNIX, SOCK_STREAM, 0, sv)) |
529 | if (socketpair (AF_UNIX, SOCK_STREAM, 0, sv)) |
518 | fatal ("could not create socket to communicate with pty/sessiondb helper, aborting.\n"); |
530 | ptytty_fatal ("could not create socket to communicate with pty/sessiondb helper, aborting.\n"); |
519 | |
531 | |
|
|
532 | #ifdef PTYTTY_REENTRANT |
|
|
533 | int lv[2]; |
|
|
534 | |
|
|
535 | if (socketpair (AF_UNIX, SOCK_STREAM, 0, lv)) |
|
|
536 | ptytty_fatal ("could not create socket to communicate with pty/sessiondb helper, aborting.\n"); |
|
|
537 | #endif |
|
|
538 | |
520 | pid = fork (); |
539 | helper_pid = fork (); |
521 | |
540 | |
522 | if (pid < 0) |
541 | if (helper_pid < 0) |
523 | fatal ("could not create pty/sessiondb helper process, aborting.\n"); |
542 | ptytty_fatal ("could not create pty/sessiondb helper process, aborting.\n"); |
524 | |
543 | |
525 | if (pid) |
544 | if (helper_pid) |
526 | { |
545 | { |
527 | // client, process |
546 | // client, process |
528 | sock_fd = sv[0]; |
547 | sock_fd = sv[0]; |
529 | close (sv[1]); |
548 | close (sv[1]); |
530 | fcntl (sock_fd, F_SETFD, FD_CLOEXEC); |
549 | fcntl (sock_fd, F_SETFD, FD_CLOEXEC); |
|
|
550 | #ifdef PTYTTY_REENTRANT |
|
|
551 | lock_fd = lv[0]; |
|
|
552 | close (lv[1]); |
|
|
553 | fcntl (lock_fd, F_SETFD, FD_CLOEXEC); |
|
|
554 | #endif |
531 | } |
555 | } |
532 | else |
556 | else |
533 | { |
557 | { |
534 | // server, pty-helper |
558 | // server, pty-helper |
535 | sock_fd = sv[1]; |
559 | sock_fd = sv[1]; |
|
|
560 | #ifdef PTYTTY_REENTRANT |
|
|
561 | lock_fd = lv[1]; |
|
|
562 | #endif |
|
|
563 | |
|
|
564 | chdir ("/"); |
|
|
565 | |
|
|
566 | signal (SIGHUP, SIG_IGN); |
|
|
567 | signal (SIGTERM, SIG_IGN); |
|
|
568 | signal (SIGINT, SIG_IGN); |
|
|
569 | signal (SIGPIPE, SIG_IGN); |
536 | |
570 | |
537 | for (int fd = 0; fd < 1023; fd++) |
571 | for (int fd = 0; fd < 1023; fd++) |
538 | if (fd != sock_fd) |
572 | if (fd != sock_fd && fd != lock_fd) |
539 | close (fd); |
573 | close (fd); |
540 | |
574 | |
541 | serve (); |
575 | serve (); |
542 | _exit (EXIT_SUCCESS); |
576 | _exit (EXIT_SUCCESS); |
543 | } |
577 | } |
544 | } |
578 | } |
545 | |
579 | |
546 | #endif |
580 | #endif |
547 | |
581 | |
548 | // a "factory" *g* |
|
|
549 | ptytty * |
582 | ptytty * |
550 | new_ptytty () |
583 | ptytty::create () |
551 | { |
584 | { |
552 | #if PTYTTY_HELPER |
585 | #if PTYTTY_HELPER |
553 | if (pid > 0) |
586 | if (helper_pid && getpid () == owner_pid) |
554 | // use helper process |
587 | // use helper process |
555 | return new ptytty_proxy; |
588 | return new ptytty_proxy; |
556 | else |
589 | else |
557 | #endif |
590 | #endif |
558 | return new ptytty_unix; |
591 | return new ptytty_unix; |
559 | } |
592 | } |
560 | |
593 | |
561 | /*----------------------- end-of-file (C source) -----------------------*/ |
594 | void |
|
|
595 | ptytty::init () |
|
|
596 | { |
|
|
597 | uid_t uid = getuid (); |
|
|
598 | gid_t gid = getgid (); |
|
|
599 | |
|
|
600 | // before doing anything else, check for setuid/setgid operation, |
|
|
601 | // start the helper process and drop privileges |
|
|
602 | if (uid != geteuid () |
|
|
603 | || gid != getegid ()) |
|
|
604 | { |
|
|
605 | #if PTYTTY_HELPER |
|
|
606 | use_helper (); |
|
|
607 | #else |
|
|
608 | ptytty_warn ("running setuid/setgid without pty helper compiled in, continuing unprivileged.\n"); |
|
|
609 | #endif |
562 | |
610 | |
|
|
611 | drop_privileges (); |
|
|
612 | } |
|
|
613 | } |
|
|
614 | |
|
|
615 | void |
|
|
616 | ptytty::drop_privileges () |
|
|
617 | { |
|
|
618 | uid_t uid = getuid (); |
|
|
619 | gid_t gid = getgid (); |
|
|
620 | |
|
|
621 | // drop privileges |
|
|
622 | #if HAVE_SETRESUID |
|
|
623 | setresgid (gid, gid, gid); |
|
|
624 | setresuid (uid, uid, uid); |
|
|
625 | #elif HAVE_SETREUID |
|
|
626 | setregid (gid, gid); |
|
|
627 | setreuid (uid, uid); |
|
|
628 | #elif HAVE_SETUID |
|
|
629 | setgid (gid); |
|
|
630 | setuid (uid); |
|
|
631 | #endif |
|
|
632 | |
|
|
633 | if (uid != geteuid () |
|
|
634 | || gid != getegid ()) |
|
|
635 | ptytty_fatal ("unable to drop privileges, aborting.\n"); |
|
|
636 | } |
|
|
637 | |
|
|
638 | ///////////////////////////////////////////////////////////////////////////// |
|
|
639 | // C API |
|
|
640 | |
|
|
641 | #ifndef NO_C_API |
|
|
642 | |
|
|
643 | #define DEFINE_METHOD(retval, name, args1, args2) \ |
|
|
644 | extern "C" retval ptytty_ ## name args1 \ |
|
|
645 | { return ((struct ptytty *)ptytty)->name args2; } |
|
|
646 | |
|
|
647 | DEFINE_METHOD(int,pty,(void *ptytty),) |
|
|
648 | DEFINE_METHOD(int,tty,(void *ptytty),) |
|
|
649 | DEFINE_METHOD(int,get,(void *ptytty),()) |
|
|
650 | DEFINE_METHOD(void,login,(void *ptytty, int cmd_pid, bool login_shell, const char *hostname),(cmd_pid,login_shell,hostname)) |
|
|
651 | |
|
|
652 | DEFINE_METHOD(void,close_tty,(void *ptytty),()) |
|
|
653 | DEFINE_METHOD(int,make_controlling_tty,(void *ptytty),()) |
|
|
654 | DEFINE_METHOD(void,set_utf8_mode,(void *ptytty, int on),(on)) |
|
|
655 | |
|
|
656 | #define DEFINE_STATIC(retval, name, args) \ |
|
|
657 | extern "C" retval ptytty_ ## name args \ |
|
|
658 | { return ptytty::name args; } |
|
|
659 | |
|
|
660 | DEFINE_STATIC(void,drop_privileges,()) |
|
|
661 | DEFINE_STATIC(void,use_helper,()) |
|
|
662 | DEFINE_STATIC(void,init,()) |
|
|
663 | |
|
|
664 | DEFINE_STATIC(void *,create,()) |
|
|
665 | |
|
|
666 | void ptytty_delete (void *ptytty) |
|
|
667 | { |
|
|
668 | delete (struct ptytty *)ptytty; |
|
|
669 | } |
|
|
670 | |
|
|
671 | // send_fd, recv_fd not exposed |
|
|
672 | |
|
|
673 | #endif |