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