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
);
122 if(timeout_ev
== NULL
)
123 timeout_ev
= rb_event_addish("timeout_dead_authd_clients", timeout_dead_authd_clients
, NULL
, 1);
125 authd_helper
= rb_helper_start("authd", authd_path
, parse_authd_reply
, restart_authd_cb
);
127 if(authd_helper
== NULL
)
129 ierror("Unable to start authd helper: %s", strerror(errno
));
130 sendto_realops_snomask(SNO_GENERAL
, L_ALL
, "Unable to start authd helper: %s", strerror(errno
));
134 ilog(L_MAIN
, "authd helper started");
135 sendto_realops_snomask(SNO_GENERAL
, L_ALL
, "authd helper started");
136 rb_helper_run(authd_helper
);
140 static inline uint32_t
141 str_to_cid(const char *str
)
143 long lcid
= strtol(str
, NULL
, 16);
145 if(lcid
> UINT32_MAX
|| lcid
<= 0)
147 iwarn("authd sent us back a bad client ID: %lx", lcid
);
152 return (uint32_t)lcid
;
155 static inline struct Client
*
156 cid_to_client(uint32_t ncid
, bool del
)
158 struct Client
*client_p
;
161 client_p
= rb_dictionary_delete(cid_clients
, RB_UINT_TO_POINTER(ncid
));
163 client_p
= rb_dictionary_retrieve(cid_clients
, RB_UINT_TO_POINTER(ncid
));
165 /* If the client's not found, that's okay, it may have already gone away.
171 static inline struct Client
*
172 str_cid_to_client(const char *str
, bool del
)
174 uint32_t ncid
= str_to_cid(str
);
179 return cid_to_client(ncid
, del
);
183 cmd_accept_client(int parc
, char **parv
)
185 struct Client
*client_p
;
187 /* cid to uid (retrieve and delete) */
188 if((client_p
= str_cid_to_client(parv
[1], true)) == NULL
)
191 authd_accept_client(client_p
, parv
[2], parv
[3]);
195 cmd_dns_result(int parc
, char **parv
)
197 dns_results_callback(parv
[1], parv
[2], parv
[3], parv
[4]);
201 cmd_notice_client(int parc
, char **parv
)
203 struct Client
*client_p
;
205 if((client_p
= str_cid_to_client(parv
[1], false)) == NULL
)
208 sendto_one_notice(client_p
, ":%s", parv
[2]);
212 cmd_reject_client(int parc
, char **parv
)
214 struct Client
*client_p
;
216 /* cid to uid (retrieve and delete) */
217 if((client_p
= str_cid_to_client(parv
[1], true)) == NULL
)
220 authd_reject_client(client_p
, parv
[3], parv
[4], toupper(*parv
[2]), parv
[5], parv
[6]);
224 cmd_oper_warn(int parc
, char **parv
)
228 case 'D': /* Debug */
229 sendto_realops_snomask(SNO_DEBUG
, L_ALL
, "authd debug: %s", parv
[2]);
230 idebug("authd: %s", parv
[2]);
233 sendto_realops_snomask(SNO_GENERAL
, L_ALL
, "authd info: %s", parv
[2]);
234 inotice("authd: %s", parv
[2]);
236 case 'W': /* Warning */
237 sendto_realops_snomask(SNO_GENERAL
, L_ALL
, "authd WARNING: %s", parv
[2]);
238 iwarn("authd: %s", parv
[2]);
240 case 'C': /* Critical (error) */
241 sendto_realops_snomask(SNO_GENERAL
, L_ALL
, "authd CRITICAL: %s", parv
[2]);
242 ierror("authd: %s", parv
[2]);
245 sendto_realops_snomask(SNO_GENERAL
, L_ALL
, "authd sent us an unknown oper notice type (%s): %s", parv
[1], parv
[2]);
246 ilog(L_MAIN
, "authd unknown oper notice type (%s): %s", parv
[1], parv
[2]);
252 cmd_stats_results(int parc
, char **parv
)
258 /* parv[0] conveys status */
261 iwarn("authd sent a result with wrong number of arguments: got %d", parc
);
265 dns_stats_results_callback(parv
[1], parv
[0], parc
- 3, (const char **)&parv
[3]);
273 parse_authd_reply(rb_helper
* helper
)
277 char buf
[READBUF_SIZE
];
278 char *parv
[MAXPARA
+ 1];
280 while((len
= rb_helper_read(helper
, buf
, sizeof(buf
))) > 0)
282 struct authd_cb
*cmd
;
284 parc
= rb_string_to_array(buf
, parv
, MAXPARA
);
285 cmd
= &authd_cmd_tab
[(unsigned char)*parv
[0]];
288 if(cmd
->min_parc
> parc
)
290 iwarn("authd sent a result with wrong number of arguments: expected %d, got %d",
291 cmd
->min_parc
, parc
);
300 iwarn("authd sent us a bad command type: %c", *parv
[0]);
312 ierror("Unable to start authd helper: %s", strerror(errno
));
318 configure_authd(void)
321 set_authd_timeout("ident_timeout", GlobalSetOptions
.ident_timeout
);
322 set_authd_timeout("rdns_timeout", ConfigFileEntry
.connect_timeout
);
323 set_authd_timeout("rbl_timeout", ConfigFileEntry
.connect_timeout
);
325 ident_check_enable(!ConfigFileEntry
.disable_auth
);
328 if(rb_dlink_list_length(&opm_list
) > 0 &&
329 (opm_listeners
[LISTEN_IPV4
].ipaddr
[0] != '\0' ||
330 opm_listeners
[LISTEN_IPV6
].ipaddr
[0] != '\0'))
334 if(opm_listeners
[LISTEN_IPV4
].ipaddr
[0] != '\0')
335 rb_helper_write(authd_helper
, "O opm_listener %s %hu",
336 opm_listeners
[LISTEN_IPV4
].ipaddr
, opm_listeners
[LISTEN_IPV4
].port
);
339 if(opm_listeners
[LISTEN_IPV6
].ipaddr
[0] != '\0')
340 rb_helper_write(authd_helper
, "O opm_listener %s %hu",
341 opm_listeners
[LISTEN_IPV6
].ipaddr
, opm_listeners
[LISTEN_IPV6
].port
);
344 RB_DLINK_FOREACH(ptr
, opm_list
.head
)
346 struct OPMScanner
*scanner
= ptr
->data
;
347 rb_helper_write(authd_helper
, "O opm_scanner %s %hu",
348 scanner
->type
, scanner
->port
);
351 opm_check_enable(true);
354 opm_check_enable(false);
358 authd_free_client(struct Client
*client_p
)
360 if(client_p
== NULL
|| client_p
->preClient
== NULL
)
363 if(client_p
->preClient
->auth
.cid
== 0)
366 if(authd_helper
!= NULL
)
367 rb_helper_write(authd_helper
, "E %x", client_p
->preClient
->auth
.cid
);
369 client_p
->preClient
->auth
.accepted
= true;
370 client_p
->preClient
->auth
.cid
= 0;
374 authd_free_client_cb(rb_dictionary_element
*delem
, void *unused
)
376 struct Client
*client_p
= delem
->data
;
377 authd_free_client(client_p
);
381 authd_abort_client(struct Client
*client_p
)
383 rb_dictionary_delete(cid_clients
, RB_UINT_TO_POINTER(client_p
->preClient
->auth
.cid
));
384 authd_free_client(client_p
);
388 restart_authd_cb(rb_helper
* helper
)
390 iwarn("authd: restart_authd_cb called, authd died?");
391 sendto_realops_snomask(SNO_GENERAL
, L_ALL
, "authd: restart_authd_cb called, authd died?");
395 rb_helper_close(helper
);
399 rb_dictionary_destroy(cid_clients
, authd_free_client_cb
, NULL
);
409 ierror("authd restarting...");
410 restart_authd_cb(authd_helper
);
416 rb_helper_write(authd_helper
, "R");
422 if(authd_helper
== NULL
)
426 static inline uint32_t
435 /* Basically when this is called we begin handing off the client to authd for
436 * processing. authd "owns" the client until processing is finished, or we
437 * timeout from authd. authd will make a decision whether or not to accept the
438 * client, but it's up to other parts of the code (for now) to decide if we're
439 * gonna accept the client and ignore authd's suggestion.
443 * If this is an SSL connection we must defer handing off the client for
444 * reading until it is open and we have the certificate fingerprint, otherwise
445 * it's possible for the client to immediately send data before authd completes
446 * and before the status of the connection is communicated via ssld. This data
447 * could then be processed too early by read_packet().
450 authd_initiate_client(struct Client
*client_p
, bool defer
)
452 char client_ipaddr
[HOSTIPLEN
+1];
453 char listen_ipaddr
[HOSTIPLEN
+1];
454 uint16_t client_port
, listen_port
;
457 if(client_p
->preClient
== NULL
|| client_p
->preClient
->auth
.cid
!= 0)
460 authd_cid
= client_p
->preClient
->auth
.cid
= generate_cid();
462 /* Collisions are extremely unlikely, so disregard the possibility */
463 rb_dictionary_add(cid_clients
, RB_UINT_TO_POINTER(authd_cid
), client_p
);
465 /* Retrieve listener and client IP's */
466 rb_inet_ntop_sock((struct sockaddr
*)&client_p
->preClient
->lip
, listen_ipaddr
, sizeof(listen_ipaddr
));
467 rb_inet_ntop_sock((struct sockaddr
*)&client_p
->localClient
->ip
, client_ipaddr
, sizeof(client_ipaddr
));
469 /* Retrieve listener and client ports */
470 listen_port
= ntohs(GET_SS_PORT(&client_p
->preClient
->lip
));
471 client_port
= ntohs(GET_SS_PORT(&client_p
->localClient
->ip
));
474 client_p
->preClient
->auth
.flags
|= AUTHC_F_DEFERRED
;
476 /* Add a bit of a fudge factor... */
477 client_p
->preClient
->auth
.timeout
= rb_current_time() + ConfigFileEntry
.connect_timeout
+ 10;
479 rb_helper_write(authd_helper
, "C %x %s %hu %s %hu", authd_cid
, listen_ipaddr
, listen_port
, client_ipaddr
, client_port
);
483 authd_read_client(struct Client
*client_p
)
486 * When a client has auth'ed, we want to start reading what it sends
487 * us. This is what read_packet() does.
490 * Above comment was originally in s_auth.c, but moved here with below code.
493 rb_dlinkAddTail(client_p
, &client_p
->node
, &global_client_list
);
494 read_packet(client_p
->localClient
->F
, client_p
);
497 /* When this is called we have a decision on client acceptance.
499 * After this point authd no longer "owns" the client, but if
500 * it's flagged as deferred then we're still waiting for a call
501 * to authd_deferred_client().
504 authd_decide_client(struct Client
*client_p
, const char *ident
, const char *host
, bool accept
, char cause
, const char *data
, const char *reason
)
506 if(client_p
->preClient
== NULL
|| client_p
->preClient
->auth
.cid
== 0)
511 rb_strlcpy(client_p
->username
, ident
, sizeof(client_p
->username
));
513 ServerStats
.is_asuc
++;
516 ServerStats
.is_abad
++; /* s_auth used to do this, stay compatible */
519 rb_strlcpy(client_p
->host
, host
, sizeof(client_p
->host
));
521 rb_dictionary_delete(cid_clients
, RB_UINT_TO_POINTER(client_p
->preClient
->auth
.cid
));
523 client_p
->preClient
->auth
.accepted
= accept
;
524 client_p
->preClient
->auth
.cause
= cause
;
525 client_p
->preClient
->auth
.data
= (data
== NULL
? NULL
: rb_strdup(data
));
526 client_p
->preClient
->auth
.reason
= (reason
== NULL
? NULL
: rb_strdup(reason
));
527 client_p
->preClient
->auth
.cid
= 0;
529 client_p
->preClient
->auth
.flags
|= AUTHC_F_COMPLETE
;
530 if((client_p
->preClient
->auth
.flags
& AUTHC_F_DEFERRED
) == 0)
531 authd_read_client(client_p
);
535 authd_deferred_client(struct Client
*client_p
)
537 client_p
->preClient
->auth
.flags
&= ~AUTHC_F_DEFERRED
;
538 if(client_p
->preClient
->auth
.flags
& AUTHC_F_COMPLETE
)
539 authd_read_client(client_p
);
542 /* Convenience function to accept client */
544 authd_accept_client(struct Client
*client_p
, const char *ident
, const char *host
)
546 authd_decide_client(client_p
, ident
, host
, true, '\0', NULL
, NULL
);
549 /* Convenience function to reject client */
551 authd_reject_client(struct Client
*client_p
, const char *ident
, const char *host
, char cause
, const char *data
, const char *reason
)
553 authd_decide_client(client_p
, ident
, host
, false, cause
, data
, reason
);
557 timeout_dead_authd_clients(void *notused __unused
)
559 rb_dictionary_iter iter
;
560 struct Client
*client_p
;
561 rb_dlink_list freelist
= { NULL
, NULL
, 0 };
562 rb_dlink_node
*ptr
, *nptr
;
564 RB_DICTIONARY_FOREACH(client_p
, &iter
, cid_clients
)
566 if(client_p
->preClient
->auth
.timeout
< rb_current_time())
568 authd_free_client(client_p
);
569 rb_dlinkAddAlloc(client_p
, &freelist
);
573 /* RB_DICTIONARY_FOREACH is not safe for deletion, so we do this crap */
574 RB_DLINK_FOREACH_SAFE(ptr
, nptr
, freelist
.head
)
576 client_p
= ptr
->data
;
577 rb_dictionary_delete(cid_clients
, RB_UINT_TO_POINTER(client_p
->preClient
->auth
.cid
));
581 /* Send a new blacklist to authd */
583 add_blacklist(const char *host
, const char *reason
, uint8_t iptype
, rb_dlink_list
*filters
)
586 struct BlacklistStats
*stats
= rb_malloc(sizeof(struct BlacklistStats
));
587 char filterbuf
[BUFSIZE
] = "*";
591 bl_stats
= rb_dictionary_create("blacklist statistics", rb_strcasecmp
);
593 /* Build a list of comma-separated values for authd.
594 * We don't check for validity - do it elsewhere.
596 RB_DLINK_FOREACH(ptr
, filters
->head
)
598 char *filter
= ptr
->data
;
599 size_t filterlen
= strlen(filter
) + 1;
601 if(s
+ filterlen
> sizeof(filterbuf
))
604 snprintf(&filterbuf
[s
], sizeof(filterbuf
) - s
, "%s,", filter
);
610 filterbuf
[s
- 1] = '\0';
612 stats
->host
= rb_strdup(host
);
613 stats
->iptype
= iptype
;
615 rb_dictionary_add(bl_stats
, stats
->host
, stats
);
617 rb_helper_write(authd_helper
, "O rbl %s %hhu %s :%s", host
, iptype
, filterbuf
, reason
);
620 /* Delete a blacklist */
622 del_blacklist(const char *host
)
624 struct BlacklistStats
*stats
= rb_dictionary_retrieve(bl_stats
, host
);
627 rb_dictionary_delete(bl_stats
, host
);
628 rb_free(stats
->host
);
632 rb_helper_write(authd_helper
, "O rbl_del %s", host
);
636 blacklist_delete(rb_dictionary_element
*delem
, void *unused
)
638 struct BlacklistStats
*stats
= delem
->data
;
640 rb_free(stats
->host
);
644 /* Delete all the blacklists */
646 del_blacklist_all(void)
649 rb_dictionary_destroy(bl_stats
, blacklist_delete
, NULL
);
652 rb_helper_write(authd_helper
, "O rbl_del_all");
655 /* Adjust an authd timeout value */
657 set_authd_timeout(const char *key
, int timeout
)
662 rb_helper_write(authd_helper
, "O %s %d", key
, timeout
);
666 /* Enable identd checks */
668 ident_check_enable(bool enabled
)
670 rb_helper_write(authd_helper
, "O ident_enabled %d", enabled
? 1 : 0);
673 /* Create an OPM listener
674 * XXX - This is a big nasty hack, but it avoids resending duplicate data when
675 * configure_authd() is called.
678 conf_create_opm_listener(const char *ip
, uint16_t port
)
680 char ipbuf
[HOSTIPLEN
];
681 struct OPMListener
*listener
;
683 rb_strlcpy(ipbuf
, ip
, sizeof(ipbuf
));
686 memmove(ipbuf
+ 1, ipbuf
, sizeof(ipbuf
) - 1);
690 /* I am much too lazy to use rb_inet_pton and GET_SS_FAMILY for now --Elizafox */
691 listener
= &opm_listeners
[(strchr(ipbuf
, ':') != NULL
? LISTEN_IPV6
: LISTEN_IPV4
)];
692 rb_strlcpy(listener
->ipaddr
, ipbuf
, sizeof(listener
->ipaddr
));
693 listener
->port
= port
;
697 create_opm_listener(const char *ip
, uint16_t port
)
699 char ipbuf
[HOSTIPLEN
];
701 /* XXX duplicated in conf_create_opm_listener */
702 rb_strlcpy(ipbuf
, ip
, sizeof(ipbuf
));
705 memmove(ipbuf
+ 1, ipbuf
, sizeof(ipbuf
) - 1);
709 conf_create_opm_listener(ip
, port
);
710 rb_helper_write(authd_helper
, "O opm_listener %s %hu", ipbuf
, port
);
714 delete_opm_listener_all(void)
716 memset(&opm_listeners
, 0, sizeof(opm_listeners
));
717 rb_helper_write(authd_helper
, "O opm_listener_del_all");
720 /* Disable all OPM scans */
722 opm_check_enable(bool enabled
)
724 rb_helper_write(authd_helper
, "O opm_enabled %d", enabled
? 1 : 0);
727 /* Create an OPM proxy scanner
728 * XXX - This is a big nasty hack, but it avoids resending duplicate data when
729 * configure_authd() is called.
732 conf_create_opm_proxy_scanner(const char *type
, uint16_t port
)
734 struct OPMScanner
*scanner
= rb_malloc(sizeof(struct OPMScanner
));
736 rb_strlcpy(scanner
->type
, type
, sizeof(scanner
->type
));
737 scanner
->port
= port
;
738 rb_dlinkAdd(scanner
, &scanner
->node
, &opm_list
);
742 create_opm_proxy_scanner(const char *type
, uint16_t port
)
744 conf_create_opm_proxy_scanner(type
, port
);
745 rb_helper_write(authd_helper
, "O opm_scanner %s %hu", type
, port
);
749 delete_opm_proxy_scanner(const char *type
, uint16_t port
)
751 rb_dlink_node
*ptr
, *nptr
;
753 RB_DLINK_FOREACH_SAFE(ptr
, nptr
, opm_list
.head
)
755 struct OPMScanner
*scanner
= ptr
->data
;
757 if(rb_strncasecmp(scanner
->type
, type
, sizeof(scanner
->type
)) == 0 &&
758 scanner
->port
== port
)
760 rb_dlinkDelete(ptr
, &opm_list
);
766 rb_helper_write(authd_helper
, "O opm_scanner_del %s %hu", type
, port
);
770 delete_opm_proxy_scanner_all(void)
772 rb_dlink_node
*ptr
, *nptr
;
774 RB_DLINK_FOREACH_SAFE(ptr
, nptr
, opm_list
.head
)
776 struct OPMScanner
*scanner
= ptr
->data
;
778 rb_dlinkDelete(ptr
, &opm_list
);
782 rb_helper_write(authd_helper
, "O opm_scanner_del_all");