4 * Copyright information at end of file.
19 #include <security/_pam_macros.h>
20 #include <security/pam_modules.h>
25 extern char *crypt(const char *key, const char *salt);
26 extern char *bigcrypt(const char *key, const char *salt);
28 /* syslogging function for errors and other information */
30 void _log_err(int err, pam_handle_t *pamh, const char *format,...)
36 pam_get_item(pamh, PAM_SERVICE, (const void **) &service);
38 strncpy(logname, service, sizeof(logname));
39 logname[sizeof(logname) - 1 - strlen("(pam_unix)")] = '\0';
40 strncat(logname, "(pam_unix)", strlen("(pam_unix)"));
42 strncpy(logname, "pam_unix", sizeof(logname) - 1);
45 va_start(args, format);
46 openlog(logname, LOG_CONS | LOG_PID, LOG_AUTH);
47 vsyslog(err, format, args);
52 /* this is a front-end for module-application conversations */
54 static int converse(pam_handle_t * pamh, int ctrl, int nargs
55 ,struct pam_message **message
56 ,struct pam_response **response)
59 struct pam_conv *conv;
61 D(("begin to converse"));
63 retval = pam_get_item(pamh, PAM_CONV, (const void **) &conv);
64 if (retval == PAM_SUCCESS) {
66 retval = conv->conv(nargs, (const struct pam_message **) message
67 ,response, conv->appdata_ptr);
69 D(("returned from application's conversation function"));
71 if (retval != PAM_SUCCESS && on(UNIX_DEBUG, ctrl)) {
72 _log_err(LOG_DEBUG, pamh, "conversation failure [%s]"
73 ,pam_strerror(pamh, retval));
75 } else if (retval != PAM_CONV_AGAIN) {
76 _log_err(LOG_ERR, pamh
77 ,"couldn't obtain coversation function [%s]"
78 ,pam_strerror(pamh, retval));
80 D(("ready to return from module conversation"));
82 return retval; /* propagate error status */
85 int _make_remark(pam_handle_t * pamh, unsigned int ctrl
86 ,int type, const char *text)
88 int retval = PAM_SUCCESS;
90 if (off(UNIX__QUIET, ctrl)) {
91 struct pam_message *pmsg[1], msg[1];
92 struct pam_response *resp;
96 msg[0].msg_style = type;
99 retval = converse(pamh, ctrl, 1, pmsg, &resp);
102 _pam_drop_reply(resp, 1);
109 * Beacause getlogin() is braindead and sometimes it just
110 * doesn't work, we reimplement it here.
112 char *PAM_getlogin(void)
114 struct utmp *ut, line;
115 char *curr_tty, *retval;
116 static char curr_user[sizeof(ut->ut_user) + 4];
120 curr_tty = ttyname(0);
121 if (curr_tty != NULL) {
122 D(("PAM_getlogin ttyname: %s", curr_tty));
125 strncpy(line.ut_line, curr_tty, sizeof line.ut_line);
126 if ((ut = getutline(&line)) != NULL) {
127 strncpy(curr_user, ut->ut_user, sizeof(ut->ut_user));
132 D(("PAM_getlogin retval: %s", retval));
138 * set the control flags for the UNIX module.
141 int _set_ctrl(pam_handle_t *pamh, int flags, int *remember, int argc,
148 ctrl = UNIX_DEFAULTS; /* the default selection of options */
150 /* set some flags manually */
152 if (getuid() == 0 && !(flags & PAM_CHANGE_EXPIRED_AUTHTOK)) {
154 set(UNIX__IAMROOT, ctrl);
156 if (flags & PAM_UPDATE_AUTHTOK) {
157 D(("UPDATE_AUTHTOK"));
158 set(UNIX__UPDATE, ctrl);
160 if (flags & PAM_PRELIM_CHECK) {
162 set(UNIX__PRELIM, ctrl);
164 if (flags & PAM_DISALLOW_NULL_AUTHTOK) {
165 D(("DISALLOW_NULL_AUTHTOK"));
166 set(UNIX__NONULL, ctrl);
168 if (flags & PAM_SILENT) {
170 set(UNIX__QUIET, ctrl);
172 /* now parse the arguments to this module */
177 D(("pam_unix arg: %s", *argv));
179 for (j = 0; j < UNIX_CTRLS_; ++j) {
180 if (unix_args[j].token
181 && !strncmp(*argv, unix_args[j].token, strlen(unix_args[j].token))) {
186 if (j >= UNIX_CTRLS_) {
187 _log_err(LOG_ERR, pamh,
188 "unrecognized option [%s]", *argv);
190 ctrl &= unix_args[j].mask; /* for turning things off */
191 ctrl |= unix_args[j].flag; /* for turning things on */
193 if (remember != NULL) {
194 if (j == UNIX_REMEMBER_PASSWD) {
195 *remember = strtol(*argv + 9, NULL, 10);
196 if ((*remember == LONG_MIN) || (*remember == LONG_MAX))
204 ++argv; /* step to next argument */
207 /* auditing is a more sensitive version of debug */
209 if (on(UNIX_AUDIT, ctrl)) {
210 set(UNIX_DEBUG, ctrl);
212 /* return the set of flags */
218 static void _cleanup(pam_handle_t * pamh, void *x, int error_status)
223 /* ************************************************************** *
224 * Useful non-trivial functions *
225 * ************************************************************** */
228 * the following is used to keep track of the number of times a user fails
229 * to authenticate themself.
232 #define FAIL_PREFIX "-UN*X-FAIL-"
233 #define UNIX_MAX_RETRIES 3
235 struct _pam_failed_auth {
236 char *user; /* user that's failed to be authenticated */
237 char *name; /* attempt from user with name */
238 int uid; /* uid of calling user */
239 int euid; /* euid of calling process */
240 int count; /* number of failures so far */
243 #ifndef PAM_DATA_REPLACE
244 #error "Need to get an updated libpam 0.52 or better"
247 static void _cleanup_failures(pam_handle_t * pamh, void *fl, int err)
250 const char *service = NULL;
251 const char *ruser = NULL;
252 const char *rhost = NULL;
253 const char *tty = NULL;
254 struct _pam_failed_auth *failure;
258 quiet = err & PAM_DATA_SILENT; /* should we log something? */
259 err &= PAM_DATA_REPLACE; /* are we just replacing data? */
260 failure = (struct _pam_failed_auth *) fl;
262 if (failure != NULL) {
264 if (!quiet && !err) { /* under advisement from Sun,may go away */
266 /* log the number of authentication failures */
267 if (failure->count > 1) {
268 (void) pam_get_item(pamh, PAM_SERVICE,
269 (const void **)&service);
270 (void) pam_get_item(pamh, PAM_RUSER,
271 (const void **)&ruser);
272 (void) pam_get_item(pamh, PAM_RHOST,
273 (const void **)&rhost);
274 (void) pam_get_item(pamh, PAM_TTY,
275 (const void **)&tty);
276 _log_err(LOG_NOTICE, pamh,
277 "%d more authentication failure%s; "
278 "logname=%s uid=%d euid=%d "
279 "tty=%s ruser=%s rhost=%s "
281 failure->count - 1, failure->count == 2 ? "" : "s",
282 failure->name, failure->uid, failure->euid,
283 tty ? tty : "", ruser ? ruser : "",
285 (failure->user && failure->user[0] != '\0')
286 ? " user=" : "", failure->user
289 if (failure->count > UNIX_MAX_RETRIES) {
290 _log_err(LOG_ALERT, pamh
291 ,"service(%s) ignoring max retries; %d > %d"
292 ,service == NULL ? "**unknown**" : service
298 _pam_delete(failure->user); /* tidy up */
299 _pam_delete(failure->name); /* tidy up */
305 * _unix_blankpasswd() is a quick check for a blank password
307 * returns TRUE if user does not have a password
308 * - to avoid prompting for one in such cases (CG)
311 int _unix_blankpasswd(unsigned int ctrl, const char *name)
313 struct passwd *pwd = NULL;
314 struct spwd *spwdent = NULL;
321 * This function does not have to be too smart if something goes
322 * wrong, return FALSE and let this case to be treated somewhere
326 if (on(UNIX__NONULL, ctrl))
327 return 0; /* will fail but don't let on yet */
329 /* UNIX passwords area */
330 pwd = getpwnam(name); /* Get password file entry... */
333 if (strcmp( pwd->pw_passwd, "*NP*" ) == 0)
335 uid_t save_euid, save_uid;
337 save_euid = geteuid();
339 if (save_uid == pwd->pw_uid)
340 setreuid( save_euid, save_uid );
343 if (setreuid( -1, pwd->pw_uid ) == -1) {
346 if(setreuid( -1, pwd->pw_uid ) == -1)
347 /* Will fail elsewhere. */
352 spwdent = getspnam( name );
353 if (save_uid == pwd->pw_uid)
354 setreuid( save_uid, save_euid );
356 if (setreuid( -1, 0 ) == -1)
357 setreuid( save_uid, -1 );
358 setreuid( -1, save_euid );
360 } else if (strcmp(pwd->pw_passwd, "x") == 0) {
362 * ...and shadow password file entry for this user,
363 * if shadowing is enabled
365 spwdent = getspnam(name);
368 salt = x_strdup(spwdent->sp_pwdp);
370 salt = x_strdup(pwd->pw_passwd);
372 /* Does this user have a password? */
376 if (strlen(salt) == 0)
391 * verify the password of a user
394 #include <sys/types.h>
395 #include <sys/wait.h>
397 static int _unix_run_helper_binary(pam_handle_t *pamh, const char *passwd,
398 unsigned int ctrl, const char *user)
400 int retval, child, fds[2];
403 /* create a pipe for the password */
404 if (pipe(fds) != 0) {
405 D(("could not make pipe"));
412 static char *envp[] = { NULL };
413 char *args[] = { NULL, NULL, NULL };
415 /* XXX - should really tidy up PAM here too */
417 /* reopen stdin as pipe */
419 dup2(fds[0], STDIN_FILENO);
421 /* exec binary helper */
422 args[0] = x_strdup(CHKPWD_HELPER);
423 args[1] = x_strdup(user);
425 execve(CHKPWD_HELPER, args, envp);
427 /* should not get here: exit with error */
428 D(("helper binary is not available"));
429 exit(PAM_AUTHINFO_UNAVAIL);
430 } else if (child > 0) {
432 /* if the stored password is NULL */
433 if (off(UNIX__NONULL, ctrl)) { /* this means we've succeeded */
434 write(fds[1], "nullok\0\0", 8);
436 write(fds[1], "nonull\0\0", 8);
438 if (passwd != NULL) { /* send the password to the child */
439 write(fds[1], passwd, strlen(passwd)+1);
442 write(fds[1], "", 1); /* blank password */
444 close(fds[0]); /* close here to avoid possible SIGPIPE above */
446 (void) waitpid(child, &retval, 0); /* wait for helper to complete */
447 retval = (retval == 0) ? PAM_SUCCESS:PAM_AUTH_ERR;
450 retval = PAM_AUTH_ERR;
453 D(("returning %d", retval));
457 int _unix_verify_password(pam_handle_t * pamh, const char *name
458 ,const char *p, unsigned int ctrl)
460 struct passwd *pwd = NULL;
461 struct spwd *spwdent = NULL;
469 #ifdef HAVE_PAM_FAIL_DELAY
470 if (off(UNIX_NODELAY, ctrl)) {
471 D(("setting delay"));
472 (void) pam_fail_delay(pamh, 2000000); /* 2 sec delay for on failure */
476 /* locate the entry for this user */
478 D(("locating user's record"));
480 /* UNIX passwords area */
481 pwd = getpwnam(name); /* Get password file entry... */
484 if (strcmp( pwd->pw_passwd, "*NP*" ) == 0)
486 uid_t save_euid, save_uid;
488 save_euid = geteuid();
490 if (save_uid == pwd->pw_uid)
491 setreuid( save_euid, save_uid );
494 if (setreuid( -1, pwd->pw_uid ) == -1) {
497 if(setreuid( -1, pwd->pw_uid ) == -1)
498 return PAM_CRED_INSUFFICIENT;
502 spwdent = getspnam( name );
503 if (save_uid == pwd->pw_uid)
504 setreuid( save_uid, save_euid );
506 if (setreuid( -1, 0 ) == -1)
507 setreuid( save_uid, -1 );
508 setreuid( -1, save_euid );
510 } else if (strcmp(pwd->pw_passwd, "x") == 0) {
512 * ...and shadow password file entry for this user,
513 * if shadowing is enabled
515 spwdent = getspnam(name);
518 salt = x_strdup(spwdent->sp_pwdp);
520 salt = x_strdup(pwd->pw_passwd);
523 data_name = (char *) malloc(sizeof(FAIL_PREFIX) + strlen(name));
524 if (data_name == NULL) {
525 _log_err(LOG_CRIT, pamh, "no memory for data-name");
527 strcpy(data_name, FAIL_PREFIX);
528 strcpy(data_name + sizeof(FAIL_PREFIX) - 1, name);
531 retval = PAM_SUCCESS;
532 if (pwd == NULL || salt == NULL || !strcmp(salt, "x")) {
534 /* we are not root perhaps this is the reason? Run helper */
535 D(("running helper binary"));
536 retval = _unix_run_helper_binary(pamh, p, ctrl, name);
537 if (pwd == NULL && !on(UNIX_AUDIT,ctrl)
538 && retval != PAM_SUCCESS)
543 D(("user's record unavailable"));
544 if (on(UNIX_AUDIT, ctrl)) {
545 /* this might be a typo and the user has given a password
546 instead of a username. Careful with this. */
547 _log_err(LOG_ALERT, pamh,
548 "check pass; user (%s) unknown", name);
551 _log_err(LOG_ALERT, pamh,
552 "check pass; user unknown");
555 retval = PAM_AUTHINFO_UNAVAIL;
559 /* the stored password is NULL */
560 if (off(UNIX__NONULL, ctrl)) { /* this means we've succeeded */
561 D(("user has empty password - access granted"));
562 retval = PAM_SUCCESS;
564 D(("user has empty password - access denied"));
565 retval = PAM_AUTH_ERR;
568 retval = PAM_AUTH_ERR;
570 if (!strncmp(salt, "$1$", 3)) {
571 pp = Goodcrypt_md5(p, salt);
572 if (strcmp(pp, salt) != 0) {
573 pp = Brokencrypt_md5(p, salt);
576 pp = bigcrypt(p, salt);
578 p = NULL; /* no longer needed here */
580 /* the moment of truth -- do we agree with the password? */
581 D(("comparing state of pp[%s] and salt[%s]", pp, salt));
583 if (strcmp(pp, salt) == 0) {
584 retval = PAM_SUCCESS;
586 retval = PAM_AUTH_ERR;
591 if (retval == PAM_SUCCESS) {
592 if (data_name) /* reset failures */
593 pam_set_data(pamh, data_name, NULL, _cleanup_failures);
595 if (data_name != NULL) {
596 struct _pam_failed_auth *new = NULL;
597 const struct _pam_failed_auth *old = NULL;
599 /* get a failure recorder */
601 new = (struct _pam_failed_auth *)
602 malloc(sizeof(struct _pam_failed_auth));
606 new->user = x_strdup(name ? name : "");
608 new->euid = geteuid();
609 new->name = x_strdup(PAM_getlogin()? PAM_getlogin() : "");
611 /* any previous failures for this user ? */
612 pam_get_data(pamh, data_name, (const void **) &old);
615 new->count = old->count + 1;
616 if (new->count >= UNIX_MAX_RETRIES) {
617 retval = PAM_MAXTRIES;
620 const char *service=NULL;
621 const char *ruser=NULL;
622 const char *rhost=NULL;
623 const char *tty=NULL;
625 (void) pam_get_item(pamh, PAM_SERVICE,
626 (const void **)&service);
627 (void) pam_get_item(pamh, PAM_RUSER,
628 (const void **)&ruser);
629 (void) pam_get_item(pamh, PAM_RHOST,
630 (const void **)&rhost);
631 (void) pam_get_item(pamh, PAM_TTY,
632 (const void **)&tty);
634 _log_err(LOG_NOTICE, pamh,
635 "authentication failure; "
636 "logname=%s uid=%d euid=%d "
637 "tty=%s ruser=%s rhost=%s "
639 new->name, new->uid, new->euid,
643 (new->user && new->user[0] != '\0')
650 pam_set_data(pamh, data_name, new, _cleanup_failures);
653 _log_err(LOG_CRIT, pamh,
654 "no memory for failure recorder");
660 _pam_delete(data_name);
666 D(("done [%d].", retval));
672 * obtain a password from the user
675 int _unix_read_password(pam_handle_t * pamh
680 ,const char *data_name
691 * make sure nothing inappropriate gets returned
694 *pass = token = NULL;
697 * which authentication token are we getting?
700 authtok_flag = on(UNIX__OLD_PASSWD, ctrl) ? PAM_OLDAUTHTOK : PAM_AUTHTOK;
703 * should we obtain the password from a PAM item ?
706 if (on(UNIX_TRY_FIRST_PASS, ctrl) || on(UNIX_USE_FIRST_PASS, ctrl)) {
707 retval = pam_get_item(pamh, authtok_flag, (const void **) &item);
708 if (retval != PAM_SUCCESS) {
710 _log_err(LOG_ALERT, pamh
711 ,"pam_get_item returned error to unix-read-password"
714 } else if (item != NULL) { /* we have a password! */
718 } else if (on(UNIX_USE_FIRST_PASS, ctrl)) {
719 return PAM_AUTHTOK_RECOVER_ERR; /* didn't work */
720 } else if (on(UNIX_USE_AUTHTOK, ctrl)
721 && off(UNIX__OLD_PASSWD, ctrl)) {
722 return PAM_AUTHTOK_RECOVER_ERR;
726 * getting here implies we will have to get the password from the
731 struct pam_message msg[3], *pmsg[3];
732 struct pam_response *resp;
735 /* prepare to converse */
737 if (comment != NULL && off(UNIX__QUIET, ctrl)) {
739 msg[0].msg_style = PAM_TEXT_INFO;
740 msg[0].msg = comment;
747 msg[i].msg_style = PAM_PROMPT_ECHO_OFF;
748 msg[i++].msg = prompt1;
751 if (prompt2 != NULL) {
753 msg[i].msg_style = PAM_PROMPT_ECHO_OFF;
754 msg[i++].msg = prompt2;
757 /* so call the conversation expecting i responses */
759 retval = converse(pamh, ctrl, i, pmsg, &resp);
763 /* interpret the response */
765 if (retval == PAM_SUCCESS) { /* a good conversation */
767 token = x_strdup(resp[i - replies].resp);
771 /* verify that password entered correctly */
772 if (!resp[i - 1].resp
773 || strcmp(token, resp[i - 1].resp)) {
774 _pam_delete(token); /* mistyped */
775 retval = PAM_AUTHTOK_RECOVER_ERR;
776 _make_remark(pamh, ctrl
777 ,PAM_ERROR_MSG, MISTYPED_PASS);
781 _log_err(LOG_NOTICE, pamh
782 ,"could not recover authentication token");
787 * tidy up the conversation (resp_retcode) is ignored
788 * -- what is it for anyway? AGM
791 _pam_drop_reply(resp, i);
794 retval = (retval == PAM_SUCCESS)
795 ? PAM_AUTHTOK_RECOVER_ERR : retval;
799 if (retval != PAM_SUCCESS) {
800 if (on(UNIX_DEBUG, ctrl))
801 _log_err(LOG_DEBUG, pamh,
802 "unable to obtain a password");
805 /* 'token' is the entered password */
807 if (off(UNIX_NOT_SET_PASS, ctrl)) {
809 /* we store this password as an item */
811 retval = pam_set_item(pamh, authtok_flag, token);
812 _pam_delete(token); /* clean it up */
813 if (retval != PAM_SUCCESS
814 || (retval = pam_get_item(pamh, authtok_flag
815 ,(const void **) &item))
818 _log_err(LOG_CRIT, pamh, "error manipulating password");
824 * then store it as data specific to this module. pam_end()
825 * will arrange to clean it up.
828 retval = pam_set_data(pamh, data_name, (void *) token, _cleanup);
829 if (retval != PAM_SUCCESS) {
830 _log_err(LOG_CRIT, pamh
831 ,"error manipulating password data [%s]"
832 ,pam_strerror(pamh, retval));
837 token = NULL; /* break link to password */
841 item = NULL; /* break link to password */
846 /* ****************************************************************** *
847 * Copyright (c) Jan Rêkorajski 1999.
848 * Copyright (c) Andrew G. Morgan 1996-8.
849 * Copyright (c) Alex O. Yuriev, 1996.
850 * Copyright (c) Cristian Gafton 1996.
852 * Redistribution and use in source and binary forms, with or without
853 * modification, are permitted provided that the following conditions
855 * 1. Redistributions of source code must retain the above copyright
856 * notice, and the entire permission notice in its entirety,
857 * including the disclaimer of warranties.
858 * 2. Redistributions in binary form must reproduce the above copyright
859 * notice, this list of conditions and the following disclaimer in the
860 * documentation and/or other materials provided with the distribution.
861 * 3. The name of the author may not be used to endorse or promote
862 * products derived from this software without specific prior
863 * written permission.
865 * ALTERNATIVELY, this product may be distributed under the terms of
866 * the GNU Public License, in which case the provisions of the GPL are
867 * required INSTEAD OF the above restrictions. (This clause is
868 * necessary due to a potential bad interaction between the GPL and
869 * the restrictions contained in a BSD-style copyright.)
871 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
872 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
873 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
874 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
875 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
876 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
877 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
878 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
879 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
880 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
881 * OF THE POSSIBILITY OF SUCH DAMAGE.