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