Expand PMF_FN_* macros.
[netbsd-mini2440.git] / dist / openpam / lib / pam_get_authtok.c
blobb581fe86bd884e57a302f759ab89a02ed3b21e15
1 /*-
2 * Copyright (c) 2002-2003 Networks Associates Technology, Inc.
3 * Copyright (c) 2004-2007 Dag-Erling Smørgrav
4 * All rights reserved.
6 * This software was developed for the FreeBSD Project by ThinkSec AS and
7 * Network Associates Laboratories, the Security Research Division of
8 * Network Associates, Inc. under DARPA/SPAWAR contract N66001-01-C-8035
9 * ("CBOSS"), as part of the DARPA CHATS research program.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. The name of the author may not be used to endorse or promote
20 * products derived from this software without specific prior written
21 * permission.
23 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
35 * $Id: pam_get_authtok.c,v 1.5 2008/07/16 18:20:17 drochner Exp $
38 #include <sys/param.h>
40 #include <stdlib.h>
41 #include <string.h>
43 #include <security/pam_appl.h>
44 #include <security/openpam.h>
46 #include "openpam_impl.h"
48 static const char authtok_prompt[] = "Password:";
49 static const char oldauthtok_prompt[] = "Old Password:";
50 static const char newauthtok_prompt[] = "New Password:";
53 * OpenPAM extension
55 * Retrieve authentication token
58 int
59 pam_get_authtok(pam_handle_t *pamh,
60 int item,
61 const char **authtok,
62 const char *prompt)
64 const void *oldauthtok, *prevauthtok, *promptp;
65 const char *default_prompt;
66 char *resp, *resp2;
67 int pitem, r, style, twice;
69 ENTER();
70 if (pamh == NULL || authtok == NULL)
71 RETURNC(PAM_SYSTEM_ERR);
72 *authtok = NULL;
73 twice = 0;
74 switch (item) {
75 case PAM_AUTHTOK:
76 pitem = PAM_AUTHTOK_PROMPT;
77 default_prompt = authtok_prompt;
78 r = pam_get_item(pamh, PAM_OLDAUTHTOK, &oldauthtok);
79 if (r == PAM_SUCCESS && oldauthtok != NULL) {
80 default_prompt = newauthtok_prompt;
81 twice = 1;
83 break;
84 case PAM_OLDAUTHTOK:
85 pitem = PAM_OLDAUTHTOK_PROMPT;
86 default_prompt = oldauthtok_prompt;
87 twice = 0;
88 break;
89 default:
90 RETURNC(PAM_SYMBOL_ERR);
92 if (openpam_get_option(pamh, "try_first_pass") ||
93 openpam_get_option(pamh, "use_first_pass")) {
94 r = pam_get_item(pamh, item, &prevauthtok);
95 if (r == PAM_SUCCESS && prevauthtok != NULL) {
96 *authtok = prevauthtok;
97 RETURNC(PAM_SUCCESS);
99 else if (openpam_get_option(pamh, "use_first_pass"))
100 RETURNC(r == PAM_SUCCESS ? PAM_AUTH_ERR : r);
102 if (prompt == NULL) {
103 r = pam_get_item(pamh, pitem, &promptp);
104 if (r != PAM_SUCCESS || promptp == NULL)
105 prompt = default_prompt;
106 else
107 prompt = promptp;
109 style = openpam_get_option(pamh, "echo_pass") ?
110 PAM_PROMPT_ECHO_ON : PAM_PROMPT_ECHO_OFF;
111 r = pam_prompt(pamh, style, &resp, "%s", prompt);
112 if (r != PAM_SUCCESS)
113 RETURNC(r);
114 if (twice) {
115 r = pam_prompt(pamh, style, &resp2, "Retype %s", prompt);
116 if (r != PAM_SUCCESS) {
117 memset(resp, 0, strlen(resp));
118 FREE(resp);
119 RETURNC(r);
121 if (strcmp(resp, resp2) != 0) {
122 memset(resp, 0, strlen(resp));
123 FREE(resp);
125 memset(resp2, 0, strlen(resp2));
126 FREE(resp2);
128 if (resp == NULL)
129 RETURNC(PAM_TRY_AGAIN);
130 r = pam_set_item(pamh, item, resp);
131 memset(resp, 0, strlen(resp));
132 FREE(resp);
133 if (r != PAM_SUCCESS)
134 RETURNC(r);
135 r = pam_get_item(pamh, item, (const void **)authtok);
136 RETURNC(r);
137 /*NOTREACHED*/
141 * Error codes:
143 * =pam_get_item
144 * =pam_prompt
145 * =pam_set_item
146 * !PAM_SYMBOL_ERR
147 * PAM_TRY_AGAIN
151 * The =pam_get_authtok function returns the cached authentication token,
152 * or prompts the user if no token is currently cached.
153 * Either way, a pointer to the authentication token is stored in the
154 * location pointed to by the =authtok argument.
156 * The =item argument must have one of the following values:
158 * =PAM_AUTHTOK:
159 * Returns the current authentication token, or the new token
160 * when changing authentication tokens.
161 * =PAM_OLDAUTHTOK:
162 * Returns the previous authentication token when changing
163 * authentication tokens.
165 * The =prompt argument specifies a prompt to use if no token is cached.
166 * If it is =NULL, the =PAM_AUTHTOK_PROMPT or =PAM_OLDAUTHTOK_PROMPT item,
167 * as appropriate, will be used.
168 * If that item is also =NULL, a hardcoded default prompt will be used.
170 * If =item is set to =PAM_AUTHTOK and there is a non-null =PAM_OLDAUTHTOK
171 * item, =pam_get_authtok will ask the user to confirm the new token by
172 * retyping it.
173 * If there is a mismatch, =pam_get_authtok will return =PAM_TRY_AGAIN.
175 * >pam_get_item
176 * >pam_get_user