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