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