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.
27 static void handle_reload(int parc
, char *parv
[]);
28 static void handle_stat(int parc
, char *parv
[]);
30 rb_helper
*authd_helper
= NULL
;
31 authd_cmd_handler authd_cmd_handlers
[256] = {
33 ['C'] = handle_new_connection
,
35 ['R'] = handle_reload
,
39 authd_stat_handler authd_stat_handlers
[256] = {
40 ['D'] = enumerate_nameservers
,
43 authd_reload_handler authd_reload_handlers
[256] = {
44 ['D'] = reload_nameservers
,
48 handle_stat(int parc
, char *parv
[])
50 authd_stat_handler handler
;
54 warn_opers(L_CRIT
, "BUG: handle_stat received too few parameters (at least 3 expected, got %d)", parc
);
58 if (!(handler
= authd_stat_handlers
[(unsigned char)parv
[2][0]]))
61 handler(parv
[1], parv
[2][0]);
65 handle_reload(int parc
, char *parv
[])
67 authd_reload_handler handler
;
71 /* Reload all handlers */
72 for(size_t i
= 0; i
< sizeof(authd_reload_handlers
); handler
= authd_reload_handlers
[i
++])
78 if (!(handler
= authd_reload_handlers
[(unsigned char)parv
[1][0]]))
85 parse_request(rb_helper
*helper
)
87 static char *parv
[MAXPARA
+ 1];
88 static char readbuf
[READBUF_SIZE
];
91 authd_cmd_handler handler
;
93 while((len
= rb_helper_read(helper
, readbuf
, sizeof(readbuf
))) > 0)
95 parc
= rb_string_to_array(readbuf
, parv
, MAXPARA
);
100 handler
= authd_cmd_handlers
[(unsigned char)parv
[0][0]];
107 error_cb(rb_helper
*helper
)
114 dummy_handler(int sig
)
124 struct sigaction act
;
127 act
.sa_handler
= SIG_IGN
;
128 sigemptyset(&act
.sa_mask
);
129 sigaddset(&act
.sa_mask
, SIGPIPE
);
130 sigaddset(&act
.sa_mask
, SIGALRM
);
132 sigaddset(&act
.sa_mask
, SIGTRAP
);
136 sigaddset(&act
.sa_mask
, SIGWINCH
);
137 sigaction(SIGWINCH
, &act
, 0);
139 sigaction(SIGPIPE
, &act
, 0);
141 sigaction(SIGTRAP
, &act
, 0);
144 act
.sa_handler
= dummy_handler
;
145 sigaction(SIGALRM
, &act
, 0);
150 main(int argc
, char *argv
[])
154 authd_helper
= rb_helper_child(parse_request
, error_cb
, NULL
, NULL
, NULL
, 256, 256, 256); /* XXX fix me */
155 if(authd_helper
== NULL
)
157 fprintf(stderr
, "authd is not meant to be invoked by end users\n");
165 rb_init_prng(NULL
, RB_PRNG_DEFAULT
);
167 rb_helper_loop(authd_helper
, 0);