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

Comparing rxvt-unicode/src/ptytty.C (file contents):
Revision 1.54 by root, Sun Jan 22 00:48:13 2006 UTC vs.
Revision 1.59 by root, Mon Jan 23 12:05:12 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
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#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
512 int sv[2]; 564 int sv[2];
513 565
514 if (socketpair (AF_UNIX, SOCK_STREAM, 0, sv)) 566 if (socketpair (AF_UNIX, SOCK_STREAM, 0, sv))
515 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");
516 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
517 pid = fork (); 576 helper_pid = fork ();
518 577
519 if (pid < 0) 578 if (helper_pid < 0)
520 fatal ("could not create pty/sessiondb helper process, aborting.\n"); 579 ptytty_fatal ("could not create pty/sessiondb helper process, aborting.\n");
521 580
522 if (pid) 581 if (helper_pid)
523 { 582 {
524 // client, process 583 // client, process
525 sock_fd = sv[0]; 584 sock_fd = sv[0];
526 close (sv[1]); 585 close (sv[1]);
527 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
528 } 592 }
529 else 593 else
530 { 594 {
531 // server, pty-helper 595 // server, pty-helper
532 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);
533 607
534 for (int fd = 0; fd < 1023; fd++) 608 for (int fd = 0; fd < 1023; fd++)
535 if (fd != sock_fd) 609 if (fd != sock_fd && fd != lock_fd)
536 close (fd); 610 close (fd);
537 611
538 serve (); 612 serve ();
539 _exit (EXIT_SUCCESS); 613 _exit (EXIT_SUCCESS);
540 } 614 }
541} 615}
542 616
543#endif 617#endif
544 618
545// a "factory" *g*
546ptytty * 619ptytty *
547new_ptytty () 620ptytty::create ()
548{ 621{
549#if PTYTTY_HELPER 622#if PTYTTY_HELPER
550 if (pid > 0) 623 if (helper_pid
624# ifndef PTYTTY_NO_PID_CHECK
625 && getpid () == owner_pid
626# endif
627 )
551 // use helper process 628 // use helper process
552 return new ptytty_proxy; 629 return new ptytty_proxy;
553 else 630 else
554#endif 631#endif
555 return new ptytty_unix; 632 return new ptytty_unix;
556} 633}
557 634
558/*----------------------- 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
559 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