ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/libptytty/src/ptytty.C
(Generate patch)

Comparing libptytty/src/ptytty.C (file contents):
Revision 1.6 by root, Sun Jan 22 00:04:20 2006 UTC vs.
Revision 1.20 by root, Mon Jan 23 11:45:44 2006 UTC

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
375static int sock_fd; 382static int sock_fd = -1, lock_fd = -1;
376static int pid; 383static int helper_pid, owner_pid;
377 384
378struct command 385struct command
379{ 386{
380 enum { get, login, destroy } type; 387 enum { get, login, destroy } type;
381 388
388 395
389struct ptytty_proxy : ptytty 396struct ptytty_proxy : ptytty
390{ 397{
391 ptytty *id; 398 ptytty *id;
392 399
393 ~ptytty_proxy () 400 ptytty_proxy ()
394 : id(0) 401 : id(0)
395 { 402 {
396 } 403 }
397 404
398 ~ptytty_proxy (); 405 ~ptytty_proxy ();
399 406
400 bool get (); 407 bool get ();
401 void login (int cmd_pid, bool login_shell, const char *hostname); 408 void login (int cmd_pid, bool login_shell, const char *hostname);
402}; 409};
403 410
411#if PTYTTY_REENTRANT
412# define NEED_TOKEN do { char ch; read (lock_fd, &ch, 1); } while (0)
413# define GIVE_TOKEN do { char ch; write (lock_fd, &ch, 1); } while (0)
414#else
415# define NEED_TOKEN (void)0
416# define GIVE_TOKEN (void)0
417#endif
418
404bool 419bool
405ptytty_proxy::get () 420ptytty_proxy::get ()
406{ 421{
422 NEED_TOKEN;
423
407 command cmd; 424 command cmd;
408 425
409 cmd.type = command::get; 426 cmd.type = command::get;
410 427
411 write (sock_fd, &cmd, sizeof (cmd)); 428 write (sock_fd, &cmd, sizeof (cmd));
412 429
413 if (read (sock_fd, &id, sizeof (id)) != sizeof (id)) 430 if (read (sock_fd, &id, sizeof (id)) != sizeof (id))
414 fatal ("protocol error while creating pty using helper process, aborting.\n"); 431 ptytty_fatal ("protocol error while creating pty using helper process, aborting.\n");
415 432
416 if (!id) 433 if (!id)
434 {
435 GIVE_TOKEN;
417 return false; 436 return false;
437 }
418 438
419 if ((pty = ptytty_recv_fd (sock_fd)) < 0 439 if ((pty = recv_fd (sock_fd)) < 0
420 || (tty = ptytty_recv_fd (sock_fd)) < 0) 440 || (tty = recv_fd (sock_fd)) < 0)
421 fatal ("protocol error while reading pty/tty fds from helper process, aborting.\n"); 441 ptytty_fatal ("protocol error while reading pty/tty fds from helper process, aborting.\n");
422 442
443 GIVE_TOKEN;
423 return true; 444 return true;
424} 445}
425 446
426void 447void
427ptytty_proxy::login (int cmd_pid, bool login_shell, const char *hostname) 448ptytty_proxy::login (int cmd_pid, bool login_shell, const char *hostname)
428{ 449{
450 NEED_TOKEN;
451
429 command cmd; 452 command cmd;
430 453
431 cmd.type = command::login; 454 cmd.type = command::login;
432 cmd.id = id; 455 cmd.id = id;
433 cmd.cmd_pid = cmd_pid; 456 cmd.cmd_pid = cmd_pid;
434 cmd.login_shell = login_shell; 457 cmd.login_shell = login_shell;
435 strncpy (cmd.hostname, hostname, sizeof (cmd.hostname)); 458 strncpy (cmd.hostname, hostname, sizeof (cmd.hostname));
436 459
437 write (sock_fd, &cmd, sizeof (cmd)); 460 write (sock_fd, &cmd, sizeof (cmd));
461
462 GIVE_TOKEN;
438} 463}
439 464
440ptytty_proxy::~ptytty_proxy () 465ptytty_proxy::~ptytty_proxy ()
441{ 466{
442 if (id) 467 if (id)
443 { 468 {
469 NEED_TOKEN;
470
444 command cmd; 471 command cmd;
445 472
446 cmd.type = command::destroy; 473 cmd.type = command::destroy;
447 cmd.id = id; 474 cmd.id = id;
448 475
449 write (sock_fd, &cmd, sizeof (cmd)); 476 write (sock_fd, &cmd, sizeof (cmd));
477
478 GIVE_TOKEN;
450 } 479 }
451} 480}
452 481
453static 482static
454void serve () 483void serve ()
455{ 484{
456 command cmd; 485 command cmd;
457 vector<ptytty *> ptys; 486 vector<ptytty *> ptys;
458 487
488 for (;;)
489 {
490 GIVE_TOKEN;
491
459 while (read (sock_fd, &cmd, sizeof (command)) == sizeof (command)) 492 if (read (sock_fd, &cmd, sizeof (command)) != sizeof (command))
460 { 493 break;
494
461 if (cmd.type == command::get) 495 if (cmd.type == command::get)
462 { 496 {
463 // -> id ptyfd ttyfd 497 // -> id ptyfd ttyfd
464 cmd.id = new ptytty_unix; 498 cmd.id = new ptytty_unix;
465 499
466 if (cmd.id->get ()) 500 if (cmd.id->get ())
467 { 501 {
468 write (sock_fd, &cmd.id, sizeof (cmd.id)); 502 write (sock_fd, &cmd.id, sizeof (cmd.id));
469 ptys.push_back (cmd.id); 503 ptys.push_back (cmd.id);
470 504
471 ptytty_send_fd (sock_fd, cmd.id->pty); 505 ptytty::send_fd (sock_fd, cmd.id->pty);
472 ptytty_send_fd (sock_fd, cmd.id->tty); 506 ptytty::send_fd (sock_fd, cmd.id->tty);
473 } 507 }
474 else 508 else
475 { 509 {
476 delete cmd.id; 510 delete cmd.id;
477 cmd.id = 0; 511 cmd.id = 0;
479 } 513 }
480 } 514 }
481 else if (cmd.type == command::login) 515 else if (cmd.type == command::login)
482 { 516 {
483#if UTMP_SUPPORT 517#if UTMP_SUPPORT
484 if (find (ptys.begin (), ptys.end (), cmd.id)) 518 if (find (ptys.begin (), ptys.end (), cmd.id) != ptys.end ())
485 { 519 {
486 cmd.hostname[sizeof (cmd.hostname) - 1] = 0; 520 cmd.hostname[sizeof (cmd.hostname) - 1] = 0;
487 cmd.id->login (cmd.cmd_pid, cmd.login_shell, cmd.hostname); 521 cmd.id->login (cmd.cmd_pid, cmd.login_shell, cmd.hostname);
488 } 522 }
489#endif 523#endif
490 } 524 }
491 else if (cmd.type == command::destroy) 525 else if (cmd.type == command::destroy)
492 { 526 {
493 ptytty **pty = find (ptys.begin (), ptys.end (), cmd.id); 527 vector<ptytty *>::iterator pty = find (ptys.begin (), ptys.end (), cmd.id);
494 528
495 if (pty) 529 if (pty != ptys.end ())
496 { 530 {
497 delete *pty; 531 delete *pty;
498 ptys.erase (pty); 532 ptys.erase (pty);
499 } 533 }
500 } 534 }
501 else 535 else
502 break; 536 break;
537
538 NEED_TOKEN;
503 } 539 }
504 540
505 // destroy all ptys 541 // destroy all ptys
506 for (ptytty **i = ptys.end (); i-- > ptys.begin (); ) 542 for (vector<ptytty *>::iterator i = ptys.end (); i-- > ptys.begin (); )
507 delete *i; 543 delete *i;
508} 544}
509 545
510void ptytty_server () 546void
547ptytty::use_helper ()
511{ 548{
549 int pid = getpid ();
550
551 if (sock_fd >= 0 && pid == owner_pid)
552 return;
553
554 owner_pid = pid;
555
512 int sv[2]; 556 int sv[2];
513 557
514 if (socketpair (AF_UNIX, SOCK_STREAM, 0, sv)) 558 if (socketpair (AF_UNIX, SOCK_STREAM, 0, sv))
515 fatal ("could not create socket to communicate with pty/sessiondb helper, aborting.\n"); 559 ptytty_fatal ("could not create socket to communicate with pty/sessiondb helper, aborting.\n");
516 560
561#ifdef PTYTTY_REENTRANT
562 int lv[2];
563
564 if (socketpair (AF_UNIX, SOCK_STREAM, 0, lv))
565 ptytty_fatal ("could not create socket to communicate with pty/sessiondb helper, aborting.\n");
566#endif
567
517 pid = fork (); 568 helper_pid = fork ();
518 569
519 if (pid < 0) 570 if (helper_pid < 0)
520 fatal ("could not create pty/sessiondb helper process, aborting.\n"); 571 ptytty_fatal ("could not create pty/sessiondb helper process, aborting.\n");
521 572
522 if (pid) 573 if (helper_pid)
523 { 574 {
524 // client, process 575 // client, process
525 sock_fd = sv[0]; 576 sock_fd = sv[0];
526 close (sv[1]); 577 close (sv[1]);
527 fcntl (sock_fd, F_SETFD, FD_CLOEXEC); 578 fcntl (sock_fd, F_SETFD, FD_CLOEXEC);
579#ifdef PTYTTY_REENTRANT
580 lock_fd = lv[0];
581 close (lv[1]);
582 fcntl (lock_fd, F_SETFD, FD_CLOEXEC);
583#endif
528 } 584 }
529 else 585 else
530 { 586 {
531 // server, pty-helper 587 // server, pty-helper
532 sock_fd = sv[1]; 588 sock_fd = sv[1];
589#ifdef PTYTTY_REENTRANT
590 lock_fd = lv[1];
591#endif
592
593 chdir ("/");
594
595 signal (SIGHUP, SIG_IGN);
596 signal (SIGTERM, SIG_IGN);
597 signal (SIGINT, SIG_IGN);
598 signal (SIGPIPE, SIG_IGN);
533 599
534 for (int fd = 0; fd < 1023; fd++) 600 for (int fd = 0; fd < 1023; fd++)
535 if (fd != sock_fd) 601 if (fd != sock_fd && fd != lock_fd)
536 close (fd); 602 close (fd);
537 603
538 serve (); 604 serve ();
539 _exit (EXIT_SUCCESS); 605 _exit (EXIT_SUCCESS);
540 } 606 }
541} 607}
542 608
543#endif 609#endif
544 610
545// a "factory" *g*
546ptytty * 611ptytty *
547new_ptytty () 612ptytty::create ()
548{ 613{
549#if PTYTTY_HELPER 614#if PTYTTY_HELPER
550 if (pid > 0) 615 if (helper_pid && getpid () == owner_pid)
551 // use helper process 616 // use helper process
552 return new ptytty_proxy; 617 return new ptytty_proxy;
553 else 618 else
554#endif 619#endif
555 return new ptytty_unix; 620 return new ptytty_unix;
556} 621}
557 622
558/*----------------------- end-of-file (C source) -----------------------*/ 623void
624ptytty::init ()
625{
626 uid_t uid = getuid ();
627 gid_t gid = getgid ();
628
629 // before doing anything else, check for setuid/setgid operation,
630 // start the helper process and drop privileges
631 if (uid != geteuid ()
632 || gid != getegid ())
633 {
634#if PTYTTY_HELPER
635 use_helper ();
636#else
637 ptytty_warn ("running setuid/setgid without pty helper compiled in, continuing unprivileged.\n");
638#endif
559 639
640 drop_privileges ();
641 }
642}
643
644void
645ptytty::drop_privileges ()
646{
647 uid_t uid = getuid ();
648 gid_t gid = getgid ();
649
650 // drop privileges
651#if HAVE_SETRESUID
652 setresgid (gid, gid, gid);
653 setresuid (uid, uid, uid);
654#elif HAVE_SETREUID
655 setregid (gid, gid);
656 setreuid (uid, uid);
657#elif HAVE_SETUID
658 setgid (gid);
659 setuid (uid);
660#endif
661
662 if (uid != geteuid ()
663 || gid != getegid ())
664 ptytty_fatal ("unable to drop privileges, aborting.\n");
665}
666
667/////////////////////////////////////////////////////////////////////////////
668// C API
669
670#ifndef NO_C_API
671
672#define DEFINE_METHOD(retval, name, args1, args2) \
673extern "C" retval ptytty_ ## name args1 \
674{ return ((struct ptytty *)ptytty)->name args2; }
675
676DEFINE_METHOD(int,pty,(void *ptytty),)
677DEFINE_METHOD(int,tty,(void *ptytty),)
678DEFINE_METHOD(int,get,(void *ptytty),())
679DEFINE_METHOD(void,login,(void *ptytty, int cmd_pid, bool login_shell, const char *hostname),(cmd_pid,login_shell,hostname))
680
681DEFINE_METHOD(void,close_tty,(void *ptytty),())
682DEFINE_METHOD(int,make_controlling_tty,(void *ptytty),())
683DEFINE_METHOD(void,set_utf8_mode,(void *ptytty, int on),(on))
684
685#define DEFINE_STATIC(retval, name, args) \
686extern "C" retval ptytty_ ## name args \
687{ return ptytty::name args; }
688
689DEFINE_STATIC(void,drop_privileges,())
690DEFINE_STATIC(void,use_helper,())
691DEFINE_STATIC(void,init,())
692
693DEFINE_STATIC(void *,create,())
694
695void ptytty_delete (void *ptytty)
696{
697 delete (struct ptytty *)ptytty;
698}
699
700// send_fd, recv_fd not exposed
701
702#endif

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines