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