Initial import of binutils 2.22 on the new vendor branch
[dragonfly.git] / lib / pam_module / 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-2003 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.26 2006/09/15 13:42:38 des Exp $
38  * $DragonFly: src/lib/pam_module/pam_opie/pam_opie.c,v 1.1 2005/07/12 22:53:20 joerg Exp $
39  */
40
41 #include <sys/types.h>
42 #include <opie.h>
43 #include <pwd.h>
44 #include <stdio.h>
45 #include <stdlib.h>
46 #include <string.h>
47 #include <unistd.h>
48
49 #define PAM_SM_AUTH
50
51 #include <security/pam_appl.h>
52 #include <security/pam_modules.h>
53 #include <security/pam_mod_misc.h>
54
55 #define PAM_OPT_NO_FAKE_PROMPTS "no_fake_prompts"
56
57 PAM_EXTERN int
58 pam_sm_authenticate(pam_handle_t *pamh, int flags __unused,
59     int argc __unused, const char *argv[] __unused)
60 {
61         struct opie opie;
62         struct passwd *pwd;
63         int retval, i;
64         const char *(promptstr[]) = { "%s\nPassword: ", "%s\nPassword [echo on]: "};
65         char challenge[OPIE_CHALLENGE_MAX];
66         char principal[OPIE_PRINCIPAL_MAX];
67         const char *user;
68         char *response;
69         int style;
70
71         user = NULL;
72         if (openpam_get_option(pamh, PAM_OPT_AUTH_AS_SELF)) {
73                 if ((pwd = getpwnam(getlogin())) == NULL)
74                         return (PAM_AUTH_ERR);
75                 user = pwd->pw_name;
76         }
77         else {
78                 retval = pam_get_user(pamh, &user, NULL);
79                 if (retval != PAM_SUCCESS)
80                         return (retval);
81         }
82
83         PAM_LOG("Got user: %s", user);
84
85         /*
86          * Watch out: libopie feels entitled to truncate the user name
87          * passed to it if it's longer than OPIE_PRINCIPAL_MAX, which is
88          * not uncommon in Windows environments.
89          */
90         if (strlen(user) >= sizeof(principal))
91                 return (PAM_AUTH_ERR);
92         strlcpy(principal, user, sizeof(principal));
93
94         /*
95          * Don't call the OPIE atexit() handler when our program exits,
96          * since the module has been unloaded and we will SEGV.
97          */
98         opiedisableaeh();
99
100         /*
101          * If the no_fake_prompts option was given, and the user
102          * doesn't have an OPIE key, just fail rather than present the
103          * user with a bogus OPIE challenge.
104          */
105         if (opiechallenge(&opie, principal, challenge) != 0 &&
106             openpam_get_option(pamh, PAM_OPT_NO_FAKE_PROMPTS))
107                 return (PAM_AUTH_ERR);
108
109         /*
110          * It doesn't make sense to use a password that has already been
111          * typed in, since we haven't presented the challenge to the user
112          * yet, so clear the stored password.
113          */
114         pam_set_item(pamh, PAM_AUTHTOK, NULL);
115
116         style = PAM_PROMPT_ECHO_OFF;
117         for (i = 0; i < 2; i++) {
118                 retval = pam_prompt(pamh, style, &response,
119                     promptstr[i], challenge);
120                 if (retval != PAM_SUCCESS) {
121                         opieunlock();
122                         return (retval);
123                 }
124
125                 PAM_LOG("Completed challenge %d: %s", i, response);
126
127                 if (response[0] != '\0')
128                         break;
129
130                 /* Second time round, echo the password */
131                 style = PAM_PROMPT_ECHO_ON;
132         }
133
134         pam_set_item(pamh, PAM_AUTHTOK, response);
135
136         /*
137          * Opieverify is supposed to return -1 only if an error occurs.
138          * But it returns -1 even if the response string isn't in the form
139          * it expects.  Thus we can't log an error and can only check for
140          * success or lack thereof.
141          */
142         retval = opieverify(&opie, response);
143         free(response);
144         return (retval == 0 ? PAM_SUCCESS : PAM_AUTH_ERR);
145 }
146
147 PAM_EXTERN int
148 pam_sm_setcred(pam_handle_t *pamh __unused, int flags __unused,
149     int argc __unused, const char *argv[] __unused)
150 {
151
152         return (PAM_SUCCESS);
153 }
154
155 PAM_MODULE_ENTRY("pam_opie");