pam_unix.c revision 1.14.6.1 1 /* $NetBSD: pam_unix.c,v 1.14.6.1 2014/05/22 11:36:59 yamt Exp $ */
2
3 /*-
4 * Copyright 1998 Juniper Networks, Inc.
5 * All rights reserved.
6 * Copyright (c) 2002-2003 Networks Associates Technology, Inc.
7 * All rights reserved.
8 *
9 * Portions of this software was developed for the FreeBSD Project by
10 * ThinkSec AS and NAI Labs, the Security Research Division of Network
11 * Associates, Inc. under DARPA/SPAWAR contract N66001-01-C-8035
12 * ("CBOSS"), as part of the DARPA CHATS research program.
13 *
14 * Redistribution and use in source and binary forms, with or without
15 * modification, are permitted provided that the following conditions
16 * are met:
17 * 1. Redistributions of source code must retain the above copyright
18 * notice, this list of conditions and the following disclaimer.
19 * 2. Redistributions in binary form must reproduce the above copyright
20 * notice, this list of conditions and the following disclaimer in the
21 * documentation and/or other materials provided with the distribution.
22 * 3. The name of the author may not be used to endorse or promote
23 * products derived from this software without specific prior written
24 * permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * SUCH DAMAGE.
37 */
38
39 #include <sys/cdefs.h>
40 #ifdef __FreeBSD__
41 __FBSDID("$FreeBSD: src/lib/libpam/modules/pam_unix/pam_unix.c,v 1.49 2004/02/10 10:13:21 des Exp $");
42 #else
43 __RCSID("$NetBSD: pam_unix.c,v 1.14.6.1 2014/05/22 11:36:59 yamt Exp $");
44 #endif
45
46
47 #include <sys/types.h>
48
49 #include <ctype.h>
50 #include <errno.h>
51 #include <fcntl.h>
52 #include <pwd.h>
53 #include <grp.h>
54 #include <limits.h>
55 #include <stdlib.h>
56 #include <string.h>
57 #include <stdio.h>
58 #include <login_cap.h>
59 #include <time.h>
60 #include <tzfile.h>
61 #include <unistd.h>
62
63 #include <util.h>
64
65 #ifdef YP
66 #include <rpc/rpc.h>
67 #include <rpcsvc/ypclnt.h>
68 #include <rpcsvc/yppasswd.h>
69 #endif
70
71 #define PAM_SM_AUTH
72 #define PAM_SM_ACCOUNT
73 #define PAM_SM_PASSWORD
74
75 #include <security/pam_appl.h>
76 #include <security/pam_modules.h>
77 #include <security/pam_mod_misc.h>
78
79 /*
80 * authentication management
81 */
82 PAM_EXTERN int
83 /*ARGSUSED*/
84 pam_sm_authenticate(pam_handle_t *pamh, int flags __unused,
85 int argc __unused, const char *argv[] __unused)
86 {
87 login_cap_t *lc;
88 struct passwd *pwd, pwres;
89 int retval;
90 const char *pass, *user, *realpw;
91 char pwbuf[1024];
92
93 pwd = NULL;
94 if (openpam_get_option(pamh, PAM_OPT_AUTH_AS_SELF)) {
95 (void) getpwnam_r(getlogin(), &pwres, pwbuf, sizeof(pwbuf),
96 &pwd);
97 } else {
98 retval = pam_get_user(pamh, &user, NULL);
99 if (retval != PAM_SUCCESS)
100 return (retval);
101 PAM_LOG("Got user: %s", user);
102 (void) getpwnam_r(user, &pwres, pwbuf, sizeof(pwbuf), &pwd);
103 }
104
105 if (pwd != NULL) {
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);
112 realpw = "*";
113 }
114 } else {
115 PAM_LOG("Doing dummy authentication");
116 realpw = "*";
117 }
118 lc = login_getpwclass(pwd);
119 retval = pam_get_authtok(pamh, PAM_AUTHTOK, &pass, NULL);
120 login_close(lc);
121 if (retval != PAM_SUCCESS)
122 return (retval);
123 PAM_LOG("Got password");
124 if (strcmp(crypt(pass, realpw), realpw) == 0)
125 return (PAM_SUCCESS);
126
127 PAM_VERBOSE_ERROR("UNIX authentication refused");
128 return (PAM_AUTH_ERR);
129 }
130
131 PAM_EXTERN int
132 /*ARGSUSED*/
133 pam_sm_setcred(pam_handle_t *pamh __unused, int flags __unused,
134 int argc __unused, const char *argv[] __unused)
135 {
136
137 return (PAM_SUCCESS);
138 }
139
140 /*
141 * account management
142 */
143 PAM_EXTERN int
144 /*ARGSUSED*/
145 pam_sm_acct_mgmt(pam_handle_t *pamh, int flags __unused,
146 int argc __unused, const char *argv[] __unused)
147 {
148 struct passwd *pwd, pwres;
149 struct timeval now;
150 login_cap_t *lc;
151 time_t warntime;
152 int retval;
153 const char *user;
154 char pwbuf[1024];
155
156 retval = pam_get_user(pamh, &user, NULL);
157 if (retval != PAM_SUCCESS)
158 return (retval);
159
160 if (user == NULL ||
161 getpwnam_r(user, &pwres, pwbuf, sizeof(pwbuf), &pwd) != 0 ||
162 pwd == NULL)
163 return (PAM_SERVICE_ERR);
164
165 PAM_LOG("Got user: %s", user);
166
167 if (*pwd->pw_passwd == '\0' &&
168 (flags & PAM_DISALLOW_NULL_AUTHTOK) != 0)
169 return (PAM_NEW_AUTHTOK_REQD);
170
171 lc = login_getpwclass(pwd);
172 if (lc == NULL) {
173 PAM_LOG("Unable to get login class for user %s", user);
174 return (PAM_SERVICE_ERR);
175 }
176
177 PAM_LOG("Got login_cap");
178
179 if (pwd->pw_change || pwd->pw_expire)
180 (void) gettimeofday(&now, NULL);
181
182 warntime = (time_t)login_getcaptime(lc, "password-warn",
183 (quad_t)(_PASSWORD_WARNDAYS * SECSPERDAY),
184 (quad_t)(_PASSWORD_WARNDAYS * SECSPERDAY));
185
186 /*
187 * Check pw_expire before pw_change - no point in letting the
188 * user change the password on an expired account.
189 */
190
191 if (pwd->pw_expire) {
192 if (now.tv_sec >= pwd->pw_expire) {
193 login_close(lc);
194 return (PAM_ACCT_EXPIRED);
195 } else if (pwd->pw_expire - now.tv_sec < warntime &&
196 (flags & PAM_SILENT) == 0) {
197 pam_error(pamh, "Warning: your account expires on %s",
198 ctime(&pwd->pw_expire));
199 }
200 }
201
202 if (pwd->pw_change) {
203 /* XXX How to handle _PASSWORD_CHGNOW? --thorpej */
204 if (now.tv_sec >= pwd->pw_change) {
205 login_close(lc);
206 return (PAM_NEW_AUTHTOK_REQD);
207 } else if (pwd->pw_change - now.tv_sec < warntime &&
208 (flags & PAM_SILENT) == 0) {
209 pam_error(pamh, "Warning: your password expires on %s",
210 ctime(&pwd->pw_change));
211 }
212 }
213
214 login_close(lc);
215
216 return (PAM_SUCCESS);
217 }
218
219 #ifdef YP
220 /*
221 * yp_check_user:
222 *
223 * Helper function; check that a user exists in the NIS
224 * password map.
225 */
226 static int
227 yp_check_user(const char *domain, const char *user)
228 {
229 char *val;
230 int reason, vallen;
231
232 val = NULL;
233 reason = yp_match(domain, "passwd.byname", user, (int)strlen(user),
234 &val, &vallen);
235 if (reason != 0) {
236 if (val != NULL)
237 free(val);
238 return (0);
239 }
240 free(val);
241 return (1);
242 }
243
244 static int
245 /*ARGSUSED*/
246 yp_set_password(pam_handle_t *pamh, struct passwd *opwd,
247 struct passwd *pwd, const char *old_pass, const char *domain)
248 {
249 char *master;
250 int r, rpcport, status;
251 struct yppasswd yppwd;
252 CLIENT *client;
253 uid_t uid;
254 int retval = PAM_SERVICE_ERR;
255 struct timeval tv;
256
257 /*
258 * Find the master for the passwd map; it should be running
259 * rpc.yppasswdd.
260 */
261 if ((r = yp_master(domain, "passwd.byname", &master)) != 0) {
262 pam_error(pamh, "Can't find master NIS server. Reason: %s",
263 yperr_string(r));
264 return (PAM_SERVICE_ERR);
265 }
266
267 /*
268 * Ask the portmapper for the port of rpc.yppasswdd.
269 */
270 if ((rpcport = getrpcport(master, YPPASSWDPROG,
271 YPPASSWDPROC_UPDATE, IPPROTO_UDP)) == 0) {
272 pam_error(pamh,
273 "Master NIS server not runing yppasswd daemon.\n\t"
274 "Can't change NIS password.");
275 return (PAM_SERVICE_ERR);
276 }
277
278 /*
279 * Be sure the port is privileged.
280 */
281 if (rpcport >= IPPORT_RESERVED) {
282 pam_error(pamh, "yppasswd daemon is on an invalid port.");
283 return (PAM_SERVICE_ERR);
284 }
285
286 uid = getuid();
287 if (uid != 0 && uid != pwd->pw_uid) {
288 pam_error(pamh, "You may only change your own password: %s",
289 strerror(EACCES));
290 return (PAM_SERVICE_ERR);
291 }
292
293 /*
294 * Fill in the yppasswd structure for yppasswdd.
295 */
296 memset(&yppwd, 0, sizeof(yppwd));
297 yppwd.oldpass = strdup(old_pass);
298 if ((yppwd.newpw.pw_passwd = strdup(pwd->pw_passwd)) == NULL)
299 goto malloc_failure;
300 if ((yppwd.newpw.pw_name = strdup(pwd->pw_name)) == NULL)
301 goto malloc_failure;
302 yppwd.newpw.pw_uid = (int)pwd->pw_uid;
303 yppwd.newpw.pw_gid = (int)pwd->pw_gid;
304 if ((yppwd.newpw.pw_gecos = strdup(pwd->pw_gecos)) == NULL)
305 goto malloc_failure;
306 if ((yppwd.newpw.pw_dir = strdup(pwd->pw_dir)) == NULL)
307 goto malloc_failure;
308 if ((yppwd.newpw.pw_shell = strdup(pwd->pw_shell)) == NULL)
309 goto malloc_failure;
310
311 client = clnt_create(master, YPPASSWDPROG, YPPASSWDVERS, "udp");
312 if (client == NULL) {
313 pam_error(pamh, "Can't contact yppasswdd on %s: Reason: %s",
314 master, yperr_string(YPERR_YPBIND));
315 goto out;
316 }
317
318 client->cl_auth = authunix_create_default();
319 tv.tv_sec = 2;
320 tv.tv_usec = 0;
321 r = clnt_call(client, YPPASSWDPROC_UPDATE,
322 xdr_yppasswd, &yppwd, xdr_int, &status, tv);
323 if (r)
324 pam_error(pamh, "RPC to yppasswdd failed.");
325 else if (status)
326 pam_error(pamh, "Couldn't change NIS password.");
327 else {
328 pam_info(pamh, "The NIS password has been changed on %s, "
329 "the master NIS passwd server.", master);
330 retval = PAM_SUCCESS;
331 }
332
333 out:
334 if (yppwd.oldpass != NULL)
335 free(yppwd.oldpass);
336 if (yppwd.newpw.pw_passwd != NULL)
337 free(yppwd.newpw.pw_passwd);
338 if (yppwd.newpw.pw_name != NULL)
339 free(yppwd.newpw.pw_name);
340 if (yppwd.newpw.pw_gecos != NULL)
341 free(yppwd.newpw.pw_gecos);
342 if (yppwd.newpw.pw_dir != NULL)
343 free(yppwd.newpw.pw_dir);
344 if (yppwd.newpw.pw_shell != NULL)
345 free(yppwd.newpw.pw_shell);
346 return (retval);
347
348 malloc_failure:
349 pam_error(pamh, "memory allocation failure");
350 goto out;
351 }
352 #endif /* YP */
353
354 static int
355 local_set_password(pam_handle_t *pamh, struct passwd *opwd,
356 struct passwd *pwd)
357 {
358 char errbuf[200];
359 int tfd, pfd;
360
361 pw_init();
362 tfd = pw_lock(0);
363 if (tfd < 0) {
364 pam_error(pamh, "The password file is busy, waiting...");
365 tfd = pw_lock(10);
366 if (tfd < 0) {
367 pam_error(pamh, "The password file is still busy, "
368 "try again later.");
369 return (PAM_SERVICE_ERR);
370 }
371 }
372
373 pfd = open(_PATH_MASTERPASSWD, O_RDONLY, 0);
374 if (pfd < 0) {
375 pam_error(pamh, "%s: %s", _PATH_MASTERPASSWD, strerror(errno));
376 pw_abort();
377 return (PAM_SERVICE_ERR);
378 }
379
380 if (pw_copyx(pfd, tfd, pwd, opwd, errbuf, sizeof(errbuf)) == 0) {
381 pam_error(pamh, "Unable to update password entry: %s",
382 errbuf);
383 pw_abort();
384 return (PAM_SERVICE_ERR);
385 }
386
387 if (pw_mkdb(pwd->pw_name, opwd->pw_change == pwd->pw_change) < 0) {
388 pam_error(pamh, "Unable to rebuild local password database.");
389 pw_abort();
390 return (PAM_SERVICE_ERR);
391 }
392
393 return (PAM_SUCCESS);
394 }
395
396 /*
397 * password management
398 *
399 * standard Unix and NIS password changing
400 */
401 PAM_EXTERN int
402 /*ARGSUSED*/
403 pam_sm_chauthtok(pam_handle_t *pamh, int flags,
404 int argc __unused, const char *argv[] __unused)
405 {
406 struct passwd *pwd, new_pwd, old_pwd;
407 login_cap_t *lc;
408 const char *user, *passwd_db, *new_pass, *old_pass, *p;
409 int retval, tries, min_pw_len = 0, pw_expiry = 0;
410 char salt[_PASSWORD_LEN+1];
411 char old_pwbuf[1024];
412 #ifdef YP
413 char *domain;
414 int r;
415 #endif
416
417 pwd = NULL;
418 if (openpam_get_option(pamh, PAM_OPT_AUTH_AS_SELF)) {
419 if ((user = getlogin()) == NULL) {
420 pam_error(pamh, "Unable to determine user.");
421 return (PAM_SERVICE_ERR);
422 }
423 (void) getpwnam_r(user, &old_pwd, old_pwbuf,
424 sizeof(old_pwbuf), &pwd);
425 } else {
426 retval = pam_get_user(pamh, &user, NULL);
427 if (retval != PAM_SUCCESS)
428 return (retval);
429 (void) getpwnam_r(user, &old_pwd, old_pwbuf,
430 sizeof(old_pwbuf), &pwd);
431 }
432
433 if (pwd == NULL)
434 return (PAM_AUTHTOK_RECOVERY_ERR);
435
436 PAM_LOG("Got user: %s", user);
437
438 /*
439 * Determine which password type we're going to change, and
440 * remember it.
441 *
442 * NOTE: domain does not need to be freed; its storage is
443 * allocated statically in libc.
444 */
445 passwd_db = openpam_get_option(pamh, "passwd_db");
446 if (passwd_db == NULL) {
447 #ifdef YP
448 /* Prefer YP, if configured. */
449 if (_yp_check(NULL)) {
450 /* If _yp_check() succeeded, then this must. */
451 if ((r = yp_get_default_domain(&domain)) != 0) {
452 pam_error(pamh,
453 "Unable to get NIS domain, reason: %s",
454 yperr_string(r));
455 return (PAM_SERVICE_ERR);
456 }
457 if (yp_check_user(domain, user))
458 passwd_db = "nis";
459 }
460 #endif
461 /* Otherwise we always use local files. */
462 if (passwd_db == NULL) {
463 /* XXX Any validation to do here? */
464 passwd_db = "files";
465 }
466
467 if ((retval = openpam_set_option(pamh, "passwd_db",
468 passwd_db)) != PAM_SUCCESS) {
469 return (retval);
470 }
471 } else {
472 /* Check to see if the specified password DB is usable. */
473 #ifdef YP
474 if (strcmp(passwd_db, "nis") == 0) {
475 if (_yp_check(NULL) == 0) {
476 pam_error(pamh, "NIS not in use.");
477 return (PAM_SERVICE_ERR);
478 }
479 if ((r = yp_get_default_domain(&domain)) != 0) {
480 pam_error(pamh,
481 "Unable to get NIS domain, reason: %s",
482 yperr_string(r));
483 return (PAM_SERVICE_ERR);
484 }
485 if (yp_check_user(domain, user) == 0) {
486 pam_error(pamh,
487 "User %s does not exist in NIS.", user);
488 return (PAM_USER_UNKNOWN);
489 }
490 goto known_passwd_db;
491 }
492 #endif
493 if (strcmp(passwd_db, "files") == 0) {
494 /* XXX Any validation to do here? */
495 goto known_passwd_db;
496 }
497 pam_error(pamh, "Unknown Unix password DB: %s", passwd_db);
498 return (PAM_SERVICE_ERR);
499 }
500 known_passwd_db:
501
502 if (flags & PAM_PRELIM_CHECK) {
503 PAM_LOG("PRELIM round");
504
505 if (strcmp(passwd_db, "files") == 0) {
506 if (getuid() == 0) {
507 /* Root doesn't need the old password. */
508 return (pam_set_item(pamh, PAM_OLDAUTHTOK, ""));
509 }
510 /*
511 * Apparently we're not root, so let's forbid editing
512 * root.
513 * XXX Check for some flag to indicate if this
514 * XXX is the desired behavior.
515 */
516 if (pwd->pw_uid == 0)
517 return (PAM_PERM_DENIED);
518 }
519
520 if (pwd->pw_passwd[0] == '\0') {
521 /*
522 * No password case.
523 * XXX Are we giviing too much away by not prompting
524 * XXX for a password?
525 * XXX Check PAM_DISALLOW_NULL_AUTHTOK
526 */
527 return (pam_set_item(pamh, PAM_OLDAUTHTOK, ""));
528 } else {
529 retval = pam_get_authtok(pamh, PAM_OLDAUTHTOK,
530 &old_pass, NULL);
531 if (retval != PAM_SUCCESS)
532 return (retval);
533 if (strcmp(crypt(old_pass, pwd->pw_passwd),
534 pwd->pw_passwd) != 0)
535 return (PAM_PERM_DENIED);
536 return (PAM_SUCCESS);
537 }
538 }
539
540 if (flags & PAM_UPDATE_AUTHTOK) {
541 char option[LINE_MAX], *key, *opt;
542
543 PAM_LOG("UPDATE round");
544
545 if ((lc = login_getpwclass(pwd)) != NULL) {
546 min_pw_len = (int) login_getcapnum(lc,
547 "minpasswordlen", (quad_t)0, (quad_t)0);
548 pw_expiry = (int) login_getcapnum(lc,
549 "passwordtime", (quad_t)0, (quad_t)0);
550 login_close(lc);
551 }
552
553 retval = pam_get_authtok(pamh, PAM_OLDAUTHTOK, &old_pass, NULL);
554 if (retval != PAM_SUCCESS)
555 return (retval);
556
557 /* Get the new password. */
558 for (tries = 0;;) {
559 retval = pam_get_authtok(pamh, PAM_AUTHTOK, &new_pass,
560 NULL);
561 if (retval == PAM_TRY_AGAIN) {
562 pam_error(pamh,
563 "Mismatch; try again, EOF to quit.");
564 continue;
565 }
566 if (retval != PAM_SUCCESS) {
567 PAM_VERBOSE_ERROR("Unable to get new password");
568 return (retval);
569 }
570 /* Successfully got new password. */
571 if (new_pass[0] == '\0') {
572 pam_info(pamh, "Password unchanged.");
573 return (PAM_SUCCESS);
574 }
575 if (min_pw_len > 0 && strlen(new_pass) < (size_t)min_pw_len) {
576 pam_error(pamh, "Password is too short.");
577 goto retry;
578 }
579 if (strlen(new_pass) <= 5 && ++tries < 2) {
580 pam_error(pamh,
581 "Please enter a longer password.");
582 goto retry;
583 }
584 for (p = new_pass; *p && islower((unsigned char)*p); ++p);
585 if (!*p && ++tries < 2) {
586 pam_error(pamh,
587 "Please don't use an all-lower case "
588 "password.\nUnusual capitalization, "
589 "control characters or digits are "
590 "suggested.");
591 goto retry;
592 }
593 /* Password is OK. */
594 break;
595 retry:
596 pam_set_item(pamh, PAM_AUTHTOK, NULL);
597 }
598 pw_getpwconf(option, sizeof(option), pwd,
599 #ifdef YP
600 strcmp(passwd_db, "nis") == 0 ? "ypcipher" :
601 #endif
602 "localcipher");
603 opt = option;
604 key = strsep(&opt, ",");
605
606 if (pw_gensalt(salt, _PASSWORD_LEN, key, opt) == -1) {
607 pam_error(pamh, "Couldn't generate salt.");
608 return (PAM_SERVICE_ERR);
609 }
610
611 new_pwd = old_pwd;
612 pwd = &new_pwd;
613 pwd->pw_passwd = crypt(new_pass, salt);
614 pwd->pw_change = pw_expiry ? pw_expiry + time(NULL) : 0;
615
616 retval = PAM_SERVICE_ERR;
617 if (strcmp(passwd_db, "files") == 0)
618 retval = local_set_password(pamh, &old_pwd, pwd);
619 #ifdef YP
620 if (strcmp(passwd_db, "nis") == 0)
621 retval = yp_set_password(pamh, &old_pwd, pwd, old_pass,
622 domain);
623 #endif
624 return (retval);
625 }
626
627 PAM_LOG("Illegal flags argument");
628 return (PAM_ABORT);
629 }
630
631 PAM_MODULE_ENTRY("pam_unix");
632