2013-02-07 00:22:19 -06:00
|
|
|
/*
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
*/
|
2017-07-30 05:32:37 -05:00
|
|
|
|
|
|
|
#include <security/pam_appl.h>
|
|
|
|
#ifdef HAVE_SECURITY_PAM_MISC_H
|
|
|
|
# include <security/pam_misc.h>
|
|
|
|
#elif defined(HAVE_SECURITY_OPENPAM_H)
|
|
|
|
# include <security/openpam.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include "c.h"
|
2013-02-07 00:22:19 -06:00
|
|
|
#include "auth.h"
|
2017-07-30 05:32:37 -05:00
|
|
|
|
|
|
|
static int pam_fail_check(pam_handle_t *pamh, int retcode)
|
|
|
|
{
|
|
|
|
if (retcode == PAM_SUCCESS)
|
|
|
|
return 0;
|
|
|
|
warnx("%s", pam_strerror(pamh, retcode));
|
|
|
|
pam_end(pamh, retcode);
|
|
|
|
return 1;
|
|
|
|
}
|
2013-02-07 00:22:19 -06:00
|
|
|
|
2013-02-13 06:44:34 -06:00
|
|
|
int auth_pam(const char *service_name, uid_t uid, const char *username)
|
|
|
|
{
|
2013-02-07 00:22:19 -06:00
|
|
|
if (uid != 0) {
|
|
|
|
pam_handle_t *pamh = NULL;
|
2014-12-09 20:40:31 -06:00
|
|
|
#ifdef HAVE_SECURITY_PAM_MISC_H
|
2013-02-07 00:22:19 -06:00
|
|
|
struct pam_conv conv = { misc_conv, NULL };
|
2014-12-09 20:40:31 -06:00
|
|
|
#elif defined(HAVE_SECURITY_OPENPAM_H)
|
|
|
|
struct pam_conv conv = { openpam_ttyconv, NULL };
|
|
|
|
#endif
|
2013-02-07 00:22:19 -06:00
|
|
|
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;
|
|
|
|
}
|