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)
76 ierror("Unable to execute authd in %s or %s/bin",
77 ircd_paths
[IRCD_PATH_LIBEXEC
], ConfigFileEntry
.dpath
);
78 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
79 "Unable to execute authd in %s or %s/bin",
80 ircd_paths
[IRCD_PATH_LIBEXEC
], ConfigFileEntry
.dpath
);
86 authd_path
= rb_strdup(fullpath
);
89 if(cid_clients
== NULL
)
90 cid_clients
= rb_dictionary_create("authd cid to uid mapping", rb_uint32cmp
);
93 bl_stats
= rb_dictionary_create("blacklist statistics", strcasecmp
);
95 if(timeout_ev
== NULL
)
96 timeout_ev
= rb_event_addish("timeout_dead_authd_clients", timeout_dead_authd_clients
, NULL
, 1);
98 authd_helper
= rb_helper_start("authd", authd_path
, parse_authd_reply
, restart_authd_cb
);
100 if(authd_helper
== NULL
)
102 ierror("Unable to start authd helper: %s", strerror(errno
));
103 sendto_realops_snomask(SNO_GENERAL
, L_ALL
, "Unable to start authd helper: %s", strerror(errno
));
106 ilog(L_MAIN
, "authd helper started");
107 sendto_realops_snomask(SNO_GENERAL
, L_ALL
, "authd helper started");
108 rb_helper_run(authd_helper
);
112 static inline uint32_t
113 str_to_cid(const char *str
)
115 long lcid
= strtol(str
, NULL
, 16);
117 if(lcid
> UINT32_MAX
|| lcid
<= 0)
119 iwarn("authd sent us back a bad client ID: %lx", lcid
);
124 return (uint32_t)lcid
;
127 static inline struct Client
*
128 cid_to_client(uint32_t cid
, bool delete)
130 struct Client
*client_p
;
133 client_p
= rb_dictionary_delete(cid_clients
, RB_UINT_TO_POINTER(cid
));
135 client_p
= rb_dictionary_retrieve(cid_clients
, RB_UINT_TO_POINTER(cid
));
139 iwarn("authd sent us back a bad client ID: %ux", cid
);
147 static inline struct Client
*
148 str_cid_to_client(const char *str
, bool delete)
150 uint32_t cid
= str_to_cid(str
);
155 return cid_to_client(cid
, delete);
159 parse_authd_reply(rb_helper
* helper
)
163 char authdBuf
[READBUF_SIZE
];
164 char *parv
[MAXPARA
+ 1];
165 struct Client
*client_p
;
167 while((len
= rb_helper_read(helper
, authdBuf
, sizeof(authdBuf
))) > 0)
169 parc
= rb_string_to_array(authdBuf
, parv
, MAXPARA
+1);
173 case 'A': /* Accepted a client */
176 iwarn("authd sent a result with wrong number of arguments: got %d", parc
);
181 /* cid to uid (retrieve and delete) */
182 if((client_p
= str_cid_to_client(parv
[1], true)) == NULL
)
185 authd_accept_client(client_p
, parv
[2], parv
[3]);
187 case 'R': /* Reject client */
190 iwarn("authd sent us a result with wrong number of arguments: got %d", parc
);
195 /* cid to uid (retrieve and delete) */
196 if((client_p
= str_cid_to_client(parv
[1], true)) == NULL
)
199 authd_reject_client(client_p
, parv
[3], parv
[4], toupper(*parv
[2]), parv
[5], parv
[6]);
201 case 'N': /* Notice to client */
204 iwarn("authd sent us a result with wrong number of arguments: got %d", parc
);
209 if((client_p
= str_cid_to_client(parv
[1], false)) == NULL
)
212 sendto_one_notice(client_p
, ":%s", parv
[2]);
214 case 'E': /* DNS Result */
217 iwarn("authd sent a result with wrong number of arguments: got %d", parc
);
222 dns_results_callback(parv
[1], parv
[2], parv
[3], parv
[4]);
224 case 'W': /* Oper warning */
227 iwarn("authd sent a result with wrong number of arguments: got %d", parc
);
234 case 'D': /* Debug */
235 sendto_realops_snomask(SNO_DEBUG
, L_ALL
, "authd debug: %s", parv
[3]);
236 idebug("authd: %s", parv
[3]);
239 sendto_realops_snomask(SNO_GENERAL
, L_ALL
, "authd info: %s", parv
[3]);
240 inotice("authd: %s", parv
[3]);
242 case 'W': /* Warning */
243 sendto_realops_snomask(SNO_GENERAL
, L_ALL
, "authd WARNING: %s", parv
[3]);
244 iwarn("authd: %s", parv
[3]);
246 case 'C': /* Critical (error) */
247 sendto_realops_snomask(SNO_GENERAL
, L_ALL
, "authd CRITICAL: %s", parv
[3]);
248 ierror("authd: %s", parv
[3]);
251 sendto_realops_snomask(SNO_GENERAL
, L_ALL
, "authd sent us an unknown oper notice type (%s): %s", parv
[2], parv
[3]);
252 ilog(L_MAIN
, "authd unknown oper notice type (%s): %s", parv
[2], parv
[3]);
258 case 'X': /* Stats error */
259 case 'Y': /* Stats reply */
260 case 'Z': /* End of stats reply */
263 iwarn("authd sent a result with wrong number of arguments: got %d", parc
);
272 /* parv[0] conveys status */
275 iwarn("authd sent a result with wrong number of arguments: got %d", parc
);
279 dns_stats_results_callback(parv
[1], parv
[0], parc
- 3, (const char **)&parv
[3]);
296 ierror("Unable to start authd helper: %s", strerror(errno
));
302 configure_authd(void)
304 /* These will do for now */
305 set_authd_timeout("ident_timeout", GlobalSetOptions
.ident_timeout
);
306 set_authd_timeout("rdns_timeout", ConfigFileEntry
.connect_timeout
);
307 set_authd_timeout("rbl_timeout", ConfigFileEntry
.connect_timeout
);
308 ident_check_enable(!ConfigFileEntry
.disable_auth
);
312 restart_authd_cb(rb_helper
* helper
)
314 rb_dictionary_iter iter
;
315 struct Client
*client_p
;
317 iwarn("authd: restart_authd_cb called, authd died?");
318 sendto_realops_snomask(SNO_GENERAL
, L_ALL
, "authd: restart_authd_cb called, authd died?");
322 rb_helper_close(helper
);
326 RB_DICTIONARY_FOREACH(client_p
, &iter
, cid_clients
)
328 /* Abort any existing clients */
329 authd_abort_client(client_p
);
338 restart_authd_cb(authd_helper
);
344 rb_helper_write(authd_helper
, "R");
351 if(authd_helper
== NULL
)
355 static inline uint32_t
364 /* Basically when this is called we begin handing off the client to authd for
365 * processing. authd "owns" the client until processing is finished, or we
366 * timeout from authd. authd will make a decision whether or not to accept the
367 * client, but it's up to other parts of the code (for now) to decide if we're
368 * gonna accept the client and ignore authd's suggestion.
373 authd_initiate_client(struct Client
*client_p
)
375 char client_ipaddr
[HOSTIPLEN
+1];
376 char listen_ipaddr
[HOSTIPLEN
+1];
377 uint16_t client_port
, listen_port
;
380 if(client_p
->preClient
== NULL
|| client_p
->preClient
->authd_cid
!= 0)
383 authd_cid
= client_p
->preClient
->authd_cid
= generate_cid();
385 /* Collisions are extremely unlikely, so disregard the possibility */
386 rb_dictionary_add(cid_clients
, RB_UINT_TO_POINTER(authd_cid
), client_p
);
388 /* Retrieve listener and client IP's */
389 rb_inet_ntop_sock((struct sockaddr
*)&client_p
->preClient
->lip
, listen_ipaddr
, sizeof(listen_ipaddr
));
390 rb_inet_ntop_sock((struct sockaddr
*)&client_p
->localClient
->ip
, client_ipaddr
, sizeof(client_ipaddr
));
392 /* Retrieve listener and client ports */
393 listen_port
= ntohs(GET_SS_PORT(&client_p
->preClient
->lip
));
394 client_port
= ntohs(GET_SS_PORT(&client_p
->localClient
->ip
));
396 /* Add a bit of a fudge factor... */
397 client_p
->preClient
->authd_timeout
= rb_current_time() + ConfigFileEntry
.connect_timeout
+ 10;
399 rb_helper_write(authd_helper
, "C %x %s %hu %s %hu", authd_cid
, listen_ipaddr
, listen_port
, client_ipaddr
, client_port
);
402 /* When this is called we have a decision on client acceptance.
404 * After this point authd no longer "owns" the client.
407 authd_decide_client(struct Client
*client_p
, const char *ident
, const char *host
, bool accept
, char cause
, const char *data
, const char *reason
)
409 if(client_p
->preClient
== NULL
|| client_p
->preClient
->authd_cid
== 0)
414 rb_strlcpy(client_p
->username
, ident
, sizeof(client_p
->username
));
415 ServerStats
.is_asuc
++;
418 ServerStats
.is_abad
++; /* s_auth used to do this, stay compatible */
421 rb_strlcpy(client_p
->host
, host
, sizeof(client_p
->host
));
423 client_p
->preClient
->authd_accepted
= accept
;
424 client_p
->preClient
->authd_cause
= cause
;
425 client_p
->preClient
->authd_data
= (data
== NULL
? NULL
: rb_strdup(data
));
426 client_p
->preClient
->authd_reason
= (reason
== NULL
? NULL
: rb_strdup(reason
));
428 rb_dictionary_delete(cid_clients
, RB_UINT_TO_POINTER(client_p
->preClient
->authd_cid
));
429 client_p
->preClient
->authd_cid
= 0;
432 * When a client has auth'ed, we want to start reading what it sends
433 * us. This is what read_packet() does.
436 * Above comment was originally in s_auth.c, but moved here with below code.
439 rb_dlinkAddTail(client_p
, &client_p
->node
, &global_client_list
);
440 read_packet(client_p
->localClient
->F
, client_p
);
443 /* Convenience function to accept client */
445 authd_accept_client(struct Client
*client_p
, const char *ident
, const char *host
)
447 authd_decide_client(client_p
, ident
, host
, true, '\0', NULL
, NULL
);
450 /* Convenience function to reject client */
452 authd_reject_client(struct Client
*client_p
, const char *ident
, const char *host
, char cause
, const char *data
, const char *reason
)
454 authd_decide_client(client_p
, ident
, host
, false, cause
, data
, reason
);
458 authd_abort_client(struct Client
*client_p
)
460 if(client_p
== NULL
|| client_p
->preClient
== NULL
)
463 if(client_p
->preClient
->authd_cid
== 0)
466 rb_dictionary_delete(cid_clients
, RB_UINT_TO_POINTER(client_p
->preClient
->authd_cid
));
468 if(authd_helper
!= NULL
)
469 rb_helper_write(authd_helper
, "E %x", client_p
->preClient
->authd_cid
);
471 /* XXX should we blindly allow like this? */
472 authd_accept_client(client_p
, "*", "*");
473 client_p
->preClient
->authd_cid
= 0;
477 timeout_dead_authd_clients(void *notused __unused
)
479 rb_dictionary_iter iter
;
480 struct Client
*client_p
;
482 RB_DICTIONARY_FOREACH(client_p
, &iter
, cid_clients
)
484 if(client_p
->preClient
->authd_timeout
< rb_current_time())
485 authd_abort_client(client_p
);
489 /* Turn a cause char (who rejected us) into the name of the provider */
491 get_provider_string(char cause
)
506 /* Send a new blacklist to authd */
508 add_blacklist(const char *host
, const char *reason
, uint8_t iptype
, rb_dlink_list
*filters
)
511 struct blacklist_stats
*stats
= rb_malloc(sizeof(struct blacklist_stats
));
512 char filterbuf
[BUFSIZE
] = "*";
515 /* Build a list of comma-separated values for authd.
516 * We don't check for validity - do it elsewhere.
518 RB_DLINK_FOREACH(ptr
, filters
->head
)
520 char *filter
= ptr
->data
;
521 size_t filterlen
= strlen(filter
) + 1;
523 if(s
+ filterlen
> sizeof(filterbuf
))
526 snprintf(&filterbuf
[s
], sizeof(filterbuf
) - s
, "%s,", filter
);
532 filterbuf
[s
- 1] = '\0';
534 stats
->iptype
= iptype
;
536 rb_dictionary_add(bl_stats
, host
, stats
);
538 rb_helper_write(authd_helper
, "O rbl %s %hhu %s :%s", host
, iptype
, filterbuf
, reason
);
541 /* Delete a blacklist */
543 del_blacklist(const char *host
)
545 rb_dictionary_delete(bl_stats
, host
);
547 rb_helper_write(authd_helper
, "O rbl_del %s", host
);
550 /* Delete all the blacklists */
552 del_blacklist_all(void)
554 struct blacklist_stats
*stats
;
555 rb_dictionary_iter iter
;
557 RB_DICTIONARY_FOREACH(stats
, &iter
, bl_stats
)
560 rb_dictionary_delete(bl_stats
, iter
.cur
->key
);
563 rb_helper_write(authd_helper
, "O rbl_del_all");
566 /* Adjust an authd timeout value */
568 set_authd_timeout(const char *key
, int timeout
)
573 rb_helper_write(authd_helper
, "O %s %d", key
, timeout
);
577 /* Enable identd checks */
579 ident_check_enable(bool enabled
)
581 rb_helper_write(authd_helper
, "O ident_enabled %d", enabled
? 1 : 0);
584 /* Create an OPM listener */
586 create_opm_listener(const char *ip
, uint16_t port
)
588 rb_helper_write(authd_helper
, "O opm_listener %s %hu", ip
, port
);
591 /* Disable all OPM scans */
593 opm_check_enable(bool enabled
)
595 rb_helper_write(authd_helper
, "O opm_enable %d", enabled
? 1 : 0);
598 /* Create an OPM proxy scanner */
600 create_opm_proxy_scanner(const char *type
, uint16_t port
)
602 rb_helper_write(authd_helper
, "O opm_scanner %s %hu", type
, port
);