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