1 /* $NetBSD: pam_get_authtok.c,v 1.4 2021/03/08 19:38:10 christos Exp $ */ 2 3 /*- 4 * Copyright (c) 2002-2003 Networks Associates Technology, Inc. 5 * Copyright (c) 2004-2017 Dag-Erling Smørgrav 6 * All rights reserved. 7 * 8 * This software was developed for the FreeBSD Project by ThinkSec AS and 9 * Network Associates Laboratories, the Security Research Division of 10 * Network 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 * $OpenPAM: pam_get_authtok.c 938 2017-04-30 21:34:42Z des $ 38 */ 39 40 #ifdef HAVE_CONFIG_H 41 # include "config.h" 42 #endif 43 44 #include <sys/cdefs.h> 45 __RCSID("$NetBSD: pam_get_authtok.c,v 1.4 2021/03/08 19:38:10 christos Exp $"); 46 47 #include <sys/param.h> 48 49 #include <stdlib.h> 50 #include <string.h> 51 52 #include <security/pam_appl.h> 53 #include <security/openpam.h> 54 55 #include "openpam_impl.h" 56 #include "openpam_strlset.h" 57 58 static const char authtok_prompt[] = "Password:"; 59 static const char authtok_prompt_remote[] = "Password for %u@%h:"; 60 static const char oldauthtok_prompt[] = "Old Password:"; 61 static const char newauthtok_prompt[] = "New Password:"; 62 63 /* 64 * OpenPAM extension 65 * 66 * Retrieve authentication token 67 */ 68 69 int 70 pam_get_authtok(pam_handle_t *pamh, 71 int item, 72 const char **authtok, 73 const char *prompt) 74 { 75 char prompt_buf[1024]; 76 size_t prompt_size; 77 const void *oldauthtok, *prevauthtok, *promptp; 78 const char *prompt_option, *default_prompt; 79 const void *lhost, *rhost; 80 char *resp, *resp2; 81 int pitem, r, style, twice; 82 83 ENTER(); 84 *authtok = NULL; 85 twice = 0; 86 switch ((enum openpam_item_primitives)item) { 87 case PAM_AUTHTOK: 88 pitem = PAM_AUTHTOK_PROMPT; 89 prompt_option = "authtok_prompt"; 90 default_prompt = authtok_prompt; 91 r = pam_get_item(pamh, PAM_RHOST, &rhost); 92 if (r == PAM_SUCCESS && rhost != NULL) { 93 r = pam_get_item(pamh, PAM_HOST, &lhost); 94 if (r == PAM_SUCCESS && lhost != NULL) { 95 if (strcmp(rhost, lhost) != 0) 96 default_prompt = authtok_prompt_remote; 97 } 98 } 99 r = pam_get_item(pamh, PAM_OLDAUTHTOK, &oldauthtok); 100 if (r == PAM_SUCCESS && oldauthtok != NULL) { 101 default_prompt = newauthtok_prompt; 102 twice = 1; 103 } 104 break; 105 case PAM_OLDAUTHTOK: 106 pitem = PAM_OLDAUTHTOK_PROMPT; 107 prompt_option = "oldauthtok_prompt"; 108 default_prompt = oldauthtok_prompt; 109 twice = 0; 110 break; 111 default: 112 RETURNC(PAM_BAD_CONSTANT); 113 } 114 if (openpam_get_option(pamh, "try_first_pass") || 115 openpam_get_option(pamh, "use_first_pass")) { 116 r = pam_get_item(pamh, item, &prevauthtok); 117 if (r == PAM_SUCCESS && prevauthtok != NULL) { 118 *authtok = prevauthtok; 119 RETURNC(PAM_SUCCESS); 120 } else if (openpam_get_option(pamh, "use_first_pass")) { 121 RETURNC(r == PAM_SUCCESS ? PAM_AUTH_ERR : r); 122 } 123 } 124 /* pam policy overrides the module's choice */ 125 if ((promptp = openpam_get_option(pamh, prompt_option)) != NULL) 126 prompt = promptp; 127 /* no prompt provided, see if there is one tucked away somewhere */ 128 if (prompt == NULL) { 129 r = pam_get_item(pamh, pitem, &promptp); 130 if (r == PAM_SUCCESS && promptp != NULL) 131 prompt = promptp; 132 } 133 /* fall back to hardcoded default */ 134 if (prompt == NULL) 135 prompt = default_prompt; 136 /* expand */ 137 prompt_size = sizeof prompt_buf; 138 r = openpam_subst(pamh, prompt_buf, &prompt_size, prompt); 139 if (r == PAM_SUCCESS && prompt_size <= sizeof prompt_buf) 140 prompt = prompt_buf; 141 style = openpam_get_option(pamh, "echo_pass") ? 142 PAM_PROMPT_ECHO_ON : PAM_PROMPT_ECHO_OFF; 143 r = pam_prompt(pamh, style, &resp, "%s", prompt); 144 if (r != PAM_SUCCESS) 145 RETURNC(r); 146 if (twice) { 147 r = pam_prompt(pamh, style, &resp2, "Retype %s", prompt); 148 if (r != PAM_SUCCESS) { 149 strlset(resp, 0, PAM_MAX_RESP_SIZE); 150 FREE(resp); 151 RETURNC(r); 152 } 153 if (strcmp(resp, resp2) != 0) { 154 strlset(resp, 0, PAM_MAX_RESP_SIZE); 155 FREE(resp); 156 } 157 strlset(resp2, 0, PAM_MAX_RESP_SIZE); 158 FREE(resp2); 159 } 160 if (resp == NULL) 161 RETURNC(PAM_TRY_AGAIN); 162 r = pam_set_item(pamh, item, resp); 163 strlset(resp, 0, PAM_MAX_RESP_SIZE); 164 FREE(resp); 165 if (r != PAM_SUCCESS) 166 RETURNC(r); 167 r = pam_get_item(pamh, item, (const void **)authtok); 168 RETURNC(r); 169 } 170 171 /* 172 * Error codes: 173 * 174 * =pam_get_item 175 * =pam_prompt 176 * =pam_set_item 177 * !PAM_SYMBOL_ERR 178 * PAM_BAD_CONSTANT 179 * PAM_TRY_AGAIN 180 */ 181 182 /** 183 * The =pam_get_authtok function either prompts the user for an 184 * authentication token or retrieves a cached authentication token, 185 * depending on circumstances. 186 * Either way, a pointer to the authentication token is stored in the 187 * location pointed to by the =authtok argument, and the corresponding PAM 188 * item is updated. 189 * 190 * The =item argument must have one of the following values: 191 * 192 * =PAM_AUTHTOK: 193 * Returns the current authentication token, or the new token 194 * when changing authentication tokens. 195 * =PAM_OLDAUTHTOK: 196 * Returns the previous authentication token when changing 197 * authentication tokens. 198 * 199 * The =prompt argument specifies a prompt to use if no token is cached. 200 * If it is =NULL, the =PAM_AUTHTOK_PROMPT or =PAM_OLDAUTHTOK_PROMPT item, 201 * as appropriate, will be used. 202 * If that item is also =NULL, a hardcoded default prompt will be used. 203 * Additionally, when =pam_get_authtok is called from a service module, 204 * the prompt may be affected by module options as described below. 205 * The prompt is then expanded using =openpam_subst before it is passed to 206 * the conversation function. 207 * 208 * If =item is set to =PAM_AUTHTOK and there is a non-null =PAM_OLDAUTHTOK 209 * item, =pam_get_authtok will ask the user to confirm the new token by 210 * retyping it. 211 * If there is a mismatch, =pam_get_authtok will return =PAM_TRY_AGAIN. 212 * 213 * MODULE OPTIONS 214 * 215 * When called by a service module, =pam_get_authtok will recognize the 216 * following module options: 217 * 218 * ;authtok_prompt: 219 * Prompt to use when =item is set to =PAM_AUTHTOK. 220 * This option overrides both the =prompt argument and the 221 * =PAM_AUTHTOK_PROMPT item. 222 * ;echo_pass: 223 * If the application's conversation function allows it, this 224 * lets the user see what they are typing. 225 * This should only be used for non-reusable authentication 226 * tokens. 227 * ;oldauthtok_prompt: 228 * Prompt to use when =item is set to =PAM_OLDAUTHTOK. 229 * This option overrides both the =prompt argument and the 230 * =PAM_OLDAUTHTOK_PROMPT item. 231 * ;try_first_pass: 232 * If the requested item is non-null, return it without 233 * prompting the user. 234 * Typically, the service module will verify the token, and 235 * if it does not match, clear the item before calling 236 * =pam_get_authtok a second time. 237 * ;use_first_pass: 238 * Do not prompt the user at all; just return the cached 239 * value, or =PAM_AUTH_ERR if there is none. 240 * 241 * >pam_conv 242 * >pam_get_item 243 * >pam_get_user 244 * >openpam_get_option 245 * >openpam_subst 246 */ 247