pamtest: Capture info and error messages from conversation
[slow/pam_wrapper.git] / include / 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
25 /* operations */
26 enum pamtest_ops {
27         /* These operations correspond to libpam ops */
28         PAMTEST_AUTHENTICATE,
29         PAMTEST_SETCRED,
30         PAMTEST_ACCOUNT,
31         PAMTEST_OPEN_SESSION,
32         PAMTEST_CLOSE_SESSION,
33         PAMTEST_CHAUTHTOK,
34
35         /* These operation affect test output */
36         PAMTEST_GETENVLIST,     /* Call pam_getenvlist. */
37         PAMTEST_KEEPHANDLE,     /* Don't call pam_end() but return handle */
38
39         /* The two below can't be set by API user, but are useful if pam_start()
40          * or pam_end() fails and the API user wants to find out what happened
41          * with pamtest_failed_case()
42          */
43         PAMTEST_START,
44         PAMTEST_END,
45
46         /* Boundary.. */
47         PAMTEST_SENTINEL,
48 };
49
50 struct pamtest_case {
51         enum pamtest_ops pam_operation;   /* The pam operation to run */
52         int expected_rv;                  /* What we expect the op to return */
53         int flags;                        /* Extra flags to pass to the op */
54
55         int op_rv;                        /* What the op really returns */
56
57         union {
58                 char **envlist;         /* output of PAMTEST_ENVLIST */
59                 pam_handle_t *ph;       /* output of PAMTEST_KEEPHANDLE */
60         } case_out;             /* depends on pam_operation, mostly unused */
61 };
62
63 #define PAMTEST_CASE_INIT 0, 0, { .envlist = NULL }
64 #define PAMTEST_CASE_SENTINEL PAMTEST_SENTINEL, 0, PAMTEST_CASE_INIT
65
66 enum pamtest_err {
67         PAMTEST_ERR_OK,         /* Testcases returns correspond with input */
68         PAMTEST_ERR_START,      /* pam_start() failed */
69         PAMTEST_ERR_CASE,       /* A testcase failed. Use pamtest_failed_case */
70         PAMTEST_ERR_OP,         /* Could not run a test case */
71         PAMTEST_ERR_END,        /* pam_end failed */
72         PAMTEST_ERR_KEEPHANDLE, /* Handled internally */
73         PAMTEST_ERR_INTERNAL,   /* Internal error - bad input or similar */
74 };
75
76 typedef int (*pam_conv_fn)(int num_msg,
77                            const struct pam_message **msg,
78                            struct pam_response **resp,
79                            void *appdata_ptr);
80
81 enum pamtest_err pamtest_ex(const char *service,
82                             const char *user,
83                             pam_conv_fn conv_fn,
84                             void *conv_userdata,
85                             struct pamtest_case *test_cases);
86
87 void pamtest_free_env(char **envlist);
88
89 const struct pamtest_case *pamtest_failed_case(struct pamtest_case *test_cases);
90
91 struct pamtest_conv_data {
92         const char **in_echo_off;
93         const char **in_echo_on;
94
95         char **out_err;
96         char **out_info;
97 };
98
99 enum pamtest_err pamtest(const char *service,
100                          const char *user,
101                          struct pamtest_conv_data *conv_data,
102                          struct pamtest_case *test_cases);
103
104 #endif /* __LIBPAMTEST_H_ */