2 #define _XOPEN_SOURCE_EXTENDED
16 #include <sys/types.h>
18 #include <sys/ioctl.h>
30 #define UTMP_FILE "/var/run/utmp"
33 #define WTMP_FILE "/var/log/wtmp"
37 #define LASTLOG_FILE _PATH_LASTLOG
39 #define LASTLOG_FILE "/var/log/lastlog"
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.
52 #if !defined HAVE_PUTUTLINE
53 #define HAVE_PUTUTLINE
58 static char pty_name[FILENAME_MAX];
59 static int pty_stamped_utmp = 0;
60 static int pty_child_pid;
61 static int pty_utmp_helper_pid, pty_utmp_helper_pipe;
62 static int pty_term_width, pty_term_height;
63 static sig_atomic_t pty_child_dead;
65 static struct utmp utmp_entry;
69 int pty_child_is_dead(void)
71 return pty_child_dead;
74 static void setup_utmp(char *ttyname, char *location)
78 struct lastlog lastlog_entry;
84 pw = getpwuid(getuid());
85 memset(&utmp_entry, 0, sizeof(utmp_entry));
86 utmp_entry.ut_type = USER_PROCESS;
87 utmp_entry.ut_pid = getpid();
88 strncpy(utmp_entry.ut_line, ttyname+5, lenof(utmp_entry.ut_line));
89 strncpy(utmp_entry.ut_id, ttyname+8, lenof(utmp_entry.ut_id));
90 strncpy(utmp_entry.ut_user, pw->pw_name, lenof(utmp_entry.ut_user));
91 strncpy(utmp_entry.ut_host, location, lenof(utmp_entry.ut_host));
92 time(&utmp_entry.ut_time);
94 #if defined HAVE_PUTUTLINE
97 pututline(&utmp_entry);
101 if ((wtmp = fopen(WTMP_FILE, "a")) != NULL) {
102 fwrite(&utmp_entry, 1, sizeof(utmp_entry), wtmp);
107 memset(&lastlog_entry, 0, sizeof(lastlog_entry));
108 strncpy(lastlog_entry.ll_line, ttyname+5, lenof(lastlog_entry.ll_line));
109 strncpy(lastlog_entry.ll_host, location, lenof(lastlog_entry.ll_host));
110 time(&lastlog_entry.ll_time);
111 if ((lastlog = fopen(LASTLOG_FILE, "r+")) != NULL) {
112 fseek(lastlog, sizeof(lastlog_entry) * getuid(), SEEK_SET);
113 fwrite(&lastlog_entry, 1, sizeof(lastlog_entry), lastlog);
118 pty_stamped_utmp = 1;
123 static void cleanup_utmp(void)
128 if (!pty_stamped_utmp)
131 utmp_entry.ut_type = DEAD_PROCESS;
132 memset(utmp_entry.ut_user, 0, lenof(utmp_entry.ut_user));
133 time(&utmp_entry.ut_time);
135 if ((wtmp = fopen(WTMP_FILE, "a")) != NULL) {
136 fwrite(&utmp_entry, 1, sizeof(utmp_entry), wtmp);
140 memset(utmp_entry.ut_line, 0, lenof(utmp_entry.ut_line));
141 utmp_entry.ut_time = 0;
143 #if defined HAVE_PUTUTLINE
146 pututline(&utmp_entry);
150 pty_stamped_utmp = 0; /* ensure we never double-cleanup */
154 static void sigchld_handler(int signum)
158 pid = waitpid(-1, &status, WNOHANG);
159 if (pid == pty_child_pid && (WIFEXITED(status) || WIFSIGNALED(status)))
160 pty_child_dead = TRUE;
163 static void fatal_sig_handler(int signum)
165 signal(signum, SIG_DFL);
171 static void pty_open_master(void)
174 const char chars1[] = "pqrstuvwxyz";
175 const char chars2[] = "0123456789abcdef";
177 char master_name[20];
180 for (p1 = chars1; *p1; p1++)
181 for (p2 = chars2; *p2; p2++) {
182 sprintf(master_name, "/dev/pty%c%c", *p1, *p2);
183 pty_master_fd = open(master_name, O_RDWR);
184 if (pty_master_fd >= 0) {
185 if (geteuid() == 0 ||
186 access(master_name, R_OK | W_OK) == 0)
188 close(pty_master_fd);
192 /* If we get here, we couldn't get a tty at all. */
193 fprintf(stderr, "pterm: unable to open a pseudo-terminal device\n");
197 strcpy(pty_name, master_name);
198 pty_name[5] = 't'; /* /dev/ptyXX -> /dev/ttyXX */
200 /* We need to chown/chmod the /dev/ttyXX device. */
201 gp = getgrnam("tty");
202 chown(pty_name, getuid(), gp ? gp->gr_gid : -1);
203 chmod(pty_name, 0600);
205 pty_master_fd = open("/dev/ptmx", O_RDWR);
207 if (pty_master_fd < 0) {
208 perror("/dev/ptmx: open");
212 if (grantpt(pty_master_fd) < 0) {
217 if (unlockpt(pty_master_fd) < 0) {
222 pty_name[FILENAME_MAX-1] = '\0';
223 strncpy(pty_name, ptsname(pty_master_fd), FILENAME_MAX-1);
228 * Pre-initialisation. This is here to get around the fact that GTK
229 * doesn't like being run in setuid/setgid programs (probably
230 * sensibly). So before we initialise GTK - and therefore before we
231 * even process the command line - we check to see if we're running
232 * set[ug]id. If so, we open our pty master _now_, chown it as
233 * necessary, and drop privileges. We can always close it again
234 * later. If we're potentially going to be doing utmp as well, we
235 * also fork off a utmp helper process and communicate with it by
236 * means of a pipe; the utmp helper will keep privileges in order
237 * to clean up utmp when we exit (i.e. when its end of our pipe
240 void pty_pre_init(void)
247 if (geteuid() != getuid() || getegid() != getgid()) {
253 * Fork off the utmp helper.
255 if (pipe(pipefd) < 0) {
256 perror("pterm: pipe");
261 perror("pterm: fork");
263 } else if (pid == 0) {
264 char display[128], buffer[128];
269 * Now sit here until we receive a display name from the
270 * other end of the pipe, and then stamp utmp. Unstamp utmp
271 * again, and exit, when the pipe closes.
277 ret = read(pipefd[0], buffer, lenof(buffer));
281 } else if (!pty_stamped_utmp) {
282 if (dlen < lenof(display))
283 memcpy(display+dlen, buffer,
284 min(ret, lenof(display)-dlen));
285 if (buffer[ret-1] == '\0') {
287 * Now we have a display name. NUL-terminate
288 * it, and stamp utmp.
290 display[lenof(display)-1] = '\0';
292 * Trap as many fatal signals as we can in the
293 * hope of having the best possible chance to
294 * clean up utmp before termination. We are
295 * unfortunately unprotected against SIGKILL,
298 signal(SIGHUP, fatal_sig_handler);
299 signal(SIGINT, fatal_sig_handler);
300 signal(SIGQUIT, fatal_sig_handler);
301 signal(SIGILL, fatal_sig_handler);
302 signal(SIGABRT, fatal_sig_handler);
303 signal(SIGFPE, fatal_sig_handler);
304 signal(SIGPIPE, fatal_sig_handler);
305 signal(SIGALRM, fatal_sig_handler);
306 signal(SIGTERM, fatal_sig_handler);
307 signal(SIGSEGV, fatal_sig_handler);
308 signal(SIGUSR1, fatal_sig_handler);
309 signal(SIGUSR2, fatal_sig_handler);
311 signal(SIGBUS, fatal_sig_handler);
314 signal(SIGPOLL, fatal_sig_handler);
317 signal(SIGPROF, fatal_sig_handler);
320 signal(SIGSYS, fatal_sig_handler);
323 signal(SIGTRAP, fatal_sig_handler);
326 signal(SIGVTALRM, fatal_sig_handler);
329 signal(SIGXCPU, fatal_sig_handler);
332 signal(SIGXFSZ, fatal_sig_handler);
335 signal(SIGIO, fatal_sig_handler);
337 /* Also clean up utmp on normal exit. */
338 atexit(cleanup_utmp);
339 setup_utmp(pty_name, display);
345 pty_utmp_helper_pid = pid;
346 pty_utmp_helper_pipe = pipefd[1];
347 signal(SIGCHLD, sigchld_handler);
353 int gid = getgid(), uid = getuid();
354 #ifndef HAVE_NO_SETRESUID
355 int setresgid(gid_t, gid_t, gid_t);
356 int setresuid(uid_t, uid_t, uid_t);
357 setresgid(gid, gid, gid);
358 setresuid(uid, uid, uid);
367 * Called to set up the pty.
369 * Returns an error message, or NULL on success.
371 * Also places the canonical host name into `realhost'. It must be
372 * freed by the caller.
374 static char *pty_init(void *frontend,
375 char *host, int port, char **realhost, int nodelay)
380 pty_term_width = cfg.width;
381 pty_term_height = cfg.height;
383 if (pty_master_fd < 0)
387 * Set the backspace character to be whichever of ^H and ^? is
388 * specified by bksp_is_delete.
391 struct termios attrs;
392 tcgetattr(pty_master_fd, &attrs);
393 attrs.c_cc[VERASE] = cfg.bksp_is_delete ? '\177' : '\010';
394 tcsetattr(pty_master_fd, TCSANOW, &attrs);
398 * Stamp utmp (that is, tell the utmp helper process to do so),
402 close(pty_utmp_helper_pipe); /* just let the child process die */
404 char *location = get_x_display();
405 int len = strlen(location)+1, pos = 0; /* +1 to include NUL */
407 int ret = write(pty_utmp_helper_pipe, location+pos, len - pos);
409 perror("pterm: writing to utmp helper process");
410 close(pty_utmp_helper_pipe); /* arrgh, just give up */
418 * Fork and execute the command.
432 slavefd = open(pty_name, O_RDWR);
434 perror("slave pty: open");
438 close(pty_master_fd);
439 fcntl(slavefd, F_SETFD, 0); /* don't close on exec */
444 ioctl(slavefd, TIOCSCTTY, 1);
446 tcsetpgrp(slavefd, pgrp);
448 close(open(pty_name, O_WRONLY, 0));
450 /* Close everything _else_, for tidiness. */
451 for (i = 3; i < 1024; i++)
454 char term_env_var[10 + sizeof(cfg.termtype)];
455 sprintf(term_env_var, "TERM=%s", cfg.termtype);
456 putenv(term_env_var);
459 * SIGINT and SIGQUIT may have been set to ignored by our
460 * parent, particularly by things like sh -c 'pterm &' and
461 * some window managers. Reverse this for our child process.
463 signal(SIGINT, SIG_DFL);
464 signal(SIGQUIT, SIG_DFL);
466 execvp(pty_argv[0], pty_argv);
468 char *shell = getenv("SHELL");
470 if (cfg.login_shell) {
471 char *p = strrchr(shell, '/');
472 shellname = smalloc(2+strlen(shell));
474 sprintf(shellname, "-%s", p);
477 execl(getenv("SHELL"), shellname, NULL);
481 * If we're here, exec has gone badly foom.
488 pty_child_dead = FALSE;
489 signal(SIGCHLD, sigchld_handler);
496 * Called to send data down the pty.
498 static int pty_send(char *buf, int len)
501 int ret = write(pty_master_fd, buf, len);
503 perror("write pty master");
513 * Called to query the current socket sendability status.
515 static int pty_sendbuffer(void)
521 * Called to set the size of the window
523 static void pty_size(int width, int height)
527 pty_term_width = width;
528 pty_term_height = height;
530 size.ws_row = (unsigned short)pty_term_height;
531 size.ws_col = (unsigned short)pty_term_width;
532 size.ws_xpixel = (unsigned short) pty_term_width * font_dimension(0);
533 size.ws_ypixel = (unsigned short) pty_term_height * font_dimension(1);
534 ioctl(pty_master_fd, TIOCSWINSZ, (void *)&size);
539 * Send special codes.
541 static void pty_special(Telnet_Special code)
547 static Socket pty_socket(void)
549 return NULL; /* shouldn't ever be needed */
552 static int pty_sendok(void)
557 static void pty_unthrottle(int backlog)
562 static int pty_ldisc(int option)
564 return 0; /* neither editing nor echoing */
567 static int pty_exitcode(void)
569 /* Shouldn't ever be required */
573 Backend pty_backend = {