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 typedef void (*authd_cb_t
)(int, char **);
51 static int start_authd(void);
52 static void parse_authd_reply(rb_helper
* helper
);
53 static void restart_authd_cb(rb_helper
* helper
);
54 static EVH timeout_dead_authd_clients
;
56 static void cmd_accept_client(int parc
, char **parv
);
57 static void cmd_reject_client(int parc
, char **parv
);
58 static void cmd_dns_result(int parc
, char **parv
);
59 static void cmd_notice_client(int parc
, char **parv
);
60 static void cmd_oper_warn(int parc
, char **parv
);
61 static void cmd_stats_results(int parc
, char **parv
);
63 rb_helper
*authd_helper
;
64 static char *authd_path
;
67 static rb_dictionary
*cid_clients
;
68 static struct ev_entry
*timeout_ev
;
70 rb_dictionary
*bl_stats
;
72 rb_dlink_list opm_list
;
73 struct OPMListener opm_listeners
[LISTEN_LAST
];
75 static struct authd_cb authd_cmd_tab
[256] =
77 ['A'] = { cmd_accept_client
, 4 },
78 ['E'] = { cmd_dns_result
, 5 },
79 ['N'] = { cmd_notice_client
, 3 },
80 ['R'] = { cmd_reject_client
, 7 },
81 ['W'] = { cmd_oper_warn
, 3 },
82 ['X'] = { cmd_stats_results
, 3 },
83 ['Y'] = { cmd_stats_results
, 3 },
84 ['Z'] = { cmd_stats_results
, 3 },
90 char fullpath
[PATH_MAX
+ 1];
92 const char *suffix
= ".exe";
94 const char *suffix
= "";
96 if(authd_path
== NULL
)
98 snprintf(fullpath
, sizeof(fullpath
), "%s%cauthd%s", ircd_paths
[IRCD_PATH_LIBEXEC
], RB_PATH_SEPARATOR
, suffix
);
100 if(access(fullpath
, X_OK
) == -1)
102 snprintf(fullpath
, sizeof(fullpath
), "%s%cbin%cauthd%s",
103 ConfigFileEntry
.dpath
, RB_PATH_SEPARATOR
, RB_PATH_SEPARATOR
, suffix
);
104 if(access(fullpath
, X_OK
) == -1)
106 ierror("Unable to execute authd in %s or %s/bin",
107 ircd_paths
[IRCD_PATH_LIBEXEC
], ConfigFileEntry
.dpath
);
108 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
109 "Unable to execute authd in %s or %s/bin",
110 ircd_paths
[IRCD_PATH_LIBEXEC
], ConfigFileEntry
.dpath
);
116 authd_path
= rb_strdup(fullpath
);
119 if(cid_clients
== NULL
)
120 cid_clients
= rb_dictionary_create("authd cid to uid mapping", rb_uint32cmp
);
123 bl_stats
= rb_dictionary_create("blacklist statistics", rb_strcasecmp
);
125 if(timeout_ev
== NULL
)
126 timeout_ev
= rb_event_addish("timeout_dead_authd_clients", timeout_dead_authd_clients
, NULL
, 1);
128 authd_helper
= rb_helper_start("authd", authd_path
, parse_authd_reply
, restart_authd_cb
);
130 if(authd_helper
== NULL
)
132 ierror("Unable to start authd helper: %s", strerror(errno
));
133 sendto_realops_snomask(SNO_GENERAL
, L_ALL
, "Unable to start authd helper: %s", strerror(errno
));
136 ilog(L_MAIN
, "authd helper started");
137 sendto_realops_snomask(SNO_GENERAL
, L_ALL
, "authd helper started");
138 rb_helper_run(authd_helper
);
142 static inline uint32_t
143 str_to_cid(const char *str
)
145 long lcid
= strtol(str
, NULL
, 16);
147 if(lcid
> UINT32_MAX
|| lcid
<= 0)
149 iwarn("authd sent us back a bad client ID: %lx", lcid
);
154 return (uint32_t)lcid
;
157 static inline struct Client
*
158 cid_to_client(uint32_t cid
, bool delete)
160 struct Client
*client_p
;
163 client_p
= rb_dictionary_delete(cid_clients
, RB_UINT_TO_POINTER(cid
));
165 client_p
= rb_dictionary_retrieve(cid_clients
, RB_UINT_TO_POINTER(cid
));
167 /* If the client's not found, that's okay, it may have already gone away.
173 static inline struct Client
*
174 str_cid_to_client(const char *str
, bool delete)
176 uint32_t cid
= str_to_cid(str
);
181 return cid_to_client(cid
, delete);
185 cmd_accept_client(int parc
, char **parv
)
187 struct Client
*client_p
;
189 /* cid to uid (retrieve and delete) */
190 if((client_p
= str_cid_to_client(parv
[1], true)) == NULL
)
193 authd_accept_client(client_p
, parv
[2], parv
[3]);
197 cmd_dns_result(int parc
, char **parv
)
199 dns_results_callback(parv
[1], parv
[2], parv
[3], parv
[4]);
203 cmd_notice_client(int parc
, char **parv
)
205 struct Client
*client_p
;
207 if((client_p
= str_cid_to_client(parv
[1], false)) == NULL
)
210 sendto_one_notice(client_p
, ":%s", parv
[2]);
214 cmd_reject_client(int parc
, char **parv
)
216 struct Client
*client_p
;
218 /* cid to uid (retrieve and delete) */
219 if((client_p
= str_cid_to_client(parv
[1], true)) == NULL
)
222 authd_reject_client(client_p
, parv
[3], parv
[4], toupper(*parv
[2]), parv
[5], parv
[6]);
226 cmd_oper_warn(int parc
, char **parv
)
230 case 'D': /* Debug */
231 sendto_realops_snomask(SNO_DEBUG
, L_ALL
, "authd debug: %s", parv
[2]);
232 idebug("authd: %s", parv
[2]);
235 sendto_realops_snomask(SNO_GENERAL
, L_ALL
, "authd info: %s", parv
[2]);
236 inotice("authd: %s", parv
[2]);
238 case 'W': /* Warning */
239 sendto_realops_snomask(SNO_GENERAL
, L_ALL
, "authd WARNING: %s", parv
[2]);
240 iwarn("authd: %s", parv
[2]);
242 case 'C': /* Critical (error) */
243 sendto_realops_snomask(SNO_GENERAL
, L_ALL
, "authd CRITICAL: %s", parv
[2]);
244 ierror("authd: %s", parv
[2]);
247 sendto_realops_snomask(SNO_GENERAL
, L_ALL
, "authd sent us an unknown oper notice type (%s): %s", parv
[1], parv
[2]);
248 ilog(L_MAIN
, "authd unknown oper notice type (%s): %s", parv
[1], parv
[2]);
254 cmd_stats_results(int parc
, char **parv
)
260 /* parv[0] conveys status */
263 iwarn("authd sent a result with wrong number of arguments: got %d", parc
);
267 dns_stats_results_callback(parv
[1], parv
[0], parc
- 3, (const char **)&parv
[3]);
275 parse_authd_reply(rb_helper
* helper
)
279 char buf
[READBUF_SIZE
];
280 char *parv
[MAXPARA
+ 1];
282 while((len
= rb_helper_read(helper
, buf
, sizeof(buf
))) > 0)
284 struct authd_cb
*cmd
;
286 parc
= rb_string_to_array(buf
, parv
, MAXPARA
+1);
287 cmd
= &authd_cmd_tab
[(unsigned char)*parv
[0]];
290 if(cmd
->min_parc
> parc
)
292 iwarn("authd sent a result with wrong number of arguments: expected %d, got %d",
293 cmd
->min_parc
, parc
);
302 iwarn("authd sent us a bad command type: %c", *parv
[0]);
314 ierror("Unable to start authd helper: %s", strerror(errno
));
320 configure_authd(void)
323 set_authd_timeout("ident_timeout", GlobalSetOptions
.ident_timeout
);
324 set_authd_timeout("rdns_timeout", ConfigFileEntry
.connect_timeout
);
325 set_authd_timeout("rbl_timeout", ConfigFileEntry
.connect_timeout
);
327 ident_check_enable(!ConfigFileEntry
.disable_auth
);
330 if(rb_dlink_list_length(&opm_list
) > 0 &&
331 (opm_listeners
[LISTEN_IPV4
].ipaddr
[0] != '\0' ||
332 opm_listeners
[LISTEN_IPV6
].ipaddr
[0] != '\0'))
336 if(opm_listeners
[LISTEN_IPV4
].ipaddr
[0] != '\0')
337 rb_helper_write(authd_helper
, "O opm_listener %s %hu",
338 opm_listeners
[LISTEN_IPV4
].ipaddr
, opm_listeners
[LISTEN_IPV4
].port
);
341 if(opm_listeners
[LISTEN_IPV6
].ipaddr
[0] != '\0')
342 rb_helper_write(authd_helper
, "O opm_listener %s %hu",
343 opm_listeners
[LISTEN_IPV6
].ipaddr
, opm_listeners
[LISTEN_IPV6
].port
);
346 RB_DLINK_FOREACH(ptr
, opm_list
.head
)
348 struct OPMScanner
*scanner
= ptr
->data
;
349 rb_helper_write(authd_helper
, "O opm_scanner %s %hu",
350 scanner
->type
, scanner
->port
);
353 opm_check_enable(true);
356 opm_check_enable(false);
360 restart_authd_cb(rb_helper
* helper
)
362 rb_dictionary_iter iter
;
363 struct Client
*client_p
;
365 iwarn("authd: restart_authd_cb called, authd died?");
366 sendto_realops_snomask(SNO_GENERAL
, L_ALL
, "authd: restart_authd_cb called, authd died?");
370 rb_helper_close(helper
);
374 RB_DICTIONARY_FOREACH(client_p
, &iter
, cid_clients
)
376 /* Abort any existing clients */
377 authd_abort_client(client_p
);
387 ierror("authd restarting...");
388 restart_authd_cb(authd_helper
);
394 rb_helper_write(authd_helper
, "R");
400 if(authd_helper
== NULL
)
404 static inline uint32_t
413 /* Basically when this is called we begin handing off the client to authd for
414 * processing. authd "owns" the client until processing is finished, or we
415 * timeout from authd. authd will make a decision whether or not to accept the
416 * client, but it's up to other parts of the code (for now) to decide if we're
417 * gonna accept the client and ignore authd's suggestion.
422 authd_initiate_client(struct Client
*client_p
)
424 char client_ipaddr
[HOSTIPLEN
+1];
425 char listen_ipaddr
[HOSTIPLEN
+1];
426 uint16_t client_port
, listen_port
;
429 if(client_p
->preClient
== NULL
|| client_p
->preClient
->auth
.cid
!= 0)
432 authd_cid
= client_p
->preClient
->auth
.cid
= generate_cid();
434 /* Collisions are extremely unlikely, so disregard the possibility */
435 rb_dictionary_add(cid_clients
, RB_UINT_TO_POINTER(authd_cid
), client_p
);
437 /* Retrieve listener and client IP's */
438 rb_inet_ntop_sock((struct sockaddr
*)&client_p
->preClient
->lip
, listen_ipaddr
, sizeof(listen_ipaddr
));
439 rb_inet_ntop_sock((struct sockaddr
*)&client_p
->localClient
->ip
, client_ipaddr
, sizeof(client_ipaddr
));
441 /* Retrieve listener and client ports */
442 listen_port
= ntohs(GET_SS_PORT(&client_p
->preClient
->lip
));
443 client_port
= ntohs(GET_SS_PORT(&client_p
->localClient
->ip
));
445 /* Add a bit of a fudge factor... */
446 client_p
->preClient
->auth
.timeout
= rb_current_time() + ConfigFileEntry
.connect_timeout
+ 10;
448 rb_helper_write(authd_helper
, "C %x %s %hu %s %hu", authd_cid
, listen_ipaddr
, listen_port
, client_ipaddr
, client_port
);
451 /* When this is called we have a decision on client acceptance.
453 * After this point authd no longer "owns" the client.
456 authd_decide_client(struct Client
*client_p
, const char *ident
, const char *host
, bool accept
, char cause
, const char *data
, const char *reason
)
458 if(client_p
->preClient
== NULL
|| client_p
->preClient
->auth
.cid
== 0)
463 rb_strlcpy(client_p
->username
, ident
, sizeof(client_p
->username
));
465 ServerStats
.is_asuc
++;
468 ServerStats
.is_abad
++; /* s_auth used to do this, stay compatible */
471 rb_strlcpy(client_p
->host
, host
, sizeof(client_p
->host
));
473 rb_dictionary_delete(cid_clients
, RB_UINT_TO_POINTER(client_p
->preClient
->auth
.cid
));
475 client_p
->preClient
->auth
.accepted
= accept
;
476 client_p
->preClient
->auth
.cause
= cause
;
477 client_p
->preClient
->auth
.data
= (data
== NULL
? NULL
: rb_strdup(data
));
478 client_p
->preClient
->auth
.reason
= (reason
== NULL
? NULL
: rb_strdup(reason
));
479 client_p
->preClient
->auth
.cid
= 0;
482 * When a client has auth'ed, we want to start reading what it sends
483 * us. This is what read_packet() does.
486 * Above comment was originally in s_auth.c, but moved here with below code.
489 rb_dlinkAddTail(client_p
, &client_p
->node
, &global_client_list
);
490 read_packet(client_p
->localClient
->F
, client_p
);
493 /* Convenience function to accept client */
495 authd_accept_client(struct Client
*client_p
, const char *ident
, const char *host
)
497 authd_decide_client(client_p
, ident
, host
, true, '\0', NULL
, NULL
);
500 /* Convenience function to reject client */
502 authd_reject_client(struct Client
*client_p
, const char *ident
, const char *host
, char cause
, const char *data
, const char *reason
)
504 authd_decide_client(client_p
, ident
, host
, false, cause
, data
, reason
);
508 authd_abort_client(struct Client
*client_p
)
510 if(client_p
== NULL
|| client_p
->preClient
== NULL
)
513 if(client_p
->preClient
->auth
.cid
== 0)
516 rb_dictionary_delete(cid_clients
, RB_UINT_TO_POINTER(client_p
->preClient
->auth
.cid
));
518 if(authd_helper
!= NULL
)
519 rb_helper_write(authd_helper
, "E %x", client_p
->preClient
->auth
.cid
);
521 client_p
->preClient
->auth
.accepted
= true;
522 client_p
->preClient
->auth
.cid
= 0;
526 timeout_dead_authd_clients(void *notused __unused
)
528 rb_dictionary_iter iter
;
529 struct Client
*client_p
;
531 RB_DICTIONARY_FOREACH(client_p
, &iter
, cid_clients
)
533 if(client_p
->preClient
->auth
.timeout
< rb_current_time())
534 authd_abort_client(client_p
);
538 /* Send a new blacklist to authd */
540 add_blacklist(const char *host
, const char *reason
, uint8_t iptype
, rb_dlink_list
*filters
)
543 struct BlacklistStats
*stats
= rb_malloc(sizeof(struct BlacklistStats
));
544 char filterbuf
[BUFSIZE
] = "*";
547 /* Build a list of comma-separated values for authd.
548 * We don't check for validity - do it elsewhere.
550 RB_DLINK_FOREACH(ptr
, filters
->head
)
552 char *filter
= ptr
->data
;
553 size_t filterlen
= strlen(filter
) + 1;
555 if(s
+ filterlen
> sizeof(filterbuf
))
558 snprintf(&filterbuf
[s
], sizeof(filterbuf
) - s
, "%s,", filter
);
564 filterbuf
[s
- 1] = '\0';
566 stats
->host
= rb_strdup(host
);
567 stats
->iptype
= iptype
;
569 rb_dictionary_add(bl_stats
, host
, stats
);
571 rb_helper_write(authd_helper
, "O rbl %s %hhu %s :%s", host
, iptype
, filterbuf
, reason
);
574 /* Delete a blacklist */
576 del_blacklist(const char *host
)
578 struct BlacklistStats
*stats
= rb_dictionary_retrieve(bl_stats
, host
);
581 rb_dictionary_delete(bl_stats
, host
);
582 rb_free(stats
->host
);
586 rb_helper_write(authd_helper
, "O rbl_del %s", host
);
589 /* Delete all the blacklists */
591 del_blacklist_all(void)
593 struct BlacklistStats
*stats
;
594 rb_dictionary_iter iter
;
596 RB_DICTIONARY_FOREACH(stats
, &iter
, bl_stats
)
598 rb_dictionary_delete(bl_stats
, stats
->host
);
599 rb_free(stats
->host
);
603 rb_helper_write(authd_helper
, "O rbl_del_all");
606 /* Adjust an authd timeout value */
608 set_authd_timeout(const char *key
, int timeout
)
613 rb_helper_write(authd_helper
, "O %s %d", key
, timeout
);
617 /* Enable identd checks */
619 ident_check_enable(bool enabled
)
621 rb_helper_write(authd_helper
, "O ident_enabled %d", enabled
? 1 : 0);
624 /* Create an OPM listener
625 * XXX - This is a big nasty hack, but it avoids resending duplicate data when
626 * configure_authd() is called.
629 conf_create_opm_listener(const char *ip
, uint16_t port
)
631 char ipbuf
[HOSTIPLEN
];
632 struct OPMListener
*listener
;
634 rb_strlcpy(ipbuf
, ip
, sizeof(ipbuf
));
637 memmove(ipbuf
+ 1, ipbuf
, sizeof(ipbuf
) - 1);
641 /* I am much too lazy to use rb_inet_pton and GET_SS_FAMILY for now --Elizafox */
642 listener
= &opm_listeners
[(strchr(ipbuf
, ':') != NULL
? LISTEN_IPV6
: LISTEN_IPV4
)];
643 rb_strlcpy(listener
->ipaddr
, ipbuf
, sizeof(listener
->ipaddr
));
644 listener
->port
= port
;
648 create_opm_listener(const char *ip
, uint16_t port
)
650 char ipbuf
[HOSTIPLEN
];
652 /* XXX duplicated in conf_create_opm_listener */
653 rb_strlcpy(ipbuf
, ip
, sizeof(ipbuf
));
656 memmove(ipbuf
+ 1, ipbuf
, sizeof(ipbuf
) - 1);
660 conf_create_opm_listener(ip
, port
);
661 rb_helper_write(authd_helper
, "O opm_listener %s %hu", ipbuf
, port
);
665 delete_opm_listener_all(void)
667 memset(&opm_listeners
, 0, sizeof(opm_listeners
));
668 rb_helper_write(authd_helper
, "O opm_listener_del_all");
671 /* Disable all OPM scans */
673 opm_check_enable(bool enabled
)
675 rb_helper_write(authd_helper
, "O opm_enabled %d", enabled
? 1 : 0);
678 /* Create an OPM proxy scanner
679 * XXX - This is a big nasty hack, but it avoids resending duplicate data when
680 * configure_authd() is called.
683 conf_create_opm_proxy_scanner(const char *type
, uint16_t port
)
685 struct OPMScanner
*scanner
= rb_malloc(sizeof(struct OPMScanner
));
687 rb_strlcpy(scanner
->type
, type
, sizeof(scanner
->type
));
688 scanner
->port
= port
;
689 rb_dlinkAdd(scanner
, &scanner
->node
, &opm_list
);
693 create_opm_proxy_scanner(const char *type
, uint16_t port
)
695 conf_create_opm_proxy_scanner(type
, port
);
696 rb_helper_write(authd_helper
, "O opm_scanner %s %hu", type
, port
);
700 delete_opm_proxy_scanner(const char *type
, uint16_t port
)
702 rb_dlink_node
*ptr
, *nptr
;
704 RB_DLINK_FOREACH_SAFE(ptr
, nptr
, opm_list
.head
)
706 struct OPMScanner
*scanner
= ptr
->data
;
708 if(rb_strncasecmp(scanner
->type
, type
, sizeof(scanner
->type
)) == 0 &&
709 scanner
->port
== port
)
711 rb_dlinkDelete(ptr
, &opm_list
);
717 rb_helper_write(authd_helper
, "O opm_scanner_del %s %hu", type
, port
);
721 delete_opm_proxy_scanner_all(void)
723 rb_dlink_node
*ptr
, *nptr
;
725 RB_DLINK_FOREACH_SAFE(ptr
, nptr
, opm_list
.head
)
727 struct OPMScanner
*scanner
= ptr
->data
;
729 rb_dlinkDelete(ptr
, &opm_list
);
733 rb_helper_write(authd_helper
, "O opm_scanner_del_all");