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 static int pty_child_pid;
45 static sig_atomic_t pty_child_dead;
47 static struct utmp utmp_entry;
51 int pty_child_is_dead(void)
53 return pty_child_dead;
56 static void pty_size(void);
58 static void setup_utmp(char *ttyname)
62 struct lastlog lastlog_entry;
66 char *location = get_x_display();
69 pw = getpwuid(getuid());
70 memset(&utmp_entry, 0, sizeof(utmp_entry));
71 utmp_entry.ut_type = USER_PROCESS;
72 utmp_entry.ut_pid = getpid();
73 strncpy(utmp_entry.ut_line, ttyname+5, lenof(utmp_entry.ut_line));
74 strncpy(utmp_entry.ut_id, ttyname+8, lenof(utmp_entry.ut_id));
75 strncpy(utmp_entry.ut_user, pw->pw_name, lenof(utmp_entry.ut_user));
76 strncpy(utmp_entry.ut_host, location, lenof(utmp_entry.ut_host));
77 time(&utmp_entry.ut_time);
79 #if defined HAVE_PUTUTLINE
82 pututline(&utmp_entry);
86 if ((wtmp = fopen(WTMP_FILE, "a")) != NULL) {
87 fwrite(&utmp_entry, 1, sizeof(utmp_entry), wtmp);
92 memset(&lastlog_entry, 0, sizeof(lastlog_entry));
93 strncpy(lastlog_entry.ll_line, ttyname+5, lenof(lastlog_entry.ll_line));
94 strncpy(lastlog_entry.ll_host, location, lenof(lastlog_entry.ll_host));
95 time(&lastlog_entry.ll_time);
96 if ((lastlog = fopen(LASTLOG_FILE, "r+")) != NULL) {
97 fseek(lastlog, sizeof(lastlog_entry) * getuid(), SEEK_SET);
98 fwrite(&lastlog_entry, 1, sizeof(lastlog_entry), lastlog);
106 static void cleanup_utmp(void)
111 utmp_entry.ut_type = DEAD_PROCESS;
112 memset(utmp_entry.ut_user, 0, lenof(utmp_entry.ut_user));
113 time(&utmp_entry.ut_time);
115 if ((wtmp = fopen(WTMP_FILE, "a")) != NULL) {
116 fwrite(&utmp_entry, 1, sizeof(utmp_entry), wtmp);
120 memset(utmp_entry.ut_line, 0, lenof(utmp_entry.ut_line));
121 utmp_entry.ut_time = 0;
123 #if defined HAVE_PUTUTLINE
126 pututline(&utmp_entry);
133 static void sigchld_handler(int signum)
137 pid = waitpid(-1, &status, WNOHANG);
138 if (pid == pty_child_pid && (WIFEXITED(status) || WIFSIGNALED(status)))
139 pty_child_dead = TRUE;
142 static void fatal_sig_handler(int signum)
144 signal(signum, SIG_DFL);
151 * Called to set up the pty.
153 * Returns an error message, or NULL on success.
155 * Also places the canonical host name into `realhost'. It must be
156 * freed by the caller.
158 static char *pty_init(char *host, int port, char **realhost, int nodelay)
161 char name[FILENAME_MAX];
166 const char chars1[] = "pqrstuvwxyz";
167 const char chars2[] = "0123456789abcdef";
169 char master_name[20];
171 for (p1 = chars1; *p1; p1++)
172 for (p2 = chars2; *p2; p2++) {
173 sprintf(master_name, "/dev/pty%c%c", *p1, *p2);
174 pty_master_fd = open(master_name, O_RDWR);
175 if (pty_master_fd >= 0) {
176 if (geteuid() == 0 ||
177 access(master_name, R_OK | W_OK) == 0)
179 close(pty_master_fd);
183 /* If we get here, we couldn't get a tty at all. */
184 fprintf(stderr, "pterm: unable to open a pseudo-terminal device\n");
188 strcpy(name, master_name);
189 name[5] = 't'; /* /dev/ptyXX -> /dev/ttyXX */
192 pty_master_fd = open("/dev/ptmx", O_RDWR);
194 if (pty_master_fd < 0) {
195 perror("/dev/ptmx: open");
199 if (grantpt(pty_master_fd) < 0) {
204 if (unlockpt(pty_master_fd) < 0) {
209 name[FILENAME_MAX-1] = '\0';
210 strncpy(name, ptsname(pty_master_fd), FILENAME_MAX-1);
214 * Trap as many fatal signals as we can in the hope of having
215 * the best chance to clean up utmp before termination.
217 signal(SIGHUP, fatal_sig_handler);
218 signal(SIGINT, fatal_sig_handler);
219 signal(SIGQUIT, fatal_sig_handler);
220 signal(SIGILL, fatal_sig_handler);
221 signal(SIGABRT, fatal_sig_handler);
222 signal(SIGFPE, fatal_sig_handler);
223 signal(SIGPIPE, fatal_sig_handler);
224 signal(SIGALRM, fatal_sig_handler);
225 signal(SIGTERM, fatal_sig_handler);
226 signal(SIGSEGV, fatal_sig_handler);
227 signal(SIGUSR1, fatal_sig_handler);
228 signal(SIGUSR2, fatal_sig_handler);
230 signal(SIGBUS, fatal_sig_handler);
233 signal(SIGPOLL, fatal_sig_handler);
236 signal(SIGPROF, fatal_sig_handler);
239 signal(SIGSYS, fatal_sig_handler);
242 signal(SIGTRAP, fatal_sig_handler);
245 signal(SIGVTALRM, fatal_sig_handler);
248 signal(SIGXCPU, fatal_sig_handler);
251 signal(SIGXFSZ, fatal_sig_handler);
254 signal(SIGIO, fatal_sig_handler);
256 /* Also clean up utmp on normal exit. */
257 atexit(cleanup_utmp);
261 * Fork and execute the command.
275 slavefd = open(name, O_RDWR);
277 perror("slave pty: open");
282 /* We need to chown/chmod the /dev/ttyXX device. */
284 struct group *gp = getgrnam("tty");
285 fchown(slavefd, getuid(), gp ? gp->gr_gid : -1);
286 fchmod(slavefd, 0600);
290 close(pty_master_fd);
294 fcntl(slavefd, F_SETFD, 0); /* don't close on exec */
299 ioctl(slavefd, TIOCSCTTY, 1);
301 tcsetpgrp(slavefd, pgrp);
303 close(open(name, O_WRONLY, 0));
305 /* In case we were setgid-utmp or setuid-root, drop privs. */
308 /* Close everything _else_, for tidiness. */
309 for (i = 3; i < 1024; i++)
312 char term_env_var[10 + sizeof(cfg.termtype)];
313 sprintf(term_env_var, "TERM=%s", cfg.termtype);
314 putenv(term_env_var);
317 execvp(pty_argv[0], pty_argv);
319 execl(getenv("SHELL"), getenv("SHELL"), NULL);
321 * If we're here, exec has gone badly foom.
328 pty_child_dead = FALSE;
329 signal(SIGCHLD, sigchld_handler);
336 * Called to send data down the pty.
338 static int pty_send(char *buf, int len)
341 int ret = write(pty_master_fd, buf, len);
343 perror("write pty master");
353 * Called to query the current socket sendability status.
355 static int pty_sendbuffer(void)
361 * Called to set the size of the window
363 static void pty_size(void)
367 size.ws_row = (unsigned short)rows;
368 size.ws_col = (unsigned short)cols;
369 ioctl(pty_master_fd, TIOCSWINSZ, (void *)&size);
374 * Send special codes.
376 static void pty_special(Telnet_Special code)
382 static Socket pty_socket(void)
384 return NULL; /* shouldn't ever be needed */
387 static int pty_sendok(void)
392 static void pty_unthrottle(int backlog)
397 static int pty_ldisc(int option)
399 return 0; /* neither editing nor echoing */
402 static int pty_exitcode(void)
404 /* Shouldn't ever be required */
408 Backend pty_backend = {