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