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