2 * Copyright 1998 Juniper Networks, Inc.
4 * Copyright (c) 2002-2003 Networks Associates Technology, Inc.
7 * Portions of this software was developed for the FreeBSD Project by
8 * ThinkSec AS and NAI Labs, the Security Research Division of Network
9 * Associates, Inc. under DARPA/SPAWAR contract N66001-01-C-8035
10 * ("CBOSS"), as part of the DARPA CHATS research program.
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 * 3. The name of the author may not be used to endorse or promote
21 * products derived from this software without specific prior written
24 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * $FreeBSD: src/lib/libpam/modules/pam_unix/pam_unix.c,v 1.51 2005/07/05 18:42:18 des Exp $
37 * $DragonFly: src/lib/pam_module/pam_unix/pam_unix.c,v 1.1 2005/08/01 16:15:19 joerg Exp $
40 #include <sys/param.h>
41 #include <sys/socket.h>
43 #include <netinet/in.h>
44 #include <arpa/inet.h>
46 #include <login_cap.h>
62 #define PAM_SM_ACCOUNT
63 #define PAM_SM_PASSWORD
65 #include <security/pam_appl.h>
66 #include <security/pam_modules.h>
67 #include <security/pam_mod_misc.h>
69 #define PASSWORD_HASH "md5"
70 #define DEFAULT_WARN (2L * 7L * 86400L) /* Two weeks */
73 static void makesalt(char []);
75 static char password_hash
[] = PASSWORD_HASH
;
77 #define PAM_OPT_LOCAL_PASS "local_pass"
78 #define PAM_OPT_NIS_PASS "nis_pass"
80 char *tempname
= NULL
;
83 * authentication management
86 pam_sm_authenticate(pam_handle_t
*pamh
, int flags __unused
,
87 int argc __unused
, const char *argv
[] __unused
)
92 const char *pass
, *user
, *realpw
, *prompt
;
94 if (openpam_get_option(pamh
, PAM_OPT_AUTH_AS_SELF
)) {
95 pwd
= getpwnam(getlogin());
97 retval
= pam_get_user(pamh
, &user
, NULL
);
98 if (retval
!= PAM_SUCCESS
)
100 pwd
= getpwnam(user
);
103 PAM_LOG("Got user: %s", user
);
106 PAM_LOG("Doing real authentication");
107 realpw
= pwd
->pw_passwd
;
108 if (realpw
[0] == '\0') {
109 if (!(flags
& PAM_DISALLOW_NULL_AUTHTOK
) &&
110 openpam_get_option(pamh
, PAM_OPT_NULLOK
))
111 return (PAM_SUCCESS
);
114 lc
= login_getpwclass(pwd
);
116 PAM_LOG("Doing dummy authentication");
118 lc
= login_getclass(NULL
);
120 prompt
= login_getcapstr(lc
, "passwd_prompt", NULL
, NULL
);
121 retval
= pam_get_authtok(pamh
, PAM_AUTHTOK
, &pass
, prompt
);
123 if (retval
!= PAM_SUCCESS
)
125 PAM_LOG("Got password");
126 if (strcmp(crypt(pass
, realpw
), realpw
) == 0)
127 return (PAM_SUCCESS
);
129 return (PAM_AUTH_ERR
);
133 pam_sm_setcred(pam_handle_t
*pamh __unused
, int flags __unused
,
134 int argc __unused
, const char *argv
[] __unused
)
137 return (PAM_SUCCESS
);
144 pam_sm_acct_mgmt(pam_handle_t
*pamh
, int flags __unused
,
145 int argc __unused
, const char *argv
[] __unused
)
147 struct addrinfo hints
, *res
;
154 const void *rhost
, *tty
;
155 char rhostip
[MAXHOSTNAMELEN
] = "";
157 retval
= pam_get_user(pamh
, &user
, NULL
);
158 if (retval
!= PAM_SUCCESS
)
161 if (user
== NULL
|| (pwd
= getpwnam(user
)) == NULL
)
162 return (PAM_SERVICE_ERR
);
164 PAM_LOG("Got user: %s", user
);
166 retval
= pam_get_item(pamh
, PAM_RHOST
, &rhost
);
167 if (retval
!= PAM_SUCCESS
)
170 retval
= pam_get_item(pamh
, PAM_TTY
, &tty
);
171 if (retval
!= PAM_SUCCESS
)
174 if (*pwd
->pw_passwd
== '\0' &&
175 (flags
& PAM_DISALLOW_NULL_AUTHTOK
) != 0)
176 return (PAM_NEW_AUTHTOK_REQD
);
178 lc
= login_getpwclass(pwd
);
180 PAM_LOG("Unable to get login class for user %s", user
);
181 return (PAM_SERVICE_ERR
);
184 PAM_LOG("Got login_cap");
186 if (pwd
->pw_change
|| pwd
->pw_expire
)
187 gettimeofday(&tp
, NULL
);
190 * Check pw_expire before pw_change - no point in letting the
191 * user change the password on an expired account.
194 if (pwd
->pw_expire
) {
195 warntime
= login_getcaptime(lc
, "warnexpire",
196 DEFAULT_WARN
, DEFAULT_WARN
);
197 if (tp
.tv_sec
>= pwd
->pw_expire
) {
199 return (PAM_ACCT_EXPIRED
);
200 } else if (pwd
->pw_expire
- tp
.tv_sec
< warntime
&&
201 (flags
& PAM_SILENT
) == 0) {
202 pam_error(pamh
, "Warning: your account expires on %s",
203 ctime(&pwd
->pw_expire
));
207 retval
= PAM_SUCCESS
;
208 if (pwd
->pw_change
) {
209 warntime
= login_getcaptime(lc
, "warnpassword",
210 DEFAULT_WARN
, DEFAULT_WARN
);
211 if (tp
.tv_sec
>= pwd
->pw_change
) {
212 retval
= PAM_NEW_AUTHTOK_REQD
;
213 } else if (pwd
->pw_change
- tp
.tv_sec
< warntime
&&
214 (flags
& PAM_SILENT
) == 0) {
215 pam_error(pamh
, "Warning: your password expires on %s",
216 ctime(&pwd
->pw_change
));
221 * From here on, we must leave retval untouched (unless we
222 * know we're going to fail), because we need to remember
223 * whether we're supposed to return PAM_SUCCESS or
224 * PAM_NEW_AUTHTOK_REQD.
227 if (rhost
&& *(const char *)rhost
!= '\0') {
228 memset(&hints
, 0, sizeof(hints
));
229 hints
.ai_family
= AF_UNSPEC
;
230 if (getaddrinfo(rhost
, NULL
, &hints
, &res
) == 0) {
231 getnameinfo(res
->ai_addr
, res
->ai_addrlen
,
232 rhostip
, sizeof(rhostip
), NULL
, 0,
240 * Check host / tty / time-of-day restrictions
243 if (!auth_hostok(lc
, rhost
, rhostip
) ||
244 !auth_ttyok(lc
, tty
) ||
245 !auth_timeok(lc
, time(NULL
)))
246 retval
= PAM_AUTH_ERR
;
254 * password management
256 * standard Unix and NIS password changing
259 pam_sm_chauthtok(pam_handle_t
*pamh
, int flags
,
260 int argc __unused
, const char *argv
[] __unused
)
263 struct ypclnt
*ypclnt
;
264 void *yp_domain
, *yp_server
;
266 char salt
[SALTSIZE
+ 1];
268 struct passwd
*pwd
, *old_pwd
;
269 const char *user
, *old_pass
, *new_pass
;
271 int pfd
, tfd
, retval
= PAM_ABORT
;
273 if (openpam_get_option(pamh
, PAM_OPT_AUTH_AS_SELF
))
274 pwd
= getpwnam(getlogin());
276 retval
= pam_get_user(pamh
, &user
, NULL
);
277 if (retval
!= PAM_SUCCESS
)
279 pwd
= getpwnam(user
);
283 return (PAM_AUTHTOK_RECOVERY_ERR
);
285 PAM_LOG("Got user: %s", user
);
287 if (flags
& PAM_PRELIM_CHECK
) {
289 PAM_LOG("PRELIM round");
293 && (pwd
->pw_fields
& _PWF_SOURCE
) == _PWF_FILES
296 /* root doesn't need the old password */
297 return (pam_set_item(pamh
, PAM_OLDAUTHTOK
, ""));
301 && (pwd
->pw_fields
& _PWF_SOURCE
) == _PWF_NIS
305 yp_domain
= yp_server
= NULL
;
306 (void)pam_get_data(pamh
, "yp_domain", &yp_domain
);
307 (void)pam_get_data(pamh
, "yp_server", &yp_server
);
309 ypclnt
= ypclnt_new(yp_domain
, "passwd.byname", yp_server
);
311 return (PAM_BUF_ERR
);
313 if (ypclnt_connect(ypclnt
) == -1) {
315 return (PAM_SERVICE_ERR
);
318 retval
= ypclnt_havepasswdd(ypclnt
);
321 return (pam_set_item(pamh
, PAM_OLDAUTHTOK
, ""));
322 else if (retval
== -1)
323 return (PAM_SERVICE_ERR
);
326 if (pwd
->pw_passwd
[0] == '\0'
327 && openpam_get_option(pamh
, PAM_OPT_NULLOK
)) {
329 * No password case. XXX Are we giving too much away
330 * by not prompting for a password?
331 * XXX check PAM_DISALLOW_NULL_AUTHTOK
335 retval
= pam_get_authtok(pamh
,
336 PAM_OLDAUTHTOK
, &old_pass
, NULL
);
337 if (retval
!= PAM_SUCCESS
)
340 PAM_LOG("Got old password");
341 /* always encrypt first */
342 encrypted
= crypt(old_pass
, pwd
->pw_passwd
);
343 if (old_pass
[0] == '\0' &&
344 !openpam_get_option(pamh
, PAM_OPT_NULLOK
))
345 return (PAM_PERM_DENIED
);
346 if (strcmp(encrypted
, pwd
->pw_passwd
) != 0)
347 return (PAM_PERM_DENIED
);
349 else if (flags
& PAM_UPDATE_AUTHTOK
) {
350 PAM_LOG("UPDATE round");
352 retval
= pam_get_authtok(pamh
,
353 PAM_OLDAUTHTOK
, &old_pass
, NULL
);
354 if (retval
!= PAM_SUCCESS
)
356 PAM_LOG("Got old password");
358 /* get new password */
360 retval
= pam_get_authtok(pamh
,
361 PAM_AUTHTOK
, &new_pass
, NULL
);
362 if (retval
!= PAM_TRY_AGAIN
)
364 pam_error(pamh
, "Mismatch; try again, EOF to quit.");
366 PAM_LOG("Got new password");
367 if (retval
!= PAM_SUCCESS
) {
368 PAM_VERBOSE_ERROR("Unable to get new password");
372 if (getuid() != 0 && new_pass
[0] == '\0' &&
373 !openpam_get_option(pamh
, PAM_OPT_NULLOK
))
374 return (PAM_PERM_DENIED
);
376 if ((old_pwd
= pw_dup(pwd
)) == NULL
)
377 return (PAM_BUF_ERR
);
380 lc
= login_getclass(pwd
->pw_class
);
381 if (login_setcryptfmt(lc
, password_hash
, NULL
) == NULL
)
382 openpam_log(PAM_LOG_ERROR
,
383 "can't set password cipher, relying on default");
386 pwd
->pw_passwd
= crypt(new_pass
, salt
);
388 switch (old_pwd
->pw_fields
& _PWF_SOURCE
) {
391 retval
= PAM_SERVICE_ERR
;
392 if (pw_init(NULL
, NULL
))
393 openpam_log(PAM_LOG_ERROR
, "pw_init() failed");
394 else if ((pfd
= pw_lock()) == -1)
395 openpam_log(PAM_LOG_ERROR
, "pw_lock() failed");
396 else if ((tfd
= pw_tmp(-1)) == -1)
397 openpam_log(PAM_LOG_ERROR
, "pw_tmp() failed");
398 else if (pw_copy(pfd
, tfd
, pwd
, old_pwd
) == -1)
399 openpam_log(PAM_LOG_ERROR
, "pw_copy() failed");
400 else if (pw_mkdb(pwd
->pw_name
) == -1)
401 openpam_log(PAM_LOG_ERROR
, "pw_mkdb() failed");
403 retval
= PAM_SUCCESS
;
408 yp_domain
= yp_server
= NULL
;
409 (void)pam_get_data(pamh
, "yp_domain", &yp_domain
);
410 (void)pam_get_data(pamh
, "yp_server", &yp_server
);
411 ypclnt
= ypclnt_new(yp_domain
,
412 "passwd.byname", yp_server
);
413 if (ypclnt
== NULL
) {
414 retval
= PAM_BUF_ERR
;
415 } else if (ypclnt_connect(ypclnt
) == -1 ||
416 ypclnt_passwd(ypclnt
, pwd
, old_pass
) == -1) {
417 openpam_log(PAM_LOG_ERROR
, "%s", ypclnt
->error
);
418 retval
= PAM_SERVICE_ERR
;
420 retval
= PAM_SUCCESS
;
425 openpam_log(PAM_LOG_ERROR
, "unsupported source 0x%x",
426 pwd
->pw_fields
& _PWF_SOURCE
);
427 retval
= PAM_SERVICE_ERR
;
435 PAM_LOG("Illegal 'flags'");
441 /* Mostly stolen from passwd(1)'s local_passwd.c - markm */
443 static unsigned char itoa64
[] = /* 0 ... 63 => ascii - 64 */
444 "./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
447 to64(char *s
, long v
, int n
)
450 *s
++ = itoa64
[v
&0x3f];
455 /* Salt suitable for traditional DES and MD5 */
457 makesalt(char salt
[SALTSIZE
])
461 /* These are not really random numbers, they are just
462 * numbers that change to thwart construction of a
463 * dictionary. This is exposed to the public.
465 for (i
= 0; i
< SALTSIZE
; i
+= 4)
466 to64(&salt
[i
], arc4random(), 4);
467 salt
[SALTSIZE
] = '\0';
470 PAM_MODULE_ENTRY("pam_unix");