|
|
1 | // This file is part of libptytty. Do not make local modifications. |
|
|
2 | // http://software.schmorp.de/pkg/libptytty |
|
|
3 | |
1 | /*--------------------------------*-C-*---------------------------------* |
4 | /*----------------------------------------------------------------------* |
2 | * File: ptytty.C |
5 | * File: ptytty.C |
3 | *----------------------------------------------------------------------* |
6 | *----------------------------------------------------------------------* |
4 | * |
7 | * |
5 | * All portions of code are copyright by their respective author/s. |
8 | * All portions of code are copyright by their respective author/s. |
6 | * Copyright (c) 1999-2001 Geoff Wing <gcw@pobox.com> |
9 | * Copyright (c) 1999-2001 Geoff Wing <gcw@pobox.com> |
7 | * 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> |
8 | * |
12 | * |
9 | * 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 |
10 | * 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 |
11 | * the Free Software Foundation; either version 2 of the License, or |
15 | * the Free Software Foundation; either version 2 of the License, or |
12 | * (at your option) any later version. |
16 | * (at your option) any later version. |
… | |
… | |
19 | * You should have received a copy of the GNU General Public License |
23 | * You should have received a copy of the GNU General Public License |
20 | * along with this program; if not, write to the Free Software |
24 | * along with this program; if not, write to the Free Software |
21 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
25 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
22 | *---------------------------------------------------------------------*/ |
26 | *---------------------------------------------------------------------*/ |
23 | |
27 | |
24 | #include "../config.h" /* NECESSARY */ |
28 | #include "../config.h" |
25 | |
29 | |
26 | #include "fdpass.h" |
|
|
27 | #include "ptytty.h" |
30 | #include "ptytty.h" |
28 | |
31 | |
29 | #include <cstdlib> |
32 | #include <cstdlib> |
30 | #include <cstring> |
33 | #include <cstring> |
|
|
34 | #include <csignal> |
31 | |
35 | |
32 | #include <sys/types.h> |
36 | #include <sys/types.h> |
33 | #include <sys/socket.h> |
37 | #include <sys/socket.h> |
34 | #include <unistd.h> |
38 | #include <unistd.h> |
35 | #include <fcntl.h> |
39 | #include <fcntl.h> |
… | |
… | |
232 | |
236 | |
233 | ioctl (fd_tty, TIOCSCTTY, NULL); |
237 | ioctl (fd_tty, TIOCSCTTY, NULL); |
234 | |
238 | |
235 | int fd = open ("/dev/tty", O_WRONLY); |
239 | int fd = open ("/dev/tty", O_WRONLY); |
236 | if (fd < 0) |
240 | if (fd < 0) |
237 | return -1; /* fatal */ |
241 | return -1; /* fatal */ |
238 | |
242 | |
239 | close (fd); |
243 | close (fd); |
240 | |
244 | |
241 | return 0; |
245 | return 0; |
242 | } |
246 | } |
… | |
… | |
368 | } |
372 | } |
369 | |
373 | |
370 | return true; |
374 | return true; |
371 | } |
375 | } |
372 | |
376 | |
|
|
377 | ///////////////////////////////////////////////////////////////////////////// |
|
|
378 | // helper/proxy support |
|
|
379 | |
373 | #if PTYTTY_HELPER |
380 | #if PTYTTY_HELPER |
374 | |
381 | |
375 | static int sock_fd; |
382 | static int sock_fd = -1, lock_fd = -1; |
376 | static int pid; |
383 | static int helper_pid, owner_pid; |
377 | |
384 | |
378 | struct command |
385 | struct command |
379 | { |
386 | { |
380 | enum { get, login, destroy } type; |
387 | enum { get, login, destroy } type; |
381 | |
388 | |
… | |
… | |
384 | bool login_shell; |
391 | bool login_shell; |
385 | int cmd_pid; |
392 | int cmd_pid; |
386 | char hostname[512]; // arbitrary, but should be plenty |
393 | char hostname[512]; // arbitrary, but should be plenty |
387 | }; |
394 | }; |
388 | |
395 | |
389 | struct ptytty_proxy : zero_initialized, ptytty |
396 | struct ptytty_proxy : ptytty |
390 | { |
397 | { |
391 | ptytty *id; |
398 | ptytty *id; |
|
|
399 | |
|
|
400 | ptytty_proxy () |
|
|
401 | : id(0) |
|
|
402 | { |
|
|
403 | } |
392 | |
404 | |
393 | ~ptytty_proxy (); |
405 | ~ptytty_proxy (); |
394 | |
406 | |
395 | bool get (); |
407 | bool get (); |
396 | void login (int cmd_pid, bool login_shell, const char *hostname); |
408 | void login (int cmd_pid, bool login_shell, const char *hostname); |
… | |
… | |
404 | cmd.type = command::get; |
416 | cmd.type = command::get; |
405 | |
417 | |
406 | write (sock_fd, &cmd, sizeof (cmd)); |
418 | write (sock_fd, &cmd, sizeof (cmd)); |
407 | |
419 | |
408 | if (read (sock_fd, &id, sizeof (id)) != sizeof (id)) |
420 | if (read (sock_fd, &id, sizeof (id)) != sizeof (id)) |
409 | fatal ("protocol error while creating pty using helper process, aborting.\n"); |
421 | ptytty_fatal ("protocol error while creating pty using helper process, aborting.\n"); |
410 | |
422 | |
411 | if (!id) |
423 | if (!id) |
412 | return false; |
424 | return false; |
413 | |
425 | |
414 | if ((pty = ptytty_recv_fd (sock_fd)) < 0 |
426 | if ((pty = recv_fd (sock_fd)) < 0 |
415 | || (tty = ptytty_recv_fd (sock_fd)) < 0) |
427 | || (tty = recv_fd (sock_fd)) < 0) |
416 | 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"); |
417 | |
429 | |
418 | return true; |
430 | return true; |
419 | } |
431 | } |
420 | |
432 | |
421 | void |
433 | void |
… | |
… | |
432 | write (sock_fd, &cmd, sizeof (cmd)); |
444 | write (sock_fd, &cmd, sizeof (cmd)); |
433 | } |
445 | } |
434 | |
446 | |
435 | ptytty_proxy::~ptytty_proxy () |
447 | ptytty_proxy::~ptytty_proxy () |
436 | { |
448 | { |
|
|
449 | if (id) |
|
|
450 | { |
437 | command cmd; |
451 | command cmd; |
438 | |
452 | |
439 | cmd.type = command::destroy; |
453 | cmd.type = command::destroy; |
440 | cmd.id = id; |
454 | cmd.id = id; |
441 | |
455 | |
442 | write (sock_fd, &cmd, sizeof (cmd)); |
456 | write (sock_fd, &cmd, sizeof (cmd)); |
|
|
457 | } |
443 | } |
458 | } |
444 | |
459 | |
445 | static |
460 | static |
446 | void serve () |
461 | void serve () |
447 | { |
462 | { |
… | |
… | |
458 | if (cmd.id->get ()) |
473 | if (cmd.id->get ()) |
459 | { |
474 | { |
460 | write (sock_fd, &cmd.id, sizeof (cmd.id)); |
475 | write (sock_fd, &cmd.id, sizeof (cmd.id)); |
461 | ptys.push_back (cmd.id); |
476 | ptys.push_back (cmd.id); |
462 | |
477 | |
463 | ptytty_send_fd (sock_fd, cmd.id->pty); |
478 | ptytty::send_fd (sock_fd, cmd.id->pty); |
464 | ptytty_send_fd (sock_fd, cmd.id->tty); |
479 | ptytty::send_fd (sock_fd, cmd.id->tty); |
465 | } |
480 | } |
466 | else |
481 | else |
467 | { |
482 | { |
468 | delete cmd.id; |
483 | delete cmd.id; |
469 | cmd.id = 0; |
484 | cmd.id = 0; |
… | |
… | |
471 | } |
486 | } |
472 | } |
487 | } |
473 | else if (cmd.type == command::login) |
488 | else if (cmd.type == command::login) |
474 | { |
489 | { |
475 | #if UTMP_SUPPORT |
490 | #if UTMP_SUPPORT |
476 | if (find (ptys.begin (), ptys.end (), cmd.id)) |
491 | if (find (ptys.begin (), ptys.end (), cmd.id) != ptys.end ()) |
477 | { |
492 | { |
478 | cmd.hostname[sizeof (cmd.hostname) - 1] = 0; |
493 | cmd.hostname[sizeof (cmd.hostname) - 1] = 0; |
479 | cmd.id->login (cmd.cmd_pid, cmd.login_shell, cmd.hostname); |
494 | cmd.id->login (cmd.cmd_pid, cmd.login_shell, cmd.hostname); |
480 | } |
495 | } |
481 | #endif |
496 | #endif |
482 | } |
497 | } |
483 | else if (cmd.type == command::destroy) |
498 | else if (cmd.type == command::destroy) |
484 | { |
499 | { |
485 | ptytty **pty = find (ptys.begin (), ptys.end (), cmd.id); |
500 | vector<ptytty *>::iterator pty = find (ptys.begin (), ptys.end (), cmd.id); |
486 | |
501 | |
487 | if (pty) |
502 | if (pty != ptys.end ()) |
488 | { |
503 | { |
489 | delete *pty; |
504 | delete *pty; |
490 | ptys.erase (pty); |
505 | ptys.erase (pty); |
491 | } |
506 | } |
492 | } |
507 | } |
493 | else |
508 | else |
494 | break; |
509 | break; |
495 | } |
510 | } |
496 | |
511 | |
497 | // destroy all ptys |
512 | // destroy all ptys |
498 | for (ptytty **i = ptys.end (); i-- > ptys.begin (); ) |
513 | for (vector<ptytty *>::iterator i = ptys.end (); i-- > ptys.begin (); ) |
499 | delete *i; |
514 | delete *i; |
500 | } |
515 | } |
501 | |
516 | |
502 | void ptytty_server () |
517 | void |
|
|
518 | ptytty::use_helper () |
503 | { |
519 | { |
|
|
520 | int pid = getpid (); |
|
|
521 | |
|
|
522 | if (sock_fd >= 0 && pid == owner_pid) |
|
|
523 | return; |
|
|
524 | |
|
|
525 | owner_pid = pid; |
|
|
526 | |
504 | int sv[2]; |
527 | int sv[2]; |
505 | |
528 | |
506 | if (socketpair (AF_UNIX, SOCK_STREAM, 0, sv)) |
529 | if (socketpair (AF_UNIX, SOCK_STREAM, 0, sv)) |
507 | 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"); |
508 | |
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 | |
509 | pid = fork (); |
539 | helper_pid = fork (); |
510 | |
540 | |
511 | if (pid < 0) |
541 | if (helper_pid < 0) |
512 | fatal ("could not create pty/sessiondb helper process, aborting.\n"); |
542 | ptytty_fatal ("could not create pty/sessiondb helper process, aborting.\n"); |
513 | |
543 | |
514 | if (pid) |
544 | if (helper_pid) |
515 | { |
545 | { |
516 | // client, process |
546 | // client, process |
517 | sock_fd = sv[0]; |
547 | sock_fd = sv[0]; |
518 | close (sv[1]); |
548 | close (sv[1]); |
519 | 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 |
520 | } |
555 | } |
521 | else |
556 | else |
522 | { |
557 | { |
523 | // server, pty-helper |
558 | // server, pty-helper |
524 | 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); |
525 | |
570 | |
526 | for (int fd = 0; fd < 1023; fd++) |
571 | for (int fd = 0; fd < 1023; fd++) |
527 | if (fd != sock_fd) |
572 | if (fd != sock_fd && fd != lock_fd) |
528 | close (fd); |
573 | close (fd); |
529 | |
574 | |
530 | serve (); |
575 | serve (); |
531 | _exit (EXIT_SUCCESS); |
576 | _exit (EXIT_SUCCESS); |
532 | } |
577 | } |
533 | } |
578 | } |
534 | |
579 | |
535 | #endif |
580 | #endif |
536 | |
581 | |
537 | // a "factory" *g* |
|
|
538 | ptytty * |
582 | ptytty * |
539 | new_ptytty () |
583 | ptytty::create () |
540 | { |
584 | { |
541 | #if PTYTTY_HELPER |
585 | #if PTYTTY_HELPER |
542 | if (pid > 0) |
586 | if (helper_pid && getpid () == owner_pid) |
543 | // use helper process |
587 | // use helper process |
544 | return new ptytty_proxy; |
588 | return new ptytty_proxy; |
545 | else |
589 | else |
546 | #endif |
590 | #endif |
547 | return new ptytty_unix; |
591 | return new ptytty_unix; |
548 | } |
592 | } |
549 | |
593 | |
550 | /*----------------------- 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 |
551 | |
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 |