ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/libptytty/src/ptytty.C
Revision: 1.14
Committed: Mon Jan 23 11:21:48 2006 UTC (18 years, 5 months ago) by root
Content type: text/plain
Branch: MAIN
Changes since 1.13: +34 -0 lines
Log Message:
*** empty log message ***

File Contents

# Content
1 // This file is part of libptytty. Do not make local modifications.
2 // http://software.schmorp.de/pkg/libptytty
3
4 /*----------------------------------------------------------------------*
5 * File: ptytty.C
6 *----------------------------------------------------------------------*
7 *
8 * All portions of code are copyright by their respective author/s.
9 * Copyright (c) 1999-2001 Geoff Wing <gcw@pobox.com>
10 * Copyright (c) 2004-2006 Marc Lehmann <pcg@goof.com>
11 * Copyright (c) 2006 Emanuele Giaquinta <e.giaquinta@glauco.it>
12 *
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or
16 * (at your option) any later version.
17 *
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
22 *
23 * You should have received a copy of the GNU General Public License
24 * along with this program; if not, write to the Free Software
25 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26 *---------------------------------------------------------------------*/
27
28 #include "../config.h"
29
30 #include "ptytty.h"
31
32 #include <cstdlib>
33 #include <cstring>
34
35 #include <sys/types.h>
36 #include <sys/socket.h>
37 #include <unistd.h>
38 #include <fcntl.h>
39
40 #ifdef HAVE_SYS_IOCTL_H
41 # include <sys/ioctl.h>
42 #endif
43 #if defined(HAVE_DEV_PTMX) && defined(HAVE_SYS_STROPTS_H)
44 # include <sys/stropts.h> /* for I_PUSH */
45 #endif
46 #ifdef HAVE_ISASTREAM
47 # include <stropts.h>
48 #endif
49 #if defined(HAVE_PTY_H)
50 # include <pty.h>
51 #elif defined(HAVE_LIBUTIL_H)
52 # include <libutil.h>
53 #elif defined(HAVE_UTIL_H)
54 # include <util.h>
55 #endif
56 #ifdef TTY_GID_SUPPORT
57 #include <grp.h>
58 #endif
59
60 #include <cstdio>
61
62 /////////////////////////////////////////////////////////////////////////////
63
64 /* ------------------------------------------------------------------------- *
65 * GET PSEUDO TELETYPE - MASTER AND SLAVE *
66 * ------------------------------------------------------------------------- */
67 /*
68 * Returns pty file descriptor, or -1 on failure
69 * If successful, ttydev is set to the name of the slave device.
70 * fd_tty _may_ also be set to an open fd to the slave device
71 */
72 #if defined(UNIX98_PTY)
73 static int
74 get_pty (int *fd_tty, char **ttydev)
75 {
76 int pfd;
77
78 # if defined(HAVE_GETPT)
79 pfd = getpt();
80 # elif defined(HAVE_POSIX_OPENPT)
81 pfd = posix_openpt (O_RDWR);
82 # else
83 pfd = open (CLONE_DEVICE, O_RDWR | O_NOCTTY, 0);
84 # endif
85 if (pfd >= 0)
86 {
87 if (grantpt (pfd) == 0 /* change slave permissions */
88 && unlockpt (pfd) == 0)
89 { /* slave now unlocked */
90 *ttydev = strdup (ptsname (pfd)); /* get slave's name */
91 return pfd;
92 }
93
94 close (pfd);
95 }
96
97 return -1;
98 }
99 #elif defined(HAVE_OPENPTY)
100 static int
101 get_pty (int *fd_tty, char **ttydev)
102 {
103 int pfd;
104 int res;
105 char tty_name[32];
106
107 res = openpty (&pfd, fd_tty, tty_name, NULL, NULL);
108 if (res != -1)
109 {
110 *ttydev = strdup (tty_name);
111 return pfd;
112 }
113
114 return -1;
115 }
116 #elif defined(HAVE__GETPTY)
117 static int
118 get_pty (int *fd_tty, char **ttydev)
119 {
120 int pfd;
121
122 *ttydev = _getpty (&pfd, O_RDWR | O_NONBLOCK | O_NOCTTY, 0622, 0);
123 if (*ttydev != NULL)
124 return pfd;
125
126 return -1;
127 }
128 #elif defined(HAVE_DEV_PTC)
129 static int
130 get_pty (int *fd_tty, char **ttydev)
131 {
132 int pfd;
133
134 if ((pfd = open ("/dev/ptc", O_RDWR | O_NOCTTY, 0)) >= 0)
135 {
136 *ttydev = strdup (ttyname (pfd));
137 return pfd;
138 }
139
140 return -1;
141 }
142 #elif defined(HAVE_DEV_CLONE)
143 static int
144 get_pty (int *fd_tty, char **ttydev)
145 {
146 int pfd;
147
148 if ((pfd = open ("/dev/ptym/clone", O_RDWR | O_NOCTTY, 0)) >= 0)
149 {
150 *ttydev = strdup (ptsname (pfd));
151 return pfd;
152 }
153
154 return -1;
155 }
156 #else
157 /* Based on the code in openssh/openbsd-compat/bsd-openpty.c */
158 static int
159 get_pty (int *fd_tty, char **ttydev)
160 {
161 int pfd;
162 int i;
163 char pty_name[32];
164 char tty_name[32];
165 const char *majors = "pqrstuvwxyzabcde";
166 const char *minors = "0123456789abcdef";
167 for (i = 0; i < 256; i++)
168 {
169 snprintf(pty_name, 32, "/dev/pty%c%c", majors[i / 16], minors[i % 16]);
170 snprintf(tty_name, 32, "/dev/tty%c%c", majors[i / 16], minors[i % 16]);
171 if ((pfd = open (pty_name, O_RDWR | O_NOCTTY, 0)) == -1)
172 {
173 snprintf(pty_name, 32, "/dev/ptyp%d", i);
174 snprintf(tty_name, 32, "/dev/ttyp%d", i);
175 if ((pfd = open (pty_name, O_RDWR | O_NOCTTY, 0)) == -1)
176 continue;
177 }
178 if (access (tty_name, R_OK | W_OK) == 0)
179 {
180 *ttydev = strdup (tty_name);
181 return pfd;
182 }
183
184 close (pfd);
185 }
186 }
187 #endif
188
189 /*----------------------------------------------------------------------*/
190 /*
191 * Returns tty file descriptor, or -1 on failure
192 */
193 static int
194 get_tty (char *ttydev)
195 {
196 return open (ttydev, O_RDWR | O_NOCTTY, 0);
197 }
198
199 /*----------------------------------------------------------------------*/
200 /*
201 * Make our tty a controlling tty so that /dev/tty points to us
202 */
203 static int
204 control_tty (int fd_tty)
205 {
206 setsid ();
207
208 #if defined(HAVE_DEV_PTMX) && defined(I_PUSH)
209 /*
210 * Push STREAMS modules:
211 * ptem: pseudo-terminal hardware emulation module.
212 * ldterm: standard terminal line discipline.
213 * ttcompat: V7, 4BSD and XENIX STREAMS compatibility module.
214 *
215 * After we push the STREAMS modules, the first open () on the slave side
216 * (i.e. the next section between the dashes giving us "tty opened OK")
217 * should make the "ptem" (or "ldterm" depending upon either which OS
218 * version or which set of manual pages you have) module give us a
219 * controlling terminal. We must already have close ()d the master side
220 * fd in this child process before we push STREAMS modules on because the
221 * documentation is really unclear about whether it is any close () on
222 * the master side or the last close () - i.e. a proper STREAMS dismantling
223 * close () - on the master side which causes a hang up to be sent
224 * through - Geoff Wing
225 */
226 # ifdef HAVE_ISASTREAM
227 if (isastream (fd_tty) == 1)
228 # endif
229 {
230 ioctl (fd_tty, I_PUSH, "ptem");
231 ioctl (fd_tty, I_PUSH, "ldterm");
232 ioctl (fd_tty, I_PUSH, "ttcompat");
233 }
234 #endif
235
236 ioctl (fd_tty, TIOCSCTTY, NULL);
237
238 int fd = open ("/dev/tty", O_WRONLY);
239 if (fd < 0)
240 return -1; /* fatal */
241
242 close (fd);
243
244 return 0;
245 }
246
247 void
248 ptytty::close_tty ()
249 {
250 if (tty < 0)
251 return;
252
253 close (tty);
254 tty = -1;
255 }
256
257 bool
258 ptytty::make_controlling_tty ()
259 {
260 return control_tty (tty) >= 0;
261 }
262
263 void
264 ptytty::set_utf8_mode (bool on)
265 {
266 #ifdef IUTF8
267 if (pty < 0)
268 return;
269
270 struct termios tio;
271
272 if (tcgetattr (pty, &tio) != -1)
273 {
274 tcflag_t new_cflag = tio.c_iflag;
275
276 if (on)
277 new_cflag |= IUTF8;
278 else
279 new_cflag &= ~IUTF8;
280
281 if (new_cflag != tio.c_iflag)
282 {
283 tio.c_iflag = new_cflag;
284 tcsetattr (pty, TCSANOW, &tio);
285 }
286 }
287 #endif
288 }
289
290 static struct ttyconf {
291 gid_t gid;
292 mode_t mode;
293
294 ttyconf ()
295 {
296 #ifdef TTY_GID_SUPPORT
297 struct group *gr = getgrnam ("tty");
298
299 if (gr)
300 { /* change group ownership of tty to "tty" */
301 mode = S_IRUSR | S_IWUSR | S_IWGRP;
302 gid = gr->gr_gid;
303 }
304 else
305 #endif /* TTY_GID_SUPPORT */
306 {
307 mode = S_IRUSR | S_IWUSR | S_IWGRP | S_IWOTH;
308 gid = 0;
309 }
310 }
311 } ttyconf;
312
313 ptytty_unix::ptytty_unix ()
314 {
315 name = 0;
316 #if UTMP_SUPPORT
317 cmd_pid = 0;
318 #endif
319 }
320
321 ptytty_unix::~ptytty_unix ()
322 {
323 #if UTMP_SUPPORT
324 logout ();
325 #endif
326 put ();
327 }
328
329 void
330 ptytty_unix::put ()
331 {
332 chmod (name, RESTORE_TTY_MODE);
333 chown (name, 0, ttyconf.gid);
334
335 close_tty ();
336
337 if (pty >= 0)
338 close (pty);
339
340 free (name);
341
342 pty = tty = -1;
343 name = 0;
344 }
345
346 bool
347 ptytty_unix::get ()
348 {
349 /* get master (pty) */
350 if ((pty = get_pty (&tty, &name)) < 0)
351 return false;
352
353 fcntl (pty, F_SETFL, O_NONBLOCK);
354
355 /* get slave (tty) */
356 if (tty < 0)
357 {
358 #ifndef NO_SETOWNER_TTYDEV
359 chown (name, getuid (), ttyconf.gid); /* fail silently */
360 chmod (name, ttyconf.mode);
361 # ifdef HAVE_REVOKE
362 revoke (name);
363 # endif
364 #endif
365
366 if ((tty = get_tty (name)) < 0)
367 {
368 put ();
369 return false;
370 }
371 }
372
373 return true;
374 }
375
376 /////////////////////////////////////////////////////////////////////////////
377 // helper/proxy support
378
379 #if PTYTTY_HELPER
380
381 static int sock_fd = -1;
382 static int helper_pid, owner_pid;
383
384 struct command
385 {
386 enum { get, login, destroy } type;
387
388 ptytty *id;
389
390 bool login_shell;
391 int cmd_pid;
392 char hostname[512]; // arbitrary, but should be plenty
393 };
394
395 struct ptytty_proxy : ptytty
396 {
397 ptytty *id;
398
399 ptytty_proxy ()
400 : id(0)
401 {
402 }
403
404 ~ptytty_proxy ();
405
406 bool get ();
407 void login (int cmd_pid, bool login_shell, const char *hostname);
408 };
409
410 bool
411 ptytty_proxy::get ()
412 {
413 command cmd;
414
415 cmd.type = command::get;
416
417 write (sock_fd, &cmd, sizeof (cmd));
418
419 if (read (sock_fd, &id, sizeof (id)) != sizeof (id))
420 ptytty_fatal ("protocol error while creating pty using helper process, aborting.\n");
421
422 if (!id)
423 return false;
424
425 if ((pty = recv_fd (sock_fd)) < 0
426 || (tty = recv_fd (sock_fd)) < 0)
427 ptytty_fatal ("protocol error while reading pty/tty fds from helper process, aborting.\n");
428
429 return true;
430 }
431
432 void
433 ptytty_proxy::login (int cmd_pid, bool login_shell, const char *hostname)
434 {
435 command cmd;
436
437 cmd.type = command::login;
438 cmd.id = id;
439 cmd.cmd_pid = cmd_pid;
440 cmd.login_shell = login_shell;
441 strncpy (cmd.hostname, hostname, sizeof (cmd.hostname));
442
443 write (sock_fd, &cmd, sizeof (cmd));
444 }
445
446 ptytty_proxy::~ptytty_proxy ()
447 {
448 if (id)
449 {
450 command cmd;
451
452 cmd.type = command::destroy;
453 cmd.id = id;
454
455 write (sock_fd, &cmd, sizeof (cmd));
456 }
457 }
458
459 static
460 void serve ()
461 {
462 command cmd;
463 vector<ptytty *> ptys;
464
465 while (read (sock_fd, &cmd, sizeof (command)) == sizeof (command))
466 {
467 if (cmd.type == command::get)
468 {
469 // -> id ptyfd ttyfd
470 cmd.id = new ptytty_unix;
471
472 if (cmd.id->get ())
473 {
474 write (sock_fd, &cmd.id, sizeof (cmd.id));
475 ptys.push_back (cmd.id);
476
477 ptytty::send_fd (sock_fd, cmd.id->pty);
478 ptytty::send_fd (sock_fd, cmd.id->tty);
479 }
480 else
481 {
482 delete cmd.id;
483 cmd.id = 0;
484 write (sock_fd, &cmd.id, sizeof (cmd.id));
485 }
486 }
487 else if (cmd.type == command::login)
488 {
489 #if UTMP_SUPPORT
490 if (find (ptys.begin (), ptys.end (), cmd.id) != ptys.end ())
491 {
492 cmd.hostname[sizeof (cmd.hostname) - 1] = 0;
493 cmd.id->login (cmd.cmd_pid, cmd.login_shell, cmd.hostname);
494 }
495 #endif
496 }
497 else if (cmd.type == command::destroy)
498 {
499 vector<ptytty *>::iterator pty = find (ptys.begin (), ptys.end (), cmd.id);
500
501 if (pty != ptys.end ())
502 {
503 delete *pty;
504 ptys.erase (pty);
505 }
506 }
507 else
508 break;
509 }
510
511 // destroy all ptys
512 for (vector<ptytty *>::iterator i = ptys.end (); i-- > ptys.begin (); )
513 delete *i;
514 }
515
516 void
517 ptytty::use_helper ()
518 {
519 int pid = getpid ();
520
521 if (sock_fd >= 0 && pid == owner_pid)
522 return;
523
524 owner_pid = pid;
525
526 int sv[2];
527
528 if (socketpair (AF_UNIX, SOCK_STREAM, 0, sv))
529 ptytty_fatal ("could not create socket to communicate with pty/sessiondb helper, aborting.\n");
530
531 helper_pid = fork ();
532
533 if (helper_pid < 0)
534 ptytty_fatal ("could not create pty/sessiondb helper process, aborting.\n");
535
536 if (helper_pid)
537 {
538 // client, process
539 sock_fd = sv[0];
540 close (sv[1]);
541 fcntl (sock_fd, F_SETFD, FD_CLOEXEC);
542 }
543 else
544 {
545 // server, pty-helper
546 sock_fd = sv[1];
547
548 chdir ("/");
549
550 for (int fd = 0; fd < 1023; fd++)
551 if (fd != sock_fd)
552 close (fd);
553
554 serve ();
555 _exit (EXIT_SUCCESS);
556 }
557 }
558
559 #endif
560
561 ptytty *
562 ptytty::create ()
563 {
564 #if PTYTTY_HELPER
565 if (helper_pid && getpid () == owner_pid)
566 // use helper process
567 return new ptytty_proxy;
568 else
569 #endif
570 return new ptytty_unix;
571 }
572
573 void
574 ptytty::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
589
590 drop_privileges ();
591 }
592 }
593
594 void
595 ptytty::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 #define DEFINE_METHOD(retval, name, args1, args2) \
621 extern "C" retval ptytty_ ## name args1 \
622 { return ((struct ptytty *)ptytty)->name args2; }
623
624 DEFINE_METHOD(int,pty,(void *ptytty),)
625 DEFINE_METHOD(int,tty,(void *ptytty),)
626 DEFINE_METHOD(int,get,(void *ptytty),())
627 DEFINE_METHOD(void,login,(void *ptytty, int cmd_pid, bool login_shell, const char *hostname),(cmd_pid,login_shell,hostname))
628
629 DEFINE_METHOD(void,close_tty,(void *ptytty),())
630 DEFINE_METHOD(int,make_controlling_tty,(void *ptytty),())
631 DEFINE_METHOD(void,set_utf8_mode,(void *ptytty, int on),(on))
632
633 #define DEFINE_STATIC(retval, name, args) \
634 extern "C" retval ptytty_ ## name args \
635 { return ptytty::name args; }
636
637 DEFINE_STATIC(void,init,())
638 DEFINE_STATIC(void *,create,())
639
640 void ptytty_delete (void *ptytty)
641 {
642 delete (struct ptytty *)ptytty;
643 }
644
645 DEFINE_STATIC(void,drop_privileges,())
646 DEFINE_STATIC(void,use_helper,())
647