2 * Copyright 1998 Juniper Networks, Inc.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * $FreeBSD: src/lib/libpam/modules/pam_unix/pam_unix.c,v 1.4.2.4 2002/08/02 10:14:18 des Exp $
29 #include <sys/types.h>
31 #include <login_cap.h>
39 #define PAM_SM_ACCOUNT
40 #include <security/pam_modules.h>
42 #include <security/pam_mod_misc.h>
44 #define PASSWORD_PROMPT "Password:"
47 PAM_OPT_AUTH_AS_SELF = PAM_OPT_STD_MAX,
51 static struct opttab other_options[] = {
52 { "auth_as_self", PAM_OPT_AUTH_AS_SELF },
53 { "nullok", PAM_OPT_NULLOK },
58 * authentication management
62 pam_sm_authenticate(pam_handle_t *pamh, int flags, int argc,
67 const char *password, *realpw;
69 struct options options;
71 pam_std_option(&options, other_options, argc, argv);
72 if (pam_test_option(&options, PAM_OPT_AUTH_AS_SELF, NULL)) {
73 pwd = getpwnam(getlogin());
75 retval = pam_get_user(pamh, &user, NULL);
76 if (retval != PAM_SUCCESS)
81 realpw = pwd->pw_passwd;
82 if (realpw[0] == '\0') {
83 if (!(flags & PAM_DISALLOW_NULL_AUTHTOK) &&
84 pam_test_option(&options, PAM_OPT_NULLOK, NULL))
91 if ((retval = pam_get_pass(pamh, &password, PASSWORD_PROMPT,
92 &options)) != PAM_SUCCESS)
94 if (strcmp(crypt(password, realpw), realpw) == 0)
100 pam_sm_setcred(pam_handle_t *pamh, int flags, int argc, const char **argv)
108 * check pw_change and pw_expire fields
111 int pam_sm_acct_mgmt(pam_handle_t *pamh, int flags,
112 int argc, const char **argv)
118 login_cap_t *lc = NULL;
122 retval = pam_get_item(pamh, PAM_USER, (const void **)&user);
123 if (retval != PAM_SUCCESS || user == NULL)
124 /* some implementations return PAM_SUCCESS here */
125 return PAM_USER_UNKNOWN;
127 if ((pw = getpwnam(user)) == NULL)
128 return PAM_USER_UNKNOWN;
130 retval = PAM_SUCCESS;
131 lc = login_getpwclass(pw);
133 if (pw->pw_change || pw->pw_expire)
134 gettimeofday(&tp, NULL);
136 #define DEFAULT_WARN (2L * 7L * 86400L) /* Two weeks */
138 warntime = login_getcaptime(lc, "warnpassword", DEFAULT_WARN,
142 if (tp.tv_sec >= pw->pw_change)
143 /* some implementations return PAM_AUTHTOK_EXPIRED */
144 retval = PAM_NEW_AUTHTOK_REQD;
145 else if (pw->pw_change - tp.tv_sec < warntime) {
146 snprintf(buf, sizeof(buf),
147 "Warning: your password expires on %s",
148 ctime(&pw->pw_change));
149 pam_prompt(pamh, PAM_ERROR_MSG, buf, NULL);
153 warntime = login_getcaptime(lc, "warnexpire", DEFAULT_WARN,
157 if (tp.tv_sec >= pw->pw_expire)
158 retval = PAM_ACCT_EXPIRED;
159 else if (pw->pw_expire - tp.tv_sec < warntime) {
160 snprintf(buf, sizeof(buf),
161 "Warning: your account expires on %s",
162 ctime(&pw->pw_expire));
163 pam_prompt(pamh, PAM_ERROR_MSG, buf, NULL);
171 PAM_MODULE_ENTRY("pam_unix");