drm/linux: Restore wait_event*() functionality
[dragonfly.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  * $FreeBSD: head/lib/libpam/modules/pam_opie/pam_opie.c 326219 2017-11-26 02:00:33Z pfg $
40  */
41
42 #include <sys/types.h>
43 #include <opie.h>
44 #include <pwd.h>
45 #include <stdio.h>
46 #include <stdlib.h>
47 #include <string.h>
48 #include <unistd.h>
49
50 #define PAM_SM_AUTH
51
52 #include <security/pam_appl.h>
53 #include <security/pam_modules.h>
54 #include <security/pam_mod_misc.h>
55
56 #define PAM_OPT_NO_FAKE_PROMPTS "no_fake_prompts"
57
58 PAM_EXTERN int
59 pam_sm_authenticate(pam_handle_t *pamh, int flags __unused,
60     int argc __unused, const char *argv[] __unused)
61 {
62         struct opie opie;
63         struct passwd *pwd;
64         int retval, i;
65         const char *(promptstr[]) = { "%s\nPassword: ", "%s\nPassword [echo on]: "};
66         char challenge[OPIE_CHALLENGE_MAX + 1];
67         char principal[OPIE_PRINCIPAL_MAX];
68         const char *user;
69         char *response;
70         int style;
71
72         user = NULL;
73         if (openpam_get_option(pamh, PAM_OPT_AUTH_AS_SELF)) {
74                 if ((pwd = getpwnam(getlogin())) == NULL)
75                         return (PAM_AUTH_ERR);
76                 user = pwd->pw_name;
77         }
78         else {
79                 retval = pam_get_user(pamh, &user, NULL);
80                 if (retval != PAM_SUCCESS)
81                         return (retval);
82         }
83
84         PAM_LOG("Got user: %s", user);
85
86         /*
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.
90          */
91         if (strlen(user) >= sizeof(principal))
92                 return (PAM_AUTH_ERR);
93         strlcpy(principal, user, sizeof(principal));
94
95         /*
96          * Don't call the OPIE atexit() handler when our program exits,
97          * since the module has been unloaded and we will SEGV.
98          */
99         opiedisableaeh();
100
101         /*
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.
105          */
106         if (opiechallenge(&opie, principal, challenge) != 0 &&
107             openpam_get_option(pamh, PAM_OPT_NO_FAKE_PROMPTS))
108                 return (PAM_AUTH_ERR);
109
110         /*
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.
114          */
115         pam_set_item(pamh, PAM_AUTHTOK, NULL);
116
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) {
122                         opieunlock();
123                         return (retval);
124                 }
125
126                 PAM_LOG("Completed challenge %d: %s", i, response);
127
128                 if (response[0] != '\0')
129                         break;
130
131                 /* Second time round, echo the password */
132                 style = PAM_PROMPT_ECHO_ON;
133         }
134
135         pam_set_item(pamh, PAM_AUTHTOK, response);
136
137         /*
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.
142          */
143         retval = opieverify(&opie, response);
144         free(response);
145         return (retval == 0 ? PAM_SUCCESS : PAM_AUTH_ERR);
146 }
147
148 PAM_EXTERN int
149 pam_sm_setcred(pam_handle_t *pamh __unused, int flags __unused,
150     int argc __unused, const char *argv[] __unused)
151 {
152
153         return (PAM_SUCCESS);
154 }
155
156 PAM_MODULE_ENTRY("pam_opie");