146
|
1 /*****************************************************************************/
|
|
2 /* pty.c - general purpose routines */
|
|
3 /* Copyright (C) 1998-2003 Brian Masney <masneyb@gftp.org> */
|
|
4 /* */
|
|
5 /* This program is free software; you can redistribute it and/or modify */
|
|
6 /* it under the terms of the GNU General Public License as published by */
|
|
7 /* the Free Software Foundation; either version 2 of the License, or */
|
|
8 /* (at your option) any later version. */
|
|
9 /* */
|
|
10 /* This program is distributed in the hope that it will be useful, */
|
|
11 /* but WITHOUT ANY WARRANTY; without even the implied warranty of */
|
|
12 /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
|
|
13 /* GNU General Public License for more details. */
|
|
14 /* */
|
|
15 /* You should have received a copy of the GNU General Public License */
|
|
16 /* along with this program; if not, write to the Free Software */
|
|
17 /* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111 USA */
|
|
18 /*****************************************************************************/
|
|
19
|
|
20 static const char cvsid[] = "$Id$";
|
|
21
|
|
22 #include "gftp.h"
|
|
23
|
179
|
24 #ifdef __sgi
|
146
|
25
|
179
|
26 char *
|
210
|
27 gftp_get_pty_impl (void)
|
179
|
28 {
|
|
29 return ("sgi");
|
|
30 }
|
|
31
|
146
|
32
|
210
|
33 static int
|
|
34 _gftp_ptym_open (char *pts_name, size_t len, int *fds)
|
146
|
35 {
|
210
|
36 char *new_pts_name;
|
|
37 int fdm;
|
146
|
38
|
210
|
39 if ((new_pts_name = _getpty (&fdm, O_RDWR, 0600, 0)) == NULL)
|
146
|
40 return (GFTP_ERETRYABLE);
|
|
41
|
210
|
42 strncpy (pts_name, new_pts_name, len);
|
249
|
43 pts_name[len - 1] = '\0';
|
210
|
44
|
|
45 return (fdm);
|
|
46 }
|
|
47
|
|
48
|
|
49 static int
|
|
50 _gftp_ptys_open (int fdm, int fds, char *pts_name)
|
|
51 {
|
|
52 int new_fds;
|
|
53
|
|
54 if ((new_fds = open (pts_name, O_RDWR)) < 0)
|
146
|
55 {
|
210
|
56 close (fdm);
|
|
57 return (-1);
|
146
|
58 }
|
|
59
|
210
|
60 return (new_fds);
|
146
|
61 }
|
|
62
|
232
|
63 #elif HAVE_OPENPTY
|
|
64
|
235
|
65 #ifdef HAVE_PTY_H
|
|
66 #include <pty.h>
|
|
67 #include <utmp.h> /* for login_tty */
|
|
68 #elif HAVE_LIBUTIL_H
|
|
69 #include <libutil.h>
|
|
70 #include <utmp.h> /* for login_tty */
|
|
71 #else
|
|
72 extern int openpty(int *amaster, int *aslave, char *name, struct termios *termp, struct winsize * winp);
|
|
73 extern int login_tty(int fd);
|
|
74 #endif
|
|
75
|
232
|
76 char *
|
|
77 gftp_get_pty_impl (void)
|
|
78 {
|
|
79 return ("openpty");
|
|
80 }
|
|
81
|
|
82
|
|
83 static int
|
|
84 _gftp_ptym_open (char *pts_name, size_t len, int *fds)
|
|
85 {
|
|
86 int fdm;
|
|
87
|
|
88 if (openpty (&fdm, fds, pts_name, NULL, NULL) < 0)
|
|
89 return (GFTP_ERETRYABLE);
|
|
90
|
|
91 ioctl (*fds, TIOCSCTTY, NULL);
|
|
92
|
|
93 return (fdm);
|
|
94 }
|
|
95
|
|
96
|
|
97 static int
|
|
98 _gftp_ptys_open (int fdm, int fds, char *pts_name)
|
|
99 {
|
|
100 if (login_tty (fds) < 0)
|
|
101 return (GFTP_EFATAL);
|
|
102
|
|
103 return (fds);
|
|
104 }
|
|
105
|
178
|
106 #elif HAVE_GRANTPT
|
146
|
107
|
235
|
108 #include <stropts.h>
|
|
109
|
179
|
110 char *
|
210
|
111 gftp_get_pty_impl (void)
|
179
|
112 {
|
|
113 return ("unix98");
|
|
114 }
|
|
115
|
|
116
|
210
|
117 static int
|
|
118 _gftp_ptym_open (char *pts_name, size_t len, int *fds)
|
146
|
119 {
|
210
|
120 char *new_pts_name;
|
|
121 int fdm;
|
146
|
122
|
210
|
123 if ((fdm = open ("/dev/ptmx", O_RDWR)) < 0)
|
146
|
124 return (GFTP_ERETRYABLE);
|
|
125
|
210
|
126 if (grantpt (fdm) < 0)
|
146
|
127 {
|
210
|
128 close (fdm);
|
|
129 return (GFTP_ERETRYABLE);
|
|
130 }
|
|
131
|
|
132 if (unlockpt (fdm) < 0)
|
|
133 {
|
|
134 close (fdm);
|
146
|
135 return (GFTP_ERETRYABLE);
|
|
136 }
|
|
137
|
210
|
138 if ((new_pts_name = ptsname (fdm)) == NULL)
|
146
|
139 {
|
210
|
140 close (fdm);
|
146
|
141 return (GFTP_ERETRYABLE);
|
|
142 }
|
|
143
|
210
|
144 strncpy (pts_name, new_pts_name, len);
|
249
|
145 pts_name[len - 1] = '\0';
|
210
|
146
|
|
147 return (fdm);
|
|
148 }
|
|
149
|
|
150
|
|
151 static int
|
|
152 _gftp_ptys_open (int fdm, int fds, char *pts_name)
|
|
153 {
|
|
154 int new_fds;
|
|
155
|
|
156 if ((new_fds = open (pts_name, O_RDWR)) < 0)
|
146
|
157 {
|
210
|
158 close (fdm);
|
|
159 return (-1);
|
146
|
160 }
|
|
161
|
179
|
162 #ifdef SYSV
|
146
|
163 /* I intentionally ignore these errors */
|
210
|
164 ioctl (new_fds, I_PUSH, "ptem");
|
|
165 ioctl (new_fds, I_PUSH, "ldterm");
|
|
166 ioctl (new_fds, I_PUSH, "ttcompat");
|
179
|
167 #endif
|
146
|
168
|
210
|
169 return (new_fds);
|
146
|
170 }
|
|
171
|
232
|
172 #else /* !HAVE_GRANTPT */
|
146
|
173
|
|
174 /* Fall back to *BSD... */
|
|
175
|
179
|
176 char *
|
210
|
177 gftp_get_pty_impl (void)
|
179
|
178 {
|
|
179 return ("bsd");
|
|
180 }
|
|
181
|
|
182
|
210
|
183 static int
|
|
184 _gftp_ptym_open (char *pts_name, size_t len, int *fds)
|
146
|
185 {
|
210
|
186 char *pos1, *pos2;
|
|
187 int fdm;
|
146
|
188
|
210
|
189 g_return_val_if_fail (len >= 10, GFTP_EFATAL);
|
|
190
|
|
191 strncpy (pts_name, "/dev/ptyXY", len);
|
249
|
192 pts_name[len - 1] = '\0';
|
|
193
|
146
|
194 for (pos1 = "pqrstuvwxyzPQRST"; *pos1 != '\0'; pos1++)
|
|
195 {
|
|
196 pts_name[8] = *pos1;
|
|
197 for (pos2 = "0123456789abcdef"; *pos2 != '\0'; pos2++)
|
|
198 {
|
|
199 pts_name[9] = *pos2;
|
210
|
200 if ((fdm = open (pts_name, O_RDWR)) < 0)
|
146
|
201 continue;
|
|
202
|
|
203 pts_name[5] = 't';
|
|
204 chmod (pts_name, S_IRUSR | S_IWUSR);
|
|
205 chown (pts_name, getuid (), -1);
|
|
206
|
210
|
207 return (fdm);
|
146
|
208 }
|
|
209 }
|
|
210
|
|
211 return (GFTP_ERETRYABLE);
|
|
212 }
|
|
213
|
210
|
214
|
|
215 static int
|
|
216 _gftp_ptys_open (int fdm, int fds, char *pts_name)
|
|
217 {
|
|
218 int new_fds;
|
|
219
|
|
220 if ((new_fds = open (pts_name, O_RDWR)) < 0)
|
|
221 {
|
|
222 close (fdm);
|
|
223 return (-1);
|
|
224 }
|
|
225
|
|
226 #if defined(TIOCSCTTY) && !defined(CIBAUD)
|
|
227 ioctl (new_fds, TIOCSCTTY, NULL);
|
|
228 #endif
|
|
229
|
|
230 return (new_fds);
|
|
231 }
|
|
232
|
146
|
233 #endif /* __sgi */
|
|
234
|
|
235
|
210
|
236 static int
|
|
237 _gftp_tty_raw (int fd)
|
146
|
238 {
|
|
239 struct termios buf;
|
|
240
|
|
241 if (tcgetattr (fd, &buf) < 0)
|
|
242 return (-1);
|
|
243
|
|
244 buf.c_iflag |= IGNPAR;
|
|
245 buf.c_iflag &= ~(ICRNL | ISTRIP | IXON | IGNCR | IXANY | IXOFF | INLCR);
|
|
246 buf.c_lflag &= ~(ECHO | ICANON | ISIG | ECHOE | ECHOK | ECHONL);
|
|
247 #ifdef IEXTEN
|
|
248 buf.c_lflag &= ~(IEXTEN);
|
|
249 #endif
|
|
250
|
|
251 buf.c_oflag &= ~(OPOST);
|
|
252 buf.c_cc[VMIN] = 1;
|
|
253 buf.c_cc[VTIME] = 0;
|
|
254
|
|
255 if (tcsetattr (fd, TCSADRAIN, &buf) < 0)
|
|
256 return (-1);
|
|
257 return (0);
|
|
258 }
|
|
259
|
|
260
|
210
|
261 static void
|
|
262 _gftp_close_all_fds (void)
|
|
263 {
|
|
264 int i, maxfds;
|
|
265
|
|
266 #ifdef HAVE_GETDTABLESIZE
|
|
267 maxfds = getdtablesize () - 1;
|
|
268 #elif defined (OPEN_MAX)
|
|
269 maxfds = OPEN_MAX;
|
|
270 #else
|
|
271 maxfds = -1;
|
|
272 #endif
|
|
273
|
|
274 for (i=3; i<maxfds; i++)
|
|
275 close (i);
|
|
276 }
|
|
277
|
|
278
|
|
279 pid_t
|
|
280 gftp_exec_without_new_pty (gftp_request * request, int *fdm, char **args)
|
|
281 {
|
|
282 pid_t child;
|
|
283 int s[2];
|
|
284
|
|
285 if (socketpair (AF_LOCAL, SOCK_STREAM, 0, s) < 0)
|
|
286 {
|
|
287 request->logging_function (gftp_logging_error, request,
|
|
288 _("Cannot create a socket pair: %s\n"),
|
|
289 g_strerror (errno));
|
|
290 return (-1);
|
|
291 }
|
|
292
|
|
293 if ((child = fork ()) == 0)
|
|
294 {
|
|
295 setsid ();
|
|
296
|
|
297 close (s[0]);
|
|
298
|
|
299 _gftp_tty_raw (s[1]);
|
|
300 dup2 (s[1], 0);
|
|
301 dup2 (s[1], 1);
|
|
302 dup2 (s[1], 2);
|
|
303 _gftp_close_all_fds ();
|
|
304
|
|
305 execvp (args[0], args);
|
|
306
|
|
307 printf (_("Error: Cannot execute ssh: %s\n"), g_strerror (errno));
|
|
308 exit (1);
|
|
309 }
|
|
310 else if (child > 0)
|
|
311 {
|
|
312 close (s[1]);
|
|
313 _gftp_tty_raw (s[0]);
|
|
314 *fdm = s[0];
|
|
315 return (child);
|
|
316 }
|
|
317 else
|
|
318 {
|
|
319 request->logging_function (gftp_logging_error, request->user_data,
|
|
320 _("Cannot fork another process: %s\n"),
|
|
321 g_strerror (errno));
|
|
322 return (-1);
|
|
323 }
|
|
324 }
|
|
325
|
|
326
|
|
327 pid_t
|
|
328 gftp_exec_with_new_pty (gftp_request * request, int *fdm, char **args)
|
|
329 {
|
|
330 char pts_name[64];
|
|
331 pid_t child;
|
|
332 int fds;
|
|
333
|
|
334 *pts_name = '\0';
|
|
335 if ((*fdm = _gftp_ptym_open (pts_name, sizeof (pts_name), &fds)) < 0)
|
|
336 {
|
|
337 request->logging_function (gftp_logging_error, request->user_data,
|
|
338 _("Cannot open master pty %s: %s\n"), pts_name,
|
|
339 g_strerror (errno));
|
|
340 return (-1);
|
|
341 }
|
|
342
|
|
343 if ((child = fork ()) == 0)
|
|
344 {
|
|
345 setsid ();
|
|
346
|
|
347 if ((fds = _gftp_ptys_open (*fdm, fds, pts_name)) < 0)
|
|
348 {
|
|
349 printf ("Cannot open slave pts %s: %s\n", pts_name,
|
|
350 g_strerror (errno));
|
|
351 return (-1);
|
|
352 }
|
|
353
|
|
354 close (*fdm);
|
|
355
|
|
356 _gftp_tty_raw (fds);
|
|
357 dup2 (fds, 0);
|
|
358 dup2 (fds, 1);
|
|
359 dup2 (fds, 2);
|
|
360 _gftp_close_all_fds ();
|
|
361
|
|
362 execvp (args[0], args);
|
|
363
|
|
364 printf (_("Error: Cannot execute ssh: %s\n"), g_strerror (errno));
|
|
365 exit (1);
|
|
366 }
|
|
367 else if (child > 0)
|
|
368 {
|
|
369 _gftp_tty_raw (*fdm);
|
|
370 return (child);
|
|
371 }
|
|
372 else
|
|
373 {
|
|
374 request->logging_function (gftp_logging_error, request->user_data,
|
|
375 _("Cannot fork another process: %s\n"),
|
|
376 g_strerror (errno));
|
|
377 return (-1);
|
|
378 }
|
|
379 }
|
|
380
|