5 Author: Pekka Riikonen <priikone@silcnet.org>
7 Copyright (C) 1997 - 2001 Pekka Riikonen
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.
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.
21 * This is the actual SILC server than handles everything relating to
22 * servicing the SILC connections. This is also a SILC router as a router
23 * is also normal server.
27 #include "serverincludes.h"
28 #include "server_internal.h"
30 /* Static prototypes */
31 SILC_TASK_CALLBACK(silc_server_connect_router);
32 SILC_TASK_CALLBACK(silc_server_connect_to_router);
33 SILC_TASK_CALLBACK(silc_server_connect_to_router_second);
34 SILC_TASK_CALLBACK(silc_server_connect_to_router_final);
35 SILC_TASK_CALLBACK(silc_server_accept_new_connection);
36 SILC_TASK_CALLBACK(silc_server_accept_new_connection_second);
37 SILC_TASK_CALLBACK(silc_server_accept_new_connection_final);
38 SILC_TASK_CALLBACK(silc_server_packet_process);
39 SILC_TASK_CALLBACK(silc_server_packet_parse_real);
40 SILC_TASK_CALLBACK(silc_server_timeout_remote);
41 SILC_TASK_CALLBACK(silc_server_failure_callback);
42 SILC_TASK_CALLBACK(silc_server_rekey_callback);
44 /* Allocates a new SILC server object. This has to be done before the server
45 can be used. After allocation one must call silc_server_init to initialize
46 the server. The new allocated server object is returned to the new_server
49 int silc_server_alloc(SilcServer *new_server)
53 SILC_LOG_DEBUG(("Allocating new server object"));
55 server = silc_calloc(1, sizeof(*server));
56 server->server_type = SILC_SERVER;
57 server->standalone = TRUE;
58 server->local_list = silc_calloc(1, sizeof(*server->local_list));
59 server->global_list = silc_calloc(1, sizeof(*server->global_list));
60 server->pending_commands = silc_dlist_init();
62 server->sim = silc_dlist_init();
70 /* Free's the SILC server object. This is called at the very end before
73 void silc_server_free(SilcServer server)
80 silc_free(server->local_list);
81 silc_free(server->global_list);
83 silc_rng_free(server->rng);
86 silc_pkcs_free(server->pkcs);
89 while ((sim = silc_dlist_get(server->sim)) != SILC_LIST_END) {
90 silc_dlist_del(server->sim, sim);
93 silc_dlist_uninit(server->sim);
96 silc_free(server->params);
98 if (server->pending_commands)
99 silc_dlist_uninit(server->pending_commands);
105 /* Initializes the entire SILC server. This is called always before running
106 the server. This is called only once at the initialization of the program.
107 This binds the server to its listenning port. After this function returns
108 one should call silc_server_run to start the server. This returns TRUE
109 when everything is ok to run the server. Configuration file must be
110 read and parsed before calling this. */
112 int silc_server_init(SilcServer server)
114 int *sock = NULL, sock_count = 0, i;
116 SilcServerEntry id_entry;
117 SilcIDListPurge purge;
118 SilcServerConfigSectionListenPort *listen;
120 SILC_LOG_DEBUG(("Initializing server"));
122 assert(server->config);
124 /* Set public and private keys */
125 if (!server->config->server_keys ||
126 !server->config->server_keys->public_key ||
127 !server->config->server_keys->private_key) {
128 SILC_LOG_ERROR(("Server public key and/or private key does not exist"));
131 server->public_key = server->config->server_keys->public_key;
132 server->private_key = server->config->server_keys->private_key;
134 /* XXX After server is made as Silc Server Library this can be given
135 as argument, for now this is hard coded */
136 server->params = silc_calloc(1, sizeof(*server->params));
137 server->params->retry_count = SILC_SERVER_RETRY_COUNT;
138 server->params->retry_interval_min = SILC_SERVER_RETRY_INTERVAL_MIN;
139 server->params->retry_interval_max = SILC_SERVER_RETRY_INTERVAL_MAX;
140 server->params->retry_keep_trying = FALSE;
141 server->params->protocol_timeout = 60;
142 server->params->require_reverse_mapping = FALSE;
144 /* Set log files where log message should be saved. */
145 server->config->server = server;
146 silc_server_config_setlogfiles(server->config);
148 /* Register all configured ciphers, PKCS and hash functions. */
149 if (!silc_server_config_register_ciphers(server->config))
150 silc_cipher_register_default();
151 if (!silc_server_config_register_pkcs(server->config))
152 silc_pkcs_register_default();
153 if (!silc_server_config_register_hashfuncs(server->config))
154 silc_hash_register_default();
155 if (!silc_server_config_register_hmacs(server->config))
156 silc_hmac_register_default();
158 /* Initialize random number generator for the server. */
159 server->rng = silc_rng_alloc();
160 silc_rng_init(server->rng);
161 silc_rng_global_init(server->rng);
163 /* Initialize hash functions for server to use */
164 silc_hash_alloc("md5", &server->md5hash);
165 silc_hash_alloc("sha1", &server->sha1hash);
167 /* Initialize none cipher */
168 silc_cipher_alloc("none", &server->none_cipher);
170 /* Allocate PKCS context for local public and private keys */
171 silc_pkcs_alloc(server->public_key->name, &server->pkcs);
172 silc_pkcs_public_key_set(server->pkcs, server->public_key);
173 silc_pkcs_private_key_set(server->pkcs, server->private_key);
175 /* Create a listening server. Note that our server can listen on multiple
176 ports. All listeners are created here and now. */
178 listen = server->config->listen_port;
182 tmp = silc_net_create_server(server->config->listen_port->port,
183 server->config->listen_port->listener_ip);
185 SILC_LOG_ERROR(("Could not create server listener: %s on %d",
186 server->config->listen_port->listener_ip,
187 server->config->listen_port->port));
191 sock = silc_realloc(sock, (sizeof(int *) * (sock_count + 1)));
192 sock[sock_count] = tmp;
194 listen = listen->next;
197 /* Initialize ID caches */
198 server->local_list->clients =
199 silc_idcache_alloc(0, SILC_ID_CLIENT, silc_idlist_client_destructor);
200 server->local_list->servers = silc_idcache_alloc(0, SILC_ID_SERVER, NULL);
201 server->local_list->channels = silc_idcache_alloc(0, SILC_ID_CHANNEL, NULL);
203 /* These are allocated for normal server as well as these hold some
204 global information that the server has fetched from its router. For
205 router these are used as they are supposed to be used on router. */
206 server->global_list->clients =
207 silc_idcache_alloc(0, SILC_ID_CLIENT, silc_idlist_client_destructor);
208 server->global_list->servers = silc_idcache_alloc(0, SILC_ID_SERVER, NULL);
209 server->global_list->channels = silc_idcache_alloc(0, SILC_ID_CHANNEL, NULL);
211 /* Allocate the entire socket list that is used in server. Eventually
212 all connections will have entry in this table (it is a table of
213 pointers to the actual object that is allocated individually
215 server->sockets = silc_calloc(SILC_SERVER_MAX_CONNECTIONS,
216 sizeof(*server->sockets));
218 for (i = 0; i < sock_count; i++) {
219 SilcSocketConnection newsocket = NULL;
221 /* Set socket to non-blocking mode */
222 silc_net_set_socket_nonblock(sock[i]);
223 server->sock = sock[i];
225 /* Create a Server ID for the server. */
226 silc_id_create_server_id(sock[i], server->rng, &id);
232 server->id_string = silc_id_id2str(id, SILC_ID_SERVER);
233 server->id_string_len = silc_id_get_len(id, SILC_ID_SERVER);
234 server->id_type = SILC_ID_SERVER;
235 server->server_name = server->config->server_info->server_name;
237 /* Add ourselves to the server list. We don't have a router yet
238 beacuse we haven't established a route yet. It will be done later.
239 For now, NULL is sent as router. This allocates new entry to
242 silc_idlist_add_server(server->local_list,
243 server->config->server_info->server_name,
244 server->server_type, server->id, NULL, NULL);
246 SILC_LOG_ERROR(("Could not add ourselves to cache"));
249 id_entry->data.status |= SILC_IDLIST_STATUS_REGISTERED;
251 /* Add ourselves also to the socket table. The entry allocated above
252 is sent as argument for fast referencing in the future. */
253 silc_socket_alloc(sock[i], SILC_SOCKET_TYPE_SERVER, id_entry,
256 server->sockets[sock[i]] = newsocket;
258 /* Perform name and address lookups to resolve the listenning address
260 if (!silc_net_check_local_by_sock(sock[i], &newsocket->hostname,
262 if ((server->params->require_reverse_mapping && !newsocket->hostname) ||
264 SILC_LOG_ERROR(("IP/DNS lookup failed for local host %s",
265 newsocket->hostname ? newsocket->hostname :
266 newsocket->ip ? newsocket->ip : ""));
267 server->stat.conn_failures++;
270 if (!newsocket->hostname)
271 newsocket->hostname = strdup(newsocket->ip);
273 newsocket->port = silc_net_get_local_port(sock[i]);
275 /* Put the allocated socket pointer also to the entry allocated above
276 for fast back-referencing to the socket list. */
277 id_entry->connection = (void *)server->sockets[sock[i]];
278 server->id_entry = id_entry;
281 /* Register protocols */
282 silc_server_protocols_register();
284 /* Initialize the scheduler. */
285 server->schedule = silc_schedule_init(SILC_SERVER_MAX_CONNECTIONS);
286 if (!server->schedule)
289 /* Add the first task to the scheduler. This is task that is executed by
290 timeout. It expires as soon as the caller calls silc_server_run. This
291 task performs authentication protocol and key exchange with our
293 silc_schedule_task_add(server->schedule, sock[0],
294 silc_server_connect_to_router,
295 (void *)server, 0, 1,
297 SILC_TASK_PRI_NORMAL);
299 /* Add listener task to the scheduler. This task receives new connections
300 to the server. This task remains on the queue until the end of the
302 silc_schedule_task_add(server->schedule, sock[0],
303 silc_server_accept_new_connection,
304 (void *)server, 0, 0,
306 SILC_TASK_PRI_NORMAL);
307 server->listenning = TRUE;
309 /* If server connections has been configured then we must be router as
310 normal server cannot have server connections, only router connections. */
311 if (server->config->servers) {
312 SilcServerConfigSectionServerConnection *ptr = server->config->servers;
314 server->server_type = SILC_ROUTER;
316 if (ptr->backup_router) {
317 server->server_type = SILC_BACKUP_ROUTER;
318 server->backup_router = TRUE;
319 server->id_entry->server_type = SILC_BACKUP_ROUTER;
326 /* Register the ID Cache purge task. This periodically purges the ID cache
327 and removes the expired cache entries. */
329 /* Clients local list */
330 purge = silc_calloc(1, sizeof(*purge));
331 purge->cache = server->local_list->clients;
332 purge->schedule = server->schedule;
333 silc_schedule_task_add(purge->schedule, 0,
335 (void *)purge, 600, 0,
336 SILC_TASK_TIMEOUT, SILC_TASK_PRI_LOW);
338 /* Clients global list */
339 purge = silc_calloc(1, sizeof(*purge));
340 purge->cache = server->global_list->clients;
341 purge->schedule = server->schedule;
342 silc_schedule_task_add(purge->schedule, 0,
344 (void *)purge, 300, 0,
345 SILC_TASK_TIMEOUT, SILC_TASK_PRI_LOW);
347 SILC_LOG_DEBUG(("Server initialized"));
349 /* We are done here, return succesfully */
353 for (i = 0; i < sock_count; i++)
354 silc_net_close_server(sock[i]);
359 /* Fork server to background and set gid+uid to non-root.
360 Silcd will not run as root, so trying to set either user or group to
361 root will cause silcd to exit. */
363 void silc_server_daemonise(SilcServer server)
365 /* Are we executing silcd as root or a regular user? */
372 if (!server->config->identity || !server->config->identity->user ||
373 !server->config->identity->group) {
374 fprintf(stderr, "Error:"
375 "\tSILC server must not be run as root. For the security of your\n"
376 "\tsystem it is strongly suggested that you run SILC under dedicated\n"
377 "\tuser account. Modify the [Identity] configuration section to run\n"
378 "\tthe server as non-root user.\n");
382 /* Get the values given for user and group in configuration file */
383 user=server->config->identity->user;
384 group=server->config->identity->group;
386 /* Check whether the user/group information is text */
387 if (atoi(user)!=0 || atoi(group)!=0) {
388 SILC_LOG_DEBUG(("Invalid user and/or group information"));
389 SILC_LOG_DEBUG(("User and/or group given as number"));
390 fprintf(stderr, "Invalid user and/or group information\n");
391 fprintf(stderr, "Please assign them as names, not numbers\n");
395 /* Catch the nasty incident of string "0" returning 0 from atoi */
396 if (strcmp("0", user)==0 || strcmp("0", group)==0) {
397 SILC_LOG_DEBUG(("User and/or group configured to 0. Unacceptable"));
398 fprintf(stderr, "User and/or group configured to 0. Exiting\n");
406 fprintf(stderr, "No such user %s found\n", user);
411 fprintf(stderr, "No such group %s found\n", group);
415 /* Check whether user and/or group is set to root. If yes, exit
416 immediately. Otherwise, setgid and setuid server to user.group */
417 if (gr->gr_gid==0 || pw->pw_uid==0) {
418 fprintf(stderr, "Error:"
419 "\tSILC server must not be run as root. For the security of your\n"
420 "\tsystem it is strongly suggested that you run SILC under dedicated\n"
421 "\tuser account. Modify the [Identity] configuration section to run\n"
422 "\tthe server as non-root user.\n");
425 /* Fork server to background, making it a daemon */
427 SILC_LOG_DEBUG(("Server started as root. Dropping privileges."));
428 SILC_LOG_DEBUG(("Forking SILC server to background"));
433 SILC_LOG_DEBUG(("Changing to group %s", group));
434 if(setgid(gr->gr_gid)==0) {
435 SILC_LOG_DEBUG(("Setgid to %s", group));
437 SILC_LOG_DEBUG(("Setgid to %s failed", group));
438 fprintf(stderr, "Tried to setgid %s but no such group. Exiting\n",
442 SILC_LOG_DEBUG(("Changing to user nobody"));
443 if(setuid(pw->pw_uid)==0) {
444 SILC_LOG_DEBUG(("Setuid to %s", user));
446 SILC_LOG_DEBUG(("Setuid to %s failed", user));
447 fprintf(stderr, "Tried to setuid %s but no such user. Exiting\n",
453 /* Fork server to background, making it a daemon */
455 SILC_LOG_DEBUG(("Server started as user"));
456 SILC_LOG_DEBUG(("Forking SILC server to background"));
463 /* The heart of the server. This runs the scheduler thus runs the server.
464 When this returns the server has been stopped and the program will
467 void silc_server_run(SilcServer server)
469 SILC_LOG_DEBUG(("Running server"));
471 SILC_LOG_INFO(("SILC Server started"));
473 /* Start the scheduler, the heart of the SILC server. When this returns
474 the program will be terminated. */
475 silc_schedule(server->schedule);
478 /* Stops the SILC server. This function is used to shutdown the server.
479 This is usually called after the scheduler has returned. After stopping
480 the server one should call silc_server_free. */
482 void silc_server_stop(SilcServer server)
484 SILC_LOG_DEBUG(("Stopping server"));
486 silc_schedule_stop(server->schedule);
487 silc_schedule_uninit(server->schedule);
489 silc_server_protocols_unregister();
491 SILC_LOG_DEBUG(("Server stopped"));
494 /* Function that is called when the network connection to a router has
495 been established. This will continue with the key exchange protocol
496 with the remote router. */
498 void silc_server_start_key_exchange(SilcServer server,
499 SilcServerConnection sconn,
502 SilcSocketConnection newsocket;
503 SilcProtocol protocol;
504 SilcServerKEInternalContext *proto_ctx;
507 /* Set socket options */
508 silc_net_set_socket_nonblock(sock);
509 silc_net_set_socket_opt(sock, SOL_SOCKET, SO_REUSEADDR, 1);
511 /* Create socket connection for the connection. Even though we
512 know that we are connecting to a router we will mark the socket
513 to be unknown connection until we have executed authentication
515 silc_socket_alloc(sock, SILC_SOCKET_TYPE_UNKNOWN, NULL, &newsocket);
516 server->sockets[sock] = newsocket;
517 newsocket->hostname = strdup(sconn->remote_host);
518 newsocket->ip = strdup(sconn->remote_host);
519 newsocket->port = sconn->remote_port;
520 sconn->sock = newsocket;
522 /* Allocate internal protocol context. This is sent as context
524 proto_ctx = silc_calloc(1, sizeof(*proto_ctx));
525 proto_ctx->server = (void *)server;
526 proto_ctx->context = (void *)sconn;
527 proto_ctx->sock = newsocket;
528 proto_ctx->rng = server->rng;
529 proto_ctx->responder = FALSE;
531 /* Perform key exchange protocol. silc_server_connect_to_router_second
532 will be called after the protocol is finished. */
533 silc_protocol_alloc(SILC_PROTOCOL_SERVER_KEY_EXCHANGE,
534 &protocol, proto_ctx,
535 silc_server_connect_to_router_second);
536 newsocket->protocol = protocol;
538 /* Register a timeout task that will be executed if the protocol
539 is not executed within set limit. */
540 proto_ctx->timeout_task =
541 silc_schedule_task_add(server->schedule, sock,
542 silc_server_timeout_remote,
543 server, server->params->protocol_timeout,
544 server->params->protocol_timeout_usec,
548 /* Register the connection for network input and output. This sets
549 that scheduler will listen for incoming packets for this connection
550 and sets that outgoing packets may be sent to this connection as
551 well. However, this doesn't set the scheduler for outgoing traffic,
552 it will be set separately by calling SILC_SET_CONNECTION_FOR_OUTPUT,
553 later when outgoing data is available. */
554 context = (void *)server;
555 SILC_REGISTER_CONNECTION_FOR_IO(sock);
557 /* Run the protocol */
558 silc_protocol_execute(protocol, server->schedule, 0, 0);
561 /* Timeout callback that will be called to retry connecting to remote
562 router. This is used by both normal and router server. This will wait
563 before retrying the connecting. The timeout is generated by exponential
564 backoff algorithm. */
566 SILC_TASK_CALLBACK(silc_server_connect_to_router_retry)
568 SilcServerConnection sconn = (SilcServerConnection)context;
569 SilcServer server = sconn->server;
571 SILC_LOG_INFO(("Retrying connecting to a router"));
573 /* Calculate next timeout */
574 if (sconn->retry_count >= 1) {
575 sconn->retry_timeout = sconn->retry_timeout * SILC_SERVER_RETRY_MULTIPLIER;
576 if (sconn->retry_timeout > SILC_SERVER_RETRY_INTERVAL_MAX)
577 sconn->retry_timeout = SILC_SERVER_RETRY_INTERVAL_MAX;
579 sconn->retry_timeout = server->params->retry_interval_min;
581 sconn->retry_count++;
582 sconn->retry_timeout = sconn->retry_timeout +
583 silc_rng_get_rn32(server->rng) % SILC_SERVER_RETRY_RANDOMIZER;
585 /* If we've reached max retry count, give up. */
586 if (sconn->retry_count > server->params->retry_count &&
587 server->params->retry_keep_trying == FALSE) {
588 SILC_LOG_ERROR(("Could not connect to router, giving up"));
592 /* Wait one before retrying */
593 silc_schedule_task_add(server->schedule, fd, silc_server_connect_router,
594 context, sconn->retry_timeout,
595 server->params->retry_interval_min_usec,
596 SILC_TASK_TIMEOUT, SILC_TASK_PRI_NORMAL);
599 /* Generic routine to use connect to a router. */
601 SILC_TASK_CALLBACK(silc_server_connect_router)
603 SilcServerConnection sconn = (SilcServerConnection)context;
604 SilcServer server = sconn->server;
607 SILC_LOG_INFO(("Connecting to the %s %s on port %d",
608 (sconn->backup ? "backup router" : "router"),
609 sconn->remote_host, sconn->remote_port));
611 server->router_connect = time(0);
613 /* Connect to remote host */
614 sock = silc_net_create_connection(server->config->listen_port->local_ip,
618 SILC_LOG_ERROR(("Could not connect to router"));
619 silc_schedule_task_add(server->schedule, fd,
620 silc_server_connect_to_router_retry,
621 context, 0, 1, SILC_TASK_TIMEOUT,
622 SILC_TASK_PRI_NORMAL);
626 /* Continue with key exchange protocol */
627 silc_server_start_key_exchange(server, sconn, sock);
630 /* This function connects to our primary router or if we are a router this
631 establishes all our primary routes. This is called at the start of the
632 server to do authentication and key exchange with our router - called
635 SILC_TASK_CALLBACK(silc_server_connect_to_router)
637 SilcServer server = (SilcServer)context;
638 SilcServerConnection sconn;
639 SilcServerConfigSectionServerConnection *ptr;
641 SILC_LOG_DEBUG(("Connecting to router(s)"));
643 if (server->server_type == SILC_SERVER) {
644 SILC_LOG_DEBUG(("We are normal server"));
645 } else if (server->server_type == SILC_ROUTER) {
646 SILC_LOG_DEBUG(("We are router"));
648 SILC_LOG_DEBUG(("We are backup router/normal server"));
651 /* Create the connections to all our routes */
652 ptr = server->config->routers;
655 SILC_LOG_DEBUG(("%s connection [%s] %s:%d",
656 ptr->backup_router ? "Backup router" : "Router",
657 ptr->initiator ? "Initiator" : "Responder",
658 ptr->host, ptr->port));
660 if (ptr->initiator) {
661 /* Allocate connection object for hold connection specific stuff. */
662 sconn = silc_calloc(1, sizeof(*sconn));
663 sconn->server = server;
664 sconn->remote_host = strdup(ptr->host);
665 sconn->remote_port = ptr->port;
666 sconn->backup = ptr->backup_router;
668 sconn->backup_replace_ip = strdup(ptr->backup_replace_ip);
669 sconn->backup_replace_port = ptr->backup_replace_port;
672 silc_schedule_task_add(server->schedule, fd,
673 silc_server_connect_router,
674 (void *)sconn, 0, 1, SILC_TASK_TIMEOUT,
675 SILC_TASK_PRI_NORMAL);
684 SILC_LOG_DEBUG(("No router(s), server will be standalone"));
686 /* There wasn't a configured router, we will continue but we don't
687 have a connection to outside world. We will be standalone server. */
688 server->standalone = TRUE;
691 /* Second part of connecting to router(s). Key exchange protocol has been
692 executed and now we will execute authentication protocol. */
694 SILC_TASK_CALLBACK(silc_server_connect_to_router_second)
696 SilcProtocol protocol = (SilcProtocol)context;
697 SilcServerKEInternalContext *ctx =
698 (SilcServerKEInternalContext *)protocol->context;
699 SilcServer server = (SilcServer)ctx->server;
700 SilcServerConnection sconn = (SilcServerConnection)ctx->context;
701 SilcSocketConnection sock = ctx->sock;
702 SilcServerConnAuthInternalContext *proto_ctx;
703 SilcServerConfigSectionServerConnection *conn = NULL;
705 SILC_LOG_DEBUG(("Start"));
707 if (protocol->state == SILC_PROTOCOL_STATE_ERROR ||
708 protocol->state == SILC_PROTOCOL_STATE_FAILURE) {
709 /* Error occured during protocol */
710 silc_protocol_free(protocol);
711 sock->protocol = NULL;
712 silc_ske_free_key_material(ctx->keymat);
714 silc_packet_context_free(ctx->packet);
716 silc_ske_free(ctx->ske);
717 silc_free(ctx->dest_id);
719 silc_schedule_task_del_by_callback(server->schedule,
720 silc_server_failure_callback);
721 silc_server_disconnect_remote(server, sock, "Server closed connection: "
722 "Key exchange failed");
726 /* We now have the key material as the result of the key exchange
727 protocol. Take the key material into use. Free the raw key material
728 as soon as we've set them into use. */
729 if (!silc_server_protocol_ke_set_keys(ctx->ske, ctx->sock, ctx->keymat,
730 ctx->ske->prop->cipher,
731 ctx->ske->prop->pkcs,
732 ctx->ske->prop->hash,
733 ctx->ske->prop->hmac,
734 ctx->ske->prop->group,
736 silc_protocol_free(protocol);
737 sock->protocol = NULL;
738 silc_ske_free_key_material(ctx->keymat);
740 silc_packet_context_free(ctx->packet);
742 silc_ske_free(ctx->ske);
743 silc_free(ctx->dest_id);
745 silc_schedule_task_del_by_callback(server->schedule,
746 silc_server_failure_callback);
747 silc_server_disconnect_remote(server, sock, "Server closed connection: "
748 "Key exchange failed");
751 silc_ske_free_key_material(ctx->keymat);
753 /* Allocate internal context for the authentication protocol. This
754 is sent as context for the protocol. */
755 proto_ctx = silc_calloc(1, sizeof(*proto_ctx));
756 proto_ctx->server = (void *)server;
757 proto_ctx->context = (void *)sconn;
758 proto_ctx->sock = sock;
759 proto_ctx->ske = ctx->ske; /* Save SKE object from previous protocol */
760 proto_ctx->dest_id_type = ctx->dest_id_type;
761 proto_ctx->dest_id = ctx->dest_id;
763 /* Resolve the authentication method used in this connection. Check if
764 we find a match from user configured connections */
765 conn = silc_server_config_find_router_conn(server->config,
769 /* Match found. Use the configured authentication method */
770 proto_ctx->auth_meth = conn->auth_meth;
771 if (conn->auth_data) {
772 proto_ctx->auth_data = strdup(conn->auth_data);
773 proto_ctx->auth_data_len = strlen(conn->auth_data);
776 SILC_LOG_ERROR(("Could not find connection data for %s (%s) on port",
777 sock->hostname, sock->ip, sock->port));
778 silc_protocol_free(protocol);
779 sock->protocol = NULL;
781 silc_packet_context_free(ctx->packet);
783 silc_ske_free(ctx->ske);
784 silc_free(ctx->dest_id);
786 silc_schedule_task_del_by_callback(server->schedule,
787 silc_server_failure_callback);
788 silc_server_disconnect_remote(server, sock, "Server closed connection: "
789 "Key exchange failed");
793 /* Free old protocol as it is finished now */
794 silc_protocol_free(protocol);
796 silc_packet_context_free(ctx->packet);
798 sock->protocol = NULL;
800 /* Allocate the authentication protocol. This is allocated here
801 but we won't start it yet. We will be receiving party of this
802 protocol thus we will wait that connecting party will make
804 silc_protocol_alloc(SILC_PROTOCOL_SERVER_CONNECTION_AUTH,
805 &sock->protocol, proto_ctx,
806 silc_server_connect_to_router_final);
808 /* Register timeout task. If the protocol is not executed inside
809 this timelimit the connection will be terminated. Currently
810 this is 15 seconds and is hard coded limit (XXX). */
811 proto_ctx->timeout_task =
812 silc_schedule_task_add(server->schedule, sock->sock,
813 silc_server_timeout_remote,
814 (void *)server, 15, 0,
818 /* Run the protocol */
819 silc_protocol_execute(sock->protocol, server->schedule, 0, 0);
822 /* Finalizes the connection to router. Registers a server task to the
823 queue so that we can accept new connections. */
825 SILC_TASK_CALLBACK(silc_server_connect_to_router_final)
827 SilcProtocol protocol = (SilcProtocol)context;
828 SilcServerConnAuthInternalContext *ctx =
829 (SilcServerConnAuthInternalContext *)protocol->context;
830 SilcServer server = (SilcServer)ctx->server;
831 SilcServerConnection sconn = (SilcServerConnection)ctx->context;
832 SilcSocketConnection sock = ctx->sock;
833 SilcServerEntry id_entry;
835 SilcServerHBContext hb_context;
836 unsigned char *id_string;
838 SilcIDListData idata;
840 SILC_LOG_DEBUG(("Start"));
842 if (protocol->state == SILC_PROTOCOL_STATE_ERROR ||
843 protocol->state == SILC_PROTOCOL_STATE_FAILURE) {
844 /* Error occured during protocol */
845 silc_free(ctx->dest_id);
846 silc_server_disconnect_remote(server, sock, "Server closed connection: "
847 "Authentication failed");
851 /* Add a task to the queue. This task receives new connections to the
852 server. This task remains on the queue until the end of the program. */
853 if (!server->listenning && !sconn->backup) {
854 silc_schedule_task_add(server->schedule, server->sock,
855 silc_server_accept_new_connection,
856 (void *)server, 0, 0,
858 SILC_TASK_PRI_NORMAL);
859 server->listenning = TRUE;
862 /* Send NEW_SERVER packet to the router. We will become registered
863 to the SILC network after sending this packet. */
864 id_string = silc_id_id2str(server->id, SILC_ID_SERVER);
865 id_len = silc_id_get_len(server->id, SILC_ID_SERVER);
866 packet = silc_buffer_alloc(2 + 2 + id_len + strlen(server->server_name));
867 silc_buffer_pull_tail(packet, SILC_BUFFER_END(packet));
868 silc_buffer_format(packet,
869 SILC_STR_UI_SHORT(id_len),
870 SILC_STR_UI_XNSTRING(id_string, id_len),
871 SILC_STR_UI_SHORT(strlen(server->server_name)),
872 SILC_STR_UI_XNSTRING(server->server_name,
873 strlen(server->server_name)),
876 /* Send the packet */
877 silc_server_packet_send(server, ctx->sock, SILC_PACKET_NEW_SERVER, 0,
878 packet->data, packet->len, TRUE);
879 silc_buffer_free(packet);
880 silc_free(id_string);
882 SILC_LOG_INFO(("Connected to router %s", sock->hostname));
884 /* Check that we do not have this ID already */
885 id_entry = silc_idlist_find_server_by_id(server->local_list,
886 ctx->dest_id, TRUE, NULL);
888 silc_idcache_del_by_context(server->local_list->servers, id_entry);
890 id_entry = silc_idlist_find_server_by_id(server->global_list,
891 ctx->dest_id, TRUE, NULL);
893 silc_idcache_del_by_context(server->global_list->servers, id_entry);
896 SILC_LOG_DEBUG(("New server id(%s)",
897 silc_id_render(ctx->dest_id, SILC_ID_SERVER)));
899 /* Add the connected router to global server list */
900 id_entry = silc_idlist_add_server(server->global_list,
901 strdup(sock->hostname),
902 SILC_ROUTER, ctx->dest_id, NULL, sock);
904 silc_free(ctx->dest_id);
905 silc_server_disconnect_remote(server, sock, "Server closed connection: "
906 "Authentication failed");
910 silc_idlist_add_data(id_entry, (SilcIDListData)sock->user_data);
911 silc_free(sock->user_data);
912 sock->user_data = (void *)id_entry;
913 sock->type = SILC_SOCKET_TYPE_ROUTER;
914 idata = (SilcIDListData)sock->user_data;
915 idata->status |= SILC_IDLIST_STATUS_REGISTERED;
917 /* Perform keepalive. The `hb_context' will be freed automatically
918 when finally calling the silc_socket_free function. XXX hardcoded
920 hb_context = silc_calloc(1, sizeof(*hb_context));
921 hb_context->server = server;
922 silc_socket_set_heartbeat(sock, 600, hb_context,
923 silc_server_perform_heartbeat,
926 /* Register re-key timeout */
927 idata->rekey->timeout = 3600; /* XXX hardcoded */
928 idata->rekey->context = (void *)server;
929 silc_schedule_task_add(server->schedule, sock->sock,
930 silc_server_rekey_callback,
931 (void *)sock, idata->rekey->timeout, 0,
932 SILC_TASK_TIMEOUT, SILC_TASK_PRI_NORMAL);
934 if (!sconn->backup) {
935 /* Mark this router our primary router if we're still standalone */
936 if (server->standalone) {
937 server->id_entry->router = id_entry;
938 server->router = id_entry;
939 server->standalone = FALSE;
941 /* If we are router then announce our possible servers. */
942 if (server->server_type == SILC_ROUTER)
943 silc_server_announce_servers(server, FALSE, 0,
944 server->router->connection);
946 /* Announce our clients and channels to the router */
947 silc_server_announce_clients(server, 0, server->router->connection);
948 silc_server_announce_channels(server, 0, server->router->connection);
951 /* Add this server to be our backup router */
952 silc_server_backup_add(server, id_entry, sconn->backup_replace_ip,
953 sconn->backup_replace_port, FALSE);
956 sock->protocol = NULL;
958 /* Call the completion callback to indicate that we've connected to
961 (*sconn->callback)(server, id_entry, sconn->callback_context);
964 /* Free the temporary connection data context */
966 silc_free(sconn->remote_host);
967 silc_free(sconn->backup_replace_ip);
971 /* Free the protocol object */
972 if (sock->protocol == protocol)
973 sock->protocol = NULL;
974 silc_protocol_free(protocol);
976 silc_packet_context_free(ctx->packet);
978 silc_ske_free(ctx->ske);
982 /* Host lookup callbcak that is called after the incoming connection's
983 IP and FQDN lookup is performed. This will actually check the acceptance
984 of the incoming connection and will register the key exchange protocol
985 for this connection. */
988 silc_server_accept_new_connection_lookup(SilcSocketConnection sock,
991 SilcServer server = (SilcServer)context;
992 SilcServerKEInternalContext *proto_ctx;
993 void *cconfig, *sconfig, *rconfig;
994 SilcServerConfigSectionDenyConnection *deny;
997 SILC_LOG_DEBUG(("Start"));
999 /* Check whether we could resolve both IP and FQDN. */
1000 if (!sock->ip || (!strcmp(sock->ip, sock->hostname) &&
1001 server->params->require_reverse_mapping)) {
1002 SILC_LOG_ERROR(("IP/DNS lookup failed %s",
1003 sock->hostname ? sock->hostname :
1004 sock->ip ? sock->ip : ""));
1005 server->stat.conn_failures++;
1006 silc_server_disconnect_remote(server, sock,
1007 "Server closed connection: Unknown host");
1011 /* Register the connection for network input and output. This sets
1012 that scheduler will listen for incoming packets for this connection
1013 and sets that outgoing packets may be sent to this connection as well.
1014 However, this doesn't set the scheduler for outgoing traffic, it
1015 will be set separately by calling SILC_SET_CONNECTION_FOR_OUTPUT,
1016 later when outgoing data is available. */
1017 SILC_REGISTER_CONNECTION_FOR_IO(sock->sock);
1019 SILC_LOG_INFO(("Incoming connection from %s (%s)", sock->hostname,
1022 port = server->sockets[server->sock]->port; /* Listenning port */
1024 /* Check whether this connection is denied to connect to us. */
1025 deny = silc_server_config_denied_conn(server->config, sock->ip, port);
1027 deny = silc_server_config_denied_conn(server->config, sock->hostname,
1030 /* The connection is denied */
1031 SILC_LOG_INFO(("Connection %s (%s) is denied",
1032 sock->hostname, sock->ip));
1033 silc_server_disconnect_remote(server, sock, deny->comment ?
1035 "Server closed connection: "
1036 "Connection refused");
1037 server->stat.conn_failures++;
1041 /* Check whether we have configred this sort of connection at all. We
1042 have to check all configurations since we don't know what type of
1043 connection this is. */
1044 if (!(cconfig = silc_server_config_find_client_conn(server->config,
1046 cconfig = silc_server_config_find_client_conn(server->config,
1049 if (!(sconfig = silc_server_config_find_server_conn(server->config,
1052 sconfig = silc_server_config_find_server_conn(server->config,
1055 if (!(rconfig = silc_server_config_find_router_conn(server->config,
1057 rconfig = silc_server_config_find_router_conn(server->config,
1060 if (!cconfig && !sconfig && !rconfig) {
1061 silc_server_disconnect_remote(server, sock,
1062 "Server closed connection: "
1063 "Connection refused");
1064 server->stat.conn_failures++;
1068 /* The connection is allowed */
1070 /* Allocate internal context for key exchange protocol. This is
1071 sent as context for the protocol. */
1072 proto_ctx = silc_calloc(1, sizeof(*proto_ctx));
1073 proto_ctx->server = context;
1074 proto_ctx->sock = sock;
1075 proto_ctx->rng = server->rng;
1076 proto_ctx->responder = TRUE;
1077 proto_ctx->cconfig = cconfig;
1078 proto_ctx->sconfig = sconfig;
1079 proto_ctx->rconfig = rconfig;
1081 /* Prepare the connection for key exchange protocol. We allocate the
1082 protocol but will not start it yet. The connector will be the
1083 initiator of the protocol thus we will wait for initiation from
1084 there before we start the protocol. */
1085 server->stat.auth_attempts++;
1086 silc_protocol_alloc(SILC_PROTOCOL_SERVER_KEY_EXCHANGE,
1087 &sock->protocol, proto_ctx,
1088 silc_server_accept_new_connection_second);
1090 /* Register a timeout task that will be executed if the connector
1091 will not start the key exchange protocol within 60 seconds. For
1092 now, this is a hard coded limit. After 60 secs the connection will
1093 be closed if the key exchange protocol has not been started. */
1094 proto_ctx->timeout_task =
1095 silc_schedule_task_add(server->schedule, sock->sock,
1096 silc_server_timeout_remote,
1102 /* Accepts new connections to the server. Accepting new connections are
1103 done in three parts to make it async. */
1105 SILC_TASK_CALLBACK(silc_server_accept_new_connection)
1107 SilcServer server = (SilcServer)context;
1108 SilcSocketConnection newsocket;
1111 SILC_LOG_DEBUG(("Accepting new connection"));
1113 server->stat.conn_attempts++;
1115 sock = silc_net_accept_connection(server->sock);
1117 SILC_LOG_ERROR(("Could not accept new connection: %s", strerror(errno)));
1118 server->stat.conn_failures++;
1122 /* Check max connections */
1123 if (sock > SILC_SERVER_MAX_CONNECTIONS) {
1124 SILC_LOG_ERROR(("Refusing connection, server is full"));
1125 server->stat.conn_failures++;
1129 /* Set socket options */
1130 silc_net_set_socket_nonblock(sock);
1131 silc_net_set_socket_opt(sock, SOL_SOCKET, SO_REUSEADDR, 1);
1133 /* We don't create a ID yet, since we don't know what type of connection
1134 this is yet. But, we do add the connection to the socket table. */
1135 silc_socket_alloc(sock, SILC_SOCKET_TYPE_UNKNOWN, NULL, &newsocket);
1136 server->sockets[sock] = newsocket;
1138 /* Perform asynchronous host lookup. This will lookup the IP and the
1139 FQDN of the remote connection. After the lookup is done the connection
1140 is accepted further. */
1141 silc_socket_host_lookup(newsocket, TRUE,
1142 silc_server_accept_new_connection_lookup, context,
1146 /* Second part of accepting new connection. Key exchange protocol has been
1147 performed and now it is time to do little connection authentication
1148 protocol to figure out whether this connection is client or server
1149 and whether it has right to access this server (especially server
1150 connections needs to be authenticated). */
1152 SILC_TASK_CALLBACK(silc_server_accept_new_connection_second)
1154 SilcProtocol protocol = (SilcProtocol)context;
1155 SilcServerKEInternalContext *ctx =
1156 (SilcServerKEInternalContext *)protocol->context;
1157 SilcServer server = (SilcServer)ctx->server;
1158 SilcSocketConnection sock = ctx->sock;
1159 SilcServerConnAuthInternalContext *proto_ctx;
1161 SILC_LOG_DEBUG(("Start"));
1163 if (protocol->state == SILC_PROTOCOL_STATE_ERROR ||
1164 protocol->state == SILC_PROTOCOL_STATE_FAILURE) {
1165 /* Error occured during protocol */
1166 silc_protocol_free(protocol);
1167 sock->protocol = NULL;
1168 silc_ske_free_key_material(ctx->keymat);
1170 silc_packet_context_free(ctx->packet);
1172 silc_ske_free(ctx->ske);
1173 silc_free(ctx->dest_id);
1175 silc_schedule_task_del_by_callback(server->schedule,
1176 silc_server_failure_callback);
1177 silc_server_disconnect_remote(server, sock, "Server closed connection: "
1178 "Key exchange failed");
1179 server->stat.auth_failures++;
1183 /* We now have the key material as the result of the key exchange
1184 protocol. Take the key material into use. Free the raw key material
1185 as soon as we've set them into use. */
1186 if (!silc_server_protocol_ke_set_keys(ctx->ske, ctx->sock, ctx->keymat,
1187 ctx->ske->prop->cipher,
1188 ctx->ske->prop->pkcs,
1189 ctx->ske->prop->hash,
1190 ctx->ske->prop->hmac,
1191 ctx->ske->prop->group,
1193 silc_protocol_free(protocol);
1194 sock->protocol = NULL;
1195 silc_ske_free_key_material(ctx->keymat);
1197 silc_packet_context_free(ctx->packet);
1199 silc_ske_free(ctx->ske);
1200 silc_free(ctx->dest_id);
1202 silc_schedule_task_del_by_callback(server->schedule,
1203 silc_server_failure_callback);
1204 silc_server_disconnect_remote(server, sock, "Server closed connection: "
1205 "Key exchange failed");
1206 server->stat.auth_failures++;
1209 silc_ske_free_key_material(ctx->keymat);
1211 /* Allocate internal context for the authentication protocol. This
1212 is sent as context for the protocol. */
1213 proto_ctx = silc_calloc(1, sizeof(*proto_ctx));
1214 proto_ctx->server = (void *)server;
1215 proto_ctx->sock = sock;
1216 proto_ctx->ske = ctx->ske; /* Save SKE object from previous protocol */
1217 proto_ctx->responder = TRUE;
1218 proto_ctx->dest_id_type = ctx->dest_id_type;
1219 proto_ctx->dest_id = ctx->dest_id;
1220 proto_ctx->cconfig = ctx->cconfig;
1221 proto_ctx->sconfig = ctx->sconfig;
1222 proto_ctx->rconfig = ctx->rconfig;
1224 /* Free old protocol as it is finished now */
1225 silc_protocol_free(protocol);
1227 silc_packet_context_free(ctx->packet);
1229 sock->protocol = NULL;
1231 /* Allocate the authentication protocol. This is allocated here
1232 but we won't start it yet. We will be receiving party of this
1233 protocol thus we will wait that connecting party will make
1234 their first move. */
1235 silc_protocol_alloc(SILC_PROTOCOL_SERVER_CONNECTION_AUTH,
1236 &sock->protocol, proto_ctx,
1237 silc_server_accept_new_connection_final);
1239 /* Register timeout task. If the protocol is not executed inside
1240 this timelimit the connection will be terminated. Currently
1241 this is 60 seconds and is hard coded limit (XXX). */
1242 proto_ctx->timeout_task =
1243 silc_schedule_task_add(server->schedule, sock->sock,
1244 silc_server_timeout_remote,
1245 (void *)server, 60, 0,
1250 /* Final part of accepting new connection. The connection has now
1251 been authenticated and keys has been exchanged. We also know whether
1252 this is client or server connection. */
1254 SILC_TASK_CALLBACK(silc_server_accept_new_connection_final)
1256 SilcProtocol protocol = (SilcProtocol)context;
1257 SilcServerConnAuthInternalContext *ctx =
1258 (SilcServerConnAuthInternalContext *)protocol->context;
1259 SilcServer server = (SilcServer)ctx->server;
1260 SilcSocketConnection sock = ctx->sock;
1261 SilcServerHBContext hb_context;
1262 SilcUnknownEntry entry = (SilcUnknownEntry)sock->user_data;
1263 void *id_entry = NULL;
1265 SILC_LOG_DEBUG(("Start"));
1267 if (protocol->state == SILC_PROTOCOL_STATE_ERROR ||
1268 protocol->state == SILC_PROTOCOL_STATE_FAILURE) {
1269 /* Error occured during protocol */
1270 silc_protocol_free(protocol);
1271 sock->protocol = NULL;
1273 silc_packet_context_free(ctx->packet);
1275 silc_ske_free(ctx->ske);
1276 silc_free(ctx->dest_id);
1278 silc_schedule_task_del_by_callback(server->schedule,
1279 silc_server_failure_callback);
1280 silc_server_disconnect_remote(server, sock, "Server closed connection: "
1281 "Authentication failed");
1282 server->stat.auth_failures++;
1286 entry->data.last_receive = time(NULL);
1288 switch (ctx->conn_type) {
1289 case SILC_SOCKET_TYPE_CLIENT:
1291 SilcClientEntry client;
1293 SILC_LOG_DEBUG(("Remote host is client"));
1294 SILC_LOG_INFO(("Connection from %s (%s) is client", sock->hostname,
1297 /* Add the client to the client ID cache. The nickname and Client ID
1298 and other information is created after we have received NEW_CLIENT
1299 packet from client. */
1300 client = silc_idlist_add_client(server->local_list,
1301 NULL, NULL, NULL, NULL, NULL, sock);
1303 SILC_LOG_ERROR(("Could not add new client to cache"));
1304 silc_free(sock->user_data);
1305 silc_server_disconnect_remote(server, sock,
1306 "Server closed connection: "
1307 "Authentication failed");
1308 server->stat.auth_failures++;
1313 server->stat.my_clients++;
1314 server->stat.clients++;
1315 if (server->server_type == SILC_ROUTER)
1316 server->stat.cell_clients++;
1318 id_entry = (void *)client;
1321 case SILC_SOCKET_TYPE_SERVER:
1322 case SILC_SOCKET_TYPE_ROUTER:
1324 SilcServerEntry new_server;
1325 SilcServerConfigSectionServerConnection *conn =
1326 ctx->conn_type == SILC_SOCKET_TYPE_SERVER ?
1327 ctx->sconfig : ctx->rconfig;
1329 SILC_LOG_DEBUG(("Remote host is %s",
1330 ctx->conn_type == SILC_SOCKET_TYPE_SERVER ?
1331 "server" : (conn->backup_router ?
1332 "backup router" : "router")));
1333 SILC_LOG_INFO(("Connection from %s (%s) is %s", sock->hostname,
1334 sock->ip, ctx->conn_type == SILC_SOCKET_TYPE_SERVER ?
1335 "server" : (conn->backup_router ?
1336 "backup router" : "router")));
1338 /* Add the server into server cache. The server name and Server ID
1339 is updated after we have received NEW_SERVER packet from the
1340 server. We mark ourselves as router for this server if we really
1343 silc_idlist_add_server((ctx->conn_type == SILC_SOCKET_TYPE_SERVER ?
1344 server->local_list : (conn->backup_router ?
1345 server->local_list :
1346 server->global_list)),
1348 (ctx->conn_type == SILC_SOCKET_TYPE_SERVER ?
1349 SILC_SERVER : SILC_ROUTER),
1351 (ctx->conn_type == SILC_SOCKET_TYPE_SERVER ?
1352 server->id_entry : (conn->backup_router ?
1353 server->id_entry : NULL)),
1356 SILC_LOG_ERROR(("Could not add new server to cache"));
1357 silc_free(sock->user_data);
1358 silc_server_disconnect_remote(server, sock,
1359 "Server closed connection: "
1360 "Authentication failed");
1361 server->stat.auth_failures++;
1366 if (ctx->conn_type == SILC_SOCKET_TYPE_SERVER)
1367 server->stat.my_servers++;
1369 server->stat.my_routers++;
1370 server->stat.servers++;
1372 id_entry = (void *)new_server;
1374 /* If the incoming connection is router and marked as backup router
1375 then add it to be one of our backups */
1376 if (ctx->conn_type == SILC_SOCKET_TYPE_ROUTER && conn->backup_router) {
1377 silc_server_backup_add(server, new_server, conn->backup_replace_ip,
1378 conn->backup_replace_port, conn->backup_local);
1380 /* Change it back to SERVER type since that's what it really is. */
1381 if (conn->backup_local)
1382 ctx->conn_type = SILC_SOCKET_TYPE_SERVER;
1384 new_server->server_type = SILC_BACKUP_ROUTER;
1387 /* Check whether this connection is to be our primary router connection
1388 if we do not already have the primary route. */
1389 if (server->standalone && ctx->conn_type == SILC_SOCKET_TYPE_ROUTER) {
1390 if (silc_server_config_is_primary_route(server->config) &&
1394 SILC_LOG_DEBUG(("We are not standalone server anymore"));
1395 server->standalone = FALSE;
1396 if (!server->id_entry->router) {
1397 server->id_entry->router = id_entry;
1398 server->router = id_entry;
1408 sock->type = ctx->conn_type;
1410 /* Add the common data structure to the ID entry. */
1412 silc_idlist_add_data(id_entry, (SilcIDListData)sock->user_data);
1414 /* Add to sockets internal pointer for fast referencing */
1415 silc_free(sock->user_data);
1416 sock->user_data = id_entry;
1418 /* Connection has been fully established now. Everything is ok. */
1419 SILC_LOG_DEBUG(("New connection authenticated"));
1421 /* Perform keepalive. The `hb_context' will be freed automatically
1422 when finally calling the silc_socket_free function. XXX hardcoded
1424 hb_context = silc_calloc(1, sizeof(*hb_context));
1425 hb_context->server = server;
1426 silc_socket_set_heartbeat(sock, 600, hb_context,
1427 silc_server_perform_heartbeat,
1431 silc_schedule_task_del_by_callback(server->schedule,
1432 silc_server_failure_callback);
1433 silc_protocol_free(protocol);
1435 silc_packet_context_free(ctx->packet);
1437 silc_ske_free(ctx->ske);
1438 silc_free(ctx->dest_id);
1440 sock->protocol = NULL;
1443 /* This function is used to read packets from network and send packets to
1444 network. This is usually a generic task. */
1446 SILC_TASK_CALLBACK(silc_server_packet_process)
1448 SilcServer server = (SilcServer)context;
1449 SilcSocketConnection sock = server->sockets[fd];
1450 SilcIDListData idata;
1451 SilcCipher cipher = NULL;
1452 SilcHmac hmac = NULL;
1453 uint32 sequence = 0;
1459 SILC_LOG_DEBUG(("Processing packet"));
1461 /* Packet sending */
1463 if (type == SILC_TASK_WRITE) {
1464 /* Do not send data to disconnected connection */
1465 if (SILC_IS_DISCONNECTED(sock))
1468 server->stat.packets_sent++;
1470 if (sock->outbuf->data - sock->outbuf->head)
1471 silc_buffer_push(sock->outbuf, sock->outbuf->data - sock->outbuf->head);
1473 /* Send the packet */
1474 ret = silc_packet_send(sock, TRUE);
1476 /* If returned -2 could not write to connection now, will do
1482 SILC_LOG_ERROR(("Error sending packet to connection "
1483 "%s:%d [%s]", sock->hostname, sock->port,
1484 (sock->type == SILC_SOCKET_TYPE_UNKNOWN ? "Unknown" :
1485 sock->type == SILC_SOCKET_TYPE_CLIENT ? "Client" :
1486 sock->type == SILC_SOCKET_TYPE_SERVER ? "Server" :
1491 /* The packet has been sent and now it is time to set the connection
1492 back to only for input. When there is again some outgoing data
1493 available for this connection it will be set for output as well.
1494 This call clears the output setting and sets it only for input. */
1495 SILC_SET_CONNECTION_FOR_INPUT(server->schedule, fd);
1496 SILC_UNSET_OUTBUF_PENDING(sock);
1498 silc_buffer_clear(sock->outbuf);
1502 /* Packet receiving */
1504 /* Read some data from connection */
1505 ret = silc_packet_receive(sock);
1509 SILC_LOG_ERROR(("Error receiving packet from connection "
1510 "%s:%d [%s] %s", sock->hostname, sock->port,
1511 (sock->type == SILC_SOCKET_TYPE_UNKNOWN ? "Unknown" :
1512 sock->type == SILC_SOCKET_TYPE_CLIENT ? "Client" :
1513 sock->type == SILC_SOCKET_TYPE_SERVER ? "Server" :
1514 "Router"), strerror(errno)));
1520 SILC_LOG_DEBUG(("Read EOF"));
1522 /* If connection is disconnecting already we will finally
1523 close the connection */
1524 if (SILC_IS_DISCONNECTING(sock)) {
1525 if (sock->user_data)
1526 silc_server_free_sock_user_data(server, sock);
1527 silc_server_close_connection(server, sock);
1531 SILC_LOG_DEBUG(("Premature EOF from connection %d", sock->sock));
1532 SILC_SET_DISCONNECTING(sock);
1534 if (sock->user_data)
1535 silc_server_free_sock_user_data(server, sock);
1536 silc_server_close_connection(server, sock);
1540 /* If connection is disconnecting or disconnected we will ignore
1542 if (SILC_IS_DISCONNECTING(sock) || SILC_IS_DISCONNECTED(sock)) {
1543 SILC_LOG_DEBUG(("Ignoring read data from disonnected connection"));
1547 server->stat.packets_received++;
1549 /* Get keys and stuff from ID entry */
1550 idata = (SilcIDListData)sock->user_data;
1552 cipher = idata->receive_key;
1553 hmac = idata->hmac_receive;
1554 sequence = idata->psn_receive;
1557 /* Process the packet. This will call the parser that will then
1558 decrypt and parse the packet. */
1559 silc_packet_receive_process(sock, server->server_type == SILC_ROUTER ?
1560 TRUE : FALSE, cipher, hmac, sequence,
1561 silc_server_packet_parse, server);
1564 /* Parses whole packet, received earlier. */
1566 SILC_TASK_CALLBACK(silc_server_packet_parse_real)
1568 SilcPacketParserContext *parse_ctx = (SilcPacketParserContext *)context;
1569 SilcServer server = (SilcServer)parse_ctx->context;
1570 SilcSocketConnection sock = parse_ctx->sock;
1571 SilcPacketContext *packet = parse_ctx->packet;
1572 SilcIDListData idata = (SilcIDListData)sock->user_data;
1575 SILC_LOG_DEBUG(("Start"));
1577 /* Parse the packet */
1578 if (parse_ctx->normal)
1579 ret = silc_packet_parse(packet, idata ? idata->receive_key : NULL);
1581 ret = silc_packet_parse_special(packet, idata ? idata->receive_key : NULL);
1583 /* If entry is disabled ignore what we got. */
1584 if (ret != SILC_PACKET_RESUME_ROUTER &&
1585 idata && idata->status & SILC_IDLIST_STATUS_DISABLED) {
1586 SILC_LOG_DEBUG(("Connection is disabled"));
1590 if (ret == SILC_PACKET_NONE)
1593 /* Check that the the current client ID is same as in the client's packet. */
1594 if (sock->type == SILC_SOCKET_TYPE_CLIENT) {
1595 SilcClientEntry client = (SilcClientEntry)sock->user_data;
1596 if (client && client->id) {
1597 void *id = silc_id_str2id(packet->src_id, packet->src_id_len,
1598 packet->src_id_type);
1599 if (!id || !SILC_ID_CLIENT_COMPARE(client->id, id)) {
1607 if (server->server_type == SILC_ROUTER) {
1608 /* Route the packet if it is not destined to us. Other ID types but
1609 server are handled separately after processing them. */
1610 if (!(packet->flags & SILC_PACKET_FLAG_BROADCAST) &&
1611 packet->dst_id_type == SILC_ID_SERVER &&
1612 sock->type != SILC_SOCKET_TYPE_CLIENT &&
1613 memcmp(packet->dst_id, server->id_string, packet->dst_id_len)) {
1615 /* Route the packet to fastest route for the destination ID */
1616 void *id = silc_id_str2id(packet->dst_id, packet->dst_id_len,
1617 packet->dst_id_type);
1620 silc_server_packet_route(server,
1621 silc_server_route_get(server, id,
1622 packet->dst_id_type),
1629 /* Parse the incoming packet type */
1630 silc_server_packet_parse_type(server, sock, packet);
1632 if (server->server_type == SILC_ROUTER) {
1633 /* Broadcast packet if it is marked as broadcast packet and it is
1634 originated from router and we are router. */
1635 if (sock->type == SILC_SOCKET_TYPE_ROUTER &&
1636 packet->flags & SILC_PACKET_FLAG_BROADCAST &&
1637 !server->standalone) {
1638 /* Broadcast to our primary route */
1639 silc_server_packet_broadcast(server, server->router->connection, packet);
1641 /* If we have backup routers then we need to feed all broadcast
1642 data to those servers. */
1643 silc_server_backup_broadcast(server, sock, packet);
1648 silc_packet_context_free(packet);
1649 silc_free(parse_ctx);
1652 /* Parser callback called by silc_packet_receive_process. This merely
1653 registers timeout that will handle the actual parsing when appropriate. */
1655 bool silc_server_packet_parse(SilcPacketParserContext *parser_context,
1658 SilcServer server = (SilcServer)context;
1659 SilcSocketConnection sock = parser_context->sock;
1660 SilcIDListData idata = (SilcIDListData)sock->user_data;
1663 idata->psn_receive = parser_context->packet->sequence + 1;
1665 /* If protocol for this connection is key exchange or rekey then we'll
1666 process all packets synchronously, since there might be packets in
1667 queue that we are not able to decrypt without first processing the
1668 packets before them. */
1669 if (sock->protocol && sock->protocol->protocol &&
1670 (sock->protocol->protocol->type == SILC_PROTOCOL_SERVER_KEY_EXCHANGE ||
1671 sock->protocol->protocol->type == SILC_PROTOCOL_SERVER_REKEY)) {
1672 silc_server_packet_parse_real(server->schedule, 0, sock->sock,
1675 /* Reprocess data since we'll return FALSE here. This is because
1676 the idata->receive_key might have become valid in the last packet
1677 and we want to call this processor with valid cipher. */
1679 silc_packet_receive_process(sock, server->server_type == SILC_ROUTER ?
1680 TRUE : FALSE, idata->receive_key,
1681 idata->hmac_receive, idata->psn_receive,
1682 silc_server_packet_parse, server);
1684 silc_packet_receive_process(sock, server->server_type == SILC_ROUTER ?
1685 TRUE : FALSE, NULL, NULL, 0,
1686 silc_server_packet_parse, server);
1690 switch (sock->type) {
1691 case SILC_SOCKET_TYPE_UNKNOWN:
1692 case SILC_SOCKET_TYPE_CLIENT:
1693 /* Parse the packet with timeout */
1694 silc_schedule_task_add(server->schedule, sock->sock,
1695 silc_server_packet_parse_real,
1696 (void *)parser_context, 0, 100000,
1698 SILC_TASK_PRI_NORMAL);
1700 case SILC_SOCKET_TYPE_SERVER:
1701 case SILC_SOCKET_TYPE_ROUTER:
1702 /* Packets from servers are parsed immediately */
1703 silc_server_packet_parse_real(server->schedule, 0, sock->sock,
1713 /* Parses the packet type and calls what ever routines the packet type
1714 requires. This is done for all incoming packets. */
1716 void silc_server_packet_parse_type(SilcServer server,
1717 SilcSocketConnection sock,
1718 SilcPacketContext *packet)
1720 SilcPacketType type = packet->type;
1721 SilcIDListData idata = (SilcIDListData)sock->user_data;
1723 SILC_LOG_DEBUG(("Parsing packet type %d", type));
1725 /* Parse the packet type */
1727 case SILC_PACKET_DISCONNECT:
1728 SILC_LOG_DEBUG(("Disconnect packet"));
1729 if (packet->flags & SILC_PACKET_FLAG_LIST)
1733 case SILC_PACKET_SUCCESS:
1735 * Success received for something. For now we can have only
1736 * one protocol for connection executing at once hence this
1737 * success message is for whatever protocol is executing currently.
1739 SILC_LOG_DEBUG(("Success packet"));
1740 if (packet->flags & SILC_PACKET_FLAG_LIST)
1743 silc_protocol_execute(sock->protocol, server->schedule, 0, 0);
1746 case SILC_PACKET_FAILURE:
1748 * Failure received for something. For now we can have only
1749 * one protocol for connection executing at once hence this
1750 * failure message is for whatever protocol is executing currently.
1752 SILC_LOG_DEBUG(("Failure packet"));
1753 if (packet->flags & SILC_PACKET_FLAG_LIST)
1755 if (sock->protocol) {
1756 SilcServerFailureContext f;
1757 f = silc_calloc(1, sizeof(*f));
1761 /* We will wait 5 seconds to process this failure packet */
1762 silc_schedule_task_add(server->schedule, sock->sock,
1763 silc_server_failure_callback, (void *)f, 5, 0,
1764 SILC_TASK_TIMEOUT, SILC_TASK_PRI_NORMAL);
1768 case SILC_PACKET_REJECT:
1769 SILC_LOG_DEBUG(("Reject packet"));
1770 if (packet->flags & SILC_PACKET_FLAG_LIST)
1775 case SILC_PACKET_NOTIFY:
1777 * Received notify packet. Server can receive notify packets from
1778 * router. Server then relays the notify messages to clients if needed.
1780 SILC_LOG_DEBUG(("Notify packet"));
1781 if (packet->flags & SILC_PACKET_FLAG_LIST)
1782 silc_server_notify_list(server, sock, packet);
1784 silc_server_notify(server, sock, packet);
1790 case SILC_PACKET_CHANNEL_MESSAGE:
1792 * Received channel message. Channel messages are special packets
1793 * (although probably most common ones) thus they are handled
1796 SILC_LOG_DEBUG(("Channel Message packet"));
1797 if (packet->flags & SILC_PACKET_FLAG_LIST)
1799 idata->last_receive = time(NULL);
1800 silc_server_channel_message(server, sock, packet);
1803 case SILC_PACKET_CHANNEL_KEY:
1805 * Received key for channel. As channels are created by the router
1806 * the keys are as well. We will distribute the key to all of our
1807 * locally connected clients on the particular channel. Router
1808 * never receives this channel and thus is ignored.
1810 SILC_LOG_DEBUG(("Channel Key packet"));
1811 if (packet->flags & SILC_PACKET_FLAG_LIST)
1813 silc_server_channel_key(server, sock, packet);
1819 case SILC_PACKET_COMMAND:
1821 * Recived command. Processes the command request and allocates the
1822 * command context and calls the command.
1824 SILC_LOG_DEBUG(("Command packet"));
1825 if (packet->flags & SILC_PACKET_FLAG_LIST)
1827 silc_server_command_process(server, sock, packet);
1830 case SILC_PACKET_COMMAND_REPLY:
1832 * Received command reply packet. Received command reply to command. It
1833 * may be reply to command sent by us or reply to command sent by client
1834 * that we've routed further.
1836 SILC_LOG_DEBUG(("Command Reply packet"));
1837 if (packet->flags & SILC_PACKET_FLAG_LIST)
1839 silc_server_command_reply(server, sock, packet);
1843 * Private Message packets
1845 case SILC_PACKET_PRIVATE_MESSAGE:
1847 * Received private message packet. The packet is coming from either
1850 SILC_LOG_DEBUG(("Private Message packet"));
1851 if (packet->flags & SILC_PACKET_FLAG_LIST)
1853 idata->last_receive = time(NULL);
1854 silc_server_private_message(server, sock, packet);
1857 case SILC_PACKET_PRIVATE_MESSAGE_KEY:
1859 * Private message key packet.
1861 if (packet->flags & SILC_PACKET_FLAG_LIST)
1863 silc_server_private_message_key(server, sock, packet);
1867 * Key Exchange protocol packets
1869 case SILC_PACKET_KEY_EXCHANGE:
1870 SILC_LOG_DEBUG(("KE packet"));
1871 if (packet->flags & SILC_PACKET_FLAG_LIST)
1874 if (sock->protocol && sock->protocol->protocol &&
1875 sock->protocol->protocol->type == SILC_PROTOCOL_SERVER_KEY_EXCHANGE) {
1877 SilcServerKEInternalContext *proto_ctx =
1878 (SilcServerKEInternalContext *)sock->protocol->context;
1880 proto_ctx->packet = silc_packet_context_dup(packet);
1882 /* Let the protocol handle the packet */
1883 silc_protocol_execute(sock->protocol, server->schedule, 0, 100000);
1885 SILC_LOG_ERROR(("Received Key Exchange packet but no key exchange "
1886 "protocol active, packet dropped."));
1890 case SILC_PACKET_KEY_EXCHANGE_1:
1891 SILC_LOG_DEBUG(("KE 1 packet"));
1892 if (packet->flags & SILC_PACKET_FLAG_LIST)
1895 if (sock->protocol && sock->protocol->protocol &&
1896 (sock->protocol->protocol->type == SILC_PROTOCOL_SERVER_KEY_EXCHANGE ||
1897 sock->protocol->protocol->type == SILC_PROTOCOL_SERVER_REKEY)) {
1899 if (sock->protocol->protocol->type == SILC_PROTOCOL_SERVER_REKEY) {
1900 SilcServerRekeyInternalContext *proto_ctx =
1901 (SilcServerRekeyInternalContext *)sock->protocol->context;
1903 if (proto_ctx->packet)
1904 silc_packet_context_free(proto_ctx->packet);
1906 proto_ctx->packet = silc_packet_context_dup(packet);
1908 /* Let the protocol handle the packet */
1909 silc_protocol_execute(sock->protocol, server->schedule, 0, 0);
1911 SilcServerKEInternalContext *proto_ctx =
1912 (SilcServerKEInternalContext *)sock->protocol->context;
1914 if (proto_ctx->packet)
1915 silc_packet_context_free(proto_ctx->packet);
1917 proto_ctx->packet = silc_packet_context_dup(packet);
1918 proto_ctx->dest_id_type = packet->src_id_type;
1919 proto_ctx->dest_id = silc_id_str2id(packet->src_id, packet->src_id_len,
1920 packet->src_id_type);
1921 if (!proto_ctx->dest_id)
1924 /* Let the protocol handle the packet */
1925 silc_protocol_execute(sock->protocol, server->schedule,
1929 SILC_LOG_ERROR(("Received Key Exchange 1 packet but no key exchange "
1930 "protocol active, packet dropped."));
1934 case SILC_PACKET_KEY_EXCHANGE_2:
1935 SILC_LOG_DEBUG(("KE 2 packet"));
1936 if (packet->flags & SILC_PACKET_FLAG_LIST)
1939 if (sock->protocol && sock->protocol->protocol &&
1940 (sock->protocol->protocol->type == SILC_PROTOCOL_SERVER_KEY_EXCHANGE ||
1941 sock->protocol->protocol->type == SILC_PROTOCOL_SERVER_REKEY)) {
1943 if (sock->protocol->protocol->type == SILC_PROTOCOL_SERVER_REKEY) {
1944 SilcServerRekeyInternalContext *proto_ctx =
1945 (SilcServerRekeyInternalContext *)sock->protocol->context;
1947 if (proto_ctx->packet)
1948 silc_packet_context_free(proto_ctx->packet);
1950 proto_ctx->packet = silc_packet_context_dup(packet);
1952 /* Let the protocol handle the packet */
1953 silc_protocol_execute(sock->protocol, server->schedule, 0, 0);
1955 SilcServerKEInternalContext *proto_ctx =
1956 (SilcServerKEInternalContext *)sock->protocol->context;
1958 if (proto_ctx->packet)
1959 silc_packet_context_free(proto_ctx->packet);
1961 proto_ctx->packet = silc_packet_context_dup(packet);
1962 proto_ctx->dest_id_type = packet->src_id_type;
1963 proto_ctx->dest_id = silc_id_str2id(packet->src_id, packet->src_id_len,
1964 packet->src_id_type);
1965 if (!proto_ctx->dest_id)
1968 /* Let the protocol handle the packet */
1969 silc_protocol_execute(sock->protocol, server->schedule,
1973 SILC_LOG_ERROR(("Received Key Exchange 2 packet but no key exchange "
1974 "protocol active, packet dropped."));
1978 case SILC_PACKET_CONNECTION_AUTH_REQUEST:
1980 * Connection authentication request packet. When we receive this packet
1981 * we will send to the other end information about our mandatory
1982 * authentication method for the connection. This packet maybe received
1985 SILC_LOG_DEBUG(("Connection authentication request packet"));
1986 if (packet->flags & SILC_PACKET_FLAG_LIST)
1988 silc_server_connection_auth_request(server, sock, packet);
1992 * Connection Authentication protocol packets
1994 case SILC_PACKET_CONNECTION_AUTH:
1995 /* Start of the authentication protocol. We receive here the
1996 authentication data and will verify it. */
1997 SILC_LOG_DEBUG(("Connection auth packet"));
1998 if (packet->flags & SILC_PACKET_FLAG_LIST)
2001 if (sock->protocol && sock->protocol->protocol->type
2002 == SILC_PROTOCOL_SERVER_CONNECTION_AUTH) {
2004 SilcServerConnAuthInternalContext *proto_ctx =
2005 (SilcServerConnAuthInternalContext *)sock->protocol->context;
2007 proto_ctx->packet = silc_packet_context_dup(packet);
2009 /* Let the protocol handle the packet */
2010 silc_protocol_execute(sock->protocol, server->schedule, 0, 0);
2012 SILC_LOG_ERROR(("Received Connection Auth packet but no authentication "
2013 "protocol active, packet dropped."));
2017 case SILC_PACKET_NEW_ID:
2019 * Received New ID packet. This includes some new ID that has been
2020 * created. It may be for client, server or channel. This is the way
2021 * to distribute information about new registered entities in the
2024 SILC_LOG_DEBUG(("New ID packet"));
2025 if (packet->flags & SILC_PACKET_FLAG_LIST)
2026 silc_server_new_id_list(server, sock, packet);
2028 silc_server_new_id(server, sock, packet);
2031 case SILC_PACKET_NEW_CLIENT:
2033 * Received new client packet. This includes client information that
2034 * we will use to create initial client ID. After creating new
2035 * ID we will send it to the client.
2037 SILC_LOG_DEBUG(("New Client packet"));
2038 if (packet->flags & SILC_PACKET_FLAG_LIST)
2040 silc_server_new_client(server, sock, packet);
2043 case SILC_PACKET_NEW_SERVER:
2045 * Received new server packet. This includes Server ID and some other
2046 * information that we may save. This is received after server has
2049 SILC_LOG_DEBUG(("New Server packet"));
2050 if (packet->flags & SILC_PACKET_FLAG_LIST)
2052 silc_server_new_server(server, sock, packet);
2055 case SILC_PACKET_NEW_CHANNEL:
2057 * Received new channel packet. Information about new channel in the
2058 * network are distributed using this packet.
2060 SILC_LOG_DEBUG(("New Channel packet"));
2061 if (packet->flags & SILC_PACKET_FLAG_LIST)
2062 silc_server_new_channel_list(server, sock, packet);
2064 silc_server_new_channel(server, sock, packet);
2067 case SILC_PACKET_HEARTBEAT:
2069 * Received heartbeat.
2071 SILC_LOG_DEBUG(("Heartbeat packet"));
2072 if (packet->flags & SILC_PACKET_FLAG_LIST)
2076 case SILC_PACKET_KEY_AGREEMENT:
2078 * Received heartbeat.
2080 SILC_LOG_DEBUG(("Key agreement packet"));
2081 if (packet->flags & SILC_PACKET_FLAG_LIST)
2083 silc_server_key_agreement(server, sock, packet);
2086 case SILC_PACKET_REKEY:
2088 * Received re-key packet. The sender wants to regenerate the session
2091 SILC_LOG_DEBUG(("Re-key packet"));
2092 if (packet->flags & SILC_PACKET_FLAG_LIST)
2094 silc_server_rekey(server, sock, packet);
2097 case SILC_PACKET_REKEY_DONE:
2099 * The re-key is done.
2101 SILC_LOG_DEBUG(("Re-key done packet"));
2102 if (packet->flags & SILC_PACKET_FLAG_LIST)
2105 if (sock->protocol && sock->protocol->protocol &&
2106 sock->protocol->protocol->type == SILC_PROTOCOL_SERVER_REKEY) {
2108 SilcServerRekeyInternalContext *proto_ctx =
2109 (SilcServerRekeyInternalContext *)sock->protocol->context;
2111 if (proto_ctx->packet)
2112 silc_packet_context_free(proto_ctx->packet);
2114 proto_ctx->packet = silc_packet_context_dup(packet);
2116 /* Let the protocol handle the packet */
2117 silc_protocol_execute(sock->protocol, server->schedule, 0, 0);
2119 SILC_LOG_ERROR(("Received Re-key done packet but no re-key "
2120 "protocol active, packet dropped."));
2124 case SILC_PACKET_FTP:
2126 SILC_LOG_DEBUG(("FTP packet"));
2127 if (packet->flags & SILC_PACKET_FLAG_LIST)
2129 silc_server_ftp(server, sock, packet);
2132 case SILC_PACKET_RESUME_ROUTER:
2133 /* Resume router packet received. This packet is received for backup
2134 router resuming protocol. */
2135 SILC_LOG_DEBUG(("Resume router packet"));
2136 if (packet->flags & SILC_PACKET_FLAG_LIST)
2138 silc_server_backup_resume_router(server, sock, packet);
2142 SILC_LOG_ERROR(("Incorrect packet type %d, packet dropped", type));
2148 /* Creates connection to a remote router. */
2150 void silc_server_create_connection(SilcServer server,
2151 char *remote_host, uint32 port)
2153 SilcServerConnection sconn;
2155 /* Allocate connection object for hold connection specific stuff. */
2156 sconn = silc_calloc(1, sizeof(*sconn));
2157 sconn->server = server;
2158 sconn->remote_host = strdup(remote_host);
2159 sconn->remote_port = port;
2161 silc_schedule_task_add(server->schedule, 0,
2162 silc_server_connect_router,
2163 (void *)sconn, 0, 1, SILC_TASK_TIMEOUT,
2164 SILC_TASK_PRI_NORMAL);
2167 SILC_TASK_CALLBACK(silc_server_close_connection_final)
2169 silc_socket_free((SilcSocketConnection)context);
2172 /* Closes connection to socket connection */
2174 void silc_server_close_connection(SilcServer server,
2175 SilcSocketConnection sock)
2177 if (!server->sockets[sock->sock])
2180 SILC_LOG_INFO(("Closing connection %s:%d [%s]", sock->hostname,
2182 (sock->type == SILC_SOCKET_TYPE_UNKNOWN ? "Unknown" :
2183 sock->type == SILC_SOCKET_TYPE_CLIENT ? "Client" :
2184 sock->type == SILC_SOCKET_TYPE_SERVER ? "Server" :
2187 /* We won't listen for this connection anymore */
2188 silc_schedule_unset_listen_fd(server->schedule, sock->sock);
2190 /* Unregister all tasks */
2191 silc_schedule_task_del_by_fd(server->schedule, sock->sock);
2193 /* Close the actual connection */
2194 silc_net_close_connection(sock->sock);
2195 server->sockets[sock->sock] = NULL;
2197 /* If sock->user_data is NULL then we'll check for active protocols
2198 here since the silc_server_free_sock_user_data has not been called
2199 for this connection. */
2200 if (!sock->user_data) {
2201 /* If any protocol is active cancel its execution. It will call
2202 the final callback which will finalize the disconnection. */
2203 if (sock->protocol) {
2204 silc_protocol_cancel(sock->protocol, server->schedule);
2205 sock->protocol->state = SILC_PROTOCOL_STATE_ERROR;
2206 silc_protocol_execute_final(sock->protocol, server->schedule);
2207 sock->protocol = NULL;
2212 silc_schedule_task_add(server->schedule, 0,
2213 silc_server_close_connection_final,
2214 (void *)sock, 0, 1, SILC_TASK_TIMEOUT,
2215 SILC_TASK_PRI_NORMAL);
2218 /* Sends disconnect message to remote connection and disconnects the
2221 void silc_server_disconnect_remote(SilcServer server,
2222 SilcSocketConnection sock,
2223 const char *fmt, ...)
2226 unsigned char buf[4096];
2231 memset(buf, 0, sizeof(buf));
2233 vsprintf(buf, fmt, ap);
2236 SILC_LOG_DEBUG(("Disconnecting remote host"));
2238 /* Notify remote end that the conversation is over. The notify message
2239 is tried to be sent immediately. */
2240 silc_server_packet_send(server, sock, SILC_PACKET_DISCONNECT, 0,
2241 buf, strlen(buf), TRUE);
2243 /* Mark the connection to be disconnected */
2244 SILC_SET_DISCONNECTED(sock);
2245 silc_server_close_connection(server, sock);
2250 SilcClientEntry client;
2251 } *FreeClientInternal;
2253 SILC_TASK_CALLBACK(silc_server_free_client_data_timeout)
2255 FreeClientInternal i = (FreeClientInternal)context;
2257 silc_idlist_del_data(i->client);
2258 silc_idcache_purge_by_context(i->server->local_list->clients, i->client);
2262 /* Frees client data and notifies about client's signoff. */
2264 void silc_server_free_client_data(SilcServer server,
2265 SilcSocketConnection sock,
2266 SilcClientEntry client,
2270 FreeClientInternal i = silc_calloc(1, sizeof(*i));
2272 /* If there is pending outgoing data for the client then purge it
2273 to the network before removing the client entry. */
2274 silc_server_packet_queue_purge(server, sock);
2279 /* Send SIGNOFF notify to routers. */
2280 if (notify && !server->standalone && server->router)
2281 silc_server_send_notify_signoff(server, server->router->connection,
2282 server->server_type == SILC_SERVER ?
2283 FALSE : TRUE, client->id, signoff);
2285 /* Remove client from all channels */
2287 silc_server_remove_from_channels(server, NULL, client,
2288 TRUE, signoff, TRUE);
2290 silc_server_remove_from_channels(server, NULL, client,
2291 FALSE, NULL, FALSE);
2293 /* We will not delete the client entry right away. We will take it
2294 into history (for WHOWAS command) for 5 minutes */
2297 silc_schedule_task_add(server->schedule, 0,
2298 silc_server_free_client_data_timeout,
2300 SILC_TASK_TIMEOUT, SILC_TASK_PRI_LOW);
2301 client->data.status &= ~SILC_IDLIST_STATUS_REGISTERED;
2302 client->router = NULL;
2303 client->connection = NULL;
2305 /* Free the client entry and everything in it */
2306 server->stat.my_clients--;
2307 server->stat.clients--;
2308 if (server->server_type == SILC_ROUTER)
2309 server->stat.cell_clients--;
2312 /* Frees user_data pointer from socket connection object. This also sends
2313 appropriate notify packets to the network to inform about leaving
2316 void silc_server_free_sock_user_data(SilcServer server,
2317 SilcSocketConnection sock)
2319 SILC_LOG_DEBUG(("Start"));
2321 switch (sock->type) {
2322 case SILC_SOCKET_TYPE_CLIENT:
2324 SilcClientEntry user_data = (SilcClientEntry)sock->user_data;
2325 silc_server_free_client_data(server, sock, user_data, TRUE, NULL);
2328 case SILC_SOCKET_TYPE_SERVER:
2329 case SILC_SOCKET_TYPE_ROUTER:
2331 SilcServerEntry user_data = (SilcServerEntry)sock->user_data;
2332 SilcServerEntry backup_router = NULL;
2335 backup_router = silc_server_backup_get(server, user_data->id);
2337 /* If this was our primary router connection then we're lost to
2338 the outside world. */
2339 if (server->router == user_data) {
2340 /* Check whether we have a backup router connection */
2341 if (!backup_router || backup_router == user_data) {
2342 silc_schedule_task_add(server->schedule, 0,
2343 silc_server_connect_to_router,
2346 SILC_TASK_PRI_NORMAL);
2348 server->id_entry->router = NULL;
2349 server->router = NULL;
2350 server->standalone = TRUE;
2351 backup_router = NULL;
2353 SILC_LOG_INFO(("New primary router is backup router %s",
2354 backup_router->server_name));
2355 SILC_LOG_DEBUG(("New primary router is backup router %s",
2356 backup_router->server_name));
2357 server->id_entry->router = backup_router;
2358 server->router = backup_router;
2359 server->router_connect = time(0);
2360 server->backup_primary = TRUE;
2361 if (server->server_type == SILC_BACKUP_ROUTER) {
2362 server->server_type = SILC_ROUTER;
2364 /* We'll need to constantly try to reconnect to the primary
2365 router so that we'll see when it comes back online. */
2366 silc_server_backup_reconnect(server, sock->ip, sock->port,
2367 silc_server_backup_connected,
2371 /* Mark this connection as replaced */
2372 silc_server_backup_replaced_add(server, user_data->id,
2375 } else if (backup_router) {
2376 SILC_LOG_INFO(("Enabling the use of backup router %s",
2377 backup_router->server_name));
2378 SILC_LOG_DEBUG(("Enabling the use of backup router %s",
2379 backup_router->server_name));
2381 /* Mark this connection as replaced */
2382 silc_server_backup_replaced_add(server, user_data->id,
2386 if (!backup_router) {
2387 /* Free all client entries that this server owns as they will
2388 become invalid now as well. */
2390 silc_server_remove_clients_by_server(server, user_data, TRUE);
2392 /* Update the client entries of this server to the new backup
2393 router. This also removes the clients that *really* was owned
2394 by the primary router and went down with the router. */
2395 silc_server_update_clients_by_server(server, user_data, backup_router,
2397 silc_server_update_servers_by_server(server, user_data, backup_router);
2400 /* Free the server entry */
2401 silc_server_backup_del(server, user_data);
2402 silc_server_backup_replaced_del(server, user_data);
2403 silc_idlist_del_data(user_data);
2404 if (!silc_idlist_del_server(server->local_list, user_data))
2405 silc_idlist_del_server(server->global_list, user_data);
2406 server->stat.my_servers--;
2407 server->stat.servers--;
2408 if (server->server_type == SILC_ROUTER)
2409 server->stat.cell_servers--;
2411 if (backup_router) {
2412 /* Announce all of our stuff that was created about 5 minutes ago.
2413 The backup router knows all the other stuff already. */
2414 if (server->server_type == SILC_ROUTER)
2415 silc_server_announce_servers(server, FALSE, time(0) - 300,
2416 backup_router->connection);
2418 /* Announce our clients and channels to the router */
2419 silc_server_announce_clients(server, time(0) - 300,
2420 backup_router->connection);
2421 silc_server_announce_channels(server, time(0) - 300,
2422 backup_router->connection);
2428 SilcUnknownEntry user_data = (SilcUnknownEntry)sock->user_data;
2430 silc_idlist_del_data(user_data);
2431 silc_free(user_data);
2436 /* If any protocol is active cancel its execution */
2437 if (sock->protocol) {
2438 silc_protocol_cancel(sock->protocol, server->schedule);
2439 sock->protocol->state = SILC_PROTOCOL_STATE_ERROR;
2440 silc_protocol_execute_final(sock->protocol, server->schedule);
2441 sock->protocol = NULL;
2444 sock->user_data = NULL;
2447 /* Removes client from all channels it has joined. This is used when client
2448 connection is disconnected. If the client on a channel is last, the
2449 channel is removed as well. This sends the SIGNOFF notify types. */
2451 void silc_server_remove_from_channels(SilcServer server,
2452 SilcSocketConnection sock,
2453 SilcClientEntry client,
2455 char *signoff_message,
2458 SilcChannelEntry channel;
2459 SilcChannelClientEntry chl;
2460 SilcHashTableList htl;
2463 SILC_LOG_DEBUG(("Start"));
2465 if (!client || !client->id)
2468 clidp = silc_id_payload_encode(client->id, SILC_ID_CLIENT);
2470 /* Remove the client from all channels. The client is removed from
2471 the channels' user list. */
2472 silc_hash_table_list(client->channels, &htl);
2473 while (silc_hash_table_get(&htl, NULL, (void *)&chl)) {
2474 channel = chl->channel;
2476 /* Remove channel from client's channel list */
2477 silc_hash_table_del(client->channels, channel);
2479 /* Remove channel if there is no users anymore */
2480 if (server->server_type == SILC_ROUTER &&
2481 silc_hash_table_count(channel->user_list) < 2) {
2483 silc_schedule_task_del_by_context(server->schedule, channel->rekey);
2484 if (silc_idlist_del_channel(server->local_list, channel))
2485 server->stat.my_channels--;
2487 silc_idlist_del_channel(server->global_list, channel);
2491 /* Remove client from channel's client list */
2492 silc_hash_table_del(channel->user_list, chl->client);
2494 /* If there is no global users on the channel anymore mark the channel
2495 as local channel. Do not check if the removed client is local client. */
2496 if (server->server_type != SILC_ROUTER && channel->global_users &&
2497 chl->client->router && !silc_server_channel_has_global(channel))
2498 channel->global_users = FALSE;
2501 server->stat.my_chanclients--;
2503 /* If there is not at least one local user on the channel then we don't
2504 need the channel entry anymore, we can remove it safely. */
2505 if (server->server_type != SILC_ROUTER &&
2506 !silc_server_channel_has_local(channel)) {
2507 /* Notify about leaving client if this channel has global users. */
2508 if (notify && channel->global_users)
2509 silc_server_send_notify_to_channel(server, NULL, channel, FALSE,
2510 SILC_NOTIFY_TYPE_SIGNOFF,
2511 signoff_message ? 2 : 1,
2512 clidp->data, clidp->len,
2513 signoff_message, signoff_message ?
2514 strlen(signoff_message) : 0);
2517 silc_schedule_task_del_by_context(server->schedule, channel->rekey);
2519 if (channel->founder_key) {
2520 /* The founder auth data exists, do not remove the channel entry */
2521 SilcChannelClientEntry chl2;
2522 SilcHashTableList htl2;
2524 channel->disabled = TRUE;
2526 silc_hash_table_list(channel->user_list, &htl2);
2527 while (silc_hash_table_get(&htl2, NULL, (void *)&chl2)) {
2528 silc_hash_table_del(chl2->client->channels, channel);
2529 silc_hash_table_del(channel->user_list, chl2->client);
2535 /* Remove the channel entry */
2536 if (silc_idlist_del_channel(server->local_list, channel))
2537 server->stat.my_channels--;
2539 silc_idlist_del_channel(server->global_list, channel);
2543 /* Send notify to channel about client leaving SILC and thus
2544 the entire channel. */
2546 silc_server_send_notify_to_channel(server, NULL, channel, FALSE,
2547 SILC_NOTIFY_TYPE_SIGNOFF,
2548 signoff_message ? 2 : 1,
2549 clidp->data, clidp->len,
2550 signoff_message, signoff_message ?
2551 strlen(signoff_message) : 0);
2553 if (keygen && !(channel->mode & SILC_CHANNEL_MODE_PRIVKEY)) {
2554 /* Re-generate channel key */
2555 if (!silc_server_create_channel_key(server, channel, 0))
2558 /* Send the channel key to the channel. The key of course is not sent
2559 to the client who was removed from the channel. */
2560 silc_server_send_channel_key(server, client->connection, channel,
2561 server->server_type == SILC_ROUTER ?
2562 FALSE : !server->standalone);
2566 silc_buffer_free(clidp);
2569 /* Removes client from one channel. This is used for example when client
2570 calls LEAVE command to remove itself from the channel. Returns TRUE
2571 if channel still exists and FALSE if the channel is removed when
2572 last client leaves the channel. If `notify' is FALSE notify messages
2575 int silc_server_remove_from_one_channel(SilcServer server,
2576 SilcSocketConnection sock,
2577 SilcChannelEntry channel,
2578 SilcClientEntry client,
2581 SilcChannelClientEntry chl;
2584 SILC_LOG_DEBUG(("Start"));
2586 /* Get the entry to the channel, if this client is not on the channel
2588 if (!silc_hash_table_find(client->channels, channel, NULL, (void *)&chl))
2591 /* Remove the client from the channel. The client is removed from
2592 the channel's user list. */
2594 clidp = silc_id_payload_encode(client->id, SILC_ID_CLIENT);
2596 /* Remove channel from client's channel list */
2597 silc_hash_table_del(client->channels, chl->channel);
2599 /* Remove channel if there is no users anymore */
2600 if (server->server_type == SILC_ROUTER &&
2601 silc_hash_table_count(channel->user_list) < 2) {
2603 silc_schedule_task_del_by_context(server->schedule, channel->rekey);
2604 if (silc_idlist_del_channel(server->local_list, channel))
2605 server->stat.my_channels--;
2607 silc_idlist_del_channel(server->global_list, channel);
2608 silc_buffer_free(clidp);
2612 /* Remove client from channel's client list */
2613 silc_hash_table_del(channel->user_list, chl->client);
2615 /* If there is no global users on the channel anymore mark the channel
2616 as local channel. Do not check if the client is local client. */
2617 if (server->server_type != SILC_ROUTER && channel->global_users &&
2618 chl->client->router && !silc_server_channel_has_global(channel))
2619 channel->global_users = FALSE;
2622 server->stat.my_chanclients--;
2624 /* If there is not at least one local user on the channel then we don't
2625 need the channel entry anymore, we can remove it safely. */
2626 if (server->server_type != SILC_ROUTER &&
2627 !silc_server_channel_has_local(channel)) {
2628 /* Notify about leaving client if this channel has global users. */
2629 if (notify && channel->global_users)
2630 silc_server_send_notify_to_channel(server, NULL, channel, FALSE,
2631 SILC_NOTIFY_TYPE_LEAVE, 1,
2632 clidp->data, clidp->len);
2634 silc_buffer_free(clidp);
2637 silc_schedule_task_del_by_context(server->schedule, channel->rekey);
2639 if (channel->founder_key) {
2640 /* The founder auth data exists, do not remove the channel entry */
2641 SilcChannelClientEntry chl2;
2642 SilcHashTableList htl2;
2644 channel->disabled = TRUE;
2646 silc_hash_table_list(channel->user_list, &htl2);
2647 while (silc_hash_table_get(&htl2, NULL, (void *)&chl2)) {
2648 silc_hash_table_del(chl2->client->channels, channel);
2649 silc_hash_table_del(channel->user_list, chl2->client);
2655 /* Remove the channel entry */
2656 if (silc_idlist_del_channel(server->local_list, channel))
2657 server->stat.my_channels--;
2659 silc_idlist_del_channel(server->global_list, channel);
2663 /* Send notify to channel about client leaving the channel */
2665 silc_server_send_notify_to_channel(server, NULL, channel, FALSE,
2666 SILC_NOTIFY_TYPE_LEAVE, 1,
2667 clidp->data, clidp->len);
2669 silc_buffer_free(clidp);
2673 /* Timeout callback. This is called if connection is idle or for some
2674 other reason is not responding within some period of time. This
2675 disconnects the remote end. */
2677 SILC_TASK_CALLBACK(silc_server_timeout_remote)
2679 SilcServer server = (SilcServer)context;
2680 SilcSocketConnection sock = server->sockets[fd];
2682 SILC_LOG_DEBUG(("Start"));
2687 /* If we have protocol active we must assure that we call the protocol's
2688 final callback so that all the memory is freed. */
2689 if (sock->protocol) {
2690 sock->protocol->state = SILC_PROTOCOL_STATE_ERROR;
2691 silc_protocol_execute_final(sock->protocol, server->schedule);
2695 if (sock->user_data)
2696 silc_server_free_sock_user_data(server, sock);
2698 silc_server_disconnect_remote(server, sock, "Server closed connection: "
2699 "Connection timeout");
2702 /* Creates new channel. Sends NEW_CHANNEL packet to primary route. This
2703 function may be used only by router. In real SILC network all channels
2704 are created by routers thus this function is never used by normal
2707 SilcChannelEntry silc_server_create_new_channel(SilcServer server,
2708 SilcServerID *router_id,
2714 SilcChannelID *channel_id;
2715 SilcChannelEntry entry;
2719 SILC_LOG_DEBUG(("Creating new channel"));
2722 cipher = SILC_DEFAULT_CIPHER;
2724 hmac = SILC_DEFAULT_HMAC;
2726 /* Allocate cipher */
2727 if (!silc_cipher_alloc(cipher, &key))
2731 if (!silc_hmac_alloc(hmac, NULL, &newhmac)) {
2732 silc_cipher_free(key);
2736 channel_name = strdup(channel_name);
2738 /* Create the channel */
2739 if (!silc_id_create_channel_id(server, router_id, server->rng,
2741 silc_free(channel_name);
2742 silc_cipher_free(key);
2743 silc_hmac_free(newhmac);
2746 entry = silc_idlist_add_channel(server->local_list, channel_name,
2747 SILC_CHANNEL_MODE_NONE, channel_id,
2748 NULL, key, newhmac);
2750 silc_free(channel_name);
2751 silc_cipher_free(key);
2752 silc_hmac_free(newhmac);
2756 entry->cipher = strdup(cipher);
2757 entry->hmac_name = strdup(hmac);
2759 /* Now create the actual key material */
2760 if (!silc_server_create_channel_key(server, entry,
2761 silc_cipher_get_key_len(key) / 8)) {
2762 silc_free(channel_name);
2763 silc_cipher_free(key);
2764 silc_hmac_free(newhmac);
2765 silc_free(entry->cipher);
2766 silc_free(entry->hmac_name);
2770 /* Notify other routers about the new channel. We send the packet
2771 to our primary route. */
2772 if (broadcast && server->standalone == FALSE)
2773 silc_server_send_new_channel(server, server->router->connection, TRUE,
2774 channel_name, entry->id,
2775 silc_id_get_len(entry->id, SILC_ID_CHANNEL),
2778 server->stat.my_channels++;
2783 /* Same as above but creates the channel with Channel ID `channel_id. */
2786 silc_server_create_new_channel_with_id(SilcServer server,
2790 SilcChannelID *channel_id,
2793 SilcChannelEntry entry;
2797 SILC_LOG_DEBUG(("Creating new channel"));
2800 cipher = SILC_DEFAULT_CIPHER;
2802 hmac = SILC_DEFAULT_HMAC;
2804 /* Allocate cipher */
2805 if (!silc_cipher_alloc(cipher, &key))
2809 if (!silc_hmac_alloc(hmac, NULL, &newhmac)) {
2810 silc_cipher_free(key);
2814 channel_name = strdup(channel_name);
2816 /* Create the channel */
2817 entry = silc_idlist_add_channel(server->local_list, channel_name,
2818 SILC_CHANNEL_MODE_NONE, channel_id,
2819 NULL, key, newhmac);
2821 silc_free(channel_name);
2825 /* Now create the actual key material */
2826 if (!silc_server_create_channel_key(server, entry,
2827 silc_cipher_get_key_len(key) / 8)) {
2828 silc_free(channel_name);
2832 /* Notify other routers about the new channel. We send the packet
2833 to our primary route. */
2834 if (broadcast && server->standalone == FALSE)
2835 silc_server_send_new_channel(server, server->router->connection, TRUE,
2836 channel_name, entry->id,
2837 silc_id_get_len(entry->id, SILC_ID_CHANNEL),
2840 server->stat.my_channels++;
2845 /* Channel's key re-key timeout callback. */
2847 SILC_TASK_CALLBACK(silc_server_channel_key_rekey)
2849 SilcServerChannelRekey rekey = (SilcServerChannelRekey)context;
2850 SilcServer server = (SilcServer)rekey->context;
2854 if (!silc_server_create_channel_key(server, rekey->channel, rekey->key_len))
2857 silc_server_send_channel_key(server, NULL, rekey->channel, FALSE);
2860 /* Generates new channel key. This is used to create the initial channel key
2861 but also to re-generate new key for channel. If `key_len' is provided
2862 it is the bytes of the key length. */
2864 bool silc_server_create_channel_key(SilcServer server,
2865 SilcChannelEntry channel,
2869 unsigned char channel_key[32], hash[32];
2872 SILC_LOG_DEBUG(("Generating channel key"));
2874 if (channel->mode & SILC_CHANNEL_MODE_PRIVKEY) {
2875 SILC_LOG_DEBUG(("Channel has private keys, will not generate new key"));
2879 if (!channel->channel_key)
2880 if (!silc_cipher_alloc(SILC_DEFAULT_CIPHER, &channel->channel_key)) {
2881 channel->channel_key = NULL;
2887 else if (channel->key_len)
2888 len = channel->key_len / 8;
2890 len = silc_cipher_get_key_len(channel->channel_key) / 8;
2892 /* Create channel key */
2893 for (i = 0; i < len; i++) channel_key[i] = silc_rng_get_byte(server->rng);
2896 silc_cipher_set_key(channel->channel_key, channel_key, len * 8);
2898 /* Remove old key if exists */
2900 memset(channel->key, 0, channel->key_len / 8);
2901 silc_free(channel->key);
2905 channel->key_len = len * 8;
2906 channel->key = silc_calloc(len, sizeof(*channel->key));
2907 memcpy(channel->key, channel_key, len);
2908 memset(channel_key, 0, sizeof(channel_key));
2910 /* Generate HMAC key from the channel key data and set it */
2912 silc_hmac_alloc(SILC_DEFAULT_HMAC, NULL, &channel->hmac);
2913 silc_hash_make(silc_hmac_get_hash(channel->hmac), channel->key, len, hash);
2914 silc_hmac_set_key(channel->hmac, hash,
2915 silc_hash_len(silc_hmac_get_hash(channel->hmac)));
2916 memset(hash, 0, sizeof(hash));
2918 if (server->server_type == SILC_ROUTER) {
2919 if (!channel->rekey)
2920 channel->rekey = silc_calloc(1, sizeof(*channel->rekey));
2921 channel->rekey->context = (void *)server;
2922 channel->rekey->channel = channel;
2923 channel->rekey->key_len = key_len;
2924 if (channel->rekey->task)
2925 silc_schedule_task_del(server->schedule, channel->rekey->task);
2927 channel->rekey->task =
2928 silc_schedule_task_add(server->schedule, 0,
2929 silc_server_channel_key_rekey,
2930 (void *)channel->rekey, 3600, 0,
2932 SILC_TASK_PRI_NORMAL);
2938 /* Saves the channel key found in the encoded `key_payload' buffer. This
2939 function is used when we receive Channel Key Payload and also when we're
2940 processing JOIN command reply. Returns entry to the channel. */
2942 SilcChannelEntry silc_server_save_channel_key(SilcServer server,
2943 SilcBuffer key_payload,
2944 SilcChannelEntry channel)
2946 SilcChannelKeyPayload payload = NULL;
2947 SilcChannelID *id = NULL;
2948 unsigned char *tmp, hash[32];
2952 SILC_LOG_DEBUG(("Start"));
2954 /* Decode channel key payload */
2955 payload = silc_channel_key_payload_parse(key_payload);
2957 SILC_LOG_ERROR(("Bad channel key payload, dropped"));
2962 /* Get the channel entry */
2965 /* Get channel ID */
2966 tmp = silc_channel_key_get_id(payload, &tmp_len);
2967 id = silc_id_str2id(tmp, tmp_len, SILC_ID_CHANNEL);
2973 channel = silc_idlist_find_channel_by_id(server->local_list, id, NULL);
2975 channel = silc_idlist_find_channel_by_id(server->global_list, id, NULL);
2977 SILC_LOG_ERROR(("Received key for non-existent channel"));
2983 tmp = silc_channel_key_get_key(payload, &tmp_len);
2989 cipher = silc_channel_key_get_cipher(payload, NULL);
2995 /* Remove old key if exists */
2997 memset(channel->key, 0, channel->key_len / 8);
2998 silc_free(channel->key);
2999 silc_cipher_free(channel->channel_key);
3002 /* Create new cipher */
3003 if (!silc_cipher_alloc(cipher, &channel->channel_key)) {
3004 channel->channel_key = NULL;
3009 if (channel->cipher)
3010 silc_free(channel->cipher);
3011 channel->cipher = strdup(cipher);
3014 channel->key_len = tmp_len * 8;
3015 channel->key = silc_calloc(tmp_len, sizeof(unsigned char));
3016 memcpy(channel->key, tmp, tmp_len);
3017 silc_cipher_set_key(channel->channel_key, tmp, channel->key_len);
3019 /* Generate HMAC key from the channel key data and set it */
3021 silc_hmac_alloc(SILC_DEFAULT_HMAC, NULL, &channel->hmac);
3022 silc_hash_make(silc_hmac_get_hash(channel->hmac), tmp, tmp_len, hash);
3023 silc_hmac_set_key(channel->hmac, hash,
3024 silc_hash_len(silc_hmac_get_hash(channel->hmac)));
3026 memset(hash, 0, sizeof(hash));
3027 memset(tmp, 0, tmp_len);
3029 if (server->server_type == SILC_ROUTER) {
3030 if (!channel->rekey)
3031 channel->rekey = silc_calloc(1, sizeof(*channel->rekey));
3032 channel->rekey->context = (void *)server;
3033 channel->rekey->channel = channel;
3034 if (channel->rekey->task)
3035 silc_schedule_task_del(server->schedule, channel->rekey->task);
3037 channel->rekey->task =
3038 silc_schedule_task_add(server->schedule, 0,
3039 silc_server_channel_key_rekey,
3040 (void *)channel->rekey, 3600, 0,
3042 SILC_TASK_PRI_NORMAL);
3048 silc_channel_key_payload_free(payload);
3053 /* Heartbeat callback. This function is set as argument for the
3054 silc_socket_set_heartbeat function. The library will call this function
3055 at the set time interval. */
3057 void silc_server_perform_heartbeat(SilcSocketConnection sock,
3060 SilcServerHBContext hb = (SilcServerHBContext)hb_context;
3062 SILC_LOG_DEBUG(("Sending heartbeat to %s (%s)", sock->hostname,
3065 /* Send the heartbeat */
3066 silc_server_send_heartbeat(hb->server, sock);
3069 /* Returns assembled of all servers in the given ID list. The packet's
3070 form is dictated by the New ID payload. */
3072 static void silc_server_announce_get_servers(SilcServer server,
3073 SilcServerEntry remote,
3075 SilcBuffer *servers,
3076 unsigned long creation_time)
3078 SilcIDCacheList list;
3079 SilcIDCacheEntry id_cache;
3080 SilcServerEntry entry;
3083 /* Go through all clients in the list */
3084 if (silc_idcache_get_all(id_list->servers, &list)) {
3085 if (silc_idcache_list_first(list, &id_cache)) {
3087 entry = (SilcServerEntry)id_cache->context;
3089 /* Do not announce the one we've sending our announcements and
3090 do not announce ourself. Also check the creation time if it's
3092 if ((entry == remote) || (entry == server->id_entry) ||
3093 (creation_time && entry->data.created < creation_time)) {
3094 if (!silc_idcache_list_next(list, &id_cache))
3099 idp = silc_id_payload_encode(entry->id, SILC_ID_SERVER);
3101 *servers = silc_buffer_realloc(*servers,
3103 (*servers)->truelen + idp->len :
3105 silc_buffer_pull_tail(*servers, ((*servers)->end - (*servers)->data));
3106 silc_buffer_put(*servers, idp->data, idp->len);
3107 silc_buffer_pull(*servers, idp->len);
3108 silc_buffer_free(idp);
3110 if (!silc_idcache_list_next(list, &id_cache))
3115 silc_idcache_list_free(list);
3119 /* This function is used by router to announce existing servers to our
3120 primary router when we've connected to it. If `creation_time' is non-zero
3121 then only the servers that has been created after the `creation_time'
3122 will be announced. */
3124 void silc_server_announce_servers(SilcServer server, bool global,
3125 unsigned long creation_time,
3126 SilcSocketConnection remote)
3128 SilcBuffer servers = NULL;
3130 SILC_LOG_DEBUG(("Announcing servers"));
3132 /* Get servers in local list */
3133 silc_server_announce_get_servers(server, remote->user_data,
3134 server->local_list, &servers,
3138 /* Get servers in global list */
3139 silc_server_announce_get_servers(server, remote->user_data,
3140 server->global_list, &servers,
3144 silc_buffer_push(servers, servers->data - servers->head);
3145 SILC_LOG_HEXDUMP(("servers"), servers->data, servers->len);
3147 /* Send the packet */
3148 silc_server_packet_send(server, remote,
3149 SILC_PACKET_NEW_ID, SILC_PACKET_FLAG_LIST,
3150 servers->data, servers->len, TRUE);
3152 silc_buffer_free(servers);
3156 /* Returns assembled packet of all clients in the given ID list. The
3157 packet's form is dictated by the New ID Payload. */
3159 static void silc_server_announce_get_clients(SilcServer server,
3161 SilcBuffer *clients,
3162 unsigned long creation_time)
3164 SilcIDCacheList list;
3165 SilcIDCacheEntry id_cache;
3166 SilcClientEntry client;
3169 /* Go through all clients in the list */
3170 if (silc_idcache_get_all(id_list->clients, &list)) {
3171 if (silc_idcache_list_first(list, &id_cache)) {
3173 client = (SilcClientEntry)id_cache->context;
3175 if (creation_time && client->data.created < creation_time) {
3176 if (!silc_idcache_list_next(list, &id_cache))
3181 idp = silc_id_payload_encode(client->id, SILC_ID_CLIENT);
3183 *clients = silc_buffer_realloc(*clients,
3185 (*clients)->truelen + idp->len :
3187 silc_buffer_pull_tail(*clients, ((*clients)->end - (*clients)->data));
3188 silc_buffer_put(*clients, idp->data, idp->len);
3189 silc_buffer_pull(*clients, idp->len);
3190 silc_buffer_free(idp);
3192 if (!silc_idcache_list_next(list, &id_cache))
3197 silc_idcache_list_free(list);
3201 /* This function is used to announce our existing clients to our router
3202 when we've connected to it. If `creation_time' is non-zero then only
3203 the clients that has been created after the `creation_time' will be
3206 void silc_server_announce_clients(SilcServer server,
3207 unsigned long creation_time,
3208 SilcSocketConnection remote)
3210 SilcBuffer clients = NULL;
3212 SILC_LOG_DEBUG(("Announcing clients"));
3214 /* Get clients in local list */
3215 silc_server_announce_get_clients(server, server->local_list,
3216 &clients, creation_time);
3218 /* As router we announce our global list as well */
3219 if (server->server_type == SILC_ROUTER)
3220 silc_server_announce_get_clients(server, server->global_list,
3221 &clients, creation_time);
3224 silc_buffer_push(clients, clients->data - clients->head);
3225 SILC_LOG_HEXDUMP(("clients"), clients->data, clients->len);
3227 /* Send the packet */
3228 silc_server_packet_send(server, remote,
3229 SILC_PACKET_NEW_ID, SILC_PACKET_FLAG_LIST,
3230 clients->data, clients->len, TRUE);
3232 silc_buffer_free(clients);
3237 silc_server_announce_encode_notify(SilcNotifyType notify, uint32 argc, ...)
3243 p = silc_notify_payload_encode(notify, argc, ap);
3249 /* Returns assembled packets for channel users of the `channel'. */
3251 void silc_server_announce_get_channel_users(SilcServer server,
3252 SilcChannelEntry channel,
3253 SilcBuffer *channel_users,
3254 SilcBuffer *channel_users_modes)
3256 SilcChannelClientEntry chl;
3257 SilcHashTableList htl;
3258 SilcBuffer chidp, clidp;
3261 unsigned char mode[4];
3263 SILC_LOG_DEBUG(("Start"));
3265 /* Now find all users on the channel */
3266 chidp = silc_id_payload_encode(channel->id, SILC_ID_CHANNEL);
3267 silc_hash_table_list(channel->user_list, &htl);
3268 while (silc_hash_table_get(&htl, NULL, (void *)&chl)) {
3269 clidp = silc_id_payload_encode(chl->client->id, SILC_ID_CLIENT);
3272 tmp = silc_server_announce_encode_notify(SILC_NOTIFY_TYPE_JOIN, 2,
3273 clidp->data, clidp->len,
3274 chidp->data, chidp->len);
3277 silc_buffer_realloc(*channel_users,
3279 (*channel_users)->truelen + len : len));
3280 silc_buffer_pull_tail(*channel_users,
3281 ((*channel_users)->end -
3282 (*channel_users)->data));
3284 silc_buffer_put(*channel_users, tmp->data, tmp->len);
3285 silc_buffer_pull(*channel_users, len);
3286 silc_buffer_free(tmp);
3288 /* CUMODE notify for mode change on the channel */
3289 SILC_PUT32_MSB(chl->mode, mode);
3290 tmp = silc_server_announce_encode_notify(SILC_NOTIFY_TYPE_CUMODE_CHANGE,
3291 3, clidp->data, clidp->len,
3293 clidp->data, clidp->len);
3295 *channel_users_modes =
3296 silc_buffer_realloc(*channel_users_modes,
3297 (*channel_users_modes ?
3298 (*channel_users_modes)->truelen + len : len));
3299 silc_buffer_pull_tail(*channel_users_modes,
3300 ((*channel_users_modes)->end -
3301 (*channel_users_modes)->data));
3303 silc_buffer_put(*channel_users_modes, tmp->data, tmp->len);
3304 silc_buffer_pull(*channel_users_modes, len);
3305 silc_buffer_free(tmp);
3307 silc_buffer_free(clidp);
3309 silc_buffer_free(chidp);
3312 /* Returns assembled packets for all channels and users on those channels
3313 from the given ID List. The packets are in the form dictated by the
3314 New Channel and New Channel User payloads. */
3316 void silc_server_announce_get_channels(SilcServer server,
3318 SilcBuffer *channels,
3319 SilcBuffer *channel_users,
3320 SilcBuffer **channel_users_modes,
3321 uint32 *channel_users_modes_c,
3322 SilcChannelID ***channel_ids,
3323 unsigned long creation_time)
3325 SilcIDCacheList list;
3326 SilcIDCacheEntry id_cache;
3327 SilcChannelEntry channel;
3332 int i = *channel_users_modes_c;
3335 SILC_LOG_DEBUG(("Start"));
3337 /* Go through all channels in the list */
3338 if (silc_idcache_get_all(id_list->channels, &list)) {
3339 if (silc_idcache_list_first(list, &id_cache)) {
3341 channel = (SilcChannelEntry)id_cache->context;
3343 if (creation_time && channel->created < creation_time)
3348 cid = silc_id_id2str(channel->id, SILC_ID_CHANNEL);
3349 id_len = silc_id_get_len(channel->id, SILC_ID_CHANNEL);
3350 name_len = strlen(channel->channel_name);
3353 len = 4 + name_len + id_len + 4;
3355 silc_buffer_realloc(*channels,
3356 (*channels ? (*channels)->truelen +
3358 silc_buffer_pull_tail(*channels,
3359 ((*channels)->end - (*channels)->data));
3360 silc_buffer_format(*channels,
3361 SILC_STR_UI_SHORT(name_len),
3362 SILC_STR_UI_XNSTRING(channel->channel_name,
3364 SILC_STR_UI_SHORT(id_len),
3365 SILC_STR_UI_XNSTRING(cid, id_len),
3366 SILC_STR_UI_INT(channel->mode),
3368 silc_buffer_pull(*channels, len);
3371 *channel_users_modes = silc_realloc(*channel_users_modes,
3372 sizeof(**channel_users_modes) *
3374 (*channel_users_modes)[i] = NULL;
3375 *channel_ids = silc_realloc(*channel_ids,
3376 sizeof(**channel_ids) * (i + 1));
3377 (*channel_ids)[i] = NULL;
3378 silc_server_announce_get_channel_users(server, channel,
3380 channel_users_modes[i]);
3381 (*channel_ids)[i] = channel->id;
3384 if (!silc_idcache_list_next(list, &id_cache))
3388 *channel_users_modes_c += i;
3391 silc_idcache_list_free(list);
3395 /* This function is used to announce our existing channels to our router
3396 when we've connected to it. This also announces the users on the
3397 channels to the router. If the `creation_time' is non-zero only the
3398 channels that was created after the `creation_time' are announced.
3399 Note that the channel users are still announced even if the `creation_time'
3402 void silc_server_announce_channels(SilcServer server,
3403 unsigned long creation_time,
3404 SilcSocketConnection remote)
3406 SilcBuffer channels = NULL, channel_users = NULL;
3407 SilcBuffer *channel_users_modes = NULL;
3408 uint32 channel_users_modes_c = 0;
3409 SilcChannelID **channel_ids = NULL;
3411 SILC_LOG_DEBUG(("Announcing channels and channel users"));
3413 /* Get channels and channel users in local list */
3414 silc_server_announce_get_channels(server, server->local_list,
3415 &channels, &channel_users,
3416 &channel_users_modes,
3417 &channel_users_modes_c,
3418 &channel_ids, creation_time);
3420 /* Get channels and channel users in global list */
3421 silc_server_announce_get_channels(server, server->global_list,
3422 &channels, &channel_users,
3423 &channel_users_modes,
3424 &channel_users_modes_c,
3425 &channel_ids, creation_time);
3428 silc_buffer_push(channels, channels->data - channels->head);
3429 SILC_LOG_HEXDUMP(("channels"), channels->data, channels->len);
3431 /* Send the packet */
3432 silc_server_packet_send(server, remote,
3433 SILC_PACKET_NEW_CHANNEL, SILC_PACKET_FLAG_LIST,
3434 channels->data, channels->len,
3437 silc_buffer_free(channels);
3440 if (channel_users) {
3441 silc_buffer_push(channel_users, channel_users->data - channel_users->head);
3442 SILC_LOG_HEXDUMP(("channel users"), channel_users->data,
3443 channel_users->len);
3445 /* Send the packet */
3446 silc_server_packet_send(server, remote,
3447 SILC_PACKET_NOTIFY, SILC_PACKET_FLAG_LIST,
3448 channel_users->data, channel_users->len,
3451 silc_buffer_free(channel_users);
3454 if (channel_users_modes) {
3457 for (i = 0; i < channel_users_modes_c; i++) {
3458 silc_buffer_push(channel_users_modes[i],
3459 channel_users_modes[i]->data -
3460 channel_users_modes[i]->head);
3461 SILC_LOG_HEXDUMP(("channel users modes"), channel_users_modes[i]->data,
3462 channel_users_modes[i]->len);
3463 silc_server_packet_send_dest(server, remote,
3464 SILC_PACKET_NOTIFY, SILC_PACKET_FLAG_LIST,
3465 channel_ids[i], SILC_ID_CHANNEL,
3466 channel_users_modes[i]->data,
3467 channel_users_modes[i]->len,
3469 silc_buffer_free(channel_users_modes[i]);
3471 silc_free(channel_users_modes);
3472 silc_free(channel_ids);
3476 /* Failure timeout callback. If this is called then we will immediately
3477 process the received failure. We always process the failure with timeout
3478 since we do not want to blindly trust to received failure packets.
3479 This won't be called (the timeout is cancelled) if the failure was
3480 bogus (it is bogus if remote does not close the connection after sending
3483 SILC_TASK_CALLBACK(silc_server_failure_callback)
3485 SilcServerFailureContext f = (SilcServerFailureContext)context;
3487 if (f->sock->protocol) {
3488 f->sock->protocol->state = SILC_PROTOCOL_STATE_FAILURE;
3489 silc_protocol_execute(f->sock->protocol, f->server->schedule, 0, 0);
3495 /* Assembles user list and users mode list from the `channel'. */
3497 void silc_server_get_users_on_channel(SilcServer server,
3498 SilcChannelEntry channel,
3499 SilcBuffer *user_list,
3500 SilcBuffer *mode_list,
3503 SilcChannelClientEntry chl;
3504 SilcHashTableList htl;
3505 SilcBuffer client_id_list;
3506 SilcBuffer client_mode_list;
3508 uint32 list_count = 0, len = 0;
3510 silc_hash_table_list(channel->user_list, &htl);
3511 while (silc_hash_table_get(&htl, NULL, (void *)&chl))
3512 len += (silc_id_get_len(chl->client->id, SILC_ID_CLIENT) + 4);
3514 client_id_list = silc_buffer_alloc(len);
3516 silc_buffer_alloc(4 * silc_hash_table_count(channel->user_list));
3517 silc_buffer_pull_tail(client_id_list, SILC_BUFFER_END(client_id_list));
3518 silc_buffer_pull_tail(client_mode_list, SILC_BUFFER_END(client_mode_list));
3520 silc_hash_table_list(channel->user_list, &htl);
3521 while (silc_hash_table_get(&htl, NULL, (void *)&chl)) {
3523 idp = silc_id_payload_encode(chl->client->id, SILC_ID_CLIENT);
3524 silc_buffer_put(client_id_list, idp->data, idp->len);
3525 silc_buffer_pull(client_id_list, idp->len);
3526 silc_buffer_free(idp);
3528 /* Client's mode on channel */
3529 SILC_PUT32_MSB(chl->mode, client_mode_list->data);
3530 silc_buffer_pull(client_mode_list, 4);
3534 silc_buffer_push(client_id_list,
3535 client_id_list->data - client_id_list->head);
3536 silc_buffer_push(client_mode_list,
3537 client_mode_list->data - client_mode_list->head);
3539 *user_list = client_id_list;
3540 *mode_list = client_mode_list;
3541 *user_count = list_count;
3544 /* Saves users and their modes to the `channel'. */
3546 void silc_server_save_users_on_channel(SilcServer server,
3547 SilcSocketConnection sock,
3548 SilcChannelEntry channel,
3549 SilcClientID *noadd,
3550 SilcBuffer user_list,
3551 SilcBuffer mode_list,
3556 for (i = 0; i < user_count; i++) {
3559 SilcClientID *client_id;
3560 SilcClientEntry client;
3563 SILC_GET16_MSB(idp_len, user_list->data + 2);
3565 client_id = silc_id_payload_parse_id(user_list->data, idp_len);
3566 silc_buffer_pull(user_list, idp_len);
3571 SILC_GET32_MSB(mode, mode_list->data);
3572 silc_buffer_pull(mode_list, 4);
3574 if (noadd && SILC_ID_CLIENT_COMPARE(client_id, noadd)) {
3575 silc_free(client_id);
3579 /* Check if we have this client cached already. */
3580 client = silc_idlist_find_client_by_id(server->local_list, client_id,
3581 server->server_type, NULL);
3583 client = silc_idlist_find_client_by_id(server->global_list,
3584 client_id, server->server_type,
3587 /* If router did not find such Client ID in its lists then this must
3588 be bogus client or some router in the net is buggy. */
3589 if (server->server_type == SILC_ROUTER) {
3590 silc_free(client_id);
3594 /* We don't have that client anywhere, add it. The client is added
3595 to global list since server didn't have it in the lists so it must be
3597 client = silc_idlist_add_client(server->global_list, NULL, NULL, NULL,
3598 silc_id_dup(client_id, SILC_ID_CLIENT),
3599 sock->user_data, NULL);
3601 SILC_LOG_ERROR(("Could not add new client to the ID Cache"));
3602 silc_free(client_id);
3606 client->data.status |= SILC_IDLIST_STATUS_REGISTERED;
3609 silc_free(client_id);
3611 if (!silc_server_client_on_channel(client, channel)) {
3612 /* Client was not on the channel, add it. */
3613 SilcChannelClientEntry chl = silc_calloc(1, sizeof(*chl));
3614 chl->client = client;
3616 chl->channel = channel;
3617 silc_hash_table_add(channel->user_list, chl->client, chl);
3618 silc_hash_table_add(client->channels, chl->channel, chl);
3623 /* Lookups route to the client indicated by the `id_data'. The connection
3624 object and internal data object is returned. Returns NULL if route
3625 could not be found to the client. If the `client_id' is specified then
3626 it is used and the `id_data' is ignored. */
3628 SilcSocketConnection silc_server_get_client_route(SilcServer server,
3629 unsigned char *id_data,
3631 SilcClientID *client_id,
3632 SilcIDListData *idata)
3635 SilcClientEntry client;
3637 SILC_LOG_DEBUG(("Start"));
3639 /* Decode destination Client ID */
3641 id = silc_id_str2id(id_data, id_len, SILC_ID_CLIENT);
3643 SILC_LOG_ERROR(("Could not decode destination Client ID, dropped"));
3647 id = silc_id_dup(client_id, SILC_ID_CLIENT);
3650 /* If the destination belongs to our server we don't have to route
3651 the packet anywhere but to send it to the local destination. */
3652 client = silc_idlist_find_client_by_id(server->local_list, id, TRUE, NULL);
3656 /* If we are router and the client has router then the client is in
3657 our cell but not directly connected to us. */
3658 if (server->server_type == SILC_ROUTER && client->router) {
3659 /* We are of course in this case the client's router thus the route
3660 to the client is the server who owns the client. So, we will send
3661 the packet to that server. */
3663 *idata = (SilcIDListData)client->router;
3664 return client->router->connection;
3667 /* Seems that client really is directly connected to us */
3669 *idata = (SilcIDListData)client;
3670 return client->connection;
3673 /* Destination belongs to someone not in this server. If we are normal
3674 server our action is to send the packet to our router. */
3675 if (server->server_type != SILC_ROUTER && !server->standalone) {
3678 *idata = (SilcIDListData)server->router;
3679 return server->router->connection;
3682 /* We are router and we will perform route lookup for the destination
3683 and send the packet to fastest route. */
3684 if (server->server_type == SILC_ROUTER && !server->standalone) {
3685 /* Check first that the ID is valid */
3686 client = silc_idlist_find_client_by_id(server->global_list, id,
3689 SilcSocketConnection dst_sock;
3691 dst_sock = silc_server_route_get(server, id, SILC_ID_CLIENT);
3695 *idata = (SilcIDListData)dst_sock->user_data;
3704 /* Encodes and returns channel list of channels the `client' has joined.
3705 Secret channels are not put to the list. */
3707 SilcBuffer silc_server_get_client_channel_list(SilcServer server,
3708 SilcClientEntry client)
3710 SilcBuffer buffer = NULL;
3711 SilcChannelEntry channel;
3712 SilcChannelClientEntry chl;
3713 SilcHashTableList htl;
3719 silc_hash_table_list(client->channels, &htl);
3720 while (silc_hash_table_get(&htl, NULL, (void *)&chl)) {
3721 channel = chl->channel;
3723 if (channel->mode & SILC_CHANNEL_MODE_SECRET ||
3724 channel->mode & SILC_CHANNEL_MODE_PRIVATE)
3727 cid = silc_id_id2str(channel->id, SILC_ID_CHANNEL);
3728 id_len = silc_id_get_len(channel->id, SILC_ID_CHANNEL);
3729 name_len = strlen(channel->channel_name);
3731 len = 4 + name_len + id_len + 4;
3732 buffer = silc_buffer_realloc(buffer,
3733 (buffer ? (buffer)->truelen + len : len));
3734 silc_buffer_pull_tail(buffer, ((buffer)->end - (buffer)->data));
3735 silc_buffer_format(buffer,
3736 SILC_STR_UI_SHORT(name_len),
3737 SILC_STR_UI_XNSTRING(channel->channel_name,
3739 SILC_STR_UI_SHORT(id_len),
3740 SILC_STR_UI_XNSTRING(cid, id_len),
3741 SILC_STR_UI_INT(chl->mode), /* Client's mode */
3743 silc_buffer_pull(buffer, len);
3748 silc_buffer_push(buffer, buffer->data - buffer->head);
3753 /* Finds client entry by Client ID and if it is not found then resolves
3754 it using WHOIS command. */
3756 SilcClientEntry silc_server_get_client_resolve(SilcServer server,
3757 SilcClientID *client_id)
3759 SilcClientEntry client;
3761 client = silc_idlist_find_client_by_id(server->local_list, client_id,
3764 client = silc_idlist_find_client_by_id(server->global_list,
3765 client_id, TRUE, NULL);
3766 if (!client && server->server_type == SILC_ROUTER)
3770 if (!client && server->standalone)
3773 if (!client || !client->nickname || !client->username) {
3774 SilcBuffer buffer, idp;
3776 client->data.status |= SILC_IDLIST_STATUS_RESOLVING;
3777 client->data.status &= ~SILC_IDLIST_STATUS_RESOLVED;
3778 client->resolve_cmd_ident = ++server->cmd_ident;
3780 idp = silc_id_payload_encode(client_id, SILC_ID_CLIENT);
3781 buffer = silc_command_payload_encode_va(SILC_COMMAND_WHOIS,
3782 server->cmd_ident, 1,
3783 3, idp->data, idp->len);
3784 silc_server_packet_send(server, client ? client->router->connection :
3785 server->router->connection,
3786 SILC_PACKET_COMMAND, 0,
3787 buffer->data, buffer->len, FALSE);
3788 silc_buffer_free(idp);
3789 silc_buffer_free(buffer);
3796 /* A timeout callback for the re-key. We will be the initiator of the
3799 SILC_TASK_CALLBACK(silc_server_rekey_callback)
3801 SilcSocketConnection sock = (SilcSocketConnection)context;
3802 SilcIDListData idata = (SilcIDListData)sock->user_data;
3803 SilcServer server = (SilcServer)idata->rekey->context;
3804 SilcProtocol protocol;
3805 SilcServerRekeyInternalContext *proto_ctx;
3807 SILC_LOG_DEBUG(("Start"));
3809 /* Allocate internal protocol context. This is sent as context
3811 proto_ctx = silc_calloc(1, sizeof(*proto_ctx));
3812 proto_ctx->server = (void *)server;
3813 proto_ctx->sock = sock;
3814 proto_ctx->responder = FALSE;
3815 proto_ctx->pfs = idata->rekey->pfs;
3817 /* Perform rekey protocol. Will call the final callback after the
3818 protocol is over. */
3819 silc_protocol_alloc(SILC_PROTOCOL_SERVER_REKEY,
3820 &protocol, proto_ctx, silc_server_rekey_final);
3821 sock->protocol = protocol;
3823 /* Run the protocol */
3824 silc_protocol_execute(protocol, server->schedule, 0, 0);
3826 /* Re-register re-key timeout */
3827 silc_schedule_task_add(server->schedule, sock->sock,
3828 silc_server_rekey_callback,
3829 context, idata->rekey->timeout, 0,
3830 SILC_TASK_TIMEOUT, SILC_TASK_PRI_NORMAL);
3833 /* The final callback for the REKEY protocol. This will actually take the
3834 new key material into use. */
3836 SILC_TASK_CALLBACK_GLOBAL(silc_server_rekey_final)
3838 SilcProtocol protocol = (SilcProtocol)context;
3839 SilcServerRekeyInternalContext *ctx =
3840 (SilcServerRekeyInternalContext *)protocol->context;
3841 SilcServer server = (SilcServer)ctx->server;
3842 SilcSocketConnection sock = ctx->sock;
3844 SILC_LOG_DEBUG(("Start"));
3846 if (protocol->state == SILC_PROTOCOL_STATE_ERROR ||
3847 protocol->state == SILC_PROTOCOL_STATE_FAILURE) {
3848 /* Error occured during protocol */
3849 SILC_LOG_ERROR(("Error occurred during rekey protocol"));
3850 silc_protocol_cancel(protocol, server->schedule);
3851 silc_protocol_free(protocol);
3852 sock->protocol = NULL;
3854 silc_packet_context_free(ctx->packet);
3856 silc_ske_free(ctx->ske);
3861 /* Purge the outgoing data queue to assure that all rekey packets really
3862 go to the network before we quit the protocol. */
3863 silc_server_packet_queue_purge(server, sock);
3866 silc_protocol_free(protocol);
3867 sock->protocol = NULL;
3869 silc_packet_context_free(ctx->packet);
3871 silc_ske_free(ctx->ske);