Add the DragonFly cvs id and perform general cleanups on cvs/rcs/sccs ids. Most
[dragonfly.git] / lib / libpam / modules / pam_opie / pam_opie.c
1 /*-
2  * Copyright 2000 James Bloom
3  * All rights reserved.
4  * Based upon code Copyright 1998 Juniper Networks, Inc.
5  * Copyright (c) 2001,2002 Networks Associates Technology, Inc.
6  * All rights reserved.
7  *
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.
12  *
13  * Redistribution and use in source and binary forms, with or without
14  * modification, are permitted provided that the following conditions
15  * are met:
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
23  *    permission.
24  *
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
35  * SUCH DAMAGE.
36  *
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 $
39  */
40
41 #include <sys/types.h>
42 #include <opie.h>
43 #include <pwd.h>
44 #include <stdio.h>
45 #include <string.h>
46 #include <unistd.h>
47
48 #define PAM_SM_AUTH
49
50 #include <security/pam_appl.h>
51 #include <security/pam_modules.h>
52 #include <security/pam_mod_misc.h>
53
54 enum {
55         PAM_OPT_AUTH_AS_SELF = PAM_OPT_STD_MAX,
56         PAM_OPT_NO_FAKE_PROMPTS
57 };
58
59 static struct opttab other_options[] = {
60         { "auth_as_self",       PAM_OPT_AUTH_AS_SELF },
61         { "no_fake_prompts",    PAM_OPT_NO_FAKE_PROMPTS },
62         { NULL, 0 }
63 };
64
65 PAM_EXTERN int
66 pam_sm_authenticate(pam_handle_t *pamh, int flags __unused,
67     int argc, const char *argv[])
68 {
69         struct opie opie;
70         struct options options;
71         struct passwd *pwd;
72         int retval, i, echo;
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];
77         char *user;
78         const char *response;
79
80         pam_std_option(&options, other_options, argc, argv);
81
82         PAM_LOG("Options processed");
83
84         user = NULL;
85         if (pam_test_option(&options, PAM_OPT_AUTH_AS_SELF, NULL)) {
86                 if ((pwd = getpwnam(getlogin())) == NULL)
87                         return (PAM_AUTH_ERR);
88                 user = pwd->pw_name;
89         }
90         else {
91                 retval = pam_get_user(pamh, (const char **)&user, NULL);
92                 if (retval != PAM_SUCCESS)
93                         return (retval);
94         }
95
96         PAM_LOG("Got user: %s", user);
97
98         /*
99          * Don't call the OPIE atexit() handler when our program exits,
100          * since the module has been unloaded and we will SEGV.
101          */
102         opiedisableaeh();
103
104         /*
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.
108          */
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);
113
114         /*
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.
118          */
119         pam_set_item(pamh, PAM_AUTHTOK, NULL);
120
121         echo = pam_test_option(&options, PAM_OPT_ECHO_PASS, NULL);
122
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) {
127                         if (!echo)
128                                 pam_clear_option(&options, PAM_OPT_ECHO_PASS);
129                         opieunlock();
130                         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         if (!echo)
143                 pam_clear_option(&options, PAM_OPT_ECHO_PASS);
144
145         /* We have to copy the response, because opieverify mucks with it. */
146         strlcpy(resp, response, sizeof (resp));
147
148         /*
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.
153          */
154         retval = opieverify(&opie, resp) == 0 ? PAM_SUCCESS : PAM_AUTH_ERR;
155         return (retval);
156 }
157
158 PAM_EXTERN int
159 pam_sm_setcred(pam_handle_t *pamh __unused, int flags __unused,
160     int argc __unused, const char *argv[] __unused)
161 {
162
163         return (PAM_SUCCESS);
164 }
165
166 PAM_MODULE_ENTRY("pam_opie");