summaryrefslogtreecommitdiffstats
path: root/login-utils/auth.c
blob: fdeb12bbcee0c1b3f0fb054f9213e270abfe0d79 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
/*
 *   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 <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"
#include "auth.h"

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;
}

int auth_pam(const char *service_name, uid_t uid, const char *username)
{
	if (uid != 0) {
		pam_handle_t *pamh = NULL;
#ifdef HAVE_SECURITY_PAM_MISC_H
		struct pam_conv conv = { misc_conv, NULL };
#elif defined(HAVE_SECURITY_OPENPAM_H)
		struct pam_conv conv = { openpam_ttyconv, NULL };
#endif
		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;
}