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