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 int pty_stamped_utmp = 0;
59 static int pty_child_pid;
60 static sig_atomic_t pty_child_dead;
62 static struct utmp utmp_entry;
66 int pty_child_is_dead(void)
68 return pty_child_dead;
71 static void pty_size(void);
73 static void setup_utmp(char *ttyname)
77 struct lastlog lastlog_entry;
87 pw = getpwuid(getuid());
88 location = get_x_display();
89 memset(&utmp_entry, 0, sizeof(utmp_entry));
90 utmp_entry.ut_type = USER_PROCESS;
91 utmp_entry.ut_pid = getpid();
92 strncpy(utmp_entry.ut_line, ttyname+5, lenof(utmp_entry.ut_line));
93 strncpy(utmp_entry.ut_id, ttyname+8, lenof(utmp_entry.ut_id));
94 strncpy(utmp_entry.ut_user, pw->pw_name, lenof(utmp_entry.ut_user));
95 strncpy(utmp_entry.ut_host, location, lenof(utmp_entry.ut_host));
96 time(&utmp_entry.ut_time);
98 #if defined HAVE_PUTUTLINE
101 pututline(&utmp_entry);
105 if ((wtmp = fopen(WTMP_FILE, "a")) != NULL) {
106 fwrite(&utmp_entry, 1, sizeof(utmp_entry), wtmp);
111 memset(&lastlog_entry, 0, sizeof(lastlog_entry));
112 strncpy(lastlog_entry.ll_line, ttyname+5, lenof(lastlog_entry.ll_line));
113 strncpy(lastlog_entry.ll_host, location, lenof(lastlog_entry.ll_host));
114 time(&lastlog_entry.ll_time);
115 if ((lastlog = fopen(LASTLOG_FILE, "r+")) != NULL) {
116 fseek(lastlog, sizeof(lastlog_entry) * getuid(), SEEK_SET);
117 fwrite(&lastlog_entry, 1, sizeof(lastlog_entry), lastlog);
122 pty_stamped_utmp = 1;
127 static void cleanup_utmp(void)
132 if (!cfg.stamp_utmp || !pty_stamped_utmp)
135 utmp_entry.ut_type = DEAD_PROCESS;
136 memset(utmp_entry.ut_user, 0, lenof(utmp_entry.ut_user));
137 time(&utmp_entry.ut_time);
139 if ((wtmp = fopen(WTMP_FILE, "a")) != NULL) {
140 fwrite(&utmp_entry, 1, sizeof(utmp_entry), wtmp);
144 memset(utmp_entry.ut_line, 0, lenof(utmp_entry.ut_line));
145 utmp_entry.ut_time = 0;
147 #if defined HAVE_PUTUTLINE
150 pututline(&utmp_entry);
154 pty_stamped_utmp = 0; /* ensure we never double-cleanup */
158 static void sigchld_handler(int signum)
162 pid = waitpid(-1, &status, WNOHANG);
163 if (pid == pty_child_pid && (WIFEXITED(status) || WIFSIGNALED(status)))
164 pty_child_dead = TRUE;
167 static void fatal_sig_handler(int signum)
169 signal(signum, SIG_DFL);
176 * Called to set up the pty.
178 * Returns an error message, or NULL on success.
180 * Also places the canonical host name into `realhost'. It must be
181 * freed by the caller.
183 static char *pty_init(char *host, int port, char **realhost, int nodelay)
186 char name[FILENAME_MAX];
191 const char chars1[] = "pqrstuvwxyz";
192 const char chars2[] = "0123456789abcdef";
194 char master_name[20];
196 for (p1 = chars1; *p1; p1++)
197 for (p2 = chars2; *p2; p2++) {
198 sprintf(master_name, "/dev/pty%c%c", *p1, *p2);
199 pty_master_fd = open(master_name, O_RDWR);
200 if (pty_master_fd >= 0) {
201 if (geteuid() == 0 ||
202 access(master_name, R_OK | W_OK) == 0)
204 close(pty_master_fd);
208 /* If we get here, we couldn't get a tty at all. */
209 fprintf(stderr, "pterm: unable to open a pseudo-terminal device\n");
213 strcpy(name, master_name);
214 name[5] = 't'; /* /dev/ptyXX -> /dev/ttyXX */
217 pty_master_fd = open("/dev/ptmx", O_RDWR);
219 if (pty_master_fd < 0) {
220 perror("/dev/ptmx: open");
224 if (grantpt(pty_master_fd) < 0) {
229 if (unlockpt(pty_master_fd) < 0) {
234 name[FILENAME_MAX-1] = '\0';
235 strncpy(name, ptsname(pty_master_fd), FILENAME_MAX-1);
239 * Trap as many fatal signals as we can in the hope of having
240 * the best chance to clean up utmp before termination.
242 signal(SIGHUP, fatal_sig_handler);
243 signal(SIGINT, fatal_sig_handler);
244 signal(SIGQUIT, fatal_sig_handler);
245 signal(SIGILL, fatal_sig_handler);
246 signal(SIGABRT, fatal_sig_handler);
247 signal(SIGFPE, fatal_sig_handler);
248 signal(SIGPIPE, fatal_sig_handler);
249 signal(SIGALRM, fatal_sig_handler);
250 signal(SIGTERM, fatal_sig_handler);
251 signal(SIGSEGV, fatal_sig_handler);
252 signal(SIGUSR1, fatal_sig_handler);
253 signal(SIGUSR2, fatal_sig_handler);
255 signal(SIGBUS, fatal_sig_handler);
258 signal(SIGPOLL, fatal_sig_handler);
261 signal(SIGPROF, fatal_sig_handler);
264 signal(SIGSYS, fatal_sig_handler);
267 signal(SIGTRAP, fatal_sig_handler);
270 signal(SIGVTALRM, fatal_sig_handler);
273 signal(SIGXCPU, fatal_sig_handler);
276 signal(SIGXFSZ, fatal_sig_handler);
279 signal(SIGIO, fatal_sig_handler);
281 /* Also clean up utmp on normal exit. */
282 atexit(cleanup_utmp);
286 * Fork and execute the command.
300 slavefd = open(name, O_RDWR);
302 perror("slave pty: open");
307 /* We need to chown/chmod the /dev/ttyXX device. */
309 struct group *gp = getgrnam("tty");
310 fchown(slavefd, getuid(), gp ? gp->gr_gid : -1);
311 fchmod(slavefd, 0600);
315 close(pty_master_fd);
319 fcntl(slavefd, F_SETFD, 0); /* don't close on exec */
324 ioctl(slavefd, TIOCSCTTY, 1);
326 tcsetpgrp(slavefd, pgrp);
328 close(open(name, O_WRONLY, 0));
330 /* In case we were setgid-utmp or setuid-root, drop privs. */
333 /* Close everything _else_, for tidiness. */
334 for (i = 3; i < 1024; i++)
337 char term_env_var[10 + sizeof(cfg.termtype)];
338 sprintf(term_env_var, "TERM=%s", cfg.termtype);
339 putenv(term_env_var);
342 execvp(pty_argv[0], pty_argv);
344 char *shell = getenv("SHELL");
346 if (cfg.login_shell) {
347 char *p = strrchr(shell, '/');
348 shellname = smalloc(2+strlen(shell));
350 sprintf(shellname, "-%s", p);
353 execl(getenv("SHELL"), shellname, NULL);
357 * If we're here, exec has gone badly foom.
364 pty_child_dead = FALSE;
365 signal(SIGCHLD, sigchld_handler);
372 * Called to send data down the pty.
374 static int pty_send(char *buf, int len)
377 int ret = write(pty_master_fd, buf, len);
379 perror("write pty master");
389 * Called to query the current socket sendability status.
391 static int pty_sendbuffer(void)
397 * Called to set the size of the window
399 static void pty_size(void)
403 size.ws_row = (unsigned short)rows;
404 size.ws_col = (unsigned short)cols;
405 ioctl(pty_master_fd, TIOCSWINSZ, (void *)&size);
410 * Send special codes.
412 static void pty_special(Telnet_Special code)
418 static Socket pty_socket(void)
420 return NULL; /* shouldn't ever be needed */
423 static int pty_sendok(void)
428 static void pty_unthrottle(int backlog)
433 static int pty_ldisc(int option)
435 return 0; /* neither editing nor echoing */
438 static int pty_exitcode(void)
440 /* Shouldn't ever be required */
444 Backend pty_backend = {