2 * Pseudo-tty backend for pterm.
4 * Unlike the other backends, data for this one is not neatly
5 * encapsulated into a data structure, because it wouldn't make
6 * sense to do so - the utmp stuff has to be done before a backend
7 * is initialised, and starting a second pterm from the same
8 * process would therefore be infeasible because privileges would
9 * already have been dropped. Hence, I haven't bothered to keep the
10 * data dynamically allocated: instead, the backend handle is just
11 * a null pointer and ignored everywhere.
15 #define _XOPEN_SOURCE_EXTENDED
29 #include <sys/types.h>
31 #include <sys/ioctl.h>
44 #define UTMP_FILE "/var/run/utmp"
47 #define WTMP_FILE "/var/log/wtmp"
51 #define LASTLOG_FILE _PATH_LASTLOG
53 #define LASTLOG_FILE "/var/log/lastlog"
58 * Set up a default for vaguely sane systems. The idea is that if
59 * OMIT_UTMP is not defined, then at least one of the symbols which
60 * enable particular forms of utmp processing should be, if only so
61 * that a link error can warn you that you should have defined
62 * OMIT_UTMP if you didn't want any. Currently HAVE_PUTUTLINE is
63 * the only such symbol.
66 #if !defined HAVE_PUTUTLINE
67 #define HAVE_PUTUTLINE
72 static void *pty_frontend;
73 static char pty_name[FILENAME_MAX];
74 static int pty_stamped_utmp = 0;
75 static int pty_child_pid;
76 static int pty_utmp_helper_pid, pty_utmp_helper_pipe;
77 static int pty_term_width, pty_term_height;
78 static volatile sig_atomic_t pty_child_dead;
79 static volatile int pty_exit_code;
81 static struct utmp utmp_entry;
85 static void setup_utmp(char *ttyname, char *location)
89 struct lastlog lastlog_entry;
95 pw = getpwuid(getuid());
96 memset(&utmp_entry, 0, sizeof(utmp_entry));
97 utmp_entry.ut_type = USER_PROCESS;
98 utmp_entry.ut_pid = getpid();
99 strncpy(utmp_entry.ut_line, ttyname+5, lenof(utmp_entry.ut_line));
100 strncpy(utmp_entry.ut_id, ttyname+8, lenof(utmp_entry.ut_id));
101 strncpy(utmp_entry.ut_user, pw->pw_name, lenof(utmp_entry.ut_user));
102 strncpy(utmp_entry.ut_host, location, lenof(utmp_entry.ut_host));
103 time(&utmp_entry.ut_time);
105 #if defined HAVE_PUTUTLINE
108 pututline(&utmp_entry);
112 if ((wtmp = fopen(WTMP_FILE, "a")) != NULL) {
113 fwrite(&utmp_entry, 1, sizeof(utmp_entry), wtmp);
118 memset(&lastlog_entry, 0, sizeof(lastlog_entry));
119 strncpy(lastlog_entry.ll_line, ttyname+5, lenof(lastlog_entry.ll_line));
120 strncpy(lastlog_entry.ll_host, location, lenof(lastlog_entry.ll_host));
121 time(&lastlog_entry.ll_time);
122 if ((lastlog = fopen(LASTLOG_FILE, "r+")) != NULL) {
123 fseek(lastlog, sizeof(lastlog_entry) * getuid(), SEEK_SET);
124 fwrite(&lastlog_entry, 1, sizeof(lastlog_entry), lastlog);
129 pty_stamped_utmp = 1;
134 static void cleanup_utmp(void)
139 if (!pty_stamped_utmp)
142 utmp_entry.ut_type = DEAD_PROCESS;
143 memset(utmp_entry.ut_user, 0, lenof(utmp_entry.ut_user));
144 time(&utmp_entry.ut_time);
146 if ((wtmp = fopen(WTMP_FILE, "a")) != NULL) {
147 fwrite(&utmp_entry, 1, sizeof(utmp_entry), wtmp);
151 memset(utmp_entry.ut_line, 0, lenof(utmp_entry.ut_line));
152 utmp_entry.ut_time = 0;
154 #if defined HAVE_PUTUTLINE
157 pututline(&utmp_entry);
161 pty_stamped_utmp = 0; /* ensure we never double-cleanup */
165 static void sigchld_handler(int signum)
167 int save_errno = errno;
172 pid = waitpid(-1, &status, WNOHANG);
173 if (pid == pty_child_pid && (WIFEXITED(status) || WIFSIGNALED(status))) {
174 pty_exit_code = status;
175 pty_child_dead = TRUE;
181 static void fatal_sig_handler(int signum)
183 putty_signal(signum, SIG_DFL);
189 static void pty_open_master(void)
192 const char chars1[] = "pqrstuvwxyz";
193 const char chars2[] = "0123456789abcdef";
195 char master_name[20];
198 for (p1 = chars1; *p1; p1++)
199 for (p2 = chars2; *p2; p2++) {
200 sprintf(master_name, "/dev/pty%c%c", *p1, *p2);
201 pty_master_fd = open(master_name, O_RDWR);
202 if (pty_master_fd >= 0) {
203 if (geteuid() == 0 ||
204 access(master_name, R_OK | W_OK) == 0)
206 close(pty_master_fd);
210 /* If we get here, we couldn't get a tty at all. */
211 fprintf(stderr, "pterm: unable to open a pseudo-terminal device\n");
215 strcpy(pty_name, master_name);
216 pty_name[5] = 't'; /* /dev/ptyXX -> /dev/ttyXX */
218 /* We need to chown/chmod the /dev/ttyXX device. */
219 gp = getgrnam("tty");
220 chown(pty_name, getuid(), gp ? gp->gr_gid : -1);
221 chmod(pty_name, 0600);
223 pty_master_fd = open("/dev/ptmx", O_RDWR);
225 if (pty_master_fd < 0) {
226 perror("/dev/ptmx: open");
230 if (grantpt(pty_master_fd) < 0) {
235 if (unlockpt(pty_master_fd) < 0) {
240 pty_name[FILENAME_MAX-1] = '\0';
241 strncpy(pty_name, ptsname(pty_master_fd), FILENAME_MAX-1);
246 * Pre-initialisation. This is here to get around the fact that GTK
247 * doesn't like being run in setuid/setgid programs (probably
248 * sensibly). So before we initialise GTK - and therefore before we
249 * even process the command line - we check to see if we're running
250 * set[ug]id. If so, we open our pty master _now_, chown it as
251 * necessary, and drop privileges. We can always close it again
252 * later. If we're potentially going to be doing utmp as well, we
253 * also fork off a utmp helper process and communicate with it by
254 * means of a pipe; the utmp helper will keep privileges in order
255 * to clean up utmp when we exit (i.e. when its end of our pipe
258 void pty_pre_init(void)
263 /* set the child signal handler straight away; it needs to be set
264 * before we ever fork. */
265 putty_signal(SIGCHLD, sigchld_handler);
268 if (geteuid() != getuid() || getegid() != getgid()) {
274 * Fork off the utmp helper.
276 if (pipe(pipefd) < 0) {
277 perror("pterm: pipe");
282 perror("pterm: fork");
284 } else if (pid == 0) {
285 char display[128], buffer[128];
290 * Now sit here until we receive a display name from the
291 * other end of the pipe, and then stamp utmp. Unstamp utmp
292 * again, and exit, when the pipe closes.
298 ret = read(pipefd[0], buffer, lenof(buffer));
302 } else if (!pty_stamped_utmp) {
303 if (dlen < lenof(display))
304 memcpy(display+dlen, buffer,
305 min(ret, lenof(display)-dlen));
306 if (buffer[ret-1] == '\0') {
308 * Now we have a display name. NUL-terminate
309 * it, and stamp utmp.
311 display[lenof(display)-1] = '\0';
313 * Trap as many fatal signals as we can in the
314 * hope of having the best possible chance to
315 * clean up utmp before termination. We are
316 * unfortunately unprotected against SIGKILL,
319 putty_signal(SIGHUP, fatal_sig_handler);
320 putty_signal(SIGINT, fatal_sig_handler);
321 putty_signal(SIGQUIT, fatal_sig_handler);
322 putty_signal(SIGILL, fatal_sig_handler);
323 putty_signal(SIGABRT, fatal_sig_handler);
324 putty_signal(SIGFPE, fatal_sig_handler);
325 putty_signal(SIGPIPE, fatal_sig_handler);
326 putty_signal(SIGALRM, fatal_sig_handler);
327 putty_signal(SIGTERM, fatal_sig_handler);
328 putty_signal(SIGSEGV, fatal_sig_handler);
329 putty_signal(SIGUSR1, fatal_sig_handler);
330 putty_signal(SIGUSR2, fatal_sig_handler);
332 putty_signal(SIGBUS, fatal_sig_handler);
335 putty_signal(SIGPOLL, fatal_sig_handler);
338 putty_signal(SIGPROF, fatal_sig_handler);
341 putty_signal(SIGSYS, fatal_sig_handler);
344 putty_signal(SIGTRAP, fatal_sig_handler);
347 putty_signal(SIGVTALRM, fatal_sig_handler);
350 putty_signal(SIGXCPU, fatal_sig_handler);
353 putty_signal(SIGXFSZ, fatal_sig_handler);
356 putty_signal(SIGIO, fatal_sig_handler);
358 setup_utmp(pty_name, display);
364 pty_utmp_helper_pid = pid;
365 pty_utmp_helper_pipe = pipefd[1];
371 int gid = getgid(), uid = getuid();
372 #ifndef HAVE_NO_SETRESUID
373 int setresgid(gid_t, gid_t, gid_t);
374 int setresuid(uid_t, uid_t, uid_t);
375 setresgid(gid, gid, gid);
376 setresuid(uid, uid, uid);
385 * Called to set up the pty.
387 * Returns an error message, or NULL on success.
389 * Also places the canonical host name into `realhost'. It must be
390 * freed by the caller.
392 static char *pty_init(void *frontend, void **backend_handle,
393 char *host, int port, char **realhost, int nodelay)
398 pty_frontend = frontend;
399 *backend_handle = NULL; /* we can't sensibly use this, sadly */
401 pty_term_width = cfg.width;
402 pty_term_height = cfg.height;
404 if (pty_master_fd < 0)
408 * Set the backspace character to be whichever of ^H and ^? is
409 * specified by bksp_is_delete.
412 struct termios attrs;
413 tcgetattr(pty_master_fd, &attrs);
414 attrs.c_cc[VERASE] = cfg.bksp_is_delete ? '\177' : '\010';
415 tcsetattr(pty_master_fd, TCSANOW, &attrs);
419 * Stamp utmp (that is, tell the utmp helper process to do so),
423 close(pty_utmp_helper_pipe); /* just let the child process die */
425 char *location = get_x_display(pty_frontend);
426 int len = strlen(location)+1, pos = 0; /* +1 to include NUL */
428 int ret = write(pty_utmp_helper_pipe, location+pos, len - pos);
430 perror("pterm: writing to utmp helper process");
431 close(pty_utmp_helper_pipe); /* arrgh, just give up */
439 * Fork and execute the command.
453 slavefd = open(pty_name, O_RDWR);
455 perror("slave pty: open");
459 close(pty_master_fd);
460 fcntl(slavefd, F_SETFD, 0); /* don't close on exec */
465 ioctl(slavefd, TIOCSCTTY, 1);
467 tcsetpgrp(slavefd, pgrp);
469 close(open(pty_name, O_WRONLY, 0));
471 /* Close everything _else_, for tidiness. */
472 for (i = 3; i < 1024; i++)
475 char term_env_var[10 + sizeof(cfg.termtype)];
476 sprintf(term_env_var, "TERM=%s", cfg.termtype);
477 putenv(term_env_var);
480 * SIGINT and SIGQUIT may have been set to ignored by our
481 * parent, particularly by things like sh -c 'pterm &' and
482 * some window managers. Reverse this for our child process.
484 putty_signal(SIGINT, SIG_DFL);
485 putty_signal(SIGQUIT, SIG_DFL);
487 execvp(pty_argv[0], pty_argv);
489 char *shell = getenv("SHELL");
491 if (cfg.login_shell) {
492 char *p = strrchr(shell, '/');
493 shellname = smalloc(2+strlen(shell));
495 sprintf(shellname, "-%s", p);
498 execl(getenv("SHELL"), shellname, NULL);
502 * If we're here, exec has gone badly foom.
508 pty_child_dead = FALSE;
515 * Called to send data down the pty.
517 static int pty_send(void *handle, char *buf, int len)
519 if (pty_master_fd < 0)
520 return 0; /* ignore all writes if fd closed */
523 int ret = write(pty_master_fd, buf, len);
525 perror("write pty master");
536 if (pty_master_fd >= 0) {
537 close(pty_master_fd);
540 close(pty_utmp_helper_pipe); /* this causes utmp to be cleaned up */
544 * Called to query the current socket sendability status.
546 static int pty_sendbuffer(void *handle)
552 * Called to set the size of the window
554 static void pty_size(void *handle, int width, int height)
558 pty_term_width = width;
559 pty_term_height = height;
561 size.ws_row = (unsigned short)pty_term_height;
562 size.ws_col = (unsigned short)pty_term_width;
563 size.ws_xpixel = (unsigned short) pty_term_width *
564 font_dimension(pty_frontend, 0);
565 size.ws_ypixel = (unsigned short) pty_term_height *
566 font_dimension(pty_frontend, 1);
567 ioctl(pty_master_fd, TIOCSWINSZ, (void *)&size);
572 * Send special codes.
574 static void pty_special(void *handle, Telnet_Special code)
580 static Socket pty_socket(void *handle)
582 return NULL; /* shouldn't ever be needed */
585 static int pty_sendok(void *handle)
590 static void pty_unthrottle(void *handle, int backlog)
595 static int pty_ldisc(void *handle, int option)
597 return 0; /* neither editing nor echoing */
600 static void pty_provide_ldisc(void *handle, void *ldisc)
602 /* This is a stub. */
605 static void pty_provide_logctx(void *handle, void *logctx)
607 /* This is a stub. */
610 static int pty_exitcode(void *handle)
613 return -1; /* not dead yet */
615 return pty_exit_code;
618 Backend pty_backend = {