File: pam_get_pass.c

package info (click to toggle)
libpam-ssh 2.1%2Bds1-2
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 1,296 kB
  • ctags: 1,465
  • sloc: ansic: 16,551; makefile: 95; sh: 38
file content (113 lines) | stat: -rw-r--r-- 3,676 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
/*-
 * Copyright 1998 Juniper Networks, Inc.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */

#include <sys/cdefs.h>

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <pam_modules.h>

#include <config.h>
#if !HAVE_PAM_STRUCT_OPTTAB
# include "pam_opttab.h"
#endif
#if !HAVE_PAM_STD_OPTION && !HAVE_OPENPAM
# include "pam_option.h"
#endif
#include "pam_get_pass.h"

int
pam_conv_pass(pam_handle_t *pamh, const char *prompt, struct options *options)
{
	const struct pam_conv *conv;
	struct pam_message msg;
	const struct pam_message *msgs[1];
	struct pam_response *resp;
	const void *item;
	int retval;

	retval = pam_get_item(pamh, PAM_CONV, &item);
	if (retval != PAM_SUCCESS)
		return retval;
	conv = (const struct pam_conv *)item;
	msg.msg_style = pam_test_option(options, PAM_OPT_ECHO_PASS, NULL) ?
	    PAM_PROMPT_ECHO_ON : PAM_PROMPT_ECHO_OFF;
	msg.msg = prompt;
	msgs[0] = &msg;
	retval = conv->conv(1, msgs, &resp, conv->appdata_ptr);
	if (retval != PAM_SUCCESS)
		return retval;
        if (resp[0].resp == NULL)
                return PAM_AUTHTOK_RECOVERY_ERR;
	retval = pam_set_item(pamh, PAM_AUTHTOK, resp[0].resp);
	if (retval != PAM_SUCCESS)
		return retval;
	memset(resp[0].resp, 0, strlen(resp[0].resp));
	free(resp[0].resp);
	free(resp);
	return PAM_SUCCESS;
}

int
pam_get_pass(pam_handle_t *pamh, const char **passp, const char *prompt,
    struct options *options)
{
	int retval;
	const void *item = NULL;

	/*
	 * Grab the already-entered password.
	 */
        retval = pam_get_item(pamh, PAM_AUTHTOK, &item);
        /*
         * Always use standard prompt for the first time.
         */
        if (item == NULL)
                prompt = "Password: ";
	if (pam_test_option(options, PAM_OPT_TRY_FIRST_PASS, NULL) ||
	    pam_test_option(options, PAM_OPT_USE_FIRST_PASS, NULL)) {
		if (retval != PAM_SUCCESS)
			return retval;
        } else {
                item = NULL;    /* Need own password. */
	}

	if (item == NULL) {
		/* The user hasn't entered a password yet. */
		if (pam_test_option(options, PAM_OPT_USE_FIRST_PASS, NULL))
			return PAM_AUTH_ERR;
		/* Use the conversation function to get a password. */
		retval = pam_conv_pass(pamh, prompt, options);
		if (retval != PAM_SUCCESS)
			return retval;
		retval = pam_get_item(pamh, PAM_AUTHTOK, &item);
		if (retval != PAM_SUCCESS)
			return retval;
	}
	*passp = (const char *)item;
	return PAM_SUCCESS;
}