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