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);
|
|
43
|
|
44 return (fdm);
|
|
45 }
|
|
46
|
|
47
|
|
48 static int
|
|
49 _gftp_ptys_open (int fdm, int fds, char *pts_name)
|
|
50 {
|
|
51 int new_fds;
|
|
52
|
|
53 if ((new_fds = open (pts_name, O_RDWR)) < 0)
|
146
|
54 {
|
210
|
55 close (fdm);
|
|
56 return (-1);
|
146
|
57 }
|
|
58
|
210
|
59 return (new_fds);
|
146
|
60 }
|
|
61
|
232
|
62 #elif HAVE_OPENPTY
|
|
63
|
|
64 char *
|
|
65 gftp_get_pty_impl (void)
|
|
66 {
|
|
67 return ("openpty");
|
|
68 }
|
|
69
|
|
70
|
|
71 static int
|
|
72 _gftp_ptym_open (char *pts_name, size_t len, int *fds)
|
|
73 {
|
|
74 int fdm;
|
|
75
|
|
76 if (openpty (&fdm, fds, pts_name, NULL, NULL) < 0)
|
|
77 return (GFTP_ERETRYABLE);
|
|
78
|
|
79 ioctl (*fds, TIOCSCTTY, NULL);
|
|
80
|
|
81 return (fdm);
|
|
82 }
|
|
83
|
|
84
|
|
85 static int
|
|
86 _gftp_ptys_open (int fdm, int fds, char *pts_name)
|
|
87 {
|
|
88 if (login_tty (fds) < 0)
|
|
89 return (GFTP_EFATAL);
|
|
90
|
|
91 return (fds);
|
|
92 }
|
|
93
|
178
|
94 #elif HAVE_GRANTPT
|
146
|
95
|
179
|
96 char *
|
210
|
97 gftp_get_pty_impl (void)
|
179
|
98 {
|
|
99 return ("unix98");
|
|
100 }
|
|
101
|
|
102
|
210
|
103 static int
|
|
104 _gftp_ptym_open (char *pts_name, size_t len, int *fds)
|
146
|
105 {
|
210
|
106 char *new_pts_name;
|
|
107 int fdm;
|
146
|
108
|
210
|
109 if ((fdm = open ("/dev/ptmx", O_RDWR)) < 0)
|
146
|
110 return (GFTP_ERETRYABLE);
|
|
111
|
210
|
112 if (grantpt (fdm) < 0)
|
146
|
113 {
|
210
|
114 close (fdm);
|
|
115 return (GFTP_ERETRYABLE);
|
|
116 }
|
|
117
|
|
118 if (unlockpt (fdm) < 0)
|
|
119 {
|
|
120 close (fdm);
|
146
|
121 return (GFTP_ERETRYABLE);
|
|
122 }
|
|
123
|
210
|
124 if ((new_pts_name = ptsname (fdm)) == NULL)
|
146
|
125 {
|
210
|
126 close (fdm);
|
146
|
127 return (GFTP_ERETRYABLE);
|
|
128 }
|
|
129
|
210
|
130 strncpy (pts_name, new_pts_name, len);
|
|
131
|
|
132 return (fdm);
|
|
133 }
|
|
134
|
|
135
|
|
136 static int
|
|
137 _gftp_ptys_open (int fdm, int fds, char *pts_name)
|
|
138 {
|
|
139 int new_fds;
|
|
140
|
|
141 if ((new_fds = open (pts_name, O_RDWR)) < 0)
|
146
|
142 {
|
210
|
143 close (fdm);
|
|
144 return (-1);
|
146
|
145 }
|
|
146
|
179
|
147 #ifdef SYSV
|
146
|
148 /* I intentionally ignore these errors */
|
210
|
149 ioctl (new_fds, I_PUSH, "ptem");
|
|
150 ioctl (new_fds, I_PUSH, "ldterm");
|
|
151 ioctl (new_fds, I_PUSH, "ttcompat");
|
179
|
152 #endif
|
146
|
153
|
210
|
154 return (new_fds);
|
146
|
155 }
|
|
156
|
232
|
157 #else /* !HAVE_GRANTPT */
|
146
|
158
|
|
159 /* Fall back to *BSD... */
|
|
160
|
179
|
161 char *
|
210
|
162 gftp_get_pty_impl (void)
|
179
|
163 {
|
|
164 return ("bsd");
|
|
165 }
|
|
166
|
|
167
|
210
|
168 static int
|
|
169 _gftp_ptym_open (char *pts_name, size_t len, int *fds)
|
146
|
170 {
|
210
|
171 char *pos1, *pos2;
|
|
172 int fdm;
|
146
|
173
|
210
|
174 g_return_val_if_fail (len >= 10, GFTP_EFATAL);
|
|
175
|
|
176 strncpy (pts_name, "/dev/ptyXY", len);
|
146
|
177 for (pos1 = "pqrstuvwxyzPQRST"; *pos1 != '\0'; pos1++)
|
|
178 {
|
|
179 pts_name[8] = *pos1;
|
|
180 for (pos2 = "0123456789abcdef"; *pos2 != '\0'; pos2++)
|
|
181 {
|
|
182 pts_name[9] = *pos2;
|
210
|
183 if ((fdm = open (pts_name, O_RDWR)) < 0)
|
146
|
184 continue;
|
|
185
|
|
186 pts_name[5] = 't';
|
|
187 chmod (pts_name, S_IRUSR | S_IWUSR);
|
|
188 chown (pts_name, getuid (), -1);
|
|
189
|
210
|
190 return (fdm);
|
146
|
191 }
|
|
192 }
|
|
193
|
|
194 return (GFTP_ERETRYABLE);
|
|
195 }
|
|
196
|
210
|
197
|
|
198 static int
|
|
199 _gftp_ptys_open (int fdm, int fds, char *pts_name)
|
|
200 {
|
|
201 int new_fds;
|
|
202
|
|
203 if ((new_fds = open (pts_name, O_RDWR)) < 0)
|
|
204 {
|
|
205 close (fdm);
|
|
206 return (-1);
|
|
207 }
|
|
208
|
|
209 #if defined(TIOCSCTTY) && !defined(CIBAUD)
|
|
210 ioctl (new_fds, TIOCSCTTY, NULL);
|
|
211 #endif
|
|
212
|
|
213 return (new_fds);
|
|
214 }
|
|
215
|
146
|
216 #endif /* __sgi */
|
|
217
|
|
218
|
210
|
219 static int
|
|
220 _gftp_tty_raw (int fd)
|
146
|
221 {
|
|
222 struct termios buf;
|
|
223
|
|
224 if (tcgetattr (fd, &buf) < 0)
|
|
225 return (-1);
|
|
226
|
|
227 buf.c_iflag |= IGNPAR;
|
|
228 buf.c_iflag &= ~(ICRNL | ISTRIP | IXON | IGNCR | IXANY | IXOFF | INLCR);
|
|
229 buf.c_lflag &= ~(ECHO | ICANON | ISIG | ECHOE | ECHOK | ECHONL);
|
|
230 #ifdef IEXTEN
|
|
231 buf.c_lflag &= ~(IEXTEN);
|
|
232 #endif
|
|
233
|
|
234 buf.c_oflag &= ~(OPOST);
|
|
235 buf.c_cc[VMIN] = 1;
|
|
236 buf.c_cc[VTIME] = 0;
|
|
237
|
|
238 if (tcsetattr (fd, TCSADRAIN, &buf) < 0)
|
|
239 return (-1);
|
|
240 return (0);
|
|
241 }
|
|
242
|
|
243
|
210
|
244 static void
|
|
245 _gftp_close_all_fds (void)
|
|
246 {
|
|
247 int i, maxfds;
|
|
248
|
|
249 #ifdef HAVE_GETDTABLESIZE
|
|
250 maxfds = getdtablesize () - 1;
|
|
251 #elif defined (OPEN_MAX)
|
|
252 maxfds = OPEN_MAX;
|
|
253 #else
|
|
254 maxfds = -1;
|
|
255 #endif
|
|
256
|
|
257 for (i=3; i<maxfds; i++)
|
|
258 close (i);
|
|
259 }
|
|
260
|
|
261
|
|
262 pid_t
|
|
263 gftp_exec_without_new_pty (gftp_request * request, int *fdm, char **args)
|
|
264 {
|
|
265 pid_t child;
|
|
266 int s[2];
|
|
267
|
|
268 if (socketpair (AF_LOCAL, SOCK_STREAM, 0, s) < 0)
|
|
269 {
|
|
270 request->logging_function (gftp_logging_error, request,
|
|
271 _("Cannot create a socket pair: %s\n"),
|
|
272 g_strerror (errno));
|
|
273 return (-1);
|
|
274 }
|
|
275
|
|
276 if ((child = fork ()) == 0)
|
|
277 {
|
|
278 setsid ();
|
|
279
|
|
280 close (s[0]);
|
|
281
|
|
282 _gftp_tty_raw (s[1]);
|
|
283 dup2 (s[1], 0);
|
|
284 dup2 (s[1], 1);
|
|
285 dup2 (s[1], 2);
|
|
286 _gftp_close_all_fds ();
|
|
287
|
|
288 execvp (args[0], args);
|
|
289
|
|
290 printf (_("Error: Cannot execute ssh: %s\n"), g_strerror (errno));
|
|
291 exit (1);
|
|
292 }
|
|
293 else if (child > 0)
|
|
294 {
|
|
295 close (s[1]);
|
|
296 _gftp_tty_raw (s[0]);
|
|
297 *fdm = s[0];
|
|
298 return (child);
|
|
299 }
|
|
300 else
|
|
301 {
|
|
302 request->logging_function (gftp_logging_error, request->user_data,
|
|
303 _("Cannot fork another process: %s\n"),
|
|
304 g_strerror (errno));
|
|
305 return (-1);
|
|
306 }
|
|
307 }
|
|
308
|
|
309
|
|
310 pid_t
|
|
311 gftp_exec_with_new_pty (gftp_request * request, int *fdm, char **args)
|
|
312 {
|
|
313 char pts_name[64];
|
|
314 pid_t child;
|
|
315 int fds;
|
|
316
|
|
317 *pts_name = '\0';
|
|
318 if ((*fdm = _gftp_ptym_open (pts_name, sizeof (pts_name), &fds)) < 0)
|
|
319 {
|
|
320 request->logging_function (gftp_logging_error, request->user_data,
|
|
321 _("Cannot open master pty %s: %s\n"), pts_name,
|
|
322 g_strerror (errno));
|
|
323 return (-1);
|
|
324 }
|
|
325
|
|
326 if ((child = fork ()) == 0)
|
|
327 {
|
|
328 setsid ();
|
|
329
|
|
330 if ((fds = _gftp_ptys_open (*fdm, fds, pts_name)) < 0)
|
|
331 {
|
|
332 printf ("Cannot open slave pts %s: %s\n", pts_name,
|
|
333 g_strerror (errno));
|
|
334 return (-1);
|
|
335 }
|
|
336
|
|
337 close (*fdm);
|
|
338
|
|
339 _gftp_tty_raw (fds);
|
|
340 dup2 (fds, 0);
|
|
341 dup2 (fds, 1);
|
|
342 dup2 (fds, 2);
|
|
343 _gftp_close_all_fds ();
|
|
344
|
|
345 execvp (args[0], args);
|
|
346
|
|
347 printf (_("Error: Cannot execute ssh: %s\n"), g_strerror (errno));
|
|
348 exit (1);
|
|
349 }
|
|
350 else if (child > 0)
|
|
351 {
|
|
352 _gftp_tty_raw (*fdm);
|
|
353 return (child);
|
|
354 }
|
|
355 else
|
|
356 {
|
|
357 request->logging_function (gftp_logging_error, request->user_data,
|
|
358 _("Cannot fork another process: %s\n"),
|
|
359 g_strerror (errno));
|
|
360 return (-1);
|
|
361 }
|
|
362 }
|
|
363
|