2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright 2000 James Bloom
6 * Based upon code Copyright 1998 Juniper Networks, Inc.
7 * Copyright (c) 2001-2003 Networks Associates Technology, Inc.
10 * Portions of this software were developed for the FreeBSD Project by
11 * ThinkSec AS and NAI Labs, the Security Research Division of Network
12 * Associates, Inc. under DARPA/SPAWAR contract N66001-01-C-8035
13 * ("CBOSS"), as part of the DARPA CHATS research program.
15 * Redistribution and use in source and binary forms, with or without
16 * modification, are permitted provided that the following conditions
18 * 1. Redistributions of source code must retain the above copyright
19 * notice, this list of conditions and the following disclaimer.
20 * 2. Redistributions in binary form must reproduce the above copyright
21 * notice, this list of conditions and the following disclaimer in the
22 * documentation and/or other materials provided with the distribution.
23 * 3. The name of the author may not be used to endorse or promote
24 * products derived from this software without specific prior written
27 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
28 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
31 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
32 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
33 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
34 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
35 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
36 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
40 #include <sys/cdefs.h>
41 __FBSDID("$FreeBSD$");
43 #include <sys/types.h>
53 #include <security/pam_appl.h>
54 #include <security/pam_modules.h>
55 #include <security/pam_mod_misc.h>
57 #define PAM_OPT_NO_FAKE_PROMPTS "no_fake_prompts"
60 pam_sm_authenticate(pam_handle_t *pamh, int flags __unused,
61 int argc __unused, const char *argv[] __unused)
66 const char *(promptstr[]) = { "%s\nPassword: ", "%s\nPassword [echo on]: "};
67 char challenge[OPIE_CHALLENGE_MAX + 1];
68 char principal[OPIE_PRINCIPAL_MAX];
74 if (openpam_get_option(pamh, PAM_OPT_AUTH_AS_SELF)) {
75 if ((pwd = getpwnam(getlogin())) == NULL)
76 return (PAM_AUTH_ERR);
80 retval = pam_get_user(pamh, &user, NULL);
81 if (retval != PAM_SUCCESS)
85 PAM_LOG("Got user: %s", user);
88 * Watch out: libopie feels entitled to truncate the user name
89 * passed to it if it's longer than OPIE_PRINCIPAL_MAX, which is
90 * not uncommon in Windows environments.
92 if (strlen(user) >= sizeof(principal))
93 return (PAM_AUTH_ERR);
94 strlcpy(principal, user, sizeof(principal));
97 * Don't call the OPIE atexit() handler when our program exits,
98 * since the module has been unloaded and we will SEGV.
103 * If the no_fake_prompts option was given, and the user
104 * doesn't have an OPIE key, just fail rather than present the
105 * user with a bogus OPIE challenge.
107 if (opiechallenge(&opie, principal, challenge) != 0 &&
108 openpam_get_option(pamh, PAM_OPT_NO_FAKE_PROMPTS))
109 return (PAM_AUTH_ERR);
112 * It doesn't make sense to use a password that has already been
113 * typed in, since we haven't presented the challenge to the user
114 * yet, so clear the stored password.
116 pam_set_item(pamh, PAM_AUTHTOK, NULL);
118 style = PAM_PROMPT_ECHO_OFF;
119 for (i = 0; i < 2; i++) {
120 retval = pam_prompt(pamh, style, &response,
121 promptstr[i], challenge);
122 if (retval != PAM_SUCCESS) {
127 PAM_LOG("Completed challenge %d: %s", i, response);
129 if (response[0] != '\0')
132 /* Second time round, echo the password */
133 style = PAM_PROMPT_ECHO_ON;
136 pam_set_item(pamh, PAM_AUTHTOK, response);
139 * Opieverify is supposed to return -1 only if an error occurs.
140 * But it returns -1 even if the response string isn't in the form
141 * it expects. Thus we can't log an error and can only check for
142 * success or lack thereof.
144 retval = opieverify(&opie, response);
146 return (retval == 0 ? PAM_SUCCESS : PAM_AUTH_ERR);
150 pam_sm_setcred(pam_handle_t *pamh __unused, int flags __unused,
151 int argc __unused, const char *argv[] __unused)
154 return (PAM_SUCCESS);
157 PAM_MODULE_ENTRY("pam_opie");