]> jfr.im git - solanum.git/blob - ircd/authd.c
extensions/helpops: implement DEHELPER command
[solanum.git] / ircd / authd.c
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>
26 #include <ratbox_lib.h>
27 #include <client.h>
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,
70 "Unable to execute authd in %s or %s/libexec/charybdis",
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];
100
101 char *parv[MAXPARA + 1];
102 while((len = rb_helper_read(helper, dnsBuf, sizeof(dnsBuf))) > 0)
103 {
104 parc = rb_string_to_array(dnsBuf, parv, MAXPARA+1);
105
106 switch (*parv[0])
107 {
108 case 'E':
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]);
116 break;
117 default:
118 break;
119 }
120 }
121 }
122
123 void
124 init_authd(void)
125 {
126 if(start_authd())
127 {
128 ilog(L_MAIN, "Unable to start authd helper: %s", strerror(errno));
129 exit(0);
130 }
131 }
132
133 static void
134 restart_authd_cb(rb_helper * helper)
135 {
136 ilog(L_MAIN, "authd: restart_authd_cb called, authd died?");
137 sendto_realops_snomask(SNO_GENERAL, L_ALL, "authd: restart_authd_cb called, authd died?");
138 if(helper != NULL)
139 {
140 rb_helper_close(helper);
141 authd_helper = NULL;
142 }
143 start_authd();
144 }
145
146 void
147 restart_authd(void)
148 {
149 restart_authd_cb(authd_helper);
150 }
151
152 void
153 rehash_authd(void)
154 {
155 rb_helper_write(authd_helper, "R");
156 }
157
158 void
159 check_authd(void)
160 {
161 if(authd_helper == NULL)
162 restart_authd();
163 }