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 sig_atomic_t pty_child_dead;
64 static struct utmp utmp_entry;
68 int pty_child_is_dead(void)
70 return pty_child_dead;
73 static void pty_size(void);
75 static void setup_utmp(char *ttyname, char *location)
79 struct lastlog lastlog_entry;
85 pw = getpwuid(getuid());
86 memset(&utmp_entry, 0, sizeof(utmp_entry));
87 utmp_entry.ut_type = USER_PROCESS;
88 utmp_entry.ut_pid = getpid();
89 strncpy(utmp_entry.ut_line, ttyname+5, lenof(utmp_entry.ut_line));
90 strncpy(utmp_entry.ut_id, ttyname+8, lenof(utmp_entry.ut_id));
91 strncpy(utmp_entry.ut_user, pw->pw_name, lenof(utmp_entry.ut_user));
92 strncpy(utmp_entry.ut_host, location, lenof(utmp_entry.ut_host));
93 time(&utmp_entry.ut_time);
95 #if defined HAVE_PUTUTLINE
98 pututline(&utmp_entry);
102 if ((wtmp = fopen(WTMP_FILE, "a")) != NULL) {
103 fwrite(&utmp_entry, 1, sizeof(utmp_entry), wtmp);
108 memset(&lastlog_entry, 0, sizeof(lastlog_entry));
109 strncpy(lastlog_entry.ll_line, ttyname+5, lenof(lastlog_entry.ll_line));
110 strncpy(lastlog_entry.ll_host, location, lenof(lastlog_entry.ll_host));
111 time(&lastlog_entry.ll_time);
112 if ((lastlog = fopen(LASTLOG_FILE, "r+")) != NULL) {
113 fseek(lastlog, sizeof(lastlog_entry) * getuid(), SEEK_SET);
114 fwrite(&lastlog_entry, 1, sizeof(lastlog_entry), lastlog);
119 pty_stamped_utmp = 1;
124 static void cleanup_utmp(void)
129 if (!pty_stamped_utmp)
132 utmp_entry.ut_type = DEAD_PROCESS;
133 memset(utmp_entry.ut_user, 0, lenof(utmp_entry.ut_user));
134 time(&utmp_entry.ut_time);
136 if ((wtmp = fopen(WTMP_FILE, "a")) != NULL) {
137 fwrite(&utmp_entry, 1, sizeof(utmp_entry), wtmp);
141 memset(utmp_entry.ut_line, 0, lenof(utmp_entry.ut_line));
142 utmp_entry.ut_time = 0;
144 #if defined HAVE_PUTUTLINE
147 pututline(&utmp_entry);
151 pty_stamped_utmp = 0; /* ensure we never double-cleanup */
155 static void sigchld_handler(int signum)
159 pid = waitpid(-1, &status, WNOHANG);
160 if (pid == pty_child_pid && (WIFEXITED(status) || WIFSIGNALED(status)))
161 pty_child_dead = TRUE;
164 static void fatal_sig_handler(int signum)
166 signal(signum, SIG_DFL);
172 static void pty_open_master(void)
175 const char chars1[] = "pqrstuvwxyz";
176 const char chars2[] = "0123456789abcdef";
178 char master_name[20];
181 for (p1 = chars1; *p1; p1++)
182 for (p2 = chars2; *p2; p2++) {
183 sprintf(master_name, "/dev/pty%c%c", *p1, *p2);
184 pty_master_fd = open(master_name, O_RDWR);
185 if (pty_master_fd >= 0) {
186 if (geteuid() == 0 ||
187 access(master_name, R_OK | W_OK) == 0)
189 close(pty_master_fd);
193 /* If we get here, we couldn't get a tty at all. */
194 fprintf(stderr, "pterm: unable to open a pseudo-terminal device\n");
198 strcpy(pty_name, master_name);
199 pty_name[5] = 't'; /* /dev/ptyXX -> /dev/ttyXX */
201 /* We need to chown/chmod the /dev/ttyXX device. */
202 gp = getgrnam("tty");
203 chown(pty_name, getuid(), gp ? gp->gr_gid : -1);
204 chmod(pty_name, 0600);
206 pty_master_fd = open("/dev/ptmx", O_RDWR);
208 if (pty_master_fd < 0) {
209 perror("/dev/ptmx: open");
213 if (grantpt(pty_master_fd) < 0) {
218 if (unlockpt(pty_master_fd) < 0) {
223 pty_name[FILENAME_MAX-1] = '\0';
224 strncpy(pty_name, ptsname(pty_master_fd), FILENAME_MAX-1);
229 * Pre-initialisation. This is here to get around the fact that GTK
230 * doesn't like being run in setuid/setgid programs (probably
231 * sensibly). So before we initialise GTK - and therefore before we
232 * even process the command line - we check to see if we're running
233 * set[ug]id. If so, we open our pty master _now_, chown it as
234 * necessary, and drop privileges. We can always close it again
235 * later. If we're potentially going to be doing utmp as well, we
236 * also fork off a utmp helper process and communicate with it by
237 * means of a pipe; the utmp helper will keep privileges in order
238 * to clean up utmp when we exit (i.e. when its end of our pipe
241 void pty_pre_init(void)
248 if (geteuid() != getuid() || getegid() != getgid()) {
254 * Fork off the utmp helper.
256 if (pipe(pipefd) < 0) {
257 perror("pterm: pipe");
262 perror("pterm: fork");
264 } else if (pid == 0) {
265 char display[128], buffer[128];
270 * Now sit here until we receive a display name from the
271 * other end of the pipe, and then stamp utmp. Unstamp utmp
272 * again, and exit, when the pipe closes.
278 ret = read(pipefd[0], buffer, lenof(buffer));
282 } else if (!pty_stamped_utmp) {
283 if (dlen < lenof(display))
284 memcpy(display+dlen, buffer,
285 min(ret, lenof(display)-dlen));
286 if (buffer[ret-1] == '\0') {
288 * Now we have a display name. NUL-terminate
289 * it, and stamp utmp.
291 display[lenof(display)-1] = '\0';
293 * Trap as many fatal signals as we can in the
294 * hope of having the best possible chance to
295 * clean up utmp before termination. We are
296 * unfortunately unprotected against SIGKILL,
299 signal(SIGHUP, fatal_sig_handler);
300 signal(SIGINT, fatal_sig_handler);
301 signal(SIGQUIT, fatal_sig_handler);
302 signal(SIGILL, fatal_sig_handler);
303 signal(SIGABRT, fatal_sig_handler);
304 signal(SIGFPE, fatal_sig_handler);
305 signal(SIGPIPE, fatal_sig_handler);
306 signal(SIGALRM, fatal_sig_handler);
307 signal(SIGTERM, fatal_sig_handler);
308 signal(SIGSEGV, fatal_sig_handler);
309 signal(SIGUSR1, fatal_sig_handler);
310 signal(SIGUSR2, fatal_sig_handler);
312 signal(SIGBUS, fatal_sig_handler);
315 signal(SIGPOLL, fatal_sig_handler);
318 signal(SIGPROF, fatal_sig_handler);
321 signal(SIGSYS, fatal_sig_handler);
324 signal(SIGTRAP, fatal_sig_handler);
327 signal(SIGVTALRM, fatal_sig_handler);
330 signal(SIGXCPU, fatal_sig_handler);
333 signal(SIGXFSZ, fatal_sig_handler);
336 signal(SIGIO, fatal_sig_handler);
338 /* Also clean up utmp on normal exit. */
339 atexit(cleanup_utmp);
340 setup_utmp(pty_name, display);
346 pty_utmp_helper_pid = pid;
347 pty_utmp_helper_pipe = pipefd[1];
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(char *host, int port, char **realhost, int nodelay)
379 if (pty_master_fd < 0)
383 * Set the backspace character to be whichever of ^H and ^? is
384 * specified by bksp_is_delete.
387 struct termios attrs;
388 tcgetattr(pty_master_fd, &attrs);
389 attrs.c_cc[VERASE] = cfg.bksp_is_delete ? '\177' : '\010';
390 tcsetattr(pty_master_fd, TCSANOW, &attrs);
394 * Stamp utmp (that is, tell the utmp helper process to do so),
398 close(pty_utmp_helper_pipe); /* just let the child process die */
400 char *location = get_x_display();
401 int len = strlen(location)+1, pos = 0; /* +1 to include NUL */
403 int ret = write(pty_utmp_helper_pipe, location+pos, len - pos);
405 perror("pterm: writing to utmp helper process");
406 close(pty_utmp_helper_pipe); /* arrgh, just give up */
414 * Fork and execute the command.
428 slavefd = open(pty_name, O_RDWR);
430 perror("slave pty: open");
434 close(pty_master_fd);
435 fcntl(slavefd, F_SETFD, 0); /* don't close on exec */
440 ioctl(slavefd, TIOCSCTTY, 1);
442 tcsetpgrp(slavefd, pgrp);
444 close(open(pty_name, O_WRONLY, 0));
446 /* Close everything _else_, for tidiness. */
447 for (i = 3; i < 1024; i++)
450 char term_env_var[10 + sizeof(cfg.termtype)];
451 sprintf(term_env_var, "TERM=%s", cfg.termtype);
452 putenv(term_env_var);
455 execvp(pty_argv[0], pty_argv);
457 char *shell = getenv("SHELL");
459 if (cfg.login_shell) {
460 char *p = strrchr(shell, '/');
461 shellname = smalloc(2+strlen(shell));
463 sprintf(shellname, "-%s", p);
466 execl(getenv("SHELL"), shellname, NULL);
470 * If we're here, exec has gone badly foom.
477 pty_child_dead = FALSE;
478 signal(SIGCHLD, sigchld_handler);
485 * Called to send data down the pty.
487 static int pty_send(char *buf, int len)
490 int ret = write(pty_master_fd, buf, len);
492 perror("write pty master");
502 * Called to query the current socket sendability status.
504 static int pty_sendbuffer(void)
510 * Called to set the size of the window
512 static void pty_size(void)
516 size.ws_row = (unsigned short)rows;
517 size.ws_col = (unsigned short)cols;
518 size.ws_xpixel = (unsigned short) cols * font_dimension(0);
519 size.ws_ypixel = (unsigned short) rows * font_dimension(1);
520 ioctl(pty_master_fd, TIOCSWINSZ, (void *)&size);
525 * Send special codes.
527 static void pty_special(Telnet_Special code)
533 static Socket pty_socket(void)
535 return NULL; /* shouldn't ever be needed */
538 static int pty_sendok(void)
543 static void pty_unthrottle(int backlog)
548 static int pty_ldisc(int option)
550 return 0; /* neither editing nor echoing */
553 static int pty_exitcode(void)
555 /* Shouldn't ever be required */
559 Backend pty_backend = {