]> jfr.im git - solanum.git/blob - authd/authd.c
authd: clean up command handlers code a little
[solanum.git] / authd / authd.c
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
21 #include "authd.h"
22 #include "dns.h"
23
24 #define MAXPARA 10
25
26 rb_helper *authd_helper = NULL;
27 authd_cmd_handler authd_cmd_handlers[255] = {};
28
29 static void
30 parse_request(rb_helper *helper)
31 {
32 static char *parv[MAXPARA + 1];
33 static char readbuf[READBUF_SIZE];
34 int parc;
35 int len;
36 authd_cmd_handler handler;
37
38 while((len = rb_helper_read(helper, readbuf, sizeof(readbuf))) > 0)
39 {
40 parc = rb_string_to_array(readbuf, parv, MAXPARA);
41
42 if(parc < 1)
43 continue;
44
45 handler = authd_cmd_handlers[parv[0][0]];
46 if (handler != NULL)
47 handler(parc, parv);
48 }
49 }
50
51 static void
52 error_cb(rb_helper *helper)
53 {
54 exit(1);
55 }
56
57 #ifndef WINDOWS
58 static void
59 dummy_handler(int sig)
60 {
61 return;
62 }
63 #endif
64
65 static void
66 setup_signals(void)
67 {
68 #ifndef WINDOWS
69 struct sigaction act;
70
71 act.sa_flags = 0;
72 act.sa_handler = SIG_IGN;
73 sigemptyset(&act.sa_mask);
74 sigaddset(&act.sa_mask, SIGPIPE);
75 sigaddset(&act.sa_mask, SIGALRM);
76 #ifdef SIGTRAP
77 sigaddset(&act.sa_mask, SIGTRAP);
78 #endif
79
80 #ifdef SIGWINCH
81 sigaddset(&act.sa_mask, SIGWINCH);
82 sigaction(SIGWINCH, &act, 0);
83 #endif
84 sigaction(SIGPIPE, &act, 0);
85 #ifdef SIGTRAP
86 sigaction(SIGTRAP, &act, 0);
87 #endif
88
89 act.sa_handler = dummy_handler;
90 sigaction(SIGALRM, &act, 0);
91 #endif
92 }
93
94 int
95 main(int argc, char *argv[])
96 {
97 setup_signals();
98
99 authd_helper = rb_helper_child(parse_request, error_cb, NULL, NULL, NULL, 256, 256, 256); /* XXX fix me */
100 if(authd_helper == NULL)
101 {
102 fprintf(stderr, "authd is not meant to be invoked by end users\n");
103 exit(1);
104 }
105
106 rb_helper_loop(authd_helper, 0);
107
108 return 0;
109 }