4 * Copyright information at end of file.
20 #include <security/_pam_macros.h>
21 #include <security/pam_modules.h>
26 extern char *crypt(const char *key, const char *salt);
27 extern char *bigcrypt(const char *key, const char *salt);
29 /* syslogging function for errors and other information */
31 void _log_err(int err, pam_handle_t *pamh, const char *format,...)
37 pam_get_item(pamh, PAM_SERVICE, (const void **) &service);
39 strncpy(logname, service, sizeof(logname));
40 logname[sizeof(logname) - 1 - strlen("(pam_unix)")] = '\0';
41 strncat(logname, "(pam_unix)", strlen("(pam_unix)"));
43 strncpy(logname, "pam_unix", sizeof(logname) - 1);
46 va_start(args, format);
47 openlog(logname, LOG_CONS | LOG_PID, LOG_AUTH);
48 vsyslog(err, format, args);
53 /* this is a front-end for module-application conversations */
55 static int converse(pam_handle_t * pamh, int ctrl, int nargs
56 ,struct pam_message **message
57 ,struct pam_response **response)
60 struct pam_conv *conv;
62 D(("begin to converse"));
64 retval = pam_get_item(pamh, PAM_CONV, (const void **) &conv);
65 if (retval == PAM_SUCCESS) {
67 retval = conv->conv(nargs, (const struct pam_message **) message
68 ,response, conv->appdata_ptr);
70 D(("returned from application's conversation function"));
72 if (retval != PAM_SUCCESS && on(UNIX_DEBUG, ctrl)) {
73 _log_err(LOG_DEBUG, pamh, "conversation failure [%s]"
74 ,pam_strerror(pamh, retval));
76 } else if (retval != PAM_CONV_AGAIN) {
77 _log_err(LOG_ERR, pamh
78 ,"couldn't obtain coversation function [%s]"
79 ,pam_strerror(pamh, retval));
81 D(("ready to return from module conversation"));
83 return retval; /* propagate error status */
86 int _make_remark(pam_handle_t * pamh, unsigned int ctrl
87 ,int type, const char *text)
89 int retval = PAM_SUCCESS;
91 if (off(UNIX__QUIET, ctrl)) {
92 struct pam_message *pmsg[1], msg[1];
93 struct pam_response *resp;
97 msg[0].msg_style = type;
100 retval = converse(pamh, ctrl, 1, pmsg, &resp);
103 _pam_drop_reply(resp, 1);
110 * Beacause getlogin() is braindead and sometimes it just
111 * doesn't work, we reimplement it here.
113 char *PAM_getlogin(void)
115 struct utmp *ut, line;
116 char *curr_tty, *retval;
117 static char curr_user[sizeof(ut->ut_user) + 4];
121 curr_tty = ttyname(0);
122 if (curr_tty != NULL) {
123 D(("PAM_getlogin ttyname: %s", curr_tty));
126 strncpy(line.ut_line, curr_tty, sizeof(line.ut_line));
127 if ((ut = getutline(&line)) != NULL) {
128 strncpy(curr_user, ut->ut_user, sizeof(ut->ut_user));
129 curr_user[sizeof(curr_user) - 1] = '\0';
134 D(("PAM_getlogin retval: %s", retval));
140 * set the control flags for the UNIX module.
143 int _set_ctrl(pam_handle_t *pamh, int flags, int *remember, int argc,
150 ctrl = UNIX_DEFAULTS; /* the default selection of options */
152 /* set some flags manually */
154 if (getuid() == 0 && !(flags & PAM_CHANGE_EXPIRED_AUTHTOK)) {
156 set(UNIX__IAMROOT, ctrl);
158 if (flags & PAM_UPDATE_AUTHTOK) {
159 D(("UPDATE_AUTHTOK"));
160 set(UNIX__UPDATE, ctrl);
162 if (flags & PAM_PRELIM_CHECK) {
164 set(UNIX__PRELIM, ctrl);
166 if (flags & PAM_DISALLOW_NULL_AUTHTOK) {
167 D(("DISALLOW_NULL_AUTHTOK"));
168 set(UNIX__NONULL, ctrl);
170 if (flags & PAM_SILENT) {
172 set(UNIX__QUIET, ctrl);
174 /* now parse the arguments to this module */
179 D(("pam_unix arg: %s", *argv));
181 for (j = 0; j < UNIX_CTRLS_; ++j) {
182 if (unix_args[j].token
183 && !strncmp(*argv, unix_args[j].token, strlen(unix_args[j].token))) {
188 if (j >= UNIX_CTRLS_) {
189 _log_err(LOG_ERR, pamh,
190 "unrecognized option [%s]", *argv);
192 ctrl &= unix_args[j].mask; /* for turning things off */
193 ctrl |= unix_args[j].flag; /* for turning things on */
195 if (remember != NULL) {
196 if (j == UNIX_REMEMBER_PASSWD) {
197 *remember = strtol(*argv + 9, NULL, 10);
198 if ((*remember == LONG_MIN) || (*remember == LONG_MAX))
206 ++argv; /* step to next argument */
209 /* auditing is a more sensitive version of debug */
211 if (on(UNIX_AUDIT, ctrl)) {
212 set(UNIX_DEBUG, ctrl);
214 /* return the set of flags */
220 static void _cleanup(pam_handle_t * pamh, void *x, int error_status)
225 /* ************************************************************** *
226 * Useful non-trivial functions *
227 * ************************************************************** */
230 * the following is used to keep track of the number of times a user fails
231 * to authenticate themself.
234 #define FAIL_PREFIX "-UN*X-FAIL-"
235 #define UNIX_MAX_RETRIES 3
237 struct _pam_failed_auth {
238 char *user; /* user that's failed to be authenticated */
239 char *name; /* attempt from user with name */
240 int uid; /* uid of calling user */
241 int euid; /* euid of calling process */
242 int count; /* number of failures so far */
245 #ifndef PAM_DATA_REPLACE
246 #error "Need to get an updated libpam 0.52 or better"
249 static void _cleanup_failures(pam_handle_t * pamh, void *fl, int err)
252 const char *service = NULL;
253 const char *ruser = NULL;
254 const char *rhost = NULL;
255 const char *tty = NULL;
256 struct _pam_failed_auth *failure;
260 quiet = err & PAM_DATA_SILENT; /* should we log something? */
261 err &= PAM_DATA_REPLACE; /* are we just replacing data? */
262 failure = (struct _pam_failed_auth *) fl;
264 if (failure != NULL) {
266 if (!quiet && !err) { /* under advisement from Sun,may go away */
268 /* log the number of authentication failures */
269 if (failure->count > 1) {
270 (void) pam_get_item(pamh, PAM_SERVICE,
271 (const void **)&service);
272 (void) pam_get_item(pamh, PAM_RUSER,
273 (const void **)&ruser);
274 (void) pam_get_item(pamh, PAM_RHOST,
275 (const void **)&rhost);
276 (void) pam_get_item(pamh, PAM_TTY,
277 (const void **)&tty);
278 _log_err(LOG_NOTICE, pamh,
279 "%d more authentication failure%s; "
280 "logname=%s uid=%d euid=%d "
281 "tty=%s ruser=%s rhost=%s "
283 failure->count - 1, failure->count == 2 ? "" : "s",
284 failure->name, failure->uid, failure->euid,
285 tty ? tty : "", ruser ? ruser : "",
287 (failure->user && failure->user[0] != '\0')
288 ? " user=" : "", failure->user
291 if (failure->count > UNIX_MAX_RETRIES) {
292 _log_err(LOG_ALERT, pamh
293 ,"service(%s) ignoring max retries; %d > %d"
294 ,service == NULL ? "**unknown**" : service
300 _pam_delete(failure->user); /* tidy up */
301 _pam_delete(failure->name); /* tidy up */
307 * _unix_blankpasswd() is a quick check for a blank password
309 * returns TRUE if user does not have a password
310 * - to avoid prompting for one in such cases (CG)
313 int _unix_blankpasswd(unsigned int ctrl, const char *name)
315 struct passwd *pwd = NULL;
316 struct spwd *spwdent = NULL;
322 struct passwd pwd_buf;
330 * This function does not have to be too smart if something goes
331 * wrong, return FALSE and let this case to be treated somewhere
335 if (on(UNIX__NONULL, ctrl))
336 return 0; /* will fail but don't let on yet */
338 /* UNIX passwords area */
340 /* Get password file entry... */
343 buf = malloc(bufsize);
345 if ((retval = getpwnam_r(name, pwd, buf, bufsize, &pwd))) {
348 while (retval == ERANGE) {
350 buf = realloc(buf, bufsize);
351 if ((retval = getpwnam_r(name, pwd, buf, bufsize, &pwd))) {
356 pwd = getpwnam(name);
360 if (strcmp( pwd->pw_passwd, "*NP*" ) == 0)
362 uid_t save_euid, save_uid;
364 save_euid = geteuid();
366 if (save_uid == pwd->pw_uid)
367 setreuid( save_euid, save_uid );
370 if (setreuid( -1, pwd->pw_uid ) == -1) {
373 if(setreuid( -1, pwd->pw_uid ) == -1)
374 /* Will fail elsewhere. */
383 spwdent = getspnam( name );
384 if (save_uid == pwd->pw_uid)
385 setreuid( save_uid, save_euid );
387 if (setreuid( -1, 0 ) == -1)
388 setreuid( save_uid, -1 );
389 setreuid( -1, save_euid );
391 } else if (strcmp(pwd->pw_passwd, "x") == 0) {
393 * ...and shadow password file entry for this user,
394 * if shadowing is enabled
396 spwdent = getspnam(name);
399 salt = x_strdup(spwdent->sp_pwdp);
401 salt = x_strdup(pwd->pw_passwd);
403 /* Does this user have a password? */
407 if (strlen(salt) == 0)
427 * verify the password of a user
430 #include <sys/types.h>
431 #include <sys/wait.h>
433 static int _unix_run_helper_binary(pam_handle_t *pamh, const char *passwd,
434 unsigned int ctrl, const char *user)
436 int retval, child, fds[2];
439 /* create a pipe for the password */
440 if (pipe(fds) != 0) {
441 D(("could not make pipe"));
448 static char *envp[] = { NULL };
449 char *args[] = { NULL, NULL, NULL };
451 /* XXX - should really tidy up PAM here too */
453 /* reopen stdin as pipe */
455 dup2(fds[0], STDIN_FILENO);
457 /* exec binary helper */
458 args[0] = x_strdup(CHKPWD_HELPER);
459 args[1] = x_strdup(user);
461 execve(CHKPWD_HELPER, args, envp);
463 /* should not get here: exit with error */
464 D(("helper binary is not available"));
465 exit(PAM_AUTHINFO_UNAVAIL);
466 } else if (child > 0) {
468 /* if the stored password is NULL */
469 if (off(UNIX__NONULL, ctrl)) { /* this means we've succeeded */
470 write(fds[1], "nullok\0\0", 8);
472 write(fds[1], "nonull\0\0", 8);
474 if (passwd != NULL) { /* send the password to the child */
475 write(fds[1], passwd, strlen(passwd)+1);
478 write(fds[1], "", 1); /* blank password */
480 close(fds[0]); /* close here to avoid possible SIGPIPE above */
482 (void) waitpid(child, &retval, 0); /* wait for helper to complete */
483 retval = (retval == 0) ? PAM_SUCCESS:PAM_AUTH_ERR;
486 retval = PAM_AUTH_ERR;
489 D(("returning %d", retval));
493 int _unix_verify_password(pam_handle_t * pamh, const char *name
494 ,const char *p, unsigned int ctrl)
496 struct passwd *pwd = NULL;
497 struct spwd *spwdent = NULL;
505 #ifdef HAVE_PAM_FAIL_DELAY
506 if (off(UNIX_NODELAY, ctrl)) {
507 D(("setting delay"));
508 (void) pam_fail_delay(pamh, 2000000); /* 2 sec delay for on failure */
512 /* locate the entry for this user */
514 D(("locating user's record"));
516 /* UNIX passwords area */
517 pwd = getpwnam(name); /* Get password file entry... */
520 if (strcmp( pwd->pw_passwd, "*NP*" ) == 0)
522 uid_t save_euid, save_uid;
524 save_euid = geteuid();
526 if (save_uid == pwd->pw_uid)
527 setreuid( save_euid, save_uid );
530 if (setreuid( -1, pwd->pw_uid ) == -1) {
533 if(setreuid( -1, pwd->pw_uid ) == -1)
534 return PAM_CRED_INSUFFICIENT;
538 spwdent = getspnam( name );
539 if (save_uid == pwd->pw_uid)
540 setreuid( save_uid, save_euid );
542 if (setreuid( -1, 0 ) == -1)
543 setreuid( save_uid, -1 );
544 setreuid( -1, save_euid );
546 } else if (strcmp(pwd->pw_passwd, "x") == 0) {
548 * ...and shadow password file entry for this user,
549 * if shadowing is enabled
551 spwdent = getspnam(name);
554 salt = x_strdup(spwdent->sp_pwdp);
556 salt = x_strdup(pwd->pw_passwd);
559 data_name = (char *) malloc(sizeof(FAIL_PREFIX) + strlen(name));
560 if (data_name == NULL) {
561 _log_err(LOG_CRIT, pamh, "no memory for data-name");
563 strcpy(data_name, FAIL_PREFIX);
564 strcpy(data_name + sizeof(FAIL_PREFIX) - 1, name);
567 retval = PAM_SUCCESS;
568 if (pwd == NULL || salt == NULL || !strcmp(salt, "x")) {
570 /* we are not root perhaps this is the reason? Run helper */
571 D(("running helper binary"));
572 retval = _unix_run_helper_binary(pamh, p, ctrl, name);
573 if (pwd == NULL && !on(UNIX_AUDIT,ctrl)
574 && retval != PAM_SUCCESS)
579 D(("user's record unavailable"));
580 if (on(UNIX_AUDIT, ctrl)) {
581 /* this might be a typo and the user has given a password
582 instead of a username. Careful with this. */
583 _log_err(LOG_ALERT, pamh,
584 "check pass; user (%s) unknown", name);
587 _log_err(LOG_ALERT, pamh,
588 "check pass; user unknown");
591 retval = PAM_AUTHINFO_UNAVAIL;
595 /* the stored password is NULL */
596 if (off(UNIX__NONULL, ctrl)) { /* this means we've succeeded */
597 D(("user has empty password - access granted"));
598 retval = PAM_SUCCESS;
600 D(("user has empty password - access denied"));
601 retval = PAM_AUTH_ERR;
604 retval = PAM_AUTH_ERR;
606 if (!strncmp(salt, "$1$", 3)) {
607 pp = Goodcrypt_md5(p, salt);
608 if (strcmp(pp, salt) != 0) {
610 pp = Brokencrypt_md5(p, salt);
613 pp = bigcrypt(p, salt);
615 p = NULL; /* no longer needed here */
617 /* the moment of truth -- do we agree with the password? */
618 D(("comparing state of pp[%s] and salt[%s]", pp, salt));
621 * Note, we are comparing the bigcrypt of the password with
622 * the contents of the password field. If the latter was
623 * encrypted with regular crypt (and not bigcrypt) it will
624 * have been truncated for storage relative to the output
625 * of bigcrypt here. As such we need to compare only the
626 * stored string with the subset of bigcrypt's result.
627 * Bug 521314: The strncmp comparison is for legacy support.
629 if (strncmp(pp, salt, strlen(salt)) == 0) {
630 retval = PAM_SUCCESS;
632 retval = PAM_AUTH_ERR;
637 if (retval == PAM_SUCCESS) {
638 if (data_name) /* reset failures */
639 pam_set_data(pamh, data_name, NULL, _cleanup_failures);
641 if (data_name != NULL) {
642 struct _pam_failed_auth *new = NULL;
643 const struct _pam_failed_auth *old = NULL;
645 /* get a failure recorder */
647 new = (struct _pam_failed_auth *)
648 malloc(sizeof(struct _pam_failed_auth));
652 new->user = x_strdup(name ? name : "");
654 new->euid = geteuid();
655 new->name = x_strdup(PAM_getlogin()? PAM_getlogin() : "");
657 /* any previous failures for this user ? */
658 pam_get_data(pamh, data_name, (const void **) &old);
661 new->count = old->count + 1;
662 if (new->count >= UNIX_MAX_RETRIES) {
663 retval = PAM_MAXTRIES;
666 const char *service=NULL;
667 const char *ruser=NULL;
668 const char *rhost=NULL;
669 const char *tty=NULL;
671 (void) pam_get_item(pamh, PAM_SERVICE,
672 (const void **)&service);
673 (void) pam_get_item(pamh, PAM_RUSER,
674 (const void **)&ruser);
675 (void) pam_get_item(pamh, PAM_RHOST,
676 (const void **)&rhost);
677 (void) pam_get_item(pamh, PAM_TTY,
678 (const void **)&tty);
680 _log_err(LOG_NOTICE, pamh,
681 "authentication failure; "
682 "logname=%s uid=%d euid=%d "
683 "tty=%s ruser=%s rhost=%s "
685 new->name, new->uid, new->euid,
689 (new->user && new->user[0] != '\0')
696 pam_set_data(pamh, data_name, new, _cleanup_failures);
699 _log_err(LOG_CRIT, pamh,
700 "no memory for failure recorder");
706 _pam_delete(data_name);
712 D(("done [%d].", retval));
718 * obtain a password from the user
721 int _unix_read_password(pam_handle_t * pamh
726 ,const char *data_name
736 * make sure nothing inappropriate gets returned
739 *pass = token = NULL;
742 * which authentication token are we getting?
745 authtok_flag = on(UNIX__OLD_PASSWD, ctrl) ? PAM_OLDAUTHTOK : PAM_AUTHTOK;
748 * should we obtain the password from a PAM item ?
751 if (on(UNIX_TRY_FIRST_PASS, ctrl) || on(UNIX_USE_FIRST_PASS, ctrl)) {
752 retval = pam_get_item(pamh, authtok_flag, (const void **) pass);
753 if (retval != PAM_SUCCESS) {
755 _log_err(LOG_ALERT, pamh
756 ,"pam_get_item returned error to unix-read-password"
759 } else if (*pass != NULL) { /* we have a password! */
761 } else if (on(UNIX_USE_FIRST_PASS, ctrl)) {
762 return PAM_AUTHTOK_RECOVER_ERR; /* didn't work */
763 } else if (on(UNIX_USE_AUTHTOK, ctrl)
764 && off(UNIX__OLD_PASSWD, ctrl)) {
765 return PAM_AUTHTOK_RECOVER_ERR;
769 * getting here implies we will have to get the password from the
774 struct pam_message msg[3], *pmsg[3];
775 struct pam_response *resp;
778 /* prepare to converse */
780 if (comment != NULL && off(UNIX__QUIET, ctrl)) {
782 msg[0].msg_style = PAM_TEXT_INFO;
783 msg[0].msg = comment;
790 msg[i].msg_style = PAM_PROMPT_ECHO_OFF;
791 msg[i++].msg = prompt1;
794 if (prompt2 != NULL) {
796 msg[i].msg_style = PAM_PROMPT_ECHO_OFF;
797 msg[i++].msg = prompt2;
800 /* so call the conversation expecting i responses */
802 retval = converse(pamh, ctrl, i, pmsg, &resp);
806 /* interpret the response */
808 if (retval == PAM_SUCCESS) { /* a good conversation */
810 token = x_strdup(resp[i - replies].resp);
814 /* verify that password entered correctly */
815 if (!resp[i - 1].resp
816 || strcmp(token, resp[i - 1].resp)) {
817 _pam_delete(token); /* mistyped */
818 retval = PAM_AUTHTOK_RECOVER_ERR;
819 _make_remark(pamh, ctrl
820 ,PAM_ERROR_MSG, MISTYPED_PASS);
824 _log_err(LOG_NOTICE, pamh
825 ,"could not recover authentication token");
830 * tidy up the conversation (resp_retcode) is ignored
831 * -- what is it for anyway? AGM
834 _pam_drop_reply(resp, i);
837 retval = (retval == PAM_SUCCESS)
838 ? PAM_AUTHTOK_RECOVER_ERR : retval;
842 if (retval != PAM_SUCCESS) {
843 if (on(UNIX_DEBUG, ctrl))
844 _log_err(LOG_DEBUG, pamh,
845 "unable to obtain a password");
848 /* 'token' is the entered password */
850 if (off(UNIX_NOT_SET_PASS, ctrl)) {
852 /* we store this password as an item */
854 retval = pam_set_item(pamh, authtok_flag, token);
855 _pam_delete(token); /* clean it up */
856 if (retval != PAM_SUCCESS
857 || (retval = pam_get_item(pamh, authtok_flag
858 ,(const void **) pass))
862 _log_err(LOG_CRIT, pamh, "error manipulating password");
868 * then store it as data specific to this module. pam_end()
869 * will arrange to clean it up.
872 retval = pam_set_data(pamh, data_name, (void *) token, _cleanup);
873 if (retval != PAM_SUCCESS) {
874 _log_err(LOG_CRIT, pamh
875 ,"error manipulating password data [%s]"
876 ,pam_strerror(pamh, retval));
881 token = NULL; /* break link to password */
887 /* ****************************************************************** *
888 * Copyright (c) Jan Rêkorajski 1999.
889 * Copyright (c) Andrew G. Morgan 1996-8.
890 * Copyright (c) Alex O. Yuriev, 1996.
891 * Copyright (c) Cristian Gafton 1996.
893 * Redistribution and use in source and binary forms, with or without
894 * modification, are permitted provided that the following conditions
896 * 1. Redistributions of source code must retain the above copyright
897 * notice, and the entire permission notice in its entirety,
898 * including the disclaimer of warranties.
899 * 2. Redistributions in binary form must reproduce the above copyright
900 * notice, this list of conditions and the following disclaimer in the
901 * documentation and/or other materials provided with the distribution.
902 * 3. The name of the author may not be used to endorse or promote
903 * products derived from this software without specific prior
904 * written permission.
906 * ALTERNATIVELY, this product may be distributed under the terms of
907 * the GNU Public License, in which case the provisions of the GPL are
908 * required INSTEAD OF the above restrictions. (This clause is
909 * necessary due to a potential bad interaction between the GPL and
910 * the restrictions contained in a BSD-style copyright.)
912 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
913 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
914 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
915 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
916 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
917 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
918 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
919 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
920 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
921 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
922 * OF THE POSSIBILITY OF SUCH DAMAGE.