pam_opie.c revision 90229
1/*-
2 * Copyright 2000 James Bloom
3 * All rights reserved.
4 * Based upon code Copyright 1998 Juniper Networks, Inc.
5 * Copyright (c) 2001 Networks Associates Technologies, Inc.
6 * All rights reserved.
7 * Copyright (c) 2002 Networks Associates Technologies, Inc.
8 * All rights reserved.
9 *
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.
14 *
15 * Redistribution and use in source and binary forms, with or without
16 * modification, are permitted provided that the following conditions
17 * are met:
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
25 *    permission.
26 *
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
37 * SUCH DAMAGE.
38 */
39
40#include <sys/cdefs.h>
41__FBSDID("$FreeBSD: head/lib/libpam/modules/pam_opie/pam_opie.c 90229 2002-02-05 06:08:26Z des $");
42
43#include <sys/types.h>
44#include <opie.h>
45#include <pwd.h>
46#include <stdio.h>
47#include <string.h>
48#include <unistd.h>
49
50#define PAM_SM_AUTH
51#define PAM_SM_ACCOUNT
52#define PAM_SM_SESSION
53#define PAM_SM_PASSWORD
54
55#include <security/pam_appl.h>
56#include <security/pam_modules.h>
57#include <security/pam_mod_misc.h>
58
59enum {
60	PAM_OPT_AUTH_AS_SELF	= PAM_OPT_STD_MAX,
61	PAM_OPT_NO_FAKE_PROMPTS
62};
63
64static struct opttab other_options[] = {
65	{ "auth_as_self",	PAM_OPT_AUTH_AS_SELF },
66	{ "no_fake_prompts",	PAM_OPT_NO_FAKE_PROMPTS },
67	{ NULL, 0 }
68};
69
70PAM_EXTERN int
71pam_sm_authenticate(pam_handle_t *pamh, int flags __unused, int argc, const char **argv)
72{
73	struct opie opie;
74	struct options options;
75	struct passwd *pwd;
76	int retval, i;
77	const char *(promptstr[]) = { "%s\nPassword: ", "%s\nPassword [echo on]: "};
78	char challenge[OPIE_CHALLENGE_MAX];
79	char prompt[OPIE_CHALLENGE_MAX+22];
80	char resp[OPIE_SECRET_MAX];
81	const char *user;
82	const char *response;
83
84	pam_std_option(&options, other_options, argc, argv);
85
86	PAM_LOG("Options processed");
87
88	user = NULL;
89	if (pam_test_option(&options, PAM_OPT_AUTH_AS_SELF, NULL)) {
90		if ((pwd = getpwnam(getlogin())) == NULL)
91			PAM_RETURN(PAM_AUTH_ERR);
92		user = pwd->pw_name;
93	}
94	else {
95		retval = pam_get_user(pamh, (const char **)&user, NULL);
96		if (retval != PAM_SUCCESS)
97			PAM_RETURN(retval);
98	}
99
100	PAM_LOG("Got user: %s", user);
101
102	/*
103	 * Don't call the OPIE atexit() handler when our program exits,
104	 * since the module has been unloaded and we will SEGV.
105	 */
106	opiedisableaeh();
107
108	/*
109	 * If the no_fake_prompts option was given, and the user
110	 * doesn't have an OPIE key, just fail rather than present the
111	 * user with a bogus OPIE challenge.
112	 */
113	/* XXX generates a const warning because of incorrect prototype */
114	if (opiechallenge(&opie, (char *)user, challenge) != 0 &&
115	    pam_test_option(&options, PAM_OPT_NO_FAKE_PROMPTS, NULL))
116		PAM_RETURN(PAM_AUTH_ERR);
117
118	/*
119	 * It doesn't make sense to use a password that has already been
120	 * typed in, since we haven't presented the challenge to the user
121	 * yet, so clear the stored password.
122	 */
123	pam_set_item(pamh, PAM_AUTHTOK, NULL);
124
125	for (i = 0; i < 2; i++) {
126		snprintf(prompt, sizeof prompt, promptstr[i], challenge);
127		retval = pam_get_pass(pamh, &response, prompt, &options);
128		if (retval != PAM_SUCCESS) {
129			opieunlock();
130			PAM_RETURN(retval);
131		}
132
133		PAM_LOG("Completed challenge %d: %s", i, response);
134
135		if (response[0] != '\0')
136			break;
137
138		/* Second time round, echo the password */
139		pam_set_option(&options, PAM_OPT_ECHO_PASS);
140	}
141
142	/* We have to copy the response, because opieverify mucks with it. */
143	strlcpy(resp, response, sizeof (resp));
144
145	/*
146	 * Opieverify is supposed to return -1 only if an error occurs.
147	 * But it returns -1 even if the response string isn't in the form
148	 * it expects.  Thus we can't log an error and can only check for
149	 * success or lack thereof.
150	 */
151	retval = opieverify(&opie, resp) == 0 ? PAM_SUCCESS : PAM_AUTH_ERR;
152	PAM_RETURN(retval);
153}
154
155PAM_EXTERN int
156pam_sm_setcred(pam_handle_t *pamh __unused, int flags __unused, int argc, const char **argv)
157{
158	struct options options;
159
160	pam_std_option(&options, other_options, argc, argv);
161
162	PAM_LOG("Options processed");
163
164	PAM_RETURN(PAM_SUCCESS);
165}
166
167PAM_EXTERN int
168pam_sm_acct_mgmt(pam_handle_t *pamh __unused, int flags __unused, int argc ,const char **argv)
169{
170	struct options options;
171
172	pam_std_option(&options, NULL, argc, argv);
173
174	PAM_LOG("Options processed");
175
176	PAM_RETURN(PAM_IGNORE);
177}
178
179PAM_EXTERN int
180pam_sm_chauthtok(pam_handle_t *pamh __unused, int flags __unused, int argc, const char **argv)
181{
182	struct options options;
183
184	pam_std_option(&options, NULL, argc, argv);
185
186	PAM_LOG("Options processed");
187
188	PAM_RETURN(PAM_IGNORE);
189}
190
191PAM_EXTERN int
192pam_sm_open_session(pam_handle_t *pamh __unused, int flags __unused, int argc, const char **argv)
193{
194	struct options options;
195
196	pam_std_option(&options, NULL, argc, argv);
197
198	PAM_LOG("Options processed");
199
200	PAM_RETURN(PAM_IGNORE);
201}
202
203PAM_EXTERN int
204pam_sm_close_session(pam_handle_t *pamh __unused, int flags __unused, int argc, const char **argv)
205{
206	struct options options;
207
208	pam_std_option(&options, NULL, argc, argv);
209
210	PAM_LOG("Options processed");
211
212	PAM_RETURN(PAM_IGNORE);
213}
214
215PAM_MODULE_ENTRY("pam_opie");
216