source: openpam/trunk/t/t_pam_env.c @ 943

Last change on this file since 943 was 943, checked in by Dag-Erling Smørgrav, 2 years ago

Fix an off-by-one bug in pam_getenv() which was inadvertantly
introduced when pam_getenv() was (needlessly) rewritten as part of
r913. Rewrite pam_getenv() again (but correctly, this time) to reduce
the number of times we iterate over the same string. Add a few unit
tests for pam_{get,put,set}env(), including one which would have
caught the bug.

Credit goes to Tim Creech <tcreech@…> for discovering and
reporting the bug.

File size: 5.4 KB
Line 
1/*-
2 * Copyright (c) 2018 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 * $OpenPAM$
30 */
31
32#ifdef HAVE_CONFIG_H
33# include "config.h"
34#endif
35
36#include <stdint.h>
37#include <stdio.h>
38#include <unistd.h>
39
40#include <cryb/test.h>
41
42#include <security/pam_appl.h>
43#include <security/openpam.h>
44
45#include "t_pam_err.h"
46
47#define T_ENV_NAME      "MAGIC_WORDS"
48#define T_ENV_VALUE     "SQUEAMISH OSSIFRAGE"
49#define T_ENV_NAMEVALUE T_ENV_NAME "=" T_ENV_VALUE
50
51struct pam_conv t_null_pamc;
52
53
54/***************************************************************************
55 * Tests
56 */
57
58static int
59t_env_empty(char **desc CRYB_UNUSED, void *arg CRYB_UNUSED)
60{
61        pam_handle_t *pamh;
62        char **envlist;
63        int pam_err, ret;
64
65        ret = 1;
66        pam_err = pam_start("t_pam_env", "test", &t_null_pamc, &pamh);
67        t_assert(pam_err == PAM_SUCCESS);
68        envlist = pam_getenvlist(pamh);
69        ret &= t_is_not_null(envlist);
70        if (envlist != NULL) {
71                ret &= t_is_null(*envlist);
72                openpam_free_envlist(envlist);
73        }
74        pam_end(pamh, pam_err);
75        return (ret);
76}
77
78static int
79t_putenv_simple(char **desc CRYB_UNUSED, void *arg CRYB_UNUSED)
80{
81        pam_handle_t *pamh;
82        char **envlist;
83        int pam_err, ret;
84
85        ret = 1;
86        pam_err = pam_start("t_pam_env", "test", &t_null_pamc, &pamh);
87        t_assert(pam_err == PAM_SUCCESS);
88        pam_err = pam_putenv(pamh, T_ENV_NAMEVALUE);
89        ret &= t_compare_pam_err(PAM_SUCCESS, pam_err);
90        envlist = pam_getenvlist(pamh);
91        ret &= t_is_not_null(envlist);
92        if (envlist != NULL) {
93                ret &= t_compare_str(T_ENV_NAMEVALUE, envlist[0])
94                    & t_is_null(envlist[1]);
95                openpam_free_envlist(envlist);
96        }
97        pam_end(pamh, pam_err);
98        return (ret);
99}
100
101static int
102t_setenv_simple(char **desc CRYB_UNUSED, void *arg CRYB_UNUSED)
103{
104        pam_handle_t *pamh;
105        char **envlist;
106        int pam_err, ret;
107
108        ret = 1;
109        pam_err = pam_start("t_pam_env", "test", &t_null_pamc, &pamh);
110        t_assert(pam_err == PAM_SUCCESS);
111        pam_err = pam_setenv(pamh, T_ENV_NAME, T_ENV_VALUE, 0);
112        ret &= t_compare_pam_err(PAM_SUCCESS, pam_err);
113        envlist = pam_getenvlist(pamh);
114        ret &= t_is_not_null(envlist);
115        if (envlist != NULL) {
116                ret &= t_compare_str(T_ENV_NAMEVALUE, envlist[0])
117                    & t_is_null(envlist[1]);
118                openpam_free_envlist(envlist);
119        }
120        pam_end(pamh, pam_err);
121        return (ret);
122}
123
124static int
125t_getenv_empty(char **desc CRYB_UNUSED, void *arg CRYB_UNUSED)
126{
127        pam_handle_t *pamh;
128        const char *value;
129        int pam_err, ret;
130
131        ret = 1;
132        pam_err = pam_start("t_pam_env", "test", &t_null_pamc, &pamh);
133        t_assert(pam_err == PAM_SUCCESS);
134        value = pam_getenv(pamh, T_ENV_NAME);
135        ret &= t_compare_str(NULL, value);
136        pam_end(pamh, pam_err);
137        return (ret);
138}
139
140static int
141t_getenv_simple_miss(char **desc CRYB_UNUSED, void *arg CRYB_UNUSED)
142{
143        pam_handle_t *pamh;
144        const char *value;
145        int pam_err, ret;
146
147        ret = 1;
148        pam_err = pam_start("t_pam_env", "test", &t_null_pamc, &pamh);
149        t_assert(pam_err == PAM_SUCCESS);
150        pam_err = pam_setenv(pamh, T_ENV_NAME, T_ENV_VALUE, 0);
151        t_assert(pam_err == PAM_SUCCESS);
152        value = pam_getenv(pamh, "XYZZY");
153        ret &= t_compare_str(NULL, value);
154        pam_end(pamh, pam_err);
155        return (ret);
156}
157
158static int
159t_getenv_simple_hit(char **desc CRYB_UNUSED, void *arg CRYB_UNUSED)
160{
161        pam_handle_t *pamh;
162        const char *value;
163        int pam_err, ret;
164
165        ret = 1;
166        pam_err = pam_start("t_pam_env", "test", &t_null_pamc, &pamh);
167        t_assert(pam_err == PAM_SUCCESS);
168        pam_err = pam_setenv(pamh, T_ENV_NAME, T_ENV_VALUE, 0);
169        t_assert(pam_err == PAM_SUCCESS);
170        value = pam_getenv(pamh, T_ENV_NAME);
171        ret &= t_compare_str(T_ENV_VALUE, value);
172        pam_end(pamh, pam_err);
173        return (ret);
174}
175
176
177/***************************************************************************
178 * Boilerplate
179 */
180
181static int
182t_prepare(int argc CRYB_UNUSED, char *argv[] CRYB_UNUSED)
183{
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
195int
196main(int argc, char *argv[])
197{
198
199        t_main(t_prepare, NULL, argc, argv);
200}
Note: See TracBrowser for help on using the repository browser.