diff --git a/src/local.c b/src/local.c index 0af461b..e484bcf 100644 --- a/src/local.c +++ b/src/local.c @@ -22,20 +22,26 @@ #include "log.h" #include "conf.h" -int pusb_local_login(t_pusb_options *opts, const char *user) +int pusb_local_login(t_pusb_options *opts, const char *user, + const char *from) { - char *from; struct utmp utsearch; struct utmp *utent; int i; log_debug("Checking whether the caller is local or not...\n"); - from = ttyname(STDIN_FILENO); - if (!from) - return (1); + if (!from || !(*from)) + { + from = ttyname(STDIN_FILENO); + if (!from) + { + log_debug("Couldn't retrieve the tty name, aborting.\n"); + return (1); + } + if (!strncmp(from, "/dev/", strlen("/dev/"))) + from += strlen("/dev/"); + } log_debug("Authentication request from tty %s\n", from); - if (!strncmp(from, "/dev/", strlen("/dev/"))) - from += strlen("/dev/"); strncpy(utsearch.ut_line, from, sizeof(utsearch.ut_line)); setutent(); utent = getutline(&utsearch); diff --git a/src/local.h b/src/local.h index ed796ea..87dd32b 100644 --- a/src/local.h +++ b/src/local.h @@ -18,6 +18,7 @@ #ifndef PUSB_LOCAL_H_ # define PUSB_LOCAL_H_ -int pusb_local_login(t_pusb_options *opts, const char *user); +int pusb_local_login(t_pusb_options *opts, const char *user, + const char *tty); #endif /* !PUSB_LOCAL_H_ */ diff --git a/src/pam.c b/src/pam.c index c0b7e9c..d257091 100644 --- a/src/pam.c +++ b/src/pam.c @@ -32,6 +32,7 @@ int pam_sm_authenticate(pam_handle_t *pamh, int flags, t_pusb_options opts; const char *service; const char *user; + const char *tty; char *conf_file = PUSB_CONF_FILE; int retval; @@ -65,7 +66,10 @@ int pam_sm_authenticate(pam_handle_t *pamh, int flags, log_info("pam_usb v.%s\n", PUSB_VERSION); log_info("Authentication request for user \"%s\" (%s)\n", user, service); - if (!pusb_local_login(&opts, user)) + + if (pam_get_item(pamh, PAM_TTY, (const void **)&tty) != PAM_SUCCESS) + tty = NULL; + if (!pusb_local_login(&opts, user, tty)) { log_error("Access denied.\n"); return (PAM_AUTH_ERR); diff --git a/src/pusb_check.c b/src/pusb_check.c index 1a3258b..ad0710d 100644 --- a/src/pusb_check.c +++ b/src/pusb_check.c @@ -52,7 +52,7 @@ static int pusb_check_perform_authentication(t_pusb_options *opts, } log_info("Authentication request for user \"%s\" (%s)\n", user, service); - if (!pusb_local_login(opts, user)) + if (!pusb_local_login(opts, user, NULL)) { log_error("Access denied.\n"); return (0);