]> jfr.im git - solanum.git/blame - authd/authd.c
authd: plug in new C type.
[solanum.git] / authd / authd.c
CommitLineData
0d73e7db
AC
1/* authd/authd.c - main code for authd
2 * Copyright (c) 2016 William Pitcock <nenolod@dereferenced.org>
3 *
4 * Permission to use, copy, modify, and/or distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice is present in all copies.
7 *
8 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
9 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
10 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
11 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
12 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
13 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
14 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
15 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
16 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
17 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
18 * POSSIBILITY OF SUCH DAMAGE.
19 */
20
f3e11b1d
AC
21#include "authd.h"
22#include "dns.h"
cafe4dd8 23#include "provider.h"
0d73e7db
AC
24
25#define MAXPARA 10
26
6445c1cf 27static void handle_reload(int parc, char *parv[]);
394b8dde
EM
28static void handle_stat(int parc, char *parv[]);
29
f3e11b1d 30rb_helper *authd_helper = NULL;
6f39a80e 31authd_cmd_handler authd_cmd_handlers[256] = {
cafe4dd8 32 ['C'] = handle_new_connection,
8cf45447 33 ['D'] = resolve_dns,
cafe4dd8 34 ['H'] = handle_reload,
394b8dde
EM
35 ['S'] = handle_stat,
36};
37
6f39a80e 38authd_stat_handler authd_stat_handlers[256] = {
394b8dde 39 ['D'] = enumerate_nameservers,
8cf45447 40};
0d73e7db 41
6f39a80e 42authd_reload_handler authd_reload_handlers[256] = {
6445c1cf
EM
43 ['D'] = reload_nameservers,
44};
45
394b8dde
EM
46static void
47handle_stat(int parc, char *parv[])
48{
49 authd_stat_handler handler;
50
51 if(parc < 3)
52 /* XXX Should log this somehow */
53 return;
54
55 if (!(handler = authd_stat_handlers[parv[2][0]]))
56 return;
57
58 handler(parv[1], parv[2][0]);
59}
60
6445c1cf
EM
61static void
62handle_reload(int parc, char *parv[])
63{
64 authd_reload_handler handler;
65
66 if(parc < 2)
67 /* XXX Should log this somehow */
68 return;
69
70 if (!(handler = authd_reload_handlers[parv[1][0]]))
71 return;
72
73 handler(parv[1][0]);
74}
75
0d73e7db
AC
76static void
77parse_request(rb_helper *helper)
78{
79 static char *parv[MAXPARA + 1];
80 static char readbuf[READBUF_SIZE];
81 int parc;
82 int len;
f3e11b1d 83 authd_cmd_handler handler;
0d73e7db
AC
84
85 while((len = rb_helper_read(helper, readbuf, sizeof(readbuf))) > 0)
86 {
87 parc = rb_string_to_array(readbuf, parv, MAXPARA);
88
89 if(parc < 1)
90 continue;
91
f3e11b1d
AC
92 handler = authd_cmd_handlers[parv[0][0]];
93 if (handler != NULL)
94 handler(parc, parv);
0d73e7db
AC
95 }
96}
97
98static void
99error_cb(rb_helper *helper)
100{
101 exit(1);
102}
103
104#ifndef WINDOWS
105static void
106dummy_handler(int sig)
107{
108 return;
109}
110#endif
111
112static void
113setup_signals(void)
114{
115#ifndef WINDOWS
116 struct sigaction act;
117
118 act.sa_flags = 0;
119 act.sa_handler = SIG_IGN;
120 sigemptyset(&act.sa_mask);
121 sigaddset(&act.sa_mask, SIGPIPE);
122 sigaddset(&act.sa_mask, SIGALRM);
123#ifdef SIGTRAP
124 sigaddset(&act.sa_mask, SIGTRAP);
125#endif
126
127#ifdef SIGWINCH
128 sigaddset(&act.sa_mask, SIGWINCH);
129 sigaction(SIGWINCH, &act, 0);
130#endif
131 sigaction(SIGPIPE, &act, 0);
132#ifdef SIGTRAP
133 sigaction(SIGTRAP, &act, 0);
134#endif
135
136 act.sa_handler = dummy_handler;
137 sigaction(SIGALRM, &act, 0);
138#endif
139}
140
141int
142main(int argc, char *argv[])
143{
144 setup_signals();
145
146 authd_helper = rb_helper_child(parse_request, error_cb, NULL, NULL, NULL, 256, 256, 256); /* XXX fix me */
147 if(authd_helper == NULL)
148 {
149 fprintf(stderr, "authd is not meant to be invoked by end users\n");
150 exit(1);
151 }
152
8cf45447
AC
153 rb_set_time();
154 setup_signals();
155 init_resolver();
156 rb_init_prng(NULL, RB_PRNG_DEFAULT);
157
0d73e7db
AC
158 rb_helper_loop(authd_helper, 0);
159
160 return 0;
161}