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
39 * $FreeBSD: head/lib/libpam/modules/pam_opie/pam_opie.c 326219 2017-11-26 02:00:33Z pfg $
42 #include <sys/types.h>
52 #include <security/pam_appl.h>
53 #include <security/pam_modules.h>
54 #include <security/pam_mod_misc.h>
56 #define PAM_OPT_NO_FAKE_PROMPTS "no_fake_prompts"
59 pam_sm_authenticate(pam_handle_t *pamh, int flags __unused,
60 int argc __unused, const char *argv[] __unused)
65 const char *(promptstr[]) = { "%s\nPassword: ", "%s\nPassword [echo on]: "};
66 char challenge[OPIE_CHALLENGE_MAX + 1];
67 char principal[OPIE_PRINCIPAL_MAX];
73 if (openpam_get_option(pamh, PAM_OPT_AUTH_AS_SELF)) {
74 if ((pwd = getpwnam(getlogin())) == NULL)
75 return (PAM_AUTH_ERR);
79 retval = pam_get_user(pamh, &user, NULL);
80 if (retval != PAM_SUCCESS)
84 PAM_LOG("Got user: %s", user);
87 * Watch out: libopie feels entitled to truncate the user name
88 * passed to it if it's longer than OPIE_PRINCIPAL_MAX, which is
89 * not uncommon in Windows environments.
91 if (strlen(user) >= sizeof(principal))
92 return (PAM_AUTH_ERR);
93 strlcpy(principal, user, sizeof(principal));
96 * Don't call the OPIE atexit() handler when our program exits,
97 * since the module has been unloaded and we will SEGV.
102 * If the no_fake_prompts option was given, and the user
103 * doesn't have an OPIE key, just fail rather than present the
104 * user with a bogus OPIE challenge.
106 if (opiechallenge(&opie, principal, challenge) != 0 &&
107 openpam_get_option(pamh, PAM_OPT_NO_FAKE_PROMPTS))
108 return (PAM_AUTH_ERR);
111 * It doesn't make sense to use a password that has already been
112 * typed in, since we haven't presented the challenge to the user
113 * yet, so clear the stored password.
115 pam_set_item(pamh, PAM_AUTHTOK, NULL);
117 style = PAM_PROMPT_ECHO_OFF;
118 for (i = 0; i < 2; i++) {
119 retval = pam_prompt(pamh, style, &response,
120 promptstr[i], challenge);
121 if (retval != PAM_SUCCESS) {
126 PAM_LOG("Completed challenge %d: %s", i, response);
128 if (response[0] != '\0')
131 /* Second time round, echo the password */
132 style = PAM_PROMPT_ECHO_ON;
135 pam_set_item(pamh, PAM_AUTHTOK, response);
138 * Opieverify is supposed to return -1 only if an error occurs.
139 * But it returns -1 even if the response string isn't in the form
140 * it expects. Thus we can't log an error and can only check for
141 * success or lack thereof.
143 retval = opieverify(&opie, response);
145 return (retval == 0 ? PAM_SUCCESS : PAM_AUTH_ERR);
149 pam_sm_setcred(pam_handle_t *pamh __unused, int flags __unused,
150 int argc __unused, const char *argv[] __unused)
153 return (PAM_SUCCESS);
156 PAM_MODULE_ENTRY("pam_opie");