31fc36683972c9d5603d92d2d1e027eae64b9b97
[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_misc.h"
39 #include "s_user.h"
40 #include "send.h"
41
42 #include <assert.h>
43 #include <errno.h>
44 #include <netdb.h>
45 #include <string.h>
46 #include <unistd.h>
47 #include <sys/socket.h>
48 #include <netinet/in.h>
49 #include <arpa/inet.h>
50 #ifdef HAVE_STDINT_H
51 #include <stdint.h>
52 #endif
53
54 struct IAuthRequest {
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 */
59 };
60
61 enum IAuthFlag
62 {
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 */
68   IAUTH_LAST_FLAG
69 };
70 DECLARE_FLAGSET(IAuthFlags, IAUTH_LAST_FLAG);
71
72 struct IAuth {
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 */
94 };
95
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)
115
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)
137
138 struct IAuthCmd {
139   const char *iac_name;
140   void (*iac_func)(struct IAuth *iauth, int, char *[]);
141 };
142
143 struct IAuth *iauth_active;
144
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[]);
153
154 static const struct IAuthCmd iauth_cmdtab[] = {
155   { "DoneAuth", iauth_cmd_doneauth },
156   { "BadAuth", iauth_cmd_badauth },
157   { NULL, NULL }
158 };
159
160 struct IAuth *iauth_connect(char *host, unsigned short port, char *passwd, time_t reconnect, time_t timeout)
161 {
162   struct IAuth *iauth;
163
164   for (iauth = iauth_active; iauth; iauth = i_next(iauth)) {
165     if (!ircd_strncmp(i_host(iauth), host, HOSTLEN)
166         && (i_port(iauth) == port)) {
167       i_ClrClosing(iauth);
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));
171       break;
172     }
173   }
174   if (NULL == 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);
177     }
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);
188   }
189   if (passwd)
190     ircd_strncpy(i_passwd(iauth), passwd, PASSWDLEN);
191   else
192     i_passwd(iauth)[0] = '\0';
193   i_timeout(iauth) = timeout;
194   return iauth;
195 }
196
197 void iauth_mark_closing(void)
198 {
199   struct IAuth *iauth;
200   for (iauth = iauth_active; iauth; iauth = i_next(iauth))
201     i_SetClosing(iauth);
202 }
203
204 void iauth_close(struct IAuth *iauth)
205 {
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.
213        */
214       struct IAuth *target = iauth_active ? iauth_active : i_next(iauth);
215
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;
223
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);
230       }
231     } else {
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));
237       }
238     }
239   }
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);
251   /* Free memory. */
252   MyFree(iauth);
253 }
254
255 void iauth_close_unused(void)
256 {
257   struct IAuth *prev, *iauth, *next;
258   
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. */
263       if (prev)
264         i_next(prev) = next;
265       else
266         iauth_active = next;
267       /* Close and destroy the connection. */
268       iauth_close(iauth);
269     } else {
270       prev = iauth;
271     }
272   }
273 }
274
275 static void iauth_send(struct IAuth *iauth, const char *format, ...)
276 {
277   va_list vl;
278   struct MsgBuf *mb;
279
280   va_start(vl, format);
281   mb = msgq_vmake(0, format, vl);
282   va_end(vl);
283   msgq_add(&i_sendQ(iauth), mb, 0);
284   msgq_clean(mb);
285 }
286
287 static void iauth_protocol_violation(struct IAuth *iauth, const char *format, ...)
288 {
289   struct VarData vd;
290   assert(iauth != 0);
291   assert(format != 0);
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);
295   va_end(vd.vd_args);
296 }
297
298 static void iauth_on_connect(struct IAuth *iauth)
299 {
300   struct IAuthRequest *iar;
301   if (EmptyString(i_passwd(iauth)))
302     iauth_send(iauth, "Server %s", cli_name(&me));
303   else
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");
308   }
309   i_SetConnected(iauth);
310   for (iar = i_list_head(iauth).iar_next;
311        iar != &i_list_head(iauth);
312        iar = iar->iar_next)
313     iauth_send_request(iauth, iar);
314   iauth_write(iauth);
315 }
316
317 static void iauth_disconnect(struct IAuth *iauth)
318 {
319   close(s_fd(&i_socket(iauth)));
320   socket_del(&i_socket(iauth));
321   s_fd(&i_socket(iauth)) = -1;
322 }
323
324 static void iauth_dns_callback(void *vptr, struct DNSReply *he)
325 {
326   struct IAuth *iauth = vptr;
327   if (!he) {
328     sendto_opmask_butone(0, SNO_OLDSNO, "IAuth connection to %s failed: host lookup failed", i_host(iauth));
329   } else {
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));
333       return;
334     }
335     iauth_reconnect(iauth);
336   }
337 }
338
339 static void iauth_reconnect_ev(struct Event *ev)
340 {
341   if (ev_type(ev) == ET_EXPIRE)
342     iauth_reconnect(t_data(ev_timer(ev)));
343 }
344
345 static void iauth_schedule_reconnect(struct IAuth *iauth)
346 {
347   struct Timer *timer;
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));
351 }
352
353 static void iauth_reconnect(struct IAuth *iauth)
354 {
355   IOResult result;
356   int fd;
357
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));
363     return;
364   }
365   fd = os_socket((feature_bool(FEAT_VIRTUAL_HOST) ? &VirtualHost : NULL), SOCK_STREAM, "IAuth");
366   if (fd < 0)
367     return;
368   if (!os_set_sockbufs(fd, SERVER_TCP_WINDOW, SERVER_TCP_WINDOW)) {
369     close(fd);
370     sendto_opmask_butone(0, SNO_OLDSNO, "IAuth reconnect unable to set socket buffers: %s", strerror(errno));
371     return;
372   }
373   result = os_connect_nonb(fd, &i_addr(iauth));
374   if (result == IO_FAILURE) {
375     close(fd);
376     sendto_opmask_butone(0, SNO_OLDSNO, "IAuth reconnect unable to initiate connection: %s", strerror(errno));
377     return;
378   }
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)) {
382     close(fd);
383     sendto_opmask_butone(0, SNO_OLDSNO, "IAuth reconnect unable to add socket: %s", strerror(errno));
384     return;
385   }
386 }
387
388 static void iauth_read(struct IAuth *iauth)
389 {
390   char *src, *endp, *old_buffer, *argv[MAXPARA + 1];
391   unsigned int length, argc, ii;
392   char readbuf[SERVER_TCP_WINDOW];
393
394   length = 0;
395   if (IO_FAILURE == os_recv_nonb(s_fd(&i_socket(iauth)), readbuf, sizeof(readbuf), &length))
396     return;
397   i_recvB(iauth) += length;
398   if (i_recvB(iauth) > 1023) {
399     i_recvK(iauth) += i_recvB(iauth) >> 10;
400     i_recvB(iauth) &= 1023;
401   }
402   old_buffer = i_buffer(iauth);
403   endp = old_buffer + i_count(iauth);
404   for (src = readbuf; length > 0; --length) {
405     *endp = *src++;
406     if (IsEol(*endp)) {
407       /* Skip blank lines. */
408       if (endp == old_buffer)
409         continue;
410       /* NUL-terminate line and split parameters. */
411       *endp = '\0';
412       for (argc = 0, endp = old_buffer; *endp && (argc < MAXPARA); ) {
413         while (*endp == ' ')
414           *endp++ = '\0';
415         if (*endp == '\0')
416           break;
417         if (*endp == ':')
418         {
419           argv[argc++] = endp + 1;
420           break;
421         }
422         argv[argc++] = endp;
423         for (; *endp && *endp != ' '; ++endp) ;
424       }
425       argv[argc] = NULL;
426       /* Count line and reset endp to start of buffer. */
427       i_recvM(iauth)++;
428       endp = old_buffer;
429       /* Look up command and try to dispatch. */
430       if (argc > 0) {
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);
436             break;
437           }
438         }
439       }
440     }
441     else if (endp < old_buffer + BUFSIZE)
442       endp++;
443   }
444   i_count(iauth) = endp - old_buffer;
445 }
446
447 static void iauth_write(struct IAuth *iauth)
448 {
449   unsigned int bytes_tried, bytes_sent;
450   IOResult iores;
451
452   if (i_GetBlocked(iauth))
453     return;
454   while (MsgQLength(&i_sendQ(iauth)) > 0) {
455     iores = os_sendv_nonb(s_fd(&i_socket(iauth)), &i_sendQ(iauth), &bytes_tried, &bytes_sent);
456     switch (iores) {
457     case IO_SUCCESS:
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;
463       }
464       if (bytes_tried == bytes_sent)
465         break;
466       /* If bytes_sent < bytes_tried, fall through to IO_BLOCKED. */
467     case IO_BLOCKED:
468       i_SetBlocked(iauth);
469       socket_events(&i_socket(iauth), SOCK_ACTION_ADD | SOCK_EVENT_WRITABLE);
470       return;
471     case IO_FAILURE:
472       iauth_disconnect(iauth);
473       return;
474     }
475   }
476   /* We were able to flush all events, so remove notification. */
477   socket_events(&i_socket(iauth), SOCK_ACTION_DEL | SOCK_EVENT_WRITABLE);
478 }
479
480 static void iauth_sock_callback(struct Event *ev)
481 {
482   struct IAuth *iauth;
483
484   assert(0 != ev_socket(ev));
485   iauth = (struct IAuth*) s_data(ev_socket(ev));
486   assert(0 != iauth);
487
488   switch (ev_type(ev)) {
489   case ET_CONNECT:
490     socket_state(ev_socket(ev), SS_CONNECTED);
491     iauth_on_connect(iauth);
492     break;
493   case ET_DESTROY:
494     if (!i_GetClosing(iauth))
495       iauth_schedule_reconnect(iauth);
496     break;
497   case ET_READ:
498     iauth_read(iauth);
499     break;
500   case ET_WRITE:
501     i_ClrBlocked(iauth);
502     iauth_write(iauth);
503     break;
504   case ET_EOF:
505     iauth_disconnect(iauth);
506     break;
507   case ET_ERROR:
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);
511     break;
512   default:
513     assert(0 && "Unrecognized event type");
514     break;
515   }
516 }
517
518 /* Functions related to IAuthRequest structs */
519
520 static void iauth_request_ev(struct Event *ev)
521 {
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)));
526   }
527 }
528
529 static void iauth_send_request(struct IAuth *iauth, struct IAuthRequest *iar)
530 {
531   struct Client *client;
532
533   /* If iauth is not connected, we must defer the request. */
534   if (!i_GetConnected(iauth))
535     return;
536
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));
541     iar->iar_timed = 1;
542   } else
543     iar->iar_timed = 0;
544
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));
552
553   /* Write to the socket if we can. */
554   iauth_write(iauth);
555 }
556
557 int iauth_start_client(struct IAuth *iauth, struct Client *cptr)
558 {
559   struct IAuthRequest *iar;
560
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;
569
570   /* Send request. */
571   iauth_send_request(iauth, iar);
572
573   return 0;
574 }
575
576 void iauth_exit_client(struct Client *cptr)
577 {
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 */
583   }
584 }
585
586 static struct IAuthRequest *iauth_find_request(struct IAuth *iauth, char *id)
587 {
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)
596       return curr;
597   }
598   return NULL;
599 }
600
601 static void iauth_dispose_request(struct IAuth *iauth, struct IAuthRequest *iar)
602 {
603   assert(iar->iar_client != NULL);
604   if (iar->iar_timed)
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;
609   MyFree(iar);
610 }
611
612 static void iauth_cmd_doneauth(struct IAuth *iauth, int argc, char *argv[])
613 {
614   struct IAuthRequest *iar;
615   struct Client *client;
616   char *id;
617   char *username;
618   char *hostname;
619   char *c_class;
620   char *account;
621
622   if (argc < 5) {
623     iauth_protocol_violation(iauth, "Only %d parameters for DoneAuth (expected >=5)", argc);
624     return;
625   }
626   id = argv[1];
627   username = argv[2];
628   hostname = argv[3];
629   c_class = argv[4];
630   account = (argc > 5) ? argv[5] : 0;
631   iar = iauth_find_request(iauth, id);
632   if (!iar) {
633     iauth_protocol_violation(iauth, "Got unexpected DoneAuth for id %s", id);
634     return;
635   }
636   client = iar->iar_client;
637   ircd_strncpy(cli_username(client), username, USERLEN);
638   ircd_strncpy(cli_user(client)->host, hostname, HOSTLEN);
639   if (account) {
640     ircd_strncpy(cli_user(client)->account, account, ACCOUNTLEN);
641     SetAccount(client);
642   }
643   SetIAuthed(client);
644   iauth_dispose_request(iauth, iar);
645   register_user(client, client, cli_name(client), username);
646 }
647
648 static void iauth_cmd_badauth(struct IAuth *iauth, int argc, char *argv[])
649 {
650   struct IAuthRequest *iar;
651   struct Client *client;
652   char *id;
653   char *reason;
654
655   if (argc < 3) {
656     iauth_protocol_violation(iauth, "Only %d parameters for BadAuth (expected >=3)", argc);
657     return;
658   }
659   id = argv[1];
660   reason = argv[2];
661   if (EmptyString(reason)) {
662     iauth_protocol_violation(iauth, "Empty BadAuth reason for id %s", id);
663     return;
664   }
665   iar = iauth_find_request(iauth, id);
666   if (!iar) {
667     iauth_protocol_violation(iauth, "Got unexpected BadAuth for id %s", id);
668     return;
669   }
670   client = iar->iar_client;
671   iauth_dispose_request(iauth, iar);
672   exit_client(client, client, &me, reason);
673 }