Debian Codegraph @

xtests/tst-pam_pwhistory1.c

raw symbols SCIP
1 /*
2 * Redistribution and use in source and binary forms, with or without
3 * modification, are permitted provided that the following conditions
4 * are met:
5 * 1. Redistributions of source code must retain the above copyright
6 * notice, and the entire permission notice in its entirety,
7 * including the disclaimer of warranties.
8 * 2. Redistributions in binary form must reproduce the above copyright
9 * notice, this list of conditions and the following disclaimer in the
10 * documentation and/or other materials provided with the distribution.
11 * 3. The name of the author may not be used to endorse or promote
12 * products derived from this software without specific prior
13 * written permission.
14 *
15 * ALTERNATIVELY, this product may be distributed under the terms of
16 * the GNU Public License, in which case the provisions of the GPL are
17 * required INSTEAD OF the above restrictions. (This clause is
18 * necessary due to a potential bad interaction between the GPL and
19 * the restrictions contained in a BSD-style copyright.)
20 *
21 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
22 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
23 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
24 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
25 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
26 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
27 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
29 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
31 * OF THE POSSIBILITY OF SUCH DAMAGE.
32 */
33
34 /*
35 * Check remember handling
36 * Change ten times the password
37 * Try the ten passwords again, should always be rejected
38 * Try a new password, should succeed
39 */
40
41 #include <config.h>
42
43 #include <stdio.h>
44 #include <stdlib.h>
45 #include <string.h>
46 #include <security/pam_appl.h>
47 #include "pam_inline.h"
48
49 static unsigned int in_test;
50
51 static const char *passwords[] = {
52 "pamhistory01", "pamhistory02", "pamhistory03",
53 "pamhistory04", "pamhistory05", "pamhistory06",
54 "pamhistory07", "pamhistory08", "pamhistory09",
55 "pamhistory10",
56 "pamhistory01", "pamhistory02", "pamhistory03",
57 "pamhistory04", "pamhistory05", "pamhistory06",
58 "pamhistory07", "pamhistory08", "pamhistory09",
59 "pamhistory10",
60 "pamhistory11",
61 "pamhistory01", "pamhistory02", "pamhistory03",
62 "pamhistory04", "pamhistory05", "pamhistory06",
63 "pamhistory07", "pamhistory08", "pamhistory09",
64 "pamhistory10"};
65
66 static int debug;
67
68 /* A conversation function which uses an internally-stored value for
69 the responses. */
70 static int
71 fake_conv (int num_msg, const struct pam_message **msgm,
72 struct pam_response **response, void *appdata_ptr UNUSED)
73 {
74 struct pam_response *reply;
75 int count;
76
77 /* Sanity test. */
78 if (num_msg <= 0)
79 return PAM_CONV_ERR;
80
81 if (debug)
82 fprintf (stderr, "msg_style=%d, msg=%s\n", msgm[0]->msg_style,
83 msgm[0]->msg);
84
85 if (msgm[0]->msg_style != 1)
86 return PAM_SUCCESS;
87
88 /* Allocate memory for the responses. */
89 reply = calloc (num_msg, sizeof (struct pam_response));
90 if (reply == NULL)
91 return PAM_CONV_ERR;
92
93 /* Each prompt elicits the same response. */
94 for (count = 0; count < num_msg; ++count)
95 {
96 reply[count].resp_retcode = 0;
97 reply[count].resp = strdup (passwords[in_test]);
98 if (debug)
99 fprintf (stderr, "send password %s\n", reply[count].resp);
100 }
101
102 /* Set the pointers in the response structure and return. */
103 *response = reply;
104 return PAM_SUCCESS;
105 }
106
107 static struct pam_conv conv = {
108 fake_conv,
109 NULL
110 };
111
112
113 int
114 main(int argc, char *argv[])
115 {
116 pam_handle_t *pamh=NULL;
117 const char *user="tstpampwhistory";
118 int retval;
119
120 if (argc > 1 && strcmp (argv[1], "-d") == 0)
121 debug = 1;
122
123 for (in_test = 0; in_test < PAM_ARRAY_SIZE(passwords); in_test++)
124 {
125
126 retval = pam_start("tst-pam_pwhistory1", user, &conv, &pamh);
127 if (retval != PAM_SUCCESS)
128 {
129 if (debug)
130 fprintf (stderr, "pwhistory1-%u: pam_start returned %d\n",
131 in_test, retval);
132 return 1;
133 }
134
135 retval = pam_chauthtok (pamh, 0);
136 if (in_test < 10 || in_test == 20)
137 {
138 if (retval != PAM_SUCCESS)
139 {
140 if (debug)
141 fprintf (stderr, "pwhistory1-%u: pam_chauthtok returned %d\n",
142 in_test, retval);
143 return 1;
144 }
145 }
146 else if (in_test < 20)
147 {
148 if (retval != PAM_MAXTRIES)
149 {
150 if (debug)
151 fprintf (stderr, "pwhistory1-%u: pam_chauthtok returned %d\n",
152 in_test, retval);
153 return 1;
154 }
155 }
156
157 retval = pam_end (pamh,retval);
158 if (retval != PAM_SUCCESS)
159 {
160 if (debug)
161 fprintf (stderr, "pwhistory1: pam_end returned %d\n", retval);
162 return 1;
163 }
164 }
165
166 return 0;
167 }