]> jfr.im git - solanum.git/blame - authd/authd.c
Merge branch 'master' into authd-framework-2
[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"
a16b484f 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] = {
f49198a6 32<<<<<<< HEAD
a16b484f 33 ['C'] = handle_new_connection,
8cf45447 34 ['D'] = resolve_dns,
f49198a6 35 ['R'] = handle_reload,
394b8dde
EM
36 ['S'] = handle_stat,
37};
38
6f39a80e 39authd_stat_handler authd_stat_handlers[256] = {
394b8dde 40 ['D'] = enumerate_nameservers,
8cf45447 41};
0d73e7db 42
6f39a80e 43authd_reload_handler authd_reload_handlers[256] = {
6445c1cf
EM
44 ['D'] = reload_nameservers,
45};
46
394b8dde
EM
47static void
48handle_stat(int parc, char *parv[])
49{
50 authd_stat_handler handler;
51
52 if(parc < 3)
b2ede1aa
EM
53 {
54 warn_opers(L_CRIT, "BUG: handle_stat received too few parameters (at least 3 expected, got %d)", parc);
394b8dde 55 return;
b2ede1aa 56 }
394b8dde 57
e23126c8 58 if (!(handler = authd_stat_handlers[(unsigned char)parv[2][0]]))
394b8dde
EM
59 return;
60
61 handler(parv[1], parv[2][0]);
62}
63
6445c1cf
EM
64static void
65handle_reload(int parc, char *parv[])
66{
67 authd_reload_handler handler;
68
69 if(parc < 2)
b2ede1aa 70 {
122ae255
EM
71 /* Reload all handlers */
72 for(size_t i = 0; i < sizeof(authd_reload_handlers); handler = authd_reload_handlers[i++])
73 handler(parv[1][0]);
74
6445c1cf 75 return;
b2ede1aa 76 }
6445c1cf 77
e23126c8 78 if (!(handler = authd_reload_handlers[(unsigned char)parv[1][0]]))
6445c1cf
EM
79 return;
80
81 handler(parv[1][0]);
82}
83
0d73e7db
AC
84static void
85parse_request(rb_helper *helper)
86{
87 static char *parv[MAXPARA + 1];
88 static char readbuf[READBUF_SIZE];
89 int parc;
90 int len;
f3e11b1d 91 authd_cmd_handler handler;
0d73e7db
AC
92
93 while((len = rb_helper_read(helper, readbuf, sizeof(readbuf))) > 0)
94 {
95 parc = rb_string_to_array(readbuf, parv, MAXPARA);
96
97 if(parc < 1)
98 continue;
99
e23126c8 100 handler = authd_cmd_handlers[(unsigned char)parv[0][0]];
f3e11b1d
AC
101 if (handler != NULL)
102 handler(parc, parv);
0d73e7db
AC
103 }
104}
105
106static void
107error_cb(rb_helper *helper)
108{
109 exit(1);
110}
111
8da0b2f2 112#ifndef _WIN32
0d73e7db
AC
113static void
114dummy_handler(int sig)
115{
116 return;
117}
118#endif
119
120static void
121setup_signals(void)
122{
8da0b2f2 123#ifndef _WIN32
0d73e7db
AC
124 struct sigaction act;
125
126 act.sa_flags = 0;
127 act.sa_handler = SIG_IGN;
128 sigemptyset(&act.sa_mask);
129 sigaddset(&act.sa_mask, SIGPIPE);
130 sigaddset(&act.sa_mask, SIGALRM);
131#ifdef SIGTRAP
132 sigaddset(&act.sa_mask, SIGTRAP);
133#endif
134
135#ifdef SIGWINCH
136 sigaddset(&act.sa_mask, SIGWINCH);
137 sigaction(SIGWINCH, &act, 0);
138#endif
139 sigaction(SIGPIPE, &act, 0);
140#ifdef SIGTRAP
141 sigaction(SIGTRAP, &act, 0);
142#endif
143
144 act.sa_handler = dummy_handler;
145 sigaction(SIGALRM, &act, 0);
146#endif
147}
148
149int
150main(int argc, char *argv[])
151{
152 setup_signals();
153
154 authd_helper = rb_helper_child(parse_request, error_cb, NULL, NULL, NULL, 256, 256, 256); /* XXX fix me */
155 if(authd_helper == NULL)
156 {
157 fprintf(stderr, "authd is not meant to be invoked by end users\n");
158 exit(1);
159 }
160
8cf45447
AC
161 rb_set_time();
162 setup_signals();
163 init_resolver();
a16b484f 164 init_providers();
8cf45447
AC
165 rb_init_prng(NULL, RB_PRNG_DEFAULT);
166
0d73e7db
AC
167 rb_helper_loop(authd_helper, 0);
168
169 return 0;
170}