|
|
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> |
… | |
… | |
19 | * You should have received a copy of the GNU General Public License |
22 | * You should have received a copy of the GNU General Public License |
20 | * along with this program; if not, write to the Free Software |
23 | * along with this program; if not, write to the Free Software |
21 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
24 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
22 | *---------------------------------------------------------------------*/ |
25 | *---------------------------------------------------------------------*/ |
23 | |
26 | |
24 | #include "../config.h" /* NECESSARY */ |
27 | #include "../config.h" |
25 | |
28 | |
26 | #include "fdpass.h" |
|
|
27 | #include "ptytty.h" |
29 | #include "ptytty.h" |
28 | |
30 | |
29 | #include <cstdlib> |
31 | #include <cstdlib> |
30 | #include <cstring> |
32 | #include <cstring> |
31 | |
33 | |
… | |
… | |
232 | |
234 | |
233 | ioctl (fd_tty, TIOCSCTTY, NULL); |
235 | ioctl (fd_tty, TIOCSCTTY, NULL); |
234 | |
236 | |
235 | int fd = open ("/dev/tty", O_WRONLY); |
237 | int fd = open ("/dev/tty", O_WRONLY); |
236 | if (fd < 0) |
238 | if (fd < 0) |
237 | return -1; /* fatal */ |
239 | return -1; /* fatal */ |
238 | |
240 | |
239 | close (fd); |
241 | close (fd); |
240 | |
242 | |
241 | return 0; |
243 | return 0; |
242 | } |
244 | } |
… | |
… | |
370 | return true; |
372 | return true; |
371 | } |
373 | } |
372 | |
374 | |
373 | #if PTYTTY_HELPER |
375 | #if PTYTTY_HELPER |
374 | |
376 | |
375 | static int sock_fd; |
377 | static int sock_fd = -1; |
376 | static int pid; |
378 | static int helper_pid, owner_pid; |
377 | |
379 | |
378 | struct command |
380 | struct command |
379 | { |
381 | { |
380 | enum { get, login, destroy } type; |
382 | enum { get, login, destroy } type; |
381 | |
383 | |
… | |
… | |
409 | cmd.type = command::get; |
411 | cmd.type = command::get; |
410 | |
412 | |
411 | write (sock_fd, &cmd, sizeof (cmd)); |
413 | write (sock_fd, &cmd, sizeof (cmd)); |
412 | |
414 | |
413 | if (read (sock_fd, &id, sizeof (id)) != sizeof (id)) |
415 | if (read (sock_fd, &id, sizeof (id)) != sizeof (id)) |
414 | fatal ("protocol error while creating pty using helper process, aborting.\n"); |
416 | ptytty_fatal ("protocol error while creating pty using helper process, aborting.\n"); |
415 | |
417 | |
416 | if (!id) |
418 | if (!id) |
417 | return false; |
419 | return false; |
418 | |
420 | |
419 | if ((pty = ptytty_recv_fd (sock_fd)) < 0 |
421 | if ((pty = recv_fd (sock_fd)) < 0 |
420 | || (tty = ptytty_recv_fd (sock_fd)) < 0) |
422 | || (tty = recv_fd (sock_fd)) < 0) |
421 | fatal ("protocol error while reading pty/tty fds from helper process, aborting.\n"); |
423 | ptytty_fatal ("protocol error while reading pty/tty fds from helper process, aborting.\n"); |
422 | |
424 | |
423 | return true; |
425 | return true; |
424 | } |
426 | } |
425 | |
427 | |
426 | void |
428 | void |
… | |
… | |
466 | if (cmd.id->get ()) |
468 | if (cmd.id->get ()) |
467 | { |
469 | { |
468 | write (sock_fd, &cmd.id, sizeof (cmd.id)); |
470 | write (sock_fd, &cmd.id, sizeof (cmd.id)); |
469 | ptys.push_back (cmd.id); |
471 | ptys.push_back (cmd.id); |
470 | |
472 | |
471 | ptytty_send_fd (sock_fd, cmd.id->pty); |
473 | ptytty::send_fd (sock_fd, cmd.id->pty); |
472 | ptytty_send_fd (sock_fd, cmd.id->tty); |
474 | ptytty::send_fd (sock_fd, cmd.id->tty); |
473 | } |
475 | } |
474 | else |
476 | else |
475 | { |
477 | { |
476 | delete cmd.id; |
478 | delete cmd.id; |
477 | cmd.id = 0; |
479 | cmd.id = 0; |
… | |
… | |
488 | } |
490 | } |
489 | #endif |
491 | #endif |
490 | } |
492 | } |
491 | else if (cmd.type == command::destroy) |
493 | else if (cmd.type == command::destroy) |
492 | { |
494 | { |
493 | ptytty **pty = find (ptys.begin (), ptys.end (), cmd.id); |
495 | vector<ptytty *>::iterator pty = find (ptys.begin (), ptys.end (), cmd.id); |
494 | |
496 | |
495 | if (pty) |
497 | if (pty != ptys.end ()) |
496 | { |
498 | { |
497 | delete *pty; |
499 | delete *pty; |
498 | ptys.erase (pty); |
500 | ptys.erase (pty); |
499 | } |
501 | } |
500 | } |
502 | } |
501 | else |
503 | else |
502 | break; |
504 | break; |
503 | } |
505 | } |
504 | |
506 | |
505 | // destroy all ptys |
507 | // destroy all ptys |
506 | for (ptytty **i = ptys.end (); i-- > ptys.begin (); ) |
508 | for (vector<ptytty *>::iterator i = ptys.end (); i-- > ptys.begin (); ) |
507 | delete *i; |
509 | delete *i; |
508 | } |
510 | } |
509 | |
511 | |
510 | void ptytty_server () |
512 | void |
|
|
513 | ptytty::use_helper () |
511 | { |
514 | { |
|
|
515 | int pid = getpid (); |
|
|
516 | |
|
|
517 | if (sock_fd >= 0 && pid == owner_pid) |
|
|
518 | return; |
|
|
519 | |
|
|
520 | owner_pid = pid; |
|
|
521 | |
512 | int sv[2]; |
522 | int sv[2]; |
513 | |
523 | |
514 | if (socketpair (AF_UNIX, SOCK_STREAM, 0, sv)) |
524 | if (socketpair (AF_UNIX, SOCK_STREAM, 0, sv)) |
515 | fatal ("could not create socket to communicate with pty/sessiondb helper, aborting.\n"); |
525 | ptytty_fatal ("could not create socket to communicate with pty/sessiondb helper, aborting.\n"); |
516 | |
526 | |
517 | pid = fork (); |
527 | helper_pid = fork (); |
518 | |
528 | |
519 | if (pid < 0) |
529 | if (helper_pid < 0) |
520 | fatal ("could not create pty/sessiondb helper process, aborting.\n"); |
530 | ptytty_fatal ("could not create pty/sessiondb helper process, aborting.\n"); |
521 | |
531 | |
522 | if (pid) |
532 | if (helper_pid) |
523 | { |
533 | { |
524 | // client, process |
534 | // client, process |
525 | sock_fd = sv[0]; |
535 | sock_fd = sv[0]; |
526 | close (sv[1]); |
536 | close (sv[1]); |
527 | fcntl (sock_fd, F_SETFD, FD_CLOEXEC); |
537 | fcntl (sock_fd, F_SETFD, FD_CLOEXEC); |
… | |
… | |
529 | else |
539 | else |
530 | { |
540 | { |
531 | // server, pty-helper |
541 | // server, pty-helper |
532 | sock_fd = sv[1]; |
542 | sock_fd = sv[1]; |
533 | |
543 | |
|
|
544 | chdir ("/"); |
|
|
545 | |
534 | for (int fd = 0; fd < 1023; fd++) |
546 | for (int fd = 0; fd < 1023; fd++) |
535 | if (fd != sock_fd) |
547 | if (fd != sock_fd) |
536 | close (fd); |
548 | close (fd); |
537 | |
549 | |
538 | serve (); |
550 | serve (); |
… | |
… | |
540 | } |
552 | } |
541 | } |
553 | } |
542 | |
554 | |
543 | #endif |
555 | #endif |
544 | |
556 | |
545 | // a "factory" *g* |
|
|
546 | ptytty * |
557 | ptytty * |
547 | new_ptytty () |
558 | ptytty::create () |
548 | { |
559 | { |
549 | #if PTYTTY_HELPER |
560 | #if PTYTTY_HELPER |
550 | if (pid > 0) |
561 | if (helper_pid && getpid () == owner_pid) |
551 | // use helper process |
562 | // use helper process |
552 | return new ptytty_proxy; |
563 | return new ptytty_proxy; |
553 | else |
564 | else |
554 | #endif |
565 | #endif |
555 | return new ptytty_unix; |
566 | return new ptytty_unix; |
556 | } |
567 | } |
557 | |
568 | |
558 | /*----------------------- end-of-file (C source) -----------------------*/ |
569 | void |
|
|
570 | ptytty::init () |
|
|
571 | { |
|
|
572 | uid_t uid = getuid (); |
|
|
573 | gid_t gid = getgid (); |
|
|
574 | |
|
|
575 | // before doing anything else, check for setuid/setgid operation, |
|
|
576 | // start the helper process and drop privileges |
|
|
577 | if (uid != geteuid () |
|
|
578 | || gid != getegid ()) |
|
|
579 | { |
|
|
580 | #if PTYTTY_HELPER |
|
|
581 | use_helper (); |
|
|
582 | #else |
|
|
583 | ptytty_warn ("running setuid/setgid without pty helper compiled in, continuing unprivileged.\n"); |
|
|
584 | #endif |
559 | |
585 | |
|
|
586 | drop_privileges (); |
|
|
587 | } |
|
|
588 | } |
|
|
589 | |
|
|
590 | void |
|
|
591 | ptytty::drop_privileges () |
|
|
592 | { |
|
|
593 | uid_t uid = getuid (); |
|
|
594 | gid_t gid = getgid (); |
|
|
595 | |
|
|
596 | // drop privileges |
|
|
597 | #if HAVE_SETRESUID |
|
|
598 | setresgid (gid, gid, gid); |
|
|
599 | setresuid (uid, uid, uid); |
|
|
600 | #elif HAVE_SETREUID |
|
|
601 | setregid (gid, gid); |
|
|
602 | setreuid (uid, uid); |
|
|
603 | #elif HAVE_SETUID |
|
|
604 | setgid (gid); |
|
|
605 | setuid (uid); |
|
|
606 | #endif |
|
|
607 | |
|
|
608 | if (uid != geteuid () |
|
|
609 | || gid != getegid ()) |
|
|
610 | ptytty_fatal ("unable to drop privileges, aborting.\n"); |
|
|
611 | } |
|
|
612 | |