f7e9cf5d93f2dd5190f629165cb0ea08d736d61c
[dragonfly.git] / lib / libpam / modules / pam_skey / pam_skey.c
1 /*-
2  * Copyright 1998 Juniper Networks, Inc.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  *
26  *      $FreeBSD: src/lib/libpam/modules/pam_skey/pam_skey.c,v 1.2.6.2 2003/02/10 12:15:30 des Exp $
27  *      $DragonFly: src/lib/libpam/modules/pam_skey/Attic/pam_skey.c,v 1.2 2003/06/17 04:26:50 dillon Exp $
28  */
29
30 #include <syslog.h>     /* XXX */
31
32 #include <stdio.h>
33 #include <string.h>
34 #include <skey.h>
35
36 #define PAM_SM_AUTH
37 #include <security/pam_appl.h>
38 #include <security/pam_modules.h>
39 #include <security/pam_mod_misc.h>
40
41 enum {
42         PAM_OPT_AUTH_AS_SELF = PAM_OPT_STD_MAX,
43         PAM_OPT_NO_FAKE_PROMPTS
44 };
45
46 static struct opttab other_options[] = {
47         { "auth_as_self",       PAM_OPT_AUTH_AS_SELF },
48         { "no_fake_prompts",    PAM_OPT_NO_FAKE_PROMPTS },
49         { NULL, 0 }
50 };
51
52 PAM_EXTERN int
53 pam_sm_authenticate(pam_handle_t *pamh, int flags, int argc,
54     const char **argv)
55 {
56         int retval;
57         const char *user;
58         const char *response;
59         struct skey skey;
60         char challenge[128];
61         char prompt[128];
62         char resp_buf[128];
63         struct options options;
64         int e;
65
66         pam_std_option(&options, other_options, argc, argv);
67         /*
68          * It doesn't make sense to use a password that has already been
69          * typed in, since we haven't presented the challenge to the user
70          * yet.
71          */
72         pam_clear_option(&options, PAM_OPT_USE_FIRST_PASS);
73         pam_clear_option(&options, PAM_OPT_TRY_FIRST_PASS);
74         if ((retval = pam_get_user(pamh, &user, NULL)) != PAM_SUCCESS)
75                 return retval;
76         if (skeyinfo(&skey, user, challenge) != 0)
77                 return PAM_AUTH_ERR;
78         snprintf(prompt, sizeof prompt, "%s\nPassword: ", challenge);
79         if ((retval = pam_get_pass(pamh, &response, prompt, &options)) !=
80             PAM_SUCCESS)
81                 return retval;
82         if (response[0] == '\0' &&
83             !pam_test_option(&options, PAM_OPT_ECHO_PASS, NULL)) {
84                 pam_set_option(&options, PAM_OPT_ECHO_PASS);
85                 snprintf(prompt, sizeof prompt,
86                          "%s\nPassword [echo on]: ", challenge);
87                 retval = pam_get_pass(pamh, &response, prompt, &options);
88                 pam_clear_option(&options, PAM_OPT_ECHO_PASS);
89                 if (retval != PAM_SUCCESS)
90                         return retval;
91         }
92         /*
93          * Skeyinfo closed the database file, so we have to call skeylookup
94          * to open it again.
95          */
96         if ((e = skeylookup(&skey, user)) != 0) {
97                 if (e == -1) {
98                         syslog(LOG_ERR, "Error opening S/Key database");
99                         return PAM_SERVICE_ERR;
100                 } else
101                         return PAM_AUTH_ERR;
102         }
103         /* We have to copy the response, because skeyverify mucks with it. */
104         snprintf(resp_buf, sizeof resp_buf, "%s", response);
105         /*
106          * Skeyverify is supposed to return -1 only if an error occurs.
107          * But it returns -1 even if the response string isn't in the form
108          * it expects.  Thus we can't log an error and can only check for
109          * success or lack thereof.
110          */
111         return skeyverify(&skey, resp_buf) == 0 ? PAM_SUCCESS : PAM_AUTH_ERR;
112 }
113
114 PAM_EXTERN int
115 pam_sm_setcred(pam_handle_t *pamh, int flags, int argc, const char **argv)
116 {
117         return PAM_SUCCESS;
118 }
119
120 PAM_MODULE_ENTRY("pam_skey");