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.56 by root, Sun Jan 22 04:01:52 2006 UTC

24 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 24 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 *---------------------------------------------------------------------*/ 25 *---------------------------------------------------------------------*/
26 26
27#include "../config.h" 27#include "../config.h"
28 28
29#include "fdpass.h"
30#include "ptytty.h" 29#include "ptytty.h"
31 30
32#include <cstdlib> 31#include <cstdlib>
33#include <cstring> 32#include <cstring>
34 33
235 234
236 ioctl (fd_tty, TIOCSCTTY, NULL); 235 ioctl (fd_tty, TIOCSCTTY, NULL);
237 236
238 int fd = open ("/dev/tty", O_WRONLY); 237 int fd = open ("/dev/tty", O_WRONLY);
239 if (fd < 0) 238 if (fd < 0)
240 return -1; /* fatal */ 239 return -1; /* fatal */
241 240
242 close (fd); 241 close (fd);
243 242
244 return 0; 243 return 0;
245} 244}
373 return true; 372 return true;
374} 373}
375 374
376#if PTYTTY_HELPER 375#if PTYTTY_HELPER
377 376
378static int sock_fd; 377static int sock_fd = -1;
379static int pid; 378static int helper_pid, owner_pid;
380 379
381struct command 380struct command
382{ 381{
383 enum { get, login, destroy } type; 382 enum { get, login, destroy } type;
384 383
412 cmd.type = command::get; 411 cmd.type = command::get;
413 412
414 write (sock_fd, &cmd, sizeof (cmd)); 413 write (sock_fd, &cmd, sizeof (cmd));
415 414
416 if (read (sock_fd, &id, sizeof (id)) != sizeof (id)) 415 if (read (sock_fd, &id, sizeof (id)) != sizeof (id))
417 fatal ("protocol error while creating pty using helper process, aborting.\n"); 416 ptytty_fatal ("protocol error while creating pty using helper process, aborting.\n");
418 417
419 if (!id) 418 if (!id)
420 return false; 419 return false;
421 420
422 if ((pty = ptytty_recv_fd (sock_fd)) < 0 421 if ((pty = recv_fd (sock_fd)) < 0
423 || (tty = ptytty_recv_fd (sock_fd)) < 0) 422 || (tty = recv_fd (sock_fd)) < 0)
424 fatal ("protocol error while reading pty/tty fds from helper process, aborting.\n"); 423 ptytty_fatal ("protocol error while reading pty/tty fds from helper process, aborting.\n");
425 424
426 return true; 425 return true;
427} 426}
428 427
429void 428void
469 if (cmd.id->get ()) 468 if (cmd.id->get ())
470 { 469 {
471 write (sock_fd, &cmd.id, sizeof (cmd.id)); 470 write (sock_fd, &cmd.id, sizeof (cmd.id));
472 ptys.push_back (cmd.id); 471 ptys.push_back (cmd.id);
473 472
474 ptytty_send_fd (sock_fd, cmd.id->pty); 473 ptytty::send_fd (sock_fd, cmd.id->pty);
475 ptytty_send_fd (sock_fd, cmd.id->tty); 474 ptytty::send_fd (sock_fd, cmd.id->tty);
476 } 475 }
477 else 476 else
478 { 477 {
479 delete cmd.id; 478 delete cmd.id;
480 cmd.id = 0; 479 cmd.id = 0;
491 } 490 }
492#endif 491#endif
493 } 492 }
494 else if (cmd.type == command::destroy) 493 else if (cmd.type == command::destroy)
495 { 494 {
496 ptytty **pty = find (ptys.begin (), ptys.end (), cmd.id); 495 vector<ptytty *>::iterator pty = find (ptys.begin (), ptys.end (), cmd.id);
497 496
498 if (pty) 497 if (pty != ptys.end ())
499 { 498 {
500 delete *pty; 499 delete *pty;
501 ptys.erase (pty); 500 ptys.erase (pty);
502 } 501 }
503 } 502 }
504 else 503 else
505 break; 504 break;
506 } 505 }
507 506
508 // destroy all ptys 507 // destroy all ptys
509 for (ptytty **i = ptys.end (); i-- > ptys.begin (); ) 508 for (vector<ptytty *>::iterator i = ptys.end (); i-- > ptys.begin (); )
510 delete *i; 509 delete *i;
511} 510}
512 511
513void ptytty_server () 512void
513ptytty::use_helper ()
514{ 514{
515 int pid = getpid ();
516
517 if (sock_fd >= 0 && pid == owner_pid)
518 return;
519
520 owner_pid = pid;
521
515 int sv[2]; 522 int sv[2];
516 523
517 if (socketpair (AF_UNIX, SOCK_STREAM, 0, sv)) 524 if (socketpair (AF_UNIX, SOCK_STREAM, 0, sv))
518 fatal ("could not create socket to communicate with pty/sessiondb helper, aborting.\n"); 525 ptytty_fatal ("could not create socket to communicate with pty/sessiondb helper, aborting.\n");
519 526
520 pid = fork (); 527 helper_pid = fork ();
521 528
522 if (pid < 0) 529 if (helper_pid < 0)
523 fatal ("could not create pty/sessiondb helper process, aborting.\n"); 530 ptytty_fatal ("could not create pty/sessiondb helper process, aborting.\n");
524 531
525 if (pid) 532 if (helper_pid)
526 { 533 {
527 // client, process 534 // client, process
528 sock_fd = sv[0]; 535 sock_fd = sv[0];
529 close (sv[1]); 536 close (sv[1]);
530 fcntl (sock_fd, F_SETFD, FD_CLOEXEC); 537 fcntl (sock_fd, F_SETFD, FD_CLOEXEC);
532 else 539 else
533 { 540 {
534 // server, pty-helper 541 // server, pty-helper
535 sock_fd = sv[1]; 542 sock_fd = sv[1];
536 543
544 chdir ("/");
545
537 for (int fd = 0; fd < 1023; fd++) 546 for (int fd = 0; fd < 1023; fd++)
538 if (fd != sock_fd) 547 if (fd != sock_fd)
539 close (fd); 548 close (fd);
540 549
541 serve (); 550 serve ();
543 } 552 }
544} 553}
545 554
546#endif 555#endif
547 556
548// a "factory" *g*
549ptytty * 557ptytty *
550new_ptytty () 558ptytty::create ()
551{ 559{
552#if PTYTTY_HELPER 560#if PTYTTY_HELPER
553 if (pid > 0) 561 if (helper_pid && getpid () == owner_pid)
554 // use helper process 562 // use helper process
555 return new ptytty_proxy; 563 return new ptytty_proxy;
556 else 564 else
557#endif 565#endif
558 return new ptytty_unix; 566 return new ptytty_unix;
559} 567}
560 568
561/*----------------------- end-of-file (C source) -----------------------*/ 569void
570ptytty::init ()
571{
572 uid_t uid = getuid ();
573 gid_t gid = getgid ();
574
575 // before doing anything else, check for setuid/setgid operation,
576 // start the helper process and drop privileges
577 if (uid != geteuid ()
578 || gid != getegid ())
579 {
580#if PTYTTY_HELPER
581 use_helper ();
582#else
583 ptytty_warn ("running setuid/setgid without pty helper compiled in, continuing unprivileged.\n");
584#endif
562 585
586 drop_privileges ();
587 }
588}
589
590void
591ptytty::drop_privileges ()
592{
593 uid_t uid = getuid ();
594 gid_t gid = getgid ();
595
596 // drop privileges
597#if HAVE_SETRESUID
598 setresgid (gid, gid, gid);
599 setresuid (uid, uid, uid);
600#elif HAVE_SETREUID
601 setregid (gid, gid);
602 setreuid (uid, uid);
603#elif HAVE_SETUID
604 setgid (gid);
605 setuid (uid);
606#endif
607
608 if (uid != geteuid ()
609 || gid != getegid ())
610 ptytty_fatal ("unable to drop privileges, aborting.\n");
611}
612

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines