Add OPENCONNECT_CHECK_VER() macro for compatibility testing
[platform/upstream/openconnect.git] / openconnect.h
1 /*
2  * OpenConnect (SSL + DTLS) VPN client
3  *
4  * Copyright © 2008-2012 Intel Corporation.
5  * Copyright © 2008 Nick Andrew <nick@nick-andrew.net>
6  *
7  * Author: David Woodhouse <dwmw2@infradead.org>
8  *
9  * This program is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public License
11  * version 2.1, as published by the Free Software Foundation.
12  *
13  * This program is distributed in the hope that it will be useful, but
14  * WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with this library; if not, write to:
20  *
21  *   Free Software Foundation, Inc.
22  *   51 Franklin Street, Fifth Floor,
23  *   Boston, MA 02110-1301 USA
24  */
25
26 #ifndef __OPENCONNECT_H__
27 #define __OPENCONNECT_H__
28
29 #include <stdint.h>
30 #include <sys/types.h>
31 #include <unistd.h>
32
33 #define OPENCONNECT_API_VERSION_MAJOR 1
34 #define OPENCONNECT_API_VERSION_MINOR 4
35
36 /*
37  * API version 1.4:
38  *  - Add openconnect_set_cancel_fd()
39  *
40  * API version 1.3:
41  *  - Add openconnect_set_cert_expiry_warning() to change from default 60 days
42  *
43  * API version 1.2:
44  *  - Add openconnect_vpninfo_new_with_cbdata()
45  *
46  * API version 1.1:
47  *  - Add openconnect_vpninfo_free()
48  *
49  * API version 1.0:
50  *  - Initial version
51  */
52
53 /* Before API version 1.4 (OpenConnect 3.19) this macro didn't exist.
54  * Somewhat ironic, that the API version check itself needs to be
55  * conditionally used depending on the API version. A very simple way
56  * for users to handle this with an approximately correct answer is
57  *   #include <openconnect.h>
58  *   #ifndef OPENCONNECT_CHECK_VER
59  *   #define OPENCONNECT_CHECK_VER(x,y) 0
60  *   #endif
61  */
62 #define OPENCONNECT_CHECK_VER(maj,min) \
63         (OPENCONNECT_API_VERSION_MAJOR > (maj) || \
64         (OPENCONNECT_API_VERSION_MAJOR == (maj) && \
65          OPENCONNECT_API_VERSION_MINOR >= (min)))
66
67 /****************************************************************************/
68
69 /* Authentication form processing */
70
71 #define OC_FORM_OPT_TEXT        1
72 #define OC_FORM_OPT_PASSWORD    2
73 #define OC_FORM_OPT_SELECT      3
74 #define OC_FORM_OPT_HIDDEN      4
75
76 /* char * fields are static (owned by XML parser) and don't need to be
77    freed by the form handling code -- except for value, which for TEXT
78    and PASSWORD options is allocated by process_form() when
79    interacting with the user and must be freed. */
80 struct oc_form_opt {
81         struct oc_form_opt *next;
82         int type;
83         char *name;
84         char *label;
85         char *value;
86 };
87
88 /* All fields are static, owned by the XML parser */
89 struct oc_choice {
90         char *name;
91         char *label;
92         char *auth_type;
93         char *override_name;
94         char *override_label;
95 };
96
97 struct oc_form_opt_select {
98         struct oc_form_opt form;
99         int nr_choices;
100         struct oc_choice choices[0];
101 };
102
103 /* All char * fields are static, owned by the XML parser */
104 struct oc_auth_form {
105         char *banner;
106         char *message;
107         char *error;
108         char *auth_id;
109         char *method;
110         char *action;
111         struct oc_form_opt *opts;
112 };
113
114 /****************************************************************************/
115
116 #define PRG_ERR         0
117 #define PRG_INFO        1
118 #define PRG_DEBUG       2
119 #define PRG_TRACE       3
120
121 struct openconnect_info;
122 /* We don't want to have to pull in OpenSSL stuff just for this */
123 struct x509_st;
124
125
126
127 /* Unless otherwise specified, all functions which set strings will take ownership of those strings
128    and should free them later in openconnect_vpninfo_free() */
129 int openconnect_get_cert_sha1(struct openconnect_info *vpninfo,
130                               struct x509_st *cert, char *buf);
131 int openconnect_set_http_proxy(struct openconnect_info *vpninfo, char *proxy);
132 int openconnect_passphrase_from_fsid(struct openconnect_info *vpninfo);
133 int openconnect_obtain_cookie(struct openconnect_info *vpninfo);
134 void openconnect_init_openssl(void);
135
136 char *openconnect_get_vpn_name (struct openconnect_info *);
137 char *openconnect_get_hostname (struct openconnect_info *);
138 void openconnect_set_hostname (struct openconnect_info *, char *);
139 char *openconnect_get_urlpath (struct openconnect_info *);
140 void openconnect_set_urlpath (struct openconnect_info *, char *);
141
142 /* This function does *not* take ownership of the string; it's copied
143    into a static buffer in the vpninfo */
144 void openconnect_set_xmlsha1 (struct openconnect_info *, char *, int size);
145
146 void openconnect_set_cafile (struct openconnect_info *, char *);
147 void openconnect_setup_csd (struct openconnect_info *, uid_t, int silent, char *wrapper);
148 void openconnect_set_client_cert (struct openconnect_info *, char *cert, char *sslkey);
149 struct x509_st *openconnect_get_peer_cert (struct openconnect_info *);
150 int openconnect_get_port (struct openconnect_info *);
151 char *openconnect_get_cookie (struct openconnect_info *);
152 void openconnect_clear_cookie (struct openconnect_info *);
153
154 void openconnect_reset_ssl (struct openconnect_info *vpninfo);
155 int openconnect_parse_url (struct openconnect_info *vpninfo, char *url);
156 void openconnect_set_cert_expiry_warning (struct openconnect_info *vpninfo,
157                                           int seconds);
158
159 /* If this is set, then openconnect_obtain_cookie() will abort and return
160    failure if the file descriptor is readable. Typically a user may create
161    a pair of pipes with the pipe(2) system call, hand the readable one to
162    this function, and then write a byte to the other end if it ever wants
163    to cancel the connection. This way, a multi-threaded UI (which will be
164    running openconnect_obtain_cookie() in a separate thread since it blocks)
165    has the ability to cancel that call, reap its thread and free the
166    vpninfo structure (or retry). An 'fd' argument of -1 will render the
167    cancellation mechanism inactive. */
168 void openconnect_set_cancel_fd (struct openconnect_info *vpninfo, int fd);
169
170 const char *openconnect_get_version(void);
171
172 /* The first (privdata) argument to each of these functions is either
173    the privdata argument provided to openconnect_vpninfo_new_with_cbdata(),
174    or if that argument was NULL then it'll be the vpninfo itself. */
175
176 /* When the server's certificate fails validation via the normal means,
177    this function is called with the offending certificate along with 
178    a textual reason for the failure (which may not be translated, if
179    it comes directly from OpenSSL, but will be if it is rejected for
180    "certificate does not match hostname", because that check is done
181    in OpenConnect and *is* translated). The function shall return zero
182    if the certificate is (or has in the past been) explicitly accepted
183    by the user, and non-zero to abort the connection. */
184 typedef int (*openconnect_validate_peer_cert_vfn) (void *privdata,
185                                                   struct x509_st *cert,
186                                                   const char *reason);
187 /* On a successful connection, the server may provide us with a new XML
188    configuration file. This contains the list of servers that can be
189    chosen by the user to connect to, amongst other stuff that we mostly
190    ignore. By "new", we mean that the SHA1 indicated by the server does
191    not match the SHA1 set with the openconnect_set_xmlsha1() above. If
192    they don't match, or openconnect_set_xmlsha1() has not been called,
193    then the new XML is downloaded and this function is invoked. */
194 typedef int (*openconnect_write_new_config_vfn) (void *privdata, char *buf,
195                                                 int buflen);
196 /* Handle an authentication form, requesting input from the user. */
197 typedef int (*openconnect_process_auth_form_vfn) (void *privdata,
198                                                  struct oc_auth_form *form);
199 /* Logging output which the user *may* want to see. */
200 typedef void __attribute__ ((format(printf, 3, 4)))
201                 (*openconnect_progress_vfn) (void *privdata, int level,
202                                             const char *fmt, ...);
203 struct openconnect_info *openconnect_vpninfo_new_with_cbdata (char *useragent,
204                                                   openconnect_validate_peer_cert_vfn,
205                                                   openconnect_write_new_config_vfn,
206                                                   openconnect_process_auth_form_vfn,
207                                                   openconnect_progress_vfn,
208                                                   void *privdata);
209 void openconnect_vpninfo_free (struct openconnect_info *vpninfo);
210
211 /* Old prototypes of the callback functions, which used to be passed the
212    vpninfo instead of a caller-provided pointer. You probably don't want to
213    use these; they're here for compatibility only. */
214 typedef int (*openconnect_validate_peer_cert_fn) (struct openconnect_info *,
215                                                   struct x509_st *cert,
216                                                   const char *reason);
217 typedef int (*openconnect_write_new_config_fn) (struct openconnect_info *, char *buf,
218                                                 int buflen);
219 typedef int (*openconnect_process_auth_form_fn) (struct openconnect_info *,
220                                                  struct oc_auth_form *form);
221 typedef void __attribute__ ((format(printf, 3, 4)))
222                 (*openconnect_progress_fn) (struct openconnect_info *, int level,
223                                             const char *fmt, ...);
224 /* Don't use this. Use the _with_cbdata version instead. */
225 struct openconnect_info *openconnect_vpninfo_new (char *useragent,
226                                                   openconnect_validate_peer_cert_fn,
227                                                   openconnect_write_new_config_fn,
228                                                   openconnect_process_auth_form_fn,
229                                                   openconnect_progress_fn);
230
231 #endif /* __OPENCONNECT_H__ */