Commit aaea0801 authored by David Woodhouse's avatar David Woodhouse

Move some helpers out into auth-common.c

Signed-off-by: default avatarDavid Woodhouse <David.Woodhouse@intel.com>
parent 24594a3b
......@@ -23,7 +23,7 @@ openconnect_SOURCES = xml.c main.c
openconnect_CFLAGS = $(AM_CFLAGS) $(SSL_CFLAGS) $(DTLS_SSL_CFLAGS) $(LIBXML2_CFLAGS) $(LIBPROXY_CFLAGS) $(ZLIB_CFLAGS) $(LIBSTOKEN_CFLAGS) $(LIBOATH_CFLAGS) $(LIBPSKC_CFLAGS) $(GSSAPI_CFLAGS) $(INTL_CFLAGS) $(ICONV_CFLAGS) $(LIBPCSCLITE_CFLAGS)
openconnect_LDADD = libopenconnect.la $(LIBXML2_LIBS) $(LIBPROXY_LIBS) $(INTL_LIBS) $(ICONV_LIBS)
library_srcs = ssl.c http.c library.c compat.c lzs.c mainloop.c script.c ntlm.c digest.c
library_srcs = ssl.c http.c auth-common.c library.c compat.c lzs.c mainloop.c script.c ntlm.c digest.c
lib_srcs_cisco = auth.c cstp.c dtls.c
lib_srcs_gnutls = gnutls.c gnutls_pkcs12.c gnutls_tpm.c
lib_srcs_openssl = openssl.c openssl-pkcs11.c
......
/*
* OpenConnect (SSL + DTLS) VPN client
*
* Copyright © 2008-2015 Intel Corporation.
*
* Author: David Woodhouse <dwmw2@infradead.org>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public License
* version 2.1, as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*/
#include <config.h>
#include <unistd.h>
#include <fcntl.h>
#include <time.h>
#include <string.h>
#include <ctype.h>
#include <errno.h>
#include <stdlib.h>
#include <stdio.h>
#include <sys/types.h>
#include <stdarg.h>
#include "openconnect-internal.h"
int xmlnode_is_named(xmlNode *xml_node, const char *name)
{
return !strcmp((char *)xml_node->name, name);
}
int xmlnode_get_prop(xmlNode *xml_node, const char *name, char **var)
{
char *str = (char *)xmlGetProp(xml_node, (unsigned char *)name);
if (!str)
return -ENOENT;
free(*var);
*var = str;
return 0;
}
int xmlnode_match_prop(xmlNode *xml_node, const char *name, const char *match)
{
char *str = (char *)xmlGetProp(xml_node, (unsigned char *)name);
int ret = 0;
if (!str)
return -ENOENT;
if (strcmp(str, match))
ret = -EEXIST;
free(str);
return ret;
}
int append_opt(struct oc_text_buf *body, char *opt, char *name)
{
if (buf_error(body))
return buf_error(body);
if (body->pos)
buf_append(body, "&");
buf_append_urlencoded(body, opt);
buf_append(body, "=");
buf_append_urlencoded(body, name);
return 0;
}
int append_form_opts(struct openconnect_info *vpninfo,
struct oc_auth_form *form, struct oc_text_buf *body)
{
struct oc_form_opt *opt;
int ret;
for (opt = form->opts; opt; opt = opt->next) {
ret = append_opt(body, opt->name, opt->_value);
if (ret)
return ret;
}
return 0;
}
void free_opt(struct oc_form_opt *opt)
{
/* for SELECT options, opt->value is a pointer to oc_choice->name */
if (opt->type != OC_FORM_OPT_SELECT)
free(opt->_value);
else {
struct oc_form_opt_select *sel = (void *)opt;
int i;
for (i = 0; i < sel->nr_choices; i++) {
free(sel->choices[i]->name);
free(sel->choices[i]->label);
free(sel->choices[i]->auth_type);
free(sel->choices[i]->override_name);
free(sel->choices[i]->override_label);
free(sel->choices[i]);
}
free(sel->choices);
}
free(opt->name);
free(opt->label);
free(opt);
}
void free_auth_form(struct oc_auth_form *form)
{
if (!form)
return;
while (form->opts) {
struct oc_form_opt *tmp = form->opts->next;
free_opt(form->opts);
form->opts = tmp;
}
free(form->error);
free(form->message);
free(form->banner);
free(form->auth_id);
free(form->method);
free(form->action);
free(form);
}
......@@ -68,60 +68,6 @@ int openconnect_set_option_value(struct oc_form_opt *opt, const char *value)
return 0;
}
static int append_opt(struct oc_text_buf *body, char *opt, char *name)
{
if (buf_error(body))
return buf_error(body);
if (body->pos)
buf_append(body, "&");
buf_append_urlencoded(body, opt);
buf_append(body, "=");
buf_append_urlencoded(body, name);
return 0;
}
static int append_form_opts(struct openconnect_info *vpninfo,
struct oc_auth_form *form, struct oc_text_buf *body)
{
struct oc_form_opt *opt;
int ret;
for (opt = form->opts; opt; opt = opt->next) {
ret = append_opt(body, opt->name, opt->_value);
if (ret)
return ret;
}
return 0;
}
static void free_opt(struct oc_form_opt *opt)
{
/* for SELECT options, opt->value is a pointer to oc_choice->name */
if (opt->type != OC_FORM_OPT_SELECT)
free(opt->_value);
else {
struct oc_form_opt_select *sel = (void *)opt;
int i;
for (i = 0; i < sel->nr_choices; i++) {
free(sel->choices[i]->name);
free(sel->choices[i]->label);
free(sel->choices[i]->auth_type);
free(sel->choices[i]->override_name);
free(sel->choices[i]->override_label);
free(sel->choices[i]);
}
free(sel->choices);
}
free(opt->name);
free(opt->label);
free(opt);
}
static int prop_equals(xmlNode *xml_node, const char *name, const char *value)
{
char *tmp = (char *)xmlGetProp(xml_node, (unsigned char *)name);
......@@ -363,38 +309,6 @@ static char *xmlnode_msg(xmlNode *xml_node)
return result;
}
static int xmlnode_is_named(xmlNode *xml_node, const char *name)
{
return !strcmp((char *)xml_node->name, name);
}
static int xmlnode_get_prop(xmlNode *xml_node, const char *name, char **var)
{
char *str = (char *)xmlGetProp(xml_node, (unsigned char *)name);
if (!str)
return -ENOENT;
free(*var);
*var = str;
return 0;
}
static int xmlnode_match_prop(xmlNode *xml_node, const char *name, const char *match)
{
char *str = (char *)xmlGetProp(xml_node, (unsigned char *)name);
int ret = 0;
if (!str)
return -ENOENT;
if (strcmp(str, match))
ret = -EEXIST;
free(str);
return ret;
}
static int xmlnode_get_text(xmlNode *xml_node, const char *name, char **var)
{
char *str;
......@@ -600,24 +514,6 @@ static void parse_config_node(struct openconnect_info *vpninfo, xmlNode *xml_nod
}
}
static void free_auth_form(struct oc_auth_form *form)
{
if (!form)
return;
while (form->opts) {
struct oc_form_opt *tmp = form->opts->next;
free_opt(form->opts);
form->opts = tmp;
}
free(form->error);
free(form->message);
free(form->banner);
free(form->auth_id);
free(form->method);
free(form->action);
free(form);
}
/* Return value:
* < 0, on error
* = 0, on success; *form is populated
......
......@@ -1144,19 +1144,10 @@ int cstp_bye(struct openconnect_info *vpninfo, const char *reason)
void cstp_common_headers(struct openconnect_info *vpninfo, struct oc_text_buf *buf)
{
struct oc_vpn_option *opt;
http_common_headers(vpninfo, buf);
buf_append(buf, "Host: %s\r\n", vpninfo->hostname);
buf_append(buf, "User-Agent: %s\r\n", vpninfo->useragent);
buf_append(buf, "Accept: */*\r\n");
buf_append(buf, "Accept-Encoding: identity\r\n");
if (vpninfo->cookies) {
buf_append(buf, "Cookie: ");
for (opt = vpninfo->cookies; opt; opt = opt->next)
buf_append(buf, "%s=%s%s", opt->option,
opt->value, opt->next ? "; " : "\r\n");
}
buf_append(buf, "X-Transcend-Version: 1\r\n");
if (vpninfo->xmlpost) {
buf_append(buf, "X-Aggregate-Auth: 1\r\n");
......
......@@ -1705,3 +1705,18 @@ int openconnect_set_http_proxy(struct openconnect_info *vpninfo,
free(url);
return ret;
}
void http_common_headers(struct openconnect_info *vpninfo, struct oc_text_buf *buf)
{
struct oc_vpn_option *opt;
buf_append(buf, "Host: %s\r\n", vpninfo->hostname);
buf_append(buf, "User-Agent: %s\r\n", vpninfo->useragent);
if (vpninfo->cookies) {
buf_append(buf, "Cookie: ");
for (opt = vpninfo->cookies; opt; opt = opt->next)
buf_append(buf, "%s=%s%s", opt->option,
opt->value, opt->next ? "; " : "\r\n");
}
}
......@@ -785,6 +785,16 @@ int do_gen_yubikey_code(struct openconnect_info *vpninfo,
/* auth.c */
int cstp_obtain_cookie(struct openconnect_info *vpninfo);
/* auth-common.c */
int xmlnode_is_named(xmlNode *xml_node, const char *name);
int xmlnode_get_prop(xmlNode *xml_node, const char *name, char **var);
int xmlnode_match_prop(xmlNode *xml_node, const char *name, const char *match);
int append_opt(struct oc_text_buf *body, char *opt, char *name);
int append_form_opts(struct openconnect_info *vpninfo,
struct oc_auth_form *form, struct oc_text_buf *body);
void free_opt(struct oc_form_opt *opt);
void free_auth_form(struct oc_auth_form *form);
/* http.c */
struct oc_text_buf *buf_alloc(void);
void dump_buf(struct openconnect_info *vpninfo, char prefix, char *buf);
......@@ -814,6 +824,7 @@ int process_http_response(struct openconnect_info *vpninfo, int connect,
int (*header_cb)(struct openconnect_info *, char *, char *),
struct oc_text_buf *body);
int handle_redirect(struct openconnect_info *vpninfo);
void http_common_headers(struct openconnect_info *vpninfo, struct oc_text_buf *buf);
/* ntlm.c */
int ntlm_authorization(struct openconnect_info *vpninfo, struct oc_text_buf *buf);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment