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
|
178
|
63 #elif HAVE_GRANTPT
|
146
|
64
|
235
|
65 #include <stropts.h>
|
|
66
|
179
|
67 char *
|
210
|
68 gftp_get_pty_impl (void)
|
179
|
69 {
|
|
70 return ("unix98");
|
|
71 }
|
|
72
|
|
73
|
210
|
74 static int
|
|
75 _gftp_ptym_open (char *pts_name, size_t len, int *fds)
|
146
|
76 {
|
210
|
77 char *new_pts_name;
|
|
78 int fdm;
|
146
|
79
|
210
|
80 if ((fdm = open ("/dev/ptmx", O_RDWR)) < 0)
|
146
|
81 return (GFTP_ERETRYABLE);
|
|
82
|
210
|
83 if (grantpt (fdm) < 0)
|
146
|
84 {
|
210
|
85 close (fdm);
|
|
86 return (GFTP_ERETRYABLE);
|
|
87 }
|
|
88
|
|
89 if (unlockpt (fdm) < 0)
|
|
90 {
|
|
91 close (fdm);
|
146
|
92 return (GFTP_ERETRYABLE);
|
|
93 }
|
|
94
|
210
|
95 if ((new_pts_name = ptsname (fdm)) == NULL)
|
146
|
96 {
|
210
|
97 close (fdm);
|
146
|
98 return (GFTP_ERETRYABLE);
|
|
99 }
|
|
100
|
210
|
101 strncpy (pts_name, new_pts_name, len);
|
249
|
102 pts_name[len - 1] = '\0';
|
210
|
103
|
|
104 return (fdm);
|
|
105 }
|
|
106
|
|
107
|
|
108 static int
|
|
109 _gftp_ptys_open (int fdm, int fds, char *pts_name)
|
|
110 {
|
|
111 int new_fds;
|
|
112
|
|
113 if ((new_fds = open (pts_name, O_RDWR)) < 0)
|
146
|
114 {
|
210
|
115 close (fdm);
|
|
116 return (-1);
|
146
|
117 }
|
|
118
|
179
|
119 #ifdef SYSV
|
146
|
120 /* I intentionally ignore these errors */
|
210
|
121 ioctl (new_fds, I_PUSH, "ptem");
|
|
122 ioctl (new_fds, I_PUSH, "ldterm");
|
|
123 ioctl (new_fds, I_PUSH, "ttcompat");
|
179
|
124 #endif
|
146
|
125
|
210
|
126 return (new_fds);
|
146
|
127 }
|
|
128
|
515
|
129 #elif HAVE_OPENPTY
|
|
130
|
|
131 #ifdef HAVE_PTY_H
|
|
132 #include <pty.h>
|
|
133 #include <utmp.h> /* for login_tty */
|
|
134 #elif HAVE_LIBUTIL_H
|
|
135 #include <libutil.h>
|
|
136 #include <utmp.h> /* for login_tty */
|
|
137 #else
|
|
138 extern int openpty(int *amaster, int *aslave, char *name, struct termios *termp, struct winsize * winp);
|
|
139 extern int login_tty(int fd);
|
|
140 #endif
|
|
141
|
|
142 char *
|
|
143 gftp_get_pty_impl (void)
|
|
144 {
|
|
145 return ("openpty");
|
|
146 }
|
|
147
|
|
148
|
|
149 static int
|
|
150 _gftp_ptym_open (char *pts_name, size_t len, int *fds)
|
|
151 {
|
|
152 int fdm;
|
|
153
|
|
154 if (openpty (&fdm, fds, pts_name, NULL, NULL) < 0)
|
|
155 return (GFTP_ERETRYABLE);
|
|
156
|
|
157 ioctl (*fds, TIOCSCTTY, NULL);
|
|
158
|
|
159 return (fdm);
|
|
160 }
|
|
161
|
|
162
|
|
163 static int
|
|
164 _gftp_ptys_open (int fdm, int fds, char *pts_name)
|
|
165 {
|
|
166 if (login_tty (fds) < 0)
|
|
167 return (GFTP_EFATAL);
|
|
168
|
|
169 return (fds);
|
|
170 }
|
|
171
|
|
172 #else
|
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
|
458
|
262 _gftp_close_all_fds (int ptysfd)
|
210
|
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++)
|
458
|
275 {
|
|
276 if (i == ptysfd)
|
|
277 continue;
|
|
278
|
|
279 close (i);
|
|
280 }
|
210
|
281 }
|
|
282
|
|
283
|
|
284 pid_t
|
458
|
285 gftp_exec (gftp_request * request, int *fdm, int *ptymfd, char **args)
|
210
|
286 {
|
458
|
287 char pts_name[64];
|
210
|
288 pid_t child;
|
458
|
289 int ptysfd;
|
210
|
290 int s[2];
|
|
291
|
458
|
292 *pts_name = '\0';
|
|
293 if ((*ptymfd = _gftp_ptym_open (pts_name, sizeof (pts_name), &ptysfd)) < 0)
|
|
294 {
|
|
295 request->logging_function (gftp_logging_error, request->user_data,
|
|
296 _("Cannot open master pty %s: %s\n"), pts_name,
|
|
297 g_strerror (errno));
|
|
298 return (-1);
|
|
299 }
|
|
300
|
210
|
301 if (socketpair (AF_LOCAL, SOCK_STREAM, 0, s) < 0)
|
|
302 {
|
|
303 request->logging_function (gftp_logging_error, request,
|
|
304 _("Cannot create a socket pair: %s\n"),
|
|
305 g_strerror (errno));
|
|
306 return (-1);
|
|
307 }
|
|
308
|
|
309 if ((child = fork ()) == 0)
|
|
310 {
|
|
311 setsid ();
|
|
312
|
458
|
313 if ((ptysfd = _gftp_ptys_open (*ptymfd, ptysfd, pts_name)) < 0)
|
|
314 {
|
|
315 printf ("Cannot open slave pts %s: %s\n", pts_name,
|
|
316 g_strerror (errno));
|
|
317 return (-1);
|
|
318 }
|
|
319
|
210
|
320 close (s[0]);
|
458
|
321 close (*ptymfd);
|
210
|
322
|
|
323 _gftp_tty_raw (s[1]);
|
458
|
324 _gftp_tty_raw (ptysfd);
|
|
325
|
210
|
326 dup2 (s[1], 0);
|
|
327 dup2 (s[1], 1);
|
458
|
328 _gftp_close_all_fds (ptysfd);
|
210
|
329
|
|
330 execvp (args[0], args);
|
|
331
|
|
332 printf (_("Error: Cannot execute ssh: %s\n"), g_strerror (errno));
|
458
|
333 _exit (1);
|
210
|
334 }
|
|
335 else if (child > 0)
|
|
336 {
|
|
337 close (s[1]);
|
458
|
338
|
|
339 *fdm = s[0];
|
210
|
340 _gftp_tty_raw (s[0]);
|
458
|
341 _gftp_tty_raw (*ptymfd);
|
|
342
|
210
|
343 return (child);
|
|
344 }
|
|
345 else
|
|
346 {
|
|
347 request->logging_function (gftp_logging_error, request->user_data,
|
|
348 _("Cannot fork another process: %s\n"),
|
|
349 g_strerror (errno));
|
|
350 return (-1);
|
|
351 }
|
|
352 }
|
|
353
|