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.55 by root, Sun Jan 22 01:00:46 2006 UTC vs.
Revision 1.59 by root, Mon Jan 23 12:05:12 2006 UTC

6 *----------------------------------------------------------------------* 6 *----------------------------------------------------------------------*
7 * 7 *
8 * All portions of code are copyright by their respective author/s. 8 * All portions of code are copyright by their respective author/s.
9 * Copyright (c) 1999-2001 Geoff Wing <gcw@pobox.com> 9 * Copyright (c) 1999-2001 Geoff Wing <gcw@pobox.com>
10 * 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>
11 * 12 *
12 * 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
13 * 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
14 * the Free Software Foundation; either version 2 of the License, or 15 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version. 16 * (at your option) any later version.
24 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 25 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 *---------------------------------------------------------------------*/ 26 *---------------------------------------------------------------------*/
26 27
27#include "../config.h" 28#include "../config.h"
28 29
29#include "fdpass.h"
30#include "ptytty.h" 30#include "ptytty.h"
31 31
32#include <cstdlib> 32#include <cstdlib>
33#include <cstring> 33#include <cstring>
34#include <csignal>
34 35
35#include <sys/types.h> 36#include <sys/types.h>
36#include <sys/socket.h> 37#include <sys/socket.h>
37#include <unistd.h> 38#include <unistd.h>
38#include <fcntl.h> 39#include <fcntl.h>
235 236
236 ioctl (fd_tty, TIOCSCTTY, NULL); 237 ioctl (fd_tty, TIOCSCTTY, NULL);
237 238
238 int fd = open ("/dev/tty", O_WRONLY); 239 int fd = open ("/dev/tty", O_WRONLY);
239 if (fd < 0) 240 if (fd < 0)
240 return -1; /* fatal */ 241 return -1; /* fatal */
241 242
242 close (fd); 243 close (fd);
243 244
244 return 0; 245 return 0;
245} 246}
371 } 372 }
372 373
373 return true; 374 return true;
374} 375}
375 376
377/////////////////////////////////////////////////////////////////////////////
378// helper/proxy support
379
376#if PTYTTY_HELPER 380#if PTYTTY_HELPER
377 381
378static int sock_fd; 382static int sock_fd = -1, lock_fd = -1;
379static int pid; 383static int helper_pid, owner_pid;
380 384
381struct command 385struct command
382{ 386{
383 enum { get, login, destroy } type; 387 enum { get, login, destroy } type;
384 388
402 406
403 bool get (); 407 bool get ();
404 void login (int cmd_pid, bool login_shell, const char *hostname); 408 void login (int cmd_pid, bool login_shell, const char *hostname);
405}; 409};
406 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
407bool 419bool
408ptytty_proxy::get () 420ptytty_proxy::get ()
409{ 421{
422 NEED_TOKEN;
423
410 command cmd; 424 command cmd;
411 425
412 cmd.type = command::get; 426 cmd.type = command::get;
413 427
414 write (sock_fd, &cmd, sizeof (cmd)); 428 write (sock_fd, &cmd, sizeof (cmd));
415 429
416 if (read (sock_fd, &id, sizeof (id)) != sizeof (id)) 430 if (read (sock_fd, &id, sizeof (id)) != sizeof (id))
417 fatal ("protocol error while creating pty using helper process, aborting.\n"); 431 ptytty_fatal ("protocol error while creating pty using helper process, aborting.\n");
418 432
419 if (!id) 433 if (!id)
434 {
435 GIVE_TOKEN;
420 return false; 436 return false;
437 }
421 438
422 if ((pty = ptytty_recv_fd (sock_fd)) < 0 439 if ((pty = recv_fd (sock_fd)) < 0
423 || (tty = ptytty_recv_fd (sock_fd)) < 0) 440 || (tty = recv_fd (sock_fd)) < 0)
424 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");
425 442
443 GIVE_TOKEN;
426 return true; 444 return true;
427} 445}
428 446
429void 447void
430ptytty_proxy::login (int cmd_pid, bool login_shell, const char *hostname) 448ptytty_proxy::login (int cmd_pid, bool login_shell, const char *hostname)
431{ 449{
450 NEED_TOKEN;
451
432 command cmd; 452 command cmd;
433 453
434 cmd.type = command::login; 454 cmd.type = command::login;
435 cmd.id = id; 455 cmd.id = id;
436 cmd.cmd_pid = cmd_pid; 456 cmd.cmd_pid = cmd_pid;
437 cmd.login_shell = login_shell; 457 cmd.login_shell = login_shell;
438 strncpy (cmd.hostname, hostname, sizeof (cmd.hostname)); 458 strncpy (cmd.hostname, hostname, sizeof (cmd.hostname));
439 459
440 write (sock_fd, &cmd, sizeof (cmd)); 460 write (sock_fd, &cmd, sizeof (cmd));
461
462 GIVE_TOKEN;
441} 463}
442 464
443ptytty_proxy::~ptytty_proxy () 465ptytty_proxy::~ptytty_proxy ()
444{ 466{
445 if (id) 467 if (id)
446 { 468 {
469 NEED_TOKEN;
470
447 command cmd; 471 command cmd;
448 472
449 cmd.type = command::destroy; 473 cmd.type = command::destroy;
450 cmd.id = id; 474 cmd.id = id;
451 475
452 write (sock_fd, &cmd, sizeof (cmd)); 476 write (sock_fd, &cmd, sizeof (cmd));
477
478 GIVE_TOKEN;
453 } 479 }
454} 480}
455 481
456static 482static
457void serve () 483void serve ()
458{ 484{
459 command cmd; 485 command cmd;
460 vector<ptytty *> ptys; 486 vector<ptytty *> ptys;
461 487
488 for (;;)
489 {
490 GIVE_TOKEN;
491
462 while (read (sock_fd, &cmd, sizeof (command)) == sizeof (command)) 492 if (read (sock_fd, &cmd, sizeof (command)) != sizeof (command))
463 { 493 break;
494
464 if (cmd.type == command::get) 495 if (cmd.type == command::get)
465 { 496 {
466 // -> id ptyfd ttyfd 497 // -> id ptyfd ttyfd
467 cmd.id = new ptytty_unix; 498 cmd.id = new ptytty_unix;
468 499
469 if (cmd.id->get ()) 500 if (cmd.id->get ())
470 { 501 {
471 write (sock_fd, &cmd.id, sizeof (cmd.id)); 502 write (sock_fd, &cmd.id, sizeof (cmd.id));
472 ptys.push_back (cmd.id); 503 ptys.push_back (cmd.id);
473 504
474 ptytty_send_fd (sock_fd, cmd.id->pty); 505 ptytty::send_fd (sock_fd, cmd.id->pty);
475 ptytty_send_fd (sock_fd, cmd.id->tty); 506 ptytty::send_fd (sock_fd, cmd.id->tty);
476 } 507 }
477 else 508 else
478 { 509 {
479 delete cmd.id; 510 delete cmd.id;
480 cmd.id = 0; 511 cmd.id = 0;
482 } 513 }
483 } 514 }
484 else if (cmd.type == command::login) 515 else if (cmd.type == command::login)
485 { 516 {
486#if UTMP_SUPPORT 517#if UTMP_SUPPORT
487 if (find (ptys.begin (), ptys.end (), cmd.id)) 518 if (find (ptys.begin (), ptys.end (), cmd.id) != ptys.end ())
488 { 519 {
489 cmd.hostname[sizeof (cmd.hostname) - 1] = 0; 520 cmd.hostname[sizeof (cmd.hostname) - 1] = 0;
490 cmd.id->login (cmd.cmd_pid, cmd.login_shell, cmd.hostname); 521 cmd.id->login (cmd.cmd_pid, cmd.login_shell, cmd.hostname);
491 } 522 }
492#endif 523#endif
493 } 524 }
494 else if (cmd.type == command::destroy) 525 else if (cmd.type == command::destroy)
495 { 526 {
496 ptytty **pty = find (ptys.begin (), ptys.end (), cmd.id); 527 vector<ptytty *>::iterator pty = find (ptys.begin (), ptys.end (), cmd.id);
497 528
498 if (pty) 529 if (pty != ptys.end ())
499 { 530 {
500 delete *pty; 531 delete *pty;
501 ptys.erase (pty); 532 ptys.erase (pty);
502 } 533 }
503 } 534 }
504 else 535 else
505 break; 536 break;
537
538 NEED_TOKEN;
506 } 539 }
507 540
508 // destroy all ptys 541 // destroy all ptys
509 for (ptytty **i = ptys.end (); i-- > ptys.begin (); ) 542 for (vector<ptytty *>::iterator i = ptys.end (); i-- > ptys.begin (); )
510 delete *i; 543 delete *i;
511} 544}
512 545
513void ptytty_server () 546void
547ptytty::use_helper ()
514{ 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
515 int sv[2]; 564 int sv[2];
516 565
517 if (socketpair (AF_UNIX, SOCK_STREAM, 0, sv)) 566 if (socketpair (AF_UNIX, SOCK_STREAM, 0, sv))
518 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");
519 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
520 pid = fork (); 576 helper_pid = fork ();
521 577
522 if (pid < 0) 578 if (helper_pid < 0)
523 fatal ("could not create pty/sessiondb helper process, aborting.\n"); 579 ptytty_fatal ("could not create pty/sessiondb helper process, aborting.\n");
524 580
525 if (pid) 581 if (helper_pid)
526 { 582 {
527 // client, process 583 // client, process
528 sock_fd = sv[0]; 584 sock_fd = sv[0];
529 close (sv[1]); 585 close (sv[1]);
530 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
531 } 592 }
532 else 593 else
533 { 594 {
534 // server, pty-helper 595 // server, pty-helper
535 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);
536 607
537 for (int fd = 0; fd < 1023; fd++) 608 for (int fd = 0; fd < 1023; fd++)
538 if (fd != sock_fd) 609 if (fd != sock_fd && fd != lock_fd)
539 close (fd); 610 close (fd);
540 611
541 serve (); 612 serve ();
542 _exit (EXIT_SUCCESS); 613 _exit (EXIT_SUCCESS);
543 } 614 }
544} 615}
545 616
546#endif 617#endif
547 618
548// a "factory" *g*
549ptytty * 619ptytty *
550new_ptytty () 620ptytty::create ()
551{ 621{
552#if PTYTTY_HELPER 622#if PTYTTY_HELPER
553 if (pid > 0) 623 if (helper_pid
624# ifndef PTYTTY_NO_PID_CHECK
625 && getpid () == owner_pid
626# endif
627 )
554 // use helper process 628 // use helper process
555 return new ptytty_proxy; 629 return new ptytty_proxy;
556 else 630 else
557#endif 631#endif
558 return new ptytty_unix; 632 return new ptytty_unix;
559} 633}
560 634
561/*----------------------- 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
562 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