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.48 by ayin, Thu Jan 19 10:44:44 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.
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 ()
384 gid = 0; 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);
332 chown (name, 0, ttyconf.gid);
333
334 close_tty ();
335
336 if (pty >= 0)
337 close (pty);
338
339 free (name);
340
341 pty = tty = -1;
342 name = 0;
343}
344
345bool
346ptytty_unix::get ()
347{
348 /* get master (pty) */
349 if ((pty = get_pty (&tty, &name)) < 0)
395 return; 350 return false;
396 351
397 if (action == SAVE) 352 fcntl (pty, F_SETFL, O_NONBLOCK);
353
354 /* get slave (tty) */
355 if (tty < 0)
398 { 356 {
357#ifndef NO_SETOWNER_TTYDEV
399 chown (name, getuid (), ttyconf.gid); /* fail silently */ 358 chown (name, getuid (), ttyconf.gid); /* fail silently */
400 chmod (name, ttyconf.mode); 359 chmod (name, ttyconf.mode);
401# ifdef HAVE_REVOKE 360# ifdef HAVE_REVOKE
402 revoke (name); 361 revoke (name);
403# endif 362# endif
404 }
405 else
406 { /* action == RESTORE */
407 chmod (name, RESTORE_TTY_MODE);
408 chown (name, 0, ttyconf.gid);
409 }
410}
411#endif
412
413rxvt_ptytty_unix::rxvt_ptytty_unix ()
414{
415 pty = tty = -1;
416 name = 0;
417#if UTMP_SUPPORT
418 cmd_pid = 0;
419#endif
420}
421
422rxvt_ptytty_unix::~rxvt_ptytty_unix ()
423{
424#if UTMP_SUPPORT
425 logout ();
426#endif
427 put ();
428}
429
430void
431rxvt_ptytty_unix::put ()
432{
433#ifndef NO_SETOWNER_TTYDEV
434 privileges (RESTORE);
435#endif
436
437 if (pty >= 0) close (pty);
438 close_tty ();
439 free (name);
440
441 pty = tty = -1;
442 name = 0;
443}
444
445bool
446rxvt_ptytty_unix::get ()
447{
448 /* get master (pty) */
449 if ((pty = get_pty (&tty, &name)) < 0)
450 return false;
451
452 fcntl (pty, F_SETFL, O_NONBLOCK);
453
454 /* get slave (tty) */
455 if (tty < 0)
456 {
457#ifndef NO_SETOWNER_TTYDEV
458 privileges (SAVE);
459#endif 363#endif
460 364
461 if ((tty = get_tty (name)) < 0) 365 if ((tty = get_tty (name)) < 0)
462 { 366 {
463 put (); 367 put ();
468 return true; 372 return true;
469} 373}
470 374
471#if PTYTTY_HELPER 375#if PTYTTY_HELPER
472 376
473static int sock_fd; 377static int sock_fd = -1;
474static int pid; 378static int helper_pid, owner_pid;
475 379
476struct command 380struct command
477{ 381{
478 enum { get, login, destroy } type; 382 enum { get, login, destroy } type;
479 383
480 rxvt_ptytty *id; 384 ptytty *id;
481 385
482 bool login_shell; 386 bool login_shell;
483 int cmd_pid; 387 int cmd_pid;
484 char hostname[512]; // arbitrary, but should be plenty 388 char hostname[512]; // arbitrary, but should be plenty
485}; 389};
486 390
487struct rxvt_ptytty_proxy : zero_initialized, rxvt_ptytty 391struct ptytty_proxy : ptytty
488{ 392{
489 rxvt_ptytty *id; 393 ptytty *id;
490 394
395 ptytty_proxy ()
396 : id(0)
397 {
398 }
399
491 ~rxvt_ptytty_proxy (); 400 ~ptytty_proxy ();
492 401
493 bool get (); 402 bool get ();
494 void login (int cmd_pid, bool login_shell, const char *hostname); 403 void login (int cmd_pid, bool login_shell, const char *hostname);
495}; 404};
496 405
497bool 406bool
498rxvt_ptytty_proxy::get () 407ptytty_proxy::get ()
499{ 408{
500 command cmd; 409 command cmd;
501 410
502 cmd.type = command::get; 411 cmd.type = command::get;
503 412
504 write (sock_fd, &cmd, sizeof (cmd)); 413 write (sock_fd, &cmd, sizeof (cmd));
505 414
506 if (read (sock_fd, &id, sizeof (id)) != sizeof (id)) 415 if (read (sock_fd, &id, sizeof (id)) != sizeof (id))
507 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");
508 417
509 if (!id) 418 if (!id)
510 return false; 419 return false;
511 420
512 if ((pty = rxvt_recv_fd (sock_fd)) < 0 421 if ((pty = recv_fd (sock_fd)) < 0
513 || (tty = rxvt_recv_fd (sock_fd)) < 0) 422 || (tty = recv_fd (sock_fd)) < 0)
514 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");
515 424
516 return true; 425 return true;
517} 426}
518 427
519void 428void
520rxvt_ptytty_proxy::login (int cmd_pid, bool login_shell, const char *hostname) 429ptytty_proxy::login (int cmd_pid, bool login_shell, const char *hostname)
521{ 430{
522 command cmd; 431 command cmd;
523 432
524 cmd.type = command::login; 433 cmd.type = command::login;
525 cmd.id = id; 434 cmd.id = id;
528 strncpy (cmd.hostname, hostname, sizeof (cmd.hostname)); 437 strncpy (cmd.hostname, hostname, sizeof (cmd.hostname));
529 438
530 write (sock_fd, &cmd, sizeof (cmd)); 439 write (sock_fd, &cmd, sizeof (cmd));
531} 440}
532 441
533rxvt_ptytty_proxy::~rxvt_ptytty_proxy () 442ptytty_proxy::~ptytty_proxy ()
534{ 443{
444 if (id)
445 {
535 command cmd; 446 command cmd;
536 447
537 cmd.type = command::destroy; 448 cmd.type = command::destroy;
538 cmd.id = id; 449 cmd.id = id;
539 450
540 write (sock_fd, &cmd, sizeof (cmd)); 451 write (sock_fd, &cmd, sizeof (cmd));
452 }
541} 453}
542 454
543static 455static
544void serve () 456void serve ()
545{ 457{
546 command cmd; 458 command cmd;
547 vector<rxvt_ptytty *> ptys; 459 vector<ptytty *> ptys;
548 460
549 while (read (sock_fd, &cmd, sizeof (command)) == sizeof (command)) 461 while (read (sock_fd, &cmd, sizeof (command)) == sizeof (command))
550 { 462 {
551 if (cmd.type == command::get) 463 if (cmd.type == command::get)
552 { 464 {
553 // -> id ptyfd ttyfd 465 // -> id ptyfd ttyfd
554 cmd.id = new rxvt_ptytty_unix; 466 cmd.id = new ptytty_unix;
555 467
556 if (cmd.id->get ()) 468 if (cmd.id->get ())
557 { 469 {
558 write (sock_fd, &cmd.id, sizeof (cmd.id)); 470 write (sock_fd, &cmd.id, sizeof (cmd.id));
559 ptys.push_back (cmd.id); 471 ptys.push_back (cmd.id);
560 472
561 rxvt_send_fd (sock_fd, cmd.id->pty); 473 ptytty::send_fd (sock_fd, cmd.id->pty);
562 rxvt_send_fd (sock_fd, cmd.id->tty); 474 ptytty::send_fd (sock_fd, cmd.id->tty);
563 } 475 }
564 else 476 else
565 { 477 {
566 delete cmd.id; 478 delete cmd.id;
567 cmd.id = 0; 479 cmd.id = 0;
578 } 490 }
579#endif 491#endif
580 } 492 }
581 else if (cmd.type == command::destroy) 493 else if (cmd.type == command::destroy)
582 { 494 {
583 rxvt_ptytty **pty = find (ptys.begin (), ptys.end (), cmd.id); 495 vector<ptytty *>::iterator pty = find (ptys.begin (), ptys.end (), cmd.id);
584 496
585 if (pty) 497 if (pty != ptys.end ())
586 { 498 {
587 delete *pty; 499 delete *pty;
588 ptys.erase (pty); 500 ptys.erase (pty);
589 } 501 }
590 } 502 }
591 else 503 else
592 break; 504 break;
593 } 505 }
594 506
595 // destroy all ptys 507 // destroy all ptys
596 for (rxvt_ptytty **i = ptys.end (); i-- > ptys.begin (); ) 508 for (vector<ptytty *>::iterator i = ptys.end (); i-- > ptys.begin (); )
597 delete *i; 509 delete *i;
598} 510}
599 511
600void rxvt_ptytty_server () 512void
513ptytty::use_helper ()
601{ 514{
515 int pid = getpid ();
516
517 if (sock_fd >= 0 && pid == owner_pid)
518 return;
519
520 owner_pid = pid;
521
602 int sv[2]; 522 int sv[2];
603 523
604 if (socketpair (AF_UNIX, SOCK_STREAM, 0, sv)) 524 if (socketpair (AF_UNIX, SOCK_STREAM, 0, sv))
605 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");
606 526
607 pid = fork (); 527 helper_pid = fork ();
608 528
609 if (pid < 0) 529 if (helper_pid < 0)
610 rxvt_fatal ("could not create pty/sessiondb helper process, aborting.\n"); 530 ptytty_fatal ("could not create pty/sessiondb helper process, aborting.\n");
611 531
612 if (pid) 532 if (helper_pid)
613 { 533 {
614 // client, urxvt 534 // client, process
615 sock_fd = sv[0]; 535 sock_fd = sv[0];
616 close (sv[1]); 536 close (sv[1]);
617 fcntl (sock_fd, F_SETFD, FD_CLOEXEC); 537 fcntl (sock_fd, F_SETFD, FD_CLOEXEC);
618 } 538 }
619 else 539 else
620 { 540 {
621 // server, pty-helper 541 // server, pty-helper
622 sock_fd = sv[1]; 542 sock_fd = sv[1];
623 543
544 chdir ("/");
545
624 for (int fd = 0; fd < 1023; fd++) 546 for (int fd = 0; fd < 1023; fd++)
625 if (fd != sock_fd) 547 if (fd != sock_fd)
626 close (fd); 548 close (fd);
627 549
628 serve (); 550 serve ();
629 _exit (EXIT_SUCCESS); 551 _exit (EXIT_SUCCESS);
630 } 552 }
631} 553}
632#endif
633 554
634// a "factory" *g* 555#endif
556
635rxvt_ptytty * 557ptytty *
636rxvt_new_ptytty () 558ptytty::create ()
637{ 559{
638#if PTYTTY_HELPER 560#if PTYTTY_HELPER
639 if (pid > 0) 561 if (helper_pid && getpid () == owner_pid)
640 // use helper process 562 // use helper process
641 return new rxvt_ptytty_proxy; 563 return new ptytty_proxy;
642 else 564 else
643#endif 565#endif
644 return new rxvt_ptytty_unix; 566 return new ptytty_unix;
645} 567}
646 568
647/*----------------------- 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
648 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