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"
47 #include <sys/socket.h>
48 #include <netinet/in.h>
49 #include <arpa/inet.h>
55 struct IAuthRequest *iar_prev; /* previous request struct */
56 struct IAuthRequest *iar_next; /* next request struct */
57 struct Client *iar_client; /* client being authenticated */
58 char iar_timed; /* if non-zero, using parent i_request_timer */
63 IAUTH_BLOCKED, /* socket buffer full */
64 IAUTH_CONNECTED, /* server greeting/handshake done */
65 IAUTH_ABORT, /* abort connection asap */
66 IAUTH_ICLASS, /* tell iauth about all local users */
67 IAUTH_CLOSING, /* candidate to be disposed */
70 DECLARE_FLAGSET(IAuthFlags, IAUTH_LAST_FLAG);
73 struct IAuthRequest i_list_head; /* doubly linked list of requests */
74 struct MsgQ i_sendQ; /* messages queued to send */
75 struct Socket i_socket; /* connection to server */
76 struct Timer i_reconn_timer; /* when to reconnect the connection */
77 struct Timer i_request_timer; /* when the current request times out */
78 struct IAuthFlags i_flags; /* connection state/status/flags */
79 struct DNSQuery i_query; /* DNS lookup for iauth server */
80 unsigned int i_recvM; /* messages received */
81 unsigned int i_sendM; /* messages sent */
82 unsigned int i_recvK; /* kilobytes received */
83 unsigned int i_sendK; /* kilobytes sent */
84 unsigned short i_recvB; /* bytes received modulo 1024 */
85 unsigned short i_sendB; /* bytes sent modulo 1024 */
86 time_t i_reconnect; /* seconds to wait before reconnecting */
87 time_t i_timeout; /* seconds to wait for a request */
88 unsigned int i_count; /* characters used in i_buffer */
89 char i_buffer[BUFSIZE+1]; /* partial unprocessed line from server */
90 char i_passwd[PASSWDLEN+1]; /* password for connection */
91 char i_host[HOSTLEN+1]; /* iauth server hostname */
92 struct irc_sockaddr i_addr; /* iauth server ip address and port */
93 struct IAuth *i_next; /* next connection in list */
96 #define i_flags(iauth) ((iauth)->i_flags)
97 #define IAuthGet(iauth, flag) FlagHas(&i_flags(iauth), flag)
98 #define IAuthSet(iauth, flag) FlagSet(&i_flags(iauth), flag)
99 #define IAuthClr(iauth, flag) FlagClr(&i_flags(iauth), flag)
100 #define i_GetBlocked(iauth) IAuthGet(iauth, IAUTH_BLOCKED)
101 #define i_SetBlocked(iauth) IAuthSet(iauth, IAUTH_BLOCKED)
102 #define i_ClrBlocked(iauth) IAuthClr(iauth, IAUTH_BLOCKED)
103 #define i_GetConnected(iauth) IAuthGet(iauth, IAUTH_CONNECTED)
104 #define i_SetConnected(iauth) IAuthSet(iauth, IAUTH_CONNECTED)
105 #define i_ClrConnected(iauth) IAuthClr(iauth, IAUTH_CONNECTED)
106 #define i_GetAbort(iauth) IAuthGet(iauth, IAUTH_ABORT)
107 #define i_SetAbort(iauth) IAuthSet(iauth, IAUTH_ABORT)
108 #define i_ClrAbort(iauth) IAuthClr(iauth, IAUTH_ABORT)
109 #define i_GetIClass(iauth) IAuthGet(iauth, IAUTH_ICLASS)
110 #define i_SetIClass(iauth) IAuthSet(iauth, IAUTH_ICLASS)
111 #define i_ClrIClass(iauth) IAuthClr(iauth, IAUTH_ICLASS)
112 #define i_GetClosing(iauth) IAuthGet(iauth, IAUTH_CLOSING)
113 #define i_SetClosing(iauth) IAuthSet(iauth, IAUTH_CLOSING)
114 #define i_ClrClosing(iauth) IAuthClr(iauth, IAUTH_CLOSING)
116 #define i_list_head(iauth) ((iauth)->i_list_head)
117 #define i_socket(iauth) ((iauth)->i_socket)
118 #define i_reconn_timer(iauth) ((iauth)->i_reconn_timer)
119 #define i_request_timer(iauth) ((iauth)->i_request_timer)
120 #define i_query(iauth) ((iauth)->i_query)
121 #define i_recvB(iauth) ((iauth)->i_recvB)
122 #define i_recvK(iauth) ((iauth)->i_recvK)
123 #define i_recvM(iauth) ((iauth)->i_recvM)
124 #define i_sendB(iauth) ((iauth)->i_sendB)
125 #define i_sendK(iauth) ((iauth)->i_sendK)
126 #define i_sendM(iauth) ((iauth)->i_sendM)
127 #define i_sendQ(iauth) ((iauth)->i_sendQ)
128 #define i_reconnect(iauth) ((iauth)->i_reconnect)
129 #define i_timeout(iauth) ((iauth)->i_timeout)
130 #define i_count(iauth) ((iauth)->i_count)
131 #define i_buffer(iauth) ((iauth)->i_buffer)
132 #define i_passwd(iauth) ((iauth)->i_passwd)
133 #define i_host(iauth) ((iauth)->i_host)
134 #define i_addr(iauth) ((iauth)->i_addr)
135 #define i_port(iauth) ((iauth)->i_addr.port)
136 #define i_next(iauth) ((iauth)->i_next)
139 const char *iac_name;
140 void (*iac_func)(struct IAuth *iauth, int, char *[]);
143 struct IAuth *iauth_active;
145 static void iauth_write(struct IAuth *iauth);
146 static void iauth_reconnect(struct IAuth *iauth);
147 static void iauth_disconnect(struct IAuth *iauth);
148 static void iauth_sock_callback(struct Event *ev);
149 static void iauth_send_request(struct IAuth *iauth, struct IAuthRequest *iar);
150 static void iauth_dispose_request(struct IAuth *iauth, struct IAuthRequest *iar);
151 static void iauth_cmd_doneauth(struct IAuth *iauth, int argc, char *argv[]);
152 static void iauth_cmd_badauth(struct IAuth *iauth, int argc, char *argv[]);
154 static const struct IAuthCmd iauth_cmdtab[] = {
155 { "DoneAuth", iauth_cmd_doneauth },
156 { "BadAuth", iauth_cmd_badauth },
160 struct IAuth *iauth_connect(char *host, unsigned short port, char *passwd, time_t reconnect, time_t timeout)
164 for (iauth = iauth_active; iauth; iauth = i_next(iauth)) {
165 if (!ircd_strncmp(i_host(iauth), host, HOSTLEN)
166 && (i_port(iauth) == port)) {
168 i_reconnect(iauth) = reconnect;
169 if (t_active(&i_reconn_timer(iauth)) && (t_expire(&i_reconn_timer(iauth)) > CurrentTime + i_reconnect(iauth)))
170 timer_chg(&i_reconn_timer(iauth), TT_RELATIVE, i_reconnect(iauth));
175 if (iauth_active && !i_GetClosing(iauth_active)) {
176 log_write(LS_CONFIG, L_WARNING, 0, "Creating extra active IAuth connection to %s:%d.", host, port);
178 iauth = MyCalloc(1, sizeof(*iauth));
179 i_list_head(iauth).iar_prev = &i_list_head(iauth);
180 i_list_head(iauth).iar_next = &i_list_head(iauth);
181 msgq_init(&i_sendQ(iauth));
182 ircd_strncpy(i_host(iauth), host, HOSTLEN);
183 i_port(iauth) = port;
184 memset(&i_addr(iauth), 0, sizeof(i_addr(iauth)));
185 iauth_active = iauth;
186 i_reconnect(iauth) = reconnect;
187 iauth_reconnect(iauth);
190 ircd_strncpy(i_passwd(iauth), passwd, PASSWDLEN);
192 i_passwd(iauth)[0] = '\0';
193 i_timeout(iauth) = timeout;
197 void iauth_mark_closing(void)
200 for (iauth = iauth_active; iauth; iauth = i_next(iauth))
204 void iauth_close(struct IAuth *iauth)
206 /* Figure out what to do with the closing connection's requests. */
207 if (i_list_head(iauth).iar_next != &i_list_head(iauth)) {
208 struct IAuthRequest *iar;
209 if (iauth_active || i_next(iauth)) {
210 /* If iauth_active != NULL, send requests to it; otherwise if
211 * i_next(iauth) != NULL, we can hope it or some later
212 * connection will be active.
214 struct IAuth *target = iauth_active ? iauth_active : i_next(iauth);
216 /* Append iauth->i_list_head to end of target->i_list_head. */
217 iar = i_list_head(iauth).iar_next;
218 iar->iar_prev = i_list_head(target).iar_prev;
219 i_list_head(target).iar_prev->iar_next = iar;
220 iar = i_list_head(iauth).iar_prev;
221 iar->iar_next = &i_list_head(target);
222 i_list_head(target).iar_prev = iar;
224 /* If the target is not closing, send the requests. */
225 for (iar = i_list_head(iauth).iar_next;
226 iar != &i_list_head(target);
227 iar = iar->iar_next) {
228 if (!i_GetClosing(target))
229 iauth_send_request(target, iar);
232 /* No active connections - approve the requests and drop them. */
233 while ((iar = i_list_head(iauth).iar_next) != &i_list_head(iauth)) {
234 struct Client *client = iar->iar_client;
235 iauth_dispose_request(iauth, iar);
236 register_user(client, client, cli_name(client), cli_username(client));
240 /* Make sure the connection closes with an empty request list. */
241 i_list_head(iauth).iar_prev = &i_list_head(iauth);
242 i_list_head(iauth).iar_next = &i_list_head(iauth);
243 /* Cancel the timer, if it is active. */
244 if (t_active(&i_reconn_timer(iauth)))
245 timer_del(&i_reconn_timer(iauth));
246 if (t_active(&i_request_timer(iauth)))
247 timer_del(&i_request_timer(iauth));
248 /* Disconnect from the server. */
249 if (s_fd(&i_socket(iauth)) != -1)
250 iauth_disconnect(iauth);
255 void iauth_close_unused(void)
257 struct IAuth *prev, *iauth, *next;
259 for (prev = NULL, iauth = iauth_active; iauth; iauth = next) {
260 next = i_next(iauth);
261 if (i_GetClosing(iauth)) {
262 /* Update iauth_active linked list. */
267 /* Close and destroy the connection. */
275 static void iauth_send(struct IAuth *iauth, const char *format, ...)
280 va_start(vl, format);
281 mb = msgq_vmake(0, format, vl);
283 msgq_add(&i_sendQ(iauth), mb, 0);
287 static void iauth_protocol_violation(struct IAuth *iauth, const char *format, ...)
292 vd.vd_format = format;
293 va_start(vd.vd_args, format);
294 sendwallto_group_butone(&me, WALL_DESYNCH, NULL, "IAuth protocol violation: %v", &vd);
298 static void iauth_on_connect(struct IAuth *iauth)
300 struct IAuthRequest *iar;
301 if (EmptyString(i_passwd(iauth)))
302 iauth_send(iauth, "Server %s", cli_name(&me));
304 iauth_send(iauth, "Server %s %s", cli_name(&me), i_passwd(iauth));
305 if (i_GetIClass(iauth)) {
306 /* TODO: report local users to iauth */
307 iauth_send(iauth, "EndUsers");
309 i_SetConnected(iauth);
310 for (iar = i_list_head(iauth).iar_next;
311 iar != &i_list_head(iauth);
313 iauth_send_request(iauth, iar);
317 static void iauth_disconnect(struct IAuth *iauth)
319 close(s_fd(&i_socket(iauth)));
320 socket_del(&i_socket(iauth));
321 s_fd(&i_socket(iauth)) = -1;
324 static void iauth_dns_callback(void *vptr, struct DNSReply *he)
326 struct IAuth *iauth = vptr;
328 sendto_opmask_butone(0, SNO_OLDSNO, "IAuth connection to %s failed: host lookup failed", i_host(iauth));
330 memcpy(&i_addr(iauth).addr, &he->addr, sizeof(i_addr(iauth).addr));
331 if (!irc_in_addr_valid(&i_addr(iauth).addr)) {
332 sendto_opmask_butone(0, SNO_OLDSNO, "IAuth connection to %s failed: host came back as unresolved", i_host(iauth));
335 iauth_reconnect(iauth);
339 static void iauth_reconnect_ev(struct Event *ev)
341 if (ev_type(ev) == ET_EXPIRE)
342 iauth_reconnect(t_data(ev_timer(ev)));
345 static void iauth_schedule_reconnect(struct IAuth *iauth)
348 assert(!t_active(&i_reconn_timer(iauth)));
349 timer = timer_init(&i_reconn_timer(iauth));
350 timer_add(timer, iauth_reconnect_ev, iauth, TT_RELATIVE, i_reconnect(iauth));
353 static void iauth_reconnect(struct IAuth *iauth)
358 if (!irc_in_addr_valid(&i_addr(iauth).addr)
359 && !ircd_aton(&i_addr(iauth).addr, i_host(iauth))) {
360 i_query(iauth).vptr = iauth;
361 i_query(iauth).callback = iauth_dns_callback;
362 gethost_byname(i_host(iauth), &i_query(iauth));
365 fd = os_socket(&VirtualHost, SOCK_STREAM, "IAuth");
368 if (!os_set_sockbufs(fd, SERVER_TCP_WINDOW, SERVER_TCP_WINDOW)) {
370 sendto_opmask_butone(0, SNO_OLDSNO, "IAuth reconnect unable to set socket buffers: %s", strerror(errno));
373 result = os_connect_nonb(fd, &i_addr(iauth));
374 if (result == IO_FAILURE) {
376 sendto_opmask_butone(0, SNO_OLDSNO, "IAuth reconnect unable to initiate connection: %s", strerror(errno));
379 if (!socket_add(&i_socket(iauth), iauth_sock_callback, iauth,
380 (result == IO_SUCCESS) ? SS_CONNECTED : SS_CONNECTING,
381 SOCK_EVENT_READABLE | SOCK_EVENT_WRITABLE, fd)) {
383 sendto_opmask_butone(0, SNO_OLDSNO, "IAuth reconnect unable to add socket: %s", strerror(errno));
388 static void iauth_read(struct IAuth *iauth)
390 char *src, *endp, *old_buffer, *argv[MAXPARA + 1];
391 unsigned int length, argc, ii;
392 char readbuf[SERVER_TCP_WINDOW];
395 if (IO_FAILURE == os_recv_nonb(s_fd(&i_socket(iauth)), readbuf, sizeof(readbuf), &length))
397 i_recvB(iauth) += length;
398 if (i_recvB(iauth) > 1023) {
399 i_recvK(iauth) += i_recvB(iauth) >> 10;
400 i_recvB(iauth) &= 1023;
402 old_buffer = i_buffer(iauth);
403 endp = old_buffer + i_count(iauth);
404 for (src = readbuf; length > 0; --length) {
407 /* Skip blank lines. */
408 if (endp == old_buffer)
410 /* NUL-terminate line and split parameters. */
412 for (argc = 0, endp = old_buffer; *endp && (argc < MAXPARA); ) {
419 argv[argc++] = endp + 1;
423 for (; *endp && *endp != ' '; ++endp) ;
426 /* Count line and reset endp to start of buffer. */
429 /* Look up command and try to dispatch. */
431 for (ii = 0; iauth_cmdtab[ii].iac_name; ++ii) {
432 if (!ircd_strcmp(iauth_cmdtab[ii].iac_name, argv[0])) {
433 iauth_cmdtab[ii].iac_func(iauth, argc, argv);
434 if (i_GetAbort(iauth))
435 iauth_disconnect(iauth);
441 else if (endp < old_buffer + BUFSIZE)
444 i_count(iauth) = endp - old_buffer;
447 static void iauth_write(struct IAuth *iauth)
449 unsigned int bytes_tried, bytes_sent;
452 if (i_GetBlocked(iauth))
454 while (MsgQLength(&i_sendQ(iauth)) > 0) {
455 iores = os_sendv_nonb(s_fd(&i_socket(iauth)), &i_sendQ(iauth), &bytes_tried, &bytes_sent);
458 msgq_delete(&i_sendQ(iauth), bytes_sent);
459 i_sendB(iauth) += bytes_sent;
460 if (i_sendB(iauth) > 1023) {
461 i_sendK(iauth) += i_sendB(iauth) >> 10;
462 i_sendB(iauth) &= 1023;
464 if (bytes_tried == bytes_sent)
466 /* If bytes_sent < bytes_tried, fall through to IO_BLOCKED. */
469 socket_events(&i_socket(iauth), SOCK_ACTION_ADD | SOCK_EVENT_WRITABLE);
472 iauth_disconnect(iauth);
476 /* We were able to flush all events, so remove notification. */
477 socket_events(&i_socket(iauth), SOCK_ACTION_DEL | SOCK_EVENT_WRITABLE);
480 static void iauth_sock_callback(struct Event *ev)
484 assert(0 != ev_socket(ev));
485 iauth = (struct IAuth*) s_data(ev_socket(ev));
488 switch (ev_type(ev)) {
490 socket_state(ev_socket(ev), SS_CONNECTED);
491 iauth_on_connect(iauth);
494 if (!i_GetClosing(iauth))
495 iauth_schedule_reconnect(iauth);
505 iauth_disconnect(iauth);
508 sendto_opmask_butone(0, SNO_OLDSNO, "IAuth socket error: %s", strerror(ev_data(ev)));
509 log_write(LS_SOCKET, L_ERROR, 0, "IAuth socket error: %s", strerror(ev_data(ev)));
510 iauth_disconnect(iauth);
513 assert(0 && "Unrecognized event type");
518 /* Functions related to IAuthRequest structs */
520 static void iauth_request_ev(struct Event *ev)
522 /* TODO: this could probably be more intelligent */
523 if (ev_type(ev) == ET_EXPIRE) {
524 sendto_opmask_butone(0, SNO_OLDSNO, "IAuth request timed out; reconnecting");
525 iauth_reconnect(t_data(ev_timer(ev)));
529 static void iauth_send_request(struct IAuth *iauth, struct IAuthRequest *iar)
531 struct Client *client;
533 /* If iauth is not connected, we must defer the request. */
534 if (!i_GetConnected(iauth))
537 /* If no timed request, set up expiration timer. */
538 if (!t_active(&i_request_timer(iauth))) {
539 struct Timer *timer = timer_init(&i_request_timer(iauth));
540 timer_add(timer, iauth_request_ev, iauth, TT_RELATIVE, i_timeout(iauth));
545 /* Send the FullAuth request. */
546 client = iar->iar_client;
547 assert(iar->iar_client != NULL);
548 iauth_send(iauth, "FullAuth %x %s %s %s %s %s :%s",
549 client, cli_name(client), cli_username(client),
550 cli_user(client)->host, cli_sock_ip(client),
551 cli_passwd(client), cli_info(client));
553 /* Write to the socket if we can. */
557 int iauth_start_client(struct IAuth *iauth, struct Client *cptr)
559 struct IAuthRequest *iar;
561 /* Allocate and initialize IAuthRequest struct. */
562 if (!(iar = MyCalloc(1, sizeof(*iar))))
563 return exit_client(cptr, cptr, &me, "IAuth memory allocation failed");
564 iar->iar_next = &i_list_head(iauth);
565 iar->iar_prev = i_list_head(iauth).iar_prev;
566 iar->iar_client = cptr;
567 iar->iar_prev->iar_next = iar;
568 iar->iar_next->iar_prev = iar;
571 iauth_send_request(iauth, iar);
576 void iauth_exit_client(struct Client *cptr)
578 if (cli_iauth(cptr)) {
579 iauth_dispose_request(iauth_active, cli_iauth(cptr));
580 cli_iauth(cptr) = NULL;
581 } else if (IsIAuthed(cptr) && i_GetIClass(iauth_active)) {
582 /* TODO: report quit to iauth */
586 static struct IAuthRequest *iauth_find_request(struct IAuth *iauth, char *id)
588 struct IAuthRequest *curr;
589 struct Client *target;
590 target = (struct Client*)strtoul(id, NULL, 16);
591 for (curr = i_list_head(iauth).iar_next;
592 curr != &i_list_head(iauth);
593 curr = curr->iar_next) {
594 assert(curr->iar_client != NULL);
595 if (target == curr->iar_client)
601 static void iauth_dispose_request(struct IAuth *iauth, struct IAuthRequest *iar)
603 assert(iar->iar_client != NULL);
605 timer_del(&i_request_timer(iauth));
606 cli_iauth(iar->iar_client) = NULL;
607 iar->iar_prev->iar_next = iar->iar_next;
608 iar->iar_next->iar_prev = iar->iar_prev;
612 static void iauth_cmd_doneauth(struct IAuth *iauth, int argc, char *argv[])
614 struct IAuthRequest *iar;
615 struct Client *client;
623 iauth_protocol_violation(iauth, "Only %d parameters for DoneAuth (expected >=5)", argc);
630 account = (argc > 5) ? argv[5] : 0;
631 iar = iauth_find_request(iauth, id);
633 iauth_protocol_violation(iauth, "Got unexpected DoneAuth for id %s", id);
636 client = iar->iar_client;
637 ircd_strncpy(cli_username(client), username, USERLEN);
638 ircd_strncpy(cli_user(client)->host, hostname, HOSTLEN);
640 ircd_strncpy(cli_user(client)->account, account, ACCOUNTLEN);
644 iauth_dispose_request(iauth, iar);
645 register_user(client, client, cli_name(client), username);
648 static void iauth_cmd_badauth(struct IAuth *iauth, int argc, char *argv[])
650 struct IAuthRequest *iar;
651 struct Client *client;
656 iauth_protocol_violation(iauth, "Only %d parameters for BadAuth (expected >=3)", argc);
661 if (EmptyString(reason)) {
662 iauth_protocol_violation(iauth, "Empty BadAuth reason for id %s", id);
665 iar = iauth_find_request(iauth, id);
667 iauth_protocol_violation(iauth, "Got unexpected BadAuth for id %s", id);
670 client = iar->iar_client;
671 iauth_dispose_request(iauth, iar);
672 exit_client(client, client, &me, reason);