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>
43 #define UTMP_FILE "/var/run/utmp"
46 #define WTMP_FILE "/var/log/wtmp"
50 #define LASTLOG_FILE _PATH_LASTLOG
52 #define LASTLOG_FILE "/var/log/lastlog"
57 * Set up a default for vaguely sane systems. The idea is that if
58 * OMIT_UTMP is not defined, then at least one of the symbols which
59 * enable particular forms of utmp processing should be, if only so
60 * that a link error can warn you that you should have defined
61 * OMIT_UTMP if you didn't want any. Currently HAVE_PUTUTLINE is
62 * the only such symbol.
65 #if !defined HAVE_PUTUTLINE
66 #define HAVE_PUTUTLINE
71 static char pty_name[FILENAME_MAX];
72 static int pty_stamped_utmp = 0;
73 static int pty_child_pid;
74 static int pty_utmp_helper_pid, pty_utmp_helper_pipe;
75 static int pty_term_width, pty_term_height;
76 static sig_atomic_t pty_child_dead;
77 static int pty_exit_code;
79 static struct utmp utmp_entry;
83 static void setup_utmp(char *ttyname, char *location)
87 struct lastlog lastlog_entry;
93 pw = getpwuid(getuid());
94 memset(&utmp_entry, 0, sizeof(utmp_entry));
95 utmp_entry.ut_type = USER_PROCESS;
96 utmp_entry.ut_pid = getpid();
97 strncpy(utmp_entry.ut_line, ttyname+5, lenof(utmp_entry.ut_line));
98 strncpy(utmp_entry.ut_id, ttyname+8, lenof(utmp_entry.ut_id));
99 strncpy(utmp_entry.ut_user, pw->pw_name, lenof(utmp_entry.ut_user));
100 strncpy(utmp_entry.ut_host, location, lenof(utmp_entry.ut_host));
101 time(&utmp_entry.ut_time);
103 #if defined HAVE_PUTUTLINE
106 pututline(&utmp_entry);
110 if ((wtmp = fopen(WTMP_FILE, "a")) != NULL) {
111 fwrite(&utmp_entry, 1, sizeof(utmp_entry), wtmp);
116 memset(&lastlog_entry, 0, sizeof(lastlog_entry));
117 strncpy(lastlog_entry.ll_line, ttyname+5, lenof(lastlog_entry.ll_line));
118 strncpy(lastlog_entry.ll_host, location, lenof(lastlog_entry.ll_host));
119 time(&lastlog_entry.ll_time);
120 if ((lastlog = fopen(LASTLOG_FILE, "r+")) != NULL) {
121 fseek(lastlog, sizeof(lastlog_entry) * getuid(), SEEK_SET);
122 fwrite(&lastlog_entry, 1, sizeof(lastlog_entry), lastlog);
127 pty_stamped_utmp = 1;
132 static void cleanup_utmp(void)
137 if (!pty_stamped_utmp)
140 utmp_entry.ut_type = DEAD_PROCESS;
141 memset(utmp_entry.ut_user, 0, lenof(utmp_entry.ut_user));
142 time(&utmp_entry.ut_time);
144 if ((wtmp = fopen(WTMP_FILE, "a")) != NULL) {
145 fwrite(&utmp_entry, 1, sizeof(utmp_entry), wtmp);
149 memset(utmp_entry.ut_line, 0, lenof(utmp_entry.ut_line));
150 utmp_entry.ut_time = 0;
152 #if defined HAVE_PUTUTLINE
155 pututline(&utmp_entry);
159 pty_stamped_utmp = 0; /* ensure we never double-cleanup */
163 static void sigchld_handler(int signum)
167 pid = waitpid(-1, &status, WNOHANG);
168 if (pid == pty_child_pid && (WIFEXITED(status) || WIFSIGNALED(status))) {
169 pty_exit_code = status;
170 pty_child_dead = TRUE;
174 static void fatal_sig_handler(int signum)
176 signal(signum, SIG_DFL);
182 static void pty_open_master(void)
185 const char chars1[] = "pqrstuvwxyz";
186 const char chars2[] = "0123456789abcdef";
188 char master_name[20];
191 for (p1 = chars1; *p1; p1++)
192 for (p2 = chars2; *p2; p2++) {
193 sprintf(master_name, "/dev/pty%c%c", *p1, *p2);
194 pty_master_fd = open(master_name, O_RDWR);
195 if (pty_master_fd >= 0) {
196 if (geteuid() == 0 ||
197 access(master_name, R_OK | W_OK) == 0)
199 close(pty_master_fd);
203 /* If we get here, we couldn't get a tty at all. */
204 fprintf(stderr, "pterm: unable to open a pseudo-terminal device\n");
208 strcpy(pty_name, master_name);
209 pty_name[5] = 't'; /* /dev/ptyXX -> /dev/ttyXX */
211 /* We need to chown/chmod the /dev/ttyXX device. */
212 gp = getgrnam("tty");
213 chown(pty_name, getuid(), gp ? gp->gr_gid : -1);
214 chmod(pty_name, 0600);
216 pty_master_fd = open("/dev/ptmx", O_RDWR);
218 if (pty_master_fd < 0) {
219 perror("/dev/ptmx: open");
223 if (grantpt(pty_master_fd) < 0) {
228 if (unlockpt(pty_master_fd) < 0) {
233 pty_name[FILENAME_MAX-1] = '\0';
234 strncpy(pty_name, ptsname(pty_master_fd), FILENAME_MAX-1);
239 * Pre-initialisation. This is here to get around the fact that GTK
240 * doesn't like being run in setuid/setgid programs (probably
241 * sensibly). So before we initialise GTK - and therefore before we
242 * even process the command line - we check to see if we're running
243 * set[ug]id. If so, we open our pty master _now_, chown it as
244 * necessary, and drop privileges. We can always close it again
245 * later. If we're potentially going to be doing utmp as well, we
246 * also fork off a utmp helper process and communicate with it by
247 * means of a pipe; the utmp helper will keep privileges in order
248 * to clean up utmp when we exit (i.e. when its end of our pipe
251 void pty_pre_init(void)
258 if (geteuid() != getuid() || getegid() != getgid()) {
264 * Fork off the utmp helper.
266 if (pipe(pipefd) < 0) {
267 perror("pterm: pipe");
272 perror("pterm: fork");
274 } else if (pid == 0) {
275 char display[128], buffer[128];
280 * Now sit here until we receive a display name from the
281 * other end of the pipe, and then stamp utmp. Unstamp utmp
282 * again, and exit, when the pipe closes.
288 ret = read(pipefd[0], buffer, lenof(buffer));
292 } else if (!pty_stamped_utmp) {
293 if (dlen < lenof(display))
294 memcpy(display+dlen, buffer,
295 min(ret, lenof(display)-dlen));
296 if (buffer[ret-1] == '\0') {
298 * Now we have a display name. NUL-terminate
299 * it, and stamp utmp.
301 display[lenof(display)-1] = '\0';
303 * Trap as many fatal signals as we can in the
304 * hope of having the best possible chance to
305 * clean up utmp before termination. We are
306 * unfortunately unprotected against SIGKILL,
309 signal(SIGHUP, fatal_sig_handler);
310 signal(SIGINT, fatal_sig_handler);
311 signal(SIGQUIT, fatal_sig_handler);
312 signal(SIGILL, fatal_sig_handler);
313 signal(SIGABRT, fatal_sig_handler);
314 signal(SIGFPE, fatal_sig_handler);
315 signal(SIGPIPE, fatal_sig_handler);
316 signal(SIGALRM, fatal_sig_handler);
317 signal(SIGTERM, fatal_sig_handler);
318 signal(SIGSEGV, fatal_sig_handler);
319 signal(SIGUSR1, fatal_sig_handler);
320 signal(SIGUSR2, fatal_sig_handler);
322 signal(SIGBUS, fatal_sig_handler);
325 signal(SIGPOLL, fatal_sig_handler);
328 signal(SIGPROF, fatal_sig_handler);
331 signal(SIGSYS, fatal_sig_handler);
334 signal(SIGTRAP, fatal_sig_handler);
337 signal(SIGVTALRM, fatal_sig_handler);
340 signal(SIGXCPU, fatal_sig_handler);
343 signal(SIGXFSZ, fatal_sig_handler);
346 signal(SIGIO, fatal_sig_handler);
348 /* Also clean up utmp on normal exit. */
349 atexit(cleanup_utmp);
350 setup_utmp(pty_name, display);
356 pty_utmp_helper_pid = pid;
357 pty_utmp_helper_pipe = pipefd[1];
358 signal(SIGCHLD, sigchld_handler);
364 int gid = getgid(), uid = getuid();
365 #ifndef HAVE_NO_SETRESUID
366 int setresgid(gid_t, gid_t, gid_t);
367 int setresuid(uid_t, uid_t, uid_t);
368 setresgid(gid, gid, gid);
369 setresuid(uid, uid, uid);
378 * Called to set up the pty.
380 * Returns an error message, or NULL on success.
382 * Also places the canonical host name into `realhost'. It must be
383 * freed by the caller.
385 static char *pty_init(void *frontend, void **backend_handle,
386 char *host, int port, char **realhost, int nodelay)
391 *backend_handle = NULL; /* we can't sensibly use this, sadly */
393 pty_term_width = cfg.width;
394 pty_term_height = cfg.height;
396 if (pty_master_fd < 0)
400 * Set the backspace character to be whichever of ^H and ^? is
401 * specified by bksp_is_delete.
404 struct termios attrs;
405 tcgetattr(pty_master_fd, &attrs);
406 attrs.c_cc[VERASE] = cfg.bksp_is_delete ? '\177' : '\010';
407 tcsetattr(pty_master_fd, TCSANOW, &attrs);
411 * Stamp utmp (that is, tell the utmp helper process to do so),
415 close(pty_utmp_helper_pipe); /* just let the child process die */
417 char *location = get_x_display();
418 int len = strlen(location)+1, pos = 0; /* +1 to include NUL */
420 int ret = write(pty_utmp_helper_pipe, location+pos, len - pos);
422 perror("pterm: writing to utmp helper process");
423 close(pty_utmp_helper_pipe); /* arrgh, just give up */
431 * Fork and execute the command.
445 slavefd = open(pty_name, O_RDWR);
447 perror("slave pty: open");
451 close(pty_master_fd);
452 fcntl(slavefd, F_SETFD, 0); /* don't close on exec */
457 ioctl(slavefd, TIOCSCTTY, 1);
459 tcsetpgrp(slavefd, pgrp);
461 close(open(pty_name, O_WRONLY, 0));
463 /* Close everything _else_, for tidiness. */
464 for (i = 3; i < 1024; i++)
467 char term_env_var[10 + sizeof(cfg.termtype)];
468 sprintf(term_env_var, "TERM=%s", cfg.termtype);
469 putenv(term_env_var);
472 * SIGINT and SIGQUIT may have been set to ignored by our
473 * parent, particularly by things like sh -c 'pterm &' and
474 * some window managers. Reverse this for our child process.
476 signal(SIGINT, SIG_DFL);
477 signal(SIGQUIT, SIG_DFL);
479 execvp(pty_argv[0], pty_argv);
481 char *shell = getenv("SHELL");
483 if (cfg.login_shell) {
484 char *p = strrchr(shell, '/');
485 shellname = smalloc(2+strlen(shell));
487 sprintf(shellname, "-%s", p);
490 execl(getenv("SHELL"), shellname, NULL);
494 * If we're here, exec has gone badly foom.
501 pty_child_dead = FALSE;
502 signal(SIGCHLD, sigchld_handler);
509 * Called to send data down the pty.
511 static int pty_send(void *handle, char *buf, int len)
513 if (pty_master_fd < 0)
514 return 0; /* ignore all writes if fd closed */
517 int ret = write(pty_master_fd, buf, len);
519 perror("write pty master");
530 if (pty_master_fd >= 0) {
531 close(pty_master_fd);
534 close(pty_utmp_helper_pipe); /* this causes utmp to be cleaned up */
538 * Called to query the current socket sendability status.
540 static int pty_sendbuffer(void *handle)
546 * Called to set the size of the window
548 static void pty_size(void *handle, int width, int height)
552 pty_term_width = width;
553 pty_term_height = height;
555 size.ws_row = (unsigned short)pty_term_height;
556 size.ws_col = (unsigned short)pty_term_width;
557 size.ws_xpixel = (unsigned short) pty_term_width * font_dimension(0);
558 size.ws_ypixel = (unsigned short) pty_term_height * font_dimension(1);
559 ioctl(pty_master_fd, TIOCSWINSZ, (void *)&size);
564 * Send special codes.
566 static void pty_special(void *handle, Telnet_Special code)
572 static Socket pty_socket(void *handle)
574 return NULL; /* shouldn't ever be needed */
577 static int pty_sendok(void *handle)
582 static void pty_unthrottle(void *handle, int backlog)
587 static int pty_ldisc(void *handle, int option)
589 return 0; /* neither editing nor echoing */
592 static int pty_exitcode(void *handle)
595 return -1; /* not dead yet */
597 return pty_exit_code;
600 Backend pty_backend = {