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