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