pam_wrapper: include/libpamtest.h Source File

pam_wrapper

libpamtest.h
1 /*
2 * Copyright (c) 2015 Andreas Schneider <asn@samba.org>
3 * Copyright (c) 2015 Jakub Hrozek <jakub.hrozek@posteo.se>
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19 #ifndef __LIBPAMTEST_H_
20 #define __LIBPAMTEST_H_
21
22 #include <stdint.h>
23 #include <security/pam_appl.h>
24
34 enum pamtest_ops {
36 PAMTEST_AUTHENTICATE,
38 PAMTEST_SETCRED,
40 PAMTEST_ACCOUNT,
42 PAMTEST_OPEN_SESSION,
44 PAMTEST_CLOSE_SESSION,
46 PAMTEST_CHAUTHTOK,
47
52 PAMTEST_GETENVLIST = 20,
57 PAMTEST_KEEPHANDLE,
58 };
59
60
67 struct pam_testcase {
68 enum pamtest_ops pam_operation; /* The pam operation to run */
69 int expected_rv; /* What we expect the op to return */
70 int flags; /* Extra flags to pass to the op */
71
72 int op_rv; /* What the op really returns */
73
74 union {
75 char **envlist; /* output of PAMTEST_ENVLIST */
76 pam_handle_t *ph; /* output of PAMTEST_KEEPHANDLE */
77 } case_out; /* depends on pam_operation, mostly unused */
78 };
79
81 #define pam_test(op, expected) { op, expected, 0, 0, { .envlist = NULL } }
82
83 #define pam_test_flags(op, expected, flags) { op, expected, flags, 0, { .envlist = NULL } }
84
88 enum pamtest_err {
90 PAMTEST_ERR_OK,
92 PAMTEST_ERR_START,
94 PAMTEST_ERR_CASE,
96 PAMTEST_ERR_OP,
98 PAMTEST_ERR_END,
100 PAMTEST_ERR_KEEPHANDLE,
102 PAMTEST_ERR_INTERNAL,
103 };
104
111 typedef int (*pam_conv_fn)(int num_msg,
112 const struct pam_message **msg,
113 struct pam_response **resp,
114 void *appdata_ptr);
115
120 struct pamtest_conv_data {
125 const char **in_echo_off;
130 const char **in_echo_on;
131
135 char **out_err;
139 char **out_info;
140 };
141
142 #ifdef DOXYGEN
143
174 enum pamtest_err run_pamtest_conv(const char *service,
175 const char *user,
176 pam_conv_fn conv_fn,
177 void *conv_userdata,
178 struct pam_testcase test_cases[]);
179 #else
180 #define run_pamtest_conv(service, user, conv_fn, conv_data, test_cases) \
181 _pamtest_conv(service, user, conv_fn, conv_data, test_cases, sizeof(test_cases)/sizeof(test_cases[0])
182 #endif
183
184 #ifdef DOXYGEN
185
214 enum pamtest_err run_pamtest(const char *service,
215 const char *user,
216 struct pamtest_conv_data *conv_data,
217 struct pam_testcase test_cases[]);
218 #else
219 #define run_pamtest(service, user, conv_data, test_cases) \
220 _pamtest(service, user, conv_data, test_cases, sizeof(test_cases)/sizeof(test_cases[0]))
221 #endif
222
223 #ifdef DOXYGEN
224
236 const struct pam_testcase *pamtest_failed_case(struct pam_testcase *test_cases);
237 #else
238 #define pamtest_failed_case(test_cases) \
239 _pamtest_failed_case(test_cases, sizeof(test_cases) / sizeof(test_cases[0]))
240 #endif
241
249 const char *pamtest_strerror(enum pamtest_err perr);
250
256 void pamtest_free_env(char **envlist);
257
258
259 /* Internal function protypes */
260 enum pamtest_err _pamtest_conv(const char *service,
261 const char *user,
262 pam_conv_fn conv_fn,
263 void *conv_userdata,
264 struct pam_testcase test_cases[],
265 size_t num_test_cases);
266
267 enum pamtest_err _pamtest(const char *service,
268 const char *user,
269 struct pamtest_conv_data *conv_data,
270 struct pam_testcase test_cases[],
271 size_t num_test_cases);
272
273 const struct pam_testcase *_pamtest_failed_case(struct pam_testcase test_cases[],
274 size_t num_test_cases);
275
278 #endif /* __LIBPAMTEST_H_ */
PAMTEST_ERR_KEEPHANDLE
@ PAMTEST_ERR_KEEPHANDLE
Handled internally.
Definition: libpamtest.h:100
PAMTEST_ERR_OP
@ PAMTEST_ERR_OP
Could not run a test case.
Definition: libpamtest.h:96
pam_testcase
The PAM testcase struction.
Definition: libpamtest.h:67
pamtest_conv_data::in_echo_off
const char ** in_echo_off
When the conversation function receives PAM_PROMPT_ECHO_OFF, it reads the auth token from the in_echo...
Definition: libpamtest.h:125
PAMTEST_CLOSE_SESSION
@ PAMTEST_CLOSE_SESSION
run pam_close_session() to end a PAM session
Definition: libpamtest.h:44
PAMTEST_KEEPHANDLE
@ PAMTEST_KEEPHANDLE
This will prevent calling pam_end() and will just return the PAM handle in case_out....
Definition: libpamtest.h:57
pamtest_conv_data::out_info
char ** out_info
Captures messages through PAM_ERROR_MSG.
Definition: libpamtest.h:139
PAMTEST_ERR_INTERNAL
@ PAMTEST_ERR_INTERNAL
Internal error - bad input or similar.
Definition: libpamtest.h:102
PAMTEST_CHAUTHTOK
@ PAMTEST_CHAUTHTOK
run pam_chauthtok() to update the authentication token
Definition: libpamtest.h:46
pamtest_conv_data::in_echo_on
const char ** in_echo_on
When the conversation function receives PAM_PROMPT_ECHO_ON, it reads the input from the in_echo_off a...
Definition: libpamtest.h:130
PAMTEST_ACCOUNT
@ PAMTEST_ACCOUNT
run pam_acct_mgmt() to validate the PAM account
Definition: libpamtest.h:40
PAMTEST_SETCRED
@ PAMTEST_SETCRED
run pam_setcred() to establish/delete user credentials
Definition: libpamtest.h:38
PAMTEST_GETENVLIST
@ PAMTEST_GETENVLIST
If this option is set the test will call pam_getenvlist() and copy the environment into case_out....
Definition: libpamtest.h:52
PAMTEST_ERR_OK
@ PAMTEST_ERR_OK
Testcases returns correspond with input.
Definition: libpamtest.h:90
pamtest_conv_data::out_err
char ** out_err
Captures messages through PAM_TEXT_INFO.
Definition: libpamtest.h:135
run_pamtest
enum pamtest_err run_pamtest(const char *service, const char *user, struct pamtest_conv_data *conv_data, struct pam_testcase test_cases[])
Run libpamtest test cases.
PAMTEST_ERR_START
@ PAMTEST_ERR_START
pam_start() failed
Definition: libpamtest.h:92
pamtest_ops
pamtest_ops
The enum which describes the operations performed by pamtest().
Definition: libpamtest.h:34
PAMTEST_AUTHENTICATE
@ PAMTEST_AUTHENTICATE
run pam_authenticate to authenticate the account
Definition: libpamtest.h:36
pamtest_err
pamtest_err
The return code of the pamtest function.
Definition: libpamtest.h:88
pamtest_free_env
void pamtest_free_env(char **envlist)
This frees the string array returned by the PAMTEST_GETENVLIST test.
Definition: libpamtest.c:120
pam_conv_fn
int(* pam_conv_fn)(int num_msg, const struct pam_message **msg, struct pam_response **resp, void *appdata_ptr)
PAM conversation function, defined in pam_conv(3)
Definition: libpamtest.h:111
run_pamtest_conv
enum pamtest_err run_pamtest_conv(const char *service, const char *user, pam_conv_fn conv_fn, void *conv_userdata, struct pam_testcase test_cases[])
Run libpamtest test cases.
PAMTEST_ERR_CASE
@ PAMTEST_ERR_CASE
A testcase failed.
Definition: libpamtest.h:94
PAMTEST_OPEN_SESSION
@ PAMTEST_OPEN_SESSION
run pam_open_session() to start a PAM session
Definition: libpamtest.h:42
pamtest_conv_data
This structure should be used when using run_pamtest, which uses an internal conversation function.
Definition: libpamtest.h:120
pamtest_failed_case
const struct pam_testcase * pamtest_failed_case(struct pam_testcase *test_cases)
Helper you can call if run_pamtest() fails.
pamtest_strerror
const char * pamtest_strerror(enum pamtest_err perr)
return a string representation of libpamtest error code.
Definition: libpamtest.c:152
PAMTEST_ERR_END
@ PAMTEST_ERR_END
pam_end failed
Definition: libpamtest.h:98