2 * Copyright 2000 James Bloom
4 * Based upon code Copyright 1998 Juniper Networks, Inc.
5 * Copyright (c) 2001,2002 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.1.2.2 2003/02/10 12:15:30 des Exp $
38 * $DragonFly: src/lib/libpam/modules/pam_opie/Attic/pam_opie.c,v 1.2 2003/06/17 04:26:50 dillon Exp $
41 #include <sys/types.h>
50 #include <security/pam_appl.h>
51 #include <security/pam_modules.h>
52 #include <security/pam_mod_misc.h>
55 PAM_OPT_AUTH_AS_SELF = PAM_OPT_STD_MAX,
56 PAM_OPT_NO_FAKE_PROMPTS
59 static struct opttab other_options[] = {
60 { "auth_as_self", PAM_OPT_AUTH_AS_SELF },
61 { "no_fake_prompts", PAM_OPT_NO_FAKE_PROMPTS },
66 pam_sm_authenticate(pam_handle_t *pamh, int flags __unused,
67 int argc, const char *argv[])
70 struct options options;
73 const char *(promptstr[]) = { "%s\nPassword: ", "%s\nPassword [echo on]: "};
74 char challenge[OPIE_CHALLENGE_MAX];
75 char prompt[OPIE_CHALLENGE_MAX+22];
76 char resp[OPIE_SECRET_MAX];
80 pam_std_option(&options, other_options, argc, argv);
82 PAM_LOG("Options processed");
85 if (pam_test_option(&options, PAM_OPT_AUTH_AS_SELF, NULL)) {
86 if ((pwd = getpwnam(getlogin())) == NULL)
87 return (PAM_AUTH_ERR);
91 retval = pam_get_user(pamh, (const char **)&user, NULL);
92 if (retval != PAM_SUCCESS)
96 PAM_LOG("Got user: %s", user);
99 * Don't call the OPIE atexit() handler when our program exits,
100 * since the module has been unloaded and we will SEGV.
105 * If the no_fake_prompts option was given, and the user
106 * doesn't have an OPIE key, just fail rather than present the
107 * user with a bogus OPIE challenge.
109 /* XXX generates a const warning because of incorrect prototype */
110 if (opiechallenge(&opie, (char *)user, challenge) != 0 &&
111 pam_test_option(&options, PAM_OPT_NO_FAKE_PROMPTS, NULL))
112 return (PAM_AUTH_ERR);
115 * It doesn't make sense to use a password that has already been
116 * typed in, since we haven't presented the challenge to the user
117 * yet, so clear the stored password.
119 pam_set_item(pamh, PAM_AUTHTOK, NULL);
121 echo = pam_test_option(&options, PAM_OPT_ECHO_PASS, NULL);
123 for (i = 0; i < 2; i++) {
124 snprintf(prompt, sizeof prompt, promptstr[i], challenge);
125 retval = pam_get_pass(pamh, &response, prompt, &options);
126 if (retval != PAM_SUCCESS) {
128 pam_clear_option(&options, PAM_OPT_ECHO_PASS);
133 PAM_LOG("Completed challenge %d: %s", i, response);
135 if (response[0] != '\0')
138 /* Second time round, echo the password */
139 pam_set_option(&options, PAM_OPT_ECHO_PASS);
143 pam_clear_option(&options, PAM_OPT_ECHO_PASS);
145 /* We have to copy the response, because opieverify mucks with it. */
146 strlcpy(resp, response, sizeof (resp));
149 * Opieverify is supposed to return -1 only if an error occurs.
150 * But it returns -1 even if the response string isn't in the form
151 * it expects. Thus we can't log an error and can only check for
152 * success or lack thereof.
154 retval = opieverify(&opie, resp) == 0 ? PAM_SUCCESS : PAM_AUTH_ERR;
159 pam_sm_setcred(pam_handle_t *pamh __unused, int flags __unused,
160 int argc __unused, const char *argv[] __unused)
163 return (PAM_SUCCESS);
166 PAM_MODULE_ENTRY("pam_opie");