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