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 int pty_term_width
, pty_term_height
;
63 static sig_atomic_t pty_child_dead
;
64 static int pty_exit_code
;
66 static struct utmp utmp_entry
;
70 static void setup_utmp(char *ttyname
, char *location
)
74 struct lastlog lastlog_entry
;
80 pw
= getpwuid(getuid());
81 memset(&utmp_entry
, 0, sizeof(utmp_entry
));
82 utmp_entry
.ut_type
= USER_PROCESS
;
83 utmp_entry
.ut_pid
= getpid();
84 strncpy(utmp_entry
.ut_line
, ttyname
+5, lenof(utmp_entry
.ut_line
));
85 strncpy(utmp_entry
.ut_id
, ttyname
+8, lenof(utmp_entry
.ut_id
));
86 strncpy(utmp_entry
.ut_user
, pw
->pw_name
, lenof(utmp_entry
.ut_user
));
87 strncpy(utmp_entry
.ut_host
, location
, lenof(utmp_entry
.ut_host
));
88 time(&utmp_entry
.ut_time
);
90 #if defined HAVE_PUTUTLINE
93 pututline(&utmp_entry
);
97 if ((wtmp
= fopen(WTMP_FILE
, "a")) != NULL
) {
98 fwrite(&utmp_entry
, 1, sizeof(utmp_entry
), wtmp
);
103 memset(&lastlog_entry
, 0, sizeof(lastlog_entry
));
104 strncpy(lastlog_entry
.ll_line
, ttyname
+5, lenof(lastlog_entry
.ll_line
));
105 strncpy(lastlog_entry
.ll_host
, location
, lenof(lastlog_entry
.ll_host
));
106 time(&lastlog_entry
.ll_time
);
107 if ((lastlog
= fopen(LASTLOG_FILE
, "r+")) != NULL
) {
108 fseek(lastlog
, sizeof(lastlog_entry
) * getuid(), SEEK_SET
);
109 fwrite(&lastlog_entry
, 1, sizeof(lastlog_entry
), lastlog
);
114 pty_stamped_utmp
= 1;
119 static void cleanup_utmp(void)
124 if (!pty_stamped_utmp
)
127 utmp_entry
.ut_type
= DEAD_PROCESS
;
128 memset(utmp_entry
.ut_user
, 0, lenof(utmp_entry
.ut_user
));
129 time(&utmp_entry
.ut_time
);
131 if ((wtmp
= fopen(WTMP_FILE
, "a")) != NULL
) {
132 fwrite(&utmp_entry
, 1, sizeof(utmp_entry
), wtmp
);
136 memset(utmp_entry
.ut_line
, 0, lenof(utmp_entry
.ut_line
));
137 utmp_entry
.ut_time
= 0;
139 #if defined HAVE_PUTUTLINE
142 pututline(&utmp_entry
);
146 pty_stamped_utmp
= 0; /* ensure we never double-cleanup */
150 static void sigchld_handler(int signum
)
154 pid
= waitpid(-1, &status
, WNOHANG
);
155 if (pid
== pty_child_pid
&& (WIFEXITED(status
) || WIFSIGNALED(status
))) {
156 pty_exit_code
= status
;
157 pty_child_dead
= TRUE
;
161 static void fatal_sig_handler(int signum
)
163 signal(signum
, SIG_DFL
);
169 static void pty_open_master(void)
172 const char chars1
[] = "pqrstuvwxyz";
173 const char chars2
[] = "0123456789abcdef";
175 char master_name
[20];
178 for (p1
= chars1
; *p1
; p1
++)
179 for (p2
= chars2
; *p2
; p2
++) {
180 sprintf(master_name
, "/dev/pty%c%c", *p1
, *p2
);
181 pty_master_fd
= open(master_name
, O_RDWR
);
182 if (pty_master_fd
>= 0) {
183 if (geteuid() == 0 ||
184 access(master_name
, R_OK
| W_OK
) == 0)
186 close(pty_master_fd
);
190 /* If we get here, we couldn't get a tty at all. */
191 fprintf(stderr
, "pterm: unable to open a pseudo-terminal device\n");
195 strcpy(pty_name
, master_name
);
196 pty_name
[5] = 't'; /* /dev/ptyXX -> /dev/ttyXX */
198 /* We need to chown/chmod the /dev/ttyXX device. */
199 gp
= getgrnam("tty");
200 chown(pty_name
, getuid(), gp ? gp
->gr_gid
: -1);
201 chmod(pty_name
, 0600);
203 pty_master_fd
= open("/dev/ptmx", O_RDWR
);
205 if (pty_master_fd
< 0) {
206 perror("/dev/ptmx: open");
210 if (grantpt(pty_master_fd
) < 0) {
215 if (unlockpt(pty_master_fd
) < 0) {
220 pty_name
[FILENAME_MAX
-1] = '\0';
221 strncpy(pty_name
, ptsname(pty_master_fd
), FILENAME_MAX
-1);
226 * Pre-initialisation. This is here to get around the fact that GTK
227 * doesn't like being run in setuid/setgid programs (probably
228 * sensibly). So before we initialise GTK - and therefore before we
229 * even process the command line - we check to see if we're running
230 * set[ug]id. If so, we open our pty master _now_, chown it as
231 * necessary, and drop privileges. We can always close it again
232 * later. If we're potentially going to be doing utmp as well, we
233 * also fork off a utmp helper process and communicate with it by
234 * means of a pipe; the utmp helper will keep privileges in order
235 * to clean up utmp when we exit (i.e. when its end of our pipe
238 void pty_pre_init(void)
245 if (geteuid() != getuid() || getegid() != getgid()) {
251 * Fork off the utmp helper.
253 if (pipe(pipefd
) < 0) {
254 perror("pterm: pipe");
259 perror("pterm: fork");
261 } else if (pid
== 0) {
262 char display
[128], buffer
[128];
267 * Now sit here until we receive a display name from the
268 * other end of the pipe, and then stamp utmp. Unstamp utmp
269 * again, and exit, when the pipe closes.
275 ret
= read(pipefd
[0], buffer
, lenof(buffer
));
279 } else if (!pty_stamped_utmp
) {
280 if (dlen
< lenof(display
))
281 memcpy(display
+dlen
, buffer
,
282 min(ret
, lenof(display
)-dlen
));
283 if (buffer
[ret
-1] == '\0') {
285 * Now we have a display name. NUL-terminate
286 * it, and stamp utmp.
288 display
[lenof(display
)-1] = '\0';
290 * Trap as many fatal signals as we can in the
291 * hope of having the best possible chance to
292 * clean up utmp before termination. We are
293 * unfortunately unprotected against SIGKILL,
296 signal(SIGHUP
, fatal_sig_handler
);
297 signal(SIGINT
, fatal_sig_handler
);
298 signal(SIGQUIT
, fatal_sig_handler
);
299 signal(SIGILL
, fatal_sig_handler
);
300 signal(SIGABRT
, fatal_sig_handler
);
301 signal(SIGFPE
, fatal_sig_handler
);
302 signal(SIGPIPE
, fatal_sig_handler
);
303 signal(SIGALRM
, fatal_sig_handler
);
304 signal(SIGTERM
, fatal_sig_handler
);
305 signal(SIGSEGV
, fatal_sig_handler
);
306 signal(SIGUSR1
, fatal_sig_handler
);
307 signal(SIGUSR2
, fatal_sig_handler
);
309 signal(SIGBUS
, fatal_sig_handler
);
312 signal(SIGPOLL
, fatal_sig_handler
);
315 signal(SIGPROF
, fatal_sig_handler
);
318 signal(SIGSYS
, fatal_sig_handler
);
321 signal(SIGTRAP
, fatal_sig_handler
);
324 signal(SIGVTALRM
, fatal_sig_handler
);
327 signal(SIGXCPU
, fatal_sig_handler
);
330 signal(SIGXFSZ
, fatal_sig_handler
);
333 signal(SIGIO
, fatal_sig_handler
);
335 /* Also clean up utmp on normal exit. */
336 atexit(cleanup_utmp
);
337 setup_utmp(pty_name
, display
);
343 pty_utmp_helper_pid
= pid
;
344 pty_utmp_helper_pipe
= pipefd
[1];
345 signal(SIGCHLD
, sigchld_handler
);
351 int gid
= getgid(), uid
= getuid();
352 #ifndef HAVE_NO_SETRESUID
353 int setresgid(gid_t
, gid_t
, gid_t
);
354 int setresuid(uid_t
, uid_t
, uid_t
);
355 setresgid(gid
, gid
, gid
);
356 setresuid(uid
, uid
, uid
);
365 * Called to set up the pty.
367 * Returns an error message, or NULL on success.
369 * Also places the canonical host name into `realhost'. It must be
370 * freed by the caller.
372 static char *pty_init(void *frontend
,
373 char *host
, int port
, char **realhost
, int nodelay
)
378 pty_term_width
= cfg
.width
;
379 pty_term_height
= cfg
.height
;
381 if (pty_master_fd
< 0)
385 * Set the backspace character to be whichever of ^H and ^? is
386 * specified by bksp_is_delete.
389 struct termios attrs
;
390 tcgetattr(pty_master_fd
, &attrs
);
391 attrs
.c_cc
[VERASE
] = cfg
.bksp_is_delete ?
'\177' : '\010';
392 tcsetattr(pty_master_fd
, TCSANOW
, &attrs
);
396 * Stamp utmp (that is, tell the utmp helper process to do so),
400 close(pty_utmp_helper_pipe
); /* just let the child process die */
402 char *location
= get_x_display();
403 int len
= strlen(location
)+1, pos
= 0; /* +1 to include NUL */
405 int ret
= write(pty_utmp_helper_pipe
, location
+pos
, len
- pos
);
407 perror("pterm: writing to utmp helper process");
408 close(pty_utmp_helper_pipe
); /* arrgh, just give up */
416 * Fork and execute the command.
430 slavefd
= open(pty_name
, O_RDWR
);
432 perror("slave pty: open");
436 close(pty_master_fd
);
437 fcntl(slavefd
, F_SETFD
, 0); /* don't close on exec */
442 ioctl(slavefd
, TIOCSCTTY
, 1);
444 tcsetpgrp(slavefd
, pgrp
);
446 close(open(pty_name
, O_WRONLY
, 0));
448 /* Close everything _else_, for tidiness. */
449 for (i
= 3; i
< 1024; i
++)
452 char term_env_var
[10 + sizeof(cfg
.termtype
)];
453 sprintf(term_env_var
, "TERM=%s", cfg
.termtype
);
454 putenv(term_env_var
);
457 * SIGINT and SIGQUIT may have been set to ignored by our
458 * parent, particularly by things like sh -c 'pterm &' and
459 * some window managers. Reverse this for our child process.
461 signal(SIGINT
, SIG_DFL
);
462 signal(SIGQUIT
, SIG_DFL
);
464 execvp(pty_argv
[0], pty_argv
);
466 char *shell
= getenv("SHELL");
468 if (cfg
.login_shell
) {
469 char *p
= strrchr(shell
, '/');
470 shellname
= smalloc(2+strlen(shell
));
472 sprintf(shellname
, "-%s", p
);
475 execl(getenv("SHELL"), shellname
, NULL
);
479 * If we're here, exec has gone badly foom.
486 pty_child_dead
= FALSE
;
487 signal(SIGCHLD
, sigchld_handler
);
494 * Called to send data down the pty.
496 static int pty_send(char *buf
, int len
)
498 if (pty_master_fd
< 0)
499 return 0; /* ignore all writes if fd closed */
502 int ret
= write(pty_master_fd
, buf
, len
);
504 perror("write pty master");
515 if (pty_master_fd
>= 0) {
516 close(pty_master_fd
);
519 close(pty_utmp_helper_pipe
); /* this causes utmp to be cleaned up */
523 * Called to query the current socket sendability status.
525 static int pty_sendbuffer(void)
531 * Called to set the size of the window
533 static void pty_size(int width
, int height
)
537 pty_term_width
= width
;
538 pty_term_height
= height
;
540 size
.ws_row
= (unsigned short)pty_term_height
;
541 size
.ws_col
= (unsigned short)pty_term_width
;
542 size
.ws_xpixel
= (unsigned short) pty_term_width
* font_dimension(0);
543 size
.ws_ypixel
= (unsigned short) pty_term_height
* font_dimension(1);
544 ioctl(pty_master_fd
, TIOCSWINSZ
, (void *)&size
);
549 * Send special codes.
551 static void pty_special(Telnet_Special code
)
557 static Socket
pty_socket(void)
559 return NULL
; /* shouldn't ever be needed */
562 static int pty_sendok(void)
567 static void pty_unthrottle(int backlog
)
572 static int pty_ldisc(int option
)
574 return 0; /* neither editing nor echoing */
577 static int pty_exitcode(void)
580 return -1; /* not dead yet */
582 return pty_exit_code
;
585 Backend pty_backend
= {