1 /* authd/authd.c - main code for authd
2 * Copyright (c) 2016 William Pitcock <nenolod@dereferenced.org>
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.
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.
28 static void handle_reload(int parc
, char *parv
[]);
29 static void handle_stat(int parc
, char *parv
[]);
30 static void handle_options(int parc
, char *parv
[]);
32 rb_helper
*authd_helper
= NULL
;
33 authd_cmd_handler authd_cmd_handlers
[256] = {
34 ['C'] = handle_new_connection
,
35 ['D'] = handle_resolve_dns
,
36 ['E'] = handle_cancel_connection
,
37 ['O'] = handle_options
,
38 ['R'] = handle_reload
,
42 authd_stat_handler authd_stat_handlers
[256] = {
43 ['D'] = enumerate_nameservers
,
46 authd_reload_handler authd_reload_handlers
[256] = {
47 ['D'] = reload_nameservers
,
50 rb_dictionary
*authd_option_handlers
;
53 handle_stat(int parc
, char *parv
[])
55 authd_stat_handler handler
;
60 warn_opers(L_CRIT
, "BUG: handle_stat received too few parameters (at least 3 expected, got %d)", parc
);
64 if((lrid
= strtol(parv
[1], NULL
, 16)) > UINT32_MAX
)
66 warn_opers(L_CRIT
, "BUG: handle_stat got a rid that was too large: %lx", lrid
);
70 if (!(handler
= authd_stat_handlers
[(unsigned char)parv
[2][0]]))
73 handler((uint32_t)lrid
, parv
[2][0]);
77 handle_options(int parc
, char *parv
[])
79 struct auth_opts_handler
*handler
;
83 warn_opers(L_CRIT
, "BUG: handle_options received too few parameters (at least 2 expected, got %d)", parc
);
87 if((handler
= rb_dictionary_retrieve(authd_option_handlers
, parv
[1])) == NULL
)
89 warn_opers(L_CRIT
, "BUG: handle_options got a bad option type %s", parv
[1]);
93 if((parc
- 2) < handler
->min_parc
)
95 warn_opers(L_CRIT
, "BUG: handle_options received too few parameters (at least %d expected, got %d)", handler
->min_parc
, parc
);
99 handler
->handler(parv
[1], parc
- 2, (const char **)&parv
[2]);
103 handle_reload(int parc
, char *parv
[])
105 authd_reload_handler handler
;
109 /* Reload all handlers */
110 for(size_t i
= 0; i
< 256; i
++)
112 if ((handler
= authd_reload_handlers
[(unsigned char) i
]) != NULL
)
119 if (!(handler
= authd_reload_handlers
[(unsigned char)parv
[1][0]]))
126 parse_request(rb_helper
*helper
)
128 static char *parv
[MAXPARA
+ 1];
129 static char readbuf
[READBUF_SIZE
];
132 authd_cmd_handler handler
;
134 while((len
= rb_helper_read(helper
, readbuf
, sizeof(readbuf
))) > 0)
136 parc
= rb_string_to_array(readbuf
, parv
, MAXPARA
);
141 handler
= authd_cmd_handlers
[(unsigned char)parv
[0][0]];
148 error_cb(rb_helper
*helper
)
155 dummy_handler(int sig
)
165 struct sigaction act
;
168 act
.sa_handler
= SIG_IGN
;
169 sigemptyset(&act
.sa_mask
);
170 sigaddset(&act
.sa_mask
, SIGPIPE
);
171 sigaddset(&act
.sa_mask
, SIGALRM
);
173 sigaddset(&act
.sa_mask
, SIGTRAP
);
177 sigaddset(&act
.sa_mask
, SIGWINCH
);
178 sigaction(SIGWINCH
, &act
, 0);
180 sigaction(SIGPIPE
, &act
, 0);
182 sigaction(SIGTRAP
, &act
, 0);
185 act
.sa_handler
= dummy_handler
;
186 sigaction(SIGALRM
, &act
, 0);
197 main(int argc
, char *argv
[])
201 authd_helper
= rb_helper_child(parse_request
, error_cb
, NULL
, NULL
, NULL
, 256, 256, 256); /* XXX fix me */
202 if(authd_helper
== NULL
)
204 fprintf(stderr
, "authd is not meant to be invoked by end users\n");
211 authd_option_handlers
= rb_dictionary_create("authd options handlers", rb_strcasecmp
);
215 rb_init_prng(NULL
, RB_PRNG_DEFAULT
);
219 rb_helper_loop(authd_helper
, 0);