]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - lib/libpam/modules/pam_opie/pam_opie.c
Import libxo-1.0.2
[FreeBSD/FreeBSD.git] / lib / libpam / modules / pam_opie / pam_opie.c
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright 2000 James Bloom
5  * All rights reserved.
6  * Based upon code Copyright 1998 Juniper Networks, Inc.
7  * Copyright (c) 2001-2003 Networks Associates Technology, 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$");
42
43 #include <sys/types.h>
44 #include <opie.h>
45 #include <pwd.h>
46 #include <stdio.h>
47 #include <stdlib.h>
48 #include <string.h>
49 #include <unistd.h>
50
51 #define PAM_SM_AUTH
52
53 #include <security/pam_appl.h>
54 #include <security/pam_modules.h>
55 #include <security/pam_mod_misc.h>
56
57 #define PAM_OPT_NO_FAKE_PROMPTS "no_fake_prompts"
58
59 PAM_EXTERN int
60 pam_sm_authenticate(pam_handle_t *pamh, int flags __unused,
61     int argc __unused, const char *argv[] __unused)
62 {
63         struct opie opie;
64         struct passwd *pwd;
65         int retval, i;
66         const char *(promptstr[]) = { "%s\nPassword: ", "%s\nPassword [echo on]: "};
67         char challenge[OPIE_CHALLENGE_MAX + 1];
68         char principal[OPIE_PRINCIPAL_MAX];
69         const char *user;
70         char *response;
71         int style;
72
73         user = NULL;
74         if (openpam_get_option(pamh, PAM_OPT_AUTH_AS_SELF)) {
75                 if ((pwd = getpwnam(getlogin())) == NULL)
76                         return (PAM_AUTH_ERR);
77                 user = pwd->pw_name;
78         }
79         else {
80                 retval = pam_get_user(pamh, &user, NULL);
81                 if (retval != PAM_SUCCESS)
82                         return (retval);
83         }
84
85         PAM_LOG("Got user: %s", user);
86
87         /*
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.
91          */
92         if (strlen(user) >= sizeof(principal))
93                 return (PAM_AUTH_ERR);
94         strlcpy(principal, user, sizeof(principal));
95
96         /*
97          * Don't call the OPIE atexit() handler when our program exits,
98          * since the module has been unloaded and we will SEGV.
99          */
100         opiedisableaeh();
101
102         /*
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.
106          */
107         if (opiechallenge(&opie, principal, challenge) != 0 &&
108             openpam_get_option(pamh, PAM_OPT_NO_FAKE_PROMPTS))
109                 return (PAM_AUTH_ERR);
110
111         /*
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.
115          */
116         pam_set_item(pamh, PAM_AUTHTOK, NULL);
117
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) {
123                         opieunlock();
124                         return (retval);
125                 }
126
127                 PAM_LOG("Completed challenge %d: %s", i, response);
128
129                 if (response[0] != '\0')
130                         break;
131
132                 /* Second time round, echo the password */
133                 style = PAM_PROMPT_ECHO_ON;
134         }
135
136         pam_set_item(pamh, PAM_AUTHTOK, response);
137
138         /*
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.
143          */
144         retval = opieverify(&opie, response);
145         free(response);
146         return (retval == 0 ? PAM_SUCCESS : PAM_AUTH_ERR);
147 }
148
149 PAM_EXTERN int
150 pam_sm_setcred(pam_handle_t *pamh __unused, int flags __unused,
151     int argc __unused, const char *argv[] __unused)
152 {
153
154         return (PAM_SUCCESS);
155 }
156
157 PAM_MODULE_ENTRY("pam_opie");