2 #define _XOPEN_SOURCE_EXTENDED
16 #include <sys/types.h>
18 #include <sys/ioctl.h>
31 #define UTMP_FILE "/var/run/utmp"
34 #define WTMP_FILE "/var/log/wtmp"
38 #define LASTLOG_FILE _PATH_LASTLOG
40 #define LASTLOG_FILE "/var/log/lastlog"
45 * Set up a default for vaguely sane systems. The idea is that if
46 * OMIT_UTMP is not defined, then at least one of the symbols which
47 * enable particular forms of utmp processing should be, if only so
48 * that a link error can warn you that you should have defined
49 * OMIT_UTMP if you didn't want any. Currently HAVE_PUTUTLINE is
50 * the only such symbol.
53 #if !defined HAVE_PUTUTLINE
54 #define HAVE_PUTUTLINE
59 static char pty_name[FILENAME_MAX];
60 static int pty_stamped_utmp = 0;
61 static int pty_child_pid;
62 static int pty_utmp_helper_pid, pty_utmp_helper_pipe;
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 pty_size(void);
76 static void setup_utmp(char *ttyname, char *location)
80 struct lastlog lastlog_entry;
86 pw = getpwuid(getuid());
87 memset(&utmp_entry, 0, sizeof(utmp_entry));
88 utmp_entry.ut_type = USER_PROCESS;
89 utmp_entry.ut_pid = getpid();
90 strncpy(utmp_entry.ut_line, ttyname+5, lenof(utmp_entry.ut_line));
91 strncpy(utmp_entry.ut_id, ttyname+8, lenof(utmp_entry.ut_id));
92 strncpy(utmp_entry.ut_user, pw->pw_name, lenof(utmp_entry.ut_user));
93 strncpy(utmp_entry.ut_host, location, lenof(utmp_entry.ut_host));
94 time(&utmp_entry.ut_time);
96 #if defined HAVE_PUTUTLINE
99 pututline(&utmp_entry);
103 if ((wtmp = fopen(WTMP_FILE, "a")) != NULL) {
104 fwrite(&utmp_entry, 1, sizeof(utmp_entry), wtmp);
109 memset(&lastlog_entry, 0, sizeof(lastlog_entry));
110 strncpy(lastlog_entry.ll_line, ttyname+5, lenof(lastlog_entry.ll_line));
111 strncpy(lastlog_entry.ll_host, location, lenof(lastlog_entry.ll_host));
112 time(&lastlog_entry.ll_time);
113 if ((lastlog = fopen(LASTLOG_FILE, "r+")) != NULL) {
114 fseek(lastlog, sizeof(lastlog_entry) * getuid(), SEEK_SET);
115 fwrite(&lastlog_entry, 1, sizeof(lastlog_entry), lastlog);
120 pty_stamped_utmp = 1;
125 static void cleanup_utmp(void)
130 if (!pty_stamped_utmp)
133 utmp_entry.ut_type = DEAD_PROCESS;
134 memset(utmp_entry.ut_user, 0, lenof(utmp_entry.ut_user));
135 time(&utmp_entry.ut_time);
137 if ((wtmp = fopen(WTMP_FILE, "a")) != NULL) {
138 fwrite(&utmp_entry, 1, sizeof(utmp_entry), wtmp);
142 memset(utmp_entry.ut_line, 0, lenof(utmp_entry.ut_line));
143 utmp_entry.ut_time = 0;
145 #if defined HAVE_PUTUTLINE
148 pututline(&utmp_entry);
152 pty_stamped_utmp = 0; /* ensure we never double-cleanup */
156 static void sigchld_handler(int signum)
160 pid = waitpid(-1, &status, WNOHANG);
161 if (pid == pty_child_pid && (WIFEXITED(status) || WIFSIGNALED(status)))
162 pty_child_dead = TRUE;
165 static void fatal_sig_handler(int signum)
167 signal(signum, SIG_DFL);
173 static void pty_open_master(void)
176 const char chars1[] = "pqrstuvwxyz";
177 const char chars2[] = "0123456789abcdef";
179 char master_name[20];
182 for (p1 = chars1; *p1; p1++)
183 for (p2 = chars2; *p2; p2++) {
184 sprintf(master_name, "/dev/pty%c%c", *p1, *p2);
185 pty_master_fd = open(master_name, O_RDWR);
186 if (pty_master_fd >= 0) {
187 if (geteuid() == 0 ||
188 access(master_name, R_OK | W_OK) == 0)
190 close(pty_master_fd);
194 /* If we get here, we couldn't get a tty at all. */
195 fprintf(stderr, "pterm: unable to open a pseudo-terminal device\n");
199 strcpy(pty_name, master_name);
200 pty_name[5] = 't'; /* /dev/ptyXX -> /dev/ttyXX */
202 /* We need to chown/chmod the /dev/ttyXX device. */
203 gp = getgrnam("tty");
204 chown(pty_name, getuid(), gp ? gp->gr_gid : -1);
205 chmod(pty_name, 0600);
207 pty_master_fd = open("/dev/ptmx", O_RDWR);
209 if (pty_master_fd < 0) {
210 perror("/dev/ptmx: open");
214 if (grantpt(pty_master_fd) < 0) {
219 if (unlockpt(pty_master_fd) < 0) {
224 pty_name[FILENAME_MAX-1] = '\0';
225 strncpy(pty_name, ptsname(pty_master_fd), FILENAME_MAX-1);
230 * Pre-initialisation. This is here to get around the fact that GTK
231 * doesn't like being run in setuid/setgid programs (probably
232 * sensibly). So before we initialise GTK - and therefore before we
233 * even process the command line - we check to see if we're running
234 * set[ug]id. If so, we open our pty master _now_, chown it as
235 * necessary, and drop privileges. We can always close it again
236 * later. If we're potentially going to be doing utmp as well, we
237 * also fork off a utmp helper process and communicate with it by
238 * means of a pipe; the utmp helper will keep privileges in order
239 * to clean up utmp when we exit (i.e. when its end of our pipe
242 void pty_pre_init(void)
249 if (geteuid() != getuid() || getegid() != getgid()) {
255 * Fork off the utmp helper.
257 if (pipe(pipefd) < 0) {
258 perror("pterm: pipe");
263 perror("pterm: fork");
265 } else if (pid == 0) {
266 char display[128], buffer[128];
271 * Now sit here until we receive a display name from the
272 * other end of the pipe, and then stamp utmp. Unstamp utmp
273 * again, and exit, when the pipe closes.
279 ret = read(pipefd[0], buffer, lenof(buffer));
283 } else if (!pty_stamped_utmp) {
284 if (dlen < lenof(display))
285 memcpy(display+dlen, buffer,
286 min(ret, lenof(display)-dlen));
287 if (buffer[ret-1] == '\0') {
289 * Now we have a display name. NUL-terminate
290 * it, and stamp utmp.
292 display[lenof(display)-1] = '\0';
294 * Trap as many fatal signals as we can in the
295 * hope of having the best possible chance to
296 * clean up utmp before termination. We are
297 * unfortunately unprotected against SIGKILL,
300 signal(SIGHUP, fatal_sig_handler);
301 signal(SIGINT, fatal_sig_handler);
302 signal(SIGQUIT, fatal_sig_handler);
303 signal(SIGILL, fatal_sig_handler);
304 signal(SIGABRT, fatal_sig_handler);
305 signal(SIGFPE, fatal_sig_handler);
306 signal(SIGPIPE, fatal_sig_handler);
307 signal(SIGALRM, fatal_sig_handler);
308 signal(SIGTERM, fatal_sig_handler);
309 signal(SIGSEGV, fatal_sig_handler);
310 signal(SIGUSR1, fatal_sig_handler);
311 signal(SIGUSR2, fatal_sig_handler);
313 signal(SIGBUS, fatal_sig_handler);
316 signal(SIGPOLL, fatal_sig_handler);
319 signal(SIGPROF, fatal_sig_handler);
322 signal(SIGSYS, fatal_sig_handler);
325 signal(SIGTRAP, fatal_sig_handler);
328 signal(SIGVTALRM, fatal_sig_handler);
331 signal(SIGXCPU, fatal_sig_handler);
334 signal(SIGXFSZ, fatal_sig_handler);
337 signal(SIGIO, fatal_sig_handler);
339 /* Also clean up utmp on normal exit. */
340 atexit(cleanup_utmp);
341 setup_utmp(pty_name, display);
347 pty_utmp_helper_pid = pid;
348 pty_utmp_helper_pipe = pipefd[1];
349 signal(SIGCHLD, sigchld_handler);
355 int gid = getgid(), uid = getuid();
356 #ifndef HAVE_NO_SETRESUID
357 int setresgid(gid_t, gid_t, gid_t);
358 int setresuid(uid_t, uid_t, uid_t);
359 setresgid(gid, gid, gid);
360 setresuid(uid, uid, uid);
369 * Called to set up the pty.
371 * Returns an error message, or NULL on success.
373 * Also places the canonical host name into `realhost'. It must be
374 * freed by the caller.
376 static char *pty_init(void *frontend,
377 char *host, int port, char **realhost, int nodelay)
382 if (pty_master_fd < 0)
386 * Set the backspace character to be whichever of ^H and ^? is
387 * specified by bksp_is_delete.
390 struct termios attrs;
391 tcgetattr(pty_master_fd, &attrs);
392 attrs.c_cc[VERASE] = cfg.bksp_is_delete ? '\177' : '\010';
393 tcsetattr(pty_master_fd, TCSANOW, &attrs);
397 * Stamp utmp (that is, tell the utmp helper process to do so),
401 close(pty_utmp_helper_pipe); /* just let the child process die */
403 char *location = get_x_display();
404 int len = strlen(location)+1, pos = 0; /* +1 to include NUL */
406 int ret = write(pty_utmp_helper_pipe, location+pos, len - pos);
408 perror("pterm: writing to utmp helper process");
409 close(pty_utmp_helper_pipe); /* arrgh, just give up */
417 * Fork and execute the command.
431 slavefd = open(pty_name, O_RDWR);
433 perror("slave pty: open");
437 close(pty_master_fd);
438 fcntl(slavefd, F_SETFD, 0); /* don't close on exec */
443 ioctl(slavefd, TIOCSCTTY, 1);
445 tcsetpgrp(slavefd, pgrp);
447 close(open(pty_name, O_WRONLY, 0));
449 /* Close everything _else_, for tidiness. */
450 for (i = 3; i < 1024; i++)
453 char term_env_var[10 + sizeof(cfg.termtype)];
454 sprintf(term_env_var, "TERM=%s", cfg.termtype);
455 putenv(term_env_var);
458 * SIGINT and SIGQUIT may have been set to ignored by our
459 * parent, particularly by things like sh -c 'pterm &' and
460 * some window managers. Reverse this for our child process.
462 signal(SIGINT, SIG_DFL);
463 signal(SIGQUIT, SIG_DFL);
465 execvp(pty_argv[0], pty_argv);
467 char *shell = getenv("SHELL");
469 if (cfg.login_shell) {
470 char *p = strrchr(shell, '/');
471 shellname = smalloc(2+strlen(shell));
473 sprintf(shellname, "-%s", p);
476 execl(getenv("SHELL"), shellname, NULL);
480 * If we're here, exec has gone badly foom.
487 pty_child_dead = FALSE;
488 signal(SIGCHLD, sigchld_handler);
495 * Called to send data down the pty.
497 static int pty_send(char *buf, int len)
500 int ret = write(pty_master_fd, buf, len);
502 perror("write pty master");
512 * Called to query the current socket sendability status.
514 static int pty_sendbuffer(void)
520 * Called to set the size of the window
522 static void pty_size(void)
526 size.ws_row = (unsigned short)term->rows;
527 size.ws_col = (unsigned short)term->cols;
528 size.ws_xpixel = (unsigned short) term->cols * font_dimension(0);
529 size.ws_ypixel = (unsigned short) term->rows * font_dimension(1);
530 ioctl(pty_master_fd, TIOCSWINSZ, (void *)&size);
535 * Send special codes.
537 static void pty_special(Telnet_Special code)
543 static Socket pty_socket(void)
545 return NULL; /* shouldn't ever be needed */
548 static int pty_sendok(void)
553 static void pty_unthrottle(int backlog)
558 static int pty_ldisc(int option)
560 return 0; /* neither editing nor echoing */
563 static int pty_exitcode(void)
565 /* Shouldn't ever be required */
569 Backend pty_backend = {