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.45 by root, Tue Jan 17 18:06:14 2006 UTC vs.
Revision 1.56 by root, Sun Jan 22 04:01:52 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>
19 * You should have received a copy of the GNU General Public License 22 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software 23 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 24 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 *---------------------------------------------------------------------*/ 25 *---------------------------------------------------------------------*/
23 26
24#include "../config.h" /* NECESSARY */ 27#include "../config.h"
28
25#include "rxvt.h" 29#include "ptytty.h"
26 30
27#include <cstdlib> 31#include <cstdlib>
28#include <cstring> 32#include <cstring>
29 33
30#include <sys/types.h> 34#include <sys/types.h>
33#include <fcntl.h> 37#include <fcntl.h>
34 38
35#ifdef HAVE_SYS_IOCTL_H 39#ifdef HAVE_SYS_IOCTL_H
36# include <sys/ioctl.h> 40# include <sys/ioctl.h>
37#endif 41#endif
38#if defined(PTYS_ARE_PTMX) && defined(HAVE_SYS_STROPTS_H) 42#if defined(HAVE_DEV_PTMX) && defined(HAVE_SYS_STROPTS_H)
39# include <sys/stropts.h> /* for I_PUSH */ 43# include <sys/stropts.h> /* for I_PUSH */
40#endif 44#endif
41#ifdef HAVE_ISASTREAM 45#ifdef HAVE_ISASTREAM
42# include <stropts.h> 46# include <stropts.h>
43#endif 47#endif
46#elif defined(HAVE_LIBUTIL_H) 50#elif defined(HAVE_LIBUTIL_H)
47# include <libutil.h> 51# include <libutil.h>
48#elif defined(HAVE_UTIL_H) 52#elif defined(HAVE_UTIL_H)
49# include <util.h> 53# include <util.h>
50#endif 54#endif
55#ifdef TTY_GID_SUPPORT
56#include <grp.h>
57#endif
51 58
52#include <cstdio> 59#include <cstdio>
53#include <grp.h>
54
55#include "rxvtutil.h"
56#include "fdpass.h"
57#include "ptytty.h"
58 60
59///////////////////////////////////////////////////////////////////////////// 61/////////////////////////////////////////////////////////////////////////////
60 62
61/* ------------------------------------------------------------------------- * 63/* ------------------------------------------------------------------------- *
62 * GET PSEUDO TELETYPE - MASTER AND SLAVE * 64 * GET PSEUDO TELETYPE - MASTER AND SLAVE *
64/* 66/*
65 * Returns pty file descriptor, or -1 on failure 67 * Returns pty file descriptor, or -1 on failure
66 * If successful, ttydev is set to the name of the slave device. 68 * If successful, ttydev is set to the name of the slave device.
67 * fd_tty _may_ also be set to an open fd to the slave device 69 * fd_tty _may_ also be set to an open fd to the slave device
68 */ 70 */
69static inline int 71#if defined(UNIX98_PTY)
72static int
70get_pty_streams (int *fd_tty, char **ttydev) 73get_pty (int *fd_tty, char **ttydev)
71{ 74{
72#ifdef NO_SETOWNER_TTYDEV
73 int pfd; 75 int pfd;
74 76
75# if defined(PTYS_ARE_GETPT) 77# if defined(HAVE_GETPT)
76 pfd = getpt(); 78 pfd = getpt();
77# elif defined(PTYS_ARE_POSIX) 79# elif defined(HAVE_POSIX_OPENPT)
78 pfd = posix_openpt (O_RDWR); 80 pfd = posix_openpt (O_RDWR);
79# else 81# else
80 pfd = open ("/dev/ptmx", O_RDWR | O_NOCTTY, 0); 82 pfd = open (CLONE_DEVICE, O_RDWR | O_NOCTTY, 0);
81# endif 83# endif
82 if (pfd >= 0) 84 if (pfd >= 0)
83 { 85 {
84 if (grantpt (pfd) == 0 /* change slave permissions */ 86 if (grantpt (pfd) == 0 /* change slave permissions */
85 && unlockpt (pfd) == 0) 87 && unlockpt (pfd) == 0)
88 return pfd; 90 return pfd;
89 } 91 }
90 92
91 close (pfd); 93 close (pfd);
92 } 94 }
93#endif
94 95
95 return -1; 96 return -1;
96} 97}
97 98#elif defined(HAVE_OPENPTY)
98static inline int 99static int
99get_pty_openpty (int *fd_tty, char **ttydev) 100get_pty (int *fd_tty, char **ttydev)
100{ 101{
101#ifdef PTYS_ARE_OPENPTY
102 int pfd; 102 int pfd;
103 int res; 103 int res;
104 char tty_name[sizeof "/dev/pts/????\0"]; 104 char tty_name[32];
105 105
106 res = openpty (&pfd, fd_tty, tty_name, NULL, NULL); 106 res = openpty (&pfd, fd_tty, tty_name, NULL, NULL);
107 if (res != -1) 107 if (res != -1)
108 { 108 {
109 *ttydev = strdup (tty_name); 109 *ttydev = strdup (tty_name);
110 return pfd; 110 return pfd;
111 } 111 }
112#endif
113 112
114 return -1; 113 return -1;
115} 114}
116 115#elif defined(HAVE__GETPTY)
117static inline int 116static int
118get_pty__getpty (int *fd_tty, char **ttydev) 117get_pty (int *fd_tty, char **ttydev)
119{ 118{
120#ifdef PTYS_ARE__GETPTY
121 int pfd; 119 int pfd;
122 120
123 *ttydev = _getpty (&pfd, O_RDWR | O_NONBLOCK | O_NOCTTY, 0622, 0); 121 *ttydev = _getpty (&pfd, O_RDWR | O_NONBLOCK | O_NOCTTY, 0622, 0);
124 if (*ttydev != NULL) 122 if (*ttydev != NULL)
125 return pfd; 123 return pfd;
126#endif
127 124
128 return -1; 125 return -1;
129} 126}
130 127#elif defined(HAVE_DEV_PTC)
131static inline int 128static int
132get_pty_ptc (int *fd_tty, char **ttydev) 129get_pty (int *fd_tty, char **ttydev)
133{ 130{
134#ifdef PTYS_ARE_PTC
135 int pfd; 131 int pfd;
136 132
137 if ((pfd = open ("/dev/ptc", O_RDWR | O_NOCTTY, 0)) >= 0) 133 if ((pfd = open ("/dev/ptc", O_RDWR | O_NOCTTY, 0)) >= 0)
138 { 134 {
139 *ttydev = strdup (ttyname (pfd)); 135 *ttydev = strdup (ttyname (pfd));
140 return pfd; 136 return pfd;
141 } 137 }
142#endif
143 138
144 return -1; 139 return -1;
145} 140}
146 141#elif defined(HAVE_DEV_CLONE)
147static inline int 142static int
148get_pty_clone (int *fd_tty, char **ttydev) 143get_pty (int *fd_tty, char **ttydev)
149{ 144{
150#ifdef PTYS_ARE_CLONE
151 int pfd; 145 int pfd;
152 146
153 if ((pfd = open ("/dev/ptym/clone", O_RDWR | O_NOCTTY, 0)) >= 0) 147 if ((pfd = open ("/dev/ptym/clone", O_RDWR | O_NOCTTY, 0)) >= 0)
154 { 148 {
155 *ttydev = strdup (ptsname (pfd)); 149 *ttydev = strdup (ptsname (pfd));
156 return pfd; 150 return pfd;
157 } 151 }
158#endif
159 152
160 return -1; 153 return -1;
161} 154}
162 155#else
163static inline int 156/* Based on the code in openssh/openbsd-compat/bsd-openpty.c */
157static int
164get_pty_numeric (int *fd_tty, char **ttydev) 158get_pty (int *fd_tty, char **ttydev)
165{ 159{
166#ifdef PTYS_ARE_NUMERIC
167 int pfd; 160 int pfd;
168 int idx; 161 int i;
169 char *c1, *c2; 162 char pty_name[32];
170 char pty_name[] = "/dev/ptyp???"; 163 char tty_name[32];
171 char tty_name[] = "/dev/ttyp???"; 164 const char *majors = "pqrstuvwxyzabcde";
172 165 const char *minors = "0123456789abcdef";
173 c1 = &(pty_name[sizeof (pty_name) - 4]);
174 c2 = &(tty_name[sizeof (tty_name) - 4]);
175
176 for (idx = 0; idx < 256; idx++) 166 for (i = 0; i < 256; i++)
177 { 167 {
178 sprintf (c1, "%d", idx); 168 snprintf(pty_name, 32, "/dev/pty%c%c", majors[i / 16], minors[i % 16]);
179 sprintf (c2, "%d", idx); 169 snprintf(tty_name, 32, "/dev/tty%c%c", majors[i / 16], minors[i % 16]);
180
181 if (access (tty_name, F_OK) < 0)
182 {
183 idx = 256;
184 break;
185 }
186
187 if ((pfd = open (pty_name, O_RDWR | O_NOCTTY, 0)) >= 0) 170 if ((pfd = open (pty_name, O_RDWR | O_NOCTTY, 0)) == -1)
171 {
172 snprintf(pty_name, 32, "/dev/ptyp%d", i);
173 snprintf(tty_name, 32, "/dev/ttyp%d", i);
174 if ((pfd = open (pty_name, O_RDWR | O_NOCTTY, 0)) == -1)
175 continue;
188 { 176 }
189 if (access (tty_name, R_OK | W_OK) == 0) 177 if (access (tty_name, R_OK | W_OK) == 0)
190 { 178 {
191 *ttydev = strdup (tty_name); 179 *ttydev = strdup (tty_name);
192 return pfd; 180 return pfd;
193 } 181 }
194 182
195 close (pfd); 183 close (pfd);
196 }
197 } 184 }
198#endif
199
200 return -1;
201} 185}
202
203static inline int
204get_pty_searched (int *fd_tty, char **ttydev)
205{
206#ifdef PTYS_ARE_SEARCHED
207# ifndef PTYCHAR1
208# define PTYCHAR1 "pqrstuvwxyz"
209# endif 186#endif
210# ifndef PTYCHAR2
211# define PTYCHAR2 "0123456789abcdef"
212# endif
213 int pfd;
214 const char *c1, *c2;
215 char pty_name[] = "/dev/pty??";
216 char tty_name[] = "/dev/tty??";
217
218 for (c1 = PTYCHAR1; *c1; c1++)
219 {
220 pty_name[ (sizeof (pty_name) - 3)] =
221 tty_name[ (sizeof (pty_name) - 3)] = *c1;
222
223 for (c2 = PTYCHAR2; *c2; c2++)
224 {
225 pty_name[ (sizeof (pty_name) - 2)] =
226 tty_name[ (sizeof (pty_name) - 2)] = *c2;
227
228 if ((pfd = open (pty_name, O_RDWR | O_NOCTTY, 0)) >= 0)
229 {
230 if (access (tty_name, R_OK | W_OK) == 0)
231 {
232 *ttydev = strdup (tty_name);
233 return pfd;
234 }
235
236 close (pfd);
237 }
238 }
239 }
240#endif
241
242 return -1;
243}
244
245static int
246get_pty (int *fd_tty, char **ttydev)
247{
248 int pfd;
249
250 if ((pfd = get_pty_streams (fd_tty, ttydev)) != -1
251 || (pfd = get_pty_openpty (fd_tty, ttydev)) != -1
252 || (pfd = get_pty__getpty (fd_tty, ttydev)) != -1
253 || (pfd = get_pty_ptc (fd_tty, ttydev)) != -1
254 || (pfd = get_pty_clone (fd_tty, ttydev)) != -1
255 || (pfd = get_pty_numeric (fd_tty, ttydev)) != -1
256 || (pfd = get_pty_searched (fd_tty, ttydev)) != -1)
257 return pfd;
258
259 return -1;
260}
261 187
262/*----------------------------------------------------------------------*/ 188/*----------------------------------------------------------------------*/
263/* 189/*
264 * Returns tty file descriptor, or -1 on failure 190 * Returns tty file descriptor, or -1 on failure
265 */ 191 */
276static int 202static int
277control_tty (int fd_tty) 203control_tty (int fd_tty)
278{ 204{
279 setsid (); 205 setsid ();
280 206
281# if defined(PTYS_ARE_PTMX) && defined(I_PUSH) 207#if defined(HAVE_DEV_PTMX) && defined(I_PUSH)
282 /* 208 /*
283 * Push STREAMS modules: 209 * Push STREAMS modules:
284 * ptem: pseudo-terminal hardware emulation module. 210 * ptem: pseudo-terminal hardware emulation module.
285 * ldterm: standard terminal line discipline. 211 * ldterm: standard terminal line discipline.
286 * ttcompat: V7, 4BSD and XENIX STREAMS compatibility module. 212 * ttcompat: V7, 4BSD and XENIX STREAMS compatibility module.
294 * documentation is really unclear about whether it is any close () on 220 * documentation is really unclear about whether it is any close () on
295 * the master side or the last close () - i.e. a proper STREAMS dismantling 221 * the master side or the last close () - i.e. a proper STREAMS dismantling
296 * close () - on the master side which causes a hang up to be sent 222 * close () - on the master side which causes a hang up to be sent
297 * through - Geoff Wing 223 * through - Geoff Wing
298 */ 224 */
299# ifdef HAVE_ISASTREAM 225# ifdef HAVE_ISASTREAM
300 if (isastream (fd_tty) == 1) 226 if (isastream (fd_tty) == 1)
301# endif 227# endif
302 { 228 {
303 ioctl (fd_tty, I_PUSH, "ptem"); 229 ioctl (fd_tty, I_PUSH, "ptem");
304 ioctl (fd_tty, I_PUSH, "ldterm"); 230 ioctl (fd_tty, I_PUSH, "ldterm");
305 ioctl (fd_tty, I_PUSH, "ttcompat"); 231 ioctl (fd_tty, I_PUSH, "ttcompat");
306 } 232 }
307# endif 233#endif
308 234
309 ioctl (fd_tty, TIOCSCTTY, NULL); 235 ioctl (fd_tty, TIOCSCTTY, NULL);
310 236
311 int fd = open ("/dev/tty", O_WRONLY); 237 int fd = open ("/dev/tty", O_WRONLY);
312 if (fd < 0) 238 if (fd < 0)
313 return -1; /* fatal */ 239 return -1; /* fatal */
314 240
315 close (fd); 241 close (fd);
316 242
317 return 0; 243 return 0;
318} 244}
319 245
320void 246void
321rxvt_ptytty::close_tty () 247ptytty::close_tty ()
322{ 248{
323 if (tty < 0) 249 if (tty < 0)
324 return; 250 return;
325 251
326 close (tty); 252 close (tty);
327 tty = -1; 253 tty = -1;
328} 254}
329 255
330bool 256bool
331rxvt_ptytty::make_controlling_tty () 257ptytty::make_controlling_tty ()
332{ 258{
333 return control_tty (tty) >= 0; 259 return control_tty (tty) >= 0;
334} 260}
335 261
336void 262void
337rxvt_ptytty::set_utf8_mode (bool on) 263ptytty::set_utf8_mode (bool on)
338{ 264{
339#ifdef IUTF8 265#ifdef IUTF8
340 if (pty < 0) 266 if (pty < 0)
341 return; 267 return;
342 268
358 } 284 }
359 } 285 }
360#endif 286#endif
361} 287}
362 288
363/////////////////////////////////////////////////////////////////////////////
364
365#ifndef NO_SETOWNER_TTYDEV
366static struct ttyconf { 289static struct ttyconf {
367 gid_t gid; 290 gid_t gid;
368 mode_t mode; 291 mode_t mode;
369 292
370 ttyconf () 293 ttyconf ()
379 } 302 }
380 else 303 else
381#endif /* TTY_GID_SUPPORT */ 304#endif /* TTY_GID_SUPPORT */
382 { 305 {
383 mode = S_IRUSR | S_IWUSR | S_IWGRP | S_IWOTH; 306 mode = S_IRUSR | S_IWUSR | S_IWGRP | S_IWOTH;
384 gid = getgid (); 307 gid = 0;
385 } 308 }
386 } 309 }
387} ttyconf; 310} ttyconf;
388 311
389///////////////////////////////////////////////////////////////////////////// 312ptytty_unix::ptytty_unix ()
313{
314 name = 0;
315#if UTMP_SUPPORT
316 cmd_pid = 0;
317#endif
318}
319
320ptytty_unix::~ptytty_unix ()
321{
322#if UTMP_SUPPORT
323 logout ();
324#endif
325 put ();
326}
390 327
391void 328void
392rxvt_ptytty_unix::privileges (rxvt_privaction action) 329ptytty_unix::put ()
393{ 330{
394 if (!name || !*name) 331 chmod (name, RESTORE_TTY_MODE);
395 return; 332 chown (name, 0, ttyconf.gid);
396 333
397 if (action == SAVE) 334 close_tty ();
398 {
399# ifndef RESET_TTY_TO_COMMON_DEFAULTS
400 /* store original tty status for restoration rxvt_clean_exit () -- rgg 04/12/95 */
401 if (lstat (name, &savestat) < 0) /* you lose out */
402 ;
403 else
404# endif
405 {
406 saved = true;
407 chown (name, getuid (), ttyconf.gid); /* fail silently */
408 chmod (name, ttyconf.mode);
409# ifdef HAVE_REVOKE
410 revoke (name);
411# endif
412 }
413 }
414 else
415 { /* action == RESTORE */
416# ifndef RESET_TTY_TO_COMMON_DEFAULTS
417 if (saved)
418 {
419 chmod (name, savestat.st_mode);
420 chown (name, savestat.st_uid, savestat.st_gid);
421 }
422# else
423 chmod (name, (S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH));
424 chown (name, 0, 0);
425# endif
426 }
427}
428#endif
429 335
430rxvt_ptytty_unix::rxvt_ptytty_unix () 336 if (pty >= 0)
431{ 337 close (pty);
338
339 free (name);
340
432 pty = tty = -1; 341 pty = tty = -1;
433 name = 0; 342 name = 0;
434#ifndef NO_SETOWNER_TTYDEV
435 saved = false;
436#endif
437#if UTMP_SUPPORT
438 cmd_pid = 0;
439#endif
440}
441
442rxvt_ptytty_unix::~rxvt_ptytty_unix ()
443{
444#if UTMP_SUPPORT
445 logout ();
446#endif
447 put ();
448}
449
450void
451rxvt_ptytty_unix::put ()
452{
453#ifndef NO_SETOWNER_TTYDEV
454 privileges (RESTORE);
455#endif
456
457 if (pty >= 0) close (pty);
458 close_tty ();
459 free (name);
460
461 pty = tty = -1;
462 name = 0;
463} 343}
464 344
465bool 345bool
466rxvt_ptytty_unix::get () 346ptytty_unix::get ()
467{ 347{
468 /* get master (pty) */ 348 /* get master (pty) */
469 if ((pty = get_pty (&tty, &name)) < 0) 349 if ((pty = get_pty (&tty, &name)) < 0)
470 return false; 350 return false;
471 351
473 353
474 /* get slave (tty) */ 354 /* get slave (tty) */
475 if (tty < 0) 355 if (tty < 0)
476 { 356 {
477#ifndef NO_SETOWNER_TTYDEV 357#ifndef NO_SETOWNER_TTYDEV
478 privileges (SAVE); 358 chown (name, getuid (), ttyconf.gid); /* fail silently */
359 chmod (name, ttyconf.mode);
360# ifdef HAVE_REVOKE
361 revoke (name);
362# endif
479#endif 363#endif
480 364
481 if ((tty = get_tty (name)) < 0) 365 if ((tty = get_tty (name)) < 0)
482 { 366 {
483 put (); 367 put ();
488 return true; 372 return true;
489} 373}
490 374
491#if PTYTTY_HELPER 375#if PTYTTY_HELPER
492 376
493static int sock_fd; 377static int sock_fd = -1;
494static int pid; 378static int helper_pid, owner_pid;
495 379
496struct command 380struct command
497{ 381{
498 enum { get, login, destroy } type; 382 enum { get, login, destroy } type;
499 383
500 rxvt_ptytty *id; 384 ptytty *id;
501 385
502 bool login_shell; 386 bool login_shell;
503 int cmd_pid; 387 int cmd_pid;
504 char hostname[512]; // arbitrary, but should be plenty 388 char hostname[512]; // arbitrary, but should be plenty
505}; 389};
506 390
507struct rxvt_ptytty_proxy : zero_initialized, rxvt_ptytty 391struct ptytty_proxy : ptytty
508{ 392{
509 rxvt_ptytty *id; 393 ptytty *id;
510 394
395 ptytty_proxy ()
396 : id(0)
397 {
398 }
399
511 ~rxvt_ptytty_proxy (); 400 ~ptytty_proxy ();
512 401
513 bool get (); 402 bool get ();
514 void login (int cmd_pid, bool login_shell, const char *hostname); 403 void login (int cmd_pid, bool login_shell, const char *hostname);
515}; 404};
516 405
517bool 406bool
518rxvt_ptytty_proxy::get () 407ptytty_proxy::get ()
519{ 408{
520 command cmd; 409 command cmd;
521 410
522 cmd.type = command::get; 411 cmd.type = command::get;
523 412
524 write (sock_fd, &cmd, sizeof (cmd)); 413 write (sock_fd, &cmd, sizeof (cmd));
525 414
526 if (read (sock_fd, &id, sizeof (id)) != sizeof (id)) 415 if (read (sock_fd, &id, sizeof (id)) != sizeof (id))
527 rxvt_fatal ("protocol error while creating pty using helper process, aborting.\n"); 416 ptytty_fatal ("protocol error while creating pty using helper process, aborting.\n");
528 417
529 if (!id) 418 if (!id)
530 return false; 419 return false;
531 420
532 if ((pty = rxvt_recv_fd (sock_fd)) < 0 421 if ((pty = recv_fd (sock_fd)) < 0
533 || (tty = rxvt_recv_fd (sock_fd)) < 0) 422 || (tty = recv_fd (sock_fd)) < 0)
534 rxvt_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");
535 424
536 return true; 425 return true;
537} 426}
538 427
539void 428void
540rxvt_ptytty_proxy::login (int cmd_pid, bool login_shell, const char *hostname) 429ptytty_proxy::login (int cmd_pid, bool login_shell, const char *hostname)
541{ 430{
542 command cmd; 431 command cmd;
543 432
544 cmd.type = command::login; 433 cmd.type = command::login;
545 cmd.id = id; 434 cmd.id = id;
548 strncpy (cmd.hostname, hostname, sizeof (cmd.hostname)); 437 strncpy (cmd.hostname, hostname, sizeof (cmd.hostname));
549 438
550 write (sock_fd, &cmd, sizeof (cmd)); 439 write (sock_fd, &cmd, sizeof (cmd));
551} 440}
552 441
553rxvt_ptytty_proxy::~rxvt_ptytty_proxy () 442ptytty_proxy::~ptytty_proxy ()
554{ 443{
444 if (id)
445 {
555 command cmd; 446 command cmd;
556 447
557 cmd.type = command::destroy; 448 cmd.type = command::destroy;
558 cmd.id = id; 449 cmd.id = id;
559 450
560 write (sock_fd, &cmd, sizeof (cmd)); 451 write (sock_fd, &cmd, sizeof (cmd));
452 }
561} 453}
562 454
563static 455static
564void serve () 456void serve ()
565{ 457{
566 command cmd; 458 command cmd;
567 vector<rxvt_ptytty *> ptys; 459 vector<ptytty *> ptys;
568 460
569 while (read (sock_fd, &cmd, sizeof (command)) == sizeof (command)) 461 while (read (sock_fd, &cmd, sizeof (command)) == sizeof (command))
570 { 462 {
571 if (cmd.type == command::get) 463 if (cmd.type == command::get)
572 { 464 {
573 // -> id ptyfd ttyfd 465 // -> id ptyfd ttyfd
574 cmd.id = new rxvt_ptytty_unix; 466 cmd.id = new ptytty_unix;
575 467
576 if (cmd.id->get ()) 468 if (cmd.id->get ())
577 { 469 {
578 write (sock_fd, &cmd.id, sizeof (cmd.id)); 470 write (sock_fd, &cmd.id, sizeof (cmd.id));
579 ptys.push_back (cmd.id); 471 ptys.push_back (cmd.id);
580 472
581 rxvt_send_fd (sock_fd, cmd.id->pty); 473 ptytty::send_fd (sock_fd, cmd.id->pty);
582 rxvt_send_fd (sock_fd, cmd.id->tty); 474 ptytty::send_fd (sock_fd, cmd.id->tty);
583 } 475 }
584 else 476 else
585 { 477 {
586 delete cmd.id; 478 delete cmd.id;
587 cmd.id = 0; 479 cmd.id = 0;
598 } 490 }
599#endif 491#endif
600 } 492 }
601 else if (cmd.type == command::destroy) 493 else if (cmd.type == command::destroy)
602 { 494 {
603 rxvt_ptytty **pty = find (ptys.begin (), ptys.end (), cmd.id); 495 vector<ptytty *>::iterator pty = find (ptys.begin (), ptys.end (), cmd.id);
604 496
605 if (pty) 497 if (pty != ptys.end ())
606 { 498 {
607 delete *pty; 499 delete *pty;
608 ptys.erase (pty); 500 ptys.erase (pty);
609 } 501 }
610 } 502 }
611 else 503 else
612 break; 504 break;
613 } 505 }
614 506
615 // destroy all ptys 507 // destroy all ptys
616 for (rxvt_ptytty **i = ptys.end (); i-- > ptys.begin (); ) 508 for (vector<ptytty *>::iterator i = ptys.end (); i-- > ptys.begin (); )
617 delete *i; 509 delete *i;
618} 510}
619 511
620void rxvt_ptytty_server () 512void
513ptytty::use_helper ()
621{ 514{
515 int pid = getpid ();
516
517 if (sock_fd >= 0 && pid == owner_pid)
518 return;
519
520 owner_pid = pid;
521
622 int sv[2]; 522 int sv[2];
623 523
624 if (socketpair (AF_UNIX, SOCK_STREAM, 0, sv)) 524 if (socketpair (AF_UNIX, SOCK_STREAM, 0, sv))
625 rxvt_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");
626 526
627 pid = fork (); 527 helper_pid = fork ();
628 528
629 if (pid < 0) 529 if (helper_pid < 0)
630 rxvt_fatal ("could not create pty/sessiondb helper process, aborting.\n"); 530 ptytty_fatal ("could not create pty/sessiondb helper process, aborting.\n");
631 531
632 if (pid) 532 if (helper_pid)
633 { 533 {
634 // client, urxvt 534 // client, process
635 sock_fd = sv[0]; 535 sock_fd = sv[0];
636 close (sv[1]); 536 close (sv[1]);
637 fcntl (sock_fd, F_SETFD, FD_CLOEXEC); 537 fcntl (sock_fd, F_SETFD, FD_CLOEXEC);
638 } 538 }
639 else 539 else
640 { 540 {
641 setgid (getegid ());
642 setuid (geteuid ());
643
644 // server, pty-helper 541 // server, pty-helper
645 sock_fd = sv[1]; 542 sock_fd = sv[1];
646 543
544 chdir ("/");
545
647 for (int fd = 0; fd < 1023; fd++) 546 for (int fd = 0; fd < 1023; fd++)
648 if (fd != sock_fd && fd != 1) 547 if (fd != sock_fd)
649 close (fd); 548 close (fd);
650 549
651 serve (); 550 serve ();
652 _exit (EXIT_SUCCESS); 551 _exit (EXIT_SUCCESS);
653 } 552 }
654} 553}
655#endif
656 554
657// a "factory" *g* 555#endif
556
658rxvt_ptytty * 557ptytty *
659rxvt_new_ptytty () 558ptytty::create ()
660{ 559{
661#if PTYTTY_HELPER 560#if PTYTTY_HELPER
662 if (pid > 0) 561 if (helper_pid && getpid () == owner_pid)
663 // use helper process 562 // use helper process
664 return new rxvt_ptytty_proxy; 563 return new ptytty_proxy;
665 else 564 else
666#endif 565#endif
667 return new rxvt_ptytty_unix; 566 return new ptytty_unix;
668} 567}
669 568
670/*----------------------- 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
671 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