]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - lib/libpam/modules/pam_skey/pam_skey.c
Build structure for contribified Linux-PAM, plus some home-grown
[FreeBSD/FreeBSD.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$
27  */
28
29 #include <syslog.h>     /* XXX */
30
31 #include <stdio.h>
32 #include <string.h>
33 #include <skey.h>
34
35 #define PAM_SM_AUTH
36 #include <security/pam_modules.h>
37
38 #include "pam_mod_misc.h"
39
40 PAM_EXTERN int
41 pam_sm_authenticate(pam_handle_t *pamh, int flags, int argc,
42     const char **argv)
43 {
44         int retval;
45         const char *user;
46         const char *response;
47         struct skey skey;
48         char challenge[128];
49         char prompt[128];
50         char resp_buf[128];
51         int options;
52         int i;
53         int e;
54
55         options = 0;
56         for (i = 0;  i < argc;  i++)
57                 pam_std_option(&options, argv[i]);
58         /*
59          * It doesn't make sense to use a password that has already been
60          * typed in, since we haven't presented the challenge to the user
61          * yet.
62          */
63         options &= ~(PAM_OPT_USE_FIRST_PASS | PAM_OPT_TRY_FIRST_PASS);
64         if ((retval = pam_get_user(pamh, &user, NULL)) != PAM_SUCCESS)
65                 return retval;
66         if (skeyinfo(&skey, user, challenge) != 0)
67                 return PAM_AUTH_ERR;
68         snprintf(prompt, sizeof prompt, "%s\nPassword: ", challenge);
69         if ((retval = pam_get_pass(pamh, &response, prompt, options)) !=
70             PAM_SUCCESS)
71                 return retval;
72         if (response[0] == '\0' && !(options & PAM_OPT_ECHO_PASS)) {
73                 options |= PAM_OPT_ECHO_PASS;
74                 snprintf(prompt, sizeof prompt,
75                          "%s\nPassword [echo on]: ", challenge);
76                 if ((retval = pam_get_pass(pamh, &response, prompt,
77                     options)) != PAM_SUCCESS)
78                         return retval;
79         }
80         /*
81          * Skeyinfo closed the database file, so we have to call skeylookup
82          * to open it again.
83          */
84         if ((e = skeylookup(&skey, user)) != 0) {
85                 if (e == -1) {
86                         syslog(LOG_ERR, "Error opening S/Key database");
87                         return PAM_SERVICE_ERR;
88                 } else
89                         return PAM_AUTH_ERR;
90         }
91         /* We have to copy the response, because skeyverify mucks with it. */
92         snprintf(resp_buf, sizeof resp_buf, "%s", response);
93         /*
94          * Skeyverify is supposed to return -1 only if an error occurs.
95          * But it returns -1 even if the response string isn't in the form
96          * it expects.  Thus we can't log an error and can only check for
97          * success or lack thereof.
98          */
99         return skeyverify(&skey, resp_buf) == 0 ? PAM_SUCCESS : PAM_AUTH_ERR;
100 }
101
102 PAM_EXTERN int
103 pam_sm_setcred(pam_handle_t *pamh, int flags, int argc, const char **argv)
104 {
105         return PAM_SUCCESS;
106 }