mirror of
https://github.com/matrix-construct/construct
synced 2024-11-16 06:51:08 +01:00
authd: implement DNS module
This commit is contained in:
parent
f3e11b1d6f
commit
8cf45447e2
6 changed files with 224 additions and 4 deletions
|
@ -3,5 +3,5 @@ AM_CFLAGS=$(WARNFLAGS)
|
||||||
AM_CPPFLAGS = -I../include -I../libratbox/include
|
AM_CPPFLAGS = -I../include -I../libratbox/include
|
||||||
|
|
||||||
|
|
||||||
authd_SOURCES = authd.c res.c reslib.c
|
authd_SOURCES = authd.c res.c reslib.c dns.c
|
||||||
authd_LDADD = ../libratbox/src/libratbox.la
|
authd_LDADD = ../libratbox/src/libratbox.la
|
||||||
|
|
|
@ -107,7 +107,8 @@ CONFIG_CLEAN_FILES =
|
||||||
CONFIG_CLEAN_VPATH_FILES =
|
CONFIG_CLEAN_VPATH_FILES =
|
||||||
am__installdirs = "$(DESTDIR)$(pkglibexecdir)"
|
am__installdirs = "$(DESTDIR)$(pkglibexecdir)"
|
||||||
PROGRAMS = $(pkglibexec_PROGRAMS)
|
PROGRAMS = $(pkglibexec_PROGRAMS)
|
||||||
am_authd_OBJECTS = authd.$(OBJEXT) res.$(OBJEXT) reslib.$(OBJEXT)
|
am_authd_OBJECTS = authd.$(OBJEXT) res.$(OBJEXT) reslib.$(OBJEXT) \
|
||||||
|
dns.$(OBJEXT)
|
||||||
authd_OBJECTS = $(am_authd_OBJECTS)
|
authd_OBJECTS = $(am_authd_OBJECTS)
|
||||||
authd_DEPENDENCIES = ../libratbox/src/libratbox.la
|
authd_DEPENDENCIES = ../libratbox/src/libratbox.la
|
||||||
AM_V_lt = $(am__v_lt_@AM_V@)
|
AM_V_lt = $(am__v_lt_@AM_V@)
|
||||||
|
@ -358,7 +359,7 @@ top_builddir = @top_builddir@
|
||||||
top_srcdir = @top_srcdir@
|
top_srcdir = @top_srcdir@
|
||||||
AM_CFLAGS = $(WARNFLAGS)
|
AM_CFLAGS = $(WARNFLAGS)
|
||||||
AM_CPPFLAGS = -I../include -I../libratbox/include
|
AM_CPPFLAGS = -I../include -I../libratbox/include
|
||||||
authd_SOURCES = authd.c res.c reslib.c
|
authd_SOURCES = authd.c res.c reslib.c dns.c
|
||||||
authd_LDADD = ../libratbox/src/libratbox.la
|
authd_LDADD = ../libratbox/src/libratbox.la
|
||||||
all: all-am
|
all: all-am
|
||||||
|
|
||||||
|
@ -454,6 +455,7 @@ distclean-compile:
|
||||||
-rm -f *.tab.c
|
-rm -f *.tab.c
|
||||||
|
|
||||||
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/authd.Po@am__quote@
|
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/authd.Po@am__quote@
|
||||||
|
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/dns.Po@am__quote@
|
||||||
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/res.Po@am__quote@
|
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/res.Po@am__quote@
|
||||||
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/reslib.Po@am__quote@
|
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/reslib.Po@am__quote@
|
||||||
|
|
||||||
|
|
|
@ -24,7 +24,9 @@
|
||||||
#define MAXPARA 10
|
#define MAXPARA 10
|
||||||
|
|
||||||
rb_helper *authd_helper = NULL;
|
rb_helper *authd_helper = NULL;
|
||||||
authd_cmd_handler authd_cmd_handlers[255] = {};
|
authd_cmd_handler authd_cmd_handlers[255] = {
|
||||||
|
['D'] = resolve_dns,
|
||||||
|
};
|
||||||
|
|
||||||
static void
|
static void
|
||||||
parse_request(rb_helper *helper)
|
parse_request(rb_helper *helper)
|
||||||
|
@ -103,6 +105,11 @@ main(int argc, char *argv[])
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
rb_set_time();
|
||||||
|
setup_signals();
|
||||||
|
init_resolver();
|
||||||
|
rb_init_prng(NULL, RB_PRNG_DEFAULT);
|
||||||
|
|
||||||
rb_helper_loop(authd_helper, 0);
|
rb_helper_loop(authd_helper, 0);
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
|
|
35
authd/authd.h
Normal file
35
authd/authd.h
Normal file
|
@ -0,0 +1,35 @@
|
||||||
|
/* authd/dns.h - header for authd DNS functions
|
||||||
|
* Copyright (c) 2016 William Pitcock <nenolod@dereferenced.org>
|
||||||
|
*
|
||||||
|
* 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.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef _AUTHD_H
|
||||||
|
#define _AUTHD_H
|
||||||
|
|
||||||
|
#include <ratbox_lib.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
#include "setup.h"
|
||||||
|
#include "common.h"
|
||||||
|
|
||||||
|
extern rb_helper *authd_helper;
|
||||||
|
|
||||||
|
typedef void (*authd_cmd_handler)(int parc, char *parv[]);
|
||||||
|
extern authd_cmd_handler authd_cmd_handlers[255];
|
||||||
|
|
||||||
|
#endif
|
137
authd/dns.c
Normal file
137
authd/dns.c
Normal file
|
@ -0,0 +1,137 @@
|
||||||
|
/* authd/dns.h - header for authd DNS functions
|
||||||
|
* Copyright (c) 2016 William Pitcock <nenolod@dereferenced.org>
|
||||||
|
*
|
||||||
|
* 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 "dns.h"
|
||||||
|
|
||||||
|
static void
|
||||||
|
submit_dns_answer(void *userdata, struct DNSReply *reply)
|
||||||
|
{
|
||||||
|
struct dns_request *req = userdata;
|
||||||
|
char response[64] = "*";
|
||||||
|
char status = 'E';
|
||||||
|
|
||||||
|
if (reply == NULL)
|
||||||
|
{
|
||||||
|
rb_helper_write(authd_helper, "E %s E %c *\n", req->reqid, req->type);
|
||||||
|
goto cleanup;
|
||||||
|
}
|
||||||
|
|
||||||
|
switch (req->type)
|
||||||
|
{
|
||||||
|
case '4':
|
||||||
|
if (GET_SS_FAMILY(&reply->addr) == AF_INET)
|
||||||
|
{
|
||||||
|
status = 'O';
|
||||||
|
rb_inet_ntop_sock((struct sockaddr *) &reply->addr, response, sizeof(response));
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
#ifdef RB_IPV6
|
||||||
|
case '6':
|
||||||
|
if (GET_SS_FAMILY(&reply->addr) == AF_INET6)
|
||||||
|
{
|
||||||
|
char tmpres[63];
|
||||||
|
rb_inet_ntop_sock((struct sockaddr *) &reply->addr, tmpres, sizeof(tmpres));
|
||||||
|
|
||||||
|
if (*tmpres == ':')
|
||||||
|
{
|
||||||
|
rb_strlcpy(response, "0", sizeof(response));
|
||||||
|
rb_strlcat(response, tmpres, sizeof(response));
|
||||||
|
}
|
||||||
|
else
|
||||||
|
rb_strlcpy(response, tmpres, sizeof(response));
|
||||||
|
|
||||||
|
status = 'O';
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
#endif
|
||||||
|
case 'R':
|
||||||
|
{
|
||||||
|
struct sockaddr_in *ip, *ip_fwd;
|
||||||
|
ip = (struct sockaddr_in *) &req->addr;
|
||||||
|
ip_fwd = (struct sockaddr_in *) &reply->addr;
|
||||||
|
|
||||||
|
if(ip->sin_addr.s_addr == ip_fwd->sin_addr.s_addr && strlen(reply->h_name) < 63)
|
||||||
|
{
|
||||||
|
rb_strlcpy(response, reply->h_name, sizeof(response));
|
||||||
|
status = 'O';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
#ifdef RB_IPV6
|
||||||
|
case 'S':
|
||||||
|
{
|
||||||
|
struct sockaddr_in6 *ip, *ip_fwd;
|
||||||
|
ip = (struct sockaddr_in6 *) &req->addr;
|
||||||
|
ip_fwd = (struct sockaddr_in6 *) &reply->addr;
|
||||||
|
|
||||||
|
if(memcmp(&ip->sin6_addr, &ip_fwd->sin6_addr, sizeof(struct in6_addr)) == 0 && strlen(reply->h_name) < 63)
|
||||||
|
{
|
||||||
|
rb_strlcpy(response, reply->h_name, sizeof(response));
|
||||||
|
status = 'O';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
#endif
|
||||||
|
default:
|
||||||
|
exit(7);
|
||||||
|
}
|
||||||
|
|
||||||
|
rb_helper_write(authd_helper, "E %s %c %c %s\n", req->reqid, status, req->type, response);
|
||||||
|
cleanup:
|
||||||
|
rb_free(req);
|
||||||
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
resolve_dns(int parc, char *parv[])
|
||||||
|
{
|
||||||
|
struct dns_request *req;
|
||||||
|
char *requestid = parv[1];
|
||||||
|
char *qtype = parv[2];
|
||||||
|
char *rec = parv[3];
|
||||||
|
int type;
|
||||||
|
|
||||||
|
req = rb_malloc(sizeof(*req));
|
||||||
|
rb_strlcpy(req->reqid, requestid, sizeof(req->reqid));
|
||||||
|
req->type = *qtype;
|
||||||
|
|
||||||
|
switch (req->type)
|
||||||
|
{
|
||||||
|
case '4':
|
||||||
|
type = T_A;
|
||||||
|
if(!rb_inet_pton_sock(rec, (struct sockaddr *) &req->addr))
|
||||||
|
exit(6);
|
||||||
|
break;
|
||||||
|
case '6':
|
||||||
|
type = T_AAAA;
|
||||||
|
if(!rb_inet_pton_sock(rec, (struct sockaddr *) &req->addr))
|
||||||
|
exit(6);
|
||||||
|
break;
|
||||||
|
case 'R':
|
||||||
|
case 'S':
|
||||||
|
type = T_PTR;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
req->query.ptr = req;
|
||||||
|
req->query.callback = submit_dns_answer;
|
||||||
|
|
||||||
|
gethost_byname_type(rec, &req->query, type);
|
||||||
|
}
|
39
authd/dns.h
Normal file
39
authd/dns.h
Normal file
|
@ -0,0 +1,39 @@
|
||||||
|
/* authd/dns.h - header for authd DNS functions
|
||||||
|
* Copyright (c) 2016 William Pitcock <nenolod@dereferenced.org>
|
||||||
|
*
|
||||||
|
* 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.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef _AUTHD_DNS_H
|
||||||
|
#define _AUTHD_DNS_H
|
||||||
|
|
||||||
|
#define DNS_REQ_IDLEN 10
|
||||||
|
|
||||||
|
#include "res.h"
|
||||||
|
#include "reslib.h"
|
||||||
|
|
||||||
|
struct dns_request
|
||||||
|
{
|
||||||
|
struct DNSQuery query;
|
||||||
|
char reqid[DNS_REQ_IDLEN];
|
||||||
|
struct rb_sockaddr_storage addr;
|
||||||
|
char type;
|
||||||
|
};
|
||||||
|
|
||||||
|
extern void resolve_dns(int parc, char *parv[]);
|
||||||
|
|
||||||
|
#endif
|
Loading…
Reference in a new issue