1ff33119918e3077a511e8bf70b60957618cccbd
[silc.git] / apps / silcd / packet_receive.c
1 /*
2
3   packet_receive.c
4
5   Author: Pekka Riikonen <priikone@poseidon.pspt.fi>
6
7   Copyright (C) 1997 - 2001 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, *channel_id2;
41   SilcClientID *client_id, *client_id2;
42   SilcServerID *server_id;
43   SilcChannelEntry channel;
44   SilcClientEntry client;
45   SilcServerEntry server_entry;
46   SilcChannelClientEntry chl;
47   SilcIDCacheEntry cache;
48   uint32 mode;
49   unsigned char *tmp;
50   uint32 tmp_len;
51
52   SILC_LOG_DEBUG(("Start"));
53
54   if (sock->type == SILC_SOCKET_TYPE_CLIENT ||
55       packet->src_id_type != SILC_ID_SERVER)
56     return;
57
58   if (!packet->dst_id)
59     return;
60
61   /* If the packet is destined directly to a client then relay the packet
62      before processing it. */
63   if (packet->dst_id_type == SILC_ID_CLIENT) {
64     SilcIDListData idata;
65     SilcSocketConnection dst_sock;
66
67     /* Get the route to the client */
68     dst_sock = silc_server_get_client_route(server, packet->dst_id,
69                                             packet->dst_id_len, NULL, &idata);
70     if (dst_sock)
71       /* Relay the packet */
72       silc_server_relay_packet(server, dst_sock, idata->send_key,
73                                idata->hmac_receive, packet, TRUE);
74   }
75
76   /* If we are router and this packet is not already broadcast packet
77      we will broadcast it. The sending socket really cannot be router or
78      the router is buggy. If this packet is coming from router then it must
79      have the broadcast flag set already and we won't do anything. */
80   if (!server->standalone && server->server_type == SILC_ROUTER &&
81       sock->type == SILC_SOCKET_TYPE_SERVER &&
82       !(packet->flags & SILC_PACKET_FLAG_BROADCAST)) {
83     SILC_LOG_DEBUG(("Broadcasting received Notify packet"));
84     silc_server_packet_send(server, server->router->connection, packet->type,
85                             packet->flags | SILC_PACKET_FLAG_BROADCAST, 
86                             packet->buffer->data, packet->buffer->len, FALSE);
87   }
88
89   payload = silc_notify_payload_parse(packet->buffer);
90   if (!payload)
91     return;
92
93   type = silc_notify_get_type(payload);
94   args = silc_notify_get_args(payload);
95   if (!args)
96     goto out;
97
98   switch(type) {
99   case SILC_NOTIFY_TYPE_JOIN:
100     /* 
101      * Distribute the notify to local clients on the channel
102      */
103     SILC_LOG_DEBUG(("JOIN notify"));
104
105     /* Get Channel ID */
106     tmp = silc_argument_get_arg_type(args, 2, &tmp_len);
107     if (!tmp)
108       goto out;
109     channel_id = silc_id_payload_parse_id(tmp, tmp_len);
110     if (!channel_id)
111       goto out;
112
113     /* Get channel entry */
114     channel = silc_idlist_find_channel_by_id(server->global_list, 
115                                              channel_id, NULL);
116     if (!channel) {
117       channel = silc_idlist_find_channel_by_id(server->local_list, 
118                                                channel_id, NULL);
119       if (!channel) {
120         silc_free(channel_id);
121         goto out;
122       }
123     }
124     silc_free(channel_id);
125
126     /* Get client ID */
127     tmp = silc_argument_get_arg_type(args, 1, &tmp_len);
128     if (!tmp)
129       goto out;
130     client_id = silc_id_payload_parse_id(tmp, tmp_len);
131     if (!client_id)
132       goto out;
133
134     /* Send to channel */
135     silc_server_packet_send_to_channel(server, sock, channel, packet->type, 
136                                        FALSE, packet->buffer->data, 
137                                        packet->buffer->len, FALSE);
138
139     /* If the the client is not in local list we check global list (ie. the
140        channel will be global channel) and if it does not exist then create
141        entry for the client. */
142     client = silc_idlist_find_client_by_id(server->global_list, 
143                                            client_id, NULL);
144     if (!client) {
145       client = silc_idlist_find_client_by_id(server->local_list, 
146                                              client_id, NULL);
147       if (!client) {
148         /* If router did not find the client the it is bogus */
149         if (server->server_type == SILC_ROUTER)
150           goto out;
151
152         client = 
153           silc_idlist_add_client(server->global_list, NULL, 0, NULL, NULL,
154                                  silc_id_dup(client_id, SILC_ID_CLIENT), 
155                                  sock->user_data, NULL);
156         if (!client) {
157           silc_free(client_id);
158           goto out;
159         }
160
161         client->data.registered = TRUE;
162       }
163     }
164
165     /* Do not add client to channel if it is there already */
166     if (silc_server_client_on_channel(client, channel))
167       break;
168
169     if (server->server_type == SILC_SERVER && 
170         sock->type == SILC_SOCKET_TYPE_ROUTER)
171       /* The channel is global now */
172       channel->global_users = TRUE;
173
174     /* JOIN the global client to the channel (local clients (if router 
175        created the channel) is joined in the pending JOIN command). */
176     chl = silc_calloc(1, sizeof(*chl));
177     chl->client = client;
178     chl->channel = channel;
179     silc_list_add(channel->user_list, chl);
180     silc_list_add(client->channels, chl);
181     silc_free(client_id);
182
183     break;
184
185   case SILC_NOTIFY_TYPE_LEAVE:
186     /* 
187      * Distribute the notify to local clients on the channel
188      */
189     SILC_LOG_DEBUG(("LEAVE notify"));
190
191     channel_id = silc_id_str2id(packet->dst_id, packet->dst_id_len,
192                                 packet->dst_id_type);
193     if (!channel_id)
194       goto out;
195
196     /* Get channel entry */
197     channel = silc_idlist_find_channel_by_id(server->global_list, 
198                                              channel_id, NULL);
199     if (!channel) { 
200       channel = silc_idlist_find_channel_by_id(server->local_list, 
201                                                channel_id, NULL);
202       if (!channel) {
203         silc_free(channel_id);
204         goto out;
205       }
206     }
207
208     /* Get client ID */
209     tmp = silc_argument_get_arg_type(args, 1, &tmp_len);
210     if (!tmp) {
211       silc_free(channel_id);
212       goto out;
213     }
214     client_id = silc_id_payload_parse_id(tmp, tmp_len);
215     if (!client_id) {
216       silc_free(channel_id);
217       goto out;
218     }
219
220     /* Send to channel */
221     silc_server_packet_send_to_channel(server, sock, channel, packet->type, 
222                                        FALSE, packet->buffer->data, 
223                                        packet->buffer->len, FALSE);
224
225     /* Get client entry */
226     client = silc_idlist_find_client_by_id(server->global_list, 
227                                            client_id, NULL);
228     if (!client) {
229       client = silc_idlist_find_client_by_id(server->local_list, 
230                                              client_id, NULL);
231       if (!client) {
232         silc_free(client_id);
233         silc_free(channel_id);
234         goto out;
235       }
236     }
237     silc_free(client_id);
238
239     /* Remove the user from channel */
240     silc_server_remove_from_one_channel(server, sock, channel, client, FALSE);
241     break;
242
243   case SILC_NOTIFY_TYPE_SIGNOFF:
244     /* 
245      * Distribute the notify to local clients on the channel
246      */
247     SILC_LOG_DEBUG(("SIGNOFF notify"));
248
249     /* Get client ID */
250     tmp = silc_argument_get_arg_type(args, 1, &tmp_len);
251     if (!tmp)
252       goto out;
253     client_id = silc_id_payload_parse_id(tmp, tmp_len);
254     if (!client_id)
255       goto out;
256
257     /* Get client entry */
258     client = silc_idlist_find_client_by_id(server->global_list, 
259                                            client_id, &cache);
260     if (!client) {
261       client = silc_idlist_find_client_by_id(server->local_list, 
262                                              client_id, &cache);
263       if (!client) {
264         silc_free(client_id);
265         goto out;
266       }
267     }
268     silc_free(client_id);
269
270     /* Get signoff message */
271     tmp = silc_argument_get_arg_type(args, 2, &tmp_len);
272     if (tmp_len > 128)
273       tmp = NULL;
274
275     /* Remove the client from all channels. */
276     silc_server_remove_from_channels(server, NULL, client, TRUE, tmp, FALSE);
277
278     client->data.registered = FALSE;
279     cache->expire = SILC_ID_CACHE_EXPIRE_DEF;
280     break;
281
282   case SILC_NOTIFY_TYPE_TOPIC_SET:
283     /* 
284      * Distribute the notify to local clients on the channel
285      */
286
287     SILC_LOG_DEBUG(("TOPIC SET notify"));
288
289     channel_id = silc_id_str2id(packet->dst_id, packet->dst_id_len,
290                                 packet->dst_id_type);
291     if (!channel_id)
292       goto out;
293
294     /* Get channel entry */
295     channel = silc_idlist_find_channel_by_id(server->global_list, 
296                                              channel_id, NULL);
297     if (!channel) {
298       channel = silc_idlist_find_channel_by_id(server->local_list, 
299                                                channel_id, NULL);
300       if (!channel) {
301         silc_free(channel_id);
302         goto out;
303       }
304     }
305
306     /* Get the topic */
307     tmp = silc_argument_get_arg_type(args, 2, &tmp_len);
308     if (!tmp) {
309       silc_free(channel_id);
310       goto out;
311     }
312
313     if (channel->topic)
314       silc_free(channel->topic);
315     channel->topic = silc_calloc(tmp_len, sizeof(*channel->topic));
316     memcpy(channel->topic, tmp, tmp_len);
317
318     /* Send the same notify to the channel */
319     silc_server_packet_send_to_channel(server, sock, channel, packet->type, 
320                                        FALSE, packet->buffer->data, 
321                                        packet->buffer->len, FALSE);
322     silc_free(channel_id);
323     break;
324
325   case SILC_NOTIFY_TYPE_NICK_CHANGE:
326     {
327       /* 
328        * Distribute the notify to local clients on the channel
329        */
330       unsigned char *id, *id2;
331
332       SILC_LOG_DEBUG(("NICK CHANGE notify"));
333       
334       /* Get old client ID */
335       id = silc_argument_get_arg_type(args, 1, &tmp_len);
336       if (!id)
337         goto out;
338       client_id = silc_id_payload_parse_id(id, tmp_len);
339       if (!client_id)
340         goto out;
341       
342       /* Get new client ID */
343       id2 = silc_argument_get_arg_type(args, 2, &tmp_len);
344       if (!id2)
345         goto out;
346       client_id2 = silc_id_payload_parse_id(id2, tmp_len);
347       if (!client_id2)
348         goto out;
349       
350       SILC_LOG_DEBUG(("Old Client ID id(%s)", 
351                       silc_id_render(client_id, SILC_ID_CLIENT)));
352       SILC_LOG_DEBUG(("New Client ID id(%s)", 
353                       silc_id_render(client_id2, SILC_ID_CLIENT)));
354
355       /* Replace the Client ID */
356       client = silc_idlist_replace_client_id(server->global_list, client_id,
357                                              client_id2);
358       if (!client)
359         client = silc_idlist_replace_client_id(server->local_list, client_id, 
360                                                client_id2);
361
362       if (client) {
363         /* The nickname is not valid anymore, set it NULL. This causes that
364            the nickname will be queried if someone wants to know it. */
365         if (client->nickname)
366           silc_free(client->nickname);
367         client->nickname = NULL;
368
369         /* Send the NICK_CHANGE notify type to local clients on the channels
370            this client is joined to. */
371         silc_server_send_notify_on_channels(server, NULL, client, 
372                                             SILC_NOTIFY_TYPE_NICK_CHANGE, 2,
373                                             id, tmp_len, 
374                                             id2, tmp_len);
375       }
376
377       silc_free(client_id);
378       if (!client)
379         silc_free(client_id2);
380       break;
381     }
382
383   case SILC_NOTIFY_TYPE_CMODE_CHANGE:
384     /* 
385      * Distribute the notify to local clients on the channel
386      */
387     
388     SILC_LOG_DEBUG(("CMODE CHANGE notify"));
389       
390     channel_id = silc_id_str2id(packet->dst_id, packet->dst_id_len,
391                                 packet->dst_id_type);
392     if (!channel_id)
393       goto out;
394
395     /* Get channel entry */
396     channel = silc_idlist_find_channel_by_id(server->global_list, 
397                                              channel_id, NULL);
398     if (!channel) {
399       channel = silc_idlist_find_channel_by_id(server->local_list, 
400                                                channel_id, NULL);
401       if (!channel) {
402         silc_free(channel_id);
403         goto out;
404       }
405     }
406
407     /* Send the same notify to the channel */
408     silc_server_packet_send_to_channel(server, sock, channel, packet->type, 
409                                        FALSE, packet->buffer->data, 
410                                        packet->buffer->len, FALSE);
411
412     /* Get the mode */
413     tmp = silc_argument_get_arg_type(args, 2, &tmp_len);
414     if (!tmp) {
415       silc_free(channel_id);
416       goto out;
417     }
418
419     SILC_GET32_MSB(mode, tmp);
420
421     /* If the channel had private keys set and the mode was removed then
422        we must re-generate and re-distribute a new channel key */
423     if (channel->mode & SILC_CHANNEL_MODE_PRIVKEY &&
424         !(mode & SILC_CHANNEL_MODE_PRIVKEY)) {
425       /* Re-generate channel key */
426       silc_server_create_channel_key(server, channel, 0);
427       
428       /* Send the channel key. This sends it to our local clients and if
429          we are normal server to our router as well. */
430       silc_server_send_channel_key(server, NULL, channel, 
431                                    server->server_type == SILC_ROUTER ? 
432                                    FALSE : !server->standalone);
433     }
434
435     /* Change mode */
436     channel->mode = mode;
437     silc_free(channel_id);
438
439     /* Get the hmac */
440     tmp = silc_argument_get_arg_type(args, 4, &tmp_len);
441     if (tmp) {
442       unsigned char hash[32];
443
444       if (channel->hmac)
445         silc_hmac_free(channel->hmac);
446       if (!silc_hmac_alloc(tmp, NULL, &channel->hmac))
447         goto out;
448
449       /* Set the HMAC key out of current channel key. The client must do
450          this locally. */
451       silc_hash_make(channel->hmac->hash, channel->key, channel->key_len / 8, 
452                      hash);
453       silc_hmac_set_key(channel->hmac, hash, 
454                         silc_hash_len(channel->hmac->hash));
455       memset(hash, 0, sizeof(hash));
456     }
457
458     break;
459
460   case SILC_NOTIFY_TYPE_CUMODE_CHANGE:
461     /* 
462      * Distribute the notify to local clients on the channel
463      */
464
465     SILC_LOG_DEBUG(("CUMODE CHANGE notify"));
466
467     channel_id = silc_id_str2id(packet->dst_id, packet->dst_id_len,
468                                 packet->dst_id_type);
469     if (!channel_id)
470       goto out;
471
472     /* Get channel entry */
473     channel = silc_idlist_find_channel_by_id(server->global_list, 
474                                              channel_id, NULL);
475     if (!channel) {
476       channel = silc_idlist_find_channel_by_id(server->local_list, 
477                                                channel_id, NULL);
478       if (!channel) {
479         silc_free(channel_id);
480         goto out;
481       }
482     }
483
484     /* Get the mode */
485     tmp = silc_argument_get_arg_type(args, 2, &tmp_len);
486     if (!tmp) {
487       silc_free(channel_id);
488       goto out;
489     }
490       
491     SILC_GET32_MSB(mode, tmp);
492
493     /* Get target client */
494     tmp = silc_argument_get_arg_type(args, 3, &tmp_len);
495     if (!tmp)
496       goto out;
497     client_id = silc_id_payload_parse_id(tmp, tmp_len);
498     if (!client_id)
499       goto out;
500     
501     /* Get client entry */
502     client = silc_idlist_find_client_by_id(server->global_list, 
503                                            client_id, NULL);
504     if (!client) {
505       client = silc_idlist_find_client_by_id(server->local_list, 
506                                              client_id, NULL);
507       if (!client) {
508         silc_free(client_id);
509         goto out;
510       }
511     }
512     silc_free(client_id);
513
514     /* Get entry to the channel user list */
515     silc_list_start(channel->user_list);
516     while ((chl = silc_list_get(channel->user_list)) != SILC_LIST_END)
517       if (chl->client == client) {
518         /* Change the mode */
519         chl->mode = mode;
520         break;
521       }
522
523     /* Send the same notify to the channel */
524     silc_server_packet_send_to_channel(server, sock, channel, packet->type, 
525                                        FALSE, packet->buffer->data, 
526                                        packet->buffer->len, FALSE);
527     silc_free(channel_id);
528     break;
529
530   case SILC_NOTIFY_TYPE_INVITE:
531
532     if (packet->dst_id_type == SILC_ID_CLIENT)
533       goto out;
534
535     SILC_LOG_DEBUG(("INVITE notify"));
536
537     /* Get Channel ID */
538     tmp = silc_argument_get_arg_type(args, 1, &tmp_len);
539     if (!tmp)
540       goto out;
541     channel_id = silc_id_payload_parse_id(tmp, tmp_len);
542     if (!channel_id)
543       goto out;
544
545     /* Get channel entry */
546     channel = silc_idlist_find_channel_by_id(server->global_list, 
547                                              channel_id, NULL);
548     if (!channel) {
549       channel = silc_idlist_find_channel_by_id(server->local_list, 
550                                                channel_id, NULL);
551       if (!channel) {
552         silc_free(channel_id);
553         goto out;
554       }
555     }
556     silc_free(channel_id);
557
558     /* Get the added invite */
559     tmp = silc_argument_get_arg_type(args, 3, &tmp_len);
560     if (tmp) {
561       if (!channel->invite_list)
562         channel->invite_list = silc_calloc(tmp_len + 2, 
563                                            sizeof(*channel->invite_list));
564       else
565         channel->invite_list = silc_realloc(channel->invite_list, 
566                                             sizeof(*channel->invite_list) * 
567                                             (tmp_len + 
568                                              strlen(channel->invite_list) + 
569                                              2));
570       if (tmp[tmp_len - 1] == ',')
571         tmp[tmp_len - 1] = '\0';
572       
573       strncat(channel->invite_list, tmp, tmp_len);
574       strncat(channel->invite_list, ",", 1);
575     }
576
577     /* Get the deleted invite */
578     tmp = silc_argument_get_arg_type(args, 4, &tmp_len);
579     if (tmp && channel->invite_list) {
580       char *start, *end, *n;
581       
582       if (!strncmp(channel->invite_list, tmp, 
583                    strlen(channel->invite_list) - 1)) {
584         silc_free(channel->invite_list);
585         channel->invite_list = NULL;
586       } else {
587         start = strstr(channel->invite_list, tmp);
588         if (start && strlen(start) >= tmp_len) {
589           end = start + tmp_len;
590           n = silc_calloc(strlen(channel->invite_list) - tmp_len, sizeof(*n));
591           strncat(n, channel->invite_list, start - channel->invite_list);
592           strncat(n, end + 1, ((channel->invite_list + 
593                                 strlen(channel->invite_list)) - end) - 1);
594           silc_free(channel->invite_list);
595           channel->invite_list = n;
596         }
597       }
598     }
599
600     break;
601
602   case SILC_NOTIFY_TYPE_CHANNEL_CHANGE:
603     /*
604      * Distribute to the local clients on the channel and change the
605      * channel ID.
606      */
607
608     SILC_LOG_DEBUG(("CHANNEL CHANGE"));
609
610     if (sock->type != SILC_SOCKET_TYPE_ROUTER)
611       break;
612
613     /* Get the old Channel ID */
614     tmp = silc_argument_get_arg_type(args, 1, &tmp_len);
615     if (!tmp)
616       goto out;
617     channel_id = silc_id_payload_parse_id(tmp, tmp_len);
618     if (!channel_id)
619       goto out;
620
621     /* Get the channel entry */
622     channel = silc_idlist_find_channel_by_id(server->global_list, 
623                                              channel_id, NULL);
624     if (!channel) {
625       channel = silc_idlist_find_channel_by_id(server->local_list, 
626                                                channel_id, NULL);
627       if (!channel) {
628         silc_free(channel_id);
629         goto out;
630       }
631     }
632
633     /* Send the notify to the channel */
634     silc_server_packet_send_to_channel(server, sock, channel, packet->type, 
635                                        FALSE, packet->buffer->data, 
636                                        packet->buffer->len, FALSE);
637
638     /* Get the new Channel ID */
639     tmp = silc_argument_get_arg_type(args, 2, &tmp_len);
640     if (!tmp)
641       goto out;
642     channel_id2 = silc_id_payload_parse_id(tmp, tmp_len);
643     if (!channel_id2)
644       goto out;
645
646     SILC_LOG_DEBUG(("Old Channel ID id(%s)", 
647                     silc_id_render(channel_id, SILC_ID_CHANNEL)));
648     SILC_LOG_DEBUG(("New Channel ID id(%s)", 
649                     silc_id_render(channel_id2, SILC_ID_CHANNEL)));
650
651     /* Replace the Channel ID */
652     if (!silc_idlist_replace_channel_id(server->global_list, channel_id,
653                                         channel_id2))
654       if (!silc_idlist_replace_channel_id(server->local_list, channel_id,
655                                           channel_id2)) {
656         silc_free(channel_id2);
657         channel_id2 = NULL;
658       }
659
660     if (channel_id2) {
661       SilcBuffer users = NULL;
662       
663       /* Re-announce our clients on the channel as the ID has changed now */
664       silc_server_announce_get_channel_users(server, channel, &users);
665       if (users) {
666         silc_buffer_push(users, users->data - users->head);
667         silc_server_packet_send(server, sock,
668                                 SILC_PACKET_NOTIFY, SILC_PACKET_FLAG_LIST,
669                                 users->data, users->len, FALSE);
670         silc_buffer_free(users);
671       }
672     }
673
674     silc_free(channel_id);
675
676     break;
677
678   case SILC_NOTIFY_TYPE_SERVER_SIGNOFF:
679     /* 
680      * Remove the server entry and all clients that this server owns.
681      */
682
683     SILC_LOG_DEBUG(("SERVER SIGNOFF notify"));
684
685     /* Get Server ID */
686     tmp = silc_argument_get_arg_type(args, 1, &tmp_len);
687     if (!tmp)
688       goto out;
689     server_id = silc_id_payload_parse_id(tmp, tmp_len);
690     if (!server_id)
691       goto out;
692
693     /* Get server entry */
694     server_entry = silc_idlist_find_server_by_id(server->global_list, 
695                                                  server_id, NULL);
696     if (!server_entry) {
697       server_entry = silc_idlist_find_server_by_id(server->local_list, 
698                                                    server_id, NULL);
699       if (!server_entry) {
700         silc_free(server_id);
701         goto out;
702       }
703     }
704     silc_free(server_id);
705
706     /* Free all client entries that this server owns as they will
707        become invalid now as well. */
708     silc_server_remove_clients_by_server(server, server_entry, TRUE);
709
710     /* Remove the server entry */
711     if (!silc_idlist_del_server(server->global_list, server_entry))
712       silc_idlist_del_server(server->local_list, server_entry);
713
714     /* XXX update statistics */
715
716     break;
717
718   case SILC_NOTIFY_TYPE_KICKED:
719     /* 
720      * Distribute the notify to local clients on the channel
721      */
722     
723     SILC_LOG_DEBUG(("KICKED notify"));
724       
725     channel_id = silc_id_str2id(packet->dst_id, packet->dst_id_len,
726                                 packet->dst_id_type);
727     if (!channel_id)
728       goto out;
729
730     /* Get channel entry */
731     channel = silc_idlist_find_channel_by_id(server->global_list, 
732                                              channel_id, NULL);
733     if (!channel) {
734       channel = silc_idlist_find_channel_by_id(server->local_list, 
735                                                channel_id, NULL);
736       if (!channel) {
737         silc_free(channel_id);
738         goto out;
739       }
740     }
741     silc_free(channel_id);
742
743     /* Get client ID */
744     tmp = silc_argument_get_arg_type(args, 1, &tmp_len);
745     if (!tmp)
746       goto out;
747     client_id = silc_id_payload_parse_id(tmp, tmp_len);
748     if (!client_id)
749       goto out;
750
751     /* Send to channel */
752     silc_server_packet_send_to_channel(server, sock, channel, packet->type, 
753                                        FALSE, packet->buffer->data, 
754                                        packet->buffer->len, FALSE);
755
756     /* If the the client is not in local list we check global list */
757     client = silc_idlist_find_client_by_id(server->global_list, 
758                                            client_id, NULL);
759     if (!client) {
760       client = silc_idlist_find_client_by_id(server->local_list, 
761                                              client_id, NULL);
762       if (!client) {
763         silc_free(client_id);
764         goto out;
765       }
766     }
767
768     /* Remove the client from channel */
769     silc_server_remove_from_one_channel(server, sock, channel, client, FALSE);
770
771     break;
772
773   case SILC_NOTIFY_TYPE_KILLED:
774     {
775       /* 
776        * Distribute the notify to local clients on channels
777        */
778       unsigned char *id;
779       uint32 id_len;
780     
781       SILC_LOG_DEBUG(("KILLED notify"));
782       
783       /* Get client ID */
784       id = silc_argument_get_arg_type(args, 1, &id_len);
785       if (!id)
786         goto out;
787       client_id = silc_id_payload_parse_id(id, id_len);
788       if (!client_id)
789         goto out;
790
791       /* If the the client is not in local list we check global list */
792       client = silc_idlist_find_client_by_id(server->global_list, 
793                                              client_id, NULL);
794       if (!client) {
795         client = silc_idlist_find_client_by_id(server->local_list, 
796                                                client_id, NULL);
797         if (!client) {
798           silc_free(client_id);
799           goto out;
800         }
801       }
802       silc_free(client_id);
803
804       /* If the client is one of ours, then close the connection to the
805          client now. This removes the client from all channels as well. */
806       if (packet->dst_id_type == SILC_ID_CLIENT && client->data.registered &&
807           client->connection) {
808         sock = client->connection;
809         silc_server_free_client_data(server, NULL, client, FALSE, NULL);
810         silc_server_close_connection(server, sock);
811         break;
812       }
813
814       /* Get comment */
815       tmp = silc_argument_get_arg_type(args, 2, &tmp_len);
816       if (tmp_len > 128)
817         tmp = NULL;
818
819       /* Send the notify to local clients on the channels except to the
820          client who is killed. */
821       silc_server_send_notify_on_channels(server, client, client,
822                                           SILC_NOTIFY_TYPE_KILLED, 
823                                           tmp ? 2 : 1,
824                                           id, id_len, 
825                                           tmp, tmp_len);
826
827       /* Remove the client from all channels */
828       silc_server_remove_from_channels(server, NULL, client, FALSE, NULL, 
829                                        FALSE);
830
831       break;
832     }
833
834   case SILC_NOTIFY_TYPE_UMODE_CHANGE:
835     /*
836      * Save the mode of the client.
837      */
838
839     SILC_LOG_DEBUG(("UMODE_CHANGE notify"));
840       
841     /* Get client ID */
842     tmp = silc_argument_get_arg_type(args, 1, &tmp_len);
843     if (!tmp)
844       goto out;
845     client_id = silc_id_payload_parse_id(tmp, tmp_len);
846     if (!client_id)
847       goto out;
848
849     /* Get client entry */
850     client = silc_idlist_find_client_by_id(server->global_list, 
851                                            client_id, NULL);
852     if (!client) {
853       client = silc_idlist_find_client_by_id(server->local_list, 
854                                              client_id, NULL);
855       if (!client) {
856         silc_free(client_id);
857         goto out;
858       }
859     }
860     silc_free(client_id);
861
862     /* Get the mode */
863     tmp = silc_argument_get_arg_type(args, 2, &tmp_len);
864     if (!tmp)
865       goto out;
866
867     /* Save the mode */
868     SILC_GET32_MSB(client->mode, tmp);
869
870     break;
871
872   case SILC_NOTIFY_TYPE_BAN:
873     /*
874      * Save the ban
875      */
876
877     SILC_LOG_DEBUG(("BAN notify"));
878     
879     /* Get Channel ID */
880     tmp = silc_argument_get_arg_type(args, 1, &tmp_len);
881     if (!tmp)
882       goto out;
883     channel_id = silc_id_payload_parse_id(tmp, tmp_len);
884     if (!channel_id)
885       goto out;
886     
887     /* Get channel entry */
888     channel = silc_idlist_find_channel_by_id(server->global_list, 
889                                              channel_id, NULL);
890     if (!channel) {
891       channel = silc_idlist_find_channel_by_id(server->local_list, 
892                                                channel_id, NULL);
893       if (!channel) {
894         silc_free(channel_id);
895         goto out;
896       }
897     }
898     silc_free(channel_id);
899
900     /* Get the new ban and add it to the ban list */
901     tmp = silc_argument_get_arg_type(args, 2, &tmp_len);
902     if (tmp) {
903       if (!channel->ban_list)
904         channel->ban_list = silc_calloc(tmp_len + 2, 
905                                         sizeof(*channel->ban_list));
906       else
907         channel->ban_list = silc_realloc(channel->ban_list, 
908                                          sizeof(*channel->ban_list) * 
909                                          (tmp_len + 
910                                           strlen(channel->ban_list) + 2));
911       strncat(channel->ban_list, tmp, tmp_len);
912       strncat(channel->ban_list, ",", 1);
913     }
914
915     /* Get the ban to be removed and remove it from the list */
916     tmp = silc_argument_get_arg_type(args, 3, &tmp_len);
917     if (tmp && channel->ban_list) {
918       char *start, *end, *n;
919       
920       if (!strcmp(channel->ban_list, tmp)) {
921         silc_free(channel->ban_list);
922         channel->ban_list = NULL;
923       } else {
924         start = strstr(channel->ban_list, tmp);
925         if (start && strlen(start) >= tmp_len) {
926           end = start + tmp_len;
927           n = silc_calloc(strlen(channel->ban_list) - tmp_len, sizeof(*n));
928           strncat(n, channel->ban_list, start - channel->ban_list);
929           strncat(n, end + 1, ((channel->ban_list + 
930                                 strlen(channel->ban_list)) - end) - 1);
931           silc_free(channel->ban_list);
932           channel->ban_list = n;
933         }
934       }
935     }
936
937     break;
938
939     /* Ignore rest of the notify types for now */
940   case SILC_NOTIFY_TYPE_NONE:
941   case SILC_NOTIFY_TYPE_MOTD:
942     break;
943   default:
944     break;
945   }
946
947  out:
948   silc_notify_payload_free(payload);
949 }
950
951 void silc_server_notify_list(SilcServer server,
952                              SilcSocketConnection sock,
953                              SilcPacketContext *packet)
954 {
955   SilcPacketContext *new;
956   SilcBuffer buffer;
957   uint16 len;
958
959   SILC_LOG_DEBUG(("Processing Notify List"));
960
961   if (sock->type == SILC_SOCKET_TYPE_CLIENT ||
962       packet->src_id_type != SILC_ID_SERVER)
963     return;
964
965   /* Make copy of the original packet context, except for the actual
966      data buffer, which we will here now fetch from the original buffer. */
967   new = silc_packet_context_alloc();
968   new->type = SILC_PACKET_NOTIFY;
969   new->flags = packet->flags;
970   new->src_id = packet->src_id;
971   new->src_id_len = packet->src_id_len;
972   new->src_id_type = packet->src_id_type;
973   new->dst_id = packet->dst_id;
974   new->dst_id_len = packet->dst_id_len;
975   new->dst_id_type = packet->dst_id_type;
976
977   buffer = silc_buffer_alloc(1024);
978   new->buffer = buffer;
979
980   while (packet->buffer->len) {
981     SILC_GET16_MSB(len, packet->buffer->data + 2);
982     if (len > packet->buffer->len)
983       break;
984
985     if (len > buffer->truelen) {
986       silc_buffer_free(buffer);
987       buffer = silc_buffer_alloc(1024 + len);
988     }
989
990     silc_buffer_pull_tail(buffer, len);
991     silc_buffer_put(buffer, packet->buffer->data, len);
992
993     /* Process the Notify */
994     silc_server_notify(server, sock, new);
995
996     silc_buffer_push_tail(buffer, len);
997     silc_buffer_pull(packet->buffer, len);
998   }
999
1000   silc_buffer_free(buffer);
1001   silc_free(new);
1002 }
1003
1004 /* Received private message. This resolves the destination of the message 
1005    and sends the packet. This is used by both server and router.  If the
1006    destination is our locally connected client this sends the packet to
1007    the client. This may also send the message for further routing if
1008    the destination is not in our server (or router). */
1009
1010 void silc_server_private_message(SilcServer server,
1011                                  SilcSocketConnection sock,
1012                                  SilcPacketContext *packet)
1013 {
1014   SilcSocketConnection dst_sock;
1015   SilcIDListData idata;
1016
1017   SILC_LOG_DEBUG(("Start"));
1018
1019   if (packet->src_id_type != SILC_ID_CLIENT ||
1020       packet->dst_id_type != SILC_ID_CLIENT)
1021     return;
1022
1023   if (!packet->dst_id)
1024     return;
1025
1026   /* Get the route to the client */
1027   dst_sock = silc_server_get_client_route(server, packet->dst_id,
1028                                           packet->dst_id_len, NULL, &idata);
1029   if (!dst_sock)
1030     return;
1031
1032   /* Send the private message */
1033   silc_server_send_private_message(server, dst_sock, idata->send_key,
1034                                    idata->hmac_send, packet);
1035 }
1036
1037 /* Received private message key packet.. This packet is never for us. It is to
1038    the client in the packet's destination ID. Sending of this sort of packet
1039    equals sending private message, ie. it is sent point to point from
1040    one client to another. */
1041
1042 void silc_server_private_message_key(SilcServer server,
1043                                      SilcSocketConnection sock,
1044                                      SilcPacketContext *packet)
1045 {
1046   SilcSocketConnection dst_sock;
1047   SilcIDListData idata;
1048
1049   SILC_LOG_DEBUG(("Start"));
1050
1051   if (packet->src_id_type != SILC_ID_CLIENT ||
1052       packet->dst_id_type != SILC_ID_CLIENT)
1053     return;
1054
1055   if (!packet->dst_id)
1056     return;
1057
1058   /* Get the route to the client */
1059   dst_sock = silc_server_get_client_route(server, packet->dst_id,
1060                                           packet->dst_id_len, NULL, &idata);
1061   if (!dst_sock)
1062     return;
1063
1064   /* Relay the packet */
1065   silc_server_relay_packet(server, dst_sock, idata->send_key,
1066                            idata->hmac_send, packet, FALSE);
1067 }
1068
1069 /* Processes incoming command reply packet. The command reply packet may
1070    be destined to one of our clients or it may directly for us. We will 
1071    call the command reply routine after processing the packet. */
1072
1073 void silc_server_command_reply(SilcServer server,
1074                                SilcSocketConnection sock,
1075                                SilcPacketContext *packet)
1076 {
1077   SilcBuffer buffer = packet->buffer;
1078   SilcClientEntry client = NULL;
1079   SilcSocketConnection dst_sock;
1080   SilcIDListData idata;
1081   SilcClientID *id = NULL;
1082
1083   SILC_LOG_DEBUG(("Start"));
1084
1085   /* Source must be server or router */
1086   if (packet->src_id_type != SILC_ID_SERVER &&
1087       sock->type != SILC_SOCKET_TYPE_ROUTER)
1088     return;
1089
1090   if (packet->dst_id_type == SILC_ID_CHANNEL)
1091     return;
1092
1093   if (packet->dst_id_type == SILC_ID_CLIENT) {
1094     /* Destination must be one of ours */
1095     id = silc_id_str2id(packet->dst_id, packet->dst_id_len, SILC_ID_CLIENT);
1096     if (!id)
1097       return;
1098     client = silc_idlist_find_client_by_id(server->local_list, id, NULL);
1099     if (!client) {
1100       SILC_LOG_ERROR(("Cannot process command reply to unknown client"));
1101       silc_free(id);
1102       return;
1103     }
1104   }
1105
1106   if (packet->dst_id_type == SILC_ID_SERVER) {
1107     /* For now this must be for us */
1108     if (SILC_ID_SERVER_COMPARE(packet->dst_id, server->id_string)) {
1109       SILC_LOG_ERROR(("Cannot process command reply to unknown server"));
1110       return;
1111     }
1112   }
1113
1114   /* Execute command reply locally for the command */
1115   silc_server_command_reply_process(server, sock, buffer);
1116
1117   if (packet->dst_id_type == SILC_ID_CLIENT && client && id) {
1118     /* Relay the packet to the client */
1119     
1120     dst_sock = (SilcSocketConnection)client->connection;
1121     silc_buffer_push(buffer, SILC_PACKET_HEADER_LEN + packet->src_id_len 
1122                      + packet->dst_id_len + packet->padlen);
1123     
1124     silc_packet_send_prepare(dst_sock, 0, 0, buffer->len);
1125     silc_buffer_put(dst_sock->outbuf, buffer->data, buffer->len);
1126     
1127     idata = (SilcIDListData)client;
1128     
1129     /* Encrypt packet */
1130     silc_packet_encrypt(idata->send_key, idata->hmac_send, dst_sock->outbuf, 
1131                         buffer->len);
1132     
1133     /* Send the packet */
1134     silc_server_packet_send_real(server, dst_sock, TRUE);
1135
1136     silc_free(id);
1137   }
1138 }
1139
1140 /* Process received channel message. The message can be originated from
1141    client or server. */
1142
1143 void silc_server_channel_message(SilcServer server,
1144                                  SilcSocketConnection sock,
1145                                  SilcPacketContext *packet)
1146 {
1147   SilcChannelEntry channel = NULL;
1148   SilcChannelClientEntry chl;
1149   SilcChannelID *id = NULL;
1150   void *sender = NULL;
1151   void *sender_entry = NULL;
1152
1153   SILC_LOG_DEBUG(("Processing channel message"));
1154
1155   /* Sanity checks */
1156   if (packet->dst_id_type != SILC_ID_CHANNEL) {
1157     SILC_LOG_DEBUG(("Received bad message for channel, dropped"));
1158     goto out;
1159   }
1160
1161   /* Find channel entry */
1162   id = silc_id_str2id(packet->dst_id, packet->dst_id_len, SILC_ID_CHANNEL);
1163   if (!id)
1164     goto out;
1165   channel = silc_idlist_find_channel_by_id(server->local_list, id, NULL);
1166   if (!channel) {
1167     channel = silc_idlist_find_channel_by_id(server->global_list, id, NULL);
1168     if (!channel) {
1169       SILC_LOG_DEBUG(("Could not find channel"));
1170       goto out;
1171     }
1172   }
1173
1174   /* See that this client is on the channel. If the message is coming
1175      from router we won't do the check as the message is from client that
1176      we don't know about. Also, if the original sender is not client
1177      (as it can be server as well) we don't do the check. */
1178   sender = silc_id_str2id(packet->src_id, packet->src_id_len, 
1179                           packet->src_id_type);
1180   if (!sender)
1181     goto out;
1182   if (packet->src_id_type == SILC_ID_CLIENT) {
1183     silc_list_start(channel->user_list);
1184     while ((chl = silc_list_get(channel->user_list)) != SILC_LIST_END) {
1185       if (chl->client && !SILC_ID_CLIENT_COMPARE(chl->client->id, sender)) {
1186         sender_entry = chl->client;
1187         break;
1188       }
1189     }
1190     if (chl == SILC_LIST_END) {
1191       SILC_LOG_DEBUG(("Client not on channel"));
1192       goto out;
1193     }
1194   }
1195
1196   /* Distribute the packet to our local clients. This will send the
1197      packet for further routing as well, if needed. */
1198   silc_server_packet_relay_to_channel(server, sock, channel, sender,
1199                                       packet->src_id_type, sender_entry,
1200                                       packet->buffer->data,
1201                                       packet->buffer->len, FALSE);
1202
1203  out:
1204   if (sender)
1205     silc_free(sender);
1206   if (id)
1207     silc_free(id);
1208 }
1209
1210 /* Received channel key packet. We distribute the key to all of our locally
1211    connected clients on the channel. */
1212
1213 void silc_server_channel_key(SilcServer server,
1214                              SilcSocketConnection sock,
1215                              SilcPacketContext *packet)
1216 {
1217   SilcBuffer buffer = packet->buffer;
1218   SilcChannelEntry channel;
1219
1220   if (packet->src_id_type != SILC_ID_SERVER ||
1221       (server->server_type == SILC_ROUTER &&
1222        sock->type == SILC_SOCKET_TYPE_ROUTER))
1223     return;
1224
1225   /* Save the channel key */
1226   channel = silc_server_save_channel_key(server, buffer, NULL);
1227   if (!channel)
1228     return;
1229
1230   /* Distribute the key to everybody who is on the channel. If we are router
1231      we will also send it to locally connected servers. */
1232   silc_server_send_channel_key(server, sock, channel, FALSE);
1233 }
1234
1235 /* Received New Client packet and processes it.  Creates Client ID for the
1236    client. Client becomes registered after calling this functions. */
1237
1238 SilcClientEntry silc_server_new_client(SilcServer server,
1239                                        SilcSocketConnection sock,
1240                                        SilcPacketContext *packet)
1241 {
1242   SilcBuffer buffer = packet->buffer;
1243   SilcClientEntry client;
1244   SilcIDCacheEntry cache;
1245   SilcClientID *client_id;
1246   SilcBuffer reply;
1247   SilcIDListData idata;
1248   char *username = NULL, *realname = NULL, *id_string;
1249   int ret;
1250
1251   SILC_LOG_DEBUG(("Creating new client"));
1252
1253   if (sock->type != SILC_SOCKET_TYPE_CLIENT)
1254     return NULL;
1255
1256   /* Take client entry */
1257   client = (SilcClientEntry)sock->user_data;
1258   idata = (SilcIDListData)client;
1259
1260   /* Fetch the old client cache entry so that we can update it. */
1261   if (!silc_idcache_find_by_context(server->local_list->clients,
1262                                     sock->user_data, &cache)) {
1263     SILC_LOG_ERROR(("Lost client's cache entry - bad thing"));
1264     silc_server_disconnect_remote(server, sock, "Server closed connection: "
1265                                   "Unknown client");
1266     return NULL;
1267   }
1268
1269   /* Parse incoming packet */
1270   ret = silc_buffer_unformat(buffer,
1271                              SILC_STR_UI16_STRING_ALLOC(&username),
1272                              SILC_STR_UI16_STRING_ALLOC(&realname),
1273                              SILC_STR_END);
1274   if (ret == -1) {
1275     if (username)
1276       silc_free(username);
1277     if (realname)
1278       silc_free(realname);
1279     silc_server_disconnect_remote(server, sock, "Server closed connection: "
1280                                   "Incomplete client information");
1281     return NULL;
1282   }
1283
1284   if (!username) {
1285     silc_free(username);
1286     if (realname)
1287       silc_free(realname);
1288     silc_server_disconnect_remote(server, sock, "Server closed connection: "
1289                                   "Incomplete client information");
1290     return NULL;
1291   }
1292
1293   /* Create Client ID */
1294   silc_id_create_client_id(server->id, server->rng, server->md5hash,
1295                            username, &client_id);
1296
1297   if (strlen(username) > 128)
1298     username[127] = '\0';
1299
1300   /* Update client entry */
1301   idata->registered = TRUE;
1302   client->nickname = strdup(username);
1303   client->username = username;
1304   client->userinfo = realname ? realname : strdup(" ");
1305   client->id = client_id;
1306
1307   /* Update the cache entry */
1308   cache->id = (void *)client_id;
1309   cache->type = SILC_ID_CLIENT;
1310   cache->data = username;
1311   cache->data_len = strlen(username);
1312   silc_idcache_sort_by_data(server->local_list->clients);
1313
1314   /* Notify our router about new client on the SILC network */
1315   if (!server->standalone)
1316     silc_server_send_new_id(server, (SilcSocketConnection) 
1317                             server->router->connection, 
1318                             server->server_type == SILC_ROUTER ? TRUE : FALSE,
1319                             client->id, SILC_ID_CLIENT, SILC_ID_CLIENT_LEN);
1320   
1321   /* Send the new client ID to the client. */
1322   id_string = silc_id_id2str(client->id, SILC_ID_CLIENT);
1323   reply = silc_buffer_alloc(2 + 2 + SILC_ID_CLIENT_LEN);
1324   silc_buffer_pull_tail(reply, SILC_BUFFER_END(reply));
1325   silc_buffer_format(reply,
1326                      SILC_STR_UI_SHORT(SILC_ID_CLIENT),
1327                      SILC_STR_UI_SHORT(SILC_ID_CLIENT_LEN),
1328                      SILC_STR_UI_XNSTRING(id_string, SILC_ID_CLIENT_LEN),
1329                      SILC_STR_END);
1330   silc_server_packet_send(server, sock, SILC_PACKET_NEW_ID, 0, 
1331                           reply->data, reply->len, FALSE);
1332   silc_free(id_string);
1333   silc_buffer_free(reply);
1334
1335   /* Send some nice info to the client */
1336   SILC_SERVER_SEND_NOTIFY(server, sock, SILC_NOTIFY_TYPE_NONE,
1337                           ("Welcome to the SILC Network %s@%s",
1338                            username, sock->hostname));
1339   SILC_SERVER_SEND_NOTIFY(server, sock, SILC_NOTIFY_TYPE_NONE,
1340                           ("Your host is %s, running version %s",
1341                            server->config->server_info->server_name,
1342                            server_version));
1343   if (server->server_type == SILC_ROUTER) {
1344     SILC_SERVER_SEND_NOTIFY(server, sock, SILC_NOTIFY_TYPE_NONE,
1345                             ("There are %d clients on %d servers in SILC "
1346                              "Network", server->stat.clients,
1347                              server->stat.servers + 1));
1348     SILC_SERVER_SEND_NOTIFY(server, sock, SILC_NOTIFY_TYPE_NONE,
1349                             ("There are %d clients on %d server in our cell",
1350                              server->stat.cell_clients,
1351                              server->stat.cell_servers + 1));
1352     SILC_SERVER_SEND_NOTIFY(server, sock, SILC_NOTIFY_TYPE_NONE,
1353                             ("I have %d clients, %d channels, %d servers and "
1354                              "%d routers",
1355                              server->stat.my_clients, 
1356                              server->stat.my_channels,
1357                              server->stat.my_servers,
1358                              server->stat.my_routers));
1359     SILC_SERVER_SEND_NOTIFY(server, sock, SILC_NOTIFY_TYPE_NONE,
1360                             ("%d server operators and %d router operators "
1361                              "online",
1362                              server->stat.my_server_ops,
1363                              server->stat.my_router_ops));
1364   } else {
1365     SILC_SERVER_SEND_NOTIFY(server, sock, SILC_NOTIFY_TYPE_NONE,
1366                             ("I have %d clients and %d channels formed",
1367                              server->stat.my_clients,
1368                              server->stat.my_channels));
1369     SILC_SERVER_SEND_NOTIFY(server, sock, SILC_NOTIFY_TYPE_NONE,
1370                             ("%d operators online",
1371                              server->stat.my_server_ops));
1372   }
1373   SILC_SERVER_SEND_NOTIFY(server, sock, SILC_NOTIFY_TYPE_NONE,
1374                           ("Your connection is secured with %s cipher, "
1375                            "key length %d bits",
1376                            idata->send_key->cipher->name,
1377                            idata->send_key->cipher->key_len));
1378   SILC_SERVER_SEND_NOTIFY(server, sock, SILC_NOTIFY_TYPE_NONE,
1379                           ("Your current nickname is %s",
1380                            client->nickname));
1381
1382   /* Send motd */
1383   silc_server_send_motd(server, sock);
1384
1385   return client;
1386 }
1387
1388 /* Create new server. This processes received New Server packet and
1389    saves the received Server ID. The server is our locally connected
1390    server thus we save all the information and save it to local list. 
1391    This funtion can be used by both normal server and router server.
1392    If normal server uses this it means that its router has connected
1393    to the server. If router uses this it means that one of the cell's
1394    servers is connected to the router. */
1395
1396 SilcServerEntry silc_server_new_server(SilcServer server,
1397                                        SilcSocketConnection sock,
1398                                        SilcPacketContext *packet)
1399 {
1400   SilcBuffer buffer = packet->buffer;
1401   SilcServerEntry new_server;
1402   SilcIDCacheEntry cache;
1403   SilcServerID *server_id;
1404   SilcIDListData idata;
1405   unsigned char *server_name, *id_string;
1406   uint16 id_len, name_len;
1407   int ret;
1408
1409   SILC_LOG_DEBUG(("Creating new server"));
1410
1411   if (sock->type != SILC_SOCKET_TYPE_SERVER &&
1412       sock->type != SILC_SOCKET_TYPE_ROUTER)
1413     return NULL;
1414
1415   /* Take server entry */
1416   new_server = (SilcServerEntry)sock->user_data;
1417   idata = (SilcIDListData)new_server;
1418
1419   /* Fetch the old server cache entry so that we can update it. */
1420   if (!silc_idcache_find_by_context(server->local_list->servers,
1421                                     sock->user_data, &cache)) {
1422     SILC_LOG_ERROR(("Lost server's cache entry - bad thing"));
1423     return NULL;
1424   }
1425
1426   /* Parse the incoming packet */
1427   ret = silc_buffer_unformat(buffer,
1428                              SILC_STR_UI16_NSTRING_ALLOC(&id_string, &id_len),
1429                              SILC_STR_UI16_NSTRING_ALLOC(&server_name, 
1430                                                          &name_len),
1431                              SILC_STR_END);
1432   if (ret == -1) {
1433     if (id_string)
1434       silc_free(id_string);
1435     if (server_name)
1436       silc_free(server_name);
1437     return NULL;
1438   }
1439
1440   if (id_len > buffer->len) {
1441     silc_free(id_string);
1442     silc_free(server_name);
1443     return NULL;
1444   }
1445
1446   if (name_len > 256)
1447     server_name[255] = '\0';
1448
1449   /* Get Server ID */
1450   server_id = silc_id_str2id(id_string, id_len, SILC_ID_SERVER);
1451   if (!server_id) {
1452     silc_free(id_string);
1453     silc_free(server_name);
1454     return NULL;
1455   }
1456   silc_free(id_string);
1457
1458   /* Update client entry */
1459   idata->registered = TRUE;
1460   new_server->server_name = server_name;
1461   new_server->id = server_id;
1462
1463   /* Update the cache entry */
1464   cache->id = (void *)server_id;
1465   cache->type = SILC_ID_SERVER;
1466   cache->data = server_name;
1467   cache->data_len = strlen(server_name);
1468   silc_idcache_sort_by_data(server->local_list->servers);
1469
1470   /* Distribute the information about new server in the SILC network
1471      to our router. If we are normal server we won't send anything
1472      since this connection must be our router connection. */
1473   if (server->server_type == SILC_ROUTER && !server->standalone &&
1474       server->router->connection != sock)
1475     silc_server_send_new_id(server, server->router->connection,
1476                             TRUE, new_server->id, SILC_ID_SERVER, 
1477                             SILC_ID_SERVER_LEN);
1478
1479   if (server->server_type == SILC_ROUTER)
1480     server->stat.cell_servers++;
1481
1482   return new_server;
1483 }
1484
1485 /* Processes incoming New ID packet. New ID Payload is used to distribute
1486    information about newly registered clients and servers. */
1487
1488 static void silc_server_new_id_real(SilcServer server, 
1489                                     SilcSocketConnection sock,
1490                                     SilcPacketContext *packet,
1491                                     int broadcast)
1492 {
1493   SilcBuffer buffer = packet->buffer;
1494   SilcIDList id_list;
1495   SilcServerEntry router;
1496   SilcSocketConnection router_sock;
1497   SilcIDPayload idp;
1498   SilcIdType id_type;
1499   unsigned char *hash = NULL;
1500   void *id;
1501
1502   SILC_LOG_DEBUG(("Processing new ID"));
1503
1504   if (sock->type == SILC_SOCKET_TYPE_CLIENT ||
1505       server->server_type == SILC_SERVER ||
1506       packet->src_id_type != SILC_ID_SERVER)
1507     return;
1508
1509   idp = silc_id_payload_parse(buffer);
1510   if (!idp)
1511     return;
1512
1513   id_type = silc_id_payload_get_type(idp);
1514
1515   /* Normal server cannot have other normal server connections */
1516   if (id_type == SILC_ID_SERVER && sock->type == SILC_SOCKET_TYPE_SERVER)
1517     goto out;
1518
1519   id = silc_id_payload_get_id(idp);
1520   if (!id)
1521     goto out;
1522
1523   /* If the sender of this packet is server and we are router we need to
1524      broadcast this packet to other routers in the network. */
1525   if (broadcast && !server->standalone && server->server_type == SILC_ROUTER &&
1526       sock->type == SILC_SOCKET_TYPE_SERVER &&
1527       !(packet->flags & SILC_PACKET_FLAG_BROADCAST)) {
1528     SILC_LOG_DEBUG(("Broadcasting received New ID packet"));
1529     silc_server_packet_send(server, server->router->connection,
1530                             packet->type, 
1531                             packet->flags | SILC_PACKET_FLAG_BROADCAST,
1532                             buffer->data, buffer->len, FALSE);
1533   }
1534
1535   if (sock->type == SILC_SOCKET_TYPE_SERVER)
1536     id_list = server->local_list;
1537   else
1538     id_list = server->global_list;
1539
1540   /* If the packet is coming from server then use the sender as the
1541      origin of the the packet. If it came from router then check the real
1542      sender of the packet and use that as the origin. */
1543   if (sock->type == SILC_SOCKET_TYPE_SERVER) {
1544     router_sock = sock;
1545     router = sock->user_data;
1546   } else {
1547     void *sender_id = silc_id_str2id(packet->src_id, packet->src_id_len,
1548                                      packet->src_id_type);
1549     router = silc_idlist_find_server_by_id(server->global_list,
1550                                            sender_id, NULL);
1551     if (!router)
1552       router = silc_idlist_find_server_by_id(server->local_list,
1553                                              sender_id, NULL);
1554     silc_free(sender_id);
1555     if (!router)
1556       goto out;
1557     router_sock = sock;
1558   }
1559
1560   switch(id_type) {
1561   case SILC_ID_CLIENT:
1562     {
1563       SilcClientEntry entry;
1564
1565       SILC_LOG_DEBUG(("New client id(%s) from [%s] %s",
1566                       silc_id_render(id, SILC_ID_CLIENT),
1567                       sock->type == SILC_SOCKET_TYPE_SERVER ?
1568                       "Server" : "Router", sock->hostname));
1569     
1570       /* As a router we keep information of all global information in our
1571          global list. Cell wide information however is kept in the local
1572          list. The client is put to global list and we will take the hash
1573          value of the Client ID and save it to the ID Cache system for fast
1574          searching in the future. */
1575       hash = silc_calloc(sizeof(((SilcClientID *)id)->hash),
1576                          sizeof(unsigned char));
1577       memcpy(hash, ((SilcClientID *)id)->hash, 
1578              sizeof(((SilcClientID *)id)->hash));
1579       entry = silc_idlist_add_client(id_list, hash, 
1580                                      sizeof(((SilcClientID *)id)->hash),
1581                                      NULL, NULL, id, router, NULL);
1582       entry->nickname = NULL;
1583       entry->data.registered = TRUE;
1584
1585       if (sock->type == SILC_SOCKET_TYPE_SERVER)
1586         server->stat.cell_clients++;
1587       server->stat.clients++;
1588     }
1589     break;
1590
1591   case SILC_ID_SERVER:
1592     /* If the ID is mine, ignore it. */
1593     if (!SILC_ID_SERVER_COMPARE(id, server->id)) {
1594       SILC_LOG_DEBUG(("Ignoring my own ID as new ID"));
1595       break;
1596     }
1597
1598     SILC_LOG_DEBUG(("New server id(%s) from [%s] %s",
1599                     silc_id_render(id, SILC_ID_SERVER),
1600                     sock->type == SILC_SOCKET_TYPE_SERVER ?
1601                     "Server" : "Router", sock->hostname));
1602
1603     /* As a router we keep information of all global information in our global
1604        list. Cell wide information however is kept in the local list. */
1605     silc_idlist_add_server(id_list, NULL, 0, id, router, router_sock);
1606
1607     if (sock->type == SILC_SOCKET_TYPE_SERVER)
1608       server->stat.cell_servers++;
1609     server->stat.servers++;
1610     break;
1611
1612   case SILC_ID_CHANNEL:
1613     SILC_LOG_ERROR(("Channel cannot be registered with NEW_ID packet"));
1614     break;
1615
1616   default:
1617     break;
1618   }
1619
1620  out:
1621   silc_id_payload_free(idp);
1622 }
1623
1624
1625 /* Processes incoming New ID packet. New ID Payload is used to distribute
1626    information about newly registered clients and servers. */
1627
1628 void silc_server_new_id(SilcServer server, SilcSocketConnection sock,
1629                         SilcPacketContext *packet)
1630 {
1631   silc_server_new_id_real(server, sock, packet, TRUE);
1632 }
1633
1634 /* Receoved New Id List packet, list of New ID payloads inside one
1635    packet. Process the New ID payloads one by one. */
1636
1637 void silc_server_new_id_list(SilcServer server, SilcSocketConnection sock,
1638                              SilcPacketContext *packet)
1639 {
1640   SilcPacketContext *new_id;
1641   SilcBuffer idp;
1642   uint16 id_len;
1643
1644   SILC_LOG_DEBUG(("Processing New ID List"));
1645
1646   if (sock->type == SILC_SOCKET_TYPE_CLIENT ||
1647       packet->src_id_type != SILC_ID_SERVER)
1648     return;
1649
1650   /* If the sender of this packet is server and we are router we need to
1651      broadcast this packet to other routers in the network. Broadcast
1652      this list packet instead of multiple New ID packets. */
1653   if (!server->standalone && server->server_type == SILC_ROUTER &&
1654       sock->type == SILC_SOCKET_TYPE_SERVER &&
1655       !(packet->flags & SILC_PACKET_FLAG_BROADCAST)) {
1656     SILC_LOG_DEBUG(("Broadcasting received New ID List packet"));
1657     silc_server_packet_send(server, server->router->connection,
1658                             packet->type, 
1659                             packet->flags | SILC_PACKET_FLAG_BROADCAST,
1660                             packet->buffer->data, packet->buffer->len, FALSE);
1661   }
1662
1663   /* Make copy of the original packet context, except for the actual
1664      data buffer, which we will here now fetch from the original buffer. */
1665   new_id = silc_packet_context_alloc();
1666   new_id->type = SILC_PACKET_NEW_ID;
1667   new_id->flags = packet->flags;
1668   new_id->src_id = packet->src_id;
1669   new_id->src_id_len = packet->src_id_len;
1670   new_id->src_id_type = packet->src_id_type;
1671   new_id->dst_id = packet->dst_id;
1672   new_id->dst_id_len = packet->dst_id_len;
1673   new_id->dst_id_type = packet->dst_id_type;
1674
1675   idp = silc_buffer_alloc(256);
1676   new_id->buffer = idp;
1677
1678   while (packet->buffer->len) {
1679     SILC_GET16_MSB(id_len, packet->buffer->data + 2);
1680     if ((id_len > packet->buffer->len) ||
1681         (id_len > idp->truelen))
1682       break;
1683
1684     silc_buffer_pull_tail(idp, 4 + id_len);
1685     silc_buffer_put(idp, packet->buffer->data, 4 + id_len);
1686
1687     /* Process the New ID */
1688     silc_server_new_id_real(server, sock, new_id, FALSE);
1689
1690     silc_buffer_push_tail(idp, 4 + id_len);
1691     silc_buffer_pull(packet->buffer, 4 + id_len);
1692   }
1693
1694   silc_buffer_free(idp);
1695   silc_free(new_id);
1696 }
1697
1698 /* Received New Channel packet. Information about new channels in the 
1699    network are distributed using this packet. Save the information about
1700    the new channel. This usually comes from router but also normal server
1701    can send this to notify channels it has when it connects to us. */
1702
1703 void silc_server_new_channel(SilcServer server,
1704                              SilcSocketConnection sock,
1705                              SilcPacketContext *packet)
1706 {
1707   SilcChannelPayload payload;
1708   SilcChannelID *channel_id;
1709   char *channel_name;
1710   uint32 name_len;
1711   unsigned char *id;
1712   uint32 id_len;
1713   uint32 mode;
1714
1715   SILC_LOG_DEBUG(("Processing New Channel"));
1716
1717   if (sock->type == SILC_SOCKET_TYPE_CLIENT ||
1718       packet->src_id_type != SILC_ID_SERVER ||
1719       server->server_type == SILC_SERVER)
1720     return;
1721
1722   /* Parse the channel payload */
1723   payload = silc_channel_payload_parse(packet->buffer);
1724   if (!payload)
1725     return;
1726     
1727   /* Get the channel ID */
1728   channel_id = silc_channel_get_id_parse(payload);
1729   if (!channel_id) {
1730     silc_channel_payload_free(payload);
1731     return;
1732   }
1733
1734   channel_name = silc_channel_get_name(payload, &name_len);
1735   if (name_len > 256)
1736     channel_name[255] = '\0';
1737
1738   id = silc_channel_get_id(payload, &id_len);
1739
1740   if (sock->type == SILC_SOCKET_TYPE_ROUTER) {
1741     /* Add the server to global list as it is coming from router. It 
1742        cannot be our own channel as it is coming from router. */
1743
1744     SILC_LOG_DEBUG(("New channel id(%s) from [Router] %s",
1745                     silc_id_render(channel_id, SILC_ID_CHANNEL), 
1746                     sock->hostname));
1747     
1748     silc_idlist_add_channel(server->global_list, strdup(channel_name), 
1749                             0, channel_id, server->router->connection, 
1750                             NULL, NULL);
1751
1752     server->stat.channels++;
1753   } else {
1754     /* The channel is coming from our server, thus it is in our cell
1755        we will add it to our local list. */
1756     SilcChannelEntry channel;
1757     SilcBuffer chk;
1758
1759     SILC_LOG_DEBUG(("New channel id(%s) from [Server] %s",
1760                     silc_id_render(channel_id, SILC_ID_CHANNEL), 
1761                     sock->hostname));
1762     
1763     /* Check that we don't already have this channel */
1764     channel = silc_idlist_find_channel_by_name(server->local_list, 
1765                                                channel_name, NULL);
1766     if (!channel)
1767       channel = silc_idlist_find_channel_by_name(server->global_list, 
1768                                                  channel_name, NULL);
1769
1770     /* If the channel does not exist, then create it. We create the channel
1771        with the channel ID provided by the server. This creates a new
1772        key to the channel as well that we will send to the server. */
1773     if (!channel) {
1774       channel = silc_server_create_new_channel_with_id(server, NULL, NULL,
1775                                                        channel_name,
1776                                                        channel_id, FALSE);
1777       if (!channel) {
1778         silc_channel_payload_free(payload);
1779         silc_free(channel_id);
1780         return;
1781       }
1782
1783       /* Send the new channel key to the server */
1784       chk = silc_channel_key_payload_encode(id_len, id,
1785                                             strlen(channel->channel_key->
1786                                                    cipher->name),
1787                                             channel->channel_key->cipher->name,
1788                                             channel->key_len / 8, 
1789                                             channel->key);
1790       silc_server_packet_send(server, sock, SILC_PACKET_CHANNEL_KEY, 0, 
1791                               chk->data, chk->len, FALSE);
1792       silc_buffer_free(chk);
1793
1794     } else {
1795       /* The channel exist by that name, check whether the ID's match.
1796          If they don't then we'll force the server to use the ID we have.
1797          We also create a new key for the channel. */
1798       SilcBuffer users = NULL;
1799
1800       if (!channel->id)
1801         channel_id = silc_id_dup(channel_id, SILC_ID_CHANNEL);
1802
1803       if (SILC_ID_CHANNEL_COMPARE(channel_id, channel->id)) {
1804         /* They don't match, send CHANNEL_CHANGE notify to the server to
1805            force the ID change. */
1806         SILC_LOG_DEBUG(("Forcing the server to change Channel ID"));
1807         silc_server_send_notify_channel_change(server, sock, FALSE, 
1808                                                channel_id,
1809                                                channel->id, 
1810                                                SILC_ID_CHANNEL_LEN);
1811       }
1812
1813       /* If the mode is different from what we have then enforce the
1814          mode change. */
1815       mode = silc_channel_get_mode(payload);
1816       if (channel->mode != mode) {
1817         SILC_LOG_DEBUG(("Forcing the server to change channel mode"));
1818         silc_server_send_notify_cmode(server, sock, FALSE, channel,
1819                                       channel->mode, server->id,
1820                                       SILC_ID_SERVER, SILC_ID_SERVER_LEN,
1821                                       channel->cipher, channel->hmac_name);
1822       }
1823
1824       /* Create new key for the channel and send it to the server and
1825          everybody else possibly on the channel. */
1826
1827       if (!(channel->mode & SILC_CHANNEL_MODE_PRIVKEY)) {
1828         silc_server_create_channel_key(server, channel, 0);
1829         
1830         /* Send to the channel */
1831         silc_server_send_channel_key(server, sock, channel, FALSE);
1832         id = silc_id_id2str(channel->id, SILC_ID_CHANNEL);
1833         id_len = SILC_ID_CHANNEL_LEN;
1834         
1835         /* Send to the server */
1836         chk = silc_channel_key_payload_encode(id_len, id,
1837                                               strlen(channel->channel_key->
1838                                                      cipher->name),
1839                                               channel->channel_key->
1840                                               cipher->name,
1841                                               channel->key_len / 8, 
1842                                               channel->key);
1843         silc_server_packet_send(server, sock, SILC_PACKET_CHANNEL_KEY, 0, 
1844                                 chk->data, chk->len, FALSE);
1845         silc_buffer_free(chk);
1846         silc_free(id);
1847       }
1848
1849       silc_free(channel_id);
1850
1851       /* Since the channel is coming from server and we also know about it
1852          then send the JOIN notify to the server so that it see's our
1853          users on the channel "joining" the channel. */
1854       silc_server_announce_get_channel_users(server, channel, &users);
1855       if (users) {
1856         silc_buffer_push(users, users->data - users->head);
1857         silc_server_packet_send(server, sock,
1858                                 SILC_PACKET_NOTIFY, SILC_PACKET_FLAG_LIST,
1859                                 users->data, users->len, FALSE);
1860         silc_buffer_free(users);
1861       }
1862     }
1863   }
1864
1865   silc_channel_payload_free(payload);
1866 }
1867
1868 /* Received New Channel List packet, list of New Channel List payloads inside
1869    one packet. Process the New Channel payloads one by one. */
1870
1871 void silc_server_new_channel_list(SilcServer server,
1872                                   SilcSocketConnection sock,
1873                                   SilcPacketContext *packet)
1874 {
1875   SilcPacketContext *new;
1876   SilcBuffer buffer;
1877   uint16 len1, len2;
1878
1879   SILC_LOG_DEBUG(("Processing New Channel List"));
1880
1881   if (sock->type == SILC_SOCKET_TYPE_CLIENT ||
1882       packet->src_id_type != SILC_ID_SERVER ||
1883       server->server_type == SILC_SERVER)
1884     return;
1885
1886   /* If the sender of this packet is server and we are router we need to
1887      broadcast this packet to other routers in the network. Broadcast
1888      this list packet instead of multiple New Channel packets. */
1889   if (!server->standalone && server->server_type == SILC_ROUTER &&
1890       sock->type == SILC_SOCKET_TYPE_SERVER &&
1891       !(packet->flags & SILC_PACKET_FLAG_BROADCAST)) {
1892     SILC_LOG_DEBUG(("Broadcasting received New Channel List packet"));
1893     silc_server_packet_send(server, server->router->connection,
1894                             packet->type, 
1895                             packet->flags | SILC_PACKET_FLAG_BROADCAST,
1896                             packet->buffer->data, packet->buffer->len, FALSE);
1897   }
1898
1899   /* Make copy of the original packet context, except for the actual
1900      data buffer, which we will here now fetch from the original buffer. */
1901   new = silc_packet_context_alloc();
1902   new->type = SILC_PACKET_NEW_CHANNEL;
1903   new->flags = packet->flags;
1904   new->src_id = packet->src_id;
1905   new->src_id_len = packet->src_id_len;
1906   new->src_id_type = packet->src_id_type;
1907   new->dst_id = packet->dst_id;
1908   new->dst_id_len = packet->dst_id_len;
1909   new->dst_id_type = packet->dst_id_type;
1910
1911   buffer = silc_buffer_alloc(512);
1912   new->buffer = buffer;
1913
1914   while (packet->buffer->len) {
1915     SILC_GET16_MSB(len1, packet->buffer->data);
1916     if ((len1 > packet->buffer->len) ||
1917         (len1 > buffer->truelen))
1918       break;
1919
1920     SILC_GET16_MSB(len2, packet->buffer->data + 2 + len1);
1921     if ((len2 > packet->buffer->len) ||
1922         (len2 > buffer->truelen))
1923       break;
1924
1925     silc_buffer_pull_tail(buffer, 8 + len1 + len2);
1926     silc_buffer_put(buffer, packet->buffer->data, 8 + len1 + len2);
1927
1928     /* Process the New Channel */
1929     silc_server_new_channel(server, sock, new);
1930
1931     silc_buffer_push_tail(buffer, 8 + len1 + len2);
1932     silc_buffer_pull(packet->buffer, 8 + len1 + len2);
1933   }
1934
1935   silc_buffer_free(buffer);
1936   silc_free(new);
1937 }
1938
1939 /* Received key agreement packet. This packet is never for us. It is to
1940    the client in the packet's destination ID. Sending of this sort of packet
1941    equals sending private message, ie. it is sent point to point from
1942    one client to another. */
1943
1944 void silc_server_key_agreement(SilcServer server,
1945                                SilcSocketConnection sock,
1946                                SilcPacketContext *packet)
1947 {
1948   SilcSocketConnection dst_sock;
1949   SilcIDListData idata;
1950
1951   SILC_LOG_DEBUG(("Start"));
1952
1953   if (packet->src_id_type != SILC_ID_CLIENT ||
1954       packet->dst_id_type != SILC_ID_CLIENT)
1955     return;
1956
1957   if (!packet->dst_id)
1958     return;
1959
1960   /* Get the route to the client */
1961   dst_sock = silc_server_get_client_route(server, packet->dst_id,
1962                                           packet->dst_id_len, NULL, &idata);
1963   if (!dst_sock)
1964     return;
1965
1966   /* Relay the packet */
1967   silc_server_relay_packet(server, dst_sock, idata->send_key,
1968                            idata->hmac_send, packet, FALSE);
1969 }
1970
1971 /* Received connection auth request packet that is used during connection
1972    phase to resolve the mandatory authentication method.  This packet can
1973    actually be received at anytime but usually it is used only during
1974    the connection authentication phase. Now, protocol says that this packet
1975    can come from client or server, however, we support only this coming
1976    from client and expect that server's always knows what authentication
1977    method to use. */
1978
1979 void silc_server_connection_auth_request(SilcServer server,
1980                                          SilcSocketConnection sock,
1981                                          SilcPacketContext *packet)
1982 {
1983   SilcServerConfigSectionClientConnection *client = NULL;
1984   uint16 conn_type;
1985   int ret;
1986   SilcAuthMethod auth_meth;
1987
1988   SILC_LOG_DEBUG(("Start"));
1989
1990   if (packet->src_id_type && packet->src_id_type != SILC_ID_CLIENT)
1991     return;
1992
1993   /* Parse the payload */
1994   ret = silc_buffer_unformat(packet->buffer,
1995                              SILC_STR_UI_SHORT(&conn_type),
1996                              SILC_STR_UI_SHORT(NULL),
1997                              SILC_STR_END);
1998   if (ret == -1)
1999     return;
2000
2001   if (conn_type != SILC_SOCKET_TYPE_CLIENT)
2002     return;
2003
2004   /* Get the authentication method for the client */
2005   auth_meth = SILC_AUTH_NONE;
2006   client = silc_server_config_find_client_conn(server->config,
2007                                                sock->ip,
2008                                                sock->port);
2009   if (!client)
2010     client = silc_server_config_find_client_conn(server->config,
2011                                                  sock->hostname,
2012                                                  sock->port);
2013   if (client)
2014     auth_meth = client->auth_meth;
2015           
2016   /* Send it back to the client */
2017   silc_server_send_connection_auth_request(server, sock,
2018                                            conn_type,
2019                                            auth_meth);
2020 }
2021
2022 /* Received REKEY packet. The sender of the packet wants to regenerate
2023    its session keys. This starts the REKEY protocol. */
2024
2025 void silc_server_rekey(SilcServer server,
2026                        SilcSocketConnection sock,
2027                        SilcPacketContext *packet)
2028 {
2029   SilcProtocol protocol;
2030   SilcServerRekeyInternalContext *proto_ctx;
2031   SilcIDListData idata = (SilcIDListData)sock->user_data;
2032
2033   SILC_LOG_DEBUG(("Start"));
2034
2035   /* Allocate internal protocol context. This is sent as context
2036      to the protocol. */
2037   proto_ctx = silc_calloc(1, sizeof(*proto_ctx));
2038   proto_ctx->server = (void *)server;
2039   proto_ctx->sock = sock;
2040   proto_ctx->responder = TRUE;
2041   proto_ctx->pfs = idata->rekey->pfs;
2042       
2043   /* Perform rekey protocol. Will call the final callback after the
2044      protocol is over. */
2045   silc_protocol_alloc(SILC_PROTOCOL_SERVER_REKEY, 
2046                       &protocol, proto_ctx, silc_server_rekey_final);
2047   sock->protocol = protocol;
2048
2049   if (proto_ctx->pfs == FALSE)
2050     /* Run the protocol */
2051     protocol->execute(server->timeout_queue, 0, protocol, sock->sock, 0, 0);
2052 }