diff options
Diffstat (limited to 'term-utils/agetty.c')
-rw-r--r-- | term-utils/agetty.c | 149 |
1 files changed, 78 insertions, 71 deletions
diff --git a/term-utils/agetty.c b/term-utils/agetty.c index cf87255..0fc6f15 100644 --- a/term-utils/agetty.c +++ b/term-utils/agetty.c @@ -48,6 +48,8 @@ #include "ttyutils.h" #include "color-names.h" #include "env.h" +#include "path.h" +#include "fileutils.h" #include "logindefs.h" @@ -73,35 +75,16 @@ # endif #endif +#ifdef USE_SYSTEMD +# include <systemd/sd-daemon.h> +# include <systemd/sd-login.h> +#endif + #ifdef __linux__ # include <sys/kd.h> # define USE_SYSLOG -# ifndef DEFAULT_VCTERM -# define DEFAULT_VCTERM "linux" -# endif -# if defined (__s390__) || defined (__s390x__) -# define DEFAULT_TTYS0 "dumb" -# define DEFAULT_TTY32 "ibm327x" -# define DEFAULT_TTYS1 "vt220" -# endif -# ifndef DEFAULT_STERM -# define DEFAULT_STERM "vt102" -# endif #elif defined(__GNU__) # define USE_SYSLOG -# ifndef DEFAULT_VCTERM -# define DEFAULT_VCTERM "hurd" -# endif -# ifndef DEFAULT_STERM -# define DEFAULT_STERM "vt102" -# endif -#else -# ifndef DEFAULT_VCTERM -# define DEFAULT_VCTERM "vt100" -# endif -# ifndef DEFAULT_STERM -# define DEFAULT_STERM "vt100" -# endif #endif #ifdef __FreeBSD_kernel__ @@ -139,7 +122,6 @@ # define ISSUE_SUPPORT # if defined(HAVE_SCANDIRAT) && defined(HAVE_OPENAT) # include <dirent.h> -# include "fileutils.h" # define ISSUEDIR_SUPPORT # define ISSUEDIR_EXT ".issue" # define ISSUEDIR_EXTSIZ (sizeof(ISSUEDIR_EXT) - 1) @@ -353,6 +335,7 @@ static void reload_agettys(void); static void print_issue_file(struct issue *ie, struct options *op, struct termios *tp); static void eval_issue_file(struct issue *ie, struct options *op, struct termios *tp); static void show_issue(struct options *op); +static void load_credentials(struct options *op); /* Fake hostname for ut_host specified on command line. */ @@ -413,6 +396,9 @@ int main(int argc, char **argv) } #endif /* DEBUGGING */ + /* Load systemd credentials. */ + load_credentials(&options); + /* Parse command-line arguments. */ parse_args(argc, argv, &options); @@ -575,7 +561,6 @@ int main(int argc, char **argv) log_warn(_("%s: can't change process priority: %m"), options.tty); - free(options.osrelease); #ifdef DEBUGGING if (close_stream(dbf) != 0) log_err("write failed: %s", DEBUG_OUTPUT); @@ -583,6 +568,10 @@ int main(int argc, char **argv) /* Let the login program take care of password validation. */ execv(options.login, login_argv); + + free(options.osrelease); + free(options.autolog); + log_err(_("%s: can't exec %s: %m"), options.tty, login_argv[0]); } @@ -779,7 +768,10 @@ static void parse_args(int argc, char **argv, struct options *op) op->flags |= F_EIGHTBITS; break; case 'a': - op->autolog = optarg; + free(op->autolog); + op->autolog = strdup(optarg); + if (!op->autolog) + log_err(_("failed to allocate memory: %m")); break; case 'c': op->flags |= F_KEEPCFLAGS; @@ -1182,35 +1174,6 @@ static void open_tty(const char *tty, struct termios *tp, struct options *op) if (tcgetattr(STDIN_FILENO, tp) < 0) log_err(_("%s: failed to get terminal attributes: %m"), tty); -#ifdef HAVE_GETTTYNAM - if (!op->term) { - struct ttyent *ent = getttynam(tty); - /* a bit nasty as it's never freed */ - if (ent && ent->ty_type) { - op->term = strdup(ent->ty_type); - if (!op->term) - log_err(_("failed to allocate memory: %m")); - } - } -#endif - -#if defined (__s390__) || defined (__s390x__) - if (!op->term) { - /* - * Special terminal on first serial line on a S/390(x) which - * is due legacy reasons a block terminal of type 3270 or - * higher. Whereas the second serial line on a S/390(x) is - * a real character terminal which is compatible with VT220. - */ - if (strcmp(op->tty, "ttyS0") == 0) /* linux/drivers/s390/char/con3215.c */ - op->term = DEFAULT_TTYS0; - else if (strncmp(op->tty, "3270/tty", 8) == 0) /* linux/drivers/s390/char/con3270.c */ - op->term = DEFAULT_TTY32; - else if (strcmp(op->tty, "ttyS1") == 0) /* linux/drivers/s390/char/sclp_vt220.c */ - op->term = DEFAULT_TTYS1; - } -#endif - #if defined(__FreeBSD_kernel__) login_tty (0); #endif @@ -1227,16 +1190,16 @@ static void open_tty(const char *tty, struct termios *tp, struct options *op) #endif { op->flags |= F_VCONSOLE; - if (!op->term) - op->term = DEFAULT_VCTERM; } else { #ifdef K_RAW op->kbmode = K_RAW; #endif - if (!op->term) - op->term = DEFAULT_STERM; } + op->term = get_terminal_default_type(op->tty, !(op->flags & F_VCONSOLE)); + if (!op->term) + log_err(_("failed to allocate memory: %m")); + if (setenv("TERM", op->term, 1) != 0) log_err(_("failed to set the %s environment variable"), "TERM"); } @@ -2021,7 +1984,7 @@ static void eval_issue_file(struct issue *ie, /* Fallback @sysconfstaticdir (usually /usr/lib) -- the file is not * required to read the dir */ - issuefile_read(ie, _PATH_SYSCONFSTATICDIR "/" _PATH_ISSUE_FILENAME, op, tp); + issuefile_read(ie, _PATH_SYSCONFSTATICDIR "/" _PATH_ISSUE_FILENAME, op, tp); issuedir_read(ie, _PATH_SYSCONFSTATICDIR "/" _PATH_ISSUE_DIRNAME, op, tp); done: @@ -2524,9 +2487,9 @@ static void __attribute__((__noreturn__)) usage(void) fputs(_(" --nice <number> run login with this priority\n"), out); fputs(_(" --reload reload prompts on running agetty instances\n"), out); fputs(_(" --list-speeds display supported baud rates\n"), out); - printf( " --help %s\n", USAGE_OPTSTR_HELP); - printf( " --version %s\n", USAGE_OPTSTR_VERSION); - printf(USAGE_MAN_TAIL("agetty(8)")); + fprintf(out, " --help %s\n", USAGE_OPTSTR_HELP); + fprintf(out, " --version %s\n", USAGE_OPTSTR_VERSION); + fprintf(out, USAGE_MAN_TAIL("agetty(8)")); exit(EXIT_SUCCESS); } @@ -2556,7 +2519,7 @@ static void dolog(int priority * automatically prepended to the message. If we write directly to * /dev/console, we must prepend the process name ourselves. */ - openlog(program_invocation_short_name, LOG_PID, LOG_AUTHPRIV); + openlog("agetty", LOG_PID, LOG_AUTHPRIV); vsyslog(priority, fmt, ap); closelog(); #else @@ -2864,12 +2827,23 @@ static void output_special_char(struct issue *ie, case 'U': { int users = 0; - struct utmpx *ut; - setutxent(); - while ((ut = getutxent())) - if (ut->ut_type == USER_PROCESS) - users++; - endutxent(); +#ifdef USE_SYSTEMD + if (sd_booted() > 0) { + users = sd_get_sessions(NULL); + if (users < 0) + users = 0; + } else { +#endif + users = 0; + struct utmpx *ut; + setutxent(); + while ((ut = getutxent())) + if (ut->ut_type == USER_PROCESS) + users++; + endutxent(); +#ifdef USE_SYSTEMD + } +#endif if (c == 'U') fprintf(ie->output, P_("%d user", "%d users", users), users); else @@ -3020,3 +2994,36 @@ static void reload_agettys(void) errx(EXIT_FAILURE, _("--reload is unsupported on your system")); #endif } + +static void load_credentials(struct options *op) { + char *env; + DIR *dir; + struct dirent *d; + struct path_cxt *pc; + + env = safe_getenv("CREDENTIALS_DIRECTORY"); + if (!env) + return; + + pc = ul_new_path("%s", env); + if (!pc) { + log_warn(_("failed to initialize path context")); + return; + } + + dir = ul_path_opendir(pc, NULL); + if (!dir) { + log_warn(_("failed to open credentials directory")); + return; + } + + while ((d = xreaddir(dir))) { + char *str; + + if (strcmp(d->d_name, "agetty.autologin") == 0) { + ul_path_read_string(pc, &str, d->d_name); + free(op->autolog); + op->autolog = str; + } + } +} |