]>
Commit | Line | Data |
---|---|---|
fb7d74ef AC |
1 | /* |
2 | * authd.c: An interface to authd. | |
3 | * (based somewhat on ircd-ratbox dns.c) | |
4 | * | |
5 | * Copyright (C) 2005 Aaron Sethman <androsyn@ratbox.org> | |
6 | * Copyright (C) 2005-2012 ircd-ratbox development team | |
7 | * Copyright (C) 2016 William Pitcock <nenolod@dereferenced.org> | |
8 | * | |
9 | * This program is free software; you can redistribute it and/or modify | |
10 | * it under the terms of the GNU General Public License as published by | |
11 | * the Free Software Foundation; either version 2 of the License, or | |
12 | * (at your option) any later version. | |
13 | * | |
14 | * This program is distributed in the hope that it will be useful, | |
15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
17 | * GNU General Public License for more details. | |
18 | * | |
19 | * You should have received a copy of the GNU General Public License | |
20 | * along with this program; if not, write to the Free Software | |
21 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 | |
22 | * USA | |
23 | */ | |
24 | ||
25 | #include <stdinc.h> | |
fe037171 | 26 | #include <rb_lib.h> |
ebccc13b | 27 | #include <client.h> |
fb7d74ef AC |
28 | #include <ircd_defs.h> |
29 | #include <parse.h> | |
30 | #include <authd.h> | |
31 | #include <match.h> | |
32 | #include <logger.h> | |
33 | #include <s_conf.h> | |
34 | #include <client.h> | |
35 | #include <send.h> | |
36 | #include <numeric.h> | |
37 | #include <msg.h> | |
38 | #include <dns.h> | |
39 | ||
40 | static int start_authd(void); | |
41 | static void parse_authd_reply(rb_helper * helper); | |
42 | static void restart_authd_cb(rb_helper * helper); | |
43 | ||
44 | rb_helper *authd_helper; | |
45 | static char *authd_path; | |
46 | ||
47 | static int | |
48 | start_authd(void) | |
49 | { | |
50 | char fullpath[PATH_MAX + 1]; | |
51 | #ifdef _WIN32 | |
52 | const char *suffix = ".exe"; | |
53 | #else | |
54 | const char *suffix = ""; | |
55 | #endif | |
56 | if(authd_path == NULL) | |
57 | { | |
4d8cfacd | 58 | snprintf(fullpath, sizeof(fullpath), "%s%cauthd%s", ircd_paths[IRCD_PATH_LIBEXEC], RB_PATH_SEPARATOR, suffix); |
fb7d74ef AC |
59 | |
60 | if(access(fullpath, X_OK) == -1) | |
61 | { | |
4d8cfacd AC |
62 | snprintf(fullpath, sizeof(fullpath), "%s%cbin%cauthd%s", |
63 | ConfigFileEntry.dpath, RB_PATH_SEPARATOR, RB_PATH_SEPARATOR, suffix); | |
fb7d74ef AC |
64 | if(access(fullpath, X_OK) == -1) |
65 | { | |
66 | ilog(L_MAIN, | |
4d8cfacd AC |
67 | "Unable to execute authd in %s or %s/bin", |
68 | ircd_paths[IRCD_PATH_LIBEXEC], ConfigFileEntry.dpath); | |
fb7d74ef | 69 | sendto_realops_snomask(SNO_GENERAL, L_ALL, |
4d8cfacd AC |
70 | "Unable to execute authd in %s or %s/bin", |
71 | ircd_paths[IRCD_PATH_LIBEXEC], ConfigFileEntry.dpath); | |
fb7d74ef AC |
72 | return 1; |
73 | } | |
74 | ||
75 | } | |
76 | ||
77 | authd_path = rb_strdup(fullpath); | |
78 | } | |
79 | ||
80 | authd_helper = rb_helper_start("authd", authd_path, parse_authd_reply, restart_authd_cb); | |
81 | ||
82 | if(authd_helper == NULL) | |
83 | { | |
84 | ilog(L_MAIN, "Unable to start authd helper: %s", strerror(errno)); | |
85 | sendto_realops_snomask(SNO_GENERAL, L_ALL, "Unable to start authd helper: %s", strerror(errno)); | |
86 | return 1; | |
87 | } | |
88 | ilog(L_MAIN, "authd helper started"); | |
89 | sendto_realops_snomask(SNO_GENERAL, L_ALL, "authd helper started"); | |
90 | rb_helper_run(authd_helper); | |
91 | return 0; | |
92 | } | |
93 | ||
94 | static void | |
95 | parse_authd_reply(rb_helper * helper) | |
96 | { | |
97 | ssize_t len; | |
98 | int parc; | |
99 | char dnsBuf[READBUF_SIZE]; | |
fb7d74ef | 100 | char *parv[MAXPARA + 1]; |
394b8dde | 101 | |
fb7d74ef AC |
102 | while((len = rb_helper_read(helper, dnsBuf, sizeof(dnsBuf))) > 0) |
103 | { | |
394b8dde | 104 | parc = rb_string_to_array(dnsBuf, parv, MAXPARA+1); |
fb7d74ef | 105 | |
3949459b | 106 | switch (*parv[0]) |
fb7d74ef | 107 | { |
3949459b | 108 | case 'E': |
fb7d74ef AC |
109 | if(parc != 5) |
110 | { | |
111 | ilog(L_MAIN, "authd sent a result with wrong number of arguments: got %d", parc); | |
112 | restart_authd(); | |
113 | return; | |
114 | } | |
115 | dns_results_callback(parv[1], parv[2], parv[3], parv[4]); | |
3949459b | 116 | break; |
0a659bf0 EM |
117 | case 'W': |
118 | if(parc != 3) | |
119 | { | |
120 | ilog(L_MAIN, "authd sent a result with wrong number of arguments: got %d", parc); | |
121 | restart_authd(); | |
122 | return; | |
123 | } | |
124 | ||
125 | switch(*parv[2]) | |
126 | { | |
938f93f4 | 127 | case 'D': /* debug */ |
0a659bf0 EM |
128 | sendto_realops_snomask(SNO_DEBUG, L_ALL, "authd debug: %s", parv[3]); |
129 | break; | |
938f93f4 | 130 | case 'I': /* Info */ |
0a659bf0 EM |
131 | sendto_realops_snomask(SNO_GENERAL, L_ALL, "authd info: %s", parv[3]); |
132 | inotice("authd info: %s", parv[3]); | |
133 | break; | |
938f93f4 | 134 | case 'W': /* Warning */ |
0a659bf0 EM |
135 | sendto_realops_snomask(SNO_GENERAL, L_ALL, "authd WARNING: %s", parv[3]); |
136 | iwarn("authd warning: %s", parv[3]); | |
137 | break; | |
938f93f4 | 138 | case 'C': /* Critical (error) */ |
0a659bf0 EM |
139 | sendto_realops_snomask(SNO_GENERAL, L_ALL, "authd CRITICAL: %s", parv[3]); |
140 | ierror("authd critical: %s", parv[3]); | |
141 | break; | |
938f93f4 | 142 | default: /* idk */ |
0a659bf0 EM |
143 | sendto_realops_snomask(SNO_GENERAL, L_ALL, "authd sent us an unknown oper notice type (%s): %s", parv[2], parv[3]); |
144 | ilog(L_MAIN, "authd unknown oper notice type (%s): %s", parv[2], parv[3]); | |
145 | break; | |
146 | } | |
147 | ||
148 | /* NOTREACHED */ | |
149 | break; | |
394b8dde EM |
150 | case 'X': |
151 | case 'Y': | |
152 | case 'Z': | |
153 | if(parc < 3) | |
154 | { | |
155 | ilog(L_MAIN, "authd sent a result with wrong number of arguments: got %d", parc); | |
156 | restart_authd(); | |
157 | return; | |
158 | } | |
159 | ||
160 | /* Select by type */ | |
161 | switch(*parv[2]) | |
162 | { | |
163 | case 'D': | |
164 | /* parv[0] conveys status */ | |
165 | if(parc < 4) | |
166 | { | |
167 | ilog(L_MAIN, "authd sent a result with wrong number of arguments: got %d", parc); | |
168 | restart_authd(); | |
169 | return; | |
170 | } | |
171 | dns_stats_results_callback(parv[1], parv[0], parc - 3, (const char **)&parv[3]); | |
172 | break; | |
173 | default: | |
174 | break; | |
175 | } | |
176 | break; | |
3949459b AC |
177 | default: |
178 | break; | |
fb7d74ef | 179 | } |
fb7d74ef AC |
180 | } |
181 | } | |
182 | ||
183 | void | |
184 | init_authd(void) | |
185 | { | |
186 | if(start_authd()) | |
187 | { | |
188 | ilog(L_MAIN, "Unable to start authd helper: %s", strerror(errno)); | |
189 | exit(0); | |
190 | } | |
191 | } | |
192 | ||
193 | static void | |
194 | restart_authd_cb(rb_helper * helper) | |
195 | { | |
196 | ilog(L_MAIN, "authd: restart_authd_cb called, authd died?"); | |
3949459b | 197 | sendto_realops_snomask(SNO_GENERAL, L_ALL, "authd: restart_authd_cb called, authd died?"); |
fb7d74ef AC |
198 | if(helper != NULL) |
199 | { | |
200 | rb_helper_close(helper); | |
201 | authd_helper = NULL; | |
202 | } | |
203 | start_authd(); | |
204 | } | |
205 | ||
206 | void | |
207 | restart_authd(void) | |
208 | { | |
209 | restart_authd_cb(authd_helper); | |
210 | } | |
211 | ||
212 | void | |
213 | rehash_authd(void) | |
214 | { | |
215 | rb_helper_write(authd_helper, "R"); | |
216 | } | |
217 | ||
218 | void | |
219 | check_authd(void) | |
220 | { | |
221 | if(authd_helper == NULL) | |
222 | restart_authd(); | |
223 | } |