a685146a0b7748a33d7df03948ed28fdf9f39dae
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 int pty_stamp_utmp
= 1;
59 static int pty_stamped_utmp
= 0;
60 static int pty_child_pid
;
61 static sig_atomic_t pty_child_dead
;
63 static struct utmp utmp_entry
;
67 int pty_child_is_dead(void)
69 return pty_child_dead
;
72 static void pty_size(void);
74 static void setup_utmp(char *ttyname
)
78 struct lastlog lastlog_entry
;
88 pw
= getpwuid(getuid());
89 location
= get_x_display();
90 memset(&utmp_entry
, 0, sizeof(utmp_entry
));
91 utmp_entry
.ut_type
= USER_PROCESS
;
92 utmp_entry
.ut_pid
= getpid();
93 strncpy(utmp_entry
.ut_line
, ttyname
+5, lenof(utmp_entry
.ut_line
));
94 strncpy(utmp_entry
.ut_id
, ttyname
+8, lenof(utmp_entry
.ut_id
));
95 strncpy(utmp_entry
.ut_user
, pw
->pw_name
, lenof(utmp_entry
.ut_user
));
96 strncpy(utmp_entry
.ut_host
, location
, lenof(utmp_entry
.ut_host
));
97 time(&utmp_entry
.ut_time
);
99 #if defined HAVE_PUTUTLINE
102 pututline(&utmp_entry
);
106 if ((wtmp
= fopen(WTMP_FILE
, "a")) != NULL
) {
107 fwrite(&utmp_entry
, 1, sizeof(utmp_entry
), wtmp
);
112 memset(&lastlog_entry
, 0, sizeof(lastlog_entry
));
113 strncpy(lastlog_entry
.ll_line
, ttyname
+5, lenof(lastlog_entry
.ll_line
));
114 strncpy(lastlog_entry
.ll_host
, location
, lenof(lastlog_entry
.ll_host
));
115 time(&lastlog_entry
.ll_time
);
116 if ((lastlog
= fopen(LASTLOG_FILE
, "r+")) != NULL
) {
117 fseek(lastlog
, sizeof(lastlog_entry
) * getuid(), SEEK_SET
);
118 fwrite(&lastlog_entry
, 1, sizeof(lastlog_entry
), lastlog
);
123 pty_stamped_utmp
= 1;
128 static void cleanup_utmp(void)
133 if (!pty_stamp_utmp
|| !pty_stamped_utmp
)
136 utmp_entry
.ut_type
= DEAD_PROCESS
;
137 memset(utmp_entry
.ut_user
, 0, lenof(utmp_entry
.ut_user
));
138 time(&utmp_entry
.ut_time
);
140 if ((wtmp
= fopen(WTMP_FILE
, "a")) != NULL
) {
141 fwrite(&utmp_entry
, 1, sizeof(utmp_entry
), wtmp
);
145 memset(utmp_entry
.ut_line
, 0, lenof(utmp_entry
.ut_line
));
146 utmp_entry
.ut_time
= 0;
148 #if defined HAVE_PUTUTLINE
151 pututline(&utmp_entry
);
155 pty_stamped_utmp
= 0; /* ensure we never double-cleanup */
159 static void sigchld_handler(int signum
)
163 pid
= waitpid(-1, &status
, WNOHANG
);
164 if (pid
== pty_child_pid
&& (WIFEXITED(status
) || WIFSIGNALED(status
)))
165 pty_child_dead
= TRUE
;
168 static void fatal_sig_handler(int signum
)
170 signal(signum
, SIG_DFL
);
177 * Called to set up the pty.
179 * Returns an error message, or NULL on success.
181 * Also places the canonical host name into `realhost'. It must be
182 * freed by the caller.
184 static char *pty_init(char *host
, int port
, char **realhost
, int nodelay
)
187 char name
[FILENAME_MAX
];
192 const char chars1
[] = "pqrstuvwxyz";
193 const char chars2
[] = "0123456789abcdef";
195 char master_name
[20];
197 for (p1
= chars1
; *p1
; p1
++)
198 for (p2
= chars2
; *p2
; p2
++) {
199 sprintf(master_name
, "/dev/pty%c%c", *p1
, *p2
);
200 pty_master_fd
= open(master_name
, O_RDWR
);
201 if (pty_master_fd
>= 0) {
202 if (geteuid() == 0 ||
203 access(master_name
, R_OK
| W_OK
) == 0)
205 close(pty_master_fd
);
209 /* If we get here, we couldn't get a tty at all. */
210 fprintf(stderr
, "pterm: unable to open a pseudo-terminal device\n");
214 strcpy(name
, master_name
);
215 name
[5] = 't'; /* /dev/ptyXX -> /dev/ttyXX */
218 pty_master_fd
= open("/dev/ptmx", O_RDWR
);
220 if (pty_master_fd
< 0) {
221 perror("/dev/ptmx: open");
225 if (grantpt(pty_master_fd
) < 0) {
230 if (unlockpt(pty_master_fd
) < 0) {
235 name
[FILENAME_MAX
-1] = '\0';
236 strncpy(name
, ptsname(pty_master_fd
), FILENAME_MAX
-1);
240 * Trap as many fatal signals as we can in the hope of having
241 * the best chance to clean up utmp before termination.
243 signal(SIGHUP
, fatal_sig_handler
);
244 signal(SIGINT
, fatal_sig_handler
);
245 signal(SIGQUIT
, fatal_sig_handler
);
246 signal(SIGILL
, fatal_sig_handler
);
247 signal(SIGABRT
, fatal_sig_handler
);
248 signal(SIGFPE
, fatal_sig_handler
);
249 signal(SIGPIPE
, fatal_sig_handler
);
250 signal(SIGALRM
, fatal_sig_handler
);
251 signal(SIGTERM
, fatal_sig_handler
);
252 signal(SIGSEGV
, fatal_sig_handler
);
253 signal(SIGUSR1
, fatal_sig_handler
);
254 signal(SIGUSR2
, fatal_sig_handler
);
256 signal(SIGBUS
, fatal_sig_handler
);
259 signal(SIGPOLL
, fatal_sig_handler
);
262 signal(SIGPROF
, fatal_sig_handler
);
265 signal(SIGSYS
, fatal_sig_handler
);
268 signal(SIGTRAP
, fatal_sig_handler
);
271 signal(SIGVTALRM
, fatal_sig_handler
);
274 signal(SIGXCPU
, fatal_sig_handler
);
277 signal(SIGXFSZ
, fatal_sig_handler
);
280 signal(SIGIO
, fatal_sig_handler
);
282 /* Also clean up utmp on normal exit. */
283 atexit(cleanup_utmp
);
287 * Fork and execute the command.
301 slavefd
= open(name
, O_RDWR
);
303 perror("slave pty: open");
308 /* We need to chown/chmod the /dev/ttyXX device. */
310 struct group
*gp
= getgrnam("tty");
311 fchown(slavefd
, getuid(), gp ? gp
->gr_gid
: -1);
312 fchmod(slavefd
, 0600);
316 close(pty_master_fd
);
320 fcntl(slavefd
, F_SETFD
, 0); /* don't close on exec */
325 ioctl(slavefd
, TIOCSCTTY
, 1);
327 tcsetpgrp(slavefd
, pgrp
);
329 close(open(name
, O_WRONLY
, 0));
331 /* In case we were setgid-utmp or setuid-root, drop privs. */
334 /* Close everything _else_, for tidiness. */
335 for (i
= 3; i
< 1024; i
++)
338 char term_env_var
[10 + sizeof(cfg
.termtype
)];
339 sprintf(term_env_var
, "TERM=%s", cfg
.termtype
);
340 putenv(term_env_var
);
343 execvp(pty_argv
[0], pty_argv
);
345 execl(getenv("SHELL"), getenv("SHELL"), NULL
);
347 * If we're here, exec has gone badly foom.
354 pty_child_dead
= FALSE
;
355 signal(SIGCHLD
, sigchld_handler
);
362 * Called to send data down the pty.
364 static int pty_send(char *buf
, int len
)
367 int ret
= write(pty_master_fd
, buf
, len
);
369 perror("write pty master");
379 * Called to query the current socket sendability status.
381 static int pty_sendbuffer(void)
387 * Called to set the size of the window
389 static void pty_size(void)
393 size
.ws_row
= (unsigned short)rows
;
394 size
.ws_col
= (unsigned short)cols
;
395 ioctl(pty_master_fd
, TIOCSWINSZ
, (void *)&size
);
400 * Send special codes.
402 static void pty_special(Telnet_Special code
)
408 static Socket
pty_socket(void)
410 return NULL
; /* shouldn't ever be needed */
413 static int pty_sendok(void)
418 static void pty_unthrottle(int backlog
)
423 static int pty_ldisc(int option
)
425 return 0; /* neither editing nor echoing */
428 static int pty_exitcode(void)
430 /* Shouldn't ever be required */
434 Backend pty_backend
= {