lib: try to find tty in get_terminal_name()
Try all standard terminal input/output file descriptors when finding tty name in get_germinal_name(). This should make all invocations of the function as robust as they can get. Signed-off-by: Sami Kerola <kerolasa@iki.fi>
This commit is contained in:
parent
a58b90255c
commit
285c1f3a3e
5 changed files with 20 additions and 19 deletions
|
@ -51,7 +51,7 @@ struct chardata {
|
|||
} while (0)
|
||||
|
||||
extern int get_terminal_width(int default_width);
|
||||
extern int get_terminal_name(int fd, const char **path, const char **name,
|
||||
extern int get_terminal_name(const char **path, const char **name,
|
||||
const char **number);
|
||||
|
||||
#define UL_TTY_KEEPCFLAGS (1 << 1)
|
||||
|
|
|
@ -5,6 +5,7 @@
|
|||
* Written by Karel Zak <kzak@redhat.com>
|
||||
*/
|
||||
#include <ctype.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include "c.h"
|
||||
#include "ttyutils.h"
|
||||
|
@ -42,13 +43,14 @@ int get_terminal_width(int default_width)
|
|||
return width > 0 ? width : default_width;
|
||||
}
|
||||
|
||||
int get_terminal_name(int fd,
|
||||
const char **path,
|
||||
int get_terminal_name(const char **path,
|
||||
const char **name,
|
||||
const char **number)
|
||||
{
|
||||
const char *tty;
|
||||
const char *p;
|
||||
int fd;
|
||||
|
||||
|
||||
if (name)
|
||||
*name = NULL;
|
||||
|
@ -57,6 +59,15 @@ int get_terminal_name(int fd,
|
|||
if (number)
|
||||
*number = NULL;
|
||||
|
||||
if (isatty(STDIN_FILENO))
|
||||
fd = STDIN_FILENO;
|
||||
else if (isatty(STDOUT_FILENO))
|
||||
fd = STDOUT_FILENO;
|
||||
else if (isatty(STDERR_FILENO))
|
||||
fd = STDERR_FILENO;
|
||||
else
|
||||
return -1;
|
||||
|
||||
tty = ttyname(fd);
|
||||
if (!tty)
|
||||
return -1;
|
||||
|
|
|
@ -356,7 +356,7 @@ static void init_tty(struct login_context *cxt)
|
|||
|
||||
cxt->tty_mode = (mode_t) getlogindefs_num("TTYPERM", TTY_MODE);
|
||||
|
||||
get_terminal_name(0, &cxt->tty_path, &cxt->tty_name, &cxt->tty_number);
|
||||
get_terminal_name(&cxt->tty_path, &cxt->tty_name, &cxt->tty_number);
|
||||
|
||||
/*
|
||||
* In case login is suid it was possible to use a hardlink as stdin
|
||||
|
|
|
@ -165,7 +165,7 @@ log_syslog(struct passwd const *pw, bool successful)
|
|||
old_user = pwd ? pwd->pw_name : "";
|
||||
}
|
||||
|
||||
if (get_terminal_name(STDERR_FILENO, NULL, &tty, NULL) != 0 || !tty)
|
||||
if (get_terminal_name(NULL, &tty, NULL) != 0 || !tty)
|
||||
tty = "none";
|
||||
|
||||
openlog (program_invocation_short_name, 0 , LOG_AUTH);
|
||||
|
@ -192,7 +192,7 @@ static void log_btmp(struct passwd const *pw)
|
|||
pw && pw->pw_name ? pw->pw_name : "(unknown)",
|
||||
sizeof(ut.ut_user));
|
||||
|
||||
get_terminal_name(STDERR_FILENO, NULL, &tty_name, &tty_num);
|
||||
get_terminal_name(NULL, &tty_name, &tty_num);
|
||||
if (tty_num)
|
||||
xstrncpy(ut.ut_id, tty_num, sizeof(ut.ut_id));
|
||||
if (tty_name)
|
||||
|
|
|
@ -296,7 +296,7 @@ static void do_write(const struct write_control *ctl)
|
|||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
int tty_writeable = 0, src_fd, c;
|
||||
int tty_writeable = 0, c;
|
||||
struct write_control ctl = { 0 };
|
||||
|
||||
static const struct option longopts[] = {
|
||||
|
@ -321,18 +321,8 @@ int main(int argc, char **argv)
|
|||
usage(stderr);
|
||||
}
|
||||
|
||||
if (get_terminal_name(&ctl.src_tty_path, &ctl.src_tty_name, NULL) == 0) {
|
||||
/* check that sender has write enabled */
|
||||
if (isatty(STDIN_FILENO))
|
||||
src_fd = STDIN_FILENO;
|
||||
else if (isatty(STDOUT_FILENO))
|
||||
src_fd = STDOUT_FILENO;
|
||||
else if (isatty(STDERR_FILENO))
|
||||
src_fd = STDERR_FILENO;
|
||||
else
|
||||
src_fd = -1;
|
||||
|
||||
if (src_fd != -1 &&
|
||||
get_terminal_name(src_fd, &ctl.src_tty_path, &ctl.src_tty_name, NULL) == 0) {
|
||||
if (check_tty(ctl.src_tty_path, &tty_writeable, NULL, 1))
|
||||
exit(EXIT_FAILURE);
|
||||
if (!tty_writeable)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue