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