2 * IRC - Internet Relay Chat, ircd/ircd_auth.c
3 * Copyright 2004 Michael Poole <mdpoole@troilus.org>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2, or (at your option)
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
25 #include "ircd_alloc.h"
26 #include "ircd_auth.h"
27 #include "ircd_events.h"
28 #include "ircd_features.h"
30 #include "ircd_osdep.h"
31 #include "ircd_snprintf.h"
32 #include "ircd_string.h"
48 #include <sys/socket.h>
49 #include <netinet/in.h>
50 #include <arpa/inet.h>
56 struct IAuthRequest *iar_prev; /* previous request struct */
57 struct IAuthRequest *iar_next; /* next request struct */
58 struct Client *iar_client; /* client being authenticated */
59 char iar_timed; /* if non-zero, using parent i_request_timer */
64 IAUTH_BLOCKED, /* socket buffer full */
65 IAUTH_CONNECTED, /* server greeting/handshake done */
66 IAUTH_ABORT, /* abort connection asap */
67 IAUTH_ICLASS, /* tell iauth about all local users */
68 IAUTH_CLOSING, /* candidate to be disposed */
71 DECLARE_FLAGSET(IAuthFlags, IAUTH_LAST_FLAG);
74 struct IAuthRequest i_list_head; /* doubly linked list of requests */
75 struct MsgQ i_sendQ; /* messages queued to send */
76 struct Socket i_socket; /* connection to server */
77 struct Timer i_reconn_timer; /* when to reconnect the connection */
78 struct Timer i_request_timer; /* when the current request times out */
79 struct IAuthFlags i_flags; /* connection state/status/flags */
80 struct DNSQuery i_query; /* DNS lookup for iauth server */
81 unsigned int i_recvM; /* messages received */
82 unsigned int i_sendM; /* messages sent */
83 unsigned int i_recvK; /* kilobytes received */
84 unsigned int i_sendK; /* kilobytes sent */
85 unsigned short i_recvB; /* bytes received modulo 1024 */
86 unsigned short i_sendB; /* bytes sent modulo 1024 */
87 time_t i_reconnect; /* seconds to wait before reconnecting */
88 time_t i_timeout; /* seconds to wait for a request */
89 unsigned int i_count; /* characters used in i_buffer */
90 char i_buffer[BUFSIZE+1]; /* partial unprocessed line from server */
91 char i_passwd[PASSWDLEN+1]; /* password for connection */
92 char i_host[HOSTLEN+1]; /* iauth server hostname */
93 struct irc_sockaddr i_addr; /* iauth server ip address and port */
94 struct IAuth *i_next; /* next connection in list */
97 #define i_flags(iauth) ((iauth)->i_flags)
98 #define IAuthGet(iauth, flag) FlagHas(&i_flags(iauth), flag)
99 #define IAuthSet(iauth, flag) FlagSet(&i_flags(iauth), flag)
100 #define IAuthClr(iauth, flag) FlagClr(&i_flags(iauth), flag)
101 #define i_GetBlocked(iauth) IAuthGet(iauth, IAUTH_BLOCKED)
102 #define i_SetBlocked(iauth) IAuthSet(iauth, IAUTH_BLOCKED)
103 #define i_ClrBlocked(iauth) IAuthClr(iauth, IAUTH_BLOCKED)
104 #define i_GetConnected(iauth) IAuthGet(iauth, IAUTH_CONNECTED)
105 #define i_SetConnected(iauth) IAuthSet(iauth, IAUTH_CONNECTED)
106 #define i_ClrConnected(iauth) IAuthClr(iauth, IAUTH_CONNECTED)
107 #define i_GetAbort(iauth) IAuthGet(iauth, IAUTH_ABORT)
108 #define i_SetAbort(iauth) IAuthSet(iauth, IAUTH_ABORT)
109 #define i_ClrAbort(iauth) IAuthClr(iauth, IAUTH_ABORT)
110 #define i_GetIClass(iauth) IAuthGet(iauth, IAUTH_ICLASS)
111 #define i_SetIClass(iauth) IAuthSet(iauth, IAUTH_ICLASS)
112 #define i_ClrIClass(iauth) IAuthClr(iauth, IAUTH_ICLASS)
113 #define i_GetClosing(iauth) IAuthGet(iauth, IAUTH_CLOSING)
114 #define i_SetClosing(iauth) IAuthSet(iauth, IAUTH_CLOSING)
115 #define i_ClrClosing(iauth) IAuthClr(iauth, IAUTH_CLOSING)
117 #define i_list_head(iauth) ((iauth)->i_list_head)
118 #define i_socket(iauth) ((iauth)->i_socket)
119 #define i_reconn_timer(iauth) ((iauth)->i_reconn_timer)
120 #define i_request_timer(iauth) ((iauth)->i_request_timer)
121 #define i_query(iauth) ((iauth)->i_query)
122 #define i_recvB(iauth) ((iauth)->i_recvB)
123 #define i_recvK(iauth) ((iauth)->i_recvK)
124 #define i_recvM(iauth) ((iauth)->i_recvM)
125 #define i_sendB(iauth) ((iauth)->i_sendB)
126 #define i_sendK(iauth) ((iauth)->i_sendK)
127 #define i_sendM(iauth) ((iauth)->i_sendM)
128 #define i_sendQ(iauth) ((iauth)->i_sendQ)
129 #define i_reconnect(iauth) ((iauth)->i_reconnect)
130 #define i_timeout(iauth) ((iauth)->i_timeout)
131 #define i_count(iauth) ((iauth)->i_count)
132 #define i_buffer(iauth) ((iauth)->i_buffer)
133 #define i_passwd(iauth) ((iauth)->i_passwd)
134 #define i_host(iauth) ((iauth)->i_host)
135 #define i_addr(iauth) ((iauth)->i_addr)
136 #define i_port(iauth) ((iauth)->i_addr.port)
137 #define i_next(iauth) ((iauth)->i_next)
140 const char *iac_name;
141 void (*iac_func)(struct IAuth *iauth, int, char *[]);
144 struct IAuth *iauth_active;
146 static void iauth_write(struct IAuth *iauth);
147 static void iauth_reconnect(struct IAuth *iauth);
148 static void iauth_disconnect(struct IAuth *iauth);
149 static void iauth_sock_callback(struct Event *ev);
150 static void iauth_send_request(struct IAuth *iauth, struct IAuthRequest *iar);
151 static void iauth_dispose_request(struct IAuth *iauth, struct IAuthRequest *iar);
152 static void iauth_cmd_doneauth(struct IAuth *iauth, int argc, char *argv[]);
153 static void iauth_cmd_badauth(struct IAuth *iauth, int argc, char *argv[]);
155 static const struct IAuthCmd iauth_cmdtab[] = {
156 { "DoneAuth", iauth_cmd_doneauth },
157 { "BadAuth", iauth_cmd_badauth },
161 struct IAuth *iauth_connect(char *host, unsigned short port, char *passwd, time_t reconnect, time_t timeout)
165 for (iauth = iauth_active; iauth; iauth = i_next(iauth)) {
166 if (!ircd_strncmp(i_host(iauth), host, HOSTLEN)
167 && (i_port(iauth) == port)) {
169 i_reconnect(iauth) = reconnect;
170 if (t_active(&i_reconn_timer(iauth)) && (t_expire(&i_reconn_timer(iauth)) > CurrentTime + i_reconnect(iauth)))
171 timer_chg(&i_reconn_timer(iauth), TT_RELATIVE, i_reconnect(iauth));
176 if (iauth_active && !i_GetClosing(iauth_active)) {
177 log_write(LS_CONFIG, L_WARNING, 0, "Creating extra active IAuth connection to %s:%d.", host, port);
179 iauth = MyCalloc(1, sizeof(*iauth));
180 i_list_head(iauth).iar_prev = &i_list_head(iauth);
181 i_list_head(iauth).iar_next = &i_list_head(iauth);
182 msgq_init(&i_sendQ(iauth));
183 ircd_strncpy(i_host(iauth), host, HOSTLEN);
184 memset(&i_addr(iauth), 0, sizeof(i_addr(iauth)));
185 i_port(iauth) = port;
186 iauth_active = iauth;
187 i_reconnect(iauth) = reconnect;
188 iauth_reconnect(iauth);
191 ircd_strncpy(i_passwd(iauth), passwd, PASSWDLEN);
193 i_passwd(iauth)[0] = '\0';
194 i_timeout(iauth) = timeout;
199 void iauth_mark_closing(void)
202 for (iauth = iauth_active; iauth; iauth = i_next(iauth))
206 void iauth_close(struct IAuth *iauth)
208 /* Figure out what to do with the closing connection's requests. */
209 if (i_list_head(iauth).iar_next != &i_list_head(iauth)) {
210 struct IAuthRequest *iar;
211 if (iauth_active || i_next(iauth)) {
212 /* If iauth_active != NULL, send requests to it; otherwise if
213 * i_next(iauth) != NULL, we can hope it or some later
214 * connection will be active.
216 struct IAuth *target = iauth_active ? iauth_active : i_next(iauth);
218 /* Append iauth->i_list_head to end of target->i_list_head. */
219 iar = i_list_head(iauth).iar_next;
220 iar->iar_prev = i_list_head(target).iar_prev;
221 i_list_head(target).iar_prev->iar_next = iar;
222 iar = i_list_head(iauth).iar_prev;
223 iar->iar_next = &i_list_head(target);
224 i_list_head(target).iar_prev = iar;
226 /* If the target is not closing, send the requests. */
227 for (iar = i_list_head(iauth).iar_next;
228 iar != &i_list_head(target);
229 iar = iar->iar_next) {
230 if (!i_GetClosing(target))
231 iauth_send_request(target, iar);
234 /* No active connections - approve the requests and drop them. */
235 while ((iar = i_list_head(iauth).iar_next) != &i_list_head(iauth)) {
236 struct Client *client = iar->iar_client;
237 iauth_dispose_request(iauth, iar);
238 register_user(client, client, cli_name(client), cli_username(client));
242 /* Make sure the connection closes with an empty request list. */
243 i_list_head(iauth).iar_prev = &i_list_head(iauth);
244 i_list_head(iauth).iar_next = &i_list_head(iauth);
245 /* Cancel the timer, if it is active. */
246 if (t_active(&i_reconn_timer(iauth)))
247 timer_del(&i_reconn_timer(iauth));
248 if (t_active(&i_request_timer(iauth)))
249 timer_del(&i_request_timer(iauth));
250 /* Disconnect from the server. */
251 if (s_fd(&i_socket(iauth)) != -1)
252 iauth_disconnect(iauth);
257 void iauth_close_unused(void)
259 struct IAuth *prev, *iauth, *next;
261 for (prev = NULL, iauth = iauth_active; iauth; iauth = next) {
262 next = i_next(iauth);
263 if (i_GetClosing(iauth)) {
264 /* Update iauth_active linked list. */
269 /* Close and destroy the connection. */
277 static void iauth_send(struct IAuth *iauth, const char *format, ...)
282 va_start(vl, format);
283 mb = msgq_vmake(0, format, vl);
285 msgq_add(&i_sendQ(iauth), mb, 0);
289 static void iauth_protocol_violation(struct IAuth *iauth, const char *format, ...)
294 vd.vd_format = format;
295 va_start(vd.vd_args, format);
296 sendwallto_group_butone(&me, WALL_DESYNCH, NULL, "IAuth protocol violation: %v", &vd);
300 static void iauth_on_connect(struct IAuth *iauth)
302 struct IAuthRequest *iar;
303 if (EmptyString(i_passwd(iauth)))
304 iauth_send(iauth, "Server %s", cli_name(&me));
306 iauth_send(iauth, "Server %s %s", cli_name(&me), i_passwd(iauth));
307 if (i_GetIClass(iauth)) {
308 /* TODO: report local users to iauth */
309 iauth_send(iauth, "EndUsers");
311 i_SetConnected(iauth);
312 for (iar = i_list_head(iauth).iar_next;
313 iar != &i_list_head(iauth);
315 iauth_send_request(iauth, iar);
319 static void iauth_disconnect(struct IAuth *iauth)
321 close(s_fd(&i_socket(iauth)));
322 socket_del(&i_socket(iauth));
323 s_fd(&i_socket(iauth)) = -1;
326 static void iauth_dns_callback(void *vptr, struct DNSReply *he)
328 struct IAuth *iauth = vptr;
330 sendto_opmask_butone(0, SNO_OLDSNO, "IAuth connection to %s failed: host lookup failed", i_host(iauth));
332 memcpy(&i_addr(iauth).addr, &he->addr, sizeof(i_addr(iauth).addr));
333 if (!irc_in_addr_valid(&i_addr(iauth).addr)) {
334 sendto_opmask_butone(0, SNO_OLDSNO, "IAuth connection to %s failed: host came back as unresolved", i_host(iauth));
337 iauth_reconnect(iauth);
341 static void iauth_reconnect_ev(struct Event *ev)
343 if (ev_type(ev) == ET_EXPIRE)
344 iauth_reconnect(t_data(ev_timer(ev)));
347 static void iauth_schedule_reconnect(struct IAuth *iauth)
350 assert(!t_active(&i_reconn_timer(iauth)));
351 timer = timer_init(&i_reconn_timer(iauth));
352 timer_add(timer, iauth_reconnect_ev, iauth, TT_RELATIVE, i_reconnect(iauth));
355 static void iauth_reconnect(struct IAuth *iauth)
360 Debug((DEBUG_INFO, "IAuth attempt connection to %s port %p.", i_host(iauth), i_port(iauth)));
361 if (!irc_in_addr_valid(&i_addr(iauth).addr)
362 && !ircd_aton(&i_addr(iauth).addr, i_host(iauth))) {
363 i_query(iauth).vptr = iauth;
364 i_query(iauth).callback = iauth_dns_callback;
365 gethost_byname(i_host(iauth), &i_query(iauth));
368 fd = os_socket(&VirtualHost, SOCK_STREAM, "IAuth");
371 if (!os_set_sockbufs(fd, SERVER_TCP_WINDOW, SERVER_TCP_WINDOW)) {
373 sendto_opmask_butone(0, SNO_OLDSNO, "IAuth reconnect unable to set socket buffers: %s", strerror(errno));
376 result = os_connect_nonb(fd, &i_addr(iauth));
377 if (result == IO_FAILURE) {
379 sendto_opmask_butone(0, SNO_OLDSNO, "IAuth reconnect unable to initiate connection: %s", strerror(errno));
382 if (!socket_add(&i_socket(iauth), iauth_sock_callback, iauth,
383 (result == IO_SUCCESS) ? SS_CONNECTED : SS_CONNECTING,
384 SOCK_EVENT_READABLE | SOCK_EVENT_WRITABLE, fd)) {
386 sendto_opmask_butone(0, SNO_OLDSNO, "IAuth reconnect unable to add socket: %s", strerror(errno));
391 static void iauth_read(struct IAuth *iauth)
393 char *src, *endp, *old_buffer, *argv[MAXPARA + 1];
394 unsigned int length, argc, ii;
395 char readbuf[SERVER_TCP_WINDOW];
398 if (IO_FAILURE == os_recv_nonb(s_fd(&i_socket(iauth)), readbuf, sizeof(readbuf), &length))
400 i_recvB(iauth) += length;
401 if (i_recvB(iauth) > 1023) {
402 i_recvK(iauth) += i_recvB(iauth) >> 10;
403 i_recvB(iauth) &= 1023;
405 old_buffer = i_buffer(iauth);
406 endp = old_buffer + i_count(iauth);
407 for (src = readbuf; length > 0; --length) {
410 /* Skip blank lines. */
411 if (endp == old_buffer)
413 /* NUL-terminate line and split parameters. */
415 for (argc = 0, endp = old_buffer; *endp && (argc < MAXPARA); ) {
422 argv[argc++] = endp + 1;
426 for (; *endp && *endp != ' '; ++endp) ;
429 /* Count line and reset endp to start of buffer. */
432 /* Look up command and try to dispatch. */
434 for (ii = 0; iauth_cmdtab[ii].iac_name; ++ii) {
435 if (!ircd_strcmp(iauth_cmdtab[ii].iac_name, argv[0])) {
436 iauth_cmdtab[ii].iac_func(iauth, argc, argv);
437 if (i_GetAbort(iauth))
438 iauth_disconnect(iauth);
444 else if (endp < old_buffer + BUFSIZE)
447 i_count(iauth) = endp - old_buffer;
450 static void iauth_write(struct IAuth *iauth)
452 unsigned int bytes_tried, bytes_sent;
455 if (i_GetBlocked(iauth))
457 while (MsgQLength(&i_sendQ(iauth)) > 0) {
458 iores = os_sendv_nonb(s_fd(&i_socket(iauth)), &i_sendQ(iauth), &bytes_tried, &bytes_sent);
461 msgq_delete(&i_sendQ(iauth), bytes_sent);
462 i_sendB(iauth) += bytes_sent;
463 if (i_sendB(iauth) > 1023) {
464 i_sendK(iauth) += i_sendB(iauth) >> 10;
465 i_sendB(iauth) &= 1023;
467 if (bytes_tried == bytes_sent)
469 /* If bytes_sent < bytes_tried, fall through to IO_BLOCKED. */
472 socket_events(&i_socket(iauth), SOCK_ACTION_ADD | SOCK_EVENT_WRITABLE);
475 iauth_disconnect(iauth);
479 /* We were able to flush all events, so remove notification. */
480 socket_events(&i_socket(iauth), SOCK_ACTION_DEL | SOCK_EVENT_WRITABLE);
483 static void iauth_sock_callback(struct Event *ev)
487 assert(0 != ev_socket(ev));
488 iauth = (struct IAuth*) s_data(ev_socket(ev));
491 switch (ev_type(ev)) {
493 socket_state(ev_socket(ev), SS_CONNECTED);
494 iauth_on_connect(iauth);
497 if (!i_GetClosing(iauth))
498 iauth_schedule_reconnect(iauth);
508 iauth_disconnect(iauth);
511 sendto_opmask_butone(0, SNO_OLDSNO, "IAuth socket error: %s", strerror(ev_data(ev)));
512 log_write(LS_SOCKET, L_ERROR, 0, "IAuth socket error: %s", strerror(ev_data(ev)));
513 iauth_disconnect(iauth);
516 assert(0 && "Unrecognized event type");
521 /* Functions related to IAuthRequest structs */
523 static void iauth_request_ev(struct Event *ev)
525 /* TODO: this could probably be more intelligent */
526 if (ev_type(ev) == ET_EXPIRE) {
527 sendto_opmask_butone(0, SNO_OLDSNO, "IAuth request timed out; reconnecting");
528 iauth_reconnect(t_data(ev_timer(ev)));
532 static void iauth_send_request(struct IAuth *iauth, struct IAuthRequest *iar)
534 struct Client *client;
536 /* If iauth is not connected, we must defer the request. */
537 if (!i_GetConnected(iauth)) {
538 Debug((DEBUG_SEND, "IAuth deferring request for %s because we are not connected.", cli_name(iar->iar_client)));
542 /* If no timed request, set up expiration timer. */
543 if (!t_active(&i_request_timer(iauth))) {
544 struct Timer *timer = timer_init(&i_request_timer(iauth));
545 timer_add(timer, iauth_request_ev, iauth, TT_RELATIVE, i_timeout(iauth));
550 /* Send the FullAuth request. */
551 client = iar->iar_client;
552 assert(iar->iar_client != NULL);
553 iauth_send(iauth, "FullAuth %x %s %s %s %s %s :%s",
554 client, cli_name(client), cli_username(client),
555 cli_user(client)->host, cli_sock_ip(client),
556 cli_passwd(client), cli_info(client));
558 /* Write to the socket if we can. */
562 int iauth_start_client(struct IAuth *iauth, struct Client *cptr)
564 struct IAuthRequest *iar;
566 /* Allocate and initialize IAuthRequest struct. */
567 if (!(iar = MyCalloc(1, sizeof(*iar))))
568 return exit_client(cptr, cptr, &me, "IAuth memory allocation failed");
569 iar->iar_next = &i_list_head(iauth);
570 iar->iar_prev = i_list_head(iauth).iar_prev;
571 iar->iar_client = cptr;
572 iar->iar_prev->iar_next = iar;
573 iar->iar_next->iar_prev = iar;
576 iauth_send_request(iauth, iar);
581 void iauth_exit_client(struct Client *cptr)
583 if (cli_iauth(cptr)) {
584 iauth_dispose_request(iauth_active, cli_iauth(cptr));
585 cli_iauth(cptr) = NULL;
586 } else if (IsIAuthed(cptr) && i_GetIClass(iauth_active)) {
587 /* TODO: report quit to iauth */
591 static struct IAuthRequest *iauth_find_request(struct IAuth *iauth, char *id)
593 struct IAuthRequest *curr;
594 struct Client *target;
595 target = (struct Client*)strtoul(id, NULL, 16);
596 for (curr = i_list_head(iauth).iar_next;
597 curr != &i_list_head(iauth);
598 curr = curr->iar_next) {
599 assert(curr->iar_client != NULL);
600 if (target == curr->iar_client)
606 static void iauth_dispose_request(struct IAuth *iauth, struct IAuthRequest *iar)
608 assert(iar->iar_client != NULL);
610 timer_del(&i_request_timer(iauth));
611 cli_iauth(iar->iar_client) = NULL;
612 iar->iar_prev->iar_next = iar->iar_next;
613 iar->iar_next->iar_prev = iar->iar_prev;
617 static void iauth_cmd_doneauth(struct IAuth *iauth, int argc, char *argv[])
619 struct IAuthRequest *iar;
620 struct Client *client;
628 iauth_protocol_violation(iauth, "Only %d parameters for DoneAuth (expected >=5)", argc);
635 account = (argc > 5) ? argv[5] : 0;
636 iar = iauth_find_request(iauth, id);
638 iauth_protocol_violation(iauth, "Got unexpected DoneAuth for id %s", id);
641 client = iar->iar_client;
642 ircd_strncpy(cli_username(client), username, USERLEN);
643 ircd_strncpy(cli_user(client)->host, hostname, HOSTLEN);
645 ircd_strncpy(cli_user(client)->account, account, ACCOUNTLEN);
649 iauth_dispose_request(iauth, iar);
650 register_user(client, client, cli_name(client), username);
653 static void iauth_cmd_badauth(struct IAuth *iauth, int argc, char *argv[])
655 struct IAuthRequest *iar;
656 struct Client *client;
661 iauth_protocol_violation(iauth, "Only %d parameters for BadAuth (expected >=3)", argc);
666 if (EmptyString(reason)) {
667 iauth_protocol_violation(iauth, "Empty BadAuth reason for id %s", id);
670 iar = iauth_find_request(iauth, id);
672 iauth_protocol_violation(iauth, "Got unexpected BadAuth for id %s", id);
675 client = iar->iar_client;
676 iauth_dispose_request(iauth, iar);
677 exit_client(client, client, &me, reason);