2 * authd.c: An interface to authd.
3 * (based somewhat on ircd-ratbox dns.c)
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>
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.
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.
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
28 #include "ircd_defs.h"
43 static int start_authd(void);
44 static void parse_authd_reply(rb_helper
* helper
);
45 static void restart_authd_cb(rb_helper
* helper
);
46 static EVH timeout_dead_authd_clients
;
48 rb_helper
*authd_helper
;
49 static char *authd_path
;
52 static rb_dictionary
*cid_clients
;
53 static struct ev_entry
*timeout_ev
;
55 rb_dictionary
*bl_stats
;
60 char fullpath
[PATH_MAX
+ 1];
62 const char *suffix
= ".exe";
64 const char *suffix
= "";
66 if(authd_path
== NULL
)
68 snprintf(fullpath
, sizeof(fullpath
), "%s%cauthd%s", ircd_paths
[IRCD_PATH_LIBEXEC
], RB_PATH_SEPARATOR
, suffix
);
70 if(access(fullpath
, X_OK
) == -1)
72 snprintf(fullpath
, sizeof(fullpath
), "%s%cbin%cauthd%s",
73 ConfigFileEntry
.dpath
, RB_PATH_SEPARATOR
, RB_PATH_SEPARATOR
, suffix
);
74 if(access(fullpath
, X_OK
) == -1)
77 "Unable to execute authd in %s or %s/bin",
78 ircd_paths
[IRCD_PATH_LIBEXEC
], ConfigFileEntry
.dpath
);
79 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
80 "Unable to execute authd in %s or %s/bin",
81 ircd_paths
[IRCD_PATH_LIBEXEC
], ConfigFileEntry
.dpath
);
87 authd_path
= rb_strdup(fullpath
);
90 if(cid_clients
== NULL
)
91 cid_clients
= rb_dictionary_create("authd cid to uid mapping", rb_uint32cmp
);
94 bl_stats
= rb_dictionary_create("blacklist statistics", strcasecmp
);
96 if(timeout_ev
== NULL
)
97 timeout_ev
= rb_event_addish("timeout_dead_authd_clients", timeout_dead_authd_clients
, NULL
, 1);
99 authd_helper
= rb_helper_start("authd", authd_path
, parse_authd_reply
, restart_authd_cb
);
101 if(authd_helper
== NULL
)
103 ilog(L_MAIN
, "Unable to start authd helper: %s", strerror(errno
));
104 sendto_realops_snomask(SNO_GENERAL
, L_ALL
, "Unable to start authd helper: %s", strerror(errno
));
107 ilog(L_MAIN
, "authd helper started");
108 sendto_realops_snomask(SNO_GENERAL
, L_ALL
, "authd helper started");
109 rb_helper_run(authd_helper
);
114 parse_authd_reply(rb_helper
* helper
)
118 char authdBuf
[READBUF_SIZE
];
119 char *parv
[MAXPARA
+ 1];
122 struct Client
*client_p
;
124 while((len
= rb_helper_read(helper
, authdBuf
, sizeof(authdBuf
))) > 0)
126 parc
= rb_string_to_array(authdBuf
, parv
, MAXPARA
+1);
130 case 'A': /* Accepted a client */
133 iwarn("authd sent a result with wrong number of arguments: got %d", parc
);
138 if((lcid
= strtol(parv
[1], NULL
, 16)) > UINT32_MAX
|| lcid
< 0)
140 iwarn("authd sent us back a bad client ID: %ld", lcid
);
145 cid
= (uint32_t)lcid
;
147 /* cid to uid (retrieve and delete) */
148 if((client_p
= rb_dictionary_delete(cid_clients
, RB_UINT_TO_POINTER(cid
))) == NULL
)
150 iwarn("authd sent us back an unknown client ID %x", cid
);
155 authd_decide_client(client_p
, parv
[2], parv
[3], true, '\0', NULL
, NULL
);
157 case 'R': /* Reject client */
160 iwarn("authd sent us a result with wrong number of arguments: got %d", parc
);
165 if((lcid
= strtol(parv
[1], NULL
, 16)) > UINT32_MAX
|| lcid
< 0)
167 iwarn("authd sent us back a bad client ID %ld", lcid
);
172 cid
= (uint32_t)lcid
;
174 /* cid to uid (retrieve and delete) */
175 if((client_p
= rb_dictionary_delete(cid_clients
, RB_UINT_TO_POINTER(cid
))) == NULL
)
177 iwarn("authd sent us back an unknown client ID %x", cid
);
182 authd_decide_client(client_p
, parv
[3], parv
[4], false, toupper(*parv
[2]), parv
[5], parv
[6]);
184 case 'N': /* Notice to client */
187 iwarn("authd sent us a result with wrong number of arguments: got %d", parc
);
192 if((lcid
= strtol(parv
[1], NULL
, 16)) > UINT32_MAX
|| lcid
< 0)
194 iwarn("authd sent us back a bad client ID %ld", lcid
);
199 cid
= (uint32_t)lcid
;
202 if((client_p
= rb_dictionary_retrieve(cid_clients
, RB_UINT_TO_POINTER(cid
))) == NULL
)
204 iwarn("authd sent us back an unknown client ID %x", cid
);
209 sendto_one_notice(client_p
, ":%s", parv
[2]);
211 case 'E': /* DNS Result */
214 ilog(L_MAIN
, "authd sent a result with wrong number of arguments: got %d", parc
);
218 dns_results_callback(parv
[1], parv
[2], parv
[3], parv
[4]);
220 case 'W': /* Oper warning */
223 ilog(L_MAIN
, "authd sent a result with wrong number of arguments: got %d", parc
);
230 case 'D': /* Debug */
231 sendto_realops_snomask(SNO_DEBUG
, L_ALL
, "authd debug: %s", parv
[3]);
234 sendto_realops_snomask(SNO_GENERAL
, L_ALL
, "authd info: %s", parv
[3]);
235 inotice("authd info: %s", parv
[3]);
237 case 'W': /* Warning */
238 sendto_realops_snomask(SNO_GENERAL
, L_ALL
, "authd WARNING: %s", parv
[3]);
239 iwarn("authd warning: %s", parv
[3]);
241 case 'C': /* Critical (error) */
242 sendto_realops_snomask(SNO_GENERAL
, L_ALL
, "authd CRITICAL: %s", parv
[3]);
243 ierror("authd critical: %s", parv
[3]);
246 sendto_realops_snomask(SNO_GENERAL
, L_ALL
, "authd sent us an unknown oper notice type (%s): %s", parv
[2], parv
[3]);
247 ilog(L_MAIN
, "authd unknown oper notice type (%s): %s", parv
[2], parv
[3]);
253 case 'X': /* Stats error */
254 case 'Y': /* Stats reply */
255 case 'Z': /* End of stats reply */
258 ilog(L_MAIN
, "authd sent a result with wrong number of arguments: got %d", parc
);
267 /* parv[0] conveys status */
270 ilog(L_MAIN
, "authd sent a result with wrong number of arguments: got %d", parc
);
274 dns_stats_results_callback(parv
[1], parv
[0], parc
- 3, (const char **)&parv
[3]);
291 ilog(L_MAIN
, "Unable to start authd helper: %s", strerror(errno
));
297 configure_authd(void)
299 /* These will do for now */
300 set_authd_timeout("ident_timeout", GlobalSetOptions
.ident_timeout
);
301 set_authd_timeout("rdns_timeout", ConfigFileEntry
.connect_timeout
);
302 set_authd_timeout("rbl_timeout", ConfigFileEntry
.connect_timeout
);
303 ident_check_enable(!ConfigFileEntry
.disable_auth
);
307 restart_authd_cb(rb_helper
* helper
)
309 ilog(L_MAIN
, "authd: restart_authd_cb called, authd died?");
310 sendto_realops_snomask(SNO_GENERAL
, L_ALL
, "authd: restart_authd_cb called, authd died?");
313 rb_helper_close(helper
);
322 restart_authd_cb(authd_helper
);
328 rb_helper_write(authd_helper
, "R");
335 if(authd_helper
== NULL
)
339 static inline uint32_t
348 /* Basically when this is called we begin handing off the client to authd for
349 * processing. authd "owns" the client until processing is finished, or we
350 * timeout from authd. authd will make a decision whether or not to accept the
351 * client, but it's up to other parts of the code (for now) to decide if we're
352 * gonna accept the client and ignore authd's suggestion.
357 authd_initiate_client(struct Client
*client_p
)
359 char client_ipaddr
[HOSTIPLEN
+1];
360 char listen_ipaddr
[HOSTIPLEN
+1];
361 uint16_t client_port
, listen_port
;
364 if(client_p
->preClient
== NULL
|| client_p
->preClient
->authd_cid
!= 0)
367 authd_cid
= client_p
->preClient
->authd_cid
= generate_cid();
369 /* Collisions are extremely unlikely, so disregard the possibility */
370 rb_dictionary_add(cid_clients
, RB_UINT_TO_POINTER(authd_cid
), client_p
);
372 /* Retrieve listener and client IP's */
373 rb_inet_ntop_sock((struct sockaddr
*)&client_p
->preClient
->lip
, listen_ipaddr
, sizeof(listen_ipaddr
));
374 rb_inet_ntop_sock((struct sockaddr
*)&client_p
->localClient
->ip
, client_ipaddr
, sizeof(client_ipaddr
));
376 /* Retrieve listener and client ports */
378 if(GET_SS_FAMILY(&client_p
->preClient
->lip
) == AF_INET6
)
379 listen_port
= ntohs(((struct sockaddr_in6
*)&client_p
->preClient
->lip
)->sin6_port
);
382 listen_port
= ntohs(((struct sockaddr_in
*)&client_p
->preClient
->lip
)->sin_port
);
385 if(GET_SS_FAMILY(&client_p
->localClient
->ip
) == AF_INET6
)
386 client_port
= ntohs(((struct sockaddr_in6
*)&client_p
->localClient
->ip
)->sin6_port
);
389 client_port
= ntohs(((struct sockaddr_in
*)&client_p
->localClient
->ip
)->sin_port
);
391 /* Add a bit of a fudge factor... */
392 client_p
->preClient
->authd_timeout
= rb_current_time() + ConfigFileEntry
.connect_timeout
+ 5;
394 rb_helper_write(authd_helper
, "C %x %s %hu %s %hu", authd_cid
, listen_ipaddr
, listen_port
, client_ipaddr
, client_port
);
397 /* When this is called we have a decision on client acceptance.
399 * After this point authd no longer "owns" the client.
402 authd_decide_client(struct Client
*client_p
, const char *ident
, const char *host
, bool accept
, char cause
, const char *data
, const char *reason
)
404 if(client_p
->preClient
== NULL
|| client_p
->preClient
->authd_cid
== 0)
409 rb_strlcpy(client_p
->username
, ident
, sizeof(client_p
->username
));
410 ServerStats
.is_abad
++; /* s_auth used to do this, stay compatible */
413 ServerStats
.is_asuc
++;
416 rb_strlcpy(client_p
->host
, host
, sizeof(client_p
->host
));
418 client_p
->preClient
->authd_accepted
= accept
;
419 client_p
->preClient
->authd_cause
= cause
;
420 client_p
->preClient
->authd_data
= (data
== NULL
? NULL
: rb_strdup(data
));
421 client_p
->preClient
->authd_reason
= (reason
== NULL
? NULL
: rb_strdup(reason
));
423 rb_dictionary_delete(cid_clients
, RB_UINT_TO_POINTER(client_p
->preClient
->authd_cid
));
425 client_p
->preClient
->authd_cid
= 0;
428 * When a client has auth'ed, we want to start reading what it sends
429 * us. This is what read_packet() does.
432 * Above comment was originally in s_auth.c, but moved here with below code.
435 rb_dlinkAddTail(client_p
, &client_p
->node
, &global_client_list
);
436 read_packet(client_p
->localClient
->F
, client_p
);
440 authd_abort_client(struct Client
*client_p
)
442 if(client_p
->preClient
== NULL
)
445 if(client_p
->preClient
->authd_cid
== 0)
448 rb_dictionary_delete(cid_clients
, RB_UINT_TO_POINTER(client_p
->preClient
->authd_cid
));
450 rb_helper_write(authd_helper
, "E %x", client_p
->preClient
->authd_cid
);
451 client_p
->preClient
->authd_cid
= 0;
455 timeout_dead_authd_clients(void *notused __unused
)
457 rb_dictionary_iter iter
;
460 RB_DICTIONARY_FOREACH(id
, &iter
, cid_clients
)
462 struct Client
*client_p
;
464 if(client_p
->preClient
->authd_timeout
< rb_current_time())
466 rb_helper_write(authd_helper
, "E %x", client_p
->preClient
->authd_cid
);
468 rb_dictionary_delete(cid_clients
, RB_UINT_TO_POINTER(client_p
->preClient
->authd_cid
));
473 /* Turn a cause char (who rejected us) into the name of the provider */
475 get_provider_string(char cause
)
490 /* Send a new blacklist to authd */
492 add_blacklist(const char *host
, const char *reason
, uint8_t iptype
, rb_dlink_list
*filters
)
495 struct blacklist_stats
*stats
= rb_malloc(sizeof(struct blacklist_stats
));
496 char filterbuf
[BUFSIZE
] = "*";
499 /* Build a list of comma-separated values for authd.
500 * We don't check for validity - do it elsewhere.
502 RB_DLINK_FOREACH(ptr
, filters
->head
)
504 char *filter
= ptr
->data
;
505 size_t filterlen
= strlen(filter
) + 1;
507 if(s
+ filterlen
> sizeof(filterbuf
))
510 snprintf(&filterbuf
[s
], sizeof(filterbuf
) - s
, "%s,", filter
);
516 filterbuf
[s
- 1] = '\0';
518 stats
->iptype
= iptype
;
520 rb_dictionary_add(bl_stats
, host
, stats
);
522 rb_helper_write(authd_helper
, "O rbl %s %hhu %s :%s", host
, iptype
, filterbuf
, reason
);
525 /* Delete a blacklist */
527 del_blacklist(const char *host
)
529 rb_dictionary_delete(bl_stats
, host
);
531 rb_helper_write(authd_helper
, "O rbl_del %s", host
);
534 /* Delete all the blacklists */
536 del_blacklist_all(void)
538 struct blacklist_stats
*stats
;
539 rb_dictionary_iter iter
;
541 RB_DICTIONARY_FOREACH(stats
, &iter
, bl_stats
)
544 rb_dictionary_delete(bl_stats
, iter
.cur
->key
);
547 rb_helper_write(authd_helper
, "O rbl_del_all");
550 /* Adjust an authd timeout value */
552 set_authd_timeout(const char *key
, int timeout
)
554 rb_helper_write(authd_helper
, "O %s %d", key
, timeout
);
558 ident_check_enable(bool enabled
)
560 rb_helper_write(authd_helper
, "O ident_enabled %d", enabled
? 1 : 0);