Invite/ban list length checks.
[silc.git] / apps / silcd / packet_receive.c
1 /*
2
3   packet_receive.c
4
5   Author: Pekka Riikonen <priikone@silcnet.org>
6
7   Copyright (C) 1997 - 2002 Pekka Riikonen
8
9   This program is free software; you can redistribute it and/or modify
10   it under the terms of the GNU General Public License as published by
11   the Free Software Foundation; either version 2 of the License, or
12   (at your option) any later version.
13   
14   This program is distributed in the hope that it will be useful,
15   but WITHOUT ANY WARRANTY; without even the implied warranty of
16   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17   GNU General Public License for more details.
18
19 */
20 /*
21  * Server packet routines to handle received packets.
22  */
23 /* $Id$ */
24
25 #include "serverincludes.h"
26 #include "server_internal.h"
27
28 /* Received notify packet. Server can receive notify packets from router. 
29    Server then relays the notify messages to clients if needed. */
30
31 void silc_server_notify(SilcServer server,
32                         SilcSocketConnection sock,
33                         SilcPacketContext *packet)
34 {
35   SilcNotifyPayload payload;
36   SilcNotifyType type;
37   SilcArgumentPayload args;
38   SilcChannelID *channel_id = NULL, *channel_id2;
39   SilcClientID *client_id, *client_id2;
40   SilcServerID *server_id;
41   SilcIdType id_type;
42   SilcChannelEntry channel = NULL;
43   SilcClientEntry client = NULL, client2 = NULL;
44   SilcServerEntry server_entry = NULL;
45   SilcChannelClientEntry chl;
46   SilcIDCacheEntry cache = NULL;
47   SilcHashTableList htl;
48   SilcUInt32 mode;
49   unsigned char *tmp;
50   SilcUInt32 tmp_len;
51   bool local;
52
53   if (sock->type == SILC_SOCKET_TYPE_CLIENT ||
54       packet->src_id_type != SILC_ID_SERVER || !packet->dst_id) {
55     SILC_LOG_DEBUG(("Bad notify packet received"));
56     return;
57   }
58
59   /* If the packet is destined directly to a client then relay the packet
60      before processing it. */
61   if (packet->dst_id_type == SILC_ID_CLIENT) {
62     SilcIDListData idata;
63     SilcSocketConnection dst_sock;
64
65     /* Get the route to the client */
66     dst_sock = silc_server_get_client_route(server, packet->dst_id,
67                                             packet->dst_id_len, NULL, 
68                                             &idata, NULL);
69     if (dst_sock)
70       /* Relay the packet */
71       silc_server_relay_packet(server, dst_sock, idata->send_key,
72                                idata->hmac_send, idata->psn_send++,
73                                packet, TRUE);
74   }
75
76   /* Parse the Notify Payload */
77   payload = silc_notify_payload_parse(packet->buffer->data,
78                                       packet->buffer->len);
79   if (!payload)
80     return;
81
82   /* If we are router and this packet is not already broadcast packet
83      we will broadcast it. The sending socket really cannot be router or
84      the router is buggy. If this packet is coming from router then it must
85      have the broadcast flag set already and we won't do anything. */
86   if (server->server_type == SILC_ROUTER &&
87       sock->type == SILC_SOCKET_TYPE_SERVER &&
88       !(packet->flags & SILC_PACKET_FLAG_BROADCAST)) {
89     SILC_LOG_DEBUG(("Broadcasting received Notify packet"));
90     if (packet->dst_id_type == SILC_ID_CHANNEL) {
91       /* Packet is destined to channel */
92       channel_id = silc_id_str2id(packet->dst_id, packet->dst_id_len,
93                                   packet->dst_id_type);
94       if (!channel_id)
95         goto out;
96
97       silc_server_packet_send_dest(server, SILC_PRIMARY_ROUTE(server), 
98                                    packet->type, packet->flags | 
99                                    SILC_PACKET_FLAG_BROADCAST, 
100                                    channel_id, SILC_ID_CHANNEL,
101                                    packet->buffer->data, 
102                                    packet->buffer->len, FALSE);
103       silc_server_backup_send_dest(server, sock->user_data, 
104                                    packet->type, packet->flags,
105                                    channel_id, SILC_ID_CHANNEL,
106                                    packet->buffer->data, packet->buffer->len, 
107                                    FALSE, TRUE);
108     } else {
109       /* Packet is destined to client or server */
110       silc_server_packet_send(server, SILC_PRIMARY_ROUTE(server), 
111                               packet->type,
112                               packet->flags | SILC_PACKET_FLAG_BROADCAST, 
113                               packet->buffer->data, packet->buffer->len, 
114                               FALSE);
115       silc_server_backup_send(server, sock->user_data,
116                               packet->type, packet->flags,
117                               packet->buffer->data, packet->buffer->len, 
118                               FALSE, TRUE);
119     }
120   }
121
122   type = silc_notify_get_type(payload);
123   args = silc_notify_get_args(payload);
124   if (!args)
125     goto out;
126
127   switch(type) {
128   case SILC_NOTIFY_TYPE_JOIN:
129     /* 
130      * Distribute the notify to local clients on the channel
131      */
132     SILC_LOG_DEBUG(("JOIN notify"));
133
134     if (channel_id)
135       silc_free(channel_id);
136
137     /* Get Channel ID */
138     tmp = silc_argument_get_arg_type(args, 2, &tmp_len);
139     if (!tmp)
140       goto out;
141     channel_id = silc_id_payload_parse_id(tmp, tmp_len, NULL);
142     if (!channel_id)
143       goto out;
144
145     /* Get channel entry */
146     channel = silc_idlist_find_channel_by_id(server->global_list, 
147                                              channel_id, NULL);
148     if (!channel) {
149       channel = silc_idlist_find_channel_by_id(server->local_list, 
150                                                channel_id, NULL);
151       if (!channel) {
152         SILC_LOG_DEBUG(("Notify for unknown channel"));
153         silc_free(channel_id);
154         goto out;
155       }
156     }
157     silc_free(channel_id);
158
159     /* Get client ID */
160     tmp = silc_argument_get_arg_type(args, 1, &tmp_len);
161     if (!tmp)
162       goto out;
163     client_id = silc_id_payload_parse_id(tmp, tmp_len, NULL);
164     if (!client_id)
165       goto out;
166
167     /* If the the client is not in local list we check global list (ie. the
168        channel will be global channel) and if it does not exist then create
169        entry for the client. */
170     client = silc_idlist_find_client_by_id(server->global_list, 
171                                            client_id, server->server_type, 
172                                            &cache);
173     if (!client) {
174       client = silc_idlist_find_client_by_id(server->local_list, 
175                                              client_id, server->server_type,
176                                              &cache);
177       if (!client) {
178         /* If router did not find the client the it is bogus */
179         if (server->server_type != SILC_SERVER) {
180           silc_free(client_id);
181           goto out;
182         }
183
184         client = 
185           silc_idlist_add_client(server->global_list, NULL, NULL, NULL,
186                                  silc_id_dup(client_id, SILC_ID_CLIENT), 
187                                  sock->user_data, NULL, 0);
188         if (!client) {
189           SILC_LOG_ERROR(("Could not add new client to the ID Cache"));
190           silc_free(client_id);
191           goto out;
192         }
193
194         client->data.status |= SILC_IDLIST_STATUS_REGISTERED;
195       }
196     }
197     silc_free(client_id);
198
199     /* Do not process the notify if the client is not registered */
200     if (!(client->data.status & SILC_IDLIST_STATUS_REGISTERED))
201       break;
202
203     /* Do not add client to channel if it is there already */
204     if (silc_server_client_on_channel(client, channel, NULL)) {
205       SILC_LOG_DEBUG(("Client already on channel %s",
206                       channel->channel_name));
207       break;
208     }
209
210     /* Send to channel */
211     silc_server_packet_send_to_channel(server, sock, channel, packet->type, 
212                                        FALSE, packet->buffer->data, 
213                                        packet->buffer->len, FALSE);
214
215     if (server->server_type != SILC_ROUTER && 
216         sock->type == SILC_SOCKET_TYPE_ROUTER)
217       /* The channel is global now */
218       channel->global_users = TRUE;
219
220     SILC_LOG_DEBUG(("Joining to channel %s", channel->channel_name));
221
222     /* JOIN the global client to the channel (local clients (if router 
223        created the channel) is joined in the pending JOIN command). */
224     chl = silc_calloc(1, sizeof(*chl));
225     chl->client = client;
226     chl->channel = channel;
227
228     if (server->server_type != SILC_ROUTER ||
229         sock->type == SILC_SOCKET_TYPE_ROUTER) {
230       /* If this is the first one on the channel then it is the founder of
231          the channel. This is done on normal server and on router if this
232          notify is coming from router */
233       if (!silc_hash_table_count(channel->user_list)) {
234         SILC_LOG_DEBUG(("Client %s is founder on channel",
235                         silc_id_render(chl->client->id, SILC_ID_CLIENT)));
236         chl->mode = (SILC_CHANNEL_UMODE_CHANOP | SILC_CHANNEL_UMODE_CHANFO);
237       }
238     }
239
240     silc_hash_table_add(channel->user_list, client, chl);
241     silc_hash_table_add(client->channels, channel, chl);
242     channel->user_count++;
243     channel->disabled = FALSE;
244
245     /* Make sure we don't expire clients that are on channel */
246     if (cache)
247       cache->expire = 0;
248
249     /* Update statistics */
250     if (server->server_type == SILC_ROUTER) {
251       if (sock->type != SILC_SOCKET_TYPE_ROUTER)
252         server->stat.cell_chanclients++;
253       server->stat.chanclients++;
254     }
255
256     break;
257
258   case SILC_NOTIFY_TYPE_LEAVE:
259     /* 
260      * Distribute the notify to local clients on the channel
261      */
262     SILC_LOG_DEBUG(("LEAVE notify"));
263
264     if (!channel_id) {
265       channel_id = silc_id_str2id(packet->dst_id, packet->dst_id_len,
266                                   packet->dst_id_type);
267       if (!channel_id)
268         goto out;
269     }
270
271     /* Get channel entry */
272     channel = silc_idlist_find_channel_by_id(server->global_list, 
273                                              channel_id, NULL);
274     if (!channel) { 
275       channel = silc_idlist_find_channel_by_id(server->local_list, 
276                                                channel_id, NULL);
277       if (!channel) {
278         SILC_LOG_DEBUG(("Notify for unknown channel"));
279         silc_free(channel_id);
280         goto out;
281       }
282     }
283
284     /* Get client ID */
285     tmp = silc_argument_get_arg_type(args, 1, &tmp_len);
286     if (!tmp) {
287       silc_free(channel_id);
288       goto out;
289     }
290     client_id = silc_id_payload_parse_id(tmp, tmp_len, NULL);
291     if (!client_id) {
292       silc_free(channel_id);
293       goto out;
294     }
295
296     /* Get client entry */
297     client = silc_idlist_find_client_by_id(server->global_list, 
298                                            client_id, TRUE, NULL);
299     if (!client) {
300       client = silc_idlist_find_client_by_id(server->local_list, 
301                                              client_id, TRUE, NULL);
302       if (!client) {
303         silc_free(client_id);
304         silc_free(channel_id);
305         goto out;
306       }
307     }
308     silc_free(client_id);
309     silc_free(channel_id);
310
311     /* Check if on channel */
312     if (!silc_server_client_on_channel(client, channel, NULL))
313       break;
314
315     /* Send the leave notify to channel */
316     silc_server_packet_send_to_channel(server, sock, channel, packet->type, 
317                                        FALSE, packet->buffer->data, 
318                                        packet->buffer->len, FALSE);
319
320     /* Remove the user from channel */
321     silc_server_remove_from_one_channel(server, sock, channel, client, FALSE);
322     break;
323
324   case SILC_NOTIFY_TYPE_SIGNOFF:
325     /* 
326      * Distribute the notify to local clients on the channel
327      */
328     SILC_LOG_DEBUG(("SIGNOFF notify"));
329
330     /* Get client ID */
331     tmp = silc_argument_get_arg_type(args, 1, &tmp_len);
332     if (!tmp)
333       goto out;
334     client_id = silc_id_payload_parse_id(tmp, tmp_len, NULL);
335     if (!client_id)
336       goto out;
337
338     /* Get client entry */
339     client = silc_idlist_find_client_by_id(server->global_list, 
340                                            client_id, TRUE, &cache);
341     if (!client) {
342       client = silc_idlist_find_client_by_id(server->local_list, 
343                                              client_id, TRUE, &cache);
344       if (!client) {
345         silc_free(client_id);
346         goto out;
347       }
348     }
349     silc_free(client_id);
350
351     /* Get signoff message */
352     tmp = silc_argument_get_arg_type(args, 2, &tmp_len);
353     if (tmp_len > 128)
354       tmp = NULL;
355
356     /* Update statistics */
357     server->stat.clients--;
358     if (server->stat.cell_clients)
359       server->stat.cell_clients--;
360     SILC_OPER_STATS_UPDATE(client, server, SILC_UMODE_SERVER_OPERATOR);
361     SILC_OPER_STATS_UPDATE(client, router, SILC_UMODE_ROUTER_OPERATOR);
362     silc_schedule_task_del_by_context(server->schedule, client);
363
364     /* Remove the client from all channels. */
365     silc_server_remove_from_channels(server, NULL, client, TRUE, tmp, FALSE);
366
367     /* Check if anyone is watching this nickname */
368     if (server->server_type == SILC_ROUTER)
369       silc_server_check_watcher_list(server, client, NULL,
370                                      SILC_NOTIFY_TYPE_SIGNOFF);
371
372     /* Remove this client from watcher list if it is */
373     silc_server_del_from_watcher_list(server, client);
374
375     client->data.status &= ~SILC_IDLIST_STATUS_REGISTERED;
376     cache->expire = SILC_ID_CACHE_EXPIRE_DEF;
377     break;
378
379   case SILC_NOTIFY_TYPE_TOPIC_SET:
380     /* 
381      * Distribute the notify to local clients on the channel
382      */
383
384     SILC_LOG_DEBUG(("TOPIC SET notify"));
385
386     /* Get client ID */
387     tmp = silc_argument_get_arg_type(args, 1, &tmp_len);
388     if (!tmp)
389       goto out;
390     client_id = silc_id_payload_parse_id(tmp, tmp_len, &id_type);
391     if (!client_id)
392       goto out;
393
394     /* Get client entry */
395     if (id_type == SILC_ID_CLIENT) {
396       client = silc_idlist_find_client_by_id(server->global_list, 
397                                              client_id, TRUE, &cache);
398       if (!client) {
399         client = silc_idlist_find_client_by_id(server->local_list, 
400                                                client_id, TRUE, &cache);
401         if (!client) {
402           silc_free(client_id);
403           goto out;
404         }
405       }
406       silc_free(client_id);
407     }
408
409     /* Get the topic */
410     tmp = silc_argument_get_arg_type(args, 2, &tmp_len);
411     if (!tmp) {
412       silc_free(channel_id);
413       goto out;
414     }
415
416     if (!channel_id) {
417       channel_id = silc_id_str2id(packet->dst_id, packet->dst_id_len,
418                                   packet->dst_id_type);
419       if (!channel_id)
420         goto out;
421     }
422
423     /* Get channel entry */
424     channel = silc_idlist_find_channel_by_id(server->global_list, 
425                                              channel_id, NULL);
426     if (!channel) {
427       channel = silc_idlist_find_channel_by_id(server->local_list, 
428                                                channel_id, NULL);
429       if (!channel) {
430         SILC_LOG_DEBUG(("Notify for unknown channel"));
431         silc_free(channel_id);
432         goto out;
433       }
434     }
435     silc_free(channel_id);
436
437     if (channel->topic && !strcmp(channel->topic, tmp)) {
438       SILC_LOG_DEBUG(("Topic is already set and same"));
439       goto out;
440     }
441
442     if (client) {
443       /* Get user's channel entry and check that topic set is allowed. */
444       if (!silc_server_client_on_channel(client, channel, &chl))
445         goto out;
446       if (channel->mode & SILC_CHANNEL_MODE_TOPIC &&
447           !(chl->mode & SILC_CHANNEL_UMODE_CHANOP) &&
448           !(chl->mode & SILC_CHANNEL_UMODE_CHANFO)) {
449         SILC_LOG_DEBUG(("Topic change is not allowed"));
450         goto out;
451       }
452     }
453
454     /* Change the topic */
455     silc_free(channel->topic);
456     channel->topic = strdup(tmp);
457
458     /* Send the same notify to the channel */
459     silc_server_packet_send_to_channel(server, NULL, channel, packet->type, 
460                                        FALSE, packet->buffer->data, 
461                                        packet->buffer->len, FALSE);
462     break;
463
464   case SILC_NOTIFY_TYPE_NICK_CHANGE:
465     {
466       /* 
467        * Distribute the notify to local clients on the channel
468        */
469       unsigned char *id, *id2;
470       char *nickname;
471       SilcUInt32 nickname_len;
472
473       SILC_LOG_DEBUG(("NICK CHANGE notify"));
474       
475       /* Get old client ID */
476       id = silc_argument_get_arg_type(args, 1, &tmp_len);
477       if (!id)
478         goto out;
479       client_id = silc_id_payload_parse_id(id, tmp_len, NULL);
480       if (!client_id)
481         goto out;
482       
483       /* Get new client ID */
484       id2 = silc_argument_get_arg_type(args, 2, &tmp_len);
485       if (!id2)
486         goto out;
487       client_id2 = silc_id_payload_parse_id(id2, tmp_len, NULL);
488       if (!client_id2) {
489         silc_free(client_id);
490         goto out;
491       }
492       
493       SILC_LOG_DEBUG(("Old Client ID id(%s)", 
494                       silc_id_render(client_id, SILC_ID_CLIENT)));
495       SILC_LOG_DEBUG(("New Client ID id(%s)", 
496                       silc_id_render(client_id2, SILC_ID_CLIENT)));
497
498       /* From protocol version 1.1 we also get the new nickname */
499       nickname = silc_argument_get_arg_type(args, 3, &nickname_len);;
500
501       /* Replace the Client ID */
502       client = silc_idlist_replace_client_id(server,
503                                              server->global_list, client_id,
504                                              client_id2, nickname);
505       if (!client)
506         client = silc_idlist_replace_client_id(server,
507                                                server->local_list, client_id, 
508                                                client_id2, nickname);
509
510       if (client) {
511         /* Send the NICK_CHANGE notify type to local clients on the channels
512            this client is joined to. */
513         silc_server_send_notify_on_channels(server, client, client,
514                                             SILC_NOTIFY_TYPE_NICK_CHANGE, 3,
515                                             id, tmp_len, id2, tmp_len,
516                                             nickname, nickname ?
517                                             nickname_len : 0);
518       }
519
520       silc_free(client_id);
521       if (!client)
522         silc_free(client_id2);
523       break;
524     }
525
526   case SILC_NOTIFY_TYPE_CMODE_CHANGE:
527     /* 
528      * Distribute the notify to local clients on the channel
529      */
530
531     SILC_LOG_DEBUG(("CMODE CHANGE notify"));
532
533     /* Get client ID */
534     tmp = silc_argument_get_arg_type(args, 1, &tmp_len);
535     if (!tmp)
536       goto out;
537     client_id = silc_id_payload_parse_id(tmp, tmp_len, &id_type);
538     if (!client_id)
539       goto out;
540
541     /* Get client entry */
542     if (id_type == SILC_ID_CLIENT) {
543       client = silc_idlist_find_client_by_id(server->global_list, 
544                                              client_id, TRUE, &cache);
545       if (!client) {
546         client = silc_idlist_find_client_by_id(server->local_list, 
547                                                client_id, TRUE, &cache);
548         if (!client) {
549           silc_free(client_id);
550           goto out;
551         }
552       }
553     }
554     silc_free(client_id);
555
556     if (!channel_id) {
557       channel_id = silc_id_str2id(packet->dst_id, packet->dst_id_len,
558                                   packet->dst_id_type);
559       if (!channel_id)
560         goto out;
561     }
562
563     /* Get channel entry */
564     channel = silc_idlist_find_channel_by_id(server->global_list, 
565                                              channel_id, NULL);
566     if (!channel) {
567       channel = silc_idlist_find_channel_by_id(server->local_list, 
568                                                channel_id, NULL);
569       if (!channel) {
570         SILC_LOG_DEBUG(("Notify for unknown channel"));
571         silc_free(channel_id);
572         goto out;
573       }
574     }
575     silc_free(channel_id);
576
577     /* Get the mode */
578     tmp = silc_argument_get_arg_type(args, 2, &tmp_len);
579     if (!tmp)
580       goto out;
581     SILC_GET32_MSB(mode, tmp);
582
583     /* Check if mode changed */
584     if (channel->mode == mode) {
585       SILC_LOG_DEBUG(("Mode is changed already"));
586
587       /* If this mode change has founder mode then we'll enforce the
588          change so that the server gets the real founder public key */
589       if (server->server_type != SILC_SERVER &&
590           sock != SILC_PRIMARY_ROUTE(server) &&
591           mode & SILC_CHANNEL_MODE_FOUNDER_AUTH && channel->founder_key) {
592         SILC_LOG_DEBUG(("Sending founder public key to server"));
593         silc_server_send_notify_cmode(server, sock, FALSE, channel,
594                                       channel->mode, server->id,
595                                       SILC_ID_SERVER, channel->cipher,
596                                       channel->hmac_name,
597                                       channel->passphrase,
598                                       channel->founder_key);
599       }
600
601       /* If we received same mode from our primary check whether founder
602          mode and key in the notify is set.  We update the founder key
603          here since we may have wrong one */
604       if (server->server_type == SILC_SERVER &&
605           sock == SILC_PRIMARY_ROUTE(server) &&
606           mode & SILC_CHANNEL_MODE_FOUNDER_AUTH) {
607         SILC_LOG_DEBUG(("Founder public key received from router"));
608         tmp = silc_argument_get_arg_type(args, 6, &tmp_len);
609         if (!tmp)
610           break;
611
612         if (channel->founder_key)
613           silc_pkcs_public_key_free(channel->founder_key);
614         channel->founder_key = NULL;
615         silc_pkcs_public_key_payload_decode(tmp, tmp_len,
616                                             &channel->founder_key);
617       }
618
619       break;
620     }
621
622     /* Get user's channel entry and check that mode change is allowed */
623     if (client) {
624       if (!silc_server_client_on_channel(client, channel, &chl))
625         goto out;
626       if (!silc_server_check_cmode_rights(server, channel, chl, mode)) {
627         SILC_LOG_DEBUG(("CMODE change is not allowed"));
628         silc_server_send_notify_cmode(server, sock, FALSE, channel,
629                                       channel->mode, server->id,
630                                       SILC_ID_SERVER, channel->cipher,
631                                       channel->hmac_name,
632                                       channel->passphrase,
633                                       channel->founder_key);
634         goto out;
635       }
636     } else {
637       /* Assure that server is not removing founder mode from us */
638       if (server->server_type == SILC_ROUTER &&
639           sock != SILC_PRIMARY_ROUTE(server) &&
640           channel->mode & SILC_CHANNEL_MODE_FOUNDER_AUTH &&
641           !(mode & SILC_CHANNEL_MODE_FOUNDER_AUTH)) {
642         SILC_LOG_DEBUG(("Enforcing sender to change channel mode"));
643         silc_server_send_notify_cmode(server, sock, FALSE, channel,
644                                       channel->mode, server->id,
645                                       SILC_ID_SERVER, channel->cipher,
646                                       channel->hmac_name,
647                                       channel->passphrase,
648                                       channel->founder_key);
649         goto out;
650       }
651
652       /* If server is adding founder mode, check whether there is founder
653          on channel already and is not from this server */
654       if (server->server_type == SILC_ROUTER &&
655           sock != SILC_PRIMARY_ROUTE(server) &&
656           mode & SILC_CHANNEL_MODE_FOUNDER_AUTH) {
657         silc_hash_table_list(channel->user_list, &htl);
658         while (silc_hash_table_get(&htl, NULL, (void *)&chl))
659           if (chl->mode & SILC_CHANNEL_UMODE_CHANFO &&
660               chl->client->router != sock->user_data) {
661             SILC_LOG_DEBUG(("Enforcing sender to change channel mode"));
662             silc_server_send_notify_cmode(server, sock, FALSE, channel,
663                                           channel->mode, server->id,
664                                           SILC_ID_SERVER, channel->cipher,
665                                           channel->hmac_name,
666                                           channel->passphrase,
667                                           channel->founder_key);
668             silc_hash_table_list_reset(&htl);
669             goto out;
670           }
671         silc_hash_table_list_reset(&htl);
672       }
673     }
674
675     /* If the channel had private keys set and the mode was removed then
676        we must re-generate and re-distribute a new channel key */
677     if (channel->mode & SILC_CHANNEL_MODE_PRIVKEY &&
678         !(mode & SILC_CHANNEL_MODE_PRIVKEY)) {
679       /* Re-generate channel key */
680       if (!silc_server_create_channel_key(server, channel, 0))
681         goto out;
682       
683       /* Send the channel key. This sends it to our local clients and if
684          we are normal server to our router as well. */
685       silc_server_send_channel_key(server, NULL, channel, 
686                                    server->server_type == SILC_ROUTER ? 
687                                    FALSE : !server->standalone);
688     }
689
690     /* Get the hmac */
691     tmp = silc_argument_get_arg_type(args, 4, &tmp_len);
692     if (tmp) {
693       unsigned char hash[32];
694
695       if (channel->hmac)
696         silc_hmac_free(channel->hmac);
697       if (!silc_hmac_alloc(tmp, NULL, &channel->hmac))
698         goto out;
699
700       /* Set the HMAC key out of current channel key. The client must do
701          this locally. */
702       silc_hash_make(silc_hmac_get_hash(channel->hmac), channel->key, 
703                      channel->key_len / 8, hash);
704       silc_hmac_set_key(channel->hmac, hash, 
705                         silc_hash_len(silc_hmac_get_hash(channel->hmac)));
706       memset(hash, 0, sizeof(hash));
707     }
708
709     /* Get the passphrase */
710     tmp = silc_argument_get_arg_type(args, 5, &tmp_len);
711     if (tmp) {
712       silc_free(channel->passphrase);
713       channel->passphrase = silc_memdup(tmp, tmp_len);
714     }
715
716     /* Get founder public key */
717     tmp = silc_argument_get_arg_type(args, 6, &tmp_len);
718     if (tmp && mode & SILC_CHANNEL_MODE_FOUNDER_AUTH) {
719       if (channel->founder_key)
720         silc_pkcs_public_key_free(channel->founder_key);
721       channel->founder_key = NULL;
722       silc_pkcs_public_key_payload_decode(tmp, tmp_len, &channel->founder_key);
723
724       if (!channel->founder_key || 
725           (client && client->data.public_key && 
726            server->server_type == SILC_ROUTER &&
727            !silc_pkcs_public_key_compare(channel->founder_key,
728                                          client->data.public_key))) {
729         /* A really buggy server isn't checking public keys correctly.
730            It's not possible that the mode setter and founder wouldn't
731            have same public key. */
732         SILC_LOG_DEBUG(("Enforcing sender to change channel mode"));
733
734         mode &= ~SILC_CHANNEL_MODE_FOUNDER_AUTH;
735         silc_server_send_notify_cmode(server, sock, FALSE, channel,
736                                       mode, server->id, SILC_ID_SERVER,
737                                       channel->cipher, 
738                                       channel->hmac_name,
739                                       channel->passphrase, NULL);
740         if (channel->founder_key)
741           silc_pkcs_public_key_free(channel->founder_key);
742         channel->founder_key = NULL;
743       } else if (client && !client->data.public_key) {
744         client->data.public_key = 
745           silc_pkcs_public_key_copy(channel->founder_key);
746       }
747     }
748
749     if (mode & SILC_CHANNEL_MODE_FOUNDER_AUTH && !channel->founder_key &&
750         server->server_type == SILC_ROUTER) {
751       SILC_LOG_DEBUG(("Enforcing sender to change channel mode"));
752       mode &= ~SILC_CHANNEL_MODE_FOUNDER_AUTH;
753       silc_server_send_notify_cmode(server, sock, FALSE, channel,
754                                     mode, server->id, SILC_ID_SERVER,
755                                     channel->cipher, 
756                                     channel->hmac_name,
757                                     channel->passphrase, NULL);
758     }
759
760     /* Send the same notify to the channel */
761     silc_server_packet_send_to_channel(server, NULL, channel, packet->type, 
762                                        FALSE, packet->buffer->data, 
763                                        packet->buffer->len, FALSE);
764
765     /* Change mode */
766     channel->mode = mode;
767
768     if (!(channel->mode & SILC_CHANNEL_MODE_FOUNDER_AUTH) &&
769         channel->founder_key) {
770       silc_pkcs_public_key_free(channel->founder_key);
771       channel->founder_key = NULL;
772     }
773
774     break;
775
776   case SILC_NOTIFY_TYPE_CUMODE_CHANGE:
777     {
778       /* 
779        * Distribute the notify to local clients on the channel
780        */
781       SilcChannelClientEntry chl2 = NULL;
782       bool notify_sent = FALSE;
783
784       SILC_LOG_DEBUG(("CUMODE CHANGE notify"));
785
786       /* Get client ID */
787       tmp = silc_argument_get_arg_type(args, 1, &tmp_len);
788       if (!tmp)
789         goto out;
790       client_id = silc_id_payload_parse_id(tmp, tmp_len, &id_type);
791       if (!client_id)
792         goto out;
793
794       /* Get client entry */
795       if (id_type == SILC_ID_CLIENT) {
796         client = silc_idlist_find_client_by_id(server->global_list, 
797                                                client_id, TRUE, &cache);
798         if (!client) {
799           client = silc_idlist_find_client_by_id(server->local_list, 
800                                                  client_id, TRUE, &cache);
801           if (!client) {
802             silc_free(client_id);
803             goto out;
804           }
805         }
806       }
807       silc_free(client_id);
808
809       if (!channel_id) {
810         channel_id = silc_id_str2id(packet->dst_id, packet->dst_id_len,
811                                     packet->dst_id_type);
812         if (!channel_id)
813           goto out;
814       }
815
816       /* Get channel entry */
817       channel = silc_idlist_find_channel_by_id(server->global_list, 
818                                                channel_id, NULL);
819       if (!channel) {
820         channel = silc_idlist_find_channel_by_id(server->local_list, 
821                                                  channel_id, NULL);
822         if (!channel) {
823           SILC_LOG_DEBUG(("Notify for unknown channel"));
824           silc_free(channel_id);
825           goto out;
826         }
827       }
828
829       /* Get the mode */
830       tmp = silc_argument_get_arg_type(args, 2, &tmp_len);
831       if (!tmp) {
832         silc_free(channel_id);
833         goto out;
834       }
835       
836       SILC_GET32_MSB(mode, tmp);
837       
838       /* Get target client */
839       tmp = silc_argument_get_arg_type(args, 3, &tmp_len);
840       if (!tmp)
841         goto out;
842       client_id = silc_id_payload_parse_id(tmp, tmp_len, NULL);
843       if (!client_id)
844         goto out;
845       
846       /* Get client entry */
847       client2 = silc_idlist_find_client_by_id(server->global_list, 
848                                               client_id, TRUE, NULL);
849       if (!client2) {
850         client2 = silc_idlist_find_client_by_id(server->local_list, 
851                                                 client_id, TRUE, NULL);
852         if (!client2) {
853           silc_free(client_id);
854           goto out;
855         }
856       }
857       silc_free(client_id);
858
859       if (client) {
860         /* Check that sender is on channel */
861         if (!silc_server_client_on_channel(client, channel, &chl))
862           goto out;
863         
864         if (client != client2 && server->server_type == SILC_ROUTER) {
865           /* Sender must be operator */
866           if (!(chl->mode & SILC_CHANNEL_UMODE_CHANOP) &&
867               !(chl->mode & SILC_CHANNEL_UMODE_CHANFO)) {
868             SILC_LOG_DEBUG(("CUMODE change is not allowed"));
869             goto out;
870           }
871
872           if (!silc_server_client_on_channel(client2, channel, &chl))
873             goto out;
874
875           /* If target is founder mode change is not allowed. */
876           if (chl->mode & SILC_CHANNEL_UMODE_CHANFO) {
877             SILC_LOG_DEBUG(("CUMODE change is not allowed"));
878             goto out;
879           }
880         }
881       }
882
883       /* Get target channel user entry */
884       if (!silc_server_client_on_channel(client2, channel, &chl))
885         goto out;
886
887       if (server->server_type == SILC_SERVER && chl->mode == mode) {
888         SILC_LOG_DEBUG(("Mode is changed already"));
889         break;
890       }
891
892       if (mode & SILC_CHANNEL_UMODE_CHANFO &&
893           !(chl->mode & SILC_CHANNEL_UMODE_CHANFO) &&
894           server->server_type == SILC_ROUTER &&
895           sock != SILC_PRIMARY_ROUTE(server)) {
896         SilcPublicKey founder_key = NULL;
897
898         /* If channel doesn't have founder auth mode then it's impossible
899            that someone would be getting founder rights with CUMODE command.
900            In that case there already either is founder or there isn't
901            founder at all on the channel. */
902         if (client && !(channel->mode & SILC_CHANNEL_MODE_FOUNDER_AUTH)) {
903           /* Force the mode to not have founder mode */
904           chl->mode = mode &= ~SILC_CHANNEL_UMODE_CHANFO;
905           silc_server_force_cumode_change(server, sock, channel, chl, mode);
906           notify_sent = TRUE;
907           break;
908         }
909
910         /* Get the founder of the channel and if found then this client
911            cannot be the founder since there already is one. */
912         silc_hash_table_list(channel->user_list, &htl);
913         while (silc_hash_table_get(&htl, NULL, (void *)&chl2))
914           if (chl2->mode & SILC_CHANNEL_UMODE_CHANFO) {
915             /* If the founder on the channel is not the one whom has set
916                the founder mode, then it's possible that this CUMODE_CHANGE
917                is correct.  Due to netsplits it's possible that this
918                situation happens. */
919             if (!(channel->mode & SILC_CHANNEL_MODE_FOUNDER_AUTH) ||
920                 (channel->founder_key && chl2->client->data.public_key &&
921                  silc_pkcs_public_key_compare(
922                                         channel->founder_key,
923                                         chl2->client->data.public_key))) {
924               chl->mode = mode &= ~SILC_CHANNEL_UMODE_CHANFO;
925               silc_server_force_cumode_change(server, sock, channel,
926                                               chl, mode);
927               notify_sent = TRUE;
928             }
929             break;
930           }
931         silc_hash_table_list_reset(&htl);
932         if (!(mode & SILC_CHANNEL_UMODE_CHANFO))
933           break;
934
935         /* Founder not found of the channel.  Since the founder auth mode
936            is set on the channel now check whether this is the client that
937            originally set the mode. */
938
939         if (channel->founder_key) {
940           /* Get public key that must be present in notify */
941           tmp = silc_argument_get_arg_type(args, 4, &tmp_len);
942           if (!tmp || !silc_pkcs_public_key_payload_decode(tmp, tmp_len,
943                                                            &founder_key)) {
944             chl->mode = mode &= ~SILC_CHANNEL_UMODE_CHANFO;
945             silc_server_force_cumode_change(server, sock, channel, chl, mode);
946             notify_sent = TRUE;
947             break;
948           }
949
950           /* Now match the public key we have cached and public key sent.
951              They must match. */
952 #if 0 /* The key may be other than the client's in 1.2 */
953           if (client && client->data.public_key && 
954               !silc_pkcs_public_key_compare(channel->founder_key,
955                                             client->data.public_key)) {
956             chl->mode = mode &= ~SILC_CHANNEL_UMODE_CHANFO;
957             silc_server_force_cumode_change(server, sock, channel, chl, mode);
958             notify_sent = TRUE;
959             break;
960           }
961 #endif
962           if (!silc_pkcs_public_key_compare(channel->founder_key,
963                                             founder_key)) {
964             chl->mode = mode &= ~SILC_CHANNEL_UMODE_CHANFO;
965             silc_server_force_cumode_change(server, sock, channel, chl, mode);
966             notify_sent = TRUE;
967             break;
968           }
969         }
970
971         /* There cannot be anyone else as founder on the channel now.  This
972            client is definitely the founder due to this authentication */
973         silc_hash_table_list(channel->user_list, &htl);
974         while (silc_hash_table_get(&htl, NULL, (void *)&chl2))
975           if (chl2->mode & SILC_CHANNEL_UMODE_CHANFO) {
976             chl2->mode &= ~SILC_CHANNEL_UMODE_CHANFO;
977             silc_server_force_cumode_change(server, NULL, channel, chl2,
978                                             chl2->mode);
979             break;
980           }
981         silc_hash_table_list_reset(&htl);
982
983         if (founder_key)
984           silc_pkcs_public_key_free(founder_key);
985       }
986
987       if (server->server_type != SILC_SERVER && chl->mode == mode) {
988         SILC_LOG_DEBUG(("Mode is changed already"));
989         break;
990       }
991
992       SILC_LOG_DEBUG(("Changing %s channel user mode",
993                       chl->client->nickname ? chl->client->nickname :
994                       (unsigned char *)""));
995
996       /* Change the mode */
997       chl->mode = mode;
998
999       /* Send the same notify to the channel */
1000       if (!notify_sent)
1001         silc_server_packet_send_to_channel(server, NULL, channel,
1002                                            packet->type,
1003                                            FALSE, packet->buffer->data,
1004                                            packet->buffer->len, FALSE);
1005       
1006       silc_free(channel_id);
1007       break;
1008     }
1009
1010   case SILC_NOTIFY_TYPE_INVITE:
1011
1012     if (packet->dst_id_type == SILC_ID_CLIENT)
1013       goto out;
1014
1015     SILC_LOG_DEBUG(("INVITE notify"));
1016
1017     /* Get Channel ID */
1018     tmp = silc_argument_get_arg_type(args, 1, &tmp_len);
1019     if (!tmp)
1020       goto out;
1021     channel_id = silc_id_payload_parse_id(tmp, tmp_len, NULL);
1022     if (!channel_id)
1023       goto out;
1024
1025     /* Get channel entry */
1026     channel = silc_idlist_find_channel_by_id(server->global_list, 
1027                                              channel_id, NULL);
1028     if (!channel) {
1029       channel = silc_idlist_find_channel_by_id(server->local_list, 
1030                                                channel_id, NULL);
1031       if (!channel) {
1032         SILC_LOG_DEBUG(("Notify for unknown channel"));
1033         silc_free(channel_id);
1034         goto out;
1035       }
1036     }
1037     silc_free(channel_id);
1038
1039     /* Get client ID */
1040     tmp = silc_argument_get_arg_type(args, 3, &tmp_len);
1041     if (!tmp)
1042       goto out;
1043     client_id = silc_id_payload_parse_id(tmp, tmp_len, NULL);
1044     if (!client_id)
1045       goto out;
1046
1047     /* Get client entry */
1048     client = silc_idlist_find_client_by_id(server->global_list, 
1049                                            client_id, TRUE, &cache);
1050     if (!client) {
1051       client = silc_idlist_find_client_by_id(server->local_list, 
1052                                              client_id, TRUE, &cache);
1053       if (!client) {
1054         silc_free(client_id);
1055         goto out;
1056       }
1057     }
1058     silc_free(client_id);
1059
1060     /* Get user's channel entry and check that inviting is allowed. */
1061     if (!silc_server_client_on_channel(client, channel, &chl))
1062       goto out;
1063     if (channel->mode & SILC_CHANNEL_MODE_INVITE &&
1064         !(chl->mode & SILC_CHANNEL_UMODE_CHANOP) &&
1065         !(chl->mode & SILC_CHANNEL_UMODE_CHANFO)) {
1066       SILC_LOG_DEBUG(("Inviting is not allowed"));
1067       goto out;
1068     }
1069
1070     /* Get the invite action */
1071     tmp = silc_argument_get_arg_type(args, 4, &tmp_len);
1072     if (tmp && tmp_len == 1) {
1073       SilcUInt8 action = (SilcUInt8)tmp[0];
1074       SilcUInt16 iargc = 0;
1075       SilcArgumentPayload iargs;
1076
1077       /* Get invite list */
1078       tmp = silc_argument_get_arg_type(args, 5, &tmp_len);
1079       if (!tmp || tmp_len < 2)
1080         goto out;
1081
1082       /* Parse the arguments to see they are constructed correctly */
1083       SILC_GET16_MSB(iargc, tmp);
1084       iargs = silc_argument_payload_parse(tmp + 2, tmp_len - 2, iargc);
1085       if (!iargs)
1086         goto out;
1087
1088       if (action == 0 && !channel->invite_list)
1089         channel->invite_list = silc_hash_table_alloc(0, silc_hash_ptr,
1090                                                      NULL, NULL, NULL,
1091                                                      NULL, NULL, TRUE);
1092
1093       /* Proces the invite action */
1094       silc_server_inviteban_process(server, channel->invite_list, action,
1095                                     iargs);
1096       silc_argument_payload_free(iargs);
1097     }
1098
1099     break;
1100
1101   case SILC_NOTIFY_TYPE_CHANNEL_CHANGE:
1102     /*
1103      * Distribute to the local clients on the channel and change the
1104      * channel ID.
1105      */
1106
1107     SILC_LOG_DEBUG(("CHANNEL CHANGE"));
1108
1109     if (sock->type != SILC_SOCKET_TYPE_ROUTER)
1110       break;
1111
1112     /* Get the old Channel ID */
1113     tmp = silc_argument_get_arg_type(args, 1, &tmp_len);
1114     if (!tmp)
1115       goto out;
1116     channel_id = silc_id_payload_parse_id(tmp, tmp_len, NULL);
1117     if (!channel_id)
1118       goto out;
1119
1120     /* Get the channel entry */
1121     channel = silc_idlist_find_channel_by_id(server->local_list, 
1122                                              channel_id, NULL);
1123     if (!channel) {
1124       channel = silc_idlist_find_channel_by_id(server->global_list, 
1125                                                channel_id, NULL);
1126       if (!channel) {
1127         SILC_LOG_DEBUG(("Notify for unknown channel"));
1128         silc_free(channel_id);
1129         goto out;
1130       }
1131     }
1132
1133     /* Send the notify to the channel */
1134     silc_server_packet_send_to_channel(server, sock, channel, packet->type, 
1135                                        FALSE, packet->buffer->data, 
1136                                        packet->buffer->len, FALSE);
1137
1138     /* Get the new Channel ID */
1139     tmp = silc_argument_get_arg_type(args, 2, &tmp_len);
1140     if (!tmp)
1141       goto out;
1142     channel_id2 = silc_id_payload_parse_id(tmp, tmp_len, NULL);
1143     if (!channel_id2)
1144       goto out;
1145
1146     SILC_LOG_DEBUG(("Old Channel ID id(%s)", 
1147                     silc_id_render(channel_id, SILC_ID_CHANNEL)));
1148     SILC_LOG_DEBUG(("New Channel ID id(%s)", 
1149                     silc_id_render(channel_id2, SILC_ID_CHANNEL)));
1150
1151     /* Replace the Channel ID */
1152     if (!silc_idlist_replace_channel_id(server->local_list, channel_id,
1153                                         channel_id2))
1154       if (!silc_idlist_replace_channel_id(server->global_list, channel_id,
1155                                           channel_id2)) {
1156         silc_free(channel_id2);
1157         channel_id2 = NULL;
1158       }
1159
1160     if (channel_id2) {
1161       SilcBuffer modes = NULL, users = NULL, users_modes = NULL;
1162
1163       /* Re-announce this channel which ID was changed. */
1164       silc_server_send_new_channel(server, sock, FALSE, channel->channel_name,
1165                                    channel->id, 
1166                                    silc_id_get_len(channel->id, 
1167                                                    SILC_ID_CHANNEL),
1168                                    channel->mode);
1169
1170       /* Re-announce our clients on the channel as the ID has changed now */
1171       silc_server_announce_get_channel_users(server, channel, &modes, &users,
1172                                              &users_modes);
1173       if (users) {
1174         silc_buffer_push(users, users->data - users->head);
1175         silc_server_packet_send(server, sock,
1176                                 SILC_PACKET_NOTIFY, SILC_PACKET_FLAG_LIST,
1177                                 users->data, users->len, FALSE);
1178         silc_buffer_free(users);
1179       }
1180       if (modes) {
1181         silc_buffer_push(modes, modes->data - modes->head);
1182         silc_server_packet_send_dest(server, sock,
1183                                      SILC_PACKET_NOTIFY, SILC_PACKET_FLAG_LIST,
1184                                      channel->id, SILC_ID_CHANNEL,
1185                                      modes->data, modes->len, FALSE);
1186         silc_buffer_free(modes);
1187       }
1188       if (users_modes) {
1189         silc_buffer_push(users_modes, users_modes->data - users_modes->head);
1190         silc_server_packet_send_dest(server, sock,
1191                                      SILC_PACKET_NOTIFY, SILC_PACKET_FLAG_LIST,
1192                                      channel->id, SILC_ID_CHANNEL,
1193                                      users_modes->data, 
1194                                      users_modes->len, FALSE);
1195         silc_buffer_free(users_modes);
1196       }
1197
1198       /* Re-announce channel's topic */
1199       if (channel->topic) {
1200         silc_server_send_notify_topic_set(server, sock,
1201                                           server->server_type == SILC_ROUTER ?
1202                                           TRUE : FALSE, channel, 
1203                                           server->id, SILC_ID_SERVER,
1204                                           channel->topic);
1205       }
1206     }
1207
1208     silc_free(channel_id);
1209
1210     break;
1211
1212   case SILC_NOTIFY_TYPE_SERVER_SIGNOFF:
1213     /* 
1214      * Remove the server entry and all clients that this server owns.
1215      */
1216
1217     SILC_LOG_DEBUG(("SERVER SIGNOFF notify"));
1218
1219     /* Get Server ID */
1220     tmp = silc_argument_get_arg_type(args, 1, &tmp_len);
1221     if (!tmp)
1222       goto out;
1223     server_id = silc_id_payload_parse_id(tmp, tmp_len, NULL);
1224     if (!server_id)
1225       goto out;
1226
1227     /* If the ID is mine, this notify is not allowed. */
1228     if (SILC_ID_SERVER_COMPARE(server_id, server->id)) {
1229       SILC_LOG_DEBUG(("Ignoring my own ID for SERVER_SIGNOFF"));
1230       break;
1231     }
1232
1233     /* Get server entry */
1234     server_entry = silc_idlist_find_server_by_id(server->global_list, 
1235                                                  server_id, TRUE, NULL);
1236     local = FALSE;
1237     if (!server_entry) {
1238       server_entry = silc_idlist_find_server_by_id(server->local_list, 
1239                                                    server_id, TRUE, NULL);
1240       local = TRUE;
1241       if (!server_entry) {
1242         /* If we are normal server then we might not have the server. Check
1243            whether router was kind enough to send the list of all clients
1244            that actually was to be removed. Remove them if the list is
1245            available. */
1246         if (server->server_type != SILC_ROUTER &&
1247             silc_argument_get_arg_num(args) > 1) {
1248           int i;
1249
1250           for (i = 1; i < silc_argument_get_arg_num(args); i++) {
1251             /* Get Client ID */
1252             tmp = silc_argument_get_arg_type(args, i + 1, &tmp_len);
1253             if (!tmp)
1254               continue;
1255             client_id = silc_id_payload_parse_id(tmp, tmp_len, NULL);
1256             if (!client_id)
1257               continue;
1258
1259             /* Get client entry */
1260             client = silc_idlist_find_client_by_id(server->global_list, 
1261                                                    client_id, TRUE, &cache);
1262             local = TRUE;
1263             if (!client) {
1264               client = silc_idlist_find_client_by_id(server->local_list, 
1265                                                      client_id, TRUE, &cache);
1266               local = FALSE;
1267               if (!client) {
1268                 silc_free(client_id);
1269                 continue;
1270               }
1271             }
1272             silc_free(client_id);
1273
1274             /* Update statistics */
1275             server->stat.clients--;
1276             if (server->stat.cell_clients)
1277               server->stat.cell_clients--;
1278             SILC_OPER_STATS_UPDATE(client, server, SILC_UMODE_SERVER_OPERATOR);
1279             SILC_OPER_STATS_UPDATE(client, router, SILC_UMODE_ROUTER_OPERATOR);
1280
1281             /* Remove the client from all channels. */
1282             silc_server_remove_from_channels(server, NULL, client, 
1283                                              TRUE, NULL, FALSE);
1284
1285             /* Check if anyone is watching this nickname */
1286             if (server->server_type == SILC_ROUTER)
1287               silc_server_check_watcher_list(server, client, NULL,
1288                                              SILC_NOTIFY_TYPE_SERVER_SIGNOFF);
1289
1290             /* Remove this client from watcher list if it is */
1291             if (local)
1292               silc_server_del_from_watcher_list(server, client);
1293
1294             /* Remove the client */
1295             silc_idlist_del_data(client);
1296             silc_idlist_del_client(local ? server->local_list :
1297                                    server->global_list, client);
1298           }
1299         }
1300
1301         silc_free(server_id);
1302         goto out;
1303       }
1304     }
1305     silc_free(server_id);
1306
1307     /* Sending SERVER_SIGNOFF is not right way to signoff local connection */
1308     if (SILC_IS_LOCAL(server_entry))
1309       break;
1310
1311     /* Remove all servers that are originated from this server, and
1312        remove the clients of those servers too. */
1313     silc_server_remove_servers_by_server(server, server_entry, TRUE);
1314
1315     /* Remove the clients that this server owns as they will become
1316        invalid now too. */
1317     silc_server_remove_clients_by_server(server, server_entry->router,
1318                                          server_entry, TRUE);
1319     silc_server_backup_del(server, server_entry);
1320
1321     /* Remove the server entry */
1322     silc_idlist_del_server(local ? server->local_list :
1323                            server->global_list, server_entry);
1324
1325     /* Update statistics */
1326     if (server->server_type == SILC_ROUTER)
1327       server->stat.servers--;
1328
1329     break;
1330
1331   case SILC_NOTIFY_TYPE_KICKED:
1332     /* 
1333      * Distribute the notify to local clients on the channel
1334      */
1335     
1336     SILC_LOG_DEBUG(("KICKED notify"));
1337       
1338     if (!channel_id) {
1339       channel_id = silc_id_str2id(packet->dst_id, packet->dst_id_len,
1340                                   packet->dst_id_type);
1341       if (!channel_id)
1342         goto out;
1343     }
1344
1345     /* Get channel entry */
1346     channel = silc_idlist_find_channel_by_id(server->global_list, 
1347                                              channel_id, NULL);
1348     if (!channel) {
1349       channel = silc_idlist_find_channel_by_id(server->local_list, 
1350                                                channel_id, NULL);
1351       if (!channel) {
1352         SILC_LOG_DEBUG(("Notify for unknown channel"));
1353         silc_free(channel_id);
1354         goto out;
1355       }
1356     }
1357     silc_free(channel_id);
1358
1359     /* Get client ID */
1360     tmp = silc_argument_get_arg_type(args, 1, &tmp_len);
1361     if (!tmp)
1362       goto out;
1363     client_id = silc_id_payload_parse_id(tmp, tmp_len, NULL);
1364     if (!client_id)
1365       goto out;
1366
1367     /* If the the client is not in local list we check global list */
1368     client = silc_idlist_find_client_by_id(server->global_list, 
1369                                            client_id, TRUE, NULL);
1370     if (!client) {
1371       client = silc_idlist_find_client_by_id(server->local_list, 
1372                                              client_id, TRUE, NULL);
1373       if (!client) {
1374         silc_free(client_id);
1375         goto out;
1376       }
1377     }
1378     silc_free(client_id);
1379
1380     /* If target is founder they cannot be kicked */
1381     if (!silc_server_client_on_channel(client, channel, &chl))
1382       goto out;
1383     if (chl->mode & SILC_CHANNEL_UMODE_CHANFO)
1384       goto out;
1385     
1386     /* Get the kicker's Client ID */
1387     tmp = silc_argument_get_arg_type(args, 3, &tmp_len);
1388     if (!tmp)
1389       goto out;
1390     client_id = silc_id_payload_parse_id(tmp, tmp_len, NULL);
1391     if (!client_id)
1392       goto out;
1393
1394     /* If the the client is not in local list we check global list */
1395     client2 = silc_idlist_find_client_by_id(server->global_list, 
1396                                             client_id, TRUE, NULL);
1397     if (!client2) {
1398       client2 = silc_idlist_find_client_by_id(server->local_list, 
1399                                               client_id, TRUE, NULL);
1400       if (!client2) {
1401         silc_free(client_id);
1402         goto out;
1403       }
1404     }
1405     silc_free(client_id);
1406
1407     /* Kicker must be operator on channel */
1408     if (!silc_server_client_on_channel(client2, channel, &chl))
1409       goto out;
1410     if (!(chl->mode & SILC_CHANNEL_UMODE_CHANOP) &&
1411         !(chl->mode & SILC_CHANNEL_UMODE_CHANFO)) {
1412       SILC_LOG_DEBUG(("Kicking is not allowed"));
1413       goto out;
1414     }
1415
1416     /* Send to channel */
1417     silc_server_packet_send_to_channel(server, sock, channel, packet->type, 
1418                                        FALSE, packet->buffer->data, 
1419                                        packet->buffer->len, FALSE);
1420
1421     /* Remove the client from channel's invite list */
1422     if (channel->invite_list && silc_hash_table_count(channel->invite_list)) {
1423       SilcBuffer ab;
1424       SilcArgumentPayload iargs;
1425       tmp = silc_argument_get_arg_type(args, 1, &tmp_len);
1426       ab = silc_argument_payload_encode_one(NULL, tmp, tmp_len, 3);
1427       iargs = silc_argument_payload_parse(ab->data, ab->len, 1);
1428       silc_server_inviteban_process(server, channel->invite_list, 1, iargs);
1429       silc_buffer_free(ab);
1430       silc_argument_payload_free(iargs);
1431     }
1432
1433     /* Remove the client from channel */
1434     silc_server_remove_from_one_channel(server, sock, channel, client, FALSE);
1435
1436     break;
1437
1438   case SILC_NOTIFY_TYPE_KILLED:
1439     {
1440       /* 
1441        * Distribute the notify to local clients on channels
1442        */
1443       unsigned char *id, *comment;
1444       SilcUInt32 id_len, comment_len;
1445     
1446       SILC_LOG_DEBUG(("KILLED notify"));
1447       
1448       /* Get client ID */
1449       id = silc_argument_get_arg_type(args, 1, &id_len);
1450       if (!id)
1451         goto out;
1452       client_id = silc_id_payload_parse_id(id, id_len, NULL);
1453       if (!client_id)
1454         goto out;
1455
1456       /* If the the client is not in local list we check global list */
1457       client = silc_idlist_find_client_by_id(server->global_list, 
1458                                              client_id, TRUE, &cache);
1459       if (!client) {
1460         client = silc_idlist_find_client_by_id(server->local_list, 
1461                                                client_id, TRUE, &cache);
1462         if (!client) {
1463           silc_free(client_id);
1464           goto out;
1465         }
1466       }
1467       silc_free(client_id);
1468
1469       /* If the client is one of ours, then close the connection to the
1470          client now. This removes the client from all channels as well. */
1471       if (packet->dst_id_type == SILC_ID_CLIENT && client->connection) {
1472         sock = client->connection;
1473         silc_server_free_client_data(server, NULL, client, FALSE, NULL);
1474         silc_server_close_connection(server, sock);
1475         break;
1476       }
1477
1478       /* Get comment */
1479       comment = silc_argument_get_arg_type(args, 2, &comment_len);
1480       if (comment_len > 128)
1481         comment_len = 127;
1482
1483       /* Get the killer's Client ID */
1484       tmp = silc_argument_get_arg_type(args, 3, &tmp_len);
1485       if (!tmp)
1486         goto out;
1487       client_id = silc_id_payload_parse_id(tmp, tmp_len, &id_type);
1488       if (!client_id)
1489         goto out;
1490
1491       if (id_type == SILC_ID_CLIENT) {
1492         /* If the the client is not in local list we check global list */
1493         client2 = silc_idlist_find_client_by_id(server->global_list, 
1494                                                 client_id, TRUE, NULL);
1495         if (!client2) {
1496           client2 = silc_idlist_find_client_by_id(server->local_list, 
1497                                                   client_id, TRUE, NULL);
1498           if (!client2) {
1499             silc_free(client_id);
1500             goto out;
1501           }
1502         }
1503         silc_free(client_id);
1504
1505         /* Killer must be router operator */
1506         if (server->server_type != SILC_SERVER &&
1507             !(client2->mode & SILC_UMODE_ROUTER_OPERATOR)) {
1508           SILC_LOG_DEBUG(("Killing is not allowed"));
1509           goto out;
1510         }
1511       }
1512
1513       /* Send the notify to local clients on the channels except to the
1514          client who is killed. */
1515       silc_server_send_notify_on_channels(server, client, client,
1516                                           SILC_NOTIFY_TYPE_KILLED, 3,
1517                                           id, id_len, comment, comment_len,
1518                                           tmp, tmp_len);
1519
1520       /* Remove the client from all channels */
1521       silc_server_remove_from_channels(server, NULL, client, FALSE, NULL, 
1522                                        FALSE);
1523
1524       /* Check if anyone is watching this nickname */
1525       silc_server_check_watcher_list(server, client, NULL,
1526                                      SILC_NOTIFY_TYPE_KILLED);
1527
1528       /* Update statistics */
1529       server->stat.clients--;
1530       if (server->stat.cell_clients)
1531         server->stat.cell_clients--;
1532       SILC_OPER_STATS_UPDATE(client, server, SILC_UMODE_SERVER_OPERATOR);
1533       SILC_OPER_STATS_UPDATE(client, router, SILC_UMODE_ROUTER_OPERATOR);
1534
1535       if (SILC_IS_LOCAL(client)) {
1536         server->stat.my_clients--;
1537         silc_schedule_task_del_by_context(server->schedule, client);
1538         silc_idlist_del_data(client);
1539         client->mode = 0;
1540       }
1541
1542       client->data.status &= ~SILC_IDLIST_STATUS_REGISTERED;
1543       cache->expire = SILC_ID_CACHE_EXPIRE_DEF;
1544       break;
1545     }
1546
1547   case SILC_NOTIFY_TYPE_UMODE_CHANGE:
1548     /*
1549      * Save the mode of the client.
1550      */
1551
1552     SILC_LOG_DEBUG(("UMODE_CHANGE notify"));
1553
1554     /* Get client ID */
1555     tmp = silc_argument_get_arg_type(args, 1, &tmp_len);
1556     if (!tmp)
1557       goto out;
1558     client_id = silc_id_payload_parse_id(tmp, tmp_len, NULL);
1559     if (!client_id)
1560       goto out;
1561
1562     /* Get client entry */
1563     client = silc_idlist_find_client_by_id(server->global_list, 
1564                                            client_id, TRUE, NULL);
1565     if (!client) {
1566       client = silc_idlist_find_client_by_id(server->local_list, 
1567                                              client_id, TRUE, NULL);
1568       if (!client) {
1569         silc_free(client_id);
1570         goto out;
1571       }
1572     }
1573     silc_free(client_id);
1574
1575     /* Get the mode */
1576     tmp = silc_argument_get_arg_type(args, 2, &tmp_len);
1577     if (!tmp)
1578       goto out;
1579     SILC_GET32_MSB(mode, tmp);
1580
1581     /* Remove internal resumed flag if client is marked detached now */
1582     if (mode & SILC_UMODE_DETACHED)
1583       client->data.status &= ~SILC_IDLIST_STATUS_RESUMED;
1584
1585     /* Update statistics */
1586     if (server->server_type == SILC_ROUTER) {
1587       if (mode & SILC_UMODE_GONE) {
1588         if (!(client->mode & SILC_UMODE_GONE))
1589           server->stat.aways++;
1590       } else {
1591         if (client->mode & SILC_UMODE_GONE)
1592           server->stat.aways--;
1593       }
1594       if (mode & SILC_UMODE_DETACHED) {
1595         if (!(client->mode & SILC_UMODE_DETACHED))
1596           server->stat.detached++;
1597       } else {
1598         if (client->mode & SILC_UMODE_DETACHED)
1599           server->stat.detached--;
1600       }
1601     }
1602     SILC_UMODE_STATS_UPDATE(server, SILC_UMODE_SERVER_OPERATOR);
1603     SILC_UMODE_STATS_UPDATE(router, SILC_UMODE_ROUTER_OPERATOR);
1604
1605     /* Change the mode */
1606     client->mode = mode;
1607
1608     /* Check if anyone is watching this nickname */
1609     if (server->server_type == SILC_ROUTER)
1610       silc_server_check_watcher_list(server, client, NULL,
1611                                      SILC_NOTIFY_TYPE_UMODE_CHANGE);
1612
1613     break;
1614
1615   case SILC_NOTIFY_TYPE_BAN:
1616     /*
1617      * Save the ban
1618      */
1619
1620     SILC_LOG_DEBUG(("BAN notify"));
1621     
1622     /* Get Channel ID */
1623     tmp = silc_argument_get_arg_type(args, 1, &tmp_len);
1624     if (!tmp)
1625       goto out;
1626     channel_id = silc_id_payload_parse_id(tmp, tmp_len, NULL);
1627     if (!channel_id)
1628       goto out;
1629     
1630     /* Get channel entry */
1631     channel = silc_idlist_find_channel_by_id(server->global_list, 
1632                                              channel_id, NULL);
1633     if (!channel) {
1634       channel = silc_idlist_find_channel_by_id(server->local_list, 
1635                                                channel_id, NULL);
1636       if (!channel) {
1637         SILC_LOG_DEBUG(("Notify for unknown channel"));
1638         silc_free(channel_id);
1639         goto out;
1640       }
1641     }
1642     silc_free(channel_id);
1643
1644     /* Get the ban action */
1645     tmp = silc_argument_get_arg_type(args, 2, &tmp_len);
1646     if (tmp && tmp_len == 1) {
1647       SilcUInt8 action = (SilcUInt8)tmp[0];
1648       SilcUInt16 iargc = 0;
1649       SilcArgumentPayload iargs;
1650
1651       /* Get ban list */
1652       tmp = silc_argument_get_arg_type(args, 3, &tmp_len);
1653       if (!tmp || tmp_len < 2)
1654         goto out;
1655
1656       /* Parse the arguments to see they are constructed correctly */
1657       SILC_GET16_MSB(iargc, tmp);
1658       iargs = silc_argument_payload_parse(tmp + 2, tmp_len - 2, iargc);
1659       if (!iargs)
1660         goto out;
1661
1662       if (action == 0 && !channel->ban_list)
1663         channel->ban_list = silc_hash_table_alloc(0, silc_hash_ptr,
1664                                                   NULL, NULL, NULL,
1665                                                   NULL, NULL, TRUE);
1666
1667       /* Proces the ban action */
1668       silc_server_inviteban_process(server, channel->ban_list, action,
1669                                     iargs);
1670       silc_argument_payload_free(iargs);
1671     }
1672     break;
1673
1674   case SILC_NOTIFY_TYPE_ERROR:
1675     {
1676       /*
1677        * Error notify
1678        */
1679       SilcStatus error;
1680
1681       tmp = silc_argument_get_arg_type(args, 1, &tmp_len);
1682       if (!tmp && tmp_len != 1)
1683         goto out;
1684       error = (SilcStatus)tmp[0];
1685
1686       SILC_LOG_DEBUG(("ERROR notify (%d)", error));
1687
1688       if (error == SILC_STATUS_ERR_NO_SUCH_CLIENT_ID &&
1689           sock->type == SILC_SOCKET_TYPE_ROUTER) {
1690         tmp = silc_argument_get_arg_type(args, 2, &tmp_len);
1691         if (tmp) {
1692           SILC_LOG_DEBUG(("Received invalid client ID notification, deleting "
1693                           "the entry from cache"));
1694           client_id = silc_id_payload_parse_id(tmp, tmp_len, NULL);
1695           if (!client_id)
1696             goto out;
1697           client = silc_idlist_find_client_by_id(server->global_list, 
1698                                                  client_id, FALSE, NULL);
1699           if (client) {
1700             silc_server_remove_from_channels(server, NULL, client, TRUE, 
1701                                              NULL, TRUE);
1702             silc_idlist_del_data(client);
1703             silc_idlist_del_client(server->global_list, client);
1704           }
1705           silc_free(client_id);
1706         }
1707       }
1708     }
1709     break;
1710
1711     /* Ignore rest of the notify types for now */
1712   case SILC_NOTIFY_TYPE_NONE:
1713   case SILC_NOTIFY_TYPE_MOTD:
1714     break;
1715   default:
1716     break;
1717   }
1718
1719  out:
1720   silc_notify_payload_free(payload);
1721 }
1722
1723 void silc_server_notify_list(SilcServer server,
1724                              SilcSocketConnection sock,
1725                              SilcPacketContext *packet)
1726 {
1727   SilcPacketContext *new;
1728   SilcBuffer buffer;
1729   SilcUInt16 len;
1730
1731   SILC_LOG_DEBUG(("Processing Notify List"));
1732
1733   if (sock->type == SILC_SOCKET_TYPE_CLIENT ||
1734       packet->src_id_type != SILC_ID_SERVER)
1735     return;
1736
1737   /* Make copy of the original packet context, except for the actual
1738      data buffer, which we will here now fetch from the original buffer. */
1739   new = silc_packet_context_alloc();
1740   new->type = SILC_PACKET_NOTIFY;
1741   new->flags = packet->flags;
1742   new->src_id = packet->src_id;
1743   new->src_id_len = packet->src_id_len;
1744   new->src_id_type = packet->src_id_type;
1745   new->dst_id = packet->dst_id;
1746   new->dst_id_len = packet->dst_id_len;
1747   new->dst_id_type = packet->dst_id_type;
1748
1749   buffer = silc_buffer_alloc(1024);
1750   new->buffer = buffer;
1751
1752   while (packet->buffer->len) {
1753     SILC_GET16_MSB(len, packet->buffer->data + 2);
1754     if (len > packet->buffer->len)
1755       break;
1756
1757     if (len > buffer->truelen) {
1758       silc_buffer_free(buffer);
1759       buffer = silc_buffer_alloc(1024 + len);
1760     }
1761
1762     silc_buffer_pull_tail(buffer, len);
1763     silc_buffer_put(buffer, packet->buffer->data, len);
1764
1765     /* Process the Notify */
1766     silc_server_notify(server, sock, new);
1767
1768     silc_buffer_push_tail(buffer, len);
1769     silc_buffer_pull(packet->buffer, len);
1770   }
1771
1772   silc_buffer_free(buffer);
1773   silc_free(new);
1774 }
1775
1776 /* Received private message. This resolves the destination of the message 
1777    and sends the packet. This is used by both server and router.  If the
1778    destination is our locally connected client this sends the packet to
1779    the client. This may also send the message for further routing if
1780    the destination is not in our server (or router). */
1781
1782 void silc_server_private_message(SilcServer server,
1783                                  SilcSocketConnection sock,
1784                                  SilcPacketContext *packet)
1785 {
1786   SilcSocketConnection dst_sock;
1787   SilcIDListData idata;
1788   SilcClientEntry client;
1789
1790   SILC_LOG_DEBUG(("Start"));
1791
1792   if (packet->src_id_type != SILC_ID_CLIENT ||
1793       packet->dst_id_type != SILC_ID_CLIENT || !packet->dst_id)
1794     return;
1795
1796   /* Get the route to the client */
1797   dst_sock = silc_server_get_client_route(server, packet->dst_id,
1798                                           packet->dst_id_len, NULL, 
1799                                           &idata, &client);
1800   if (!dst_sock) {
1801     SilcBuffer idp;
1802     unsigned char error;
1803
1804     if (client && client->mode & SILC_UMODE_DETACHED) {
1805       SILC_LOG_DEBUG(("Client is detached, discarding packet"));
1806       return;
1807     }
1808
1809     /* Send SILC_NOTIFY_TYPE_ERROR to indicate that such destination ID
1810        does not exist or is invalid. */
1811     idp = silc_id_payload_encode_data(packet->dst_id,
1812                                       packet->dst_id_len,
1813                                       packet->dst_id_type);
1814     if (!idp)
1815       return;
1816
1817     error = SILC_STATUS_ERR_NO_SUCH_CLIENT_ID;
1818     if (packet->src_id_type == SILC_ID_CLIENT) {
1819       SilcClientID *client_id = silc_id_str2id(packet->src_id,
1820                                                packet->src_id_len,
1821                                                packet->src_id_type);
1822       silc_server_send_notify_dest(server, sock, FALSE,
1823                                    client_id, SILC_ID_CLIENT,
1824                                    SILC_NOTIFY_TYPE_ERROR, 2,
1825                                    &error, 1,
1826                                    idp->data, idp->len);
1827       silc_free(client_id);
1828     } else {
1829       silc_server_send_notify(server, sock, FALSE,
1830                               SILC_NOTIFY_TYPE_ERROR, 2,
1831                               &error, 1,
1832                               idp->data, idp->len);
1833     }
1834
1835     silc_buffer_free(idp);
1836     return;
1837   }
1838
1839   /* Check whether destination client wishes to receive private messages */
1840   if (client && !(packet->flags & SILC_PACKET_FLAG_PRIVMSG_KEY) &&
1841       client->mode & SILC_UMODE_BLOCK_PRIVMSG) {
1842     SILC_LOG_DEBUG(("Client blocks private messages, discarding packet"));
1843     return;
1844   }
1845
1846   /* Send the private message */
1847   silc_server_send_private_message(server, dst_sock, idata->send_key,
1848                                    idata->hmac_send, idata->psn_send++,
1849                                    packet);
1850 }
1851
1852 /* Received private message key packet.. This packet is never for us. It is to
1853    the client in the packet's destination ID. Sending of this sort of packet
1854    equals sending private message, ie. it is sent point to point from
1855    one client to another. */
1856
1857 void silc_server_private_message_key(SilcServer server,
1858                                      SilcSocketConnection sock,
1859                                      SilcPacketContext *packet)
1860 {
1861   SilcSocketConnection dst_sock;
1862   SilcIDListData idata;
1863
1864   SILC_LOG_DEBUG(("Start"));
1865
1866   if (packet->src_id_type != SILC_ID_CLIENT ||
1867       packet->dst_id_type != SILC_ID_CLIENT)
1868     return;
1869
1870   if (!packet->dst_id)
1871     return;
1872
1873   /* Get the route to the client */
1874   dst_sock = silc_server_get_client_route(server, packet->dst_id,
1875                                           packet->dst_id_len, NULL, 
1876                                           &idata, NULL);
1877   if (!dst_sock)
1878     return;
1879
1880   /* Relay the packet */
1881   silc_server_relay_packet(server, dst_sock, idata->send_key,
1882                            idata->hmac_send, idata->psn_send++, packet, FALSE);
1883 }
1884
1885 /* Processes incoming command reply packet. The command reply packet may
1886    be destined to one of our clients or it may directly for us. We will 
1887    call the command reply routine after processing the packet. */
1888
1889 void silc_server_command_reply(SilcServer server,
1890                                SilcSocketConnection sock,
1891                                SilcPacketContext *packet)
1892 {
1893   SilcBuffer buffer = packet->buffer;
1894   SilcClientEntry client = NULL;
1895   SilcSocketConnection dst_sock;
1896   SilcIDListData idata;
1897   SilcClientID *id = NULL;
1898
1899   SILC_LOG_DEBUG(("Start"));
1900
1901   if (packet->dst_id_type == SILC_ID_CHANNEL)
1902     return;
1903
1904   if (packet->dst_id_type == SILC_ID_CLIENT) {
1905     /* Destination must be one of ours */
1906     id = silc_id_str2id(packet->dst_id, packet->dst_id_len, SILC_ID_CLIENT);
1907     if (!id)
1908       return;
1909     client = silc_idlist_find_client_by_id(server->local_list, id, TRUE, NULL);
1910     if (!client) {
1911       SILC_LOG_ERROR(("Cannot process command reply to unknown client"));
1912       silc_free(id);
1913       return;
1914     }
1915   }
1916
1917   if (packet->dst_id_type == SILC_ID_SERVER) {
1918     /* For now this must be for us */
1919     if (memcmp(packet->dst_id, server->id_string, server->id_string_len)) {
1920       SILC_LOG_ERROR(("Cannot process command reply to unknown server"));
1921       return;
1922     }
1923   }
1924
1925   /* Execute command reply locally for the command */
1926   silc_server_command_reply_process(server, sock, buffer);
1927
1928   if (packet->dst_id_type == SILC_ID_CLIENT && client && id) {
1929     /* Relay the packet to the client */
1930     const SilcBufferStruct p;
1931     
1932     dst_sock = (SilcSocketConnection)client->connection;
1933     idata = (SilcIDListData)client;
1934     
1935     silc_buffer_push(buffer, SILC_PACKET_HEADER_LEN + packet->src_id_len 
1936                      + packet->dst_id_len + packet->padlen);
1937     if (!silc_packet_send_prepare(dst_sock, 0, 0, buffer->len,
1938                                   idata->hmac_send, (const SilcBuffer)&p)) {
1939       SILC_LOG_ERROR(("Cannot send packet"));
1940       return;
1941     }
1942     silc_buffer_put((SilcBuffer)&p, buffer->data, buffer->len);
1943     
1944     /* Encrypt packet */
1945     silc_packet_encrypt(idata->send_key, idata->hmac_send, idata->psn_send++,
1946                         (SilcBuffer)&p, buffer->len);
1947     
1948     /* Send the packet */
1949     silc_server_packet_send_real(server, dst_sock, TRUE);
1950
1951     silc_free(id);
1952   }
1953 }
1954
1955 /* Process received channel message. The message can be originated from
1956    client or server. */
1957
1958 void silc_server_channel_message(SilcServer server,
1959                                  SilcSocketConnection sock,
1960                                  SilcPacketContext *packet)
1961 {
1962   SilcChannelEntry channel = NULL;
1963   SilcChannelID *id = NULL;
1964   void *sender_id = NULL;
1965   SilcClientEntry sender_entry = NULL;
1966   SilcChannelClientEntry chl;
1967   bool local = TRUE;
1968
1969   SILC_LOG_DEBUG(("Processing channel message"));
1970
1971   /* Sanity checks */
1972   if (packet->dst_id_type != SILC_ID_CHANNEL) {
1973     SILC_LOG_DEBUG(("Received bad message for channel, dropped"));
1974     goto out;
1975   }
1976
1977   /* Find channel entry */
1978   id = silc_id_str2id(packet->dst_id, packet->dst_id_len, SILC_ID_CHANNEL);
1979   if (!id)
1980     goto out;
1981   channel = silc_idlist_find_channel_by_id(server->local_list, id, NULL);
1982   if (!channel) {
1983     channel = silc_idlist_find_channel_by_id(server->global_list, id, NULL);
1984     if (!channel) {
1985       SilcBuffer idp;
1986       unsigned char error;
1987
1988       /* Send SILC_NOTIFY_TYPE_ERROR to indicate that such destination ID
1989          does not exist or is invalid. */
1990       idp = silc_id_payload_encode_data(packet->dst_id,
1991                                         packet->dst_id_len,
1992                                         packet->dst_id_type);
1993       if (!idp)
1994         goto out;
1995
1996       error = SILC_STATUS_ERR_NO_SUCH_CHANNEL_ID;
1997       if (packet->src_id_type == SILC_ID_CLIENT) {
1998         SilcClientID *client_id = silc_id_str2id(packet->src_id,
1999                                                  packet->src_id_len,
2000                                                  packet->src_id_type);
2001         silc_server_send_notify_dest(server, sock, FALSE,
2002                                      client_id, SILC_ID_CLIENT,
2003                                      SILC_NOTIFY_TYPE_ERROR, 2,
2004                                      &error, 1, idp->data, idp->len);
2005         silc_free(client_id);
2006       } else {
2007         silc_server_send_notify(server, sock, FALSE,
2008                                 SILC_NOTIFY_TYPE_ERROR, 2,
2009                                 &error, 1, idp->data, idp->len);
2010       }
2011       
2012       silc_buffer_free(idp);
2013       goto out;
2014     }
2015   }
2016
2017   /* See that this client is on the channel. If the original sender is
2018      not client (as it can be server as well) we don't do the check. */
2019   sender_id = silc_id_str2id(packet->src_id, packet->src_id_len, 
2020                              packet->src_id_type);
2021   if (!sender_id)
2022     goto out;
2023   if (packet->src_id_type == SILC_ID_CLIENT) {
2024     sender_entry = silc_idlist_find_client_by_id(server->local_list, 
2025                                                  sender_id, TRUE, NULL);
2026     if (!sender_entry) {
2027       local = FALSE;
2028       sender_entry = silc_idlist_find_client_by_id(server->global_list, 
2029                                                    sender_id, TRUE, NULL);
2030     }
2031     if (!sender_entry || !silc_server_client_on_channel(sender_entry, 
2032                                                         channel, &chl)) {
2033       SILC_LOG_DEBUG(("Client not on channel"));
2034       goto out;
2035     }
2036
2037     /* If channel is moderated check that client is allowed to send
2038        messages. */
2039     if (channel->mode & SILC_CHANNEL_MODE_SILENCE_USERS && 
2040         !(chl->mode & SILC_CHANNEL_UMODE_CHANOP) &&
2041         !(chl->mode & SILC_CHANNEL_UMODE_CHANFO)) {
2042       SILC_LOG_DEBUG(("Channel is silenced from normal users"));
2043       goto out;
2044     }
2045     if (channel->mode & SILC_CHANNEL_MODE_SILENCE_OPERS && 
2046         chl->mode & SILC_CHANNEL_UMODE_CHANOP &&
2047         !(chl->mode & SILC_CHANNEL_UMODE_CHANFO)) {
2048       SILC_LOG_DEBUG(("Channel is silenced from operators"));
2049       goto out;
2050     }
2051     if (chl->mode & SILC_CHANNEL_UMODE_QUIET) {
2052       SILC_LOG_DEBUG(("Sender is quieted on the channel"));
2053       goto out;
2054     }
2055
2056     /* If the packet is coming from router, but the client entry is local 
2057        entry to us then some router is rerouting this to us and it is not 
2058        allowed. When the client is local to us it means that we've routed
2059        this packet to network, and now someone is routing it back to us. */
2060     if (server->server_type == SILC_ROUTER &&
2061         sock->type == SILC_SOCKET_TYPE_ROUTER && local) {
2062       SILC_LOG_DEBUG(("Channel message rerouted to the sender, drop it"));
2063       goto out;
2064     }
2065   }
2066
2067   /* Distribute the packet to our local clients. This will send the
2068      packet for further routing as well, if needed. */
2069   silc_server_packet_relay_to_channel(server, sock, channel, sender_id,
2070                                       packet->src_id_type, sender_entry,
2071                                       packet->buffer->data,
2072                                       packet->buffer->len, FALSE);
2073
2074  out:
2075   silc_free(sender_id);
2076   silc_free(id);
2077 }
2078
2079 /* Received channel key packet. We distribute the key to all of our locally
2080    connected clients on the channel. */
2081
2082 void silc_server_channel_key(SilcServer server,
2083                              SilcSocketConnection sock,
2084                              SilcPacketContext *packet)
2085 {
2086   SilcBuffer buffer = packet->buffer;
2087   SilcChannelEntry channel;
2088
2089   if (packet->src_id_type != SILC_ID_SERVER ||
2090       (server->server_type == SILC_ROUTER && !server->backup_router &&
2091        sock->type == SILC_SOCKET_TYPE_ROUTER))
2092     return;
2093
2094   /* Save the channel key */
2095   channel = silc_server_save_channel_key(server, buffer, NULL);
2096   if (!channel)
2097     return;
2098
2099   /* Distribute the key to everybody who is on the channel. If we are router
2100      we will also send it to locally connected servers. */
2101   silc_server_send_channel_key(server, sock, channel, FALSE);
2102   
2103   if (server->server_type != SILC_BACKUP_ROUTER) {
2104     /* Distribute to local cell backup routers. */
2105     silc_server_backup_send(server, sock->user_data, 
2106                             SILC_PACKET_CHANNEL_KEY, 0,
2107                             buffer->data, buffer->len, FALSE, TRUE);
2108   }
2109 }
2110
2111 /* Received New Client packet and processes it.  Creates Client ID for the
2112    client. Client becomes registered after calling this functions. */
2113
2114 SilcClientEntry silc_server_new_client(SilcServer server,
2115                                        SilcSocketConnection sock,
2116                                        SilcPacketContext *packet)
2117 {
2118   SilcBuffer buffer = packet->buffer;
2119   SilcClientEntry client;
2120   SilcClientID *client_id;
2121   SilcIDListData idata;
2122   char *username = NULL, *realname = NULL;
2123   SilcUInt16 username_len;
2124   SilcUInt32 id_len;
2125   int ret;
2126   char *hostname, *nickname;
2127   int nickfail = 0;
2128
2129   SILC_LOG_DEBUG(("Creating new client"));
2130
2131   if (sock->type != SILC_SOCKET_TYPE_CLIENT)
2132     return NULL;
2133
2134   /* Take client entry */
2135   client = (SilcClientEntry)sock->user_data;
2136   idata = (SilcIDListData)client;
2137
2138   /* Remove the old cache entry. */
2139   if (!silc_idcache_del_by_context(server->local_list->clients, client)) {
2140     SILC_LOG_INFO(("Unauthenticated client attempted to register to network"));
2141     silc_server_disconnect_remote(server, sock, 
2142                                   SILC_STATUS_ERR_NOT_AUTHENTICATED, NULL);
2143     if (sock->user_data)
2144       silc_server_free_sock_user_data(server, sock, NULL);
2145     return NULL;
2146   }
2147
2148   /* Parse incoming packet */
2149   ret = silc_buffer_unformat(buffer,
2150                              SILC_STR_UI16_NSTRING_ALLOC(&username, 
2151                                                          &username_len),
2152                              SILC_STR_UI16_STRING_ALLOC(&realname),
2153                              SILC_STR_END);
2154   if (ret == -1) {
2155     silc_free(username);
2156     silc_free(realname);
2157     SILC_LOG_ERROR(("Client %s (%s) sent incomplete information, closing "
2158                     "connection", sock->hostname, sock->ip));
2159     silc_server_disconnect_remote(server, sock, 
2160                                   SILC_STATUS_ERR_INCOMPLETE_INFORMATION, 
2161                                   NULL);
2162     if (sock->user_data)
2163       silc_server_free_sock_user_data(server, sock, NULL);
2164     return NULL;
2165   }
2166
2167   if (!username) {
2168     silc_free(username);
2169     silc_free(realname);
2170     SILC_LOG_ERROR(("Client %s (%s) did not send its username, closing "
2171                     "connection", sock->hostname, sock->ip));
2172     silc_server_disconnect_remote(server, sock, 
2173                                   SILC_STATUS_ERR_INCOMPLETE_INFORMATION,
2174                                   NULL);
2175     if (sock->user_data)
2176       silc_server_free_sock_user_data(server, sock, NULL);
2177     return NULL;
2178   }
2179
2180   if (username_len > 128)
2181     username[128] = '\0';
2182
2183   /* Check for bad characters for nickname, and modify the nickname if
2184      it includes those. */
2185   if (silc_server_name_bad_chars(username, username_len)) {
2186     nickname = silc_server_name_modify_bad(username, username_len);
2187   } else {
2188     nickname = strdup(username);
2189   }
2190
2191   /* Make sanity checks for the hostname of the client. If the hostname
2192      is provided in the `username' check that it is the same than the
2193      resolved hostname, or if not resolved the hostname that appears in
2194      the client's public key. If the hostname is not present then put
2195      it from the resolved name or from the public key. */
2196   if (strchr(username, '@')) {
2197     SilcPublicKeyIdentifier pident;
2198     int tlen = strcspn(username, "@");
2199     char *phostname = NULL;
2200
2201     hostname = silc_memdup(username + tlen + 1, strlen(username) - tlen - 1);
2202
2203     if (strcmp(sock->hostname, sock->ip) && 
2204         strcmp(sock->hostname, hostname)) {
2205       silc_free(username);
2206       silc_free(hostname);
2207       silc_free(realname);
2208       SILC_LOG_ERROR(("Client %s (%s) sent incomplete information, closing "
2209                       "connection", sock->hostname, sock->ip));
2210       silc_server_disconnect_remote(server, sock, 
2211                                     SILC_STATUS_ERR_INCOMPLETE_INFORMATION,
2212                                     NULL);
2213       if (sock->user_data)
2214         silc_server_free_sock_user_data(server, sock, NULL);
2215       return NULL;
2216     }
2217     
2218     pident = silc_pkcs_decode_identifier(client->data.public_key->identifier);
2219     if (pident) {
2220       phostname = strdup(pident->host);
2221       silc_pkcs_free_identifier(pident);
2222     }
2223
2224     if (!strcmp(sock->hostname, sock->ip) && 
2225         phostname && strcmp(phostname, hostname)) {
2226       silc_free(username);
2227       silc_free(hostname);
2228       silc_free(phostname);
2229       silc_free(realname);
2230       SILC_LOG_ERROR(("Client %s (%s) sent incomplete information, closing "
2231                       "connection", sock->hostname, sock->ip));
2232       silc_server_disconnect_remote(server, sock, 
2233                                     SILC_STATUS_ERR_INCOMPLETE_INFORMATION,
2234                                     NULL);
2235       if (sock->user_data)
2236         silc_server_free_sock_user_data(server, sock, NULL);
2237       return NULL;
2238     }
2239     
2240     silc_free(phostname);
2241   } else {
2242     /* The hostname is not present, add it. */
2243     char *newusername;
2244     /* XXX For now we cannot take the host name from the public key since
2245        they are not trusted or we cannot verify them as trusted. Just take
2246        what the resolved name or address is. */
2247 #if 0
2248     if (strcmp(sock->hostname, sock->ip)) {
2249 #endif
2250       newusername = silc_calloc(strlen(username) + 
2251                                 strlen(sock->hostname) + 2,
2252                                 sizeof(*newusername));
2253       strncat(newusername, username, strlen(username));
2254       strncat(newusername, "@", 1);
2255       strncat(newusername, sock->hostname, strlen(sock->hostname));
2256       silc_free(username);
2257       username = newusername;
2258 #if 0
2259     } else {
2260       SilcPublicKeyIdentifier pident = 
2261         silc_pkcs_decode_identifier(client->data.public_key->identifier);
2262       
2263       if (pident) {
2264         newusername = silc_calloc(strlen(username) + 
2265                                   strlen(pident->host) + 2,
2266                                   sizeof(*newusername));
2267         strncat(newusername, username, strlen(username));
2268         strncat(newusername, "@", 1);
2269         strncat(newusername, pident->host, strlen(pident->host));
2270         silc_free(username);
2271         username = newusername;
2272         silc_pkcs_free_identifier(pident);
2273       }
2274     }
2275 #endif
2276   }
2277
2278   /* Create Client ID */
2279   while (!silc_id_create_client_id(server, server->id, server->rng, 
2280                                    server->md5hash, nickname, &client_id)) {
2281     nickfail++;
2282     if (nickfail > 9) {
2283       silc_server_disconnect_remote(server, sock, 
2284                                     SILC_STATUS_ERR_BAD_NICKNAME, NULL);
2285       if (sock->user_data)
2286         silc_server_free_sock_user_data(server, sock, NULL);
2287       return NULL;
2288     }
2289     snprintf(&nickname[strlen(nickname) - 1], 1, "%d", nickfail);
2290   }
2291
2292   /* If client marked as anonymous, scramble the username and hostname */
2293   if (client->mode & SILC_UMODE_ANONYMOUS) {
2294     char *scramble;
2295
2296     if (strlen(username) >= 2) {
2297       username[0] = silc_rng_get_byte_fast(server->rng);
2298       username[1] = silc_rng_get_byte_fast(server->rng);
2299     }
2300
2301     scramble = silc_hash_babbleprint(server->sha1hash, username,
2302                                      strlen(username));
2303     scramble[5] = '@';
2304     scramble[11] = '.';
2305     memcpy(&scramble[16], ".silc", 5);
2306     scramble[21] = '\0';
2307     silc_free(username);
2308     username = scramble;
2309   }
2310
2311   /* Update client entry */
2312   idata->status |= SILC_IDLIST_STATUS_REGISTERED;
2313   client->nickname = nickname;
2314   client->username = username;
2315   client->userinfo = realname ? realname : strdup(username);
2316   client->id = client_id;
2317   id_len = silc_id_get_len(client_id, SILC_ID_CLIENT);
2318
2319   /* Add the client again to the ID cache */
2320   silc_idcache_add(server->local_list->clients, client->nickname,
2321                    client_id, client, 0, NULL);
2322
2323   /* Notify our router about new client on the SILC network */
2324   silc_server_send_new_id(server, SILC_PRIMARY_ROUTE(server),
2325                           SILC_BROADCAST(server), client->id,
2326                           SILC_ID_CLIENT, id_len);
2327
2328   /* Distribute to backup routers */
2329   if (server->server_type == SILC_ROUTER) {
2330     SilcBuffer idp = silc_id_payload_encode(client->id, SILC_ID_CLIENT);
2331     silc_server_backup_send(server, sock->user_data, SILC_PACKET_NEW_ID, 0,
2332                             idp->data, idp->len, FALSE, TRUE);
2333     silc_buffer_free(idp);
2334   }
2335
2336   /* Send the new client ID to the client. */
2337   silc_server_send_new_id(server, sock, FALSE, client->id, SILC_ID_CLIENT,
2338                           silc_id_get_len(client->id, SILC_ID_CLIENT));
2339
2340   /* Send some nice info to the client */
2341   silc_server_send_connect_notifys(server, sock, client);
2342
2343   /* Check if anyone is watching this nickname */
2344   if (server->server_type == SILC_ROUTER)
2345     silc_server_check_watcher_list(server, client, NULL, 0);
2346
2347   return client;
2348 }
2349
2350 /* Create new server. This processes received New Server packet and
2351    saves the received Server ID. The server is our locally connected
2352    server thus we save all the information and save it to local list. 
2353    This funtion can be used by both normal server and router server.
2354    If normal server uses this it means that its router has connected
2355    to the server. If router uses this it means that one of the cell's
2356    servers is connected to the router. */
2357
2358 SilcServerEntry silc_server_new_server(SilcServer server,
2359                                        SilcSocketConnection sock,
2360                                        SilcPacketContext *packet)
2361 {
2362   SilcBuffer buffer = packet->buffer;
2363   SilcServerEntry new_server, server_entry;
2364   SilcServerID *server_id;
2365   SilcIDListData idata;
2366   unsigned char *server_name, *id_string;
2367   SilcUInt16 id_len, name_len;
2368   int ret;
2369   bool local = TRUE;
2370
2371   SILC_LOG_DEBUG(("Creating new server"));
2372
2373   if (sock->type != SILC_SOCKET_TYPE_SERVER &&
2374       sock->type != SILC_SOCKET_TYPE_ROUTER)
2375     return NULL;
2376
2377   /* Take server entry */
2378   new_server = (SilcServerEntry)sock->user_data;
2379   idata = (SilcIDListData)new_server;
2380
2381   /* Remove the old cache entry */
2382   if (!silc_idcache_del_by_context(server->local_list->servers, new_server)) {
2383     if (!silc_idcache_del_by_context(server->global_list->servers, 
2384                                      new_server)) {
2385       SILC_LOG_INFO(("Unauthenticated %s attempted to register to "
2386                      "network", (sock->type == SILC_SOCKET_TYPE_SERVER ?
2387                                  "server" : "router")));
2388       silc_server_disconnect_remote(server, sock, 
2389                                     SILC_STATUS_ERR_NOT_AUTHENTICATED, NULL);
2390       if (sock->user_data)
2391         silc_server_free_sock_user_data(server, sock, NULL);
2392       return NULL;
2393     }
2394     local = FALSE;
2395   }
2396
2397   /* Parse the incoming packet */
2398   ret = silc_buffer_unformat(buffer,
2399                              SILC_STR_UI16_NSTRING_ALLOC(&id_string, &id_len),
2400                              SILC_STR_UI16_NSTRING_ALLOC(&server_name, 
2401                                                          &name_len),
2402                              SILC_STR_END);
2403   if (ret == -1) {
2404     silc_free(id_string);
2405     silc_free(server_name);
2406     silc_server_disconnect_remote(server, sock, 
2407                                   SILC_STATUS_ERR_INCOMPLETE_INFORMATION,
2408                                   NULL);
2409     if (sock->user_data)
2410       silc_server_free_sock_user_data(server, sock, NULL);
2411     return NULL;
2412   }
2413
2414   if (id_len > buffer->len) {
2415     silc_free(id_string);
2416     silc_free(server_name);
2417     silc_server_disconnect_remote(server, sock, 
2418                                   SILC_STATUS_ERR_INCOMPLETE_INFORMATION,
2419                                   NULL);
2420     if (sock->user_data)
2421       silc_server_free_sock_user_data(server, sock, NULL);
2422     return NULL;
2423   }
2424
2425   if (name_len > 256)
2426     server_name[255] = '\0';
2427
2428   /* Get Server ID */
2429   server_id = silc_id_str2id(id_string, id_len, SILC_ID_SERVER);
2430   if (!server_id) {
2431     silc_free(id_string);
2432     silc_free(server_name);
2433     silc_server_disconnect_remote(server, sock, 
2434                                   SILC_STATUS_ERR_INCOMPLETE_INFORMATION,
2435                                   NULL);
2436     if (sock->user_data)
2437       silc_server_free_sock_user_data(server, sock, NULL);
2438     return NULL;
2439   }
2440   silc_free(id_string);
2441
2442   /* Check for valid server ID */
2443   if (!silc_id_is_valid_server_id(server, server_id, sock)) {
2444     SILC_LOG_INFO(("Invalid server ID sent by %s (%s)",
2445                    sock->ip, sock->hostname));
2446     silc_server_disconnect_remote(server, sock, 
2447                                   SILC_STATUS_ERR_BAD_SERVER_ID, NULL);
2448     if (sock->user_data)
2449       silc_server_free_sock_user_data(server, sock, NULL);
2450     silc_free(server_name);
2451     return NULL;
2452   }
2453
2454   /* Check that we do not have this ID already */
2455   server_entry = silc_idlist_find_server_by_id(server->local_list, 
2456                                                server_id, TRUE, NULL);
2457   if (server_entry) {
2458     silc_idcache_del_by_context(server->local_list->servers, server_entry);
2459   } else {
2460     server_entry = silc_idlist_find_server_by_id(server->global_list, 
2461                                                  server_id, TRUE, NULL);
2462     if (server_entry) 
2463       silc_idcache_del_by_context(server->global_list->servers, server_entry);
2464   }
2465
2466   /* Update server entry */
2467   idata->status |= SILC_IDLIST_STATUS_REGISTERED;
2468   new_server->server_name = server_name;
2469   new_server->id = server_id;
2470   
2471   SILC_LOG_DEBUG(("New server id(%s)",
2472                   silc_id_render(server_id, SILC_ID_SERVER)));
2473
2474   /* Add again the entry to the ID cache. */
2475   silc_idcache_add(local ? server->local_list->servers : 
2476                    server->global_list->servers, server_name, server_id, 
2477                    new_server, 0, NULL);
2478
2479   /* Distribute the information about new server in the SILC network
2480      to our router. If we are normal server we won't send anything
2481      since this connection must be our router connection. */
2482   if (server->server_type == SILC_ROUTER && !server->standalone &&
2483       SILC_PRIMARY_ROUTE(server) != sock)
2484     silc_server_send_new_id(server, SILC_PRIMARY_ROUTE(server),
2485                             TRUE, new_server->id, SILC_ID_SERVER, 
2486                             silc_id_get_len(server_id, SILC_ID_SERVER));
2487
2488   if (server->server_type == SILC_ROUTER) {
2489     /* Distribute to backup routers */
2490     SilcBuffer idp = silc_id_payload_encode(new_server->id, SILC_ID_SERVER);
2491     silc_server_backup_send(server, sock->user_data, SILC_PACKET_NEW_ID, 0,
2492                             idp->data, idp->len, FALSE, TRUE);
2493     silc_buffer_free(idp);
2494
2495     /* Statistics */
2496     server->stat.cell_servers++;
2497   }
2498
2499   /* Check whether this router connection has been replaced by an
2500      backup router. If it has been then we'll disable the server and will
2501      ignore everything it will send until the backup router resuming
2502      protocol has been completed. */
2503   if (sock->type == SILC_SOCKET_TYPE_ROUTER &&
2504       silc_server_backup_replaced_get(server, server_id, NULL)) {
2505     /* Send packet to the server indicating that it cannot use this
2506        connection as it has been replaced by backup router. */
2507     SilcBuffer packet = silc_buffer_alloc(2);
2508     silc_buffer_pull_tail(packet, SILC_BUFFER_END(packet));
2509     silc_buffer_format(packet,
2510                        SILC_STR_UI_CHAR(SILC_SERVER_BACKUP_REPLACED),
2511                        SILC_STR_UI_CHAR(0),
2512                        SILC_STR_END);
2513     silc_server_packet_send(server, sock, 
2514                             SILC_PACKET_RESUME_ROUTER, 0, 
2515                             packet->data, packet->len, TRUE);
2516     silc_buffer_free(packet);
2517
2518     /* Mark the router disabled. The data sent earlier will go but nothing
2519        after this does not go to this connection. */
2520     idata->status |= SILC_IDLIST_STATUS_DISABLED;
2521   } else {
2522     /* If it is router announce our stuff to it. */
2523     if (sock->type == SILC_SOCKET_TYPE_ROUTER && 
2524         server->server_type == SILC_ROUTER) {
2525       silc_server_announce_servers(server, FALSE, 0, sock);
2526       silc_server_announce_clients(server, 0, sock);
2527       silc_server_announce_channels(server, 0, sock);
2528     }
2529
2530     /* Announce our information to backup router */
2531     if (new_server->server_type == SILC_BACKUP_ROUTER &&
2532         sock->type == SILC_SOCKET_TYPE_SERVER &&
2533         server->server_type == SILC_ROUTER) {
2534       silc_server_announce_servers(server, TRUE, 0, sock);
2535       silc_server_announce_clients(server, 0, sock);
2536       silc_server_announce_channels(server, 0, sock);
2537     }
2538
2539     /* If backup router, mark it as one of ours.  This server is considered
2540        to be backup router after this setting. */
2541     if (new_server->server_type == SILC_BACKUP_ROUTER) {
2542       SilcServerConfigRouter *backup;
2543       backup = silc_server_config_find_backup_conn(server, sock->ip);
2544       if (!backup)
2545         backup = silc_server_config_find_backup_conn(server, sock->hostname);
2546       if (backup) {
2547         /* Add as our backup router */
2548         silc_server_backup_add(server, new_server, backup->backup_replace_ip,
2549                                backup->backup_replace_port,
2550                                backup->backup_local);
2551       }
2552     }
2553
2554     /* By default the servers connected to backup router are disabled
2555        until backup router has become the primary */
2556     if (server->server_type == SILC_BACKUP_ROUTER &&
2557         sock->type == SILC_SOCKET_TYPE_SERVER)
2558       idata->status |= SILC_IDLIST_STATUS_DISABLED;
2559   }
2560
2561   return new_server;
2562 }
2563
2564 /* Processes incoming New ID packet. New ID Payload is used to distribute
2565    information about newly registered clients and servers. */
2566
2567 static void silc_server_new_id_real(SilcServer server, 
2568                                     SilcSocketConnection sock,
2569                                     SilcPacketContext *packet,
2570                                     int broadcast)
2571 {
2572   SilcBuffer buffer = packet->buffer;
2573   SilcIDList id_list;
2574   SilcServerEntry router, server_entry;
2575   SilcSocketConnection router_sock;
2576   SilcIDPayload idp;
2577   SilcIdType id_type;
2578   void *id;
2579
2580   SILC_LOG_DEBUG(("Processing new ID"));
2581
2582   if (sock->type == SILC_SOCKET_TYPE_CLIENT ||
2583       server->server_type == SILC_SERVER ||
2584       packet->src_id_type != SILC_ID_SERVER)
2585     return;
2586
2587   idp = silc_id_payload_parse(buffer->data, buffer->len);
2588   if (!idp)
2589     return;
2590
2591   id_type = silc_id_payload_get_type(idp);
2592
2593   /* Normal server cannot have other normal server connections */
2594   server_entry = (SilcServerEntry)sock->user_data;
2595   if (id_type == SILC_ID_SERVER && sock->type == SILC_SOCKET_TYPE_SERVER &&
2596       server_entry->server_type == SILC_SERVER)
2597     goto out;
2598
2599   id = silc_id_payload_get_id(idp);
2600   if (!id)
2601     goto out;
2602
2603   /* If the packet is coming from server then use the sender as the
2604      origin of the the packet. If it came from router then check the real
2605      sender of the packet and use that as the origin. */
2606   if (sock->type == SILC_SOCKET_TYPE_SERVER) {
2607     id_list = server->local_list;
2608     router_sock = sock;
2609     router = sock->user_data;
2610
2611     /* If the sender is backup router and ID is server (and we are not
2612        backup router) then switch the entry to global list. */
2613     if (server_entry->server_type == SILC_BACKUP_ROUTER && 
2614         id_type == SILC_ID_SERVER && 
2615         server->id_entry->server_type != SILC_BACKUP_ROUTER) {
2616       id_list = server->global_list;
2617       router_sock = server->router ? SILC_PRIMARY_ROUTE(server) : sock;
2618     }
2619   } else {
2620     void *sender_id = silc_id_str2id(packet->src_id, packet->src_id_len,
2621                                      packet->src_id_type);
2622     router = silc_idlist_find_server_by_id(server->global_list,
2623                                            sender_id, TRUE, NULL);
2624     if (!router)
2625       router = silc_idlist_find_server_by_id(server->local_list,
2626                                              sender_id, TRUE, NULL);
2627     silc_free(sender_id);
2628     router_sock = sock;
2629     id_list = server->global_list;
2630   }
2631
2632   if (!router)
2633     goto out;
2634
2635   switch(id_type) {
2636   case SILC_ID_CLIENT:
2637     {
2638       SilcClientEntry entry;
2639
2640       /* Check that we do not have this client already */
2641       entry = silc_idlist_find_client_by_id(server->global_list, 
2642                                             id, server->server_type, 
2643                                             NULL);
2644       if (!entry)
2645         entry = silc_idlist_find_client_by_id(server->local_list, 
2646                                               id, server->server_type,
2647                                               NULL);
2648       if (entry) {
2649         SILC_LOG_DEBUG(("Ignoring client that we already have"));
2650         goto out;
2651       }
2652
2653       SILC_LOG_DEBUG(("New client id(%s) from [%s] %s",
2654                       silc_id_render(id, SILC_ID_CLIENT),
2655                       sock->type == SILC_SOCKET_TYPE_SERVER ?
2656                       "Server" : "Router", sock->hostname));
2657     
2658       /* As a router we keep information of all global information in our
2659          global list. Cell wide information however is kept in the local
2660          list. */
2661       entry = silc_idlist_add_client(id_list, NULL, NULL, NULL, 
2662                                      id, router, NULL, 0);
2663       if (!entry) {
2664         SILC_LOG_ERROR(("Could not add new client to the ID Cache"));
2665
2666         /* Inform the sender that the ID is not usable */
2667         silc_server_send_notify_signoff(server, sock, FALSE, id, NULL);
2668         goto out;
2669       }
2670       entry->nickname = NULL;
2671       entry->data.status |= SILC_IDLIST_STATUS_REGISTERED;
2672
2673       if (sock->type == SILC_SOCKET_TYPE_SERVER)
2674         server->stat.cell_clients++;
2675       server->stat.clients++;
2676
2677       /* Check if anyone is watching this nickname */
2678       if (server->server_type == SILC_ROUTER && id_list == server->local_list)
2679         silc_server_check_watcher_list(server, entry, NULL, 0);
2680     }
2681     break;
2682
2683   case SILC_ID_SERVER:
2684     {
2685       SilcServerEntry entry;
2686
2687       /* If the ID is mine, ignore it. */
2688       if (SILC_ID_SERVER_COMPARE(id, server->id)) {
2689         SILC_LOG_DEBUG(("Ignoring my own ID as new ID"));
2690         break;
2691       }
2692
2693       /* If the ID is the sender's ID, ignore it (we have it already) */
2694       if (SILC_ID_SERVER_COMPARE(id, router->id)) {
2695         SILC_LOG_DEBUG(("Ignoring sender's own ID"));
2696         break;
2697       }
2698       
2699       /* Check that we do not have this server already */
2700       entry = silc_idlist_find_server_by_id(server->global_list, 
2701                                             id, server->server_type, 
2702                                             NULL);
2703       if (!entry)
2704         entry = silc_idlist_find_server_by_id(server->local_list, 
2705                                               id, server->server_type,
2706                                               NULL);
2707       if (entry) {
2708         SILC_LOG_DEBUG(("Ignoring server that we already have"));
2709         goto out;
2710       }
2711
2712       SILC_LOG_DEBUG(("New server id(%s) from [%s] %s",
2713                       silc_id_render(id, SILC_ID_SERVER),
2714                       sock->type == SILC_SOCKET_TYPE_SERVER ?
2715                       "Server" : "Router", sock->hostname));
2716       
2717       /* As a router we keep information of all global information in our 
2718          global list. Cell wide information however is kept in the local
2719          list. */
2720       entry = silc_idlist_add_server(id_list, NULL, 0, id, router, 
2721                                      router_sock);
2722       if (!entry) {
2723         SILC_LOG_ERROR(("Could not add new server to the ID Cache"));
2724         goto out;
2725       }
2726       entry->data.status |= SILC_IDLIST_STATUS_REGISTERED;
2727       
2728       if (sock->type == SILC_SOCKET_TYPE_SERVER)
2729         server->stat.cell_servers++;
2730       server->stat.servers++;
2731     }
2732     break;
2733
2734   case SILC_ID_CHANNEL:
2735     SILC_LOG_ERROR(("Channel cannot be registered with NEW_ID packet"));
2736     goto out;
2737     break;
2738
2739   default:
2740     goto out;
2741     break;
2742   }
2743
2744   /* If the sender of this packet is server and we are router we need to
2745      broadcast this packet to other routers in the network. */
2746   if (broadcast && server->server_type == SILC_ROUTER &&
2747       sock->type == SILC_SOCKET_TYPE_SERVER &&
2748       !(packet->flags & SILC_PACKET_FLAG_BROADCAST)) {
2749     SILC_LOG_DEBUG(("Broadcasting received New ID packet"));
2750     silc_server_packet_send(server, SILC_PRIMARY_ROUTE(server),
2751                             packet->type, 
2752                             packet->flags | SILC_PACKET_FLAG_BROADCAST,
2753                             buffer->data, buffer->len, FALSE);
2754     silc_server_backup_send(server, sock->user_data, 
2755                             packet->type, packet->flags,
2756                             packet->buffer->data, packet->buffer->len, 
2757                             FALSE, TRUE);
2758   }
2759
2760  out:
2761   silc_id_payload_free(idp);
2762 }
2763
2764
2765 /* Processes incoming New ID packet. New ID Payload is used to distribute
2766    information about newly registered clients and servers. */
2767
2768 void silc_server_new_id(SilcServer server, SilcSocketConnection sock,
2769                         SilcPacketContext *packet)
2770 {
2771   silc_server_new_id_real(server, sock, packet, TRUE);
2772 }
2773
2774 /* Receoved New Id List packet, list of New ID payloads inside one
2775    packet. Process the New ID payloads one by one. */
2776
2777 void silc_server_new_id_list(SilcServer server, SilcSocketConnection sock,
2778                              SilcPacketContext *packet)
2779 {
2780   SilcPacketContext *new_id;
2781   SilcBuffer idp;
2782   SilcUInt16 id_len;
2783
2784   SILC_LOG_DEBUG(("Processing New ID List"));
2785
2786   if (sock->type == SILC_SOCKET_TYPE_CLIENT ||
2787       packet->src_id_type != SILC_ID_SERVER)
2788     return;
2789
2790   /* If the sender of this packet is server and we are router we need to
2791      broadcast this packet to other routers in the network. Broadcast
2792      this list packet instead of multiple New ID packets. */
2793   if (server->server_type == SILC_ROUTER &&
2794       sock->type == SILC_SOCKET_TYPE_SERVER &&
2795       !(packet->flags & SILC_PACKET_FLAG_BROADCAST)) {
2796     SILC_LOG_DEBUG(("Broadcasting received New ID List packet"));
2797     silc_server_packet_send(server, SILC_PRIMARY_ROUTE(server),
2798                             packet->type, 
2799                             packet->flags | SILC_PACKET_FLAG_BROADCAST,
2800                             packet->buffer->data, 
2801                             packet->buffer->len, FALSE);
2802     silc_server_backup_send(server, sock->user_data, 
2803                             packet->type, packet->flags,
2804                             packet->buffer->data, packet->buffer->len, 
2805                             FALSE, TRUE);
2806   }
2807
2808   /* Make copy of the original packet context, except for the actual
2809      data buffer, which we will here now fetch from the original buffer. */
2810   new_id = silc_packet_context_alloc();
2811   new_id->type = SILC_PACKET_NEW_ID;
2812   new_id->flags = packet->flags & (~SILC_PACKET_FLAG_LIST);
2813   new_id->src_id = packet->src_id;
2814   new_id->src_id_len = packet->src_id_len;
2815   new_id->src_id_type = packet->src_id_type;
2816   new_id->dst_id = packet->dst_id;
2817   new_id->dst_id_len = packet->dst_id_len;
2818   new_id->dst_id_type = packet->dst_id_type;
2819
2820   idp = silc_buffer_alloc(256);
2821   new_id->buffer = idp;
2822
2823   while (packet->buffer->len) {
2824     SILC_GET16_MSB(id_len, packet->buffer->data + 2);
2825     if ((id_len > packet->buffer->len) ||
2826         (id_len > idp->truelen))
2827       break;
2828
2829     silc_buffer_pull_tail(idp, 4 + id_len);
2830     silc_buffer_put(idp, packet->buffer->data, 4 + id_len);
2831
2832     /* Process the New ID */
2833     silc_server_new_id_real(server, sock, new_id, FALSE);
2834
2835     silc_buffer_push_tail(idp, 4 + id_len);
2836     silc_buffer_pull(packet->buffer, 4 + id_len);
2837   }
2838
2839   silc_buffer_free(idp);
2840   silc_free(new_id);
2841 }
2842
2843 /* Received New Channel packet. Information about new channels in the 
2844    network are distributed using this packet. Save the information about
2845    the new channel. This usually comes from router but also normal server
2846    can send this to notify channels it has when it connects to us. */
2847
2848 void silc_server_new_channel(SilcServer server,
2849                              SilcSocketConnection sock,
2850                              SilcPacketContext *packet)
2851 {
2852   SilcChannelPayload payload;
2853   SilcChannelID *channel_id;
2854   char *channel_name;
2855   SilcUInt32 name_len;
2856   unsigned char *id;
2857   SilcUInt32 id_len, cipher_len;
2858   SilcServerEntry server_entry;
2859   SilcChannelEntry channel;
2860   const char *cipher;
2861
2862   if (sock->type == SILC_SOCKET_TYPE_CLIENT ||
2863       packet->src_id_type != SILC_ID_SERVER ||
2864       server->server_type == SILC_SERVER)
2865     return;
2866
2867   /* Parse the channel payload */
2868   payload = silc_channel_payload_parse(packet->buffer->data,
2869                                        packet->buffer->len);
2870   if (!payload)
2871     return;
2872     
2873   /* Get the channel ID */
2874   channel_id = silc_channel_get_id_parse(payload);
2875   if (!channel_id) {
2876     silc_channel_payload_free(payload);
2877     return;
2878   }
2879
2880   channel_name = silc_channel_get_name(payload, &name_len);
2881   if (name_len > 256)
2882     channel_name[255] = '\0';
2883
2884   id = silc_channel_get_id(payload, &id_len);
2885
2886   server_entry = (SilcServerEntry)sock->user_data;
2887
2888   if (sock->type == SILC_SOCKET_TYPE_ROUTER) {
2889     /* Add the channel to global list as it is coming from router. It 
2890        cannot be our own channel as it is coming from router. */
2891
2892     /* Check that we don't already have this channel */
2893     channel = silc_idlist_find_channel_by_name(server->local_list, 
2894                                                channel_name, NULL);
2895     if (!channel)
2896       channel = silc_idlist_find_channel_by_name(server->global_list, 
2897                                                  channel_name, NULL);
2898     if (!channel) {
2899       SILC_LOG_DEBUG(("New channel id(%s) from [Router] %s",
2900                       silc_id_render(channel_id, SILC_ID_CHANNEL), 
2901                       sock->hostname));
2902     
2903       channel = 
2904         silc_idlist_add_channel(server->global_list, strdup(channel_name), 
2905                                 0, channel_id, sock->user_data, NULL, NULL, 0);
2906       if (!channel) {
2907         silc_channel_payload_free(payload);
2908         silc_free(channel_id);
2909         return;
2910       }
2911       channel->disabled = TRUE;    /* Disabled until someone JOINs */
2912
2913       server->stat.channels++;
2914       if (server->server_type == SILC_ROUTER)
2915         channel->users_resolved = TRUE;
2916     }
2917   } else {
2918     /* The channel is coming from our server, thus it is in our cell
2919        we will add it to our local list. */
2920     SilcBuffer chk;
2921
2922     SILC_LOG_DEBUG(("Channel id(%s) from [Server] %s",
2923                     silc_id_render(channel_id, SILC_ID_CHANNEL), 
2924                     sock->hostname));
2925
2926     /* Check that we don't already have this channel */
2927     channel = silc_idlist_find_channel_by_name(server->local_list, 
2928                                                channel_name, NULL);
2929     if (!channel)
2930       channel = silc_idlist_find_channel_by_name(server->global_list, 
2931                                                  channel_name, NULL);
2932
2933     /* If the channel does not exist, then create it. This creates a new
2934        key to the channel as well that we will send to the server. */
2935     if (!channel) {
2936       SILC_LOG_DEBUG(("Channel is new to us"));
2937
2938       /* The protocol says that the Channel ID's IP address must be based
2939          on the router's IP address.  Check whether the ID is based in our
2940          IP and if it is not then create a new ID and enforce the server
2941          to switch the ID. */
2942       if (server_entry->server_type != SILC_BACKUP_ROUTER &&
2943           !SILC_ID_COMPARE(channel_id, server->id, server->id->ip.data_len)) {
2944         SilcChannelID *tmp;
2945         SILC_LOG_DEBUG(("Forcing the server to change Channel ID"));
2946         if (silc_id_create_channel_id(server, server->id, server->rng, &tmp)) {
2947           silc_server_send_notify_channel_change(server, sock, FALSE, 
2948                                                  channel_id, tmp);
2949           silc_channel_payload_free(payload);
2950           silc_free(channel_id);
2951           silc_free(tmp);
2952         }
2953
2954         /* Wait that server re-announces this channel */
2955         return;
2956       }
2957
2958       /* Create the channel with the provided Channel ID */
2959       channel = silc_server_create_new_channel_with_id(server, NULL, NULL,
2960                                                        channel_name,
2961                                                        channel_id, FALSE);
2962       if (!channel) {
2963         silc_channel_payload_free(payload);
2964         silc_free(channel_id);
2965         return;
2966       }
2967       channel->disabled = TRUE;    /* Disabled until someone JOINs */
2968
2969 #if 0 /* We assume that CMODE_CHANGE notify is sent to us after this. */
2970
2971       /* XXX Dunno if this is supposed to be set in any server type.  If set
2972          here the CMODE_CHANGE that may follow sets mode that we already
2973          have, and we may loose data from the CMODE_CHANGE notify. */
2974       if (server_entry->server_type != SILC_BACKUP_ROUTER)
2975         channel->mode = silc_channel_get_mode(payload);
2976 #endif
2977
2978       /* Send the new channel key to the server */
2979       id = silc_id_id2str(channel->id, SILC_ID_CHANNEL);
2980       id_len = silc_id_get_len(channel->id, SILC_ID_CHANNEL);
2981       cipher = silc_cipher_get_name(channel->channel_key);
2982       cipher_len = strlen(cipher);
2983       chk = silc_channel_key_payload_encode(id_len, id,
2984                                             cipher_len, cipher,
2985                                             channel->key_len / 8, 
2986                                             channel->key);
2987       silc_server_packet_send(server, sock, SILC_PACKET_CHANNEL_KEY, 0, 
2988                               chk->data, chk->len, FALSE);
2989       silc_buffer_free(chk);
2990       silc_free(id);
2991     } else {
2992       /* The channel exist by that name, check whether the ID's match.
2993          If they don't then we'll force the server to use the ID we have.
2994          We also create a new key for the channel. */
2995       SilcBuffer modes = NULL, users = NULL, users_modes = NULL;
2996
2997       SILC_LOG_DEBUG(("Channel already exists"));
2998
2999       if (!SILC_ID_CHANNEL_COMPARE(channel_id, channel->id)) {
3000         /* They don't match, send CHANNEL_CHANGE notify to the server to
3001            force the ID change. */
3002         SILC_LOG_DEBUG(("Forcing the server to change Channel ID"));
3003         silc_server_send_notify_channel_change(server, sock, FALSE, 
3004                                                channel_id, channel->id);
3005         silc_channel_payload_free(payload);
3006         silc_free(channel_id);
3007
3008         /* Wait that server re-announces this channel */
3009         return;
3010       }
3011
3012 #if 0 /* We will announce our CMODE anyway for this channel, so no need
3013          to check it (implicit enforce). */
3014
3015       /* If the mode is different from what we have then enforce the
3016          mode change. */
3017       mode = silc_channel_get_mode(payload);
3018       if (channel->mode != mode) {
3019         SILC_LOG_DEBUG(("Forcing the server to change channel mode"));
3020         silc_server_send_notify_cmode(server, sock, FALSE, channel,
3021                                       channel->mode, server->id,
3022                                       SILC_ID_SERVER, channel->cipher,
3023                                       channel->hmac_name,
3024                                       channel->passphrase,
3025                                       channel->founder_key);
3026       }
3027 #endif
3028
3029       /* Create new key for the channel and send it to the server and
3030          everybody else possibly on the channel. */
3031       if (!(channel->mode & SILC_CHANNEL_MODE_PRIVKEY)) {
3032
3033         if (silc_hash_table_count(channel->user_list)) {
3034           if (!silc_server_create_channel_key(server, channel, 0)) {
3035             silc_channel_payload_free(payload);
3036             silc_free(channel_id);
3037             return;
3038           }
3039
3040           /* Send to the channel */
3041           silc_server_send_channel_key(server, sock, channel, FALSE);
3042         }
3043
3044         /* Send to the server */
3045         id = silc_id_id2str(channel->id, SILC_ID_CHANNEL);
3046         id_len = silc_id_get_len(channel->id, SILC_ID_CHANNEL);
3047         cipher = silc_cipher_get_name(channel->channel_key);
3048         cipher_len = strlen(cipher);
3049         chk = silc_channel_key_payload_encode(id_len, id,
3050                                               cipher_len, cipher,
3051                                               channel->key_len / 8, 
3052                                               channel->key);
3053         silc_server_packet_send(server, sock, SILC_PACKET_CHANNEL_KEY, 0, 
3054                                 chk->data, chk->len, FALSE);
3055         silc_buffer_free(chk);
3056         silc_free(id);
3057       }
3058
3059       silc_free(channel_id);
3060
3061       /* Since the channel is coming from server and we also know about it
3062          then send the JOIN notify to the server so that it see's our
3063          users on the channel "joining" the channel. */
3064       silc_server_announce_get_channel_users(server, channel, &modes, &users,
3065                                              &users_modes);
3066       if (users) {
3067         silc_buffer_push(users, users->data - users->head);
3068         silc_server_packet_send(server, sock,
3069                                 SILC_PACKET_NOTIFY, SILC_PACKET_FLAG_LIST,
3070                                 users->data, users->len, FALSE);
3071         silc_buffer_free(users);
3072       }
3073       if (modes) {
3074         silc_buffer_push(modes, modes->data - modes->head);
3075         silc_server_packet_send_dest(server, sock,
3076                                      SILC_PACKET_NOTIFY, SILC_PACKET_FLAG_LIST,
3077                                      channel->id, SILC_ID_CHANNEL,
3078                                      modes->data, modes->len, FALSE);
3079         silc_buffer_free(modes);
3080       }
3081       if (users_modes) {
3082         silc_buffer_push(users_modes, users_modes->data - users_modes->head);
3083         silc_server_packet_send_dest(server, sock,
3084                                      SILC_PACKET_NOTIFY, SILC_PACKET_FLAG_LIST,
3085                                      channel->id, SILC_ID_CHANNEL,
3086                                      users_modes->data, 
3087                                      users_modes->len, FALSE);
3088         silc_buffer_free(users_modes);
3089       }
3090       if (channel->topic) {
3091         silc_server_send_notify_topic_set(server, sock,
3092                                           server->server_type == SILC_ROUTER ?
3093                                           TRUE : FALSE, channel, 
3094                                           server->id, SILC_ID_SERVER,
3095                                           channel->topic);
3096       }
3097     }
3098   }
3099
3100   /* If the sender of this packet is server and we are router we need to
3101      broadcast this packet to other routers in the network. Broadcast
3102      this list packet instead of multiple New Channel packets. */
3103   if (server->server_type == SILC_ROUTER &&
3104       sock->type == SILC_SOCKET_TYPE_SERVER &&
3105       !(packet->flags & SILC_PACKET_FLAG_BROADCAST)) {
3106     SILC_LOG_DEBUG(("Broadcasting received New Channel packet"));
3107     silc_server_packet_send(server, SILC_PRIMARY_ROUTE(server),
3108                             packet->type, 
3109                             packet->flags | SILC_PACKET_FLAG_BROADCAST,
3110                             packet->buffer->data, 
3111                             packet->buffer->len, FALSE);
3112     silc_server_backup_send(server, sock->user_data, 
3113                             packet->type, packet->flags,
3114                             packet->buffer->data, packet->buffer->len, 
3115                             FALSE, TRUE);
3116   }
3117
3118   silc_channel_payload_free(payload);
3119 }
3120
3121 /* Received New Channel List packet, list of New Channel List payloads inside
3122    one packet. Process the New Channel payloads one by one. */
3123
3124 void silc_server_new_channel_list(SilcServer server,
3125                                   SilcSocketConnection sock,
3126                                   SilcPacketContext *packet)
3127 {
3128   SilcPacketContext *new;
3129   SilcBuffer buffer;
3130   SilcUInt16 len1, len2;
3131
3132   SILC_LOG_DEBUG(("Processing New Channel List"));
3133
3134   if (sock->type == SILC_SOCKET_TYPE_CLIENT ||
3135       packet->src_id_type != SILC_ID_SERVER ||
3136       server->server_type == SILC_SERVER)
3137     return;
3138
3139   /* Make copy of the original packet context, except for the actual
3140      data buffer, which we will here now fetch from the original buffer. */
3141   new = silc_packet_context_alloc();
3142   new->type = SILC_PACKET_NEW_CHANNEL;
3143   new->flags = packet->flags & (~SILC_PACKET_FLAG_LIST);
3144   new->src_id = packet->src_id;
3145   new->src_id_len = packet->src_id_len;
3146   new->src_id_type = packet->src_id_type;
3147   new->dst_id = packet->dst_id;
3148   new->dst_id_len = packet->dst_id_len;
3149   new->dst_id_type = packet->dst_id_type;
3150
3151   buffer = silc_buffer_alloc(512);
3152   new->buffer = buffer;
3153
3154   while (packet->buffer->len) {
3155     SILC_GET16_MSB(len1, packet->buffer->data);
3156     if ((len1 > packet->buffer->len) ||
3157         (len1 > buffer->truelen))
3158       break;
3159
3160     SILC_GET16_MSB(len2, packet->buffer->data + 2 + len1);
3161     if ((len2 > packet->buffer->len) ||
3162         (len2 > buffer->truelen))
3163       break;
3164
3165     silc_buffer_pull_tail(buffer, 8 + len1 + len2);
3166     silc_buffer_put(buffer, packet->buffer->data, 8 + len1 + len2);
3167
3168     /* Process the New Channel */
3169     silc_server_new_channel(server, sock, new);
3170
3171     silc_buffer_push_tail(buffer, 8 + len1 + len2);
3172     silc_buffer_pull(packet->buffer, 8 + len1 + len2);
3173   }
3174
3175   silc_buffer_free(buffer);
3176   silc_free(new);
3177 }
3178
3179 /* Received key agreement packet. This packet is never for us. It is to
3180    the client in the packet's destination ID. Sending of this sort of packet
3181    equals sending private message, ie. it is sent point to point from
3182    one client to another. */
3183
3184 void silc_server_key_agreement(SilcServer server,
3185                                SilcSocketConnection sock,
3186                                SilcPacketContext *packet)
3187 {
3188   SilcSocketConnection dst_sock;
3189   SilcIDListData idata;
3190
3191   SILC_LOG_DEBUG(("Start"));
3192
3193   if (packet->src_id_type != SILC_ID_CLIENT ||
3194       packet->dst_id_type != SILC_ID_CLIENT)
3195     return;
3196
3197   if (!packet->dst_id)
3198     return;
3199
3200   /* Get the route to the client */
3201   dst_sock = silc_server_get_client_route(server, packet->dst_id,
3202                                           packet->dst_id_len, NULL, 
3203                                           &idata, NULL);
3204   if (!dst_sock)
3205     return;
3206
3207   /* Relay the packet */
3208   silc_server_relay_packet(server, dst_sock, idata->send_key,
3209                            idata->hmac_send, idata->psn_send++,
3210                            packet, FALSE);
3211 }
3212
3213 /* Received connection auth request packet that is used during connection
3214    phase to resolve the mandatory authentication method.  This packet can
3215    actually be received at anytime but usually it is used only during
3216    the connection authentication phase. Now, protocol says that this packet
3217    can come from client or server, however, we support only this coming
3218    from client and expect that server always knows what authentication
3219    method to use. */
3220
3221 void silc_server_connection_auth_request(SilcServer server,
3222                                          SilcSocketConnection sock,
3223                                          SilcPacketContext *packet)
3224 {
3225   SilcServerConfigClient *client = NULL;
3226   SilcUInt16 conn_type;
3227   int ret;
3228   SilcAuthMethod auth_meth = SILC_AUTH_NONE;
3229
3230   if (packet->src_id_type && packet->src_id_type != SILC_ID_CLIENT) {
3231     SILC_LOG_DEBUG(("Request not from client"));
3232     return;
3233   }
3234
3235   /* Parse the payload */
3236   ret = silc_buffer_unformat(packet->buffer,
3237                              SILC_STR_UI_SHORT(&conn_type),
3238                              SILC_STR_UI_SHORT(NULL),
3239                              SILC_STR_END);
3240   if (ret == -1)
3241     return;
3242
3243   if (conn_type != SILC_SOCKET_TYPE_CLIENT)
3244     return;
3245
3246   /* Get the authentication method for the client */
3247   auth_meth = SILC_AUTH_NONE;
3248   client = silc_server_config_find_client(server, sock->ip);
3249   if (!client)
3250     client = silc_server_config_find_client(server, sock->hostname);
3251   if (client) {
3252     if (client->passphrase) {
3253       if (client->publickeys && !server->config->prefer_passphrase_auth)
3254         auth_meth = SILC_AUTH_PUBLIC_KEY;
3255       else
3256         auth_meth = SILC_AUTH_PASSWORD;
3257     } else if (client->publickeys)
3258       auth_meth = SILC_AUTH_PUBLIC_KEY;
3259   }
3260
3261   SILC_LOG_DEBUG(("Authentication method is [%s]",
3262                   (auth_meth == SILC_AUTH_NONE ? "None" :
3263                    auth_meth == SILC_AUTH_PASSWORD ? "Passphrase" :
3264                    "Digital signatures")));
3265
3266   /* Send it back to the client */
3267   silc_server_send_connection_auth_request(server, sock, conn_type, auth_meth);
3268 }
3269
3270 /* Received REKEY packet. The sender of the packet wants to regenerate
3271    its session keys. This starts the REKEY protocol. */
3272
3273 void silc_server_rekey(SilcServer server,
3274                        SilcSocketConnection sock,
3275                        SilcPacketContext *packet)
3276 {
3277   SilcProtocol protocol;
3278   SilcServerRekeyInternalContext *proto_ctx;
3279   SilcIDListData idata = (SilcIDListData)sock->user_data;
3280
3281   SILC_LOG_DEBUG(("Start"));
3282
3283   /* Allocate internal protocol context. This is sent as context
3284      to the protocol. */
3285   proto_ctx = silc_calloc(1, sizeof(*proto_ctx));
3286   proto_ctx->server = (void *)server;
3287   proto_ctx->sock = sock;
3288   proto_ctx->responder = TRUE;
3289   proto_ctx->pfs = idata->rekey->pfs;
3290       
3291   /* Perform rekey protocol. Will call the final callback after the
3292      protocol is over. */
3293   silc_protocol_alloc(SILC_PROTOCOL_SERVER_REKEY, 
3294                       &protocol, proto_ctx, silc_server_rekey_final);
3295   sock->protocol = protocol;
3296
3297   if (proto_ctx->pfs == FALSE)
3298     /* Run the protocol */
3299     silc_protocol_execute(protocol, server->schedule, 0, 0);
3300 }
3301
3302 /* Received file transger packet. This packet is never for us. It is to
3303    the client in the packet's destination ID. Sending of this sort of packet
3304    equals sending private message, ie. it is sent point to point from
3305    one client to another. */
3306
3307 void silc_server_ftp(SilcServer server,
3308                      SilcSocketConnection sock,
3309                      SilcPacketContext *packet)
3310 {
3311   SilcSocketConnection dst_sock;
3312   SilcIDListData idata;
3313
3314   SILC_LOG_DEBUG(("Start"));
3315
3316   if (packet->src_id_type != SILC_ID_CLIENT ||
3317       packet->dst_id_type != SILC_ID_CLIENT)
3318     return;
3319
3320   if (!packet->dst_id)
3321     return;
3322
3323   /* Get the route to the client */
3324   dst_sock = silc_server_get_client_route(server, packet->dst_id,
3325                                           packet->dst_id_len, NULL, 
3326                                           &idata, NULL);
3327   if (!dst_sock)
3328     return;
3329
3330   /* Relay the packet */
3331   silc_server_relay_packet(server, dst_sock, idata->send_key,
3332                            idata->hmac_send, idata->psn_send++,
3333                            packet, FALSE);
3334 }
3335
3336 typedef struct {
3337   SilcServer server;
3338   SilcSocketConnection sock;
3339   SilcPacketContext *packet;
3340   void *data;
3341 } *SilcServerResumeResolve;
3342
3343 SILC_SERVER_CMD_FUNC(resume_resolve)
3344 {
3345   SilcServerResumeResolve r = (SilcServerResumeResolve)context;
3346   SilcServer server = r->server;
3347   SilcSocketConnection sock = r->sock;
3348   SilcServerCommandReplyContext reply = context2;
3349   SilcClientEntry client;
3350
3351   SILC_LOG_DEBUG(("Start"));
3352
3353   if (!reply || !silc_command_get_status(reply->payload, NULL, NULL)) {
3354     SILC_LOG_ERROR(("Client %s (%s) tried to resume unknown client, "
3355                     "closing connection", sock->hostname, sock->ip));
3356     silc_server_disconnect_remote(server, sock,
3357                                   SILC_STATUS_ERR_INCOMPLETE_INFORMATION,
3358                                   "Resuming not possible");
3359     if (sock->user_data)
3360       silc_server_free_sock_user_data(server, sock, NULL);
3361     goto out;
3362   }
3363
3364   if (reply && silc_command_get(reply->payload) == SILC_COMMAND_WHOIS) {
3365     /* Get entry to the client, and resolve it if we don't have it. */
3366     client = silc_idlist_find_client_by_id(server->local_list, 
3367                                            r->data, TRUE, NULL);
3368     if (!client) {
3369       client = silc_idlist_find_client_by_id(server->global_list,
3370                                              r->data, TRUE, NULL);
3371       if (!client) {
3372         SILC_LOG_ERROR(("Client %s (%s) tried to resume unknown client, "
3373                         "closing connection", sock->hostname, sock->ip));
3374         silc_server_disconnect_remote(server, sock,
3375                                       SILC_STATUS_ERR_INCOMPLETE_INFORMATION,
3376                                       "Resuming not possible");
3377         if (sock->user_data)
3378           silc_server_free_sock_user_data(server, sock, NULL);
3379         goto out;
3380       }
3381     }
3382
3383     if (!(client->mode & SILC_UMODE_DETACHED)) {
3384       SILC_LOG_ERROR(("Client %s (%s) tried to resume un-detached client, "
3385                       "closing connection", sock->hostname, sock->ip));
3386       silc_server_disconnect_remote(server, sock,
3387                                     SILC_STATUS_ERR_INCOMPLETE_INFORMATION,
3388                                     "Resuming not possible");
3389       if (sock->user_data)
3390         silc_server_free_sock_user_data(server, sock, NULL);
3391       goto out;
3392     }
3393
3394     client->data.status |= SILC_IDLIST_STATUS_RESUME_RES;
3395   }
3396
3397   /* Reprocess the packet */
3398   silc_server_resume_client(server, sock, r->packet);
3399
3400  out:
3401   silc_socket_free(r->sock);
3402   silc_packet_context_free(r->packet);
3403   silc_free(r->data);
3404   silc_free(r);
3405 }
3406
3407 /* Received client resuming packet.  This is used to resume detached
3408    client session.  It can be sent by the client who wishes to resume
3409    but this is also sent by servers and routers to notify other routers
3410    that the client is not detached anymore. */
3411
3412 void silc_server_resume_client(SilcServer server,
3413                                SilcSocketConnection sock,
3414                                SilcPacketContext *packet)
3415 {
3416   SilcBuffer buffer = packet->buffer, buf;
3417   SilcIDListData idata;
3418   SilcIDCacheEntry id_cache = NULL;
3419   SilcClientEntry detached_client;
3420   SilcClientID *client_id = NULL;
3421   unsigned char *id_string, *auth = NULL;
3422   SilcUInt16 id_len, auth_len = 0;
3423   int ret, nickfail = 0;
3424   bool resolved, local, nick_change = FALSE, resolve = FALSE;
3425   SilcChannelEntry channel;
3426   SilcHashTableList htl;
3427   SilcChannelClientEntry chl;
3428   SilcServerResumeResolve r;
3429   const char *cipher;
3430
3431   ret = silc_buffer_unformat(buffer,
3432                              SILC_STR_UI16_NSTRING(&id_string, &id_len),
3433                              SILC_STR_END);
3434   if (ret != -1)
3435     client_id = silc_id_str2id(id_string, id_len, SILC_ID_CLIENT);
3436
3437   if (sock->type == SILC_SOCKET_TYPE_CLIENT) {
3438     /* Client send this and is attempting to resume to old client session */
3439     SilcClientEntry client;
3440     SilcBuffer keyp;
3441
3442     if (ret != -1) {
3443       silc_buffer_pull(buffer, 2 + id_len);
3444       auth = buffer->data;
3445       auth_len = buffer->len;
3446       silc_buffer_push(buffer, 2 + id_len);
3447     }
3448
3449     if (!client_id || auth_len < 128) {
3450       SILC_LOG_ERROR(("Client %s (%s) sent incomplete resume information, "
3451                       "closing connection", sock->hostname, sock->ip));
3452       silc_server_disconnect_remote(server, sock,
3453                                     SILC_STATUS_ERR_INCOMPLETE_INFORMATION,
3454                                     "Resuming not possible");
3455       if (sock->user_data)
3456         silc_server_free_sock_user_data(server, sock, NULL);
3457       silc_free(client_id);
3458       return;
3459     }
3460
3461     /* Take client entry of this connection */
3462     client = (SilcClientEntry)sock->user_data;
3463     idata = (SilcIDListData)client;
3464
3465     /* Get entry to the client, and resolve it if we don't have it. */
3466     detached_client = silc_server_query_client(server, client_id, FALSE,
3467                                                &resolved);
3468     if (!detached_client) {
3469       if (resolved) {
3470         /* The client info is being resolved. Reprocess this packet after
3471            receiving the reply to the query. */
3472         SILC_LOG_DEBUG(("Resolving client"));
3473         r = silc_calloc(1, sizeof(*r));
3474         if (!r)
3475           return;
3476         r->server = server;
3477         r->sock = silc_socket_dup(sock);
3478         r->packet = silc_packet_context_dup(packet);
3479         r->data = client_id;
3480         silc_server_command_pending(server, SILC_COMMAND_WHOIS,
3481                                     server->cmd_ident,
3482                                     silc_server_command_resume_resolve, r);
3483       } else {
3484         SILC_LOG_ERROR(("Client %s (%s) tried to resume unknown client, "
3485                         "closing connection", sock->hostname, sock->ip));
3486         silc_server_disconnect_remote(server, sock,
3487                                       SILC_STATUS_ERR_INCOMPLETE_INFORMATION,
3488                                       "Resuming not possible");
3489         if (sock->user_data)
3490           silc_server_free_sock_user_data(server, sock, NULL);
3491         silc_free(client_id);
3492       }
3493       return;
3494     }
3495
3496     if (!(detached_client->mode & SILC_UMODE_DETACHED))
3497       resolve = TRUE;
3498     if (!silc_hash_table_count(detached_client->channels) &&
3499         detached_client->router)
3500       resolve = TRUE;
3501     if (!detached_client->nickname)
3502       resolve = TRUE;
3503     if (detached_client->data.status & SILC_IDLIST_STATUS_RESUME_RES)
3504       resolve = FALSE;
3505
3506     if (resolve) {
3507       if (server->server_type == SILC_SERVER && !server->standalone) {
3508         /* The client info is being resolved. Reprocess this packet after
3509            receiving the reply to the query. */
3510         SILC_LOG_DEBUG(("Resolving client info"));
3511         silc_server_query_client(server, client_id, TRUE, NULL);
3512         r = silc_calloc(1, sizeof(*r));
3513         if (!r)
3514           return;
3515         r->server = server;
3516         r->sock = silc_socket_dup(sock);
3517         r->packet = silc_packet_context_dup(packet);
3518         r->data = client_id;
3519         silc_server_command_pending(server, SILC_COMMAND_WHOIS,
3520                                     server->cmd_ident,
3521                                     silc_server_command_resume_resolve, r);
3522         return;
3523       }
3524       if (server->server_type == SILC_SERVER) {
3525         SILC_LOG_ERROR(("Client %s (%s) tried to resume un-detached client, "
3526                         "closing connection", sock->hostname, sock->ip));
3527         silc_server_disconnect_remote(server, sock,
3528                                       SILC_STATUS_ERR_INCOMPLETE_INFORMATION,
3529                                       "Resuming not possible");
3530         if (sock->user_data)
3531           silc_server_free_sock_user_data(server, sock, NULL);
3532         silc_free(client_id);
3533         return;
3534       }
3535     }
3536
3537     /* Check that we have the public key of the client, if not then we must
3538        resolve it first. */
3539     if (!detached_client->data.public_key) {
3540       if (server->server_type == SILC_SERVER && server->standalone) {
3541         SILC_LOG_ERROR(("Detached client's public key not present, "
3542                         "closing connection"));
3543         silc_server_disconnect_remote(server, sock,
3544                                       SILC_STATUS_ERR_INCOMPLETE_INFORMATION,
3545                                       "Resuming not possible");
3546         if (sock->user_data)
3547           silc_server_free_sock_user_data(server, sock, NULL);
3548         silc_free(client_id);
3549       } else {
3550         /* We must retrieve the detached client's public key by sending
3551            GETKEY command. Reprocess this packet after receiving the key */
3552         SilcBuffer idp = silc_id_payload_encode(client_id, SILC_ID_CLIENT);
3553         SilcSocketConnection dest_sock = 
3554           silc_server_get_client_route(server, NULL, 0, client_id, NULL, NULL);
3555
3556         SILC_LOG_DEBUG(("Resolving client public key"));
3557
3558         silc_server_send_command(server, dest_sock ? dest_sock : 
3559                                  SILC_PRIMARY_ROUTE(server),
3560                                  SILC_COMMAND_GETKEY, ++server->cmd_ident,
3561                                  1, 1, idp->data, idp->len);
3562
3563         r = silc_calloc(1, sizeof(*r));
3564         if (!r) {
3565           silc_free(client_id);
3566           return;
3567         }
3568
3569         r->server = server;
3570         r->sock = silc_socket_dup(sock);
3571         r->packet = silc_packet_context_dup(packet);
3572         silc_server_command_pending(server, SILC_COMMAND_GETKEY,
3573                                     server->cmd_ident,
3574                                     silc_server_command_resume_resolve, r);
3575
3576         silc_buffer_free(idp);
3577       }
3578       silc_free(client_id);
3579       return;
3580     } else if (!silc_pkcs_public_key_compare(detached_client->data.public_key,
3581                                              idata->public_key)) {
3582       /* We require that the connection and resuming authentication data
3583          must be using same key pair. */
3584       SILC_LOG_ERROR(("Resuming attempted with wrong public key, "
3585                       "closing connection"));
3586       silc_server_disconnect_remote(server, sock,
3587                                     SILC_STATUS_ERR_INCOMPLETE_INFORMATION,
3588                                     "Resuming not possible");
3589       if (sock->user_data)
3590         silc_server_free_sock_user_data(server, sock, NULL);
3591       silc_free(client_id);
3592       return;
3593     }
3594
3595     /* Verify the authentication payload.  This has to be successful in
3596        order to allow the resuming */
3597     if (!idata->hash ||
3598         !silc_auth_verify_data(auth, auth_len, SILC_AUTH_PUBLIC_KEY,
3599                                detached_client->data.public_key, 0,
3600                                idata->hash, detached_client->id, 
3601                                SILC_ID_CLIENT)) {
3602       SILC_LOG_ERROR(("Client %s (%s) resume authentication failed, "
3603                       "closing connection", sock->hostname, sock->ip));
3604       silc_server_disconnect_remote(server, sock,
3605                                     SILC_STATUS_ERR_INCOMPLETE_INFORMATION,
3606                                     "Resuming not possible");
3607       if (sock->user_data)
3608         silc_server_free_sock_user_data(server, sock, NULL);
3609       silc_free(client_id);
3610       return;
3611     }
3612
3613     /* Now resume the client to the network */
3614
3615     silc_schedule_task_del_by_context(server->schedule, detached_client);
3616     sock->user_data = detached_client;
3617     detached_client->connection = sock;
3618
3619     /* Take new keys and stuff into use in the old entry */
3620     silc_idlist_del_data(detached_client);
3621     silc_idlist_add_data(detached_client, idata);
3622     detached_client->data.status |= SILC_IDLIST_STATUS_REGISTERED;
3623     detached_client->data.status |= SILC_IDLIST_STATUS_RESUMED;
3624     detached_client->data.status |= SILC_IDLIST_STATUS_LOCAL;
3625     detached_client->data.status &= ~SILC_IDLIST_STATUS_RESUME_RES;
3626     detached_client->mode &= ~SILC_UMODE_DETACHED;
3627     server->stat.my_detached--;
3628
3629     /* Send the RESUME_CLIENT packet to our primary router so that others
3630        know this client isn't detached anymore. */
3631     buf = silc_buffer_alloc_size(2 + id_len);
3632     silc_buffer_format(buf,
3633                        SILC_STR_UI_SHORT(id_len),
3634                        SILC_STR_UI_XNSTRING(id_string, id_len),
3635                        SILC_STR_END);
3636
3637     /* Send to primary router */
3638     silc_server_packet_send(server, SILC_PRIMARY_ROUTE(server),
3639                             SILC_PACKET_RESUME_CLIENT, 0, 
3640                             buf->data, buf->len, TRUE);
3641     silc_server_backup_send(server, detached_client->router,
3642                             SILC_PACKET_RESUME_CLIENT, 0,
3643                             buf->data, buf->len, TRUE, TRUE);
3644
3645     /* As router we must deliver this packet directly to the original
3646        server whom this client was earlier. */
3647     if (server->server_type == SILC_ROUTER && detached_client->router &&
3648         detached_client->router->server_type != SILC_ROUTER)
3649       silc_server_packet_send(server, detached_client->router->connection,
3650                               SILC_PACKET_RESUME_CLIENT, 0, 
3651                               buf->data, buf->len, TRUE);
3652     silc_buffer_free(buf);
3653
3654     detached_client->router = NULL;
3655
3656     /* Delete this client entry since we're resuming to old one. */
3657     server->stat.my_clients--;
3658     server->stat.clients--;
3659     if (server->stat.cell_clients)
3660       server->stat.cell_clients--;
3661     silc_server_remove_from_channels(server, NULL, client, FALSE, NULL, FALSE);
3662     silc_server_del_from_watcher_list(server, client);
3663     if (!silc_idlist_del_client(server->local_list, client))
3664       silc_idlist_del_client(server->global_list, client);
3665     client = detached_client;
3666     silc_free(client->servername);
3667     client->servername = strdup(server->server_name);
3668
3669     /* If the ID is not based in our ID then change it */
3670     if (!SILC_ID_COMPARE(client->id, server->id, server->id->ip.data_len)) {
3671       silc_free(client_id);
3672       while (!silc_id_create_client_id(server, server->id, server->rng, 
3673                                        server->md5hash, client->nickname, 
3674                                        &client_id)) {
3675         nickfail++;
3676         if (nickfail > 9) {
3677           silc_server_disconnect_remote(server, sock, 
3678                                         SILC_STATUS_ERR_BAD_NICKNAME, NULL);
3679           if (sock->user_data)
3680             silc_server_free_sock_user_data(server, sock, NULL);
3681           return;
3682         }
3683         snprintf(&client->nickname[strlen(client->nickname) - 1], 1, 
3684                  "%d", nickfail);
3685       }
3686       nick_change = TRUE;
3687     }
3688
3689     if (nick_change) {
3690       /* Notify about Client ID change, nickname doesn't actually change. */
3691       silc_server_send_notify_nick_change(server, SILC_PRIMARY_ROUTE(server),
3692                                           SILC_BROADCAST(server),
3693                                           client->id, client_id,
3694                                           client->nickname);
3695     }
3696
3697     /* Resolve users on those channels that client has joined but we
3698        haven't resolved user list yet. */
3699     if (server->server_type == SILC_SERVER && !server->standalone) {
3700       silc_hash_table_list(client->channels, &htl);
3701       while (silc_hash_table_get(&htl, NULL, (void **)&chl)) {
3702         channel = chl->channel;
3703         SILC_LOG_DEBUG(("Resolving users for %s channel", 
3704                         channel->channel_name));
3705         if (channel->disabled || !channel->users_resolved) {
3706           silc_server_send_command(server, SILC_PRIMARY_ROUTE(server),
3707                                    SILC_COMMAND_USERS, ++server->cmd_ident,
3708                                    1, 2, channel->channel_name,
3709                                    strlen(channel->channel_name));
3710         }
3711       }
3712       silc_hash_table_list_reset(&htl);
3713     }
3714
3715     /* Send the new client ID to the client. After this client may start
3716        receiving other packets, and may start sending packets too. */
3717     silc_server_send_new_id(server, sock, FALSE, client_id, SILC_ID_CLIENT,
3718                             silc_id_get_len(client_id, SILC_ID_CLIENT));
3719
3720     if (nick_change) {
3721       /* Send NICK change notify to channels as well. */
3722       SilcBuffer oidp, nidp;
3723       oidp = silc_id_payload_encode(client->id, SILC_ID_CLIENT);
3724       nidp = silc_id_payload_encode(client_id, SILC_ID_CLIENT);
3725       silc_server_send_notify_on_channels(server, NULL, client, 
3726                                           SILC_NOTIFY_TYPE_NICK_CHANGE, 3,
3727                                           oidp->data, oidp->len, 
3728                                           nidp->data, nidp->len,
3729                                           client->nickname, 
3730                                           strlen(client->nickname));
3731       silc_buffer_free(oidp);
3732       silc_buffer_free(nidp);
3733     }
3734
3735     /* Add the client again to the ID cache to get it to correct list */
3736     if (!silc_idcache_del_by_context(server->local_list->clients, client))
3737       silc_idcache_del_by_context(server->global_list->clients, client);
3738     silc_free(client->id);
3739     client->id = client_id;
3740     client_id = NULL;
3741     silc_idcache_add(server->local_list->clients, client->nickname,
3742                      client->id, client, 0, NULL);
3743
3744     /* Send some nice info to the client */
3745     silc_server_send_connect_notifys(server, sock, client);
3746
3747     /* Send all channel keys of channels the client has joined */
3748     silc_hash_table_list(client->channels, &htl);
3749     while (silc_hash_table_get(&htl, NULL, (void **)&chl)) {
3750       bool created = FALSE;
3751       channel = chl->channel;
3752
3753       if (channel->mode & SILC_CHANNEL_MODE_PRIVKEY)
3754         continue;
3755
3756       /* If we don't have channel key, then create one */
3757       if (!channel->channel_key) {
3758         if (!silc_server_create_channel_key(server, channel, 0))
3759           continue;
3760         created = TRUE;
3761       }
3762
3763       id_string = silc_id_id2str(channel->id, SILC_ID_CHANNEL);
3764       cipher = silc_cipher_get_name(channel->channel_key);
3765       keyp = 
3766         silc_channel_key_payload_encode(silc_id_get_len(channel->id,
3767                                                         SILC_ID_CHANNEL), 
3768                                         id_string,
3769                                         strlen(cipher), cipher,
3770                                         channel->key_len / 8, channel->key);
3771       silc_free(id_string);
3772
3773       /* Send the channel key to the client */
3774       silc_server_packet_send(server, sock, SILC_PACKET_CHANNEL_KEY, 0, 
3775                               keyp->data, keyp->len, FALSE);
3776
3777       /* Distribute the channel key to channel */
3778       if (created) {
3779         silc_server_send_channel_key(server, NULL, channel,
3780                                      server->server_type == SILC_ROUTER ? 
3781                                      FALSE : !server->standalone);
3782         silc_server_backup_send(server, NULL, SILC_PACKET_CHANNEL_KEY, 0,
3783                                 keyp->data, keyp->len, FALSE, TRUE);
3784       }
3785
3786       silc_buffer_free(keyp);
3787     }
3788     silc_hash_table_list_reset(&htl);
3789
3790   } else if (sock->type != SILC_SOCKET_TYPE_CLIENT) {
3791     /* Server or router sent this to us to notify that that a client has
3792        been resumed. */
3793     SilcServerEntry server_entry;
3794     SilcServerID *server_id;
3795
3796     if (!client_id) {
3797       SILC_LOG_DEBUG(("Malformed resuming packet"));
3798       return;
3799     }
3800
3801     /* Get entry to the client, and resolve it if we don't have it. */
3802     detached_client = silc_idlist_find_client_by_id(server->local_list, 
3803                                                     client_id, TRUE,
3804                                                     &id_cache);
3805     if (!detached_client) {
3806       detached_client = silc_idlist_find_client_by_id(server->global_list,
3807                                                       client_id, TRUE,
3808                                                       &id_cache);
3809       if (!detached_client) {
3810         SILC_LOG_DEBUG(("Resuming client is unknown"));
3811         silc_free(client_id);
3812         return;
3813       }
3814     }
3815
3816     /* Check that the client has not been resumed already because it is
3817        protocol error to attempt to resume more than once.  The client
3818        will be killed if this protocol error occurs. */
3819     if (detached_client->data.status & SILC_IDLIST_STATUS_RESUMED &&
3820         !(detached_client->mode & SILC_UMODE_DETACHED)) {
3821       /* The client is clearly attempting to resume more than once and
3822          perhaps playing around by resuming from several different places
3823          at the same time. */
3824       SILC_LOG_DEBUG(("Attempting to re-resume client, killing both"));
3825       silc_server_kill_client(server, detached_client, NULL,
3826                               server->id, SILC_ID_SERVER);
3827       silc_free(client_id);
3828       return;
3829     }
3830
3831     /* Check whether client is detached at all */
3832     if (!(detached_client->mode & SILC_UMODE_DETACHED)) {
3833       SILC_LOG_DEBUG(("Client is not detached"));
3834       silc_free(client_id);
3835       return;
3836     }
3837
3838     SILC_LOG_DEBUG(("Resuming detached client"));
3839
3840     /* If the sender of this packet is server and we are router we need to
3841        broadcast this packet to other routers in the network. */
3842     if (server->server_type == SILC_ROUTER &&
3843         sock->type == SILC_SOCKET_TYPE_SERVER &&
3844         !(packet->flags & SILC_PACKET_FLAG_BROADCAST)) {
3845       SILC_LOG_DEBUG(("Broadcasting received Resume Client packet"));
3846       silc_server_packet_send(server, SILC_PRIMARY_ROUTE(server),
3847                               packet->type, 
3848                               packet->flags | SILC_PACKET_FLAG_BROADCAST,
3849                               buffer->data, buffer->len, FALSE);
3850       silc_server_backup_send(server, sock->user_data, 
3851                               packet->type, packet->flags,
3852                               packet->buffer->data, packet->buffer->len, 
3853                               FALSE, TRUE);
3854     }
3855
3856     /* Client is detached, and now it is resumed.  Remove the detached
3857        mode and mark that it is resumed. */
3858     silc_idlist_del_data(detached_client);
3859     detached_client->mode &= ~SILC_UMODE_DETACHED;
3860     detached_client->data.status |= SILC_IDLIST_STATUS_RESUMED;
3861     detached_client->data.status &= ~SILC_IDLIST_STATUS_LOCAL;
3862     id_cache->expire = 0;
3863
3864     silc_schedule_task_del_by_context(server->schedule, detached_client);
3865
3866     /* Get the new owner of the resumed client */
3867     server_id = silc_id_str2id(packet->src_id, packet->src_id_len,
3868                                packet->src_id_type);
3869     if (!server_id) {
3870       silc_free(client_id);
3871       return;
3872     }
3873
3874     /* Get server entry */
3875     server_entry = silc_idlist_find_server_by_id(server->global_list, 
3876                                                  server_id, TRUE, NULL);
3877     local = FALSE;
3878     if (!server_entry) {
3879       server_entry = silc_idlist_find_server_by_id(server->local_list, 
3880                                                    server_id, TRUE, NULL);
3881       local = TRUE;
3882       if (!server_entry) {
3883         silc_free(server_id);
3884         silc_free(client_id);
3885         return;
3886       }
3887     }
3888
3889     if (server->server_type == SILC_ROUTER &&
3890         sock->type == SILC_SOCKET_TYPE_ROUTER &&
3891         server_entry->server_type == SILC_ROUTER)
3892       local = FALSE;
3893
3894     /* Change the client to correct list. */
3895     if (!silc_idcache_del_by_context(server->local_list->clients,
3896                                      detached_client))
3897       silc_idcache_del_by_context(server->global_list->clients,
3898                                   detached_client);
3899     silc_idcache_add(local && server->server_type == SILC_ROUTER ?
3900                      server->local_list->clients :
3901                      server->global_list->clients,
3902                      detached_client->nickname,
3903                      detached_client->id, detached_client, FALSE, NULL);
3904
3905     /* Change the owner of the client */
3906     detached_client->router = server_entry;
3907
3908     /* Update channel information regarding global clients on channel. */
3909     if (server->server_type != SILC_ROUTER) {
3910       silc_hash_table_list(detached_client->channels, &htl);
3911       while (silc_hash_table_get(&htl, NULL, (void **)&chl))
3912         chl->channel->global_users = 
3913           silc_server_channel_has_global(chl->channel);
3914       silc_hash_table_list_reset(&htl);
3915     }
3916
3917     silc_free(server_id);
3918   }
3919
3920   silc_free(client_id);
3921     silc_idlist_del_data(detached_client);
3922 }