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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines