xref: /netbsd-src/external/bsd/openpam/dist/t/t_pam_env.c (revision 024a2e8c049a7132c2274c1f5271ae54063f3c4d)
1*77513ecfSchristos /*-
2*77513ecfSchristos  * Copyright (c) 2019 Dag-Erling Smørgrav
3*77513ecfSchristos  * All rights reserved.
4*77513ecfSchristos  *
5*77513ecfSchristos  * Redistribution and use in source and binary forms, with or without
6*77513ecfSchristos  * modification, are permitted provided that the following conditions
7*77513ecfSchristos  * are met:
8*77513ecfSchristos  * 1. Redistributions of source code must retain the above copyright
9*77513ecfSchristos  *    notice, this list of conditions and the following disclaimer.
10*77513ecfSchristos  * 2. Redistributions in binary form must reproduce the above copyright
11*77513ecfSchristos  *    notice, this list of conditions and the following disclaimer in the
12*77513ecfSchristos  *    documentation and/or other materials provided with the distribution.
13*77513ecfSchristos  * 3. The name of the author may not be used to endorse or promote
14*77513ecfSchristos  *    products derived from this software without specific prior written
15*77513ecfSchristos  *    permission.
16*77513ecfSchristos  *
17*77513ecfSchristos  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18*77513ecfSchristos  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19*77513ecfSchristos  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20*77513ecfSchristos  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21*77513ecfSchristos  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22*77513ecfSchristos  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23*77513ecfSchristos  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24*77513ecfSchristos  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25*77513ecfSchristos  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26*77513ecfSchristos  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27*77513ecfSchristos  * SUCH DAMAGE.
28*77513ecfSchristos  */
29*77513ecfSchristos 
30*77513ecfSchristos #ifdef HAVE_CONFIG_H
31*77513ecfSchristos # include "config.h"
32*77513ecfSchristos #endif
33*77513ecfSchristos 
34*77513ecfSchristos #include <stdint.h>
35*77513ecfSchristos #include <stdio.h>
36*77513ecfSchristos #include <unistd.h>
37*77513ecfSchristos 
38*77513ecfSchristos #include <cryb/test.h>
39*77513ecfSchristos 
40*77513ecfSchristos #include <security/pam_appl.h>
41*77513ecfSchristos #include <security/openpam.h>
42*77513ecfSchristos 
43*77513ecfSchristos #include "t_pam_err.h"
44*77513ecfSchristos 
45*77513ecfSchristos #define T_ENV_NAME	"MAGIC_WORDS"
46*77513ecfSchristos #define T_ENV_VALUE	"SQUEAMISH OSSIFRAGE"
47*77513ecfSchristos #define T_ENV_NAMEVALUE	T_ENV_NAME "=" T_ENV_VALUE
48*77513ecfSchristos 
49*77513ecfSchristos struct pam_conv t_null_pamc;
50*77513ecfSchristos 
51*77513ecfSchristos 
52*77513ecfSchristos /***************************************************************************
53*77513ecfSchristos  * Tests
54*77513ecfSchristos  */
55*77513ecfSchristos 
56*77513ecfSchristos static int
t_env_empty(char ** desc CRYB_UNUSED,void * arg CRYB_UNUSED)57*77513ecfSchristos t_env_empty(char **desc CRYB_UNUSED, void *arg CRYB_UNUSED)
58*77513ecfSchristos {
59*77513ecfSchristos 	pam_handle_t *pamh;
60*77513ecfSchristos 	char **envlist;
61*77513ecfSchristos 	int pam_err, ret;
62*77513ecfSchristos 
63*77513ecfSchristos 	ret = 1;
64*77513ecfSchristos 	pam_err = pam_start("t_pam_env", "test", &t_null_pamc, &pamh);
65*77513ecfSchristos 	t_assert(pam_err == PAM_SUCCESS);
66*77513ecfSchristos 	envlist = pam_getenvlist(pamh);
67*77513ecfSchristos 	ret &= t_is_not_null(envlist);
68*77513ecfSchristos 	if (envlist != NULL) {
69*77513ecfSchristos 		ret &= t_is_null(*envlist);
70*77513ecfSchristos 		openpam_free_envlist(envlist);
71*77513ecfSchristos 	}
72*77513ecfSchristos 	pam_end(pamh, pam_err);
73*77513ecfSchristos 	return (ret);
74*77513ecfSchristos }
75*77513ecfSchristos 
76*77513ecfSchristos static int
t_putenv_simple(char ** desc CRYB_UNUSED,void * arg CRYB_UNUSED)77*77513ecfSchristos t_putenv_simple(char **desc CRYB_UNUSED, void *arg CRYB_UNUSED)
78*77513ecfSchristos {
79*77513ecfSchristos 	pam_handle_t *pamh;
80*77513ecfSchristos 	char **envlist;
81*77513ecfSchristos 	int pam_err, ret;
82*77513ecfSchristos 
83*77513ecfSchristos 	ret = 1;
84*77513ecfSchristos 	pam_err = pam_start("t_pam_env", "test", &t_null_pamc, &pamh);
85*77513ecfSchristos 	t_assert(pam_err == PAM_SUCCESS);
86*77513ecfSchristos 	pam_err = pam_putenv(pamh, T_ENV_NAMEVALUE);
87*77513ecfSchristos 	ret &= t_compare_pam_err(PAM_SUCCESS, pam_err);
88*77513ecfSchristos 	envlist = pam_getenvlist(pamh);
89*77513ecfSchristos 	ret &= t_is_not_null(envlist);
90*77513ecfSchristos 	if (envlist != NULL) {
91*77513ecfSchristos 		ret &= t_compare_str(T_ENV_NAMEVALUE, envlist[0])
92*77513ecfSchristos 		    & t_is_null(envlist[1]);
93*77513ecfSchristos 		openpam_free_envlist(envlist);
94*77513ecfSchristos 	}
95*77513ecfSchristos 	pam_end(pamh, pam_err);
96*77513ecfSchristos 	return (ret);
97*77513ecfSchristos }
98*77513ecfSchristos 
99*77513ecfSchristos static int
t_setenv_simple(char ** desc CRYB_UNUSED,void * arg CRYB_UNUSED)100*77513ecfSchristos t_setenv_simple(char **desc CRYB_UNUSED, void *arg CRYB_UNUSED)
101*77513ecfSchristos {
102*77513ecfSchristos 	pam_handle_t *pamh;
103*77513ecfSchristos 	char **envlist;
104*77513ecfSchristos 	int pam_err, ret;
105*77513ecfSchristos 
106*77513ecfSchristos 	ret = 1;
107*77513ecfSchristos 	pam_err = pam_start("t_pam_env", "test", &t_null_pamc, &pamh);
108*77513ecfSchristos 	t_assert(pam_err == PAM_SUCCESS);
109*77513ecfSchristos 	pam_err = pam_setenv(pamh, T_ENV_NAME, T_ENV_VALUE, 0);
110*77513ecfSchristos 	ret &= t_compare_pam_err(PAM_SUCCESS, pam_err);
111*77513ecfSchristos 	envlist = pam_getenvlist(pamh);
112*77513ecfSchristos 	ret &= t_is_not_null(envlist);
113*77513ecfSchristos 	if (envlist != NULL) {
114*77513ecfSchristos 		ret &= t_compare_str(T_ENV_NAMEVALUE, envlist[0])
115*77513ecfSchristos 		    & t_is_null(envlist[1]);
116*77513ecfSchristos 		openpam_free_envlist(envlist);
117*77513ecfSchristos 	}
118*77513ecfSchristos 	pam_end(pamh, pam_err);
119*77513ecfSchristos 	return (ret);
120*77513ecfSchristos }
121*77513ecfSchristos 
122*77513ecfSchristos static int
t_getenv_empty(char ** desc CRYB_UNUSED,void * arg CRYB_UNUSED)123*77513ecfSchristos t_getenv_empty(char **desc CRYB_UNUSED, void *arg CRYB_UNUSED)
124*77513ecfSchristos {
125*77513ecfSchristos 	pam_handle_t *pamh;
126*77513ecfSchristos 	const char *value;
127*77513ecfSchristos 	int pam_err, ret;
128*77513ecfSchristos 
129*77513ecfSchristos 	ret = 1;
130*77513ecfSchristos 	pam_err = pam_start("t_pam_env", "test", &t_null_pamc, &pamh);
131*77513ecfSchristos 	t_assert(pam_err == PAM_SUCCESS);
132*77513ecfSchristos 	value = pam_getenv(pamh, T_ENV_NAME);
133*77513ecfSchristos 	ret &= t_compare_str(NULL, value);
134*77513ecfSchristos 	pam_end(pamh, pam_err);
135*77513ecfSchristos 	return (ret);
136*77513ecfSchristos }
137*77513ecfSchristos 
138*77513ecfSchristos static int
t_getenv_simple_miss(char ** desc CRYB_UNUSED,void * arg CRYB_UNUSED)139*77513ecfSchristos t_getenv_simple_miss(char **desc CRYB_UNUSED, void *arg CRYB_UNUSED)
140*77513ecfSchristos {
141*77513ecfSchristos 	pam_handle_t *pamh;
142*77513ecfSchristos 	const char *value;
143*77513ecfSchristos 	int pam_err, ret;
144*77513ecfSchristos 
145*77513ecfSchristos 	ret = 1;
146*77513ecfSchristos 	pam_err = pam_start("t_pam_env", "test", &t_null_pamc, &pamh);
147*77513ecfSchristos 	t_assert(pam_err == PAM_SUCCESS);
148*77513ecfSchristos 	pam_err = pam_setenv(pamh, T_ENV_NAME, T_ENV_VALUE, 0);
149*77513ecfSchristos 	t_assert(pam_err == PAM_SUCCESS);
150*77513ecfSchristos 	value = pam_getenv(pamh, "XYZZY");
151*77513ecfSchristos 	ret &= t_compare_str(NULL, value);
152*77513ecfSchristos 	pam_end(pamh, pam_err);
153*77513ecfSchristos 	return (ret);
154*77513ecfSchristos }
155*77513ecfSchristos 
156*77513ecfSchristos static int
t_getenv_simple_hit(char ** desc CRYB_UNUSED,void * arg CRYB_UNUSED)157*77513ecfSchristos t_getenv_simple_hit(char **desc CRYB_UNUSED, void *arg CRYB_UNUSED)
158*77513ecfSchristos {
159*77513ecfSchristos 	pam_handle_t *pamh;
160*77513ecfSchristos 	const char *value;
161*77513ecfSchristos 	int pam_err, ret;
162*77513ecfSchristos 
163*77513ecfSchristos 	ret = 1;
164*77513ecfSchristos 	pam_err = pam_start("t_pam_env", "test", &t_null_pamc, &pamh);
165*77513ecfSchristos 	t_assert(pam_err == PAM_SUCCESS);
166*77513ecfSchristos 	pam_err = pam_setenv(pamh, T_ENV_NAME, T_ENV_VALUE, 0);
167*77513ecfSchristos 	t_assert(pam_err == PAM_SUCCESS);
168*77513ecfSchristos 	value = pam_getenv(pamh, T_ENV_NAME);
169*77513ecfSchristos 	ret &= t_compare_str(T_ENV_VALUE, value);
170*77513ecfSchristos 	pam_end(pamh, pam_err);
171*77513ecfSchristos 	return (ret);
172*77513ecfSchristos }
173*77513ecfSchristos 
174*77513ecfSchristos 
175*77513ecfSchristos /***************************************************************************
176*77513ecfSchristos  * Boilerplate
177*77513ecfSchristos  */
178*77513ecfSchristos 
179*77513ecfSchristos static int
t_prepare(int argc CRYB_UNUSED,char * argv[]CRYB_UNUSED)180*77513ecfSchristos t_prepare(int argc CRYB_UNUSED, char *argv[] CRYB_UNUSED)
181*77513ecfSchristos {
182*77513ecfSchristos 
183*77513ecfSchristos 	openpam_set_feature(OPENPAM_FALLBACK_TO_OTHER, 0);
184*77513ecfSchristos 
185*77513ecfSchristos 	t_add_test(t_env_empty, NULL, "initially empty");
186*77513ecfSchristos 	t_add_test(t_putenv_simple, NULL, "put - simple");
187*77513ecfSchristos 	t_add_test(t_setenv_simple, NULL, "set - simple");
188*77513ecfSchristos 	t_add_test(t_getenv_empty, NULL, "get - empty");
189*77513ecfSchristos 	t_add_test(t_getenv_simple_miss, NULL, "get - simple (miss)");
190*77513ecfSchristos 	t_add_test(t_getenv_simple_hit, NULL, "get - simple (hit)");
191*77513ecfSchristos 
192*77513ecfSchristos 	return (0);
193*77513ecfSchristos }
194*77513ecfSchristos 
195*77513ecfSchristos int
main(int argc,char * argv[])196*77513ecfSchristos main(int argc, char *argv[])
197*77513ecfSchristos {
198*77513ecfSchristos 
199*77513ecfSchristos 	t_main(t_prepare, NULL, argc, argv);
200*77513ecfSchristos }
201