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
21 * @brief IAuth client implementation for an IRC server.
27 #include "ircd_alloc.h"
28 #include "ircd_auth.h"
29 #include "ircd_events.h"
30 #include "ircd_features.h"
32 #include "ircd_osdep.h"
33 #include "ircd_snprintf.h"
34 #include "ircd_string.h"
45 /* #include <assert.h> -- Now using assert in ircd_log.h */
50 #include <sys/socket.h>
51 #include <netinet/in.h>
56 /** Describes state of a single pending IAuth request. */
58 struct IAuthRequest *iar_prev; /**< previous request struct */
59 struct IAuthRequest *iar_next; /**< next request struct */
60 struct Client *iar_client; /**< client being authenticated */
61 char iar_timed; /**< if non-zero, using parent i_request_timer */
64 /** Enumeration of IAuth connection flags. */
67 IAUTH_BLOCKED, /**< socket buffer full */
68 IAUTH_CONNECTED, /**< server greeting/handshake done */
69 IAUTH_ABORT, /**< abort connection asap */
70 IAUTH_ICLASS, /**< tell iauth about all local users */
71 IAUTH_CLOSING, /**< candidate to be disposed */
72 IAUTH_LAST_FLAG /**< total number of flags */
74 /** Declare a bitset structure indexed by IAuthFlag. */
75 DECLARE_FLAGSET(IAuthFlags, IAUTH_LAST_FLAG);
77 /** Describes state of an IAuth connection. */
79 struct IAuthRequest i_list_head; /**< doubly linked list of requests */
80 struct MsgQ i_sendQ; /**< messages queued to send */
81 struct Socket i_socket; /**< connection to server */
82 struct Timer i_reconn_timer; /**< when to reconnect the connection */
83 struct Timer i_request_timer; /**< when the current request times out */
84 struct IAuthFlags i_flags; /**< connection state/status/flags */
85 struct DNSQuery i_query; /**< DNS lookup for iauth server */
86 unsigned int i_recvM; /**< messages received */
87 unsigned int i_sendM; /**< messages sent */
88 unsigned int i_recvK; /**< kilobytes received */
89 unsigned int i_sendK; /**< kilobytes sent */
90 unsigned short i_recvB; /**< bytes received modulo 1024 */
91 unsigned short i_sendB; /**< bytes sent modulo 1024 */
92 time_t i_reconnect; /**< seconds to wait before reconnecting */
93 time_t i_timeout; /**< seconds to wait for a request */
94 unsigned int i_count; /**< characters used in i_buffer */
95 char i_buffer[BUFSIZE+1]; /**< partial unprocessed line from server */
96 char i_passwd[PASSWDLEN+1]; /**< password for connection */
97 char i_host[HOSTLEN+1]; /**< iauth server hostname */
98 struct irc_sockaddr i_addr; /**< iauth server ip address and port */
99 struct IAuth *i_next; /**< next connection in list */
102 /** Return flags element of \a iauth. */
103 #define i_flags(iauth) ((iauth)->i_flags)
104 /** Return whether flag \a flag is set on \a iauth. */
105 #define IAuthGet(iauth, flag) FlagHas(&i_flags(iauth), flag)
106 /** Set flag \a flag on \a iauth. */
107 #define IAuthSet(iauth, flag) FlagSet(&i_flags(iauth), flag)
108 /** Clear flag \a flag from \a iauth. */
109 #define IAuthClr(iauth, flag) FlagClr(&i_flags(iauth), flag)
110 /** Get blocked state for \a iauth. */
111 #define i_GetBlocked(iauth) IAuthGet(iauth, IAUTH_BLOCKED)
112 /** Set blocked state for \a iauth. */
113 #define i_SetBlocked(iauth) IAuthSet(iauth, IAUTH_BLOCKED)
114 /** Clear blocked state for \a iauth. */
115 #define i_ClrBlocked(iauth) IAuthClr(iauth, IAUTH_BLOCKED)
116 /** Get connected flag for \a iauth. */
117 #define i_GetConnected(iauth) IAuthGet(iauth, IAUTH_CONNECTED)
118 /** Set connected flag for \a iauth. */
119 #define i_SetConnected(iauth) IAuthSet(iauth, IAUTH_CONNECTED)
120 /** Clear connected flag for \a iauth. */
121 #define i_ClrConnected(iauth) IAuthClr(iauth, IAUTH_CONNECTED)
122 /** Get abort flag for \a iauth. */
123 #define i_GetAbort(iauth) IAuthGet(iauth, IAUTH_ABORT)
124 /** Set abort flag for \a iauth. */
125 #define i_SetAbort(iauth) IAuthSet(iauth, IAUTH_ABORT)
126 /** Clear abort flag for \a iauth. */
127 #define i_ClrAbort(iauth) IAuthClr(iauth, IAUTH_ABORT)
128 /** Get IClass flag for \a iauth. */
129 #define i_GetIClass(iauth) IAuthGet(iauth, IAUTH_ICLASS)
130 /** Set IClass flag for \a iauth. */
131 #define i_SetIClass(iauth) IAuthSet(iauth, IAUTH_ICLASS)
132 /** Clear IClass flag for \a iauth. */
133 #define i_ClrIClass(iauth) IAuthClr(iauth, IAUTH_ICLASS)
134 /** Get closing flag for \a iauth. */
135 #define i_GetClosing(iauth) IAuthGet(iauth, IAUTH_CLOSING)
136 /** Set closing flag for \a iauth. */
137 #define i_SetClosing(iauth) IAuthSet(iauth, IAUTH_CLOSING)
138 /** Clear closing flag for \a iauth. */
139 #define i_ClrClosing(iauth) IAuthClr(iauth, IAUTH_CLOSING)
141 /** Return head of request linked list for \a iauth. */
142 #define i_list_head(iauth) ((iauth)->i_list_head)
143 /** Return socket event generator for \a iauth. */
144 #define i_socket(iauth) ((iauth)->i_socket)
145 /** Return reconnect timer for \a iauth. */
146 #define i_reconn_timer(iauth) ((iauth)->i_reconn_timer)
147 /** Return request timeout timer for \a iauth. */
148 #define i_request_timer(iauth) ((iauth)->i_request_timer)
149 /** Return DNS query for \a iauth. */
150 #define i_query(iauth) ((iauth)->i_query)
151 /** Return received bytes (modulo 1024) for \a iauth. */
152 #define i_recvB(iauth) ((iauth)->i_recvB)
153 /** Return received kilobytes (modulo 1024) for \a iauth. */
154 #define i_recvK(iauth) ((iauth)->i_recvK)
155 /** Return received megabytes for \a iauth. */
156 #define i_recvM(iauth) ((iauth)->i_recvM)
157 /** Return sent bytes (modulo 1024) for \a iauth. */
158 #define i_sendB(iauth) ((iauth)->i_sendB)
159 /** Return sent kilobytes (modulo 1024) for \a iauth. */
160 #define i_sendK(iauth) ((iauth)->i_sendK)
161 /** Return sent megabytes for \a iauth. */
162 #define i_sendM(iauth) ((iauth)->i_sendM)
163 /** Return outbound message queue for \a iauth. */
164 #define i_sendQ(iauth) ((iauth)->i_sendQ)
165 /** Return reconnection interval for \a iauth. */
166 #define i_reconnect(iauth) ((iauth)->i_reconnect)
167 /** Return request timeout interval for \a iauth. */
168 #define i_timeout(iauth) ((iauth)->i_timeout)
169 /** Return length of unprocessed message data for \a iauth. */
170 #define i_count(iauth) ((iauth)->i_count)
171 /** Return start of unprocessed message data for \a iauth. */
172 #define i_buffer(iauth) ((iauth)->i_buffer)
173 /** Return password we send for \a iauth. */
174 #define i_passwd(iauth) ((iauth)->i_passwd)
175 /** Return server hostname for \a iauth. */
176 #define i_host(iauth) ((iauth)->i_host)
177 /** Return address of IAuth server for \a iauth. */
178 #define i_addr(iauth) ((iauth)->i_addr)
179 /** Return server port for \a iauth. */
180 #define i_port(iauth) ((iauth)->i_addr.port)
181 /** Return next IAuth connection after \a iauth. */
182 #define i_next(iauth) ((iauth)->i_next)
184 /** Command table entry. */
186 const char *iac_name; /**< Name of command. */
187 void (*iac_func)(struct IAuth *iauth, int, char *[]); /**< Handler function. */
190 /** Active %IAuth connection(s). */
191 struct IAuth *iauth_active;
193 static void iauth_write(struct IAuth *iauth);
194 static void iauth_reconnect(struct IAuth *iauth);
195 static void iauth_disconnect(struct IAuth *iauth);
196 static void iauth_sock_callback(struct Event *ev);
197 static void iauth_send_request(struct IAuth *iauth, struct IAuthRequest *iar);
198 static void iauth_dispose_request(struct IAuth *iauth, struct IAuthRequest *iar);
199 static void iauth_cmd_doneauth(struct IAuth *iauth, int argc, char *argv[]);
200 static void iauth_cmd_badauth(struct IAuth *iauth, int argc, char *argv[]);
202 /** Table of responses we might get from the IAuth server. */
203 static const struct IAuthCmd iauth_cmdtab[] = {
204 { "DoneAuth", iauth_cmd_doneauth },
205 { "BadAuth", iauth_cmd_badauth },
209 /** Start (or update) a connection to an %IAuth server.
210 * If a connection already exists for the specified server name and
211 * port, update it with the other parameters; otherwise allocate a new
213 * @param[in] host %IAuth server hostname.
214 * @param[in] port %IAuth server port.
215 * @param[in] passwd Password to send.
216 * @param[in] reconnect Reconnect interval.
217 * @param[in] timeout Request timeout interval.
218 * @return IAuth structure for that connection.
220 struct IAuth *iauth_connect(char *host, unsigned short port, char *passwd, time_t reconnect, time_t timeout)
224 for (iauth = iauth_active; iauth; iauth = i_next(iauth)) {
225 if (!ircd_strncmp(i_host(iauth), host, HOSTLEN)
226 && (i_port(iauth) == port)) {
228 i_reconnect(iauth) = reconnect;
229 if (t_active(&i_reconn_timer(iauth)) && (t_expire(&i_reconn_timer(iauth)) > CurrentTime + i_reconnect(iauth)))
230 timer_chg(&i_reconn_timer(iauth), TT_RELATIVE, i_reconnect(iauth));
235 if (iauth_active && !i_GetClosing(iauth_active)) {
236 log_write(LS_CONFIG, L_WARNING, 0, "Creating extra active IAuth connection to %s:%d.", host, port);
238 iauth = MyCalloc(1, sizeof(*iauth));
239 i_list_head(iauth).iar_prev = &i_list_head(iauth);
240 i_list_head(iauth).iar_next = &i_list_head(iauth);
241 msgq_init(&i_sendQ(iauth));
242 ircd_strncpy(i_host(iauth), host, HOSTLEN);
243 memset(&i_addr(iauth), 0, sizeof(i_addr(iauth)));
244 i_port(iauth) = port;
245 iauth_active = iauth;
246 i_reconnect(iauth) = reconnect;
247 iauth_reconnect(iauth);
250 ircd_strncpy(i_passwd(iauth), passwd, PASSWDLEN);
252 i_passwd(iauth)[0] = '\0';
253 i_timeout(iauth) = timeout;
258 /** Mark all %IAuth connections as closing. */
259 void iauth_mark_closing(void)
262 for (iauth = iauth_active; iauth; iauth = i_next(iauth))
266 /** Close a particular %IAuth connection.
267 * @param[in] iauth %Connection to close.
269 void iauth_close(struct IAuth *iauth)
271 /* Figure out what to do with the closing connection's requests. */
272 if (i_list_head(iauth).iar_next != &i_list_head(iauth)) {
273 struct IAuthRequest *iar;
274 if (iauth_active || i_next(iauth)) {
275 /* If iauth_active != NULL, send requests to it; otherwise if
276 * i_next(iauth) != NULL, we can hope it or some later
277 * connection will be active.
279 struct IAuth *target = iauth_active ? iauth_active : i_next(iauth);
281 /* Append iauth->i_list_head to end of target->i_list_head. */
282 iar = i_list_head(iauth).iar_next;
283 iar->iar_prev = i_list_head(target).iar_prev;
284 i_list_head(target).iar_prev->iar_next = iar;
285 iar = i_list_head(iauth).iar_prev;
286 iar->iar_next = &i_list_head(target);
287 i_list_head(target).iar_prev = iar;
289 /* If the target is not closing, send the requests. */
290 for (iar = i_list_head(iauth).iar_next;
291 iar != &i_list_head(target);
292 iar = iar->iar_next) {
293 if (!i_GetClosing(target))
294 iauth_send_request(target, iar);
297 /* No active connections - approve the requests and drop them. */
298 while ((iar = i_list_head(iauth).iar_next) != &i_list_head(iauth)) {
299 struct Client *client = iar->iar_client;
300 iauth_dispose_request(iauth, iar);
301 register_user(client, client, cli_name(client), cli_username(client));
305 /* Make sure the connection closes with an empty request list. */
306 i_list_head(iauth).iar_prev = &i_list_head(iauth);
307 i_list_head(iauth).iar_next = &i_list_head(iauth);
308 /* Cancel the timer, if it is active. */
309 if (t_active(&i_reconn_timer(iauth)))
310 timer_del(&i_reconn_timer(iauth));
311 if (t_active(&i_request_timer(iauth)))
312 timer_del(&i_request_timer(iauth));
313 /* Disconnect from the server. */
314 if (s_fd(&i_socket(iauth)) != -1)
315 iauth_disconnect(iauth);
320 /** Close all %IAuth connections marked as closing. */
321 void iauth_close_unused(void)
323 struct IAuth *prev, *iauth, *next;
325 for (prev = NULL, iauth = iauth_active; iauth; iauth = next) {
326 next = i_next(iauth);
327 if (i_GetClosing(iauth)) {
328 /* Update iauth_active linked list. */
333 /* Close and destroy the connection. */
341 /** Send a line to an %IAuth server.
342 * @param[in] iauth %Connection to send on.
343 * @param[in] format Format string for message.
345 static void iauth_send(struct IAuth *iauth, const char *format, ...)
350 va_start(vl, format);
351 mb = msgq_vmake(0, format, vl);
353 msgq_add(&i_sendQ(iauth), mb, 0);
357 /** Report a protocol violation from the %IAuth server.
358 * @param[in] iauth %Connection that experienced the violation.
359 * @param[in] format Format string for message to operators.
361 static void iauth_protocol_violation(struct IAuth *iauth, const char *format, ...)
366 vd.vd_format = format;
367 va_start(vd.vd_args, format);
368 sendwallto_group_butone(&me, WALL_DESYNCH, NULL, "IAuth protocol violation: %v", &vd);
372 /** Send on-connect burst to an %IAuth server.
373 * @param[in] iauth %Connection that has completed.
375 static void iauth_on_connect(struct IAuth *iauth)
377 struct IAuthRequest *iar;
378 if (EmptyString(i_passwd(iauth)))
379 iauth_send(iauth, "Server %s", cli_name(&me));
381 iauth_send(iauth, "Server %s %s", cli_name(&me), i_passwd(iauth));
382 if (i_GetIClass(iauth)) {
383 /* TODO: report local users to iauth */
384 iauth_send(iauth, "EndUsers");
386 i_SetConnected(iauth);
387 for (iar = i_list_head(iauth).iar_next;
388 iar != &i_list_head(iauth);
390 iauth_send_request(iauth, iar);
394 /** Complete disconnection of an %IAuth connection.
395 * @param[in] iauth %Connection to fully close.
397 static void iauth_disconnect(struct IAuth *iauth)
399 close(s_fd(&i_socket(iauth)));
400 socket_del(&i_socket(iauth));
401 s_fd(&i_socket(iauth)) = -1;
404 /** DNS completion callback for an %IAuth connection.
405 * @param[in] vptr Pointer to the IAuth struct.
406 * @param[in] he DNS reply parameters.
408 static void iauth_dns_callback(void *vptr, struct DNSReply *he)
410 struct IAuth *iauth = vptr;
412 sendto_opmask_butone(0, SNO_OLDSNO, "IAuth connection to %s failed: host lookup failed", i_host(iauth));
414 memcpy(&i_addr(iauth).addr, &he->addr, sizeof(i_addr(iauth).addr));
415 if (!irc_in_addr_valid(&i_addr(iauth).addr)) {
416 sendto_opmask_butone(0, SNO_OLDSNO, "IAuth connection to %s failed: host came back as unresolved", i_host(iauth));
419 iauth_reconnect(iauth);
423 /** Timer callback for reconnecting to %IAuth.
424 * @param[in] ev Timer event for reconnect.
426 static void iauth_reconnect_ev(struct Event *ev)
428 if (ev_type(ev) == ET_EXPIRE)
429 iauth_reconnect(t_data(ev_timer(ev)));
432 /** Schedule a reconnection for \a iauth.
433 * @param[in] iauth %Connection that needs to be reconnected.
435 static void iauth_schedule_reconnect(struct IAuth *iauth)
438 assert(!t_active(&i_reconn_timer(iauth)));
439 timer = timer_init(&i_reconn_timer(iauth));
440 timer_add(timer, iauth_reconnect_ev, iauth, TT_RELATIVE, i_reconnect(iauth));
443 /** Initiate a (re-)connection to \a iauth.
444 * @param[in] iauth %Connection that should be initiated.
446 static void iauth_reconnect(struct IAuth *iauth)
451 Debug((DEBUG_INFO, "IAuth attempt connection to %s port %p.", i_host(iauth), i_port(iauth)));
452 if (!irc_in_addr_valid(&i_addr(iauth).addr)
453 && !ircd_aton(&i_addr(iauth).addr, i_host(iauth))) {
454 i_query(iauth).vptr = iauth;
455 i_query(iauth).callback = iauth_dns_callback;
456 gethost_byname(i_host(iauth), &i_query(iauth));
459 fd = os_socket(&VirtualHost, SOCK_STREAM, "IAuth");
462 if (!os_set_sockbufs(fd, SERVER_TCP_WINDOW, SERVER_TCP_WINDOW)) {
464 sendto_opmask_butone(0, SNO_OLDSNO, "IAuth reconnect unable to set socket buffers: %s", strerror(errno));
467 result = os_connect_nonb(fd, &i_addr(iauth));
468 if (result == IO_FAILURE) {
470 sendto_opmask_butone(0, SNO_OLDSNO, "IAuth reconnect unable to initiate connection: %s", strerror(errno));
473 if (!socket_add(&i_socket(iauth), iauth_sock_callback, iauth,
474 (result == IO_SUCCESS) ? SS_CONNECTED : SS_CONNECTING,
475 SOCK_EVENT_READABLE | SOCK_EVENT_WRITABLE, fd)) {
477 sendto_opmask_butone(0, SNO_OLDSNO, "IAuth reconnect unable to add socket: %s", strerror(errno));
482 /** Read input from \a iauth.
483 * Reads up to SERVER_TCP_WINDOW bytes per pass.
484 * @param[in] iauth Readable connection.
486 static void iauth_read(struct IAuth *iauth)
488 char *src, *endp, *old_buffer, *argv[MAXPARA + 1];
489 unsigned int length, argc, ii;
490 char readbuf[SERVER_TCP_WINDOW];
493 if (IO_FAILURE == os_recv_nonb(s_fd(&i_socket(iauth)), readbuf, sizeof(readbuf), &length))
495 i_recvB(iauth) += length;
496 if (i_recvB(iauth) > 1023) {
497 i_recvK(iauth) += i_recvB(iauth) >> 10;
498 i_recvB(iauth) &= 1023;
500 old_buffer = i_buffer(iauth);
501 endp = old_buffer + i_count(iauth);
502 for (src = readbuf; length > 0; --length) {
505 /* Skip blank lines. */
506 if (endp == old_buffer)
508 /* NUL-terminate line and split parameters. */
510 for (argc = 0, endp = old_buffer; *endp && (argc < MAXPARA); ) {
517 argv[argc++] = endp + 1;
521 for (; *endp && *endp != ' '; ++endp) ;
524 /* Count line and reset endp to start of buffer. */
527 /* Look up command and try to dispatch. */
529 for (ii = 0; iauth_cmdtab[ii].iac_name; ++ii) {
530 if (!ircd_strcmp(iauth_cmdtab[ii].iac_name, argv[0])) {
531 iauth_cmdtab[ii].iac_func(iauth, argc, argv);
532 if (i_GetAbort(iauth))
533 iauth_disconnect(iauth);
539 else if (endp < old_buffer + BUFSIZE)
542 i_count(iauth) = endp - old_buffer;
545 /** Send queued output to \a iauth.
546 * @param[in] iauth Writable connection with queued data.
548 static void iauth_write(struct IAuth *iauth)
550 unsigned int bytes_tried, bytes_sent;
553 if (i_GetBlocked(iauth))
555 while (MsgQLength(&i_sendQ(iauth)) > 0) {
556 iores = os_sendv_nonb(s_fd(&i_socket(iauth)), &i_sendQ(iauth), &bytes_tried, &bytes_sent);
559 msgq_delete(&i_sendQ(iauth), bytes_sent);
560 i_sendB(iauth) += bytes_sent;
561 if (i_sendB(iauth) > 1023) {
562 i_sendK(iauth) += i_sendB(iauth) >> 10;
563 i_sendB(iauth) &= 1023;
565 if (bytes_tried == bytes_sent)
567 /* If bytes_sent < bytes_tried, fall through to IO_BLOCKED. */
570 socket_events(&i_socket(iauth), SOCK_ACTION_ADD | SOCK_EVENT_WRITABLE);
573 iauth_disconnect(iauth);
577 /* We were able to flush all events, so remove notification. */
578 socket_events(&i_socket(iauth), SOCK_ACTION_DEL | SOCK_EVENT_WRITABLE);
581 /** Handle socket activity for an %IAuth connection.
582 * @param[in] ev &Socket event; the IAuth connection is the user data pointer for the socket.
584 static void iauth_sock_callback(struct Event *ev)
588 assert(0 != ev_socket(ev));
589 iauth = (struct IAuth*) s_data(ev_socket(ev));
592 switch (ev_type(ev)) {
594 socket_state(ev_socket(ev), SS_CONNECTED);
595 iauth_on_connect(iauth);
598 if (!i_GetClosing(iauth))
599 iauth_schedule_reconnect(iauth);
609 iauth_disconnect(iauth);
612 sendto_opmask_butone(0, SNO_OLDSNO, "IAuth socket error: %s", strerror(ev_data(ev)));
613 log_write(LS_SOCKET, L_ERROR, 0, "IAuth socket error: %s", strerror(ev_data(ev)));
614 iauth_disconnect(iauth);
617 assert(0 && "Unrecognized event type");
622 /* Functions related to IAuthRequest structs */
624 /** Handle timeout while waiting for a response.
625 * @param[in] ev Timer event that expired.
627 static void iauth_request_ev(struct Event *ev)
629 /* TODO: this could probably be more intelligent */
630 if (ev_type(ev) == ET_EXPIRE) {
631 sendto_opmask_butone(0, SNO_OLDSNO, "IAuth request timed out; reconnecting");
632 iauth_reconnect(t_data(ev_timer(ev)));
636 /** Send a authorization request to an %IAuth server.
637 * @param[in] iauth %Connection to send request on.
638 * @param[in] iar Request to send.
640 static void iauth_send_request(struct IAuth *iauth, struct IAuthRequest *iar)
642 struct Client *client;
644 /* If iauth is not connected, we must defer the request. */
645 if (!i_GetConnected(iauth)) {
646 Debug((DEBUG_SEND, "IAuth deferring request for %s because we are not connected.", cli_name(iar->iar_client)));
650 /* If no timed request, set up expiration timer. */
651 if (!t_active(&i_request_timer(iauth))) {
652 struct Timer *timer = timer_init(&i_request_timer(iauth));
653 timer_add(timer, iauth_request_ev, iauth, TT_RELATIVE, i_timeout(iauth));
658 /* Send the FullAuth request. */
659 client = iar->iar_client;
660 assert(iar->iar_client != NULL);
661 iauth_send(iauth, "FullAuth %x %s %s %s %s %s :%s",
662 client, cli_name(client), cli_username(client),
663 cli_user(client)->host, cli_sock_ip(client),
664 cli_passwd(client), cli_info(client));
666 /* Write to the socket if we can. */
670 /** Start independent authorization check for a client.
671 * @param[in] iauth %Connection to send request on.
672 * @param[in] cptr Client to check.
673 * @return Zero, or CPTR_KILLED in case of memory allocation failure.
675 int iauth_start_client(struct IAuth *iauth, struct Client *cptr)
677 struct IAuthRequest *iar;
679 /* Allocate and initialize IAuthRequest struct. */
680 if (!(iar = MyCalloc(1, sizeof(*iar))))
681 return exit_client(cptr, cptr, &me, "IAuth memory allocation failed");
682 iar->iar_next = &i_list_head(iauth);
683 iar->iar_prev = i_list_head(iauth).iar_prev;
684 iar->iar_client = cptr;
685 iar->iar_prev->iar_next = iar;
686 iar->iar_next->iar_prev = iar;
689 iauth_send_request(iauth, iar);
694 /** Handle a client that is disconnecting.
695 * If there is a pending %IAuth request for the client, close it.
696 * @param[in] cptr Client that is disconnecting.
698 void iauth_exit_client(struct Client *cptr)
700 if (cli_iauth(cptr)) {
701 iauth_dispose_request(iauth_active, cli_iauth(cptr));
702 cli_iauth(cptr) = NULL;
703 } else if (IsIAuthed(cptr) && i_GetIClass(iauth_active)) {
704 /* TODO: report quit to iauth */
708 /** Find pending request with a particular ID.
709 * @param[in] iauth %Connection context for the ID.
710 * @param[in] id Identifier to look up.
711 * @return IAuthRequest with that ID, or NULL.
713 static struct IAuthRequest *iauth_find_request(struct IAuth *iauth, char *id)
715 struct IAuthRequest *curr;
716 struct Client *target;
717 target = (struct Client*)strtoul(id, NULL, 16);
718 for (curr = i_list_head(iauth).iar_next;
719 curr != &i_list_head(iauth);
720 curr = curr->iar_next) {
721 assert(curr->iar_client != NULL);
722 if (target == curr->iar_client)
728 /** Unlink and free a request.
729 * @param[in] iauth Connection that owns the request.
730 * @param[in] iar Request to free.
732 static void iauth_dispose_request(struct IAuth *iauth, struct IAuthRequest *iar)
734 assert(iar->iar_client != NULL);
736 timer_del(&i_request_timer(iauth));
737 cli_iauth(iar->iar_client) = NULL;
738 iar->iar_prev->iar_next = iar->iar_next;
739 iar->iar_next->iar_prev = iar->iar_prev;
743 /** Handle a DoneAuth response from %IAuth.
744 * THis means the client is authorized, so let them in.
745 * @param[in] iauth Connection that sent the message.
746 * @param[in] argc Argument count.
747 * @param[in] argv Argument list.
749 static void iauth_cmd_doneauth(struct IAuth *iauth, int argc, char *argv[])
751 struct IAuthRequest *iar;
752 struct Client *client;
760 iauth_protocol_violation(iauth, "Only %d parameters for DoneAuth (expected >=5)", argc);
767 account = (argc > 5) ? argv[5] : 0;
768 iar = iauth_find_request(iauth, id);
770 iauth_protocol_violation(iauth, "Got unexpected DoneAuth for id %s", id);
773 client = iar->iar_client;
774 ircd_strncpy(cli_username(client), username, USERLEN);
775 ircd_strncpy(cli_user(client)->host, hostname, HOSTLEN);
777 ircd_strncpy(cli_user(client)->account, account, ACCOUNTLEN);
781 iauth_dispose_request(iauth, iar);
782 register_user(client, client, cli_name(client), username);
785 /** Handle a BadAuth response from %IAuth.
786 * This means the client is not authorized, so dump them.
787 * @param[in] iauth Connection that sent the message.
788 * @param[in] argc Argument count.
789 * @param[in] argv Argument list.
791 static void iauth_cmd_badauth(struct IAuth *iauth, int argc, char *argv[])
793 struct IAuthRequest *iar;
794 struct Client *client;
799 iauth_protocol_violation(iauth, "Only %d parameters for BadAuth (expected >=3)", argc);
804 if (EmptyString(reason)) {
805 iauth_protocol_violation(iauth, "Empty BadAuth reason for id %s", id);
808 iar = iauth_find_request(iauth, id);
810 iauth_protocol_violation(iauth, "Got unexpected BadAuth for id %s", id);
813 client = iar->iar_client;
814 iauth_dispose_request(iauth, iar);
815 exit_client(client, client, &me, reason);