diff options
author | Cody Maloney | 2013-02-07 07:22:19 +0100 |
---|---|---|
committer | Karel Zak | 2013-02-13 09:28:33 +0100 |
commit | d91ad6ab3c925ad88c9df80dd78818b5aa2d14df (patch) | |
tree | d6b233aec5d9ab2698115a3e3db4a06c3f257b81 /login-utils/auth.c | |
parent | build-sys: Add flag for enabling/disabling libuser support. (diff) | |
download | kernel-qcow2-util-linux-d91ad6ab3c925ad88c9df80dd78818b5aa2d14df.tar.gz kernel-qcow2-util-linux-d91ad6ab3c925ad88c9df80dd78818b5aa2d14df.tar.xz kernel-qcow2-util-linux-d91ad6ab3c925ad88c9df80dd78818b5aa2d14df.zip |
chsh-chfn: Move pam auth to its own function, factoring out common code
This makes it easier to add support for libuser, which needs the same PAM
authentication. Also removes duplicate code between chsh and chfn.
Signed-off-by: Cody Maloney <cmaloney@theoreticalchaos.com>
Diffstat (limited to 'login-utils/auth.c')
-rw-r--r-- | login-utils/auth.c | 47 |
1 files changed, 47 insertions, 0 deletions
diff --git a/login-utils/auth.c b/login-utils/auth.c new file mode 100644 index 000000000..373bd22c1 --- /dev/null +++ b/login-utils/auth.c @@ -0,0 +1,47 @@ +/* + * auth.c -- PAM authorization code, common between chsh and chfn + * (c) 2012 by Cody Maloney <cmaloney@theoreticalchaos.com> + * + * this program is free software. you can redistribute it and + * modify it under the terms of the gnu general public license. + * there is no warranty. + * + */ + +#include "auth.h" + +#include "pamfail.h" + +int auth_pam(const char *service_name, uid_t uid, const char *username) { +#ifdef REQUIRE_PASSWORD + if (uid != 0) { + pam_handle_t *pamh = NULL; + struct pam_conv conv = { misc_conv, NULL }; + int retcode; + + retcode = pam_start(service_name, username, &conv, &pamh); + if (pam_fail_check(pamh, retcode)) + return FALSE; + + retcode = pam_authenticate(pamh, 0); + if (pam_fail_check(pamh, retcode)) + return FALSE; + + retcode = pam_acct_mgmt(pamh, 0); + if (retcode == PAM_NEW_AUTHTOK_REQD) + retcode = + pam_chauthtok(pamh, PAM_CHANGE_EXPIRED_AUTHTOK); + if (pam_fail_check(pamh, retcode)) + return FALSE; + + retcode = pam_setcred(pamh, 0); + if (pam_fail_check(pamh, retcode)) + return FALSE; + + pam_end(pamh, 0); + /* no need to establish a session; this isn't a + * session-oriented activity... */ + } + return TRUE; +#endif /* REQUIRE_PASSWORD */ +} |