2016-03-31 07:28:05 +02:00
|
|
|
/* authd/providers/opm.c - small open proxy monitor
|
|
|
|
* Copyright (c) 2016 Elizabeth Myers <elizabeth@interlinked.me>
|
|
|
|
*
|
|
|
|
* Permission to use, copy, modify, and/or distribute this software for any
|
|
|
|
* purpose with or without fee is hereby granted, provided that the above
|
|
|
|
* copyright notice and this permission notice is present in all copies.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
|
|
|
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
|
|
|
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
|
|
* DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
|
|
|
|
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
|
|
|
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
|
|
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
|
|
|
|
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
|
|
|
|
* IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
* POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "authd.h"
|
|
|
|
#include "notice.h"
|
|
|
|
#include "provider.h"
|
|
|
|
|
2016-08-13 05:05:54 +02:00
|
|
|
using namespace ircd::defaults;
|
|
|
|
|
2016-04-05 11:31:22 +02:00
|
|
|
#define SELF_PID (opm_provider.id)
|
|
|
|
|
2016-03-31 07:28:05 +02:00
|
|
|
#define OPM_READSIZE 128
|
|
|
|
|
|
|
|
typedef enum protocol_t
|
|
|
|
{
|
|
|
|
PROTO_NONE,
|
|
|
|
PROTO_SOCKS4,
|
|
|
|
PROTO_SOCKS5,
|
2016-04-02 10:11:12 +02:00
|
|
|
PROTO_HTTP_CONNECT,
|
2016-04-03 01:37:59 +02:00
|
|
|
PROTO_HTTPS_CONNECT,
|
2016-03-31 07:28:05 +02:00
|
|
|
} protocol_t;
|
|
|
|
|
2016-04-06 16:22:24 +02:00
|
|
|
/* Lookup data associated with auth client */
|
2016-03-31 07:28:05 +02:00
|
|
|
struct opm_lookup
|
|
|
|
{
|
|
|
|
rb_dlink_list scans; /* List of scans */
|
2016-04-03 07:05:53 +02:00
|
|
|
bool in_progress;
|
2016-03-31 07:28:05 +02:00
|
|
|
};
|
|
|
|
|
2016-04-06 16:22:24 +02:00
|
|
|
struct opm_scan;
|
|
|
|
typedef void (*opm_callback_t)(struct opm_scan *);
|
|
|
|
|
|
|
|
/* A proxy scanner */
|
2016-03-31 07:28:05 +02:00
|
|
|
struct opm_proxy
|
|
|
|
{
|
2016-04-02 07:47:02 +02:00
|
|
|
char note[16];
|
2016-03-31 07:28:05 +02:00
|
|
|
protocol_t proto;
|
|
|
|
uint16_t port;
|
2016-04-06 16:22:24 +02:00
|
|
|
bool ssl; /* Connect to proxy with SSL */
|
|
|
|
bool ipv6; /* Proxy supports IPv6 */
|
2016-03-31 07:28:05 +02:00
|
|
|
|
2016-04-06 16:22:24 +02:00
|
|
|
opm_callback_t callback;
|
2016-03-31 07:28:05 +02:00
|
|
|
|
|
|
|
rb_dlink_node node;
|
|
|
|
};
|
|
|
|
|
2016-04-06 16:22:24 +02:00
|
|
|
/* A listener for proxy replies */
|
2016-03-31 07:28:05 +02:00
|
|
|
struct opm_listener
|
|
|
|
{
|
|
|
|
char ip[HOSTIPLEN];
|
|
|
|
uint16_t port;
|
2016-03-31 08:27:50 +02:00
|
|
|
struct rb_sockaddr_storage addr;
|
2016-03-31 07:28:05 +02:00
|
|
|
rb_fde_t *F;
|
|
|
|
};
|
|
|
|
|
2016-04-06 16:22:24 +02:00
|
|
|
/* An individual proxy scan */
|
|
|
|
struct opm_scan
|
|
|
|
{
|
|
|
|
struct auth_client *auth;
|
|
|
|
rb_fde_t *F; /* fd for scan */
|
|
|
|
|
|
|
|
struct opm_proxy *proxy; /* Associated proxy */
|
|
|
|
struct opm_listener *listener; /* Associated listener */
|
|
|
|
|
|
|
|
rb_dlink_node node;
|
|
|
|
};
|
|
|
|
|
2016-03-31 07:28:05 +02:00
|
|
|
/* Proxies that we scan for */
|
2016-04-02 07:47:02 +02:00
|
|
|
static rb_dlink_list proxy_scanners;
|
2016-03-31 07:28:05 +02:00
|
|
|
|
|
|
|
static ACCB accept_opm;
|
|
|
|
static PF read_opm_reply;
|
|
|
|
|
2016-04-06 16:22:24 +02:00
|
|
|
static CNCB opm_connected;
|
2016-03-31 07:28:05 +02:00
|
|
|
|
|
|
|
static void opm_cancel(struct auth_client *auth);
|
2016-04-02 00:16:17 +02:00
|
|
|
static bool create_listener(const char *ip, uint16_t port);
|
2016-03-31 07:28:05 +02:00
|
|
|
|
2016-04-07 11:47:48 +02:00
|
|
|
static int opm_timeout = OPM_TIMEOUT_DEFAULT;
|
2016-03-31 07:28:05 +02:00
|
|
|
static bool opm_enable = false;
|
|
|
|
|
2016-04-12 16:33:51 +02:00
|
|
|
enum
|
|
|
|
{
|
|
|
|
LISTEN_IPV4,
|
|
|
|
LISTEN_IPV6,
|
|
|
|
LISTEN_LAST,
|
|
|
|
};
|
2016-03-31 07:28:05 +02:00
|
|
|
|
|
|
|
/* IPv4 and IPv6 */
|
2016-04-12 16:33:51 +02:00
|
|
|
static struct opm_listener listeners[LISTEN_LAST];
|
2016-03-31 07:28:05 +02:00
|
|
|
|
2016-04-02 07:47:02 +02:00
|
|
|
static inline protocol_t
|
2016-04-02 10:11:12 +02:00
|
|
|
get_protocol_from_string(const char *str)
|
2016-04-02 07:47:02 +02:00
|
|
|
{
|
2016-04-02 10:11:12 +02:00
|
|
|
if(strcasecmp(str, "socks4") == 0)
|
2016-04-02 07:47:02 +02:00
|
|
|
return PROTO_SOCKS4;
|
2016-04-02 10:11:12 +02:00
|
|
|
else if(strcasecmp(str, "socks5") == 0)
|
2016-04-02 07:47:02 +02:00
|
|
|
return PROTO_SOCKS5;
|
2016-04-02 10:11:12 +02:00
|
|
|
else if(strcasecmp(str, "httpconnect") == 0)
|
|
|
|
return PROTO_HTTP_CONNECT;
|
2016-04-03 01:37:59 +02:00
|
|
|
else if(strcasecmp(str, "httpsconnect") == 0)
|
|
|
|
return PROTO_HTTPS_CONNECT;
|
2016-04-02 07:47:02 +02:00
|
|
|
else
|
|
|
|
return PROTO_NONE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline struct opm_proxy *
|
|
|
|
find_proxy_scanner(protocol_t proto, uint16_t port)
|
|
|
|
{
|
|
|
|
rb_dlink_node *ptr;
|
|
|
|
|
|
|
|
RB_DLINK_FOREACH(ptr, proxy_scanners.head)
|
|
|
|
{
|
2016-07-13 07:17:21 +02:00
|
|
|
struct opm_proxy *proxy = (opm_proxy *)ptr->data;
|
2016-04-02 07:47:02 +02:00
|
|
|
|
|
|
|
if(proxy->proto == proto && proxy->port == port)
|
|
|
|
return proxy;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
2016-03-31 07:28:05 +02:00
|
|
|
|
2016-04-04 10:58:46 +02:00
|
|
|
/* This is called when an open proxy connects to us */
|
2016-03-31 07:28:05 +02:00
|
|
|
static void
|
|
|
|
read_opm_reply(rb_fde_t *F, void *data)
|
|
|
|
{
|
2016-04-02 07:47:02 +02:00
|
|
|
rb_dlink_node *ptr;
|
2016-07-13 07:17:21 +02:00
|
|
|
struct auth_client *auth = (auth_client *)data;
|
2016-03-31 07:28:05 +02:00
|
|
|
struct opm_lookup *lookup;
|
|
|
|
char readbuf[OPM_READSIZE];
|
|
|
|
ssize_t len;
|
|
|
|
|
2016-04-04 10:58:46 +02:00
|
|
|
lrb_assert(auth != NULL);
|
2016-07-13 07:17:21 +02:00
|
|
|
lookup = (opm_lookup *)get_provider_data(auth, SELF_PID);
|
2016-04-04 10:58:46 +02:00
|
|
|
lrb_assert(lookup != NULL);
|
2016-03-31 07:28:05 +02:00
|
|
|
|
|
|
|
if((len = rb_read(F, readbuf, sizeof(readbuf))) < 0 && rb_ignore_errno(errno))
|
|
|
|
{
|
|
|
|
rb_setselect(F, RB_SELECT_READ, read_opm_reply, auth);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
else if(len <= 0)
|
|
|
|
{
|
|
|
|
/* Dead */
|
|
|
|
rb_close(F);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-04-02 07:47:02 +02:00
|
|
|
RB_DLINK_FOREACH(ptr, proxy_scanners.head)
|
2016-03-31 07:28:05 +02:00
|
|
|
{
|
2016-07-13 07:17:21 +02:00
|
|
|
struct opm_proxy *proxy = (opm_proxy *)ptr->data;
|
2016-04-02 07:47:02 +02:00
|
|
|
|
2016-04-02 10:11:12 +02:00
|
|
|
if(strncmp(proxy->note, readbuf, strlen(proxy->note)) == 0)
|
2016-04-01 09:17:25 +02:00
|
|
|
{
|
|
|
|
rb_dlink_node *ptr, *nptr;
|
|
|
|
|
|
|
|
/* Cancel outstanding lookups */
|
|
|
|
RB_DLINK_FOREACH_SAFE(ptr, nptr, lookup->scans.head)
|
|
|
|
{
|
2016-07-13 07:17:21 +02:00
|
|
|
struct opm_scan *scan = (opm_scan *)ptr->data;
|
2016-04-01 09:17:25 +02:00
|
|
|
|
|
|
|
rb_close(scan->F);
|
|
|
|
rb_free(scan);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* No longer needed, client is going away */
|
|
|
|
rb_free(lookup);
|
|
|
|
|
2016-04-05 11:31:22 +02:00
|
|
|
reject_client(auth, SELF_PID, readbuf, "Open proxy detected");
|
2016-04-01 09:17:25 +02:00
|
|
|
break;
|
|
|
|
}
|
2016-03-31 07:28:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
rb_close(F);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
accept_opm(rb_fde_t *F, int status, struct sockaddr *addr, rb_socklen_t len, void *data)
|
|
|
|
{
|
2016-05-01 02:20:12 +02:00
|
|
|
struct auth_client *auth = NULL;
|
2016-07-13 07:17:21 +02:00
|
|
|
struct opm_listener *listener = (opm_listener *)data;
|
2016-03-31 08:45:19 +02:00
|
|
|
struct rb_sockaddr_storage localaddr;
|
2016-07-22 21:22:01 +02:00
|
|
|
rb_socklen_t llen = sizeof(struct rb_sockaddr_storage);
|
2016-05-01 02:20:12 +02:00
|
|
|
rb_dictionary_iter iter;
|
2016-03-31 07:28:05 +02:00
|
|
|
|
|
|
|
if(status != 0 || listener == NULL)
|
|
|
|
{
|
|
|
|
rb_close(F);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-03-31 08:45:19 +02:00
|
|
|
if(getsockname(rb_get_fd(F), (struct sockaddr *)&localaddr, &llen))
|
2016-03-31 07:28:05 +02:00
|
|
|
{
|
|
|
|
/* This can happen if the client goes away after accept */
|
|
|
|
rb_close(F);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Correlate connection with client(s) */
|
2016-07-13 07:17:21 +02:00
|
|
|
void *elem;
|
|
|
|
RB_DICTIONARY_FOREACH(elem, &iter, auth_clients)
|
2016-03-31 07:28:05 +02:00
|
|
|
{
|
2016-07-13 07:17:21 +02:00
|
|
|
auth = (auth_client *)elem;
|
2016-03-31 08:45:19 +02:00
|
|
|
if(GET_SS_FAMILY(&auth->c_addr) != GET_SS_FAMILY(&localaddr))
|
2016-03-31 07:28:05 +02:00
|
|
|
continue;
|
|
|
|
|
|
|
|
/* Compare the addresses */
|
2016-03-31 08:45:19 +02:00
|
|
|
switch(GET_SS_FAMILY(&localaddr))
|
2016-03-31 07:28:05 +02:00
|
|
|
{
|
|
|
|
case AF_INET:
|
|
|
|
{
|
2016-03-31 08:45:19 +02:00
|
|
|
struct sockaddr_in *s = (struct sockaddr_in *)&localaddr, *c = (struct sockaddr_in *)&auth->c_addr;
|
2016-03-31 07:28:05 +02:00
|
|
|
|
|
|
|
if(s->sin_addr.s_addr == c->sin_addr.s_addr)
|
|
|
|
{
|
|
|
|
/* Match... check if it's real */
|
|
|
|
rb_setselect(F, RB_SELECT_READ, read_opm_reply, auth);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
#ifdef RB_IPV6
|
|
|
|
case AF_INET6:
|
|
|
|
{
|
2016-03-31 08:45:19 +02:00
|
|
|
struct sockaddr_in6 *s = (struct sockaddr_in6 *)&localaddr, *c = (struct sockaddr_in6 *)&auth->c_addr;
|
2016-03-31 07:28:05 +02:00
|
|
|
|
2016-03-31 10:00:29 +02:00
|
|
|
if(IN6_ARE_ADDR_EQUAL(&s->sin6_addr, &c->sin6_addr))
|
2016-03-31 07:28:05 +02:00
|
|
|
{
|
|
|
|
rb_setselect(F, RB_SELECT_READ, read_opm_reply, auth);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
default:
|
|
|
|
warn_opers(L_CRIT, "OPM: unknown address type in listen function");
|
|
|
|
exit(EX_PROVIDER_ERROR);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* We don't care about the socket if we get here */
|
|
|
|
rb_close(F);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Scanners */
|
|
|
|
|
|
|
|
static void
|
2016-04-06 16:22:24 +02:00
|
|
|
opm_connected(rb_fde_t *F, int error, void *data)
|
2016-03-31 07:28:05 +02:00
|
|
|
{
|
2016-07-13 07:17:21 +02:00
|
|
|
struct opm_scan *scan = (opm_scan *)data;
|
2016-04-06 16:22:24 +02:00
|
|
|
struct opm_proxy *proxy = scan->proxy;
|
|
|
|
struct auth_client *auth = scan->auth;
|
2016-07-13 07:17:21 +02:00
|
|
|
struct opm_lookup *lookup = (opm_lookup *)get_provider_data(auth, SELF_PID);
|
2016-03-31 07:28:05 +02:00
|
|
|
|
|
|
|
if(error || !opm_enable)
|
2016-04-06 16:22:24 +02:00
|
|
|
{
|
|
|
|
//notice_client(scan->auth->cid, "*** Scan not connected: %s", proxy->note);
|
2016-03-31 07:28:05 +02:00
|
|
|
goto end;
|
2016-04-06 16:22:24 +02:00
|
|
|
}
|
2016-03-31 07:28:05 +02:00
|
|
|
|
|
|
|
switch(GET_SS_FAMILY(&auth->c_addr))
|
|
|
|
{
|
|
|
|
case AF_INET:
|
|
|
|
if(listeners[LISTEN_IPV4].F == NULL)
|
|
|
|
/* They cannot respond to us */
|
|
|
|
goto end;
|
|
|
|
|
|
|
|
break;
|
|
|
|
#ifdef RB_IPV6
|
|
|
|
case AF_INET6:
|
2016-04-06 16:22:24 +02:00
|
|
|
if(!proxy->ipv6)
|
|
|
|
/* Welp, too bad */
|
|
|
|
goto end;
|
|
|
|
|
|
|
|
if(listeners[LISTEN_IPV6].F == NULL)
|
|
|
|
/* They cannot respond to us */
|
|
|
|
goto end;
|
|
|
|
|
|
|
|
break;
|
2016-03-31 07:28:05 +02:00
|
|
|
#endif
|
|
|
|
default:
|
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
|
2016-04-06 16:22:24 +02:00
|
|
|
proxy->callback(scan);
|
|
|
|
|
|
|
|
end:
|
|
|
|
rb_close(scan->F);
|
2016-04-30 08:58:42 +02:00
|
|
|
rb_dlinkDelete(&scan->node, &lookup->scans);
|
2016-04-06 16:22:24 +02:00
|
|
|
rb_free(scan);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
socks4_connected(struct opm_scan *scan)
|
|
|
|
{
|
|
|
|
struct auth_client *auth = scan->auth;
|
2016-07-13 07:17:21 +02:00
|
|
|
struct opm_lookup *lookup = (opm_lookup *)get_provider_data(auth, SELF_PID);
|
2016-04-06 16:22:24 +02:00
|
|
|
uint8_t sendbuf[9]; /* Size we're building */
|
|
|
|
uint8_t *c = sendbuf;
|
|
|
|
|
|
|
|
memcpy(c, "\x04\x01", 2); c += 2; /* Socks version 4, connect command */
|
|
|
|
memcpy(c, &(((struct sockaddr_in *)&scan->listener->addr)->sin_port), 2); c += 2; /* Port */
|
|
|
|
memcpy(c, &(((struct sockaddr_in *)&scan->listener->addr)->sin_addr.s_addr), 4); c += 4; /* Address */
|
2016-03-31 07:28:05 +02:00
|
|
|
*c = '\x00'; /* No userid */
|
|
|
|
|
|
|
|
/* Send header */
|
|
|
|
if(rb_write(scan->F, sendbuf, sizeof(sendbuf)) < 0)
|
2016-04-06 16:22:24 +02:00
|
|
|
return;
|
2016-03-31 07:28:05 +02:00
|
|
|
|
|
|
|
/* Send note */
|
|
|
|
if(rb_write(scan->F, scan->proxy->note, strlen(scan->proxy->note) + 1) < 0)
|
2016-04-06 16:22:24 +02:00
|
|
|
return;
|
2016-03-31 07:28:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2016-04-06 16:22:24 +02:00
|
|
|
socks5_connected(struct opm_scan *scan)
|
2016-03-31 07:28:05 +02:00
|
|
|
{
|
2016-04-06 16:22:24 +02:00
|
|
|
struct auth_client *auth = scan->auth;
|
2016-07-13 07:17:21 +02:00
|
|
|
struct opm_lookup *lookup = (opm_lookup *)get_provider_data(auth, SELF_PID);
|
2016-03-31 07:28:05 +02:00
|
|
|
uint8_t sendbuf[25]; /* Size we're building */
|
|
|
|
uint8_t *c = sendbuf;
|
2016-04-04 10:58:46 +02:00
|
|
|
|
|
|
|
auth = scan->auth;
|
2016-07-13 07:17:21 +02:00
|
|
|
lookup = (opm_lookup *)get_provider_data(auth, SELF_PID);
|
2016-04-04 10:58:46 +02:00
|
|
|
|
2016-03-31 07:28:05 +02:00
|
|
|
/* Build the version header and socks request
|
2016-04-02 10:11:12 +02:00
|
|
|
* version header (3 bytes): version, number of auth methods, auth type (0 for none)
|
2016-03-31 07:28:05 +02:00
|
|
|
* connect req (3 bytes): version, command (1 = connect), reserved (0)
|
|
|
|
*/
|
|
|
|
memcpy(c, "\x05\x01\x00\x05\x01\x00", 6); c += 6;
|
|
|
|
|
|
|
|
switch(GET_SS_FAMILY(&auth->c_addr))
|
|
|
|
{
|
|
|
|
case AF_INET:
|
|
|
|
*(c++) = '\x01'; /* Address type (1 = IPv4) */
|
2016-04-06 16:22:24 +02:00
|
|
|
memcpy(c, &(((struct sockaddr_in *)&scan->listener->addr)->sin_addr.s_addr), 4); c += 4; /* Address */
|
|
|
|
memcpy(c, &(((struct sockaddr_in *)&scan->listener->addr)->sin_port), 2); c += 2; /* Port */
|
2016-03-31 07:28:05 +02:00
|
|
|
break;
|
|
|
|
#ifdef RB_IPV6
|
|
|
|
case AF_INET6:
|
|
|
|
*(c++) = '\x04'; /* Address type (4 = IPv6) */
|
2016-04-06 16:22:24 +02:00
|
|
|
memcpy(c, ((struct sockaddr_in6 *)&scan->listener->addr)->sin6_addr.s6_addr, 16); c += 16; /* Address */
|
|
|
|
memcpy(c, &(((struct sockaddr_in6 *)&scan->listener->addr)->sin6_port), 2); c += 2; /* Port */
|
2016-03-31 07:28:05 +02:00
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
default:
|
2016-04-06 16:22:24 +02:00
|
|
|
return;
|
2016-03-31 07:28:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Send header */
|
|
|
|
if(rb_write(scan->F, sendbuf, (size_t)(sendbuf - c)) <= 0)
|
2016-04-06 16:22:24 +02:00
|
|
|
return;
|
2016-03-31 07:28:05 +02:00
|
|
|
|
|
|
|
/* Now the note in a separate write */
|
|
|
|
if(rb_write(scan->F, scan->proxy->note, strlen(scan->proxy->note) + 1) <= 0)
|
2016-04-06 16:22:24 +02:00
|
|
|
return;
|
2016-03-31 07:28:05 +02:00
|
|
|
}
|
|
|
|
|
2016-04-02 10:11:12 +02:00
|
|
|
static void
|
2016-04-06 16:22:24 +02:00
|
|
|
http_connect_connected(struct opm_scan *scan)
|
2016-04-02 10:11:12 +02:00
|
|
|
{
|
2016-04-06 16:22:24 +02:00
|
|
|
struct auth_client *auth = scan->auth;
|
2016-07-13 07:17:21 +02:00
|
|
|
struct opm_lookup *lookup = (opm_lookup *)get_provider_data(auth, SELF_PID);
|
2016-04-02 10:11:12 +02:00
|
|
|
char sendbuf[128]; /* A bit bigger than we need but better safe than sorry */
|
|
|
|
char *c = sendbuf;
|
|
|
|
|
|
|
|
/* Simple enough to build */
|
2016-04-06 16:22:24 +02:00
|
|
|
snprintf(sendbuf, sizeof(sendbuf), "CONNECT %s:%hu HTTP/1.0\r\n\r\n", scan->listener->ip, scan->listener->port);
|
2016-04-02 10:11:12 +02:00
|
|
|
|
|
|
|
/* Send request */
|
|
|
|
if(rb_write(scan->F, sendbuf, strlen(sendbuf)) <= 0)
|
2016-04-06 16:22:24 +02:00
|
|
|
return;
|
2016-04-02 10:11:12 +02:00
|
|
|
|
|
|
|
/* Now the note in a separate write */
|
|
|
|
if(rb_write(scan->F, scan->proxy->note, strlen(scan->proxy->note) + 1) <= 0)
|
2016-04-06 16:22:24 +02:00
|
|
|
return;
|
2016-04-02 10:11:12 +02:00
|
|
|
|
|
|
|
/* MiroTik needs this, and as a separate write */
|
|
|
|
if(rb_write(scan->F, "\r\n", 2) <= 0)
|
2016-04-06 16:22:24 +02:00
|
|
|
return;
|
2016-04-02 10:11:12 +02:00
|
|
|
}
|
|
|
|
|
2016-03-31 07:28:05 +02:00
|
|
|
/* Establish connections */
|
|
|
|
static inline void
|
|
|
|
establish_connection(struct auth_client *auth, struct opm_proxy *proxy)
|
|
|
|
{
|
2016-07-13 07:17:21 +02:00
|
|
|
struct opm_lookup *lookup = (opm_lookup *)get_provider_data(auth, SELF_PID);
|
|
|
|
struct opm_scan *scan = (opm_scan *)rb_malloc(sizeof(struct opm_scan));
|
2016-04-06 16:22:24 +02:00
|
|
|
struct opm_listener *listener;
|
2016-03-31 08:27:50 +02:00
|
|
|
struct rb_sockaddr_storage c_a, l_a;
|
2016-03-31 07:28:05 +02:00
|
|
|
int opt = 1;
|
|
|
|
|
2016-04-04 10:58:46 +02:00
|
|
|
lrb_assert(lookup != NULL);
|
|
|
|
|
2016-04-01 02:07:57 +02:00
|
|
|
#ifdef RB_IPV6
|
2016-04-06 16:22:24 +02:00
|
|
|
if(GET_SS_FAMILY(&auth->c_addr) == AF_INET6)
|
|
|
|
{
|
|
|
|
if(proxy->proto == PROTO_SOCKS4)
|
2016-04-01 02:07:57 +02:00
|
|
|
{
|
2016-04-06 16:22:24 +02:00
|
|
|
/* SOCKS4 doesn't support IPv6 */
|
2016-04-01 02:07:57 +02:00
|
|
|
rb_free(scan);
|
|
|
|
return;
|
|
|
|
}
|
2016-03-31 07:28:05 +02:00
|
|
|
listener = &listeners[LISTEN_IPV6];
|
2016-04-06 16:22:24 +02:00
|
|
|
}
|
2016-03-31 07:28:05 +02:00
|
|
|
else
|
|
|
|
#endif
|
|
|
|
listener = &listeners[LISTEN_IPV4];
|
|
|
|
|
2016-04-06 16:22:24 +02:00
|
|
|
if(listener->F == NULL)
|
|
|
|
{
|
|
|
|
/* We can't respond */
|
|
|
|
rb_free(scan);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-03-31 07:28:05 +02:00
|
|
|
c_a = auth->c_addr; /* Client */
|
2016-04-06 16:22:24 +02:00
|
|
|
l_a = listener->addr; /* Listener (connect using its IP) */
|
2016-03-31 07:28:05 +02:00
|
|
|
|
|
|
|
scan->auth = auth;
|
|
|
|
scan->proxy = proxy;
|
2016-04-06 16:22:24 +02:00
|
|
|
scan->listener = listener;
|
2016-03-31 07:28:05 +02:00
|
|
|
if((scan->F = rb_socket(GET_SS_FAMILY(&auth->c_addr), SOCK_STREAM, 0, proxy->note)) == NULL)
|
|
|
|
{
|
2016-04-03 02:50:25 +02:00
|
|
|
warn_opers(L_WARN, "OPM: could not create OPM socket (proto %s): %s", proxy->note, strerror(errno));
|
2016-03-31 07:28:05 +02:00
|
|
|
rb_free(scan);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Disable Nagle's algorithim - buffering could affect scans */
|
|
|
|
(void)setsockopt(rb_get_fd(scan->F), IPPROTO_TCP, TCP_NODELAY, (char *)&opt, sizeof(opt));
|
|
|
|
|
2016-03-31 10:00:29 +02:00
|
|
|
SET_SS_PORT(&l_a, 0);
|
2016-04-01 02:07:57 +02:00
|
|
|
SET_SS_PORT(&c_a, htons(proxy->port));
|
2016-03-31 07:28:05 +02:00
|
|
|
|
|
|
|
rb_dlinkAdd(scan, &scan->node, &lookup->scans);
|
2016-04-03 01:37:59 +02:00
|
|
|
|
|
|
|
if(!proxy->ssl)
|
|
|
|
rb_connect_tcp(scan->F,
|
|
|
|
(struct sockaddr *)&c_a,
|
|
|
|
(struct sockaddr *)&l_a,
|
2016-04-06 16:22:24 +02:00
|
|
|
opm_connected, scan, opm_timeout);
|
2016-04-03 01:37:59 +02:00
|
|
|
else
|
|
|
|
rb_connect_tcp_ssl(scan->F,
|
|
|
|
(struct sockaddr *)&c_a,
|
|
|
|
(struct sockaddr *)&l_a,
|
2016-04-06 16:22:24 +02:00
|
|
|
opm_connected, scan, opm_timeout);
|
2016-03-31 07:28:05 +02:00
|
|
|
}
|
|
|
|
|
2016-04-02 07:47:02 +02:00
|
|
|
static bool
|
|
|
|
create_listener(const char *ip, uint16_t port)
|
|
|
|
{
|
2016-05-01 02:20:12 +02:00
|
|
|
struct auth_client *auth;
|
2016-04-02 07:47:02 +02:00
|
|
|
struct opm_listener *listener;
|
|
|
|
struct rb_sockaddr_storage addr;
|
2016-05-01 02:20:12 +02:00
|
|
|
rb_dictionary_iter iter;
|
2016-04-02 07:47:02 +02:00
|
|
|
rb_fde_t *F;
|
|
|
|
int opt = 1;
|
|
|
|
|
|
|
|
if(!rb_inet_pton_sock(ip, (struct sockaddr *)&addr))
|
|
|
|
{
|
|
|
|
warn_opers(L_CRIT, "OPM: got a bad listener: %s:%hu", ip, port);
|
|
|
|
exit(EX_PROVIDER_ERROR);
|
|
|
|
}
|
|
|
|
|
2016-04-03 04:13:44 +02:00
|
|
|
SET_SS_PORT(&addr, htons(port));
|
|
|
|
|
2016-04-02 07:47:02 +02:00
|
|
|
#ifdef RB_IPV6
|
|
|
|
if(GET_SS_FAMILY(&addr) == AF_INET6)
|
|
|
|
{
|
|
|
|
struct sockaddr_in6 *a1, *a2;
|
|
|
|
|
|
|
|
listener = &listeners[LISTEN_IPV6];
|
|
|
|
|
|
|
|
a1 = (struct sockaddr_in6 *)&addr;
|
|
|
|
a2 = (struct sockaddr_in6 *)&listener->addr;
|
|
|
|
|
|
|
|
if(IN6_ARE_ADDR_EQUAL(&a1->sin6_addr, &a2->sin6_addr) &&
|
|
|
|
GET_SS_PORT(&addr) == GET_SS_PORT(&listener->addr) &&
|
|
|
|
listener->F != NULL)
|
|
|
|
{
|
|
|
|
/* Listener already exists */
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
struct sockaddr_in *a1, *a2;
|
|
|
|
|
|
|
|
listener = &listeners[LISTEN_IPV4];
|
|
|
|
|
|
|
|
a1 = (struct sockaddr_in *)&addr;
|
|
|
|
a2 = (struct sockaddr_in *)&listener->addr;
|
|
|
|
|
|
|
|
if(a1->sin_addr.s_addr == a2->sin_addr.s_addr &&
|
|
|
|
GET_SS_PORT(&addr) == GET_SS_PORT(&listener->addr) &&
|
|
|
|
listener->F != NULL)
|
|
|
|
{
|
|
|
|
/* Listener already exists */
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if((F = rb_socket(GET_SS_FAMILY(&addr), SOCK_STREAM, 0, "OPM listener socket")) == NULL)
|
|
|
|
{
|
|
|
|
/* This shouldn't fail, or we have big problems... */
|
|
|
|
warn_opers(L_CRIT, "OPM: cannot create socket: %s", strerror(errno));
|
|
|
|
exit(EX_PROVIDER_ERROR);
|
|
|
|
}
|
|
|
|
|
|
|
|
if(setsockopt(rb_get_fd(F), SOL_SOCKET, SO_REUSEADDR, (char *)&opt, sizeof(opt)))
|
|
|
|
{
|
|
|
|
/* This shouldn't fail either... */
|
|
|
|
warn_opers(L_CRIT, "OPM: cannot set options on socket: %s", strerror(errno));
|
|
|
|
exit(EX_PROVIDER_ERROR);
|
|
|
|
}
|
|
|
|
|
|
|
|
if(bind(rb_get_fd(F), (struct sockaddr *)&addr, GET_SS_LEN(&addr)))
|
|
|
|
{
|
|
|
|
/* Shit happens, let's not cripple authd over /this/ since it could be user error */
|
|
|
|
warn_opers(L_WARN, "OPM: cannot bind on socket: %s", strerror(errno));
|
|
|
|
rb_close(F);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(rb_listen(F, SOMAXCONN, false)) /* deferred accept could interfere with detection */
|
|
|
|
{
|
|
|
|
/* Again, could be user error */
|
|
|
|
warn_opers(L_WARN, "OPM: cannot listen on socket: %s", strerror(errno));
|
|
|
|
rb_close(F);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* From this point forward we assume we have a listener */
|
|
|
|
|
|
|
|
if(listener->F != NULL)
|
|
|
|
/* Close old listener */
|
|
|
|
rb_close(listener->F);
|
|
|
|
|
|
|
|
listener->F = F;
|
|
|
|
|
|
|
|
/* Cancel clients that may be on old listener
|
|
|
|
* XXX - should rescan clients that need it
|
|
|
|
*/
|
2016-07-13 07:17:21 +02:00
|
|
|
void *elem;
|
|
|
|
RB_DICTIONARY_FOREACH(elem, &iter, auth_clients)
|
2016-04-02 07:47:02 +02:00
|
|
|
{
|
2016-07-13 07:17:21 +02:00
|
|
|
auth = (auth_client *)elem;
|
2016-04-02 07:47:02 +02:00
|
|
|
opm_cancel(auth);
|
2016-05-01 12:31:05 +02:00
|
|
|
/* auth is now invalid as we have no reference */
|
2016-04-02 07:47:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Copy data */
|
|
|
|
rb_strlcpy(listener->ip, ip, sizeof(listener->ip));
|
|
|
|
listener->port = port;
|
|
|
|
listener->addr = addr;
|
|
|
|
|
|
|
|
opm_enable = true; /* Implicitly set this to true for now if we have a listener */
|
|
|
|
rb_accept_tcp(listener->F, NULL, accept_opm, listener);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-03-31 07:28:05 +02:00
|
|
|
static void
|
2016-04-03 07:05:53 +02:00
|
|
|
opm_scan(struct auth_client *auth)
|
2016-03-31 07:28:05 +02:00
|
|
|
{
|
2016-04-03 07:05:53 +02:00
|
|
|
rb_dlink_node *ptr;
|
|
|
|
struct opm_lookup *lookup;
|
2016-03-31 07:28:05 +02:00
|
|
|
|
2016-04-03 07:05:53 +02:00
|
|
|
lrb_assert(auth != NULL);
|
|
|
|
|
2016-07-13 07:17:21 +02:00
|
|
|
lookup = (opm_lookup *)get_provider_data(auth, SELF_PID);
|
2016-04-05 11:31:22 +02:00
|
|
|
set_provider_timeout_relative(auth, SELF_PID, opm_timeout);
|
2016-04-03 07:05:53 +02:00
|
|
|
|
|
|
|
lookup->in_progress = true;
|
|
|
|
|
|
|
|
RB_DLINK_FOREACH(ptr, proxy_scanners.head)
|
2016-03-31 07:28:05 +02:00
|
|
|
{
|
2016-07-13 07:17:21 +02:00
|
|
|
struct opm_proxy *proxy = (opm_proxy *)ptr->data;
|
2016-04-06 16:22:24 +02:00
|
|
|
//notice_client(auth->cid, "*** Scanning for proxy type %s", proxy->note);
|
2016-04-03 07:05:53 +02:00
|
|
|
establish_connection(auth, proxy);
|
2016-03-31 07:28:05 +02:00
|
|
|
}
|
2016-04-03 07:05:53 +02:00
|
|
|
|
|
|
|
notice_client(auth->cid, "*** Scanning for open proxies...");
|
|
|
|
}
|
|
|
|
|
|
|
|
/* This is called every time a provider is completed as long as we are marked not done */
|
|
|
|
static void
|
2016-04-06 14:43:36 +02:00
|
|
|
opm_initiate(struct auth_client *auth, uint32_t provider)
|
2016-04-03 07:05:53 +02:00
|
|
|
{
|
2016-07-13 07:17:21 +02:00
|
|
|
struct opm_lookup *lookup = (opm_lookup *)get_provider_data(auth, SELF_PID);
|
2016-04-05 11:31:22 +02:00
|
|
|
uint32_t rdns_pid, ident_pid;
|
2016-04-03 07:05:53 +02:00
|
|
|
|
2016-04-05 11:31:22 +02:00
|
|
|
lrb_assert(provider != SELF_PID);
|
|
|
|
lrb_assert(!is_provider_done(auth, SELF_PID));
|
2016-04-03 07:05:53 +02:00
|
|
|
lrb_assert(rb_dlink_list_length(&proxy_scanners) > 0);
|
|
|
|
|
|
|
|
if(lookup == NULL || lookup->in_progress)
|
|
|
|
/* Nothing to do */
|
|
|
|
return;
|
2016-04-05 11:31:22 +02:00
|
|
|
else if((!get_provider_id("rdns", &rdns_pid) || is_provider_done(auth, rdns_pid)) &&
|
|
|
|
(!get_provider_id("ident", &ident_pid) || is_provider_done(auth, ident_pid)))
|
|
|
|
/* Don't start until ident and rdns are finished (or not loaded) */
|
2016-04-03 07:05:53 +02:00
|
|
|
return;
|
|
|
|
else
|
|
|
|
opm_scan(auth);
|
2016-03-31 07:28:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static bool
|
|
|
|
opm_start(struct auth_client *auth)
|
|
|
|
{
|
2016-04-05 11:31:22 +02:00
|
|
|
uint32_t rdns_pid, ident_pid;
|
|
|
|
|
|
|
|
lrb_assert(get_provider_data(auth, SELF_PID) == NULL);
|
2016-03-31 07:28:05 +02:00
|
|
|
|
2016-04-03 05:45:52 +02:00
|
|
|
if(!opm_enable || rb_dlink_list_length(&proxy_scanners) == 0)
|
2016-04-03 07:05:53 +02:00
|
|
|
/* Nothing to do... */
|
2016-03-31 07:28:05 +02:00
|
|
|
return true;
|
|
|
|
|
2016-05-01 12:31:05 +02:00
|
|
|
auth_client_ref(auth);
|
|
|
|
|
2016-04-05 11:31:22 +02:00
|
|
|
set_provider_data(auth, SELF_PID, rb_malloc(sizeof(struct opm_lookup)));
|
2016-03-31 07:28:05 +02:00
|
|
|
|
2016-04-05 11:31:22 +02:00
|
|
|
if((!get_provider_id("rdns", &rdns_pid) || is_provider_done(auth, rdns_pid)) &&
|
|
|
|
(!get_provider_id("ident", &ident_pid) || is_provider_done(auth, ident_pid)))
|
|
|
|
{
|
|
|
|
/* Don't start until ident and rdns are finished (or not loaded) */
|
2016-04-03 07:05:53 +02:00
|
|
|
opm_scan(auth);
|
2016-04-05 11:31:22 +02:00
|
|
|
}
|
2016-03-31 07:28:05 +02:00
|
|
|
|
2016-04-05 11:31:22 +02:00
|
|
|
set_provider_running(auth, SELF_PID);
|
2016-03-31 07:28:05 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
opm_cancel(struct auth_client *auth)
|
|
|
|
{
|
2016-07-13 07:17:21 +02:00
|
|
|
struct opm_lookup *lookup = (opm_lookup *)get_provider_data(auth, SELF_PID);
|
2016-03-31 07:28:05 +02:00
|
|
|
|
|
|
|
if(lookup != NULL)
|
|
|
|
{
|
|
|
|
rb_dlink_node *ptr, *nptr;
|
|
|
|
|
2016-04-01 23:35:21 +02:00
|
|
|
notice_client(auth->cid, "*** Did not detect open proxies");
|
|
|
|
|
2016-03-31 07:28:05 +02:00
|
|
|
RB_DLINK_FOREACH_SAFE(ptr, nptr, lookup->scans.head)
|
|
|
|
{
|
2016-07-13 07:17:21 +02:00
|
|
|
struct opm_scan *scan = (struct opm_scan *)ptr->data;
|
2016-03-31 07:28:05 +02:00
|
|
|
|
|
|
|
rb_close(scan->F);
|
|
|
|
rb_free(scan);
|
|
|
|
}
|
|
|
|
|
|
|
|
rb_free(lookup);
|
2016-04-04 10:58:46 +02:00
|
|
|
|
2016-04-05 11:31:22 +02:00
|
|
|
set_provider_data(auth, SELF_PID, NULL);
|
|
|
|
set_provider_timeout_absolute(auth, SELF_PID, 0);
|
|
|
|
provider_done(auth, SELF_PID);
|
2016-05-01 12:31:05 +02:00
|
|
|
|
|
|
|
auth_client_unref(auth);
|
2016-03-31 07:28:05 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-03 07:05:53 +02:00
|
|
|
static void
|
|
|
|
opm_destroy(void)
|
|
|
|
{
|
2016-05-01 02:20:12 +02:00
|
|
|
struct auth_client *auth;
|
|
|
|
rb_dictionary_iter iter;
|
2016-04-03 07:05:53 +02:00
|
|
|
|
|
|
|
/* Nuke all opm lookups */
|
2016-07-13 07:17:21 +02:00
|
|
|
void *elem;
|
|
|
|
RB_DICTIONARY_FOREACH(elem, &iter, auth_clients)
|
2016-04-03 07:05:53 +02:00
|
|
|
{
|
2016-07-13 07:17:21 +02:00
|
|
|
auth = (auth_client *)elem;
|
2016-04-03 07:05:53 +02:00
|
|
|
opm_cancel(auth);
|
2016-05-01 12:31:05 +02:00
|
|
|
/* auth is now invalid as we have no reference */
|
2016-04-03 07:05:53 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-03-31 07:28:05 +02:00
|
|
|
static void
|
2016-08-15 06:54:32 +02:00
|
|
|
add_conf_opm_timeout(const char *key, int parc, const char **parv)
|
2016-03-31 07:28:05 +02:00
|
|
|
{
|
|
|
|
int timeout = atoi(parv[0]);
|
|
|
|
|
|
|
|
if(timeout < 0)
|
|
|
|
{
|
|
|
|
warn_opers(L_CRIT, "opm: opm timeout < 0 (value: %d)", timeout);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
opm_timeout = timeout;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2016-08-15 06:54:32 +02:00
|
|
|
set_opm_enabled(const char *key, int parc, const char **parv)
|
2016-03-31 07:28:05 +02:00
|
|
|
{
|
2016-04-02 00:16:17 +02:00
|
|
|
bool enable = (*parv[0] == '1');
|
|
|
|
|
|
|
|
if(!enable)
|
2016-04-01 23:35:21 +02:00
|
|
|
{
|
2016-04-02 00:16:17 +02:00
|
|
|
if(listeners[LISTEN_IPV4].F != NULL || listeners[LISTEN_IPV6].F != NULL)
|
2016-04-01 23:35:21 +02:00
|
|
|
{
|
2016-05-01 02:20:12 +02:00
|
|
|
struct auth_client *auth;
|
|
|
|
rb_dictionary_iter iter;
|
2016-04-01 23:35:21 +02:00
|
|
|
|
|
|
|
/* Close the listening socket */
|
|
|
|
if(listeners[LISTEN_IPV4].F != NULL)
|
|
|
|
rb_close(listeners[LISTEN_IPV4].F);
|
|
|
|
|
|
|
|
if(listeners[LISTEN_IPV6].F != NULL)
|
|
|
|
rb_close(listeners[LISTEN_IPV6].F);
|
|
|
|
|
2016-04-02 00:16:17 +02:00
|
|
|
listeners[LISTEN_IPV4].F = listeners[LISTEN_IPV6].F = NULL;
|
|
|
|
|
2016-07-13 07:17:21 +02:00
|
|
|
void *elem;
|
|
|
|
RB_DICTIONARY_FOREACH(elem, &iter, auth_clients)
|
2016-04-01 23:35:21 +02:00
|
|
|
{
|
2016-07-13 07:17:21 +02:00
|
|
|
auth = (auth_client *)elem;
|
2016-04-01 23:35:21 +02:00
|
|
|
opm_cancel(auth);
|
2016-05-01 12:31:05 +02:00
|
|
|
/* auth is now invalid as we have no reference */
|
2016-04-01 23:35:21 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-03-31 07:28:05 +02:00
|
|
|
else
|
2016-04-02 00:16:17 +02:00
|
|
|
{
|
|
|
|
if(listeners[LISTEN_IPV4].ip[0] != '\0' && listeners[LISTEN_IPV4].port != 0)
|
|
|
|
{
|
2016-04-06 15:34:39 +02:00
|
|
|
if(listeners[LISTEN_IPV4].F == NULL)
|
|
|
|
/* Pre-configured IP/port, just re-establish */
|
|
|
|
create_listener(listeners[LISTEN_IPV4].ip, listeners[LISTEN_IPV4].port);
|
2016-04-02 00:16:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if(listeners[LISTEN_IPV6].ip[0] != '\0' && listeners[LISTEN_IPV6].port != 0)
|
|
|
|
{
|
2016-04-06 15:34:39 +02:00
|
|
|
if(listeners[LISTEN_IPV6].F == NULL)
|
|
|
|
/* Pre-configured IP/port, just re-establish */
|
|
|
|
create_listener(listeners[LISTEN_IPV6].ip, listeners[LISTEN_IPV6].port);
|
2016-04-02 00:16:17 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
opm_enable = enable;
|
2016-03-31 07:28:05 +02:00
|
|
|
}
|
|
|
|
|
2016-04-02 07:47:02 +02:00
|
|
|
static void
|
2016-08-15 06:54:32 +02:00
|
|
|
set_opm_listener(const char *key, int parc, const char **parv)
|
2016-03-31 07:28:05 +02:00
|
|
|
{
|
2016-04-02 07:47:02 +02:00
|
|
|
const char *ip = parv[0];
|
|
|
|
int iport = atoi(parv[1]);
|
2016-03-31 07:28:05 +02:00
|
|
|
|
2016-04-02 07:47:02 +02:00
|
|
|
if(iport > 65535 || iport <= 0)
|
2016-03-31 07:28:05 +02:00
|
|
|
{
|
2016-04-02 07:47:02 +02:00
|
|
|
warn_opers(L_CRIT, "OPM: got a bad listener: %s:%s", parv[0], parv[1]);
|
2016-03-31 07:28:05 +02:00
|
|
|
exit(EX_PROVIDER_ERROR);
|
|
|
|
}
|
|
|
|
|
2016-04-02 07:47:02 +02:00
|
|
|
create_listener(ip, (uint16_t)iport);
|
|
|
|
}
|
2016-04-02 00:16:17 +02:00
|
|
|
|
2016-04-02 07:47:02 +02:00
|
|
|
static void
|
2016-08-15 06:54:32 +02:00
|
|
|
create_opm_scanner(const char *key, int parc, const char **parv)
|
2016-04-02 07:47:02 +02:00
|
|
|
{
|
|
|
|
int iport = atoi(parv[1]);
|
2016-07-13 07:17:21 +02:00
|
|
|
struct opm_proxy *proxy = (opm_proxy *)rb_malloc(sizeof(struct opm_proxy));
|
2016-04-02 00:16:17 +02:00
|
|
|
|
2016-04-02 07:47:02 +02:00
|
|
|
if(iport <= 0 || iport > 65535)
|
2016-04-02 00:16:17 +02:00
|
|
|
{
|
2016-04-02 07:47:02 +02:00
|
|
|
warn_opers(L_CRIT, "OPM: got a bad scanner: %s (port %s)", parv[0], parv[1]);
|
|
|
|
exit(EX_PROVIDER_ERROR);
|
|
|
|
}
|
2016-04-02 00:16:17 +02:00
|
|
|
|
2016-04-02 07:47:02 +02:00
|
|
|
proxy->port = (uint16_t)iport;
|
2016-03-31 07:28:05 +02:00
|
|
|
|
2016-04-02 07:47:02 +02:00
|
|
|
switch((proxy->proto = get_protocol_from_string(parv[0])))
|
|
|
|
{
|
|
|
|
case PROTO_SOCKS4:
|
|
|
|
snprintf(proxy->note, sizeof(proxy->note), "socks4:%hu", proxy->port);
|
2016-04-03 01:37:59 +02:00
|
|
|
proxy->ssl = false;
|
2016-04-06 16:22:24 +02:00
|
|
|
proxy->callback = socks4_connected;
|
2016-04-02 07:47:02 +02:00
|
|
|
break;
|
|
|
|
case PROTO_SOCKS5:
|
|
|
|
snprintf(proxy->note, sizeof(proxy->note), "socks5:%hu", proxy->port);
|
2016-04-03 01:37:59 +02:00
|
|
|
proxy->ssl = false;
|
2016-04-06 16:22:24 +02:00
|
|
|
proxy->callback = socks5_connected;
|
2016-04-02 07:47:02 +02:00
|
|
|
break;
|
2016-04-02 10:11:12 +02:00
|
|
|
case PROTO_HTTP_CONNECT:
|
|
|
|
snprintf(proxy->note, sizeof(proxy->note), "httpconnect:%hu", proxy->port);
|
2016-04-03 01:37:59 +02:00
|
|
|
proxy->ssl = false;
|
2016-04-06 16:22:24 +02:00
|
|
|
proxy->callback = http_connect_connected;
|
2016-04-03 01:37:59 +02:00
|
|
|
break;
|
|
|
|
case PROTO_HTTPS_CONNECT:
|
|
|
|
snprintf(proxy->note, sizeof(proxy->note), "httpsconnect:%hu", proxy->port);
|
2016-04-06 16:22:24 +02:00
|
|
|
proxy->callback = http_connect_connected;
|
2016-04-03 01:37:59 +02:00
|
|
|
proxy->ssl = true;
|
2016-04-02 10:11:12 +02:00
|
|
|
break;
|
2016-04-02 07:47:02 +02:00
|
|
|
default:
|
|
|
|
warn_opers(L_CRIT, "OPM: got an unknown proxy type: %s (port %hu)", parv[0], proxy->port);
|
|
|
|
exit(EX_PROVIDER_ERROR);
|
2016-04-02 00:16:17 +02:00
|
|
|
}
|
2016-03-31 07:28:05 +02:00
|
|
|
|
2016-04-02 07:47:02 +02:00
|
|
|
if(find_proxy_scanner(proxy->proto, proxy->port) != NULL)
|
2016-03-31 07:28:05 +02:00
|
|
|
{
|
2016-04-02 07:47:02 +02:00
|
|
|
warn_opers(L_CRIT, "OPM: got a duplicate scanner: %s (port %hu)", parv[0], proxy->port);
|
2016-03-31 07:28:05 +02:00
|
|
|
exit(EX_PROVIDER_ERROR);
|
|
|
|
}
|
|
|
|
|
2016-04-02 07:47:02 +02:00
|
|
|
rb_dlinkAdd(proxy, &proxy->node, &proxy_scanners);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2016-08-15 06:54:32 +02:00
|
|
|
delete_opm_scanner(const char *key, int parc, const char **parv)
|
2016-04-02 07:47:02 +02:00
|
|
|
{
|
2016-05-01 02:20:12 +02:00
|
|
|
struct auth_client *auth;
|
2016-04-02 07:47:02 +02:00
|
|
|
struct opm_proxy *proxy;
|
|
|
|
protocol_t proto = get_protocol_from_string(parv[0]);
|
|
|
|
int iport = atoi(parv[1]);
|
2016-05-01 02:20:12 +02:00
|
|
|
rb_dictionary_iter iter;
|
2016-04-02 07:47:02 +02:00
|
|
|
|
|
|
|
if(iport <= 0 || iport > 65535)
|
2016-03-31 07:28:05 +02:00
|
|
|
{
|
2016-04-02 07:47:02 +02:00
|
|
|
warn_opers(L_CRIT, "OPM: got a bad scanner to delete: %s (port %s)", parv[0], parv[1]);
|
2016-03-31 07:28:05 +02:00
|
|
|
exit(EX_PROVIDER_ERROR);
|
|
|
|
}
|
|
|
|
|
2016-04-02 07:47:02 +02:00
|
|
|
if(proto == PROTO_NONE)
|
2016-03-31 07:28:05 +02:00
|
|
|
{
|
2016-04-02 07:47:02 +02:00
|
|
|
warn_opers(L_CRIT, "OPM: got an unknown proxy type to delete: %s (port %d)", parv[0], iport);
|
|
|
|
exit(EX_PROVIDER_ERROR);
|
2016-03-31 07:28:05 +02:00
|
|
|
}
|
|
|
|
|
2016-04-02 07:47:02 +02:00
|
|
|
if((proxy = find_proxy_scanner(proto, (uint16_t)iport)) == NULL)
|
2016-03-31 07:28:05 +02:00
|
|
|
{
|
2016-04-02 07:47:02 +02:00
|
|
|
warn_opers(L_CRIT, "OPM: cannot find proxy to delete: %s (port %d)", parv[0], iport);
|
|
|
|
exit(EX_PROVIDER_ERROR);
|
2016-03-31 07:28:05 +02:00
|
|
|
}
|
|
|
|
|
2016-04-02 07:47:02 +02:00
|
|
|
/* Abort remaining clients on this scanner */
|
2016-07-13 07:17:21 +02:00
|
|
|
void *elem;
|
|
|
|
RB_DICTIONARY_FOREACH(elem, &iter, auth_clients)
|
2016-04-02 07:47:02 +02:00
|
|
|
{
|
2016-05-01 02:20:12 +02:00
|
|
|
rb_dlink_node *ptr;
|
2016-07-13 07:17:21 +02:00
|
|
|
auth = (auth_client *)elem;
|
|
|
|
struct opm_lookup *lookup = (opm_lookup *)get_provider_data(auth, SELF_PID);
|
2016-03-31 09:13:27 +02:00
|
|
|
|
2016-04-02 07:47:02 +02:00
|
|
|
if(lookup == NULL)
|
|
|
|
continue;
|
2016-03-31 07:28:05 +02:00
|
|
|
|
2016-05-01 12:31:05 +02:00
|
|
|
auth_client_ref(auth);
|
|
|
|
|
2016-05-01 02:20:12 +02:00
|
|
|
RB_DLINK_FOREACH(ptr, lookup->scans.head)
|
2016-04-02 07:47:02 +02:00
|
|
|
{
|
2016-07-13 07:17:21 +02:00
|
|
|
struct opm_scan *scan = (struct opm_scan *)ptr->data;
|
2016-03-31 07:28:05 +02:00
|
|
|
|
2016-04-02 07:47:02 +02:00
|
|
|
if(scan->proxy->port == proxy->port && scan->proxy->proto == proxy->proto)
|
|
|
|
{
|
|
|
|
/* Match */
|
2016-04-30 08:58:42 +02:00
|
|
|
rb_dlinkDelete(&scan->node, &lookup->scans);
|
2016-04-02 07:47:02 +02:00
|
|
|
rb_free(scan);
|
2016-03-31 07:28:05 +02:00
|
|
|
|
2016-04-03 05:45:52 +02:00
|
|
|
if(rb_dlink_list_length(&lookup->scans) == 0)
|
2016-04-02 07:47:02 +02:00
|
|
|
opm_cancel(auth);
|
2016-03-31 07:28:05 +02:00
|
|
|
|
2016-04-02 07:47:02 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2016-05-01 12:31:05 +02:00
|
|
|
|
|
|
|
auth_client_unref(auth);
|
2016-04-02 07:47:02 +02:00
|
|
|
}
|
|
|
|
|
2016-04-30 08:58:42 +02:00
|
|
|
rb_dlinkDelete(&proxy->node, &proxy_scanners);
|
2016-04-02 07:47:02 +02:00
|
|
|
rb_free(proxy);
|
2016-04-03 05:31:19 +02:00
|
|
|
|
2016-04-03 05:45:52 +02:00
|
|
|
if(rb_dlink_list_length(&proxy_scanners) == 0)
|
2016-04-03 05:31:19 +02:00
|
|
|
opm_enable = false;
|
2016-04-02 00:16:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2016-08-15 06:54:32 +02:00
|
|
|
delete_opm_scanner_all(const char *key, int parc, const char **parv)
|
2016-04-02 00:16:17 +02:00
|
|
|
{
|
2016-05-01 02:20:12 +02:00
|
|
|
struct auth_client *auth;
|
2016-04-02 07:47:02 +02:00
|
|
|
rb_dlink_node *ptr, *nptr;
|
2016-05-01 02:20:12 +02:00
|
|
|
rb_dictionary_iter iter;
|
2016-04-02 00:16:17 +02:00
|
|
|
|
2016-04-02 07:47:02 +02:00
|
|
|
RB_DLINK_FOREACH_SAFE(ptr, nptr, proxy_scanners.head)
|
2016-04-02 00:16:17 +02:00
|
|
|
{
|
2016-04-02 07:47:02 +02:00
|
|
|
rb_free(ptr->data);
|
|
|
|
rb_dlinkDelete(ptr, &proxy_scanners);
|
2016-04-02 00:16:17 +02:00
|
|
|
}
|
|
|
|
|
2016-07-13 07:17:21 +02:00
|
|
|
void *elem;
|
|
|
|
RB_DICTIONARY_FOREACH(elem, &iter, auth_clients)
|
2016-04-02 07:47:02 +02:00
|
|
|
{
|
2016-07-13 07:17:21 +02:00
|
|
|
auth = (auth_client *)elem;
|
2016-04-02 07:47:02 +02:00
|
|
|
opm_cancel(auth);
|
2016-05-01 12:31:05 +02:00
|
|
|
/* auth is now invalid as we have no reference */
|
2016-04-02 07:47:02 +02:00
|
|
|
}
|
2016-04-03 05:31:19 +02:00
|
|
|
|
|
|
|
opm_enable = false;
|
2016-03-31 07:28:05 +02:00
|
|
|
}
|
|
|
|
|
2016-04-12 16:33:51 +02:00
|
|
|
static void
|
2016-08-15 06:54:32 +02:00
|
|
|
delete_opm_listener_all(const char *key, int parc, const char **parv)
|
2016-04-12 16:33:51 +02:00
|
|
|
{
|
|
|
|
if(listeners[LISTEN_IPV4].F != NULL)
|
|
|
|
rb_close(listeners[LISTEN_IPV4].F);
|
|
|
|
|
|
|
|
#ifdef RB_IPV6
|
|
|
|
if(listeners[LISTEN_IPV6].F != NULL)
|
|
|
|
rb_close(listeners[LISTEN_IPV6].F);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
memset(&listeners, 0, sizeof(listeners));
|
|
|
|
}
|
|
|
|
|
2016-04-02 07:47:02 +02:00
|
|
|
|
2016-03-31 07:28:05 +02:00
|
|
|
struct auth_opts_handler opm_options[] =
|
|
|
|
{
|
|
|
|
{ "opm_timeout", 1, add_conf_opm_timeout },
|
|
|
|
{ "opm_enabled", 1, set_opm_enabled },
|
|
|
|
{ "opm_listener", 2, set_opm_listener },
|
2016-04-12 16:33:51 +02:00
|
|
|
{ "opm_listener_del_all", 0, delete_opm_listener_all },
|
2016-04-02 07:47:02 +02:00
|
|
|
{ "opm_scanner", 2, create_opm_scanner },
|
|
|
|
{ "opm_scanner_del", 2, delete_opm_scanner },
|
|
|
|
{ "opm_scanner_del_all", 0, delete_opm_scanner_all },
|
2016-03-31 07:28:05 +02:00
|
|
|
{ NULL, 0, NULL },
|
|
|
|
};
|
|
|
|
|
2016-07-23 00:11:39 +02:00
|
|
|
struct auth_provider opm_provider =
|
|
|
|
[]{
|
2016-07-13 07:17:21 +02:00
|
|
|
auth_provider ap {0};
|
|
|
|
ap.name = "opm";
|
|
|
|
ap.letter = 'O';
|
|
|
|
ap.destroy = opm_destroy;
|
|
|
|
ap.start = opm_start;
|
|
|
|
ap.cancel = opm_cancel;
|
|
|
|
ap.timeout = opm_cancel;
|
|
|
|
ap.completed = opm_initiate;
|
|
|
|
ap.opt_handlers = opm_options;
|
|
|
|
return ap;
|
2016-07-23 00:11:39 +02:00
|
|
|
}();
|