8edbe4844267e9da978492cc1ad480393b129462
[u/mdw/putty] / unix / pty.c
1 #define _XOPEN_SOURCE
2 #define _XOPEN_SOURCE_EXTENDED
3 #include <features.h>
4
5 #include <stdio.h>
6 #include <stdlib.h>
7 #include <string.h>
8 #include <unistd.h>
9 #include <signal.h>
10 #include <fcntl.h>
11 #include <termios.h>
12 #include <grp.h>
13 #include <utmp.h>
14 #include <pwd.h>
15 #include <time.h>
16 #include <sys/types.h>
17 #include <sys/wait.h>
18 #include <sys/ioctl.h>
19
20 #include "putty.h"
21
22 #ifndef FALSE
23 #define FALSE 0
24 #endif
25 #ifndef TRUE
26 #define TRUE 1
27 #endif
28
29 #ifndef UTMP_FILE
30 #define UTMP_FILE "/var/run/utmp"
31 #endif
32 #ifndef WTMP_FILE
33 #define WTMP_FILE "/var/log/wtmp"
34 #endif
35 #ifndef LASTLOG_FILE
36 #ifdef _PATH_LASTLOG
37 #define LASTLOG_FILE _PATH_LASTLOG
38 #else
39 #define LASTLOG_FILE "/var/log/lastlog"
40 #endif
41 #endif
42
43 int pty_master_fd;
44 static int pty_child_pid;
45 static sig_atomic_t pty_child_dead;
46 #ifndef OMIT_UTMP
47 static struct utmp utmp_entry;
48 #endif
49 char **pty_argv;
50
51 int pty_child_is_dead(void)
52 {
53 return pty_child_dead;
54 }
55
56 static void pty_size(void);
57
58 static void setup_utmp(char *ttyname)
59 {
60 #ifndef OMIT_UTMP
61 #ifdef HAVE_LASTLOG
62 struct lastlog lastlog_entry;
63 FILE *lastlog;
64 #endif
65 struct passwd *pw;
66 char *location = get_x_display();
67 FILE *wtmp;
68
69 pw = getpwuid(getuid());
70 memset(&utmp_entry, 0, sizeof(utmp_entry));
71 utmp_entry.ut_type = USER_PROCESS;
72 utmp_entry.ut_pid = getpid();
73 strncpy(utmp_entry.ut_line, ttyname+5, lenof(utmp_entry.ut_line));
74 strncpy(utmp_entry.ut_id, ttyname+8, lenof(utmp_entry.ut_id));
75 strncpy(utmp_entry.ut_user, pw->pw_name, lenof(utmp_entry.ut_user));
76 strncpy(utmp_entry.ut_host, location, lenof(utmp_entry.ut_host));
77 time(&utmp_entry.ut_time);
78
79 #if defined HAVE_PUTUTLINE
80 utmpname(UTMP_FILE);
81 setutent();
82 pututline(&utmp_entry);
83 endutent();
84 #endif
85
86 if ((wtmp = fopen(WTMP_FILE, "a")) != NULL) {
87 fwrite(&utmp_entry, 1, sizeof(utmp_entry), wtmp);
88 fclose(wtmp);
89 }
90
91 #ifdef HAVE_LASTLOG
92 memset(&lastlog_entry, 0, sizeof(lastlog_entry));
93 strncpy(lastlog_entry.ll_line, ttyname+5, lenof(lastlog_entry.ll_line));
94 strncpy(lastlog_entry.ll_host, location, lenof(lastlog_entry.ll_host));
95 time(&lastlog_entry.ll_time);
96 if ((lastlog = fopen(LASTLOG_FILE, "r+")) != NULL) {
97 fseek(lastlog, sizeof(lastlog_entry) * getuid(), SEEK_SET);
98 fwrite(&lastlog_entry, 1, sizeof(lastlog_entry), lastlog);
99 fclose(lastlog);
100 }
101 #endif
102
103 #endif
104 }
105
106 static void cleanup_utmp(void)
107 {
108 #ifndef OMIT_UTMP
109 FILE *wtmp;
110
111 utmp_entry.ut_type = DEAD_PROCESS;
112 memset(utmp_entry.ut_user, 0, lenof(utmp_entry.ut_user));
113 time(&utmp_entry.ut_time);
114
115 if ((wtmp = fopen(WTMP_FILE, "a")) != NULL) {
116 fwrite(&utmp_entry, 1, sizeof(utmp_entry), wtmp);
117 fclose(wtmp);
118 }
119
120 memset(utmp_entry.ut_line, 0, lenof(utmp_entry.ut_line));
121 utmp_entry.ut_time = 0;
122
123 #if defined HAVE_PUTUTLINE
124 utmpname(UTMP_FILE);
125 setutent();
126 pututline(&utmp_entry);
127 endutent();
128 #endif
129
130 #endif
131 }
132
133 static void sigchld_handler(int signum)
134 {
135 pid_t pid;
136 int status;
137 pid = waitpid(-1, &status, WNOHANG);
138 if (pid == pty_child_pid && (WIFEXITED(status) || WIFSIGNALED(status)))
139 pty_child_dead = TRUE;
140 }
141
142 static void fatal_sig_handler(int signum)
143 {
144 signal(signum, SIG_DFL);
145 cleanup_utmp();
146 setuid(getuid());
147 raise(signum);
148 }
149
150 /*
151 * Called to set up the pty.
152 *
153 * Returns an error message, or NULL on success.
154 *
155 * Also places the canonical host name into `realhost'. It must be
156 * freed by the caller.
157 */
158 static char *pty_init(char *host, int port, char **realhost, int nodelay)
159 {
160 int slavefd;
161 char name[FILENAME_MAX];
162 pid_t pid, pgrp;
163
164 #ifdef BSD_PTYS
165 {
166 const char chars1[] = "pqrstuvwxyz";
167 const char chars2[] = "0123456789abcdef";
168 const char *p1, *p2;
169 char master_name[20];
170
171 for (p1 = chars1; *p1; p1++)
172 for (p2 = chars2; *p2; p2++) {
173 sprintf(master_name, "/dev/pty%c%c", *p1, *p2);
174 pty_master_fd = open(master_name, O_RDWR);
175 if (pty_master_fd >= 0) {
176 if (geteuid() == 0 ||
177 access(master_name, R_OK | W_OK) == 0)
178 goto got_one;
179 close(pty_master_fd);
180 }
181 }
182
183 /* If we get here, we couldn't get a tty at all. */
184 fprintf(stderr, "pterm: unable to open a pseudo-terminal device\n");
185 exit(1);
186
187 got_one:
188 strcpy(name, master_name);
189 name[5] = 't'; /* /dev/ptyXX -> /dev/ttyXX */
190 }
191 #else
192 pty_master_fd = open("/dev/ptmx", O_RDWR);
193
194 if (pty_master_fd < 0) {
195 perror("/dev/ptmx: open");
196 exit(1);
197 }
198
199 if (grantpt(pty_master_fd) < 0) {
200 perror("grantpt");
201 exit(1);
202 }
203
204 if (unlockpt(pty_master_fd) < 0) {
205 perror("unlockpt");
206 exit(1);
207 }
208
209 name[FILENAME_MAX-1] = '\0';
210 strncpy(name, ptsname(pty_master_fd), FILENAME_MAX-1);
211 #endif
212
213 /*
214 * Trap as many fatal signals as we can in the hope of having
215 * the best chance to clean up utmp before termination.
216 */
217 signal(SIGHUP, fatal_sig_handler);
218 signal(SIGINT, fatal_sig_handler);
219 signal(SIGQUIT, fatal_sig_handler);
220 signal(SIGILL, fatal_sig_handler);
221 signal(SIGABRT, fatal_sig_handler);
222 signal(SIGFPE, fatal_sig_handler);
223 signal(SIGPIPE, fatal_sig_handler);
224 signal(SIGALRM, fatal_sig_handler);
225 signal(SIGTERM, fatal_sig_handler);
226 signal(SIGSEGV, fatal_sig_handler);
227 signal(SIGUSR1, fatal_sig_handler);
228 signal(SIGUSR2, fatal_sig_handler);
229 #ifdef SIGBUS
230 signal(SIGBUS, fatal_sig_handler);
231 #endif
232 #ifdef SIGPOLL
233 signal(SIGPOLL, fatal_sig_handler);
234 #endif
235 #ifdef SIGPROF
236 signal(SIGPROF, fatal_sig_handler);
237 #endif
238 #ifdef SIGSYS
239 signal(SIGSYS, fatal_sig_handler);
240 #endif
241 #ifdef SIGTRAP
242 signal(SIGTRAP, fatal_sig_handler);
243 #endif
244 #ifdef SIGVTALRM
245 signal(SIGVTALRM, fatal_sig_handler);
246 #endif
247 #ifdef SIGXCPU
248 signal(SIGXCPU, fatal_sig_handler);
249 #endif
250 #ifdef SIGXFSZ
251 signal(SIGXFSZ, fatal_sig_handler);
252 #endif
253 #ifdef SIGIO
254 signal(SIGIO, fatal_sig_handler);
255 #endif
256 /* Also clean up utmp on normal exit. */
257 atexit(cleanup_utmp);
258 setup_utmp(name);
259
260 /*
261 * Fork and execute the command.
262 */
263 pid = fork();
264 if (pid < 0) {
265 perror("fork");
266 exit(1);
267 }
268
269 if (pid == 0) {
270 int i;
271 /*
272 * We are the child.
273 */
274
275 slavefd = open(name, O_RDWR);
276 if (slavefd < 0) {
277 perror("slave pty: open");
278 exit(1);
279 }
280
281 #ifdef BSD_PTYS
282 /* We need to chown/chmod the /dev/ttyXX device. */
283 {
284 struct group *gp = getgrnam("tty");
285 fchown(slavefd, getuid(), gp ? gp->gr_gid : -1);
286 fchmod(slavefd, 0600);
287 }
288 #endif
289
290 close(pty_master_fd);
291 close(0);
292 close(1);
293 close(2);
294 fcntl(slavefd, F_SETFD, 0); /* don't close on exec */
295 dup2(slavefd, 0);
296 dup2(slavefd, 1);
297 dup2(slavefd, 2);
298 setsid();
299 ioctl(slavefd, TIOCSCTTY, 1);
300 pgrp = getpid();
301 tcsetpgrp(slavefd, pgrp);
302 setpgrp();
303 close(open(name, O_WRONLY, 0));
304 setpgrp();
305 /* In case we were setgid-utmp or setuid-root, drop privs. */
306 setgid(getgid());
307 setuid(getuid());
308 /* Close everything _else_, for tidiness. */
309 for (i = 3; i < 1024; i++)
310 close(i);
311 {
312 char term_env_var[10 + sizeof(cfg.termtype)];
313 sprintf(term_env_var, "TERM=%s", cfg.termtype);
314 putenv(term_env_var);
315 }
316 if (pty_argv)
317 execvp(pty_argv[0], pty_argv);
318 else
319 execl(getenv("SHELL"), getenv("SHELL"), NULL);
320 /*
321 * If we're here, exec has gone badly foom.
322 */
323 perror("exec");
324 exit(127);
325 } else {
326 close(slavefd);
327 pty_child_pid = pid;
328 pty_child_dead = FALSE;
329 signal(SIGCHLD, sigchld_handler);
330 }
331
332 return NULL;
333 }
334
335 /*
336 * Called to send data down the pty.
337 */
338 static int pty_send(char *buf, int len)
339 {
340 while (len > 0) {
341 int ret = write(pty_master_fd, buf, len);
342 if (ret < 0) {
343 perror("write pty master");
344 exit(1);
345 }
346 buf += ret;
347 len -= ret;
348 }
349 return 0;
350 }
351
352 /*
353 * Called to query the current socket sendability status.
354 */
355 static int pty_sendbuffer(void)
356 {
357 return 0;
358 }
359
360 /*
361 * Called to set the size of the window
362 */
363 static void pty_size(void)
364 {
365 struct winsize size;
366
367 size.ws_row = (unsigned short)rows;
368 size.ws_col = (unsigned short)cols;
369 ioctl(pty_master_fd, TIOCSWINSZ, (void *)&size);
370 return;
371 }
372
373 /*
374 * Send special codes.
375 */
376 static void pty_special(Telnet_Special code)
377 {
378 /* Do nothing! */
379 return;
380 }
381
382 static Socket pty_socket(void)
383 {
384 return NULL; /* shouldn't ever be needed */
385 }
386
387 static int pty_sendok(void)
388 {
389 return 1;
390 }
391
392 static void pty_unthrottle(int backlog)
393 {
394 /* do nothing */
395 }
396
397 static int pty_ldisc(int option)
398 {
399 return 0; /* neither editing nor echoing */
400 }
401
402 static int pty_exitcode(void)
403 {
404 /* Shouldn't ever be required */
405 return 0;
406 }
407
408 Backend pty_backend = {
409 pty_init,
410 pty_send,
411 pty_sendbuffer,
412 pty_size,
413 pty_special,
414 pty_socket,
415 pty_exitcode,
416 pty_sendok,
417 pty_ldisc,
418 pty_unthrottle,
419 1
420 };