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 * Set the backspace character to be whichever of ^H and ^? is
240 * specified by bksp_is_delete.
243 struct termios attrs
;
244 tcgetattr(pty_master_fd
, &attrs
);
245 attrs
.c_cc
[VERASE
] = cfg
.bksp_is_delete ?
'\177' : '\010';
246 tcsetattr(pty_master_fd
, TCSANOW
, &attrs
);
250 * Trap as many fatal signals as we can in the hope of having
251 * the best chance to clean up utmp before termination.
253 signal(SIGHUP
, fatal_sig_handler
);
254 signal(SIGINT
, fatal_sig_handler
);
255 signal(SIGQUIT
, fatal_sig_handler
);
256 signal(SIGILL
, fatal_sig_handler
);
257 signal(SIGABRT
, fatal_sig_handler
);
258 signal(SIGFPE
, fatal_sig_handler
);
259 signal(SIGPIPE
, fatal_sig_handler
);
260 signal(SIGALRM
, fatal_sig_handler
);
261 signal(SIGTERM
, fatal_sig_handler
);
262 signal(SIGSEGV
, fatal_sig_handler
);
263 signal(SIGUSR1
, fatal_sig_handler
);
264 signal(SIGUSR2
, fatal_sig_handler
);
266 signal(SIGBUS
, fatal_sig_handler
);
269 signal(SIGPOLL
, fatal_sig_handler
);
272 signal(SIGPROF
, fatal_sig_handler
);
275 signal(SIGSYS
, fatal_sig_handler
);
278 signal(SIGTRAP
, fatal_sig_handler
);
281 signal(SIGVTALRM
, fatal_sig_handler
);
284 signal(SIGXCPU
, fatal_sig_handler
);
287 signal(SIGXFSZ
, fatal_sig_handler
);
290 signal(SIGIO
, fatal_sig_handler
);
292 /* Also clean up utmp on normal exit. */
293 atexit(cleanup_utmp
);
297 * Fork and execute the command.
311 slavefd
= open(name
, O_RDWR
);
313 perror("slave pty: open");
318 /* We need to chown/chmod the /dev/ttyXX device. */
320 struct group
*gp
= getgrnam("tty");
321 fchown(slavefd
, getuid(), gp ? gp
->gr_gid
: -1);
322 fchmod(slavefd
, 0600);
326 close(pty_master_fd
);
330 fcntl(slavefd
, F_SETFD
, 0); /* don't close on exec */
335 ioctl(slavefd
, TIOCSCTTY
, 1);
337 tcsetpgrp(slavefd
, pgrp
);
339 close(open(name
, O_WRONLY
, 0));
341 /* In case we were setgid-utmp or setuid-root, drop privs. */
344 /* Close everything _else_, for tidiness. */
345 for (i
= 3; i
< 1024; i
++)
348 char term_env_var
[10 + sizeof(cfg
.termtype
)];
349 sprintf(term_env_var
, "TERM=%s", cfg
.termtype
);
350 putenv(term_env_var
);
353 execvp(pty_argv
[0], pty_argv
);
355 char *shell
= getenv("SHELL");
357 if (cfg
.login_shell
) {
358 char *p
= strrchr(shell
, '/');
359 shellname
= smalloc(2+strlen(shell
));
361 sprintf(shellname
, "-%s", p
);
364 execl(getenv("SHELL"), shellname
, NULL
);
368 * If we're here, exec has gone badly foom.
375 pty_child_dead
= FALSE
;
376 signal(SIGCHLD
, sigchld_handler
);
383 * Called to send data down the pty.
385 static int pty_send(char *buf
, int len
)
388 int ret
= write(pty_master_fd
, buf
, len
);
390 perror("write pty master");
400 * Called to query the current socket sendability status.
402 static int pty_sendbuffer(void)
408 * Called to set the size of the window
410 static void pty_size(void)
414 size
.ws_row
= (unsigned short)rows
;
415 size
.ws_col
= (unsigned short)cols
;
416 size
.ws_xpixel
= (unsigned short) cols
* font_dimension(0);
417 size
.ws_ypixel
= (unsigned short) rows
* font_dimension(1);
418 ioctl(pty_master_fd
, TIOCSWINSZ
, (void *)&size
);
423 * Send special codes.
425 static void pty_special(Telnet_Special code
)
431 static Socket
pty_socket(void)
433 return NULL
; /* shouldn't ever be needed */
436 static int pty_sendok(void)
441 static void pty_unthrottle(int backlog
)
446 static int pty_ldisc(int option
)
448 return 0; /* neither editing nor echoing */
451 static int pty_exitcode(void)
453 /* Shouldn't ever be required */
457 Backend pty_backend
= {