… | |
… | |
6 | *----------------------------------------------------------------------* |
6 | *----------------------------------------------------------------------* |
7 | * |
7 | * |
8 | * All portions of code are copyright by their respective author/s. |
8 | * All portions of code are copyright by their respective author/s. |
9 | * Copyright (c) 1999-2001 Geoff Wing <gcw@pobox.com> |
9 | * Copyright (c) 1999-2001 Geoff Wing <gcw@pobox.com> |
10 | * 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> |
11 | * |
12 | * |
12 | * 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 |
13 | * 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 |
14 | * the Free Software Foundation; either version 2 of the License, or |
15 | * the Free Software Foundation; either version 2 of the License, or |
15 | * (at your option) any later version. |
16 | * (at your option) any later version. |
… | |
… | |
24 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
25 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
25 | *---------------------------------------------------------------------*/ |
26 | *---------------------------------------------------------------------*/ |
26 | |
27 | |
27 | #include "../config.h" |
28 | #include "../config.h" |
28 | |
29 | |
29 | #include "fdpass.h" |
|
|
30 | #include "ptytty.h" |
30 | #include "ptytty.h" |
31 | |
31 | |
32 | #include <cstdlib> |
32 | #include <cstdlib> |
33 | #include <cstring> |
33 | #include <cstring> |
|
|
34 | #include <csignal> |
34 | |
35 | |
35 | #include <sys/types.h> |
36 | #include <sys/types.h> |
36 | #include <sys/socket.h> |
|
|
37 | #include <unistd.h> |
37 | #include <unistd.h> |
38 | #include <fcntl.h> |
38 | #include <fcntl.h> |
39 | |
39 | |
40 | #ifdef HAVE_SYS_IOCTL_H |
40 | #ifdef HAVE_SYS_IOCTL_H |
41 | # include <sys/ioctl.h> |
41 | # include <sys/ioctl.h> |
… | |
… | |
181 | return pfd; |
181 | return pfd; |
182 | } |
182 | } |
183 | |
183 | |
184 | close (pfd); |
184 | close (pfd); |
185 | } |
185 | } |
|
|
186 | |
|
|
187 | return -1; |
186 | } |
188 | } |
187 | #endif |
189 | #endif |
188 | |
190 | |
189 | /*----------------------------------------------------------------------*/ |
191 | /*----------------------------------------------------------------------*/ |
190 | /* |
192 | /* |
… | |
… | |
235 | |
237 | |
236 | ioctl (fd_tty, TIOCSCTTY, NULL); |
238 | ioctl (fd_tty, TIOCSCTTY, NULL); |
237 | |
239 | |
238 | int fd = open ("/dev/tty", O_WRONLY); |
240 | int fd = open ("/dev/tty", O_WRONLY); |
239 | if (fd < 0) |
241 | if (fd < 0) |
240 | return -1; /* fatal */ |
242 | return -1; /* fatal */ |
241 | |
243 | |
242 | close (fd); |
244 | close (fd); |
243 | |
245 | |
244 | return 0; |
246 | return 0; |
245 | } |
247 | } |
… | |
… | |
371 | } |
373 | } |
372 | |
374 | |
373 | return true; |
375 | return true; |
374 | } |
376 | } |
375 | |
377 | |
376 | #if PTYTTY_HELPER |
|
|
377 | |
|
|
378 | static int sock_fd; |
|
|
379 | static int pid; |
|
|
380 | |
|
|
381 | struct command |
|
|
382 | { |
|
|
383 | enum { get, login, destroy } type; |
|
|
384 | |
|
|
385 | ptytty *id; |
|
|
386 | |
|
|
387 | bool login_shell; |
|
|
388 | int cmd_pid; |
|
|
389 | char hostname[512]; // arbitrary, but should be plenty |
|
|
390 | }; |
|
|
391 | |
|
|
392 | struct ptytty_proxy : ptytty |
|
|
393 | { |
|
|
394 | ptytty *id; |
|
|
395 | |
|
|
396 | ptytty_proxy () |
|
|
397 | : id(0) |
|
|
398 | { |
|
|
399 | } |
|
|
400 | |
|
|
401 | ~ptytty_proxy (); |
|
|
402 | |
|
|
403 | bool get (); |
|
|
404 | void login (int cmd_pid, bool login_shell, const char *hostname); |
|
|
405 | }; |
|
|
406 | |
|
|
407 | bool |
|
|
408 | ptytty_proxy::get () |
|
|
409 | { |
|
|
410 | command cmd; |
|
|
411 | |
|
|
412 | cmd.type = command::get; |
|
|
413 | |
|
|
414 | write (sock_fd, &cmd, sizeof (cmd)); |
|
|
415 | |
|
|
416 | if (read (sock_fd, &id, sizeof (id)) != sizeof (id)) |
|
|
417 | fatal ("protocol error while creating pty using helper process, aborting.\n"); |
|
|
418 | |
|
|
419 | if (!id) |
|
|
420 | return false; |
|
|
421 | |
|
|
422 | if ((pty = ptytty_recv_fd (sock_fd)) < 0 |
|
|
423 | || (tty = ptytty_recv_fd (sock_fd)) < 0) |
|
|
424 | fatal ("protocol error while reading pty/tty fds from helper process, aborting.\n"); |
|
|
425 | |
|
|
426 | return true; |
|
|
427 | } |
|
|
428 | |
|
|
429 | void |
|
|
430 | ptytty_proxy::login (int cmd_pid, bool login_shell, const char *hostname) |
|
|
431 | { |
|
|
432 | command cmd; |
|
|
433 | |
|
|
434 | cmd.type = command::login; |
|
|
435 | cmd.id = id; |
|
|
436 | cmd.cmd_pid = cmd_pid; |
|
|
437 | cmd.login_shell = login_shell; |
|
|
438 | strncpy (cmd.hostname, hostname, sizeof (cmd.hostname)); |
|
|
439 | |
|
|
440 | write (sock_fd, &cmd, sizeof (cmd)); |
|
|
441 | } |
|
|
442 | |
|
|
443 | ptytty_proxy::~ptytty_proxy () |
|
|
444 | { |
|
|
445 | if (id) |
|
|
446 | { |
|
|
447 | command cmd; |
|
|
448 | |
|
|
449 | cmd.type = command::destroy; |
|
|
450 | cmd.id = id; |
|
|
451 | |
|
|
452 | write (sock_fd, &cmd, sizeof (cmd)); |
|
|
453 | } |
|
|
454 | } |
|
|
455 | |
|
|
456 | static |
|
|
457 | void serve () |
|
|
458 | { |
|
|
459 | command cmd; |
|
|
460 | vector<ptytty *> ptys; |
|
|
461 | |
|
|
462 | while (read (sock_fd, &cmd, sizeof (command)) == sizeof (command)) |
|
|
463 | { |
|
|
464 | if (cmd.type == command::get) |
|
|
465 | { |
|
|
466 | // -> id ptyfd ttyfd |
|
|
467 | cmd.id = new ptytty_unix; |
|
|
468 | |
|
|
469 | if (cmd.id->get ()) |
|
|
470 | { |
|
|
471 | write (sock_fd, &cmd.id, sizeof (cmd.id)); |
|
|
472 | ptys.push_back (cmd.id); |
|
|
473 | |
|
|
474 | ptytty_send_fd (sock_fd, cmd.id->pty); |
|
|
475 | ptytty_send_fd (sock_fd, cmd.id->tty); |
|
|
476 | } |
|
|
477 | else |
|
|
478 | { |
|
|
479 | delete cmd.id; |
|
|
480 | cmd.id = 0; |
|
|
481 | write (sock_fd, &cmd.id, sizeof (cmd.id)); |
|
|
482 | } |
|
|
483 | } |
|
|
484 | else if (cmd.type == command::login) |
|
|
485 | { |
|
|
486 | #if UTMP_SUPPORT |
|
|
487 | if (find (ptys.begin (), ptys.end (), cmd.id)) |
|
|
488 | { |
|
|
489 | cmd.hostname[sizeof (cmd.hostname) - 1] = 0; |
|
|
490 | cmd.id->login (cmd.cmd_pid, cmd.login_shell, cmd.hostname); |
|
|
491 | } |
|
|
492 | #endif |
|
|
493 | } |
|
|
494 | else if (cmd.type == command::destroy) |
|
|
495 | { |
|
|
496 | ptytty **pty = find (ptys.begin (), ptys.end (), cmd.id); |
|
|
497 | |
|
|
498 | if (pty) |
|
|
499 | { |
|
|
500 | delete *pty; |
|
|
501 | ptys.erase (pty); |
|
|
502 | } |
|
|
503 | } |
|
|
504 | else |
|
|
505 | break; |
|
|
506 | } |
|
|
507 | |
|
|
508 | // destroy all ptys |
|
|
509 | for (ptytty **i = ptys.end (); i-- > ptys.begin (); ) |
|
|
510 | delete *i; |
|
|
511 | } |
|
|
512 | |
|
|
513 | void ptytty_server () |
|
|
514 | { |
|
|
515 | int sv[2]; |
|
|
516 | |
|
|
517 | if (socketpair (AF_UNIX, SOCK_STREAM, 0, sv)) |
|
|
518 | fatal ("could not create socket to communicate with pty/sessiondb helper, aborting.\n"); |
|
|
519 | |
|
|
520 | pid = fork (); |
|
|
521 | |
|
|
522 | if (pid < 0) |
|
|
523 | fatal ("could not create pty/sessiondb helper process, aborting.\n"); |
|
|
524 | |
|
|
525 | if (pid) |
|
|
526 | { |
|
|
527 | // client, process |
|
|
528 | sock_fd = sv[0]; |
|
|
529 | close (sv[1]); |
|
|
530 | fcntl (sock_fd, F_SETFD, FD_CLOEXEC); |
|
|
531 | } |
|
|
532 | else |
|
|
533 | { |
|
|
534 | // server, pty-helper |
|
|
535 | sock_fd = sv[1]; |
|
|
536 | |
|
|
537 | for (int fd = 0; fd < 1023; fd++) |
|
|
538 | if (fd != sock_fd) |
|
|
539 | close (fd); |
|
|
540 | |
|
|
541 | serve (); |
|
|
542 | _exit (EXIT_SUCCESS); |
|
|
543 | } |
|
|
544 | } |
|
|
545 | |
|
|
546 | #endif |
|
|
547 | |
|
|
548 | // a "factory" *g* |
|
|
549 | ptytty * |
|
|
550 | new_ptytty () |
|
|
551 | { |
|
|
552 | #if PTYTTY_HELPER |
|
|
553 | if (pid > 0) |
|
|
554 | // use helper process |
|
|
555 | return new ptytty_proxy; |
|
|
556 | else |
|
|
557 | #endif |
|
|
558 | return new ptytty_unix; |
|
|
559 | } |
|
|
560 | |
|
|
561 | /*----------------------- end-of-file (C source) -----------------------*/ |
|
|
562 | |
|
|