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