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