pam_opie.c revision 115465
1/*- 2 * Copyright 2000 James Bloom 3 * All rights reserved. 4 * Based upon code Copyright 1998 Juniper Networks, Inc. 5 * Copyright (c) 2001,2002 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 38#include <sys/cdefs.h> 39__FBSDID("$FreeBSD: head/lib/libpam/modules/pam_opie/pam_opie.c 115465 2003-05-31 16:55:07Z des $"); 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 57PAM_EXTERN int 58pam_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 *user; 67 char *response; 68 int style; 69 70 user = NULL; 71 if (openpam_get_option(pamh, PAM_OPT_AUTH_AS_SELF)) { 72 if ((pwd = getpwnam(getlogin())) == NULL) 73 return (PAM_AUTH_ERR); 74 user = pwd->pw_name; 75 } 76 else { 77 retval = pam_get_user(pamh, (const char **)&user, NULL); 78 if (retval != PAM_SUCCESS) 79 return (retval); 80 } 81 82 PAM_LOG("Got user: %s", user); 83 84 /* 85 * Don't call the OPIE atexit() handler when our program exits, 86 * since the module has been unloaded and we will SEGV. 87 */ 88 opiedisableaeh(); 89 90 /* 91 * If the no_fake_prompts option was given, and the user 92 * doesn't have an OPIE key, just fail rather than present the 93 * user with a bogus OPIE challenge. 94 */ 95 /* XXX generates a const warning because of incorrect prototype */ 96 if (opiechallenge(&opie, (char *)user, challenge) != 0 && 97 openpam_get_option(pamh, PAM_OPT_NO_FAKE_PROMPTS)) 98 return (PAM_AUTH_ERR); 99 100 /* 101 * It doesn't make sense to use a password that has already been 102 * typed in, since we haven't presented the challenge to the user 103 * yet, so clear the stored password. 104 */ 105 pam_set_item(pamh, PAM_AUTHTOK, NULL); 106 107 style = PAM_PROMPT_ECHO_OFF; 108 for (i = 0; i < 2; i++) { 109 retval = pam_prompt(pamh, style, &response, 110 promptstr[i], challenge); 111 if (retval != PAM_SUCCESS) { 112 opieunlock(); 113 return (retval); 114 } 115 116 PAM_LOG("Completed challenge %d: %s", i, response); 117 118 if (response[0] != '\0') 119 break; 120 121 /* Second time round, echo the password */ 122 style = PAM_PROMPT_ECHO_ON; 123 } 124 125 pam_set_item(pamh, PAM_AUTHTOK, response); 126 127 /* 128 * Opieverify is supposed to return -1 only if an error occurs. 129 * But it returns -1 even if the response string isn't in the form 130 * it expects. Thus we can't log an error and can only check for 131 * success or lack thereof. 132 */ 133 retval = opieverify(&opie, response); 134 free(response); 135 return (retval == 0 ? PAM_SUCCESS : PAM_AUTH_ERR); 136} 137 138PAM_EXTERN int 139pam_sm_setcred(pam_handle_t *pamh __unused, int flags __unused, 140 int argc __unused, const char *argv[] __unused) 141{ 142 143 return (PAM_SUCCESS); 144} 145 146PAM_MODULE_ENTRY("pam_opie"); 147