]>
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 | { | |
58 | snprintf(fullpath, sizeof(fullpath), "%s/authd%s", PKGLIBEXECDIR, suffix); | |
59 | ||
60 | if(access(fullpath, X_OK) == -1) | |
61 | { | |
62 | snprintf(fullpath, sizeof(fullpath), "%s/libexec/charybdis/authd%s", | |
63 | ConfigFileEntry.dpath, suffix); | |
64 | if(access(fullpath, X_OK) == -1) | |
65 | { | |
66 | ilog(L_MAIN, | |
67 | "Unable to execute authd in %s or %s/libexec/charybdis", | |
68 | PKGLIBEXECDIR, ConfigFileEntry.dpath); | |
69 | sendto_realops_snomask(SNO_GENERAL, L_ALL, | |
3949459b | 70 | "Unable to execute authd in %s or %s/libexec/charybdis", |
fb7d74ef AC |
71 | PKGLIBEXECDIR, ConfigFileEntry.dpath); |
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; |
394b8dde EM |
117 | case 'X': |
118 | case 'Y': | |
119 | case 'Z': | |
120 | if(parc < 3) | |
121 | { | |
122 | ilog(L_MAIN, "authd sent a result with wrong number of arguments: got %d", parc); | |
123 | restart_authd(); | |
124 | return; | |
125 | } | |
126 | ||
127 | /* Select by type */ | |
128 | switch(*parv[2]) | |
129 | { | |
130 | case 'D': | |
131 | /* parv[0] conveys status */ | |
132 | if(parc < 4) | |
133 | { | |
134 | ilog(L_MAIN, "authd sent a result with wrong number of arguments: got %d", parc); | |
135 | restart_authd(); | |
136 | return; | |
137 | } | |
138 | dns_stats_results_callback(parv[1], parv[0], parc - 3, (const char **)&parv[3]); | |
139 | break; | |
140 | default: | |
141 | break; | |
142 | } | |
143 | break; | |
3949459b AC |
144 | default: |
145 | break; | |
fb7d74ef | 146 | } |
fb7d74ef AC |
147 | } |
148 | } | |
149 | ||
150 | void | |
151 | init_authd(void) | |
152 | { | |
153 | if(start_authd()) | |
154 | { | |
155 | ilog(L_MAIN, "Unable to start authd helper: %s", strerror(errno)); | |
156 | exit(0); | |
157 | } | |
158 | } | |
159 | ||
160 | static void | |
161 | restart_authd_cb(rb_helper * helper) | |
162 | { | |
163 | ilog(L_MAIN, "authd: restart_authd_cb called, authd died?"); | |
3949459b | 164 | sendto_realops_snomask(SNO_GENERAL, L_ALL, "authd: restart_authd_cb called, authd died?"); |
fb7d74ef AC |
165 | if(helper != NULL) |
166 | { | |
167 | rb_helper_close(helper); | |
168 | authd_helper = NULL; | |
169 | } | |
170 | start_authd(); | |
171 | } | |
172 | ||
173 | void | |
174 | restart_authd(void) | |
175 | { | |
176 | restart_authd_cb(authd_helper); | |
177 | } | |
178 | ||
179 | void | |
180 | rehash_authd(void) | |
181 | { | |
182 | rb_helper_write(authd_helper, "R"); | |
183 | } | |
184 | ||
185 | void | |
186 | check_authd(void) | |
187 | { | |
188 | if(authd_helper == NULL) | |
189 | restart_authd(); | |
190 | } |