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.3 by root, Sat Jan 21 22:06:16 2006 UTC vs.
Revision 1.19 by root, Mon Jan 23 11:36:01 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}
243 247
244void 248void
245rxvt_ptytty::close_tty () 249ptytty::close_tty ()
246{ 250{
247 if (tty < 0) 251 if (tty < 0)
248 return; 252 return;
249 253
250 close (tty); 254 close (tty);
251 tty = -1; 255 tty = -1;
252} 256}
253 257
254bool 258bool
255rxvt_ptytty::make_controlling_tty () 259ptytty::make_controlling_tty ()
256{ 260{
257 return control_tty (tty) >= 0; 261 return control_tty (tty) >= 0;
258} 262}
259 263
260void 264void
261rxvt_ptytty::set_utf8_mode (bool on) 265ptytty::set_utf8_mode (bool on)
262{ 266{
263#ifdef IUTF8 267#ifdef IUTF8
264 if (pty < 0) 268 if (pty < 0)
265 return; 269 return;
266 270
305 gid = 0; 309 gid = 0;
306 } 310 }
307 } 311 }
308} ttyconf; 312} ttyconf;
309 313
310rxvt_ptytty_unix::rxvt_ptytty_unix () 314ptytty_unix::ptytty_unix ()
311{ 315{
312 name = 0; 316 name = 0;
313#if UTMP_SUPPORT 317#if UTMP_SUPPORT
314 cmd_pid = 0; 318 cmd_pid = 0;
315#endif 319#endif
316} 320}
317 321
318rxvt_ptytty_unix::~rxvt_ptytty_unix () 322ptytty_unix::~ptytty_unix ()
319{ 323{
320#if UTMP_SUPPORT 324#if UTMP_SUPPORT
321 logout (); 325 logout ();
322#endif 326#endif
323 put (); 327 put ();
324} 328}
325 329
326void 330void
327rxvt_ptytty_unix::put () 331ptytty_unix::put ()
328{ 332{
329 chmod (name, RESTORE_TTY_MODE); 333 chmod (name, RESTORE_TTY_MODE);
330 chown (name, 0, ttyconf.gid); 334 chown (name, 0, ttyconf.gid);
331 335
332 close_tty (); 336 close_tty ();
339 pty = tty = -1; 343 pty = tty = -1;
340 name = 0; 344 name = 0;
341} 345}
342 346
343bool 347bool
344rxvt_ptytty_unix::get () 348ptytty_unix::get ()
345{ 349{
346 /* get master (pty) */ 350 /* get master (pty) */
347 if ((pty = get_pty (&tty, &name)) < 0) 351 if ((pty = get_pty (&tty, &name)) < 0)
348 return false; 352 return false;
349 353
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
382 rxvt_ptytty *id; 389 ptytty *id;
383 390
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 rxvt_ptytty_proxy : zero_initialized, rxvt_ptytty 396struct ptytty_proxy : ptytty
390{ 397{
391 rxvt_ptytty *id; 398 ptytty *id;
392 399
400 ptytty_proxy ()
401 : id(0)
402 {
403 }
404
393 ~rxvt_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
399bool 411bool
400rxvt_ptytty_proxy::get () 412ptytty_proxy::get ()
401{ 413{
402 command cmd; 414 command cmd;
403 415
404 cmd.type = command::get; 416 cmd.type = command::get;
405 417
406 write (sock_fd, &cmd, sizeof (cmd)); 418 write (sock_fd, &cmd, sizeof (cmd));
407 419
408 if (read (sock_fd, &id, sizeof (id)) != sizeof (id)) 420 if (read (sock_fd, &id, sizeof (id)) != sizeof (id))
409 rxvt_fatal ("protocol error while creating pty using helper process, aborting.\n"); 421 ptytty_fatal ("protocol error while creating pty using helper process, aborting.\n");
410 422
411 if (!id) 423 if (!id)
412 return false; 424 return false;
413 425
414 if ((pty = rxvt_recv_fd (sock_fd)) < 0 426 if ((pty = recv_fd (sock_fd)) < 0
415 || (tty = rxvt_recv_fd (sock_fd)) < 0) 427 || (tty = recv_fd (sock_fd)) < 0)
416 rxvt_fatal ("protocol error while reading pty/tty fds from helper process, aborting.\n"); 428 ptytty_fatal ("protocol error while reading pty/tty fds from helper process, aborting.\n");
417 429
418 return true; 430 return true;
419} 431}
420 432
421void 433void
422rxvt_ptytty_proxy::login (int cmd_pid, bool login_shell, const char *hostname) 434ptytty_proxy::login (int cmd_pid, bool login_shell, const char *hostname)
423{ 435{
424 command cmd; 436 command cmd;
425 437
426 cmd.type = command::login; 438 cmd.type = command::login;
427 cmd.id = id; 439 cmd.id = id;
430 strncpy (cmd.hostname, hostname, sizeof (cmd.hostname)); 442 strncpy (cmd.hostname, hostname, sizeof (cmd.hostname));
431 443
432 write (sock_fd, &cmd, sizeof (cmd)); 444 write (sock_fd, &cmd, sizeof (cmd));
433} 445}
434 446
435rxvt_ptytty_proxy::~rxvt_ptytty_proxy () 447ptytty_proxy::~ptytty_proxy ()
436{ 448{
449 if (id)
450 {
437 command cmd; 451 command cmd;
438 452
439 cmd.type = command::destroy; 453 cmd.type = command::destroy;
440 cmd.id = id; 454 cmd.id = id;
441 455
442 write (sock_fd, &cmd, sizeof (cmd)); 456 write (sock_fd, &cmd, sizeof (cmd));
457 }
443} 458}
444 459
445static 460static
446void serve () 461void serve ()
447{ 462{
448 command cmd; 463 command cmd;
449 vector<rxvt_ptytty *> ptys; 464 vector<ptytty *> ptys;
450 465
451 while (read (sock_fd, &cmd, sizeof (command)) == sizeof (command)) 466 while (read (sock_fd, &cmd, sizeof (command)) == sizeof (command))
452 { 467 {
453 if (cmd.type == command::get) 468 if (cmd.type == command::get)
454 { 469 {
455 // -> id ptyfd ttyfd 470 // -> id ptyfd ttyfd
456 cmd.id = new rxvt_ptytty_unix; 471 cmd.id = new ptytty_unix;
457 472
458 if (cmd.id->get ()) 473 if (cmd.id->get ())
459 { 474 {
460 write (sock_fd, &cmd.id, sizeof (cmd.id)); 475 write (sock_fd, &cmd.id, sizeof (cmd.id));
461 ptys.push_back (cmd.id); 476 ptys.push_back (cmd.id);
462 477
463 rxvt_send_fd (sock_fd, cmd.id->pty); 478 ptytty::send_fd (sock_fd, cmd.id->pty);
464 rxvt_send_fd (sock_fd, cmd.id->tty); 479 ptytty::send_fd (sock_fd, cmd.id->tty);
465 } 480 }
466 else 481 else
467 { 482 {
468 delete cmd.id; 483 delete cmd.id;
469 cmd.id = 0; 484 cmd.id = 0;
471 } 486 }
472 } 487 }
473 else if (cmd.type == command::login) 488 else if (cmd.type == command::login)
474 { 489 {
475#if UTMP_SUPPORT 490#if UTMP_SUPPORT
476 if (find (ptys.begin (), ptys.end (), cmd.id)) 491 if (find (ptys.begin (), ptys.end (), cmd.id) != ptys.end ())
477 { 492 {
478 cmd.hostname[sizeof (cmd.hostname) - 1] = 0; 493 cmd.hostname[sizeof (cmd.hostname) - 1] = 0;
479 cmd.id->login (cmd.cmd_pid, cmd.login_shell, cmd.hostname); 494 cmd.id->login (cmd.cmd_pid, cmd.login_shell, cmd.hostname);
480 } 495 }
481#endif 496#endif
482 } 497 }
483 else if (cmd.type == command::destroy) 498 else if (cmd.type == command::destroy)
484 { 499 {
485 rxvt_ptytty **pty = find (ptys.begin (), ptys.end (), cmd.id); 500 vector<ptytty *>::iterator pty = find (ptys.begin (), ptys.end (), cmd.id);
486 501
487 if (pty) 502 if (pty != ptys.end ())
488 { 503 {
489 delete *pty; 504 delete *pty;
490 ptys.erase (pty); 505 ptys.erase (pty);
491 } 506 }
492 } 507 }
493 else 508 else
494 break; 509 break;
495 } 510 }
496 511
497 // destroy all ptys 512 // destroy all ptys
498 for (rxvt_ptytty **i = ptys.end (); i-- > ptys.begin (); ) 513 for (vector<ptytty *>::iterator i = ptys.end (); i-- > ptys.begin (); )
499 delete *i; 514 delete *i;
500} 515}
501 516
502void rxvt_ptytty_server () 517void
518ptytty::use_helper ()
503{ 519{
520 int pid = getpid ();
521
522 if (sock_fd >= 0 && pid == owner_pid)
523 return;
524
525 owner_pid = pid;
526
504 int sv[2]; 527 int sv[2];
505 528
506 if (socketpair (AF_UNIX, SOCK_STREAM, 0, sv)) 529 if (socketpair (AF_UNIX, SOCK_STREAM, 0, sv))
507 rxvt_fatal ("could not create socket to communicate with pty/sessiondb helper, aborting.\n"); 530 ptytty_fatal ("could not create socket to communicate with pty/sessiondb helper, aborting.\n");
508 531
532#ifdef PTYTTY_REENTRANT
533 int lv[2];
534
535 if (socketpair (AF_UNIX, SOCK_STREAM, 0, lv))
536 ptytty_fatal ("could not create socket to communicate with pty/sessiondb helper, aborting.\n");
537#endif
538
509 pid = fork (); 539 helper_pid = fork ();
510 540
511 if (pid < 0) 541 if (helper_pid < 0)
512 rxvt_fatal ("could not create pty/sessiondb helper process, aborting.\n"); 542 ptytty_fatal ("could not create pty/sessiondb helper process, aborting.\n");
513 543
514 if (pid) 544 if (helper_pid)
515 { 545 {
516 // client, urxvt 546 // client, process
517 sock_fd = sv[0]; 547 sock_fd = sv[0];
518 close (sv[1]); 548 close (sv[1]);
519 fcntl (sock_fd, F_SETFD, FD_CLOEXEC); 549 fcntl (sock_fd, F_SETFD, FD_CLOEXEC);
550#ifdef PTYTTY_REENTRANT
551 lock_fd = lv[0];
552 close (lv[1]);
553 fcntl (lock_fd, F_SETFD, FD_CLOEXEC);
554#endif
520 } 555 }
521 else 556 else
522 { 557 {
523 // server, pty-helper 558 // server, pty-helper
524 sock_fd = sv[1]; 559 sock_fd = sv[1];
560#ifdef PTYTTY_REENTRANT
561 lock_fd = lv[1];
562#endif
563
564 chdir ("/");
565
566 signal (SIGHUP, SIG_IGN);
567 signal (SIGTERM, SIG_IGN);
568 signal (SIGINT, SIG_IGN);
569 signal (SIGPIPE, SIG_IGN);
525 570
526 for (int fd = 0; fd < 1023; fd++) 571 for (int fd = 0; fd < 1023; fd++)
527 if (fd != sock_fd) 572 if (fd != sock_fd && fd != lock_fd)
528 close (fd); 573 close (fd);
529 574
530 serve (); 575 serve ();
531 _exit (EXIT_SUCCESS); 576 _exit (EXIT_SUCCESS);
532 } 577 }
533} 578}
534 579
535#endif 580#endif
536 581
537// a "factory" *g*
538rxvt_ptytty * 582ptytty *
539rxvt_new_ptytty () 583ptytty::create ()
540{ 584{
541#if PTYTTY_HELPER 585#if PTYTTY_HELPER
542 if (pid > 0) 586 if (helper_pid && getpid () == owner_pid)
543 // use helper process 587 // use helper process
544 return new rxvt_ptytty_proxy; 588 return new ptytty_proxy;
545 else 589 else
546#endif 590#endif
547 return new rxvt_ptytty_unix; 591 return new ptytty_unix;
548} 592}
549 593
550/*----------------------- end-of-file (C source) -----------------------*/ 594void
595ptytty::init ()
596{
597 uid_t uid = getuid ();
598 gid_t gid = getgid ();
599
600 // before doing anything else, check for setuid/setgid operation,
601 // start the helper process and drop privileges
602 if (uid != geteuid ()
603 || gid != getegid ())
604 {
605#if PTYTTY_HELPER
606 use_helper ();
607#else
608 ptytty_warn ("running setuid/setgid without pty helper compiled in, continuing unprivileged.\n");
609#endif
551 610
611 drop_privileges ();
612 }
613}
614
615void
616ptytty::drop_privileges ()
617{
618 uid_t uid = getuid ();
619 gid_t gid = getgid ();
620
621 // drop privileges
622#if HAVE_SETRESUID
623 setresgid (gid, gid, gid);
624 setresuid (uid, uid, uid);
625#elif HAVE_SETREUID
626 setregid (gid, gid);
627 setreuid (uid, uid);
628#elif HAVE_SETUID
629 setgid (gid);
630 setuid (uid);
631#endif
632
633 if (uid != geteuid ()
634 || gid != getegid ())
635 ptytty_fatal ("unable to drop privileges, aborting.\n");
636}
637
638/////////////////////////////////////////////////////////////////////////////
639// C API
640
641#ifndef NO_C_API
642
643#define DEFINE_METHOD(retval, name, args1, args2) \
644extern "C" retval ptytty_ ## name args1 \
645{ return ((struct ptytty *)ptytty)->name args2; }
646
647DEFINE_METHOD(int,pty,(void *ptytty),)
648DEFINE_METHOD(int,tty,(void *ptytty),)
649DEFINE_METHOD(int,get,(void *ptytty),())
650DEFINE_METHOD(void,login,(void *ptytty, int cmd_pid, bool login_shell, const char *hostname),(cmd_pid,login_shell,hostname))
651
652DEFINE_METHOD(void,close_tty,(void *ptytty),())
653DEFINE_METHOD(int,make_controlling_tty,(void *ptytty),())
654DEFINE_METHOD(void,set_utf8_mode,(void *ptytty, int on),(on))
655
656#define DEFINE_STATIC(retval, name, args) \
657extern "C" retval ptytty_ ## name args \
658{ return ptytty::name args; }
659
660DEFINE_STATIC(void,drop_privileges,())
661DEFINE_STATIC(void,use_helper,())
662DEFINE_STATIC(void,init,())
663
664DEFINE_STATIC(void *,create,())
665
666void ptytty_delete (void *ptytty)
667{
668 delete (struct ptytty *)ptytty;
669}
670
671// send_fd, recv_fd not exposed
672
673#endif

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines