aboutsummaryrefslogtreecommitdiff
path: root/modules/pam_sepermit/tst-pam_sepermit-retval.c
blob: 21ebaba20a22d072aec5af13672746dea61d3666 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
/*
 * Check pam_sepermit return values and conf= option.
 *
 * Copyright (c) 2020-2022 Dmitry V. Levin <ldv@altlinux.org>
 */

#include "test_assert.h"
#include "pam_inline.h"

#include <limits.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <security/pam_appl.h>

#define MODULE_NAME "pam_sepermit"
#define TEST_NAME "tst-" MODULE_NAME "-retval"

static const char service_file[] = TEST_NAME ".service";
static const char missing_file[] = TEST_NAME ".missing";
static const char config_file[] = TEST_NAME ".conf";
static struct pam_conv conv;

int
main(void)
{
	pam_handle_t *pamh = NULL;
	FILE *fp;
	char *cwd;

	cwd = xgetcwd();
	ASSERT_NE(NULL, cwd);

	/* PAM_USER_UNKNOWN */
	ASSERT_NE(NULL, fp = fopen(service_file, "w"));
	ASSERT_LT(0,
		  fprintf(fp, "#%%PAM-1.0\n"
			      "auth required %s/" LTDIR "%s.so\n"
			      "account required %s/" LTDIR "%s.so\n"
			      "password required %s/" LTDIR "%s.so\n"
			      "session required %s/" LTDIR "%s.so\n",
			  cwd, MODULE_NAME,
			  cwd, MODULE_NAME,
			  cwd, MODULE_NAME,
			  cwd, MODULE_NAME));
	ASSERT_EQ(0, fclose(fp));

	ASSERT_EQ(PAM_SUCCESS,
		  pam_start_confdir(service_file, "", &conv, ".", &pamh));
	ASSERT_NE(NULL, pamh);
	ASSERT_EQ(PAM_USER_UNKNOWN, pam_authenticate(pamh, 0));
	ASSERT_EQ(PAM_PERM_DENIED, pam_setcred(pamh, 0));
	ASSERT_EQ(PAM_USER_UNKNOWN, pam_acct_mgmt(pamh, 0));
	ASSERT_EQ(PAM_MODULE_UNKNOWN, pam_chauthtok(pamh, 0));
	ASSERT_EQ(PAM_MODULE_UNKNOWN, pam_open_session(pamh, 0));
	ASSERT_EQ(PAM_MODULE_UNKNOWN, pam_close_session(pamh, 0));
	ASSERT_EQ(PAM_SUCCESS, pam_end(pamh, 0));
	pamh = NULL;

	ASSERT_NE(NULL, fp = fopen(config_file, "w"));
	ASSERT_LT(0, fprintf(fp, "nosuchuser:ignore\n"));
	ASSERT_EQ(0, fclose(fp));

	/*
	 * conf= specifies an existing file,
	 * PAM_IGNORE -> PAM_PERM_DENIED
	 */
	ASSERT_NE(NULL, fp = fopen(service_file, "w"));
	ASSERT_LT(0,
		  fprintf(fp, "#%%PAM-1.0\n"
			      "auth required %s/" LTDIR "%s.so conf=%s\n"
			      "account required %s/" LTDIR "%s.so conf=%s\n"
			      "password required %s/" LTDIR "%s.so conf=%s\n"
			      "session required %s/" LTDIR "%s.so conf=%s\n",
			  cwd, MODULE_NAME, config_file,
			  cwd, MODULE_NAME, config_file,
			  cwd, MODULE_NAME, config_file,
			  cwd, MODULE_NAME, config_file));
	ASSERT_EQ(0, fclose(fp));

	ASSERT_EQ(PAM_SUCCESS,
		  pam_start_confdir(service_file, "root", &conv, ".", &pamh));
	ASSERT_NE(NULL, pamh);
	ASSERT_EQ(PAM_PERM_DENIED, pam_authenticate(pamh, 0));
	ASSERT_EQ(PAM_PERM_DENIED, pam_setcred(pamh, 0));
	ASSERT_EQ(PAM_PERM_DENIED, pam_acct_mgmt(pamh, 0));
	ASSERT_EQ(PAM_MODULE_UNKNOWN, pam_chauthtok(pamh, 0));
	ASSERT_EQ(PAM_MODULE_UNKNOWN, pam_open_session(pamh, 0));
	ASSERT_EQ(PAM_MODULE_UNKNOWN, pam_close_session(pamh, 0));
	ASSERT_EQ(PAM_SUCCESS, pam_end(pamh, 0));
	pamh = NULL;

	/*
	 * conf= specifies an existing file,
	 * PAM_IGNORE -> PAM_SUCCESS
	 */
	ASSERT_NE(NULL, fp = fopen(service_file, "w"));
	ASSERT_LT(0,
		  fprintf(fp, "#%%PAM-1.0\n"
			      "auth required %s/" LTDIR "%s.so conf=%s\n"
			      "auth required %s/../pam_permit/" LTDIR "pam_permit.so\n"
			      "account required %s/" LTDIR "%s.so conf=%s\n"
			      "account required %s/../pam_permit/" LTDIR "pam_permit.so\n"
			      "password required %s/" LTDIR "%s.so conf=%s\n"
			      "password required %s/../pam_permit/" LTDIR "pam_permit.so\n"
			      "session required %s/" LTDIR "%s.so conf=%s\n"
			      "session required %s/../pam_permit/" LTDIR "pam_permit.so\n",
			  cwd, MODULE_NAME, config_file, cwd,
			  cwd, MODULE_NAME, config_file, cwd,
			  cwd, MODULE_NAME, config_file, cwd,
			  cwd, MODULE_NAME, config_file, cwd));
	ASSERT_EQ(0, fclose(fp));

	ASSERT_EQ(PAM_SUCCESS,
		  pam_start_confdir(service_file, "root", &conv, ".", &pamh));
	ASSERT_NE(NULL, pamh);
	ASSERT_EQ(PAM_SUCCESS, pam_authenticate(pamh, 0));
	ASSERT_EQ(PAM_SUCCESS, pam_setcred(pamh, 0));
	ASSERT_EQ(PAM_SUCCESS, pam_acct_mgmt(pamh, 0));
	ASSERT_EQ(PAM_MODULE_UNKNOWN, pam_chauthtok(pamh, 0));
	ASSERT_EQ(PAM_MODULE_UNKNOWN, pam_open_session(pamh, 0));
	ASSERT_EQ(PAM_MODULE_UNKNOWN, pam_close_session(pamh, 0));
	ASSERT_EQ(PAM_SUCCESS, pam_end(pamh, 0));
	pamh = NULL;

	/*
	 * conf= specifies a missing file,
	 * PAM_IGNORE -> PAM_PERM_DENIED
	 */
	ASSERT_NE(NULL, fp = fopen(service_file, "w"));
	ASSERT_LT(0,
		  fprintf(fp, "#%%PAM-1.0\n"
			      "auth required %s/" LTDIR "%s.so conf=%s\n"
			      "account required %s/" LTDIR "%s.so conf=%s\n"
			      "password required %s/" LTDIR "%s.so conf=%s\n"
			      "session required %s/" LTDIR "%s.so conf=%s\n",
			  cwd, MODULE_NAME, missing_file,
			  cwd, MODULE_NAME, missing_file,
			  cwd, MODULE_NAME, missing_file,
			  cwd, MODULE_NAME, missing_file));
	ASSERT_EQ(0, fclose(fp));

	ASSERT_EQ(PAM_SUCCESS,
		  pam_start_confdir(service_file, "root", &conv, ".", &pamh));
	ASSERT_NE(NULL, pamh);
	ASSERT_EQ(PAM_SERVICE_ERR, pam_authenticate(pamh, 0));
	ASSERT_EQ(PAM_PERM_DENIED, pam_setcred(pamh, 0));
	ASSERT_EQ(PAM_SERVICE_ERR, pam_acct_mgmt(pamh, 0));
	ASSERT_EQ(PAM_MODULE_UNKNOWN, pam_chauthtok(pamh, 0));
	ASSERT_EQ(PAM_MODULE_UNKNOWN, pam_open_session(pamh, 0));
	ASSERT_EQ(PAM_MODULE_UNKNOWN, pam_close_session(pamh, 0));
	ASSERT_EQ(PAM_SUCCESS, pam_end(pamh, 0));
	pamh = NULL;

	/* cleanup */
	ASSERT_EQ(0, unlink(config_file));
	ASSERT_EQ(0, unlink(service_file));

	free(cwd);

	return 0;
}