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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines