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
,
36 ['O'] = handle_options
,
37 ['R'] = handle_reload
,
41 authd_stat_handler authd_stat_handlers
[256] = {
42 ['D'] = enumerate_nameservers
,
45 authd_reload_handler authd_reload_handlers
[256] = {
46 ['D'] = reload_nameservers
,
49 rb_dictionary
*authd_option_handlers
;
52 handle_stat(int parc
, char *parv
[])
54 authd_stat_handler handler
;
58 warn_opers(L_CRIT
, "BUG: handle_stat received too few parameters (at least 3 expected, got %d)", parc
);
62 if (!(handler
= authd_stat_handlers
[(unsigned char)parv
[2][0]]))
65 handler(parv
[1], parv
[2][0]);
69 handle_options(int parc
, char *parv
[])
71 struct auth_opts_handler
*handler
;
75 warn_opers(L_CRIT
, "BUG: handle_options received too few parameters (at least 4 expected, got %d)", parc
);
79 if((handler
= rb_dictionary_retrieve(authd_option_handlers
, parv
[1])) == NULL
)
81 warn_opers(L_CRIT
, "BUG: handle_options got a bad option type %s", parv
[1]);
85 if((parc
- 2) < handler
->min_parc
)
87 warn_opers(L_CRIT
, "BUG: handle_options received too few parameters (at least %d expected, got %d)", handler
->min_parc
, parc
);
91 handler
->handler(parv
[1], parc
- 2, (const char **)(parv
+ 3));
95 handle_reload(int parc
, char *parv
[])
97 authd_reload_handler handler
;
101 /* Reload all handlers */
102 for(size_t i
= 0; i
< 256; i
++)
104 if ((handler
= authd_reload_handlers
[(unsigned char) i
]) != NULL
)
111 if (!(handler
= authd_reload_handlers
[(unsigned char)parv
[1][0]]))
118 parse_request(rb_helper
*helper
)
120 static char *parv
[MAXPARA
+ 1];
121 static char readbuf
[READBUF_SIZE
];
124 authd_cmd_handler handler
;
126 while((len
= rb_helper_read(helper
, readbuf
, sizeof(readbuf
))) > 0)
128 parc
= rb_string_to_array(readbuf
, parv
, MAXPARA
);
133 handler
= authd_cmd_handlers
[(unsigned char)parv
[0][0]];
140 error_cb(rb_helper
*helper
)
147 dummy_handler(int sig
)
157 struct sigaction act
;
160 act
.sa_handler
= SIG_IGN
;
161 sigemptyset(&act
.sa_mask
);
162 sigaddset(&act
.sa_mask
, SIGPIPE
);
163 sigaddset(&act
.sa_mask
, SIGALRM
);
165 sigaddset(&act
.sa_mask
, SIGTRAP
);
169 sigaddset(&act
.sa_mask
, SIGWINCH
);
170 sigaction(SIGWINCH
, &act
, 0);
172 sigaction(SIGPIPE
, &act
, 0);
174 sigaction(SIGTRAP
, &act
, 0);
177 act
.sa_handler
= dummy_handler
;
178 sigaction(SIGALRM
, &act
, 0);
183 main(int argc
, char *argv
[])
187 authd_helper
= rb_helper_child(parse_request
, error_cb
, NULL
, NULL
, NULL
, 256, 256, 256); /* XXX fix me */
188 if(authd_helper
== NULL
)
190 fprintf(stderr
, "authd is not meant to be invoked by end users\n");
197 authd_option_handlers
= rb_dictionary_create("authd options handlers", strcasecmp
);
201 rb_init_prng(NULL
, RB_PRNG_DEFAULT
);
203 rb_helper_loop(authd_helper
, 0);