2 * Copyright 2000 James Bloom
4 * Based upon code Copyright 1998 Juniper Networks, Inc.
5 * Copyright (c) 2001-2003 Networks Associates Technology, Inc.
8 * Portions of this software were developed for the FreeBSD Project by
9 * ThinkSec AS and NAI Labs, the Security Research Division of Network
10 * Associates, Inc. under DARPA/SPAWAR contract N66001-01-C-8035
11 * ("CBOSS"), as part of the DARPA CHATS research program.
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
21 * 3. The name of the author may not be used to endorse or promote
22 * products derived from this software without specific prior written
25 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37 * $FreeBSD: src/lib/libpam/modules/pam_opie/pam_opie.c,v 1.25 2003/05/31 17:19:03 des Exp $
38 * $DragonFly: src/lib/pam_module/pam_opie/pam_opie.c,v 1.1 2005/07/12 22:53:20 joerg Exp $
41 #include <sys/types.h>
51 #include <security/pam_appl.h>
52 #include <security/pam_modules.h>
53 #include <security/pam_mod_misc.h>
55 #define PAM_OPT_NO_FAKE_PROMPTS "no_fake_prompts"
58 pam_sm_authenticate(pam_handle_t *pamh, int flags __unused,
59 int argc __unused, const char *argv[] __unused)
64 const char *(promptstr[]) = { "%s\nPassword: ", "%s\nPassword [echo on]: "};
65 char challenge[OPIE_CHALLENGE_MAX];
71 if (openpam_get_option(pamh, PAM_OPT_AUTH_AS_SELF)) {
72 if ((pwd = getpwnam(getlogin())) == NULL)
73 return (PAM_AUTH_ERR);
77 retval = pam_get_user(pamh, (const char **)&user, NULL);
78 if (retval != PAM_SUCCESS)
82 PAM_LOG("Got user: %s", user);
85 * Don't call the OPIE atexit() handler when our program exits,
86 * since the module has been unloaded and we will SEGV.
91 * If the no_fake_prompts option was given, and the user
92 * doesn't have an OPIE key, just fail rather than present the
93 * user with a bogus OPIE challenge.
95 /* XXX generates a const warning because of incorrect prototype */
96 if (opiechallenge(&opie, (char *)user, challenge) != 0 &&
97 openpam_get_option(pamh, PAM_OPT_NO_FAKE_PROMPTS))
98 return (PAM_AUTH_ERR);
101 * It doesn't make sense to use a password that has already been
102 * typed in, since we haven't presented the challenge to the user
103 * yet, so clear the stored password.
105 pam_set_item(pamh, PAM_AUTHTOK, NULL);
107 style = PAM_PROMPT_ECHO_OFF;
108 for (i = 0; i < 2; i++) {
109 retval = pam_prompt(pamh, style, &response,
110 promptstr[i], challenge);
111 if (retval != PAM_SUCCESS) {
116 PAM_LOG("Completed challenge %d: %s", i, response);
118 if (response[0] != '\0')
121 /* Second time round, echo the password */
122 style = PAM_PROMPT_ECHO_ON;
125 pam_set_item(pamh, PAM_AUTHTOK, response);
128 * Opieverify is supposed to return -1 only if an error occurs.
129 * But it returns -1 even if the response string isn't in the form
130 * it expects. Thus we can't log an error and can only check for
131 * success or lack thereof.
133 retval = opieverify(&opie, response);
135 return (retval == 0 ? PAM_SUCCESS : PAM_AUTH_ERR);
139 pam_sm_setcred(pam_handle_t *pamh __unused, int flags __unused,
140 int argc __unused, const char *argv[] __unused)
143 return (PAM_SUCCESS);
146 PAM_MODULE_ENTRY("pam_opie");