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 #ifdef HAVE_CONFIG_H
35 #include <config.h>
36 #endif
37
38 #include <config.h>
39
40 #include <stdio.h>
41 #include <unistd.h>
42 #include <string.h>
43 #include <stdlib.h>
44 #include <security/pam_appl.h>
45 #include <security/pam_modules.h>
46
47 static const char *prompt = "myprompt:";
48 static const char *user = "itsme";
49
50 static int
51 login_conv (int num_msg, const struct pam_message **mesg,
52 struct pam_response **resp, void *appdata_ptr UNUSED)
53 {
54 struct pam_response *reply;
55 int count;
56
57 reply = calloc(num_msg, sizeof (struct pam_response));
58
59 if (reply == NULL)
60 return PAM_BUF_ERR;
61
62 for (count = 0; count < num_msg; count++)
63 {
64 reply[count].resp_retcode = 0;
65 reply[count].resp = NULL;
66
67 switch (mesg[count]->msg_style)
68 {
69 case PAM_PROMPT_ECHO_ON:
70 if (strcmp (mesg[count]->msg, prompt) != 0)
71 {
72 fprintf (stderr, "conv function called with wrong prompt: %s\n",
73 mesg[count]->msg);
74 exit (1);
75 }
76 reply[count].resp = strdup (user);
77 break;
78
79 default:
80 fprintf (stderr,
81 "pam_get_user calls conv function with unexpected msg style");
82 exit (1);
83 }
84 }
85
86 *resp = reply;
87 return PAM_SUCCESS;
88 }
89
90 int
91 main (void)
92 {
93 const char *service = "dummy";
94 const char *value;
95 struct pam_conv conv = { &login_conv, NULL};
96 pam_handle_t *pamh;
97 int retval;
98
99 /* 1: Call with NULL for every argument */
100 retval = pam_get_user (NULL, NULL, NULL);
101 if (retval == PAM_SUCCESS)
102 {
103 fprintf (stderr,
104 "tst-pam_get_user (NULL, NULL, NULL) returned PAM_SUCCESS\n");
105 return 1;
106 }
107
108 /* setup pam handle */
109 retval = pam_start (service, user, &conv, &pamh);
110 if (retval != PAM_SUCCESS)
111 {
112 fprintf (stderr, "pam_start (%s, %s, &conv, &pamh) returned %d\n",
113 service, user, retval);
114 return 1;
115 }
116
117 /* 2: Call with valid pamh handle but NULL for user */
118 retval = pam_get_user (pamh, NULL, NULL);
119 if (retval == PAM_SUCCESS)
120 {
121 fprintf (stderr,
122 "tst-pam_get_user (pamh, NULL, NULL) returned PAM_SUCCESS\n");
123 return 1;
124 }
125
126 /* 3: Call with valid pamh handle and valid user ptr */
127 retval = pam_get_user (pamh, &value, NULL);
128 if (retval != PAM_SUCCESS)
129 {
130 fprintf (stderr,
131 "tst-pam_get_user (pamh, &value, NULL) returned %d\n",
132 retval);
133 return 1;
134 }
135 if (strcmp (user, value) != 0)
136 {
137 fprintf (stderr,
138 "tst-pam_get_user (pamh, &value, NULL) mismatch:\n"
139 "expected: %s\n"
140 "got: %s\n", user, value);
141 return 1;
142 }
143
144 pam_end (pamh, 0);
145
146 /* setup pam handle without user */
147 retval = pam_start (service, NULL, &conv, &pamh);
148 if (retval != PAM_SUCCESS)
149 {
150 fprintf (stderr, "pam_start (%s, %s, &conv, &pamh) returned %d\n",
151 service, user, retval);
152 return 1;
153 }
154
155 /* 4: Call with valid pamh handle and valid user ptr */
156 retval = pam_get_user (pamh, &value, prompt);
157 if (retval != PAM_SUCCESS)
158 {
159 fprintf (stderr,
160 "tst-pam_get_user (pamh, &value, prompt) returned %d\n",
161 retval);
162 return 1;
163 }
164 if (strcmp (user, value) != 0)
165 {
166 fprintf (stderr,
167 "tst-pam_get_user (pamh, &value, prompt) mismatch:\n"
168 "expected: %s\n"
169 "got: %s\n", user, value);
170 return 1;
171 }
172
173 pam_end (pamh, 0);
174
175 return 0;
176 }