]> 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"
db821ee9 24#include "notice.h"
0d73e7db
AC
25
26#define MAXPARA 10
27
6445c1cf 28static void handle_reload(int parc, char *parv[]);
394b8dde 29static void handle_stat(int parc, char *parv[]);
a51487e0 30static void handle_options(int parc, char *parv[]);
394b8dde 31
f3e11b1d 32rb_helper *authd_helper = NULL;
6f39a80e 33authd_cmd_handler authd_cmd_handlers[256] = {
a16b484f 34 ['C'] = handle_new_connection,
8cf45447 35 ['D'] = resolve_dns,
a51487e0 36 ['O'] = handle_options,
f49198a6 37 ['R'] = handle_reload,
394b8dde
EM
38 ['S'] = handle_stat,
39};
40
6f39a80e 41authd_stat_handler authd_stat_handlers[256] = {
394b8dde 42 ['D'] = enumerate_nameservers,
8cf45447 43};
0d73e7db 44
6f39a80e 45authd_reload_handler authd_reload_handlers[256] = {
6445c1cf
EM
46 ['D'] = reload_nameservers,
47};
48
a51487e0
EM
49rb_dictionary *authd_option_handlers;
50
394b8dde
EM
51static void
52handle_stat(int parc, char *parv[])
53{
54 authd_stat_handler handler;
55
56 if(parc < 3)
b2ede1aa
EM
57 {
58 warn_opers(L_CRIT, "BUG: handle_stat received too few parameters (at least 3 expected, got %d)", parc);
394b8dde 59 return;
b2ede1aa 60 }
394b8dde 61
e23126c8 62 if (!(handler = authd_stat_handlers[(unsigned char)parv[2][0]]))
394b8dde
EM
63 return;
64
65 handler(parv[1], parv[2][0]);
66}
67
a51487e0
EM
68static void
69handle_options(int parc, char *parv[])
70{
71 struct auth_opts_handler *handler;
72
73 if(parc < 4)
74 {
75 warn_opers(L_CRIT, "BUG: handle_options received too few parameters (at least 4 expected, got %d)", parc);
76 return;
77 }
78
79 if((handler = rb_dictionary_retrieve(authd_option_handlers, parv[1])) == NULL)
80 {
81 warn_opers(L_CRIT, "BUG: handle_options got a bad option type %s", parv[1]);
82 return;
83 }
84
85 if((parc - 2) < handler->min_parc)
86 {
87 warn_opers(L_CRIT, "BUG: handle_options received too few parameters (at least %d expected, got %d)", handler->min_parc, parc);
88 return;
89 }
90
91 handler->handler(parv[1], parc - 2, (const char **)(parv + 3));
92}
93
6445c1cf
EM
94static void
95handle_reload(int parc, char *parv[])
96{
97 authd_reload_handler handler;
98
99 if(parc < 2)
b2ede1aa 100 {
122ae255 101 /* Reload all handlers */
a51487e0 102 for(size_t i = 0; i < 256; i++)
c63cd21e
AC
103 {
104 if ((handler = authd_reload_handlers[(unsigned char) i]) != NULL)
105 handler(parv[1][0]);
106 }
122ae255 107
6445c1cf 108 return;
b2ede1aa 109 }
6445c1cf 110
e23126c8 111 if (!(handler = authd_reload_handlers[(unsigned char)parv[1][0]]))
6445c1cf
EM
112 return;
113
114 handler(parv[1][0]);
115}
116
0d73e7db
AC
117static void
118parse_request(rb_helper *helper)
119{
120 static char *parv[MAXPARA + 1];
121 static char readbuf[READBUF_SIZE];
122 int parc;
123 int len;
f3e11b1d 124 authd_cmd_handler handler;
0d73e7db
AC
125
126 while((len = rb_helper_read(helper, readbuf, sizeof(readbuf))) > 0)
127 {
128 parc = rb_string_to_array(readbuf, parv, MAXPARA);
129
130 if(parc < 1)
131 continue;
132
e23126c8 133 handler = authd_cmd_handlers[(unsigned char)parv[0][0]];
f3e11b1d
AC
134 if (handler != NULL)
135 handler(parc, parv);
0d73e7db
AC
136 }
137}
138
139static void
140error_cb(rb_helper *helper)
141{
142 exit(1);
143}
144
8da0b2f2 145#ifndef _WIN32
0d73e7db
AC
146static void
147dummy_handler(int sig)
148{
149 return;
150}
151#endif
152
153static void
154setup_signals(void)
155{
8da0b2f2 156#ifndef _WIN32
0d73e7db
AC
157 struct sigaction act;
158
159 act.sa_flags = 0;
160 act.sa_handler = SIG_IGN;
161 sigemptyset(&act.sa_mask);
162 sigaddset(&act.sa_mask, SIGPIPE);
163 sigaddset(&act.sa_mask, SIGALRM);
164#ifdef SIGTRAP
165 sigaddset(&act.sa_mask, SIGTRAP);
166#endif
167
168#ifdef SIGWINCH
169 sigaddset(&act.sa_mask, SIGWINCH);
170 sigaction(SIGWINCH, &act, 0);
171#endif
172 sigaction(SIGPIPE, &act, 0);
173#ifdef SIGTRAP
174 sigaction(SIGTRAP, &act, 0);
175#endif
176
177 act.sa_handler = dummy_handler;
178 sigaction(SIGALRM, &act, 0);
179#endif
180}
181
182int
183main(int argc, char *argv[])
184{
185 setup_signals();
186
187 authd_helper = rb_helper_child(parse_request, error_cb, NULL, NULL, NULL, 256, 256, 256); /* XXX fix me */
188 if(authd_helper == NULL)
189 {
190 fprintf(stderr, "authd is not meant to be invoked by end users\n");
191 exit(1);
192 }
193
8cf45447
AC
194 rb_set_time();
195 setup_signals();
a51487e0
EM
196
197 authd_option_handlers = rb_dictionary_create("authd options handlers", strcasecmp);
198
8cf45447 199 init_resolver();
a16b484f 200 init_providers();
8cf45447
AC
201 rb_init_prng(NULL, RB_PRNG_DEFAULT);
202
0d73e7db
AC
203 rb_helper_loop(authd_helper, 0);
204
205 return 0;
206}