0
0
Fork 0
mirror of https://github.com/matrix-construct/construct synced 2024-12-25 23:14:13 +01:00

ircd/authd: split out authd lifecycle functions from actual DNS functions

This commit is contained in:
William Pitcock 2016-01-06 04:41:57 -06:00
parent 7d2852b4d1
commit fb7d74efef
6 changed files with 213 additions and 136 deletions

36
include/authd.h Normal file
View file

@ -0,0 +1,36 @@
/*
* charybdis
* authd.h: A header with the authd functions.
*
* Copyright (C) 1990 Jarkko Oikarinen and University of Oulu, Co Center
* Copyright (C) 1996-2002 Hybrid Development Team
* Copyright (C) 2002-2012 ircd-ratbox development team
* Copyright (C) 2016 William Pitcock <nenolod@dereferenced.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
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
* USA
*/
#ifndef CHARYBDIS_AUTHD_H
#define CHARYBDIS_AUTHD_H
extern rb_helper *authd_helper;
void init_authd(void);
void restart_authd(void);
void rehash_authd(void);
void check_authd(void);
#endif

View file

@ -28,14 +28,17 @@
typedef void (*DNSCB)(const char *res, int status, int aftype, void *data);
extern rb_helper *authd_helper;
void init_authd(void);
void restart_authd(void);
void rehash_authd(void);
void check_authd(void);
uint16_t lookup_hostname(const char *hostname, int aftype, DNSCB callback, void *data);
uint16_t lookup_ip(const char *hostname, int aftype, DNSCB callback, void *data);
void cancel_lookup(uint16_t xid);
void dns_results_callback(const char *callid, const char *status, const char *aftype, const char *results);
void report_dns_servers(struct Client *);
#endif

View file

@ -15,6 +15,7 @@ version.c: version.c.SH ../CREDITS ../include/patchlevel.h ../include/serno.h
touch version.c.SH
libircd_la_SOURCES = \
authd.c \
bandbi.c \
blacklist.c \
cache.c \

View file

@ -136,13 +136,13 @@ am__uninstall_files_from_dir = { \
am__installdirs = "$(DESTDIR)$(libircddir)" "$(DESTDIR)$(bindir)"
LTLIBRARIES = $(libircd_LTLIBRARIES)
libircd_la_DEPENDENCIES =
am_libircd_la_OBJECTS = bandbi.lo blacklist.lo cache.lo capability.lo \
channel.lo chmode.lo class.lo client.lo dns.lo extban.lo \
getopt.lo hash.lo hook.lo hostmask.lo ipv4_from_ipv6.lo \
irc_dictionary.lo ircd.lo ircd_parser.lo ircd_lexer.lo \
ircd_signal.lo listener.lo logger.lo match.lo modules.lo \
monitor.lo newconf.lo operhash.lo packet.lo parse.lo \
privilege.lo ratelimit.lo res.lo reslib.lo reject.lo \
am_libircd_la_OBJECTS = authd.lo bandbi.lo blacklist.lo cache.lo \
capability.lo channel.lo chmode.lo class.lo client.lo dns.lo \
extban.lo getopt.lo hash.lo hook.lo hostmask.lo \
ipv4_from_ipv6.lo irc_dictionary.lo ircd.lo ircd_parser.lo \
ircd_lexer.lo ircd_signal.lo listener.lo logger.lo match.lo \
modules.lo monitor.lo newconf.lo operhash.lo packet.lo \
parse.lo privilege.lo ratelimit.lo res.lo reslib.lo reject.lo \
restart.lo s_auth.lo s_conf.lo s_newconf.lo s_serv.lo \
s_user.lo scache.lo send.lo snomask.lo sslproc.lo \
substitution.lo supported.lo tgchange.lo version.lo whowas.lo
@ -429,6 +429,7 @@ AM_LDFLAGS = -L$(top_srcdir)/libratbox/src -export-dynamic -avoid-version -no-un
AM_YFLAGS = -d
BUILT_SOURCES = version.c
libircd_la_SOURCES = \
authd.c \
bandbi.c \
blacklist.c \
cache.c \
@ -620,6 +621,7 @@ mostlyclean-compile:
distclean-compile:
-rm -f *.tab.c
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/authd.Plo@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/bandbi.Plo@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/blacklist.Plo@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/cache.Plo@am__quote@

161
ircd/authd.c Normal file
View file

@ -0,0 +1,161 @@
/*
* authd.c: An interface to authd.
* (based somewhat on ircd-ratbox dns.c)
*
* Copyright (C) 2005 Aaron Sethman <androsyn@ratbox.org>
* Copyright (C) 2005-2012 ircd-ratbox development team
* Copyright (C) 2016 William Pitcock <nenolod@dereferenced.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
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
* USA
*/
#include <stdinc.h>
#include <ratbox_lib.h>
#include <struct.h>
#include <ircd_defs.h>
#include <parse.h>
#include <authd.h>
#include <match.h>
#include <logger.h>
#include <s_conf.h>
#include <client.h>
#include <send.h>
#include <numeric.h>
#include <msg.h>
#include <dns.h>
static int start_authd(void);
static void parse_authd_reply(rb_helper * helper);
static void restart_authd_cb(rb_helper * helper);
rb_helper *authd_helper;
static char *authd_path;
static int
start_authd(void)
{
char fullpath[PATH_MAX + 1];
#ifdef _WIN32
const char *suffix = ".exe";
#else
const char *suffix = "";
#endif
if(authd_path == NULL)
{
snprintf(fullpath, sizeof(fullpath), "%s/authd%s", PKGLIBEXECDIR, suffix);
if(access(fullpath, X_OK) == -1)
{
snprintf(fullpath, sizeof(fullpath), "%s/libexec/charybdis/authd%s",
ConfigFileEntry.dpath, suffix);
if(access(fullpath, X_OK) == -1)
{
ilog(L_MAIN,
"Unable to execute authd in %s or %s/libexec/charybdis",
PKGLIBEXECDIR, ConfigFileEntry.dpath);
sendto_realops_snomask(SNO_GENERAL, L_ALL,
"Unable to execute resolver in %s or %s/libexec/charybdis",
PKGLIBEXECDIR, ConfigFileEntry.dpath);
return 1;
}
}
authd_path = rb_strdup(fullpath);
}
authd_helper = rb_helper_start("authd", authd_path, parse_authd_reply, restart_authd_cb);
if(authd_helper == NULL)
{
ilog(L_MAIN, "Unable to start authd helper: %s", strerror(errno));
sendto_realops_snomask(SNO_GENERAL, L_ALL, "Unable to start authd helper: %s", strerror(errno));
return 1;
}
ilog(L_MAIN, "authd helper started");
sendto_realops_snomask(SNO_GENERAL, L_ALL, "authd helper started");
rb_helper_run(authd_helper);
return 0;
}
static void
parse_authd_reply(rb_helper * helper)
{
ssize_t len;
int parc;
char dnsBuf[READBUF_SIZE];
char *parv[MAXPARA + 1];
while((len = rb_helper_read(helper, dnsBuf, sizeof(dnsBuf))) > 0)
{
parc = rb_string_to_array(dnsBuf, parv, MAXPARA+1);
if(*parv[0] == 'R')
{
if(parc != 5)
{
ilog(L_MAIN, "authd sent a result with wrong number of arguments: got %d", parc);
restart_authd();
return;
}
dns_results_callback(parv[1], parv[2], parv[3], parv[4]);
}
else
return;
}
}
void
init_authd(void)
{
if(start_authd())
{
ilog(L_MAIN, "Unable to start authd helper: %s", strerror(errno));
exit(0);
}
}
static void
restart_authd_cb(rb_helper * helper)
{
ilog(L_MAIN, "authd: restart_authd_cb called, authd died?");
sendto_realops_snomask(SNO_GENERAL, L_ALL, "authd - restart_authd_cb called, authd died?");
if(helper != NULL)
{
rb_helper_close(helper);
authd_helper = NULL;
}
start_authd();
}
void
restart_authd(void)
{
restart_authd_cb(authd_helper);
}
void
rehash_authd(void)
{
rb_helper_write(authd_helper, "R");
}
void
check_authd(void)
{
if(authd_helper == NULL)
restart_authd();
}

View file

@ -43,12 +43,6 @@
static void submit_dns(const char, uint16_t id, int aftype, const char *addr);
static int start_authd(void);
static void parse_authd_reply(rb_helper * helper);
static void restart_authd_cb(rb_helper * helper);
static rb_helper *authd_helper;
struct dnsreq
{
DNSCB callback;
@ -76,13 +70,6 @@ assign_dns_id(void)
return (id);
}
static inline void
check_authd(void)
{
if(authd_helper == NULL)
restart_authd();
}
static void
handle_dns_failure(uint16_t xid)
{
@ -158,8 +145,8 @@ lookup_ip(const char *addr, int aftype, DNSCB callback, void *data)
return (nid);
}
static void
results_callback(const char *callid, const char *status, const char *aftype, const char *results)
void
dns_results_callback(const char *callid, const char *status, const char *aftype, const char *results)
{
struct dnsreq *req;
uint16_t nid;
@ -191,56 +178,6 @@ results_callback(const char *callid, const char *status, const char *aftype, con
req->data = NULL;
}
static char *resolver_path;
static int
start_authd(void)
{
char fullpath[PATH_MAX + 1];
#ifdef _WIN32
const char *suffix = ".exe";
#else
const char *suffix = "";
#endif
if(resolver_path == NULL)
{
snprintf(fullpath, sizeof(fullpath), "%s/authd%s", PKGLIBEXECDIR, suffix);
if(access(fullpath, X_OK) == -1)
{
snprintf(fullpath, sizeof(fullpath), "%s/libexec/charybdis/authd%s",
ConfigFileEntry.dpath, suffix);
if(access(fullpath, X_OK) == -1)
{
ilog(L_MAIN,
"Unable to execute authd in %s or %s/libexec/charybdis",
PKGLIBEXECDIR, ConfigFileEntry.dpath);
sendto_realops_snomask(SNO_GENERAL, L_ALL,
"Unable to execute resolver in %s or %s/libexec/charybdis",
PKGLIBEXECDIR, ConfigFileEntry.dpath);
return 1;
}
}
resolver_path = rb_strdup(fullpath);
}
authd_helper = rb_helper_start("authd", resolver_path, parse_authd_reply, restart_authd_cb);
if(authd_helper == NULL)
{
ilog(L_MAIN, "Unable to start authd helper: %s", strerror(errno));
sendto_realops_snomask(SNO_GENERAL, L_ALL, "Unable to start authd helper: %s", strerror(errno));
return 1;
}
ilog(L_MAIN, "resolver helper started");
sendto_realops_snomask(SNO_GENERAL, L_ALL, "resolver helper started");
rb_helper_run(authd_helper);
return 0;
}
void
report_dns_servers(struct Client *source_p)
{
@ -253,33 +190,6 @@ report_dns_servers(struct Client *source_p)
#endif
}
static void
parse_authd_reply(rb_helper * helper)
{
ssize_t len;
int parc;
char dnsBuf[READBUF_SIZE];
char *parv[MAXPARA + 1];
while((len = rb_helper_read(helper, dnsBuf, sizeof(dnsBuf))) > 0)
{
parc = rb_string_to_array(dnsBuf, parv, MAXPARA+1);
if(*parv[0] == 'R')
{
if(parc != 5)
{
ilog(L_MAIN, "authd sent a result with wrong number of arguments: got %d", parc);
restart_authd();
return;
}
results_callback(parv[1], parv[2], parv[3], parv[4]);
}
else
return;
}
}
static void
submit_dns(char type, uint16_t nid, int aftype, const char *addr)
{
@ -290,39 +200,3 @@ submit_dns(char type, uint16_t nid, int aftype, const char *addr)
}
rb_helper_write(authd_helper, "%c %x %d %s", type, nid, aftype, addr);
}
void
init_authd(void)
{
if(start_authd())
{
ilog(L_MAIN, "Unable to start authd helper: %s", strerror(errno));
exit(0);
}
}
static void
restart_authd_cb(rb_helper * helper)
{
ilog(L_MAIN, "authd: restart_authd_cb called, authd died?");
sendto_realops_snomask(SNO_GENERAL, L_ALL, "authd - restart_authd_cb called, authd died?");
if(helper != NULL)
{
rb_helper_close(helper);
authd_helper = NULL;
}
start_authd();
}
void
restart_authd(void)
{
restart_authd_cb(authd_helper);
}
void
rehash_resolver(void)
{
rb_helper_write(authd_helper, "R");
}