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.53 by ayin, Sat Jan 21 18:15:26 2006 UTC vs.
Revision 1.60 by root, Mon Jan 23 12:37:59 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"
29
25#include "rxvt.h" 30#include "ptytty.h"
26 31
27#include <cstdlib> 32#include <cstdlib>
28#include <cstring> 33#include <cstring>
34#include <csignal>
29 35
30#include <sys/types.h> 36#include <sys/types.h>
31#include <sys/socket.h> 37#include <sys/socket.h>
32#include <unistd.h> 38#include <unistd.h>
33#include <fcntl.h> 39#include <fcntl.h>
51#ifdef TTY_GID_SUPPORT 57#ifdef TTY_GID_SUPPORT
52#include <grp.h> 58#include <grp.h>
53#endif 59#endif
54 60
55#include <cstdio> 61#include <cstdio>
56
57#include "rxvtutil.h"
58#include "fdpass.h"
59#include "ptytty.h"
60 62
61///////////////////////////////////////////////////////////////////////////// 63/////////////////////////////////////////////////////////////////////////////
62 64
63/* ------------------------------------------------------------------------- * 65/* ------------------------------------------------------------------------- *
64 * GET PSEUDO TELETYPE - MASTER AND SLAVE * 66 * GET PSEUDO TELETYPE - MASTER AND SLAVE *
234 236
235 ioctl (fd_tty, TIOCSCTTY, NULL); 237 ioctl (fd_tty, TIOCSCTTY, NULL);
236 238
237 int fd = open ("/dev/tty", O_WRONLY); 239 int fd = open ("/dev/tty", O_WRONLY);
238 if (fd < 0) 240 if (fd < 0)
239 return -1; /* fatal */ 241 return -1; /* fatal */
240 242
241 close (fd); 243 close (fd);
242 244
243 return 0; 245 return 0;
244} 246}
245 247
246void 248void
247rxvt_ptytty::close_tty () 249ptytty::close_tty ()
248{ 250{
249 if (tty < 0) 251 if (tty < 0)
250 return; 252 return;
251 253
252 close (tty); 254 close (tty);
253 tty = -1; 255 tty = -1;
254} 256}
255 257
256bool 258bool
257rxvt_ptytty::make_controlling_tty () 259ptytty::make_controlling_tty ()
258{ 260{
259 return control_tty (tty) >= 0; 261 return control_tty (tty) >= 0;
260} 262}
261 263
262void 264void
263rxvt_ptytty::set_utf8_mode (bool on) 265ptytty::set_utf8_mode (bool on)
264{ 266{
265#ifdef IUTF8 267#ifdef IUTF8
266 if (pty < 0) 268 if (pty < 0)
267 return; 269 return;
268 270
307 gid = 0; 309 gid = 0;
308 } 310 }
309 } 311 }
310} ttyconf; 312} ttyconf;
311 313
312rxvt_ptytty_unix::rxvt_ptytty_unix () 314ptytty_unix::ptytty_unix ()
313{ 315{
314 name = 0; 316 name = 0;
315#if UTMP_SUPPORT 317#if UTMP_SUPPORT
316 cmd_pid = 0; 318 cmd_pid = 0;
317#endif 319#endif
318} 320}
319 321
320rxvt_ptytty_unix::~rxvt_ptytty_unix () 322ptytty_unix::~ptytty_unix ()
321{ 323{
322#if UTMP_SUPPORT 324#if UTMP_SUPPORT
323 logout (); 325 logout ();
324#endif 326#endif
325 put (); 327 put ();
326} 328}
327 329
328void 330void
329rxvt_ptytty_unix::put () 331ptytty_unix::put ()
330{ 332{
331 chmod (name, RESTORE_TTY_MODE); 333 chmod (name, RESTORE_TTY_MODE);
332 chown (name, 0, ttyconf.gid); 334 chown (name, 0, ttyconf.gid);
333 335
334 close_tty (); 336 close_tty ();
341 pty = tty = -1; 343 pty = tty = -1;
342 name = 0; 344 name = 0;
343} 345}
344 346
345bool 347bool
346rxvt_ptytty_unix::get () 348ptytty_unix::get ()
347{ 349{
348 /* get master (pty) */ 350 /* get master (pty) */
349 if ((pty = get_pty (&tty, &name)) < 0) 351 if ((pty = get_pty (&tty, &name)) < 0)
350 return false; 352 return false;
351 353
370 } 372 }
371 373
372 return true; 374 return true;
373} 375}
374 376
377/////////////////////////////////////////////////////////////////////////////
378// helper/proxy support
379
375#if PTYTTY_HELPER 380#if PTYTTY_HELPER
376 381
377static int sock_fd; 382static int sock_fd = -1, lock_fd = -1;
378static int pid; 383static int helper_pid, owner_pid;
379 384
380struct command 385struct command
381{ 386{
382 enum { get, login, destroy } type; 387 enum { get, login, destroy } type;
383 388
384 rxvt_ptytty *id; 389 ptytty *id;
385 390
386 bool login_shell; 391 bool login_shell;
387 int cmd_pid; 392 int cmd_pid;
388 char hostname[512]; // arbitrary, but should be plenty 393 char hostname[512]; // arbitrary, but should be plenty
389}; 394};
390 395
391struct rxvt_ptytty_proxy : zero_initialized, rxvt_ptytty 396struct ptytty_proxy : ptytty
392{ 397{
393 rxvt_ptytty *id; 398 ptytty *id;
394 399
400 ptytty_proxy ()
401 : id(0)
402 {
403 }
404
395 ~rxvt_ptytty_proxy (); 405 ~ptytty_proxy ();
396 406
397 bool get (); 407 bool get ();
398 void login (int cmd_pid, bool login_shell, const char *hostname); 408 void login (int cmd_pid, bool login_shell, const char *hostname);
399}; 409};
400 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
401bool 419bool
402rxvt_ptytty_proxy::get () 420ptytty_proxy::get ()
403{ 421{
422 NEED_TOKEN;
423
404 command cmd; 424 command cmd;
405 425
406 cmd.type = command::get; 426 cmd.type = command::get;
407 427
408 write (sock_fd, &cmd, sizeof (cmd)); 428 write (sock_fd, &cmd, sizeof (cmd));
409 429
410 if (read (sock_fd, &id, sizeof (id)) != sizeof (id)) 430 if (read (sock_fd, &id, sizeof (id)) != sizeof (id))
411 rxvt_fatal ("protocol error while creating pty using helper process, aborting.\n"); 431 ptytty_fatal ("protocol error while creating pty using helper process, aborting.\n");
412 432
413 if (!id) 433 if (!id)
434 {
435 GIVE_TOKEN;
414 return false; 436 return false;
437 }
415 438
416 if ((pty = rxvt_recv_fd (sock_fd)) < 0 439 if ((pty = recv_fd (sock_fd)) < 0
417 || (tty = rxvt_recv_fd (sock_fd)) < 0) 440 || (tty = recv_fd (sock_fd)) < 0)
418 rxvt_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");
419 442
443 GIVE_TOKEN;
420 return true; 444 return true;
421} 445}
422 446
423void 447void
424rxvt_ptytty_proxy::login (int cmd_pid, bool login_shell, const char *hostname) 448ptytty_proxy::login (int cmd_pid, bool login_shell, const char *hostname)
425{ 449{
450 NEED_TOKEN;
451
426 command cmd; 452 command cmd;
427 453
428 cmd.type = command::login; 454 cmd.type = command::login;
429 cmd.id = id; 455 cmd.id = id;
430 cmd.cmd_pid = cmd_pid; 456 cmd.cmd_pid = cmd_pid;
431 cmd.login_shell = login_shell; 457 cmd.login_shell = login_shell;
432 strncpy (cmd.hostname, hostname, sizeof (cmd.hostname)); 458 strncpy (cmd.hostname, hostname, sizeof (cmd.hostname));
433 459
434 write (sock_fd, &cmd, sizeof (cmd)); 460 write (sock_fd, &cmd, sizeof (cmd));
435}
436 461
462 GIVE_TOKEN;
463}
464
437rxvt_ptytty_proxy::~rxvt_ptytty_proxy () 465ptytty_proxy::~ptytty_proxy ()
438{ 466{
467 if (id)
468 {
469 NEED_TOKEN;
470
439 command cmd; 471 command cmd;
440 472
441 cmd.type = command::destroy; 473 cmd.type = command::destroy;
442 cmd.id = id; 474 cmd.id = id;
443 475
444 write (sock_fd, &cmd, sizeof (cmd)); 476 write (sock_fd, &cmd, sizeof (cmd));
477
478 GIVE_TOKEN;
479 }
445} 480}
446 481
447static 482static
448void serve () 483void serve ()
449{ 484{
450 command cmd; 485 command cmd;
451 vector<rxvt_ptytty *> ptys; 486 vector<ptytty *> ptys;
452 487
488 for (;;)
489 {
490 GIVE_TOKEN;
491
453 while (read (sock_fd, &cmd, sizeof (command)) == sizeof (command)) 492 if (read (sock_fd, &cmd, sizeof (command)) != sizeof (command))
454 { 493 break;
494
455 if (cmd.type == command::get) 495 if (cmd.type == command::get)
456 { 496 {
457 // -> id ptyfd ttyfd 497 // -> id ptyfd ttyfd
458 cmd.id = new rxvt_ptytty_unix; 498 cmd.id = new ptytty_unix;
459 499
460 if (cmd.id->get ()) 500 if (cmd.id->get ())
461 { 501 {
462 write (sock_fd, &cmd.id, sizeof (cmd.id)); 502 write (sock_fd, &cmd.id, sizeof (cmd.id));
463 ptys.push_back (cmd.id); 503 ptys.push_back (cmd.id);
464 504
465 rxvt_send_fd (sock_fd, cmd.id->pty); 505 ptytty::send_fd (sock_fd, cmd.id->pty);
466 rxvt_send_fd (sock_fd, cmd.id->tty); 506 ptytty::send_fd (sock_fd, cmd.id->tty);
467 } 507 }
468 else 508 else
469 { 509 {
470 delete cmd.id; 510 delete cmd.id;
471 cmd.id = 0; 511 cmd.id = 0;
473 } 513 }
474 } 514 }
475 else if (cmd.type == command::login) 515 else if (cmd.type == command::login)
476 { 516 {
477#if UTMP_SUPPORT 517#if UTMP_SUPPORT
478 if (find (ptys.begin (), ptys.end (), cmd.id)) 518 if (find (ptys.begin (), ptys.end (), cmd.id) != ptys.end ())
479 { 519 {
480 cmd.hostname[sizeof (cmd.hostname) - 1] = 0; 520 cmd.hostname[sizeof (cmd.hostname) - 1] = 0;
481 cmd.id->login (cmd.cmd_pid, cmd.login_shell, cmd.hostname); 521 cmd.id->login (cmd.cmd_pid, cmd.login_shell, cmd.hostname);
482 } 522 }
483#endif 523#endif
484 } 524 }
485 else if (cmd.type == command::destroy) 525 else if (cmd.type == command::destroy)
486 { 526 {
487 rxvt_ptytty **pty = find (ptys.begin (), ptys.end (), cmd.id); 527 vector<ptytty *>::iterator pty = find (ptys.begin (), ptys.end (), cmd.id);
488 528
489 if (pty) 529 if (pty != ptys.end ())
490 { 530 {
491 delete *pty; 531 delete *pty;
492 ptys.erase (pty); 532 ptys.erase (pty);
493 } 533 }
494 } 534 }
495 else 535 else
496 break; 536 break;
537
538 NEED_TOKEN;
497 } 539 }
498 540
499 // destroy all ptys 541 // destroy all ptys
500 for (rxvt_ptytty **i = ptys.end (); i-- > ptys.begin (); ) 542 for (vector<ptytty *>::iterator i = ptys.end (); i-- > ptys.begin (); )
501 delete *i; 543 delete *i;
502} 544}
503 545
504void rxvt_ptytty_server () 546void
547ptytty::use_helper ()
505{ 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
506 int sv[2]; 564 int sv[2];
507 565
508 if (socketpair (AF_UNIX, SOCK_STREAM, 0, sv)) 566 if (socketpair (AF_UNIX, SOCK_STREAM, 0, sv))
509 rxvt_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");
510 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
511 pid = fork (); 576 helper_pid = fork ();
512 577
513 if (pid < 0) 578 if (helper_pid < 0)
514 rxvt_fatal ("could not create pty/sessiondb helper process, aborting.\n"); 579 ptytty_fatal ("could not create pty/sessiondb helper process, aborting.\n");
515 580
516 if (pid) 581 if (helper_pid)
517 { 582 {
518 // client, urxvt 583 // client, process
519 sock_fd = sv[0]; 584 sock_fd = sv[0];
520 close (sv[1]); 585 close (sv[1]);
521 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
522 } 592 }
523 else 593 else
524 { 594 {
525 // server, pty-helper 595 // server, pty-helper
526 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);
527 607
528 for (int fd = 0; fd < 1023; fd++) 608 for (int fd = 0; fd < 1023; fd++)
529 if (fd != sock_fd) 609 if (fd != sock_fd && fd != lock_fd)
530 close (fd); 610 close (fd);
531 611
532 serve (); 612 serve ();
533 _exit (EXIT_SUCCESS); 613 _exit (EXIT_SUCCESS);
534 } 614 }
535} 615}
536 616
537#endif 617#endif
538 618
539// a "factory" *g*
540rxvt_ptytty * 619ptytty *
541rxvt_new_ptytty () 620ptytty::create ()
542{ 621{
543#if PTYTTY_HELPER 622#if PTYTTY_HELPER
544 if (pid > 0) 623 if (helper_pid
624# ifndef PTYTTY_NO_PID_CHECK
625 && getpid () == owner_pid
626# endif
627 )
545 // use helper process 628 // use helper process
546 return new rxvt_ptytty_proxy; 629 return new ptytty_proxy;
547 else 630 else
548#endif 631#endif
549 return new rxvt_ptytty_unix; 632 return new ptytty_unix;
550} 633}
551 634
552/*----------------------- 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
553 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
684typedef void *PTYTTY;
685
686#define DEFINE_METHOD(retval, name, args1, args2) \
687extern "C" retval ptytty_ ## name args1 \
688{ return ((struct ptytty *)ptytty)->name args2; }
689
690DEFINE_METHOD(int,pty,(PTYTTY ptytty),)
691DEFINE_METHOD(int,tty,(PTYTTY ptytty),)
692DEFINE_METHOD(int,get,(PTYTTY ptytty),())
693DEFINE_METHOD(void,login,(PTYTTY ptytty, int cmd_pid, bool login_shell, const char *hostname),(cmd_pid,login_shell,hostname))
694
695DEFINE_METHOD(void,close_tty,(PTYTTY ptytty),())
696DEFINE_METHOD(int,make_controlling_tty,(PTYTTY ptytty),())
697DEFINE_METHOD(void,set_utf8_mode,(PTYTTY ptytty, int on),(on))
698
699#define DEFINE_STATIC(retval, name, args) \
700extern "C" retval ptytty_ ## name args \
701{ return ptytty::name args; }
702
703DEFINE_STATIC(void,drop_privileges,())
704DEFINE_STATIC(void,use_helper,())
705DEFINE_STATIC(void,init,())
706
707DEFINE_STATIC(PTYTTY ,create,())
708
709void ptytty_delete (PTYTTY ptytty)
710{
711 delete (struct ptytty *)ptytty;
712}
713
714// send_fd, recv_fd not exposed
715
716#endif

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines