aboutsummaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
Diffstat (limited to 'lib')
-rw-r--r--lib/Makefile2
-rw-r--r--lib/events.h2
-rw-r--r--lib/events_glib.c6
-rw-r--r--lib/http_client.c3
-rw-r--r--lib/misc.c38
-rw-r--r--lib/misc.h83
-rw-r--r--lib/ns_parse.c217
-rw-r--r--lib/proxy.c8
-rw-r--r--lib/proxy.h2
-rw-r--r--lib/ssl_gnutls.c7
-rw-r--r--lib/ssl_sspi.c278
11 files changed, 325 insertions, 321 deletions
diff --git a/lib/Makefile b/lib/Makefile
index f20b3797..40c03215 100644
--- a/lib/Makefile
+++ b/lib/Makefile
@@ -12,7 +12,7 @@ _SRCDIR_ := $(_SRCDIR_)lib/
endif
# [SH] Program variables
-objects = arc.o base64.o $(EVENT_HANDLER) ftutil.o http_client.o ini.o json.o json_util.o md5.o misc.o oauth.o oauth2.o proxy.o sha1.o $(SSL_CLIENT) url.o xmltree.o
+objects = arc.o base64.o $(EVENT_HANDLER) ftutil.o http_client.o ini.o json.o json_util.o md5.o misc.o oauth.o oauth2.o proxy.o sha1.o $(SSL_CLIENT) url.o xmltree.o ns_parse.o
LFLAGS += -r
diff --git a/lib/events.h b/lib/events.h
index 66c4c6b4..e31ddba4 100644
--- a/lib/events.h
+++ b/lib/events.h
@@ -36,11 +36,9 @@
#define _EVENTS_H_
#include <sys/types.h>
-#ifndef _WIN32
#include <sys/socket.h>
#include <netdb.h>
#include <netinet/in.h>
-#endif
#include <glib.h>
#include <gmodule.h>
diff --git a/lib/events_glib.c b/lib/events_glib.c
index 8f53fbbf..369d47f2 100644
--- a/lib/events_glib.c
+++ b/lib/events_glib.c
@@ -30,17 +30,11 @@
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
-#ifndef _WIN32
#include <sys/socket.h>
#include <netdb.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
-#else
-#include "sock.h"
-#define ETIMEDOUT WSAETIMEDOUT
-#define EINPROGRESS WSAEINPROGRESS
-#endif
#include <fcntl.h>
#include <errno.h>
#include "proxy.h"
diff --git a/lib/http_client.c b/lib/http_client.c
index b509c839..18c393f8 100644
--- a/lib/http_client.c
+++ b/lib/http_client.c
@@ -291,6 +291,9 @@ eof:
}
cleanup:
+ /* Avoid g_source_remove warnings */
+ req->inpa = 0;
+
if( req->ssl )
ssl_disconnect( req->ssl );
else
diff --git a/lib/misc.c b/lib/misc.c
index 49c4aa5d..6ceb6f3c 100644
--- a/lib/misc.c
+++ b/lib/misc.c
@@ -419,7 +419,6 @@ signed int do_iconv( char *from_cs, char *to_cs, char *src, char *dst, size_t si
lack of entropy won't halt BitlBee. */
void random_bytes( unsigned char *buf, int count )
{
-#ifndef _WIN32
static int use_dev = -1;
/* Actually this probing code isn't really necessary, is it? */
@@ -469,7 +468,6 @@ void random_bytes( unsigned char *buf, int count )
}
if( !use_dev )
-#endif
{
int i;
@@ -524,7 +522,7 @@ struct ns_srv_reply **srv_lookup( char *service, char *protocol, char *domain )
const unsigned char *buf;
ns_msg nsh;
ns_rr rr;
- int i, n, len, size;
+ int n, len, size;
g_snprintf( name, sizeof( name ), "_%s._%s.%s", service, protocol, domain );
@@ -537,27 +535,20 @@ struct ns_srv_reply **srv_lookup( char *service, char *protocol, char *domain )
n = 0;
while( ns_parserr( &nsh, ns_s_an, n, &rr ) == 0 )
{
- size = ns_rr_rdlen( rr );
+ char name[NS_MAXDNAME];
+
+ if( ns_rr_rdlen( rr ) < 7)
+ break;
+
buf = ns_rr_rdata( rr );
- len = 0;
- for( i = 6; i < size && buf[i]; i += buf[i] + 1 )
- len += buf[i] + 1;
-
- if( i > size )
+ if( dn_expand(querybuf, querybuf + size, &buf[6], name, NS_MAXDNAME) == -1 )
break;
+
+ len = strlen(name) + 1;
reply = g_malloc( sizeof( struct ns_srv_reply ) + len );
- memcpy( reply->name, buf + 7, len );
-
- for( i = buf[6]; i < len && buf[7+i]; i += buf[7+i] + 1 )
- reply->name[i] = '.';
-
- if( i > len )
- {
- g_free( reply );
- break;
- }
+ memcpy( reply->name, name, len );
reply->prio = ( buf[0] << 8 ) | buf[1];
reply->weight = ( buf[2] << 8 ) | buf[3];
@@ -681,7 +672,7 @@ int md5_verify_password( char *password, char *hash )
/* Split commands (root-style, *not* IRC-style). Handles "quoting of"
white\ space in 'various ways'. Returns a NULL-terminated static
char** so watch out with nested use! Definitely not thread-safe. */
-char **split_command_parts( char *command )
+char **split_command_parts( char *command, int limit )
{
static char *cmd[IRC_MAX_ARGS+1];
char *s, q = 0;
@@ -691,11 +682,12 @@ char **split_command_parts( char *command )
cmd[0] = command;
k = 1;
for( s = command; *s && k < IRC_MAX_ARGS; s ++ )
+ {
if( *s == ' ' && !q )
{
*s = 0;
while( *++s == ' ' );
- if( *s == '"' || *s == '\'' )
+ if( k != limit && (*s == '"' || *s == '\'') )
{
q = *s;
s ++;
@@ -703,6 +695,9 @@ char **split_command_parts( char *command )
if( *s )
{
cmd[k++] = s;
+ if (limit && k > limit) {
+ break;
+ }
s --;
}
else
@@ -721,6 +716,7 @@ char **split_command_parts( char *command )
{
q = *s = 0;
}
+ }
/* Full zero-padding for easier argc checking. */
while( k <= IRC_MAX_ARGS )
diff --git a/lib/misc.h b/lib/misc.h
index 059c8231..460cce8c 100644
--- a/lib/misc.h
+++ b/lib/misc.h
@@ -37,6 +37,87 @@ struct ns_srv_reply
char name[];
};
+#ifndef NAMESER_HAS_NS_TYPES
+
+#define NS_MAXDNAME 1025
+#define NS_INT16SZ 2
+#define NS_INT32SZ 4
+
+#define NS_GET16(s, cp) do { \
+ register const unsigned char *t_cp = (const unsigned char*)(cp); \
+ (s) = ((guint16)t_cp[0] << 8) \
+ | ((guint16)t_cp[1]) \
+ ; \
+ (cp) += NS_INT16SZ; \
+} while(0)
+
+#define NS_GET32(s, cp) do { \
+ register const unsigned char *t_cp = (const unsigned char*)(cp); \
+ (s) = ((guint16)t_cp[0] << 24) \
+ | ((guint16)t_cp[1] << 16) \
+ | ((guint16)t_cp[2] << 8) \
+ | ((guint16)t_cp[3]) \
+ ; \
+ (cp) += NS_INT32SZ; \
+} while(0)
+
+#define ns_rr_rdlen(rr) ((rr).rdlength + 0)
+#define ns_rr_rdata(rr) ((rr).rdata + 0)
+
+struct _ns_flagdata { int mask, shift; };
+
+typedef struct __ns_rr {
+ char name[NS_MAXDNAME];
+ guint16 type;
+ guint16 rr_class;
+ guint32 ttl;
+ guint16 rdlength;
+ const unsigned char* rdata;
+} ns_rr;
+
+typedef enum __ns_sect {
+ ns_s_qd = 0,
+ ns_s_zn = 0,
+ ns_s_an = 1,
+ ns_s_pr = 1,
+ ns_s_ns = 2,
+ ns_s_ud = 2,
+ ns_s_ar = 3,
+ ns_s_max =4
+} ns_sect;
+
+typedef struct __ns_msg
+{
+ const unsigned char* _msg;
+ const unsigned char* _eom;
+ guint16 _id;
+ guint16 _flags;
+ guint16 _counts[ns_s_max];
+ const unsigned char* _sections[ns_s_max];
+ ns_sect _sect;
+ int _rrnum;
+ const unsigned char* _msg_ptr;
+} ns_msg;
+
+typedef enum __ns_class {
+ ns_c_invalid = 0,
+ ns_c_in = 1,
+ ns_c_2 = 2,
+ ns_c_chaos = 3,
+ ns_c_hs = 4,
+ ns_c_none = 254,
+ ns_c_any = 255,
+ ns_c_max = 65536
+} ns_class;
+
+
+/* TODO : fill out the rest */
+typedef enum __ns_type {
+ ns_t_srv = 33
+} ns_type;
+
+#endif /* NAMESER_HAS_NS_INITPARSE */
+
G_MODULE_EXPORT void strip_linefeed( gchar *text );
G_MODULE_EXPORT char *add_cr( char *text );
G_MODULE_EXPORT char *strip_newlines(char *source);
@@ -66,7 +147,7 @@ G_MODULE_EXPORT void srv_free( struct ns_srv_reply **srv );
G_MODULE_EXPORT char *word_wrap( const char *msg, int line_len );
G_MODULE_EXPORT gboolean ssl_sockerr_again( void *ssl );
G_MODULE_EXPORT int md5_verify_password( char *password, char *hash );
-G_MODULE_EXPORT char **split_command_parts( char *command );
+G_MODULE_EXPORT char **split_command_parts( char *command, int limit );
G_MODULE_EXPORT char *get_rfc822_header( const char *text, const char *header, int len );
#endif
diff --git a/lib/ns_parse.c b/lib/ns_parse.c
new file mode 100644
index 00000000..ba45d513
--- /dev/null
+++ b/lib/ns_parse.c
@@ -0,0 +1,217 @@
+/*
+ * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
+ * Copyright (c) 1996,1999 by Internet Software Consortium.
+ *
+ * Permission to use, copy, modify, and distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
+ * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ */
+
+#include "bitlbee.h"
+
+#ifndef lint
+static const char rcsid[] = "$Id: ns_parse.c,v 1.10 2009/01/23 19:59:16 each Exp $";
+#endif
+
+#ifdef HAVE_RESOLV_A
+#ifndef HAVE_RESOLV_A_WITH_NS
+/* Import. */
+
+
+#include <sys/types.h>
+
+#include <netinet/in.h>
+#include <arpa/nameser.h>
+
+#include <errno.h>
+#include <resolv.h>
+#include <string.h>
+
+
+/* Forward. */
+
+static void setsection(ns_msg *msg, ns_sect sect);
+
+/* Macros. */
+
+#if !defined(SOLARIS2) || defined(__COVERITY__)
+#define RETERR(err) do { errno = (err); return (-1); } while (0)
+#else
+#define RETERR(err) \
+ do { errno = (err); if (errno == errno) return (-1); } while (0)
+#endif
+
+#define PARSE_FMT_PRESO 0 /* Parse using presentation-format names */
+#define PARSE_FMT_WIRE 1 /* Parse using network-format names */
+
+/* Public. */
+
+/* These need to be in the same order as the nres.h:ns_flag enum. */
+struct _ns_flagdata _ns_flagdata[16] = {
+ { 0x8000, 15 }, /*%< qr. */
+ { 0x7800, 11 }, /*%< opcode. */
+ { 0x0400, 10 }, /*%< aa. */
+ { 0x0200, 9 }, /*%< tc. */
+ { 0x0100, 8 }, /*%< rd. */
+ { 0x0080, 7 }, /*%< ra. */
+ { 0x0040, 6 }, /*%< z. */
+ { 0x0020, 5 }, /*%< ad. */
+ { 0x0010, 4 }, /*%< cd. */
+ { 0x000f, 0 }, /*%< rcode. */
+ { 0x0000, 0 }, /*%< expansion (1/6). */
+ { 0x0000, 0 }, /*%< expansion (2/6). */
+ { 0x0000, 0 }, /*%< expansion (3/6). */
+ { 0x0000, 0 }, /*%< expansion (4/6). */
+ { 0x0000, 0 }, /*%< expansion (5/6). */
+ { 0x0000, 0 }, /*%< expansion (6/6). */
+};
+
+int ns_msg_getflag(ns_msg handle, int flag) {
+ return(((handle)._flags & _ns_flagdata[flag].mask) >> _ns_flagdata[flag].shift);
+}
+
+int
+ns_skiprr(const u_char *ptr, const u_char *eom, ns_sect section, int count) {
+ const u_char *optr = ptr;
+
+ for ((void)NULL; count > 0; count--) {
+ int b, rdlength;
+
+ b = dn_skipname(ptr, eom);
+ if (b < 0)
+ RETERR(EMSGSIZE);
+ ptr += b/*Name*/ + NS_INT16SZ/*Type*/ + NS_INT16SZ/*Class*/;
+ if (section != ns_s_qd) {
+ if (ptr + NS_INT32SZ + NS_INT16SZ > eom)
+ RETERR(EMSGSIZE);
+ ptr += NS_INT32SZ/*TTL*/;
+ NS_GET16(rdlength, ptr);
+ ptr += rdlength/*RData*/;
+ }
+ }
+ if (ptr > eom)
+ RETERR(EMSGSIZE);
+ return (ptr - optr);
+}
+
+int
+ns_initparse(const u_char *msg, int msglen, ns_msg *handle) {
+ const u_char *eom = msg + msglen;
+ int i;
+
+ handle->_msg = msg;
+ handle->_eom = eom;
+ if (msg + NS_INT16SZ > eom)
+ RETERR(EMSGSIZE);
+ NS_GET16(handle->_id, msg);
+ if (msg + NS_INT16SZ > eom)
+ RETERR(EMSGSIZE);
+ NS_GET16(handle->_flags, msg);
+ for (i = 0; i < ns_s_max; i++) {
+ if (msg + NS_INT16SZ > eom)
+ RETERR(EMSGSIZE);
+ NS_GET16(handle->_counts[i], msg);
+ }
+ for (i = 0; i < ns_s_max; i++)
+ if (handle->_counts[i] == 0)
+ handle->_sections[i] = NULL;
+ else {
+ int b = ns_skiprr(msg, eom, (ns_sect)i,
+ handle->_counts[i]);
+
+ if (b < 0)
+ return (-1);
+ handle->_sections[i] = msg;
+ msg += b;
+ }
+ if (msg != eom)
+ RETERR(EMSGSIZE);
+ setsection(handle, ns_s_max);
+ return (0);
+}
+
+int
+ns_parserr(ns_msg *handle, ns_sect section, int rrnum, ns_rr *rr) {
+ int b;
+ int tmp;
+
+ /* Make section right. */
+ tmp = section;
+ if (tmp < 0 || section >= ns_s_max)
+ RETERR(ENODEV);
+ if (section != handle->_sect)
+ setsection(handle, section);
+
+ /* Make rrnum right. */
+ if (rrnum == -1)
+ rrnum = handle->_rrnum;
+ if (rrnum < 0 || rrnum >= handle->_counts[(int)section])
+ RETERR(ENODEV);
+ if (rrnum < handle->_rrnum)
+ setsection(handle, section);
+ if (rrnum > handle->_rrnum) {
+ b = ns_skiprr(handle->_msg_ptr, handle->_eom, section,
+ rrnum - handle->_rrnum);
+
+ if (b < 0)
+ return (-1);
+ handle->_msg_ptr += b;
+ handle->_rrnum = rrnum;
+ }
+
+ /* Do the parse. */
+ b = dn_expand(handle->_msg, handle->_eom,
+ handle->_msg_ptr, rr->name, NS_MAXDNAME);
+ if (b < 0)
+ return (-1);
+ handle->_msg_ptr += b;
+ if (handle->_msg_ptr + NS_INT16SZ + NS_INT16SZ > handle->_eom)
+ RETERR(EMSGSIZE);
+ NS_GET16(rr->type, handle->_msg_ptr);
+ NS_GET16(rr->rr_class, handle->_msg_ptr);
+ if (section == ns_s_qd) {
+ rr->ttl = 0;
+ rr->rdlength = 0;
+ rr->rdata = NULL;
+ } else {
+ if (handle->_msg_ptr + NS_INT32SZ + NS_INT16SZ > handle->_eom)
+ RETERR(EMSGSIZE);
+ NS_GET32(rr->ttl, handle->_msg_ptr);
+ NS_GET16(rr->rdlength, handle->_msg_ptr);
+ if (handle->_msg_ptr + rr->rdlength > handle->_eom)
+ RETERR(EMSGSIZE);
+ rr->rdata = handle->_msg_ptr;
+ handle->_msg_ptr += rr->rdlength;
+ }
+ if (++handle->_rrnum > handle->_counts[(int)section])
+ setsection(handle, (ns_sect)((int)section + 1));
+
+ /* All done. */
+ return (0);
+}
+
+/* Private. */
+
+static void
+setsection(ns_msg *msg, ns_sect sect) {
+ msg->_sect = sect;
+ if (sect == ns_s_max) {
+ msg->_rrnum = -1;
+ msg->_msg_ptr = NULL;
+ } else {
+ msg->_rrnum = 0;
+ msg->_msg_ptr = msg->_sections[(int)sect];
+ }
+}
+
+/*! \file */
+#endif
+#endif
diff --git a/lib/proxy.c b/lib/proxy.c
index b6b02d72..6ada4917 100644
--- a/lib/proxy.c
+++ b/lib/proxy.c
@@ -25,17 +25,11 @@
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
-#ifndef _WIN32
#include <sys/socket.h>
#include <netdb.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
-#else
-#include "sock.h"
-#define ETIMEDOUT WSAETIMEDOUT
-#define EINPROGRESS WSAEINPROGRESS
-#endif
#include <fcntl.h>
#include <errno.h>
#include "nogaim.h"
@@ -75,7 +69,6 @@ static gboolean gaim_io_connected(gpointer data, gint source, b_input_condition
int error = ETIMEDOUT;
len = sizeof(error);
-#ifndef _WIN32
if (getsockopt(source, SOL_SOCKET, SO_ERROR, &error, &len) < 0 || error) {
if ((phb->gai_cur = phb->gai_cur->ai_next)) {
int new_fd;
@@ -100,7 +93,6 @@ static gboolean gaim_io_connected(gpointer data, gint source, b_input_condition
}
return FALSE;
}
-#endif
freeaddrinfo(phb->gai);
sock_make_blocking(source);
b_event_remove(phb->inpa);
diff --git a/lib/proxy.h b/lib/proxy.h
index 680790a5..b3be0a66 100644
--- a/lib/proxy.h
+++ b/lib/proxy.h
@@ -27,11 +27,9 @@
#define _PROXY_H_
#include <sys/types.h>
-#ifndef _WIN32
#include <sys/socket.h>
#include <netdb.h>
#include <netinet/in.h>
-#endif
#include <glib.h>
#include <gmodule.h>
diff --git a/lib/ssl_gnutls.c b/lib/ssl_gnutls.c
index b698e630..f6bdd6b2 100644
--- a/lib/ssl_gnutls.c
+++ b/lib/ssl_gnutls.c
@@ -235,7 +235,7 @@ struct ssl_session
static void ssl_cache_add( struct scd *conn )
{
- size_t data_size;
+ size_t data_size = 0;
struct ssl_session *data;
char *hostname;
@@ -244,7 +244,7 @@ static void ssl_cache_add( struct scd *conn )
return;
data = g_malloc( sizeof( struct ssl_session ) + data_size );
- if( gnutls_session_get_data( conn->session, data->data, &data->size ) != 0 )
+ if( gnutls_session_get_data( conn->session, data->data, &data_size ) != 0 )
{
g_free( data );
return;
@@ -334,6 +334,9 @@ static gboolean ssl_handshake( gpointer data, gint source, b_input_condition con
struct scd *conn = data;
int st, stver;
+ /* This function returns false, so avoid calling b_event_remove again */
+ conn->inpa = -1;
+
if( ( st = gnutls_handshake( conn->session ) ) < 0 )
{
if( st == GNUTLS_E_AGAIN || st == GNUTLS_E_INTERRUPTED )
diff --git a/lib/ssl_sspi.c b/lib/ssl_sspi.c
deleted file mode 100644
index e14c451e..00000000
--- a/lib/ssl_sspi.c
+++ /dev/null
@@ -1,278 +0,0 @@
- /********************************************************************\
- * BitlBee -- An IRC to other IM-networks gateway *
- * *
- * Copyright 2002-2004 Wilmer van der Gaast and others *
- \********************************************************************/
-
-/* SSL module - SSPI backend */
-
-/* Copyright (C) 2005 Jelmer Vernooij <jelmer@samba.org> */
-
-/*
- This program is free software; you can redistribute it and/or modify
- it under the terms of the GNU General Public License as published by
- the Free Software Foundation; either version 2 of the License, or
- (at your option) any later version.
-
- 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 General Public License for more details.
-
- You should have received a copy of the GNU General Public License with
- the Debian GNU/Linux distribution in /usr/share/common-licenses/GPL;
- if not, write to the Free Software Foundation, Inc., 59 Temple Place,
- Suite 330, Boston, MA 02111-1307 USA
-*/
-
-#include "ssl_client.h"
-#include <windows.h>
-#define SECURITY_WIN32
-#include <security.h>
-#include <sspi.h>
-#include <schannel.h>
-#include "sock.h"
-
-static gboolean initialized = FALSE;
-int ssl_errno;
-
-struct scd
-{
- int fd;
- ssl_input_function func;
- gpointer data;
- gboolean established;
- CredHandle cred; /* SSL credentials */
- CtxtHandle context; /* SSL context */
- SecPkgContext_StreamSizes sizes;
-
- char *host;
-
- char *pending_raw_data;
- gsize pending_raw_data_len;
- char *pending_data;
- gsize pending_data_len;
-};
-
-static void ssl_connected(gpointer, gint, GaimInputCondition);
-
-void sspi_global_init(void)
-{
- /* FIXME */
-}
-
-void sspi_global_deinit(void)
-{
- /* FIXME */
-}
-
-void *ssl_connect(char *host, int port, ssl_input_function func, gpointer data)
-{
- struct scd *conn = g_new0(struct scd, 1);
-
- conn->fd = proxy_connect(host, port, ssl_connected, conn);
- sock_make_nonblocking(conn->fd);
- conn->func = func;
- conn->data = data;
- conn->host = g_strdup(host);
-
- if (conn->fd < 0)
- {
- g_free(conn);
- return NULL;
- }
-
- if (!initialized)
- {
- sspi_global_init();
- initialized = TRUE;
- atexit(sspi_global_deinit);
- }
-
- return conn;
-}
-
-static void ssl_connected(gpointer _conn, gint fd, GaimInputCondition cond)
-{
- struct scd *conn = _conn;
- SCHANNEL_CRED ssl_cred;
- TimeStamp timestamp;
- SecBuffer ibuf[2],obuf[1];
- SecBufferDesc ibufs,obufs;
- ULONG req = ISC_REQ_REPLAY_DETECT | ISC_REQ_SEQUENCE_DETECT |
- ISC_REQ_CONFIDENTIALITY | ISC_REQ_USE_SESSION_KEY |
- ISC_REQ_ALLOCATE_MEMORY | ISC_REQ_STREAM | ISC_REQ_EXTENDED_ERROR |
- ISC_REQ_MANUAL_CRED_VALIDATION;
- ULONG a;
- gsize size = 0;
- gchar *data = NULL;
-
- memset(&ssl_cred, 0, sizeof(SCHANNEL_CRED));
- ssl_cred.dwVersion = SCHANNEL_CRED_VERSION;
- ssl_cred.grbitEnabledProtocols = SP_PROT_SSL3_CLIENT;
-
- SECURITY_STATUS st = AcquireCredentialsHandle(NULL, UNISP_NAME, SECPKG_CRED_OUTBOUND, NULL, &ssl_cred, NULL, NULL, &conn->cred, &timestamp);
-
- if (st != SEC_E_OK) {
- conn->func(conn->data, NULL, cond);
- return;
- }
-
- do {
- /* initialize buffers */
- ibuf[0].cbBuffer = size; ibuf[0].pvBuffer = data;
- ibuf[1].cbBuffer = 0; ibuf[1].pvBuffer = NULL;
- obuf[0].cbBuffer = 0; obuf[0].pvBuffer = NULL;
- ibuf[0].BufferType = obuf[0].BufferType = SECBUFFER_TOKEN;
- ibuf[1].BufferType = SECBUFFER_EMPTY;
-
- /* initialize buffer descriptors */
- ibufs.ulVersion = obufs.ulVersion = SECBUFFER_VERSION;
- ibufs.cBuffers = 2; obufs.cBuffers = 1;
- ibufs.pBuffers = ibuf; obufs.pBuffers = obuf;
-
- st = InitializeSecurityContext(&conn->cred, size?&conn->context:NULL, conn->host, req, 0, SECURITY_NETWORK_DREP, size?&ibufs:NULL, 0, &conn->context, &obufs, &a, &timestamp);
- if (obuf[0].pvBuffer && obuf[0].cbBuffer) {
- /* FIXME: Check return value */
- send(conn->fd, obuf[0].pvBuffer, obuf[0].cbBuffer, 0);
- }
-
- switch (st) {
- case SEC_I_INCOMPLETE_CREDENTIALS:
- break;
- case SEC_I_CONTINUE_NEEDED:
- break;
- case SEC_E_INCOMPLETE_MESSAGE:
- break;
- case SEC_E_OK:
- break;
- }
-
- QueryContextAttributes(&conn->context, SECPKG_ATTR_STREAM_SIZES, &conn->sizes);
- } while (1);
-
- conn->func(conn->data, conn, cond);
-}
-
-int ssl_read(void *conn, char *retdata, int len)
-{
- struct scd *scd = conn;
- SecBufferDesc msg;
- SecBuffer buf[4];
- int ret = -1, i;
- char *data = g_malloc(scd->sizes.cbHeader + scd->sizes.cbMaximumMessage + scd->sizes.cbTrailer);
-
- /* FIXME: Try to read some data */
-
- msg.ulVersion = SECBUFFER_VERSION;
- msg.cBuffers = 4;
- msg.pBuffers = buf;
-
- buf[0].BufferType = SECBUFFER_DATA;
- buf[0].cbBuffer = len;
- buf[0].pvBuffer = data;
-
- buf[1].BufferType = SECBUFFER_EMPTY;
- buf[2].BufferType = SECBUFFER_EMPTY;
- buf[3].BufferType = SECBUFFER_EMPTY;
-
- SECURITY_STATUS st = DecryptMessage(&scd->context, &msg, 0, NULL);
-
- if (st != SEC_E_OK) {
- /* FIXME */
- return -1;
- }
-
- for (i = 0; i < 4; i++) {
- if (buf[i].BufferType == SECBUFFER_DATA) {
- memcpy(retdata, buf[i].pvBuffer, len);
- ret = len;
- }
- }
-
- g_free(data);
- return -1;
-}
-
-int ssl_write(void *conn, const char *userdata, int len)
-{
- struct scd *scd = conn;
- SecBuffer buf[4];
- SecBufferDesc msg;
- char *data;
- int ret;
-
- msg.ulVersion = SECBUFFER_VERSION;
- msg.cBuffers = 4;
- msg.pBuffers = buf;
-
- data = g_malloc(scd->sizes.cbHeader + scd->sizes.cbMaximumMessage + scd->sizes.cbTrailer);
- memcpy(data + scd->sizes.cbHeader, userdata, len);
-
- buf[0].BufferType = SECBUFFER_STREAM_HEADER;
- buf[0].cbBuffer = scd->sizes.cbHeader;
- buf[0].pvBuffer = data;
-
- buf[1].BufferType = SECBUFFER_DATA;
- buf[1].cbBuffer = len;
- buf[1].pvBuffer = data + scd->sizes.cbHeader;
-
- buf[2].BufferType = SECBUFFER_STREAM_TRAILER;
- buf[2].cbBuffer = scd->sizes.cbTrailer;
- buf[2].pvBuffer = data + scd->sizes.cbHeader + len;
- buf[3].BufferType = SECBUFFER_EMPTY;
-
- SECURITY_STATUS st = EncryptMessage(&scd->context, 0, &msg, 0);
-
- ret = send(scd->fd, data,
- buf[0].cbBuffer + buf[1].cbBuffer + buf[2].cbBuffer, 0);
-
- g_free(data);
-
- return ret;
-}
-
-void ssl_disconnect(void *conn)
-{
- struct scd *scd = conn;
-
- SecBufferDesc msg;
- SecBuffer buf;
- DWORD dw;
-
- dw = SCHANNEL_SHUTDOWN;
- buf.cbBuffer = sizeof(dw);
- buf.BufferType = SECBUFFER_TOKEN;
- buf.pvBuffer = &dw;
-
- msg.ulVersion = SECBUFFER_VERSION;
- msg.cBuffers = 1;
- msg.pBuffers = &buf;
-
- SECURITY_STATUS st = ApplyControlToken(&scd->context, &msg);
-
- if (st != SEC_E_OK) {
- /* FIXME */
- }
-
- /* FIXME: call InitializeSecurityContext(Schannel), passing
- * in empty buffers*/
-
- DeleteSecurityContext(&scd->context);
-
- FreeCredentialsHandle(&scd->cred);
-
- closesocket(scd->fd);
- g_free(scd->host);
- g_free(scd);
-}
-
-int ssl_getfd(void *conn)
-{
- return ((struct scd*)conn)->fd;
-}
-
-GaimInputCondition ssl_getdirection( void *conn )
-{
- return B_EV_IO_WRITE; /* FIXME: or B_EV_IO_READ */
-}