Update translations from GNOME
[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 2
34 #define OPENCONNECT_API_VERSION_MINOR 1
35
36 /*
37  * API version 2.1:
38  *  - Add openconnect_set_reported_os()
39  *  - Add openconnect_set_stoken_mode(), openconnect_has_stoken_support()
40  *
41  * API version 2.0:
42  *  - OPENCONNECT_X509 is now an opaque type.
43  *  - Add openconnect_has_pkcs11_support(), openconnect_has_tss_blob_support()
44  *  - Rename openconnect_init_openssl() -> openconnect_init_ssl()
45  *  - Rename openconnect_vpninfo_new_with_cbdata() -> openconnect_vpninfo_new()
46  *    and kill the old openconnect_vpninfo_new() and its callback types.
47  *
48  * API version 1.5:
49  *  - Add openconnect_get_cert_details(), openconnect_get_cert_DER().
50  *
51  * API version 1.4:
52  *  - Add openconnect_set_cancel_fd()
53  *
54  * API version 1.3:
55  *  - Add openconnect_set_cert_expiry_warning() to change from default 60 days
56  *
57  * API version 1.2:
58  *  - Add openconnect_vpninfo_new_with_cbdata()
59  *
60  * API version 1.1:
61  *  - Add openconnect_vpninfo_free()
62  *
63  * API version 1.0:
64  *  - Initial version
65  */
66
67 /* Before API version 1.4 (OpenConnect 3.19) this macro didn't exist.
68  * Somewhat ironic, that the API version check itself needs to be
69  * conditionally used depending on the API version. A very simple way
70  * for users to handle this with an approximately correct answer is
71  *   #include <openconnect.h>
72  *   #ifndef OPENCONNECT_CHECK_VER
73  *   #define OPENCONNECT_CHECK_VER(x,y) 0
74  *   #endif
75  */
76 #define OPENCONNECT_CHECK_VER(maj,min) \
77         (OPENCONNECT_API_VERSION_MAJOR > (maj) || \
78         (OPENCONNECT_API_VERSION_MAJOR == (maj) && \
79          OPENCONNECT_API_VERSION_MINOR >= (min)))
80
81 /****************************************************************************/
82
83 /* Authentication form processing */
84
85 #define OC_FORM_OPT_TEXT        1
86 #define OC_FORM_OPT_PASSWORD    2
87 #define OC_FORM_OPT_SELECT      3
88 #define OC_FORM_OPT_HIDDEN      4
89 #define OC_FORM_OPT_STOKEN      5
90
91 /* char * fields are static (owned by XML parser) and don't need to be
92    freed by the form handling code -- except for value, which for TEXT
93    and PASSWORD options is allocated by process_form() when
94    interacting with the user and must be freed. */
95 struct oc_form_opt {
96         struct oc_form_opt *next;
97         int type;
98         char *name;
99         char *label;
100         char *value;
101 };
102
103 /* All fields are static, owned by the XML parser */
104 struct oc_choice {
105         char *name;
106         char *label;
107         char *auth_type;
108         char *override_name;
109         char *override_label;
110 };
111
112 struct oc_form_opt_select {
113         struct oc_form_opt form;
114         int nr_choices;
115         struct oc_choice choices[0];
116 };
117
118 /* All char * fields are static, owned by the XML parser */
119 struct oc_auth_form {
120         char *banner;
121         char *message;
122         char *error;
123         char *auth_id;
124         char *method;
125         char *action;
126         struct oc_form_opt *opts;
127 };
128
129 /****************************************************************************/
130
131 #define PRG_ERR         0
132 #define PRG_INFO        1
133 #define PRG_DEBUG       2
134 #define PRG_TRACE       3
135
136 struct openconnect_info;
137
138 #define OPENCONNECT_X509 void
139
140 /* Unless otherwise specified, all functions which set strings will take
141    ownership of those strings and the library will free them later in
142    openconnect_vpninfo_free() */
143
144
145 /* The buffer 'buf' must be at least 41 bytes. It will receive a hex string
146    with trailing NUL, representing the SHA1 fingerprint of the certificate. */
147 int openconnect_get_cert_sha1(struct openconnect_info *vpninfo,
148                               OPENCONNECT_X509 *cert, char *buf);
149 char *openconnect_get_cert_details(struct openconnect_info *vpninfo,
150                                    OPENCONNECT_X509 *cert);
151 /* Returns the length of the created DER output, in a newly-allocated buffer
152    that will need to be freed by the caller. */
153 int openconnect_get_cert_DER(struct openconnect_info *vpninfo,
154                              OPENCONNECT_X509 *cert, unsigned char **buf);
155 int openconnect_set_http_proxy(struct openconnect_info *vpninfo, char *proxy);
156 int openconnect_passphrase_from_fsid(struct openconnect_info *vpninfo);
157 int openconnect_obtain_cookie(struct openconnect_info *vpninfo);
158 void openconnect_init_ssl(void);
159
160 char *openconnect_get_vpn_name (struct openconnect_info *);
161 char *openconnect_get_hostname (struct openconnect_info *);
162 void openconnect_set_hostname (struct openconnect_info *, char *);
163 char *openconnect_get_urlpath (struct openconnect_info *);
164 void openconnect_set_urlpath (struct openconnect_info *, char *);
165
166 /* This function does *not* take ownership of the string; it is parsed
167    and then discarded. */
168 int openconnect_set_stoken_mode (struct openconnect_info *,
169                                  int use_stoken, const char *token_str);
170
171 /* This function does *not* take ownership of the string; it's copied
172    into a static buffer in the vpninfo. The size must be 41 bytes,
173    since that's the size of a 20-byte SHA1 represented as hex with
174    a trailing NUL. */
175 void openconnect_set_xmlsha1 (struct openconnect_info *, const char *, int size);
176
177 void openconnect_set_cafile (struct openconnect_info *, char *);
178 void openconnect_setup_csd (struct openconnect_info *, uid_t, int silent, char *wrapper);
179 int openconnect_set_reported_os (struct openconnect_info *, const char *os);
180 void openconnect_set_client_cert (struct openconnect_info *, char *cert, char *sslkey);
181
182 /* This is *not* yours and must not be destroyed with X509_free(). It
183  * will be valid when a cookie has been obtained successfully, and will
184  * be valid until the connection is destroyed or another attempt it made
185  * to use it. */
186 OPENCONNECT_X509 *openconnect_get_peer_cert (struct openconnect_info *);
187
188 int openconnect_get_port (struct openconnect_info *);
189 char *openconnect_get_cookie (struct openconnect_info *);
190 void openconnect_clear_cookie (struct openconnect_info *);
191
192 void openconnect_reset_ssl (struct openconnect_info *vpninfo);
193 int openconnect_parse_url (struct openconnect_info *vpninfo, char *url);
194 void openconnect_set_cert_expiry_warning (struct openconnect_info *vpninfo,
195                                           int seconds);
196
197 /* If this is set, then openconnect_obtain_cookie() will abort and return
198    failure if the file descriptor is readable. Typically a user may create
199    a pair of pipes with the pipe(2) system call, hand the readable one to
200    this function, and then write a byte to the other end if it ever wants
201    to cancel the connection. This way, a multi-threaded UI (which will be
202    running openconnect_obtain_cookie() in a separate thread since it blocks)
203    has the ability to cancel that call, reap its thread and free the
204    vpninfo structure (or retry). An 'fd' argument of -1 will render the
205    cancellation mechanism inactive. */
206 void openconnect_set_cancel_fd (struct openconnect_info *vpninfo, int fd);
207
208 const char *openconnect_get_version(void);
209
210 /* The first (privdata) argument to each of these functions is either
211    the privdata argument provided to openconnect_vpninfo_new_with_cbdata(),
212    or if that argument was NULL then it'll be the vpninfo itself. */
213
214 /* When the server's certificate fails validation via the normal means,
215    this function is called with the offending certificate along with 
216    a textual reason for the failure (which may not be translated, if
217    it comes directly from OpenSSL, but will be if it is rejected for
218    "certificate does not match hostname", because that check is done
219    in OpenConnect and *is* translated). The function shall return zero
220    if the certificate is (or has in the past been) explicitly accepted
221    by the user, and non-zero to abort the connection. */
222 typedef int (*openconnect_validate_peer_cert_vfn) (void *privdata,
223                                                    OPENCONNECT_X509 *cert,
224                                                    const char *reason);
225 /* On a successful connection, the server may provide us with a new XML
226    configuration file. This contains the list of servers that can be
227    chosen by the user to connect to, amongst other stuff that we mostly
228    ignore. By "new", we mean that the SHA1 indicated by the server does
229    not match the SHA1 set with the openconnect_set_xmlsha1() above. If
230    they don't match, or openconnect_set_xmlsha1() has not been called,
231    then the new XML is downloaded and this function is invoked. */
232 typedef int (*openconnect_write_new_config_vfn) (void *privdata, char *buf,
233                                                 int buflen);
234 /* Handle an authentication form, requesting input from the user. 
235  * Return value:
236  *  < 0, on error
237  *  = 0, when form was parsed and POST required
238  *  = 1, when response was cancelled by user
239  */
240 typedef int (*openconnect_process_auth_form_vfn) (void *privdata,
241                                                  struct oc_auth_form *form);
242 /* Logging output which the user *may* want to see. */
243 typedef void __attribute__ ((format(printf, 3, 4)))
244                 (*openconnect_progress_vfn) (void *privdata, int level,
245                                             const char *fmt, ...);
246 struct openconnect_info *openconnect_vpninfo_new (char *useragent,
247                                                   openconnect_validate_peer_cert_vfn,
248                                                   openconnect_write_new_config_vfn,
249                                                   openconnect_process_auth_form_vfn,
250                                                   openconnect_progress_vfn,
251                                                   void *privdata);
252 void openconnect_vpninfo_free (struct openconnect_info *vpninfo);
253
254 /* SSL certificate capabilities. openconnect_has_pkcs11_support() means that we
255    can accept PKCS#11 URLs in place of filenames, for the certificate and key. */
256 int openconnect_has_pkcs11_support(void);
257
258 /* The OpenSSL TPM ENGINE stores keys in a PEM file labelled with the string
259    -----BEGIN TSS KEY BLOB-----. GnuTLS may learn to support this format too,
260    in the near future. */
261 int openconnect_has_tss_blob_support(void);
262
263 /* Software token capabilities. */
264 int openconnect_has_stoken_support(void);
265
266 #endif /* __OPENCONNECT_H__ */