Make IAuth protocol work again and add a bit of debugging to it.
[ircu2.10.12-pk.git] / ircd / ircd_auth.c
1 /*
2  * IRC - Internet Relay Chat, ircd/ircd_auth.c
3  * Copyright 2004 Michael Poole <mdpoole@troilus.org>
4  *
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)
8  * any later version.
9  *
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.
14  *
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
18  * 02111-1307, USA.
19  *
20  * $Id$
21  */
22
23 #include "config.h"
24 #include "client.h"
25 #include "ircd_alloc.h"
26 #include "ircd_auth.h"
27 #include "ircd_events.h"
28 #include "ircd_features.h"
29 #include "ircd_log.h"
30 #include "ircd_osdep.h"
31 #include "ircd_snprintf.h"
32 #include "ircd_string.h"
33 #include "ircd.h"
34 #include "msg.h"
35 #include "msgq.h"
36 #include "res.h"
37 #include "s_bsd.h"
38 #include "s_debug.h"
39 #include "s_misc.h"
40 #include "s_user.h"
41 #include "send.h"
42
43 #include <assert.h>
44 #include <errno.h>
45 #include <netdb.h>
46 #include <string.h>
47 #include <unistd.h>
48 #include <sys/socket.h>
49 #include <netinet/in.h>
50 #include <arpa/inet.h>
51 #ifdef HAVE_STDINT_H
52 #include <stdint.h>
53 #endif
54
55 struct IAuthRequest {
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 */
60 };
61
62 enum IAuthFlag
63 {
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 */
69   IAUTH_LAST_FLAG
70 };
71 DECLARE_FLAGSET(IAuthFlags, IAUTH_LAST_FLAG);
72
73 struct IAuth {
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 */
95 };
96
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)
116
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)
138
139 struct IAuthCmd {
140   const char *iac_name;
141   void (*iac_func)(struct IAuth *iauth, int, char *[]);
142 };
143
144 struct IAuth *iauth_active;
145
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[]);
154
155 static const struct IAuthCmd iauth_cmdtab[] = {
156   { "DoneAuth", iauth_cmd_doneauth },
157   { "BadAuth", iauth_cmd_badauth },
158   { NULL, NULL }
159 };
160
161 struct IAuth *iauth_connect(char *host, unsigned short port, char *passwd, time_t reconnect, time_t timeout)
162 {
163   struct IAuth *iauth;
164
165   for (iauth = iauth_active; iauth; iauth = i_next(iauth)) {
166     if (!ircd_strncmp(i_host(iauth), host, HOSTLEN)
167         && (i_port(iauth) == port)) {
168       i_ClrClosing(iauth);
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));
172       break;
173     }
174   }
175   if (NULL == 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);
178     }
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);
189   }
190   if (passwd)
191     ircd_strncpy(i_passwd(iauth), passwd, PASSWDLEN);
192   else
193     i_passwd(iauth)[0] = '\0';
194   i_timeout(iauth) = timeout;
195   i_SetIClass(iauth);
196   return iauth;
197 }
198
199 void iauth_mark_closing(void)
200 {
201   struct IAuth *iauth;
202   for (iauth = iauth_active; iauth; iauth = i_next(iauth))
203     i_SetClosing(iauth);
204 }
205
206 void iauth_close(struct IAuth *iauth)
207 {
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.
215        */
216       struct IAuth *target = iauth_active ? iauth_active : i_next(iauth);
217
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;
225
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);
232       }
233     } else {
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));
239       }
240     }
241   }
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);
253   /* Free memory. */
254   MyFree(iauth);
255 }
256
257 void iauth_close_unused(void)
258 {
259   struct IAuth *prev, *iauth, *next;
260
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. */
265       if (prev)
266         i_next(prev) = next;
267       else
268         iauth_active = next;
269       /* Close and destroy the connection. */
270       iauth_close(iauth);
271     } else {
272       prev = iauth;
273     }
274   }
275 }
276
277 static void iauth_send(struct IAuth *iauth, const char *format, ...)
278 {
279   va_list vl;
280   struct MsgBuf *mb;
281
282   va_start(vl, format);
283   mb = msgq_vmake(0, format, vl);
284   va_end(vl);
285   msgq_add(&i_sendQ(iauth), mb, 0);
286   msgq_clean(mb);
287 }
288
289 static void iauth_protocol_violation(struct IAuth *iauth, const char *format, ...)
290 {
291   struct VarData vd;
292   assert(iauth != 0);
293   assert(format != 0);
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);
297   va_end(vd.vd_args);
298 }
299
300 static void iauth_on_connect(struct IAuth *iauth)
301 {
302   struct IAuthRequest *iar;
303   if (EmptyString(i_passwd(iauth)))
304     iauth_send(iauth, "Server %s", cli_name(&me));
305   else
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");
310   }
311   i_SetConnected(iauth);
312   for (iar = i_list_head(iauth).iar_next;
313        iar != &i_list_head(iauth);
314        iar = iar->iar_next)
315     iauth_send_request(iauth, iar);
316   iauth_write(iauth);
317 }
318
319 static void iauth_disconnect(struct IAuth *iauth)
320 {
321   close(s_fd(&i_socket(iauth)));
322   socket_del(&i_socket(iauth));
323   s_fd(&i_socket(iauth)) = -1;
324 }
325
326 static void iauth_dns_callback(void *vptr, struct DNSReply *he)
327 {
328   struct IAuth *iauth = vptr;
329   if (!he) {
330     sendto_opmask_butone(0, SNO_OLDSNO, "IAuth connection to %s failed: host lookup failed", i_host(iauth));
331   } else {
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));
335       return;
336     }
337     iauth_reconnect(iauth);
338   }
339 }
340
341 static void iauth_reconnect_ev(struct Event *ev)
342 {
343   if (ev_type(ev) == ET_EXPIRE)
344     iauth_reconnect(t_data(ev_timer(ev)));
345 }
346
347 static void iauth_schedule_reconnect(struct IAuth *iauth)
348 {
349   struct Timer *timer;
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));
353 }
354
355 static void iauth_reconnect(struct IAuth *iauth)
356 {
357   IOResult result;
358   int fd;
359
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));
366     return;
367   }
368   fd = os_socket(&VirtualHost, SOCK_STREAM, "IAuth");
369   if (fd < 0)
370     return;
371   if (!os_set_sockbufs(fd, SERVER_TCP_WINDOW, SERVER_TCP_WINDOW)) {
372     close(fd);
373     sendto_opmask_butone(0, SNO_OLDSNO, "IAuth reconnect unable to set socket buffers: %s", strerror(errno));
374     return;
375   }
376   result = os_connect_nonb(fd, &i_addr(iauth));
377   if (result == IO_FAILURE) {
378     close(fd);
379     sendto_opmask_butone(0, SNO_OLDSNO, "IAuth reconnect unable to initiate connection: %s", strerror(errno));
380     return;
381   }
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)) {
385     close(fd);
386     sendto_opmask_butone(0, SNO_OLDSNO, "IAuth reconnect unable to add socket: %s", strerror(errno));
387     return;
388   }
389 }
390
391 static void iauth_read(struct IAuth *iauth)
392 {
393   char *src, *endp, *old_buffer, *argv[MAXPARA + 1];
394   unsigned int length, argc, ii;
395   char readbuf[SERVER_TCP_WINDOW];
396
397   length = 0;
398   if (IO_FAILURE == os_recv_nonb(s_fd(&i_socket(iauth)), readbuf, sizeof(readbuf), &length))
399     return;
400   i_recvB(iauth) += length;
401   if (i_recvB(iauth) > 1023) {
402     i_recvK(iauth) += i_recvB(iauth) >> 10;
403     i_recvB(iauth) &= 1023;
404   }
405   old_buffer = i_buffer(iauth);
406   endp = old_buffer + i_count(iauth);
407   for (src = readbuf; length > 0; --length) {
408     *endp = *src++;
409     if (IsEol(*endp)) {
410       /* Skip blank lines. */
411       if (endp == old_buffer)
412         continue;
413       /* NUL-terminate line and split parameters. */
414       *endp = '\0';
415       for (argc = 0, endp = old_buffer; *endp && (argc < MAXPARA); ) {
416         while (*endp == ' ')
417           *endp++ = '\0';
418         if (*endp == '\0')
419           break;
420         if (*endp == ':')
421         {
422           argv[argc++] = endp + 1;
423           break;
424         }
425         argv[argc++] = endp;
426         for (; *endp && *endp != ' '; ++endp) ;
427       }
428       argv[argc] = NULL;
429       /* Count line and reset endp to start of buffer. */
430       i_recvM(iauth)++;
431       endp = old_buffer;
432       /* Look up command and try to dispatch. */
433       if (argc > 0) {
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);
439             break;
440           }
441         }
442       }
443     }
444     else if (endp < old_buffer + BUFSIZE)
445       endp++;
446   }
447   i_count(iauth) = endp - old_buffer;
448 }
449
450 static void iauth_write(struct IAuth *iauth)
451 {
452   unsigned int bytes_tried, bytes_sent;
453   IOResult iores;
454
455   if (i_GetBlocked(iauth))
456     return;
457   while (MsgQLength(&i_sendQ(iauth)) > 0) {
458     iores = os_sendv_nonb(s_fd(&i_socket(iauth)), &i_sendQ(iauth), &bytes_tried, &bytes_sent);
459     switch (iores) {
460     case IO_SUCCESS:
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;
466       }
467       if (bytes_tried == bytes_sent)
468         break;
469       /* If bytes_sent < bytes_tried, fall through to IO_BLOCKED. */
470     case IO_BLOCKED:
471       i_SetBlocked(iauth);
472       socket_events(&i_socket(iauth), SOCK_ACTION_ADD | SOCK_EVENT_WRITABLE);
473       return;
474     case IO_FAILURE:
475       iauth_disconnect(iauth);
476       return;
477     }
478   }
479   /* We were able to flush all events, so remove notification. */
480   socket_events(&i_socket(iauth), SOCK_ACTION_DEL | SOCK_EVENT_WRITABLE);
481 }
482
483 static void iauth_sock_callback(struct Event *ev)
484 {
485   struct IAuth *iauth;
486
487   assert(0 != ev_socket(ev));
488   iauth = (struct IAuth*) s_data(ev_socket(ev));
489   assert(0 != iauth);
490
491   switch (ev_type(ev)) {
492   case ET_CONNECT:
493     socket_state(ev_socket(ev), SS_CONNECTED);
494     iauth_on_connect(iauth);
495     break;
496   case ET_DESTROY:
497     if (!i_GetClosing(iauth))
498       iauth_schedule_reconnect(iauth);
499     break;
500   case ET_READ:
501     iauth_read(iauth);
502     break;
503   case ET_WRITE:
504     i_ClrBlocked(iauth);
505     iauth_write(iauth);
506     break;
507   case ET_EOF:
508     iauth_disconnect(iauth);
509     break;
510   case ET_ERROR:
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);
514     break;
515   default:
516     assert(0 && "Unrecognized event type");
517     break;
518   }
519 }
520
521 /* Functions related to IAuthRequest structs */
522
523 static void iauth_request_ev(struct Event *ev)
524 {
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)));
529   }
530 }
531
532 static void iauth_send_request(struct IAuth *iauth, struct IAuthRequest *iar)
533 {
534   struct Client *client;
535
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)));
539     return;
540   }
541
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));
546     iar->iar_timed = 1;
547   } else
548     iar->iar_timed = 0;
549
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));
557
558   /* Write to the socket if we can. */
559   iauth_write(iauth);
560 }
561
562 int iauth_start_client(struct IAuth *iauth, struct Client *cptr)
563 {
564   struct IAuthRequest *iar;
565
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;
574
575   /* Send request. */
576   iauth_send_request(iauth, iar);
577
578   return 0;
579 }
580
581 void iauth_exit_client(struct Client *cptr)
582 {
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 */
588   }
589 }
590
591 static struct IAuthRequest *iauth_find_request(struct IAuth *iauth, char *id)
592 {
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)
601       return curr;
602   }
603   return NULL;
604 }
605
606 static void iauth_dispose_request(struct IAuth *iauth, struct IAuthRequest *iar)
607 {
608   assert(iar->iar_client != NULL);
609   if (iar->iar_timed)
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;
614   MyFree(iar);
615 }
616
617 static void iauth_cmd_doneauth(struct IAuth *iauth, int argc, char *argv[])
618 {
619   struct IAuthRequest *iar;
620   struct Client *client;
621   char *id;
622   char *username;
623   char *hostname;
624   char *c_class;
625   char *account;
626
627   if (argc < 5) {
628     iauth_protocol_violation(iauth, "Only %d parameters for DoneAuth (expected >=5)", argc);
629     return;
630   }
631   id = argv[1];
632   username = argv[2];
633   hostname = argv[3];
634   c_class = argv[4];
635   account = (argc > 5) ? argv[5] : 0;
636   iar = iauth_find_request(iauth, id);
637   if (!iar) {
638     iauth_protocol_violation(iauth, "Got unexpected DoneAuth for id %s", id);
639     return;
640   }
641   client = iar->iar_client;
642   ircd_strncpy(cli_username(client), username, USERLEN);
643   ircd_strncpy(cli_user(client)->host, hostname, HOSTLEN);
644   if (account) {
645     ircd_strncpy(cli_user(client)->account, account, ACCOUNTLEN);
646     SetAccount(client);
647   }
648   SetIAuthed(client);
649   iauth_dispose_request(iauth, iar);
650   register_user(client, client, cli_name(client), username);
651 }
652
653 static void iauth_cmd_badauth(struct IAuth *iauth, int argc, char *argv[])
654 {
655   struct IAuthRequest *iar;
656   struct Client *client;
657   char *id;
658   char *reason;
659
660   if (argc < 3) {
661     iauth_protocol_violation(iauth, "Only %d parameters for BadAuth (expected >=3)", argc);
662     return;
663   }
664   id = argv[1];
665   reason = argv[2];
666   if (EmptyString(reason)) {
667     iauth_protocol_violation(iauth, "Empty BadAuth reason for id %s", id);
668     return;
669   }
670   iar = iauth_find_request(iauth, id);
671   if (!iar) {
672     iauth_protocol_violation(iauth, "Got unexpected BadAuth for id %s", id);
673     return;
674   }
675   client = iar->iar_client;
676   iauth_dispose_request(iauth, iar);
677   exit_client(client, client, &me, reason);
678 }