… | |
… | |
33 | #include <fcntl.h> |
33 | #include <fcntl.h> |
34 | |
34 | |
35 | #ifdef HAVE_SYS_IOCTL_H |
35 | #ifdef HAVE_SYS_IOCTL_H |
36 | # include <sys/ioctl.h> |
36 | # include <sys/ioctl.h> |
37 | #endif |
37 | #endif |
38 | #if defined(PTYS_ARE_PTMX) && defined(HAVE_SYS_STROPTS_H) |
38 | #if defined(HAVE_DEV_PTMX) && defined(HAVE_SYS_STROPTS_H) |
39 | # include <sys/stropts.h> /* for I_PUSH */ |
39 | # include <sys/stropts.h> /* for I_PUSH */ |
40 | #endif |
40 | #endif |
41 | #ifdef HAVE_ISASTREAM |
41 | #ifdef HAVE_ISASTREAM |
42 | # include <stropts.h> |
42 | # include <stropts.h> |
43 | #endif |
43 | #endif |
… | |
… | |
46 | #elif defined(HAVE_LIBUTIL_H) |
46 | #elif defined(HAVE_LIBUTIL_H) |
47 | # include <libutil.h> |
47 | # include <libutil.h> |
48 | #elif defined(HAVE_UTIL_H) |
48 | #elif defined(HAVE_UTIL_H) |
49 | # include <util.h> |
49 | # include <util.h> |
50 | #endif |
50 | #endif |
|
|
51 | #ifdef TTY_GID_SUPPORT |
|
|
52 | #include <grp.h> |
|
|
53 | #endif |
51 | |
54 | |
52 | #include <cstdio> |
55 | #include <cstdio> |
53 | #include <grp.h> |
|
|
54 | |
56 | |
55 | #include "rxvtutil.h" |
57 | #include "rxvtutil.h" |
56 | #include "fdpass.h" |
58 | #include "fdpass.h" |
57 | #include "ptytty.h" |
59 | #include "ptytty.h" |
58 | |
60 | |
… | |
… | |
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 | */ |
69 | static inline int |
71 | #if defined(UNIX98_PTY) |
|
|
72 | static int |
70 | get_pty_streams (int *fd_tty, char **ttydev) |
73 | get_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) |
98 | static inline int |
99 | static int |
99 | get_pty_openpty (int *fd_tty, char **ttydev) |
100 | get_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) |
117 | static inline int |
116 | static int |
118 | get_pty__getpty (int *fd_tty, char **ttydev) |
117 | get_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) |
131 | static inline int |
128 | static int |
132 | get_pty_ptc (int *fd_tty, char **ttydev) |
129 | get_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) |
147 | static inline int |
142 | static int |
148 | get_pty_clone (int *fd_tty, char **ttydev) |
143 | get_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 |
163 | static inline int |
156 | /* Based on the code in openssh/openbsd-compat/bsd-openpty.c */ |
|
|
157 | static int |
164 | get_pty_numeric (int *fd_tty, char **ttydev) |
158 | get_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 | |
|
|
203 | static inline int |
|
|
204 | get_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 | |
|
|
245 | static int |
|
|
246 | get_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 | */ |
… | |
… | |
276 | static int |
202 | static int |
277 | control_tty (int fd_tty) |
203 | control_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. |
… | |
… | |
358 | } |
284 | } |
359 | } |
285 | } |
360 | #endif |
286 | #endif |
361 | } |
287 | } |
362 | |
288 | |
363 | ///////////////////////////////////////////////////////////////////////////// |
|
|
364 | |
|
|
365 | #ifndef NO_SETOWNER_TTYDEV |
|
|
366 | static struct ttyconf { |
289 | static 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 | ///////////////////////////////////////////////////////////////////////////// |
312 | rxvt_ptytty_unix::rxvt_ptytty_unix () |
|
|
313 | { |
|
|
314 | name = 0; |
|
|
315 | #if UTMP_SUPPORT |
|
|
316 | cmd_pid = 0; |
|
|
317 | #endif |
|
|
318 | } |
|
|
319 | |
|
|
320 | rxvt_ptytty_unix::~rxvt_ptytty_unix () |
|
|
321 | { |
|
|
322 | #if UTMP_SUPPORT |
|
|
323 | logout (); |
|
|
324 | #endif |
|
|
325 | put (); |
|
|
326 | } |
390 | |
327 | |
391 | void |
328 | void |
392 | rxvt_ptytty_unix::privileges (rxvt_privaction action) |
329 | rxvt_ptytty_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 | |
|
|
345 | bool |
|
|
346 | rxvt_ptytty_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 | |
|
|
413 | rxvt_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 | |
|
|
422 | rxvt_ptytty_unix::~rxvt_ptytty_unix () |
|
|
423 | { |
|
|
424 | #if UTMP_SUPPORT |
|
|
425 | logout (); |
|
|
426 | #endif |
|
|
427 | put (); |
|
|
428 | } |
|
|
429 | |
|
|
430 | void |
|
|
431 | rxvt_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 | |
|
|
445 | bool |
|
|
446 | rxvt_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 (); |
… | |
… | |
627 | |
531 | |
628 | serve (); |
532 | serve (); |
629 | _exit (EXIT_SUCCESS); |
533 | _exit (EXIT_SUCCESS); |
630 | } |
534 | } |
631 | } |
535 | } |
|
|
536 | |
632 | #endif |
537 | #endif |
633 | |
538 | |
634 | // a "factory" *g* |
539 | // a "factory" *g* |
635 | rxvt_ptytty * |
540 | rxvt_ptytty * |
636 | rxvt_new_ptytty () |
541 | rxvt_new_ptytty () |