5 Author: Pekka Riikonen <priikone@silcnet.org>
7 Copyright (C) 1997 - 2002 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; version 2 of the License.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
21 #include "serverincludes.h"
22 #include "server_internal.h"
24 extern char *server_version;
26 /* Removes the client from channels and possibly removes the channels
27 as well. After removing those channels that exist, their channel
28 keys are regnerated. This is called only by the function
29 silc_server_remove_clients_by_server. */
32 silc_server_remove_clients_channels(SilcServer server,
33 SilcServerEntry server_entry,
34 SilcHashTable clients,
35 SilcClientEntry client,
36 SilcHashTable channels)
38 SilcChannelEntry channel;
39 SilcChannelClientEntry chl, chl2;
40 SilcHashTableList htl, htl2;
45 SILC_LOG_DEBUG(("Remove client from all channels"));
47 if (silc_hash_table_find(clients, client, NULL, NULL))
48 silc_hash_table_del(clients, client);
50 /* Remove the client from all channels. The client is removed from
51 the channels' user list. */
52 silc_hash_table_list(client->channels, &htl);
53 while (silc_hash_table_get(&htl, NULL, (void **)&chl)) {
54 channel = chl->channel;
56 /* Remove channel if this is last client leaving the channel, unless
57 the channel is permanent. */
58 if (server->server_type != SILC_SERVER &&
59 silc_hash_table_count(channel->user_list) < 2) {
60 if (silc_hash_table_find(channels, channel, NULL, NULL))
61 silc_hash_table_del(channels, channel);
62 silc_schedule_task_del_by_context(server->schedule, channel->rekey);
63 silc_server_channel_delete(server, channel);
67 silc_hash_table_del(client->channels, channel);
68 silc_hash_table_del(channel->user_list, chl->client);
69 channel->user_count--;
71 /* If there is no global users on the channel anymore mark the channel
72 as local channel. Do not check if the removed client is local client. */
73 if (server->server_type != SILC_ROUTER && channel->global_users &&
74 chl->client->router && !silc_server_channel_has_global(channel))
75 channel->global_users = FALSE;
79 /* Update statistics */
80 if (SILC_IS_LOCAL(client))
81 server->stat.my_chanclients--;
82 if (server->server_type == SILC_ROUTER) {
83 server->stat.cell_chanclients--;
84 server->stat.chanclients--;
87 /* If there is not at least one local user on the channel then we don't
88 need the channel entry anymore, we can remove it safely, unless the
89 channel is permanent channel */
90 if (server->server_type == SILC_SERVER &&
91 !silc_server_channel_has_local(channel)) {
92 if (silc_hash_table_find(channels, channel, NULL, NULL))
93 silc_hash_table_del(channels, channel);
94 silc_schedule_task_del_by_context(server->schedule, channel->rekey);
95 silc_server_channel_delete(server, channel);
99 /* Mark other local clients to the table of clients whom will receive
100 the SERVER_SIGNOFF notify. */
101 silc_hash_table_list(channel->user_list, &htl2);
102 while (silc_hash_table_get(&htl2, NULL, (void **)&chl2)) {
103 SilcClientEntry c = chl2->client;
107 /* Add client to table, if it's not from the signoff server */
108 if (c->router != server_entry &&
109 !silc_hash_table_find(clients, c, NULL, NULL))
110 silc_hash_table_add(clients, c, c);
112 silc_hash_table_list_reset(&htl2);
114 /* Add the channel to the the channels list to regenerate the
116 if (!silc_hash_table_find(channels, channel, NULL, NULL))
117 silc_hash_table_add(channels, channel, channel);
119 silc_hash_table_list_reset(&htl);
122 /* This function removes all client entries that are originated from
123 `router' and are owned by `entry'. `router' and `entry' can be same
124 too. If `server_signoff' is TRUE then SERVER_SIGNOFF notify is
125 distributed to our local clients. */
127 bool silc_server_remove_clients_by_server(SilcServer server,
128 SilcServerEntry router,
129 SilcServerEntry entry,
132 SilcIDCacheList list = NULL;
133 SilcIDCacheEntry id_cache = NULL;
134 SilcClientEntry client = NULL;
136 unsigned char **argv = NULL;
137 SilcUInt32 *argv_lens = NULL, *argv_types = NULL, argc = 0;
138 SilcHashTableList htl;
139 SilcChannelEntry channel;
140 SilcHashTable channels, clients;
143 if (!(entry->data.status & SILC_IDLIST_STATUS_REGISTERED))
146 SILC_LOG_DEBUG(("Removing clients by %s",
147 entry->server_name ? entry->server_name : "server"));
152 /* Allocate the hash table that holds the channels that require
153 channel key re-generation after we've removed this server's clients
154 from the channels. */
155 channels = silc_hash_table_alloc(0, silc_hash_ptr, NULL, NULL, NULL,
157 clients = silc_hash_table_alloc(0, silc_hash_ptr, NULL, NULL, NULL,
160 if (server_signoff) {
161 idp = silc_id_payload_encode(entry->id, SILC_ID_SERVER);
162 argv = silc_realloc(argv, sizeof(*argv) * (argc + 1));
163 argv_lens = silc_realloc(argv_lens, sizeof(*argv_lens) * (argc + 1));
164 argv_types = silc_realloc(argv_types, sizeof(*argv_types) * (argc + 1));
165 argv[argc] = silc_calloc(idp->len, sizeof(*argv[0]));
166 memcpy(argv[argc], idp->data, idp->len);
167 argv_lens[argc] = idp->len;
168 argv_types[argc] = argc + 1;
170 silc_buffer_free(idp);
173 if (silc_idcache_get_all(server->local_list->clients, &list)) {
174 if (silc_idcache_list_first(list, &id_cache)) {
176 client = (SilcClientEntry)id_cache->context;
178 /* If client is not registered, is not originated from `router'
179 or is not owned by `entry', skip it. */
180 if (!(client->data.status & SILC_IDLIST_STATUS_REGISTERED) ||
181 client->router != router ||
182 (router != entry && !SILC_ID_COMPARE(client->id, entry->id,
183 client->id->ip.data_len))) {
184 if (!silc_idcache_list_next(list, &id_cache))
190 if (server_signoff) {
191 idp = silc_id_payload_encode(client->id, SILC_ID_CLIENT);
192 argv = silc_realloc(argv, sizeof(*argv) * (argc + 1));
193 argv_lens = silc_realloc(argv_lens, sizeof(*argv_lens) *
195 argv_types = silc_realloc(argv_types, sizeof(*argv_types) *
197 argv[argc] = silc_calloc(idp->len, sizeof(*argv[0]));
198 memcpy(argv[argc], idp->data, idp->len);
199 argv_lens[argc] = idp->len;
200 argv_types[argc] = argc + 1;
202 silc_buffer_free(idp);
205 /* Update statistics */
206 server->stat.clients--;
207 if (server->stat.cell_clients)
208 server->stat.cell_clients--;
209 SILC_OPER_STATS_UPDATE(client, server, SILC_UMODE_SERVER_OPERATOR);
210 SILC_OPER_STATS_UPDATE(client, router, SILC_UMODE_ROUTER_OPERATOR);
212 silc_server_remove_clients_channels(server, entry, clients,
214 silc_server_del_from_watcher_list(server, client);
216 /* Remove the client entry */
217 if (!server_signoff) {
218 client->data.status &= ~SILC_IDLIST_STATUS_REGISTERED;
219 id_cache->expire = SILC_ID_CACHE_EXPIRE_DEF;
221 silc_idlist_del_data(client);
222 silc_idlist_del_client(server->local_list, client);
225 if (!silc_idcache_list_next(list, &id_cache))
229 silc_idcache_list_free(list);
232 if (silc_idcache_get_all(server->global_list->clients, &list)) {
234 if (silc_idcache_list_first(list, &id_cache)) {
236 client = (SilcClientEntry)id_cache->context;
238 /* If client is not registered, is not originated from `router'
239 or is not owned by `entry', skip it. */
240 if (!(client->data.status & SILC_IDLIST_STATUS_REGISTERED) ||
241 client->router != router ||
242 (router != entry && !SILC_ID_COMPARE(client->id, entry->id,
243 client->id->ip.data_len))) {
244 if (!silc_idcache_list_next(list, &id_cache))
250 if (server_signoff) {
251 idp = silc_id_payload_encode(client->id, SILC_ID_CLIENT);
252 argv = silc_realloc(argv, sizeof(*argv) * (argc + 1));
253 argv_lens = silc_realloc(argv_lens, sizeof(*argv_lens) *
255 argv_types = silc_realloc(argv_types, sizeof(*argv_types) *
257 argv[argc] = silc_calloc(idp->len, sizeof(*argv[0]));
258 memcpy(argv[argc], idp->data, idp->len);
259 argv_lens[argc] = idp->len;
260 argv_types[argc] = argc + 1;
262 silc_buffer_free(idp);
265 /* Update statistics */
266 server->stat.clients--;
267 if (server->stat.cell_clients)
268 server->stat.cell_clients--;
269 SILC_OPER_STATS_UPDATE(client, server, SILC_UMODE_SERVER_OPERATOR);
270 SILC_OPER_STATS_UPDATE(client, router, SILC_UMODE_ROUTER_OPERATOR);
272 silc_server_remove_clients_channels(server, entry, clients,
274 silc_server_del_from_watcher_list(server, client);
276 /* Remove the client entry */
277 if (!server_signoff) {
278 client->data.status &= ~SILC_IDLIST_STATUS_REGISTERED;
279 id_cache->expire = SILC_ID_CACHE_EXPIRE_DEF;
281 silc_idlist_del_data(client);
282 silc_idlist_del_client(server->global_list, client);
285 if (!silc_idcache_list_next(list, &id_cache))
289 silc_idcache_list_free(list);
292 /* Return now if we are shutting down */
293 if (server->server_shutdown) {
294 silc_hash_table_free(channels);
296 if (server_signoff) {
297 for (i = 0; i < argc; i++)
300 silc_free(argv_lens);
301 silc_free(argv_types);
302 silc_hash_table_free(clients);
307 /* Send the SERVER_SIGNOFF notify */
308 if (server_signoff) {
309 SilcBuffer args, not;
311 SILC_LOG_DEBUG(("Sending SERVER_SIGNOFF for %s with %d clients",
312 silc_id_render(entry->id, SILC_ID_SERVER), argc - 1));
314 /* Send SERVER_SIGNOFF notify to our primary router */
315 if (server->router != entry) {
316 args = silc_argument_payload_encode(1, argv, argv_lens,
318 silc_server_send_notify_args(server, SILC_PRIMARY_ROUTE(server),
319 SILC_BROADCAST(server),
320 SILC_NOTIFY_TYPE_SERVER_SIGNOFF,
322 silc_buffer_free(args);
325 /* Send to local clients. We also send the list of client ID's that
326 is to be removed for those servers that would like to use that list. */
327 args = silc_argument_payload_encode(argc, argv, argv_lens,
329 not = silc_notify_payload_encode_args(SILC_NOTIFY_TYPE_SERVER_SIGNOFF,
331 silc_server_packet_send_clients(server, clients,
332 SILC_PACKET_NOTIFY, 0, FALSE,
333 not->data, not->len, FALSE);
335 silc_buffer_free(args);
336 silc_buffer_free(not);
337 for (i = 0; i < argc; i++)
340 silc_free(argv_lens);
341 silc_free(argv_types);
342 silc_hash_table_free(clients);
345 /* We must now re-generate the channel key for all channels that had
346 this server's client(s) on the channel. As they left the channel we
347 must re-generate the channel key. */
348 silc_hash_table_list(channels, &htl);
349 while (silc_hash_table_get(&htl, NULL, (void **)&channel)) {
350 if (!silc_server_create_channel_key(server, channel, 0)) {
351 silc_hash_table_list_reset(&htl);
352 silc_hash_table_free(channels);
356 /* Do not send the channel key if private channel key mode is set */
357 if (channel->mode & SILC_CHANNEL_MODE_PRIVKEY)
360 silc_server_send_channel_key(server, NULL, channel,
361 server->server_type == SILC_ROUTER ?
362 FALSE : !server->standalone);
364 silc_hash_table_list_reset(&htl);
365 silc_hash_table_free(channels);
370 static SilcServerEntry
371 silc_server_update_clients_by_real_server(SilcServer server,
372 SilcServerEntry from,
373 SilcClientEntry client,
375 SilcIDCacheEntry client_cache)
377 SilcServerEntry server_entry;
378 SilcIDCacheEntry id_cache = NULL;
379 SilcIDCacheList list;
381 if (!silc_idcache_get_all(server->local_list->servers, &list))
384 if (silc_idcache_list_first(list, &id_cache)) {
386 server_entry = (SilcServerEntry)id_cache->context;
387 if (server_entry != from &&
388 SILC_ID_COMPARE(server_entry->id, client->id,
389 client->id->ip.data_len)) {
390 SILC_LOG_DEBUG(("Found (local) %s",
391 silc_id_render(server_entry->id, SILC_ID_SERVER)));
393 if (!server_entry->data.send_key && server_entry->router) {
394 SILC_LOG_DEBUG(("Server not locally connected, use its router"));
395 /* If the client is not marked as local then move it to local list
396 since the server is local. */
398 SILC_LOG_DEBUG(("Moving client to local list"));
399 silc_idcache_add(server->local_list->clients, client_cache->name,
400 client_cache->id, client_cache->context,
401 client_cache->expire, NULL);
402 silc_idcache_del_by_context(server->global_list->clients, client);
404 server_entry = server_entry->router;
406 /* If the client is not marked as local then move it to local list
407 since the server is local. */
408 if (server_entry->server_type != SILC_BACKUP_ROUTER && !local) {
409 SILC_LOG_DEBUG(("Moving client to local list"));
410 silc_idcache_add(server->local_list->clients, client_cache->name,
411 client_cache->id, client_cache->context,
412 client_cache->expire, NULL);
413 silc_idcache_del_by_context(server->global_list->clients, client);
417 silc_idcache_list_free(list);
421 if (!silc_idcache_list_next(list, &id_cache))
426 silc_idcache_list_free(list);
428 if (!silc_idcache_get_all(server->global_list->servers, &list))
431 if (silc_idcache_list_first(list, &id_cache)) {
433 server_entry = (SilcServerEntry)id_cache->context;
434 if (server_entry != from &&
435 SILC_ID_COMPARE(server_entry->id, client->id,
436 client->id->ip.data_len)) {
437 SILC_LOG_DEBUG(("Found (global) %s",
438 silc_id_render(server_entry->id, SILC_ID_SERVER)));
440 if (!server_entry->data.send_key && server_entry->router) {
441 SILC_LOG_DEBUG(("Server not locally connected, use its router"));
442 /* If the client is marked as local then move it to global list
443 since the server is global. */
445 SILC_LOG_DEBUG(("Moving client to global list"));
446 silc_idcache_add(server->global_list->clients, client_cache->name,
447 client_cache->id, client_cache->context, 0, NULL);
448 silc_idcache_del_by_context(server->local_list->clients, client);
450 server_entry = server_entry->router;
452 /* If the client is marked as local then move it to global list
453 since the server is global. */
454 if (server_entry->server_type != SILC_BACKUP_ROUTER && local) {
455 SILC_LOG_DEBUG(("Moving client to global list"));
456 silc_idcache_add(server->global_list->clients, client_cache->name,
457 client_cache->id, client_cache->context, 0, NULL);
458 silc_idcache_del_by_context(server->local_list->clients, client);
462 silc_idcache_list_free(list);
466 if (!silc_idcache_list_next(list, &id_cache))
471 silc_idcache_list_free(list);
476 /* Updates the clients that are originated from the `from' to be originated
477 from the `to'. If the `resolve_real_server' is TRUE then this will
478 attempt to figure out which clients really are originated from the
479 `from' and which are originated from a server that we have connection
480 to, when we've acting as backup router. If it is FALSE the `to' will
481 be the new source. */
483 void silc_server_update_clients_by_server(SilcServer server,
484 SilcServerEntry from,
486 bool resolve_real_server)
488 SilcIDCacheList list = NULL;
489 SilcIDCacheEntry id_cache = NULL;
490 SilcClientEntry client = NULL;
494 if (silc_idcache_get_all(server->global_list->clients, &list)) {
495 if (silc_idcache_list_first(list, &id_cache)) {
497 client = (SilcClientEntry)id_cache->context;
499 /* If entry is disabled skip it. If entry is local to us, do not
500 switch it to anyone else, it is ours so skip it. */
501 if (!(client->data.status & SILC_IDLIST_STATUS_REGISTERED) ||
502 SILC_IS_LOCAL(client)) {
503 if (!silc_idcache_list_next(list, &id_cache))
509 SILC_LOG_DEBUG(("Client %s",
510 silc_id_render(client->id, SILC_ID_CLIENT)));
512 SILC_LOG_DEBUG(("Client->router %s",
513 silc_id_render(client->router->id, SILC_ID_SERVER)));
516 if (client->router == from) {
517 if (resolve_real_server) {
519 silc_server_update_clients_by_real_server(server, from, client,
521 if (!client->router) {
522 if (server->server_type == SILC_ROUTER)
523 client->router = from;
532 /* All are changed */
537 SILC_LOG_DEBUG(("Client changed to %s",
538 silc_id_render(client->router->id, SILC_ID_CLIENT)));
540 if (!silc_idcache_list_next(list, &id_cache))
544 silc_idcache_list_free(list);
548 if (silc_idcache_get_all(server->local_list->clients, &list)) {
549 if (silc_idcache_list_first(list, &id_cache)) {
551 client = (SilcClientEntry)id_cache->context;
553 /* If entry is disabled skip it. If entry is local to us, do not
554 switch it to anyone else, it is ours so skip it. */
555 if (!(client->data.status & SILC_IDLIST_STATUS_REGISTERED) ||
556 SILC_IS_LOCAL(client)) {
557 if (!silc_idcache_list_next(list, &id_cache))
563 SILC_LOG_DEBUG(("Client %s",
564 silc_id_render(client->id, SILC_ID_CLIENT)));
566 SILC_LOG_DEBUG(("Client->router %s",
567 silc_id_render(client->router->id, SILC_ID_SERVER)));
570 if (client->router == from) {
571 if (resolve_real_server) {
573 silc_server_update_clients_by_real_server(server, from, client,
576 client->router = from;
582 /* All are changed */
587 SILC_LOG_DEBUG(("Client changed to %s",
588 silc_id_render(client->router->id, SILC_ID_CLIENT)));
590 if (!silc_idcache_list_next(list, &id_cache))
594 silc_idcache_list_free(list);
598 /* Updates servers that are from `from' to be originated from `to'. This
599 will also update the server's connection to `to's connection. */
601 void silc_server_update_servers_by_server(SilcServer server,
602 SilcServerEntry from,
605 SilcIDCacheList list = NULL;
606 SilcIDCacheEntry id_cache = NULL;
607 SilcServerEntry server_entry = NULL;
609 SILC_LOG_DEBUG(("Updating servers"));
611 if (silc_idcache_get_all(server->local_list->servers, &list)) {
612 if (silc_idcache_list_first(list, &id_cache)) {
614 server_entry = (SilcServerEntry)id_cache->context;
616 /* If entry is local to us, do not switch it to any anyone else,
618 if (SILC_IS_LOCAL(server_entry) || server_entry == server->id_entry ||
619 server_entry == from) {
620 if (!silc_idcache_list_next(list, &id_cache))
626 /* If we are standalone router, any server that is not directly
627 connected to cannot exist anymore. If we are not standalone
628 we update it correctly. */
629 if (server->server_type == SILC_ROUTER && server->standalone) {
630 silc_server_backup_del(server, server_entry);
631 silc_server_backup_replaced_del(server, server_entry);
632 silc_idlist_del_data(server_entry);
633 silc_idlist_del_server(server->local_list, server_entry);
634 server->stat.servers--;
635 server->stat.cell_servers--;
637 /* XXX if we are not standalone, do a check from local config
638 whether this server is in our cell, but not connected to
639 us (in which case we must remove it). */
641 if (server_entry->router == from) {
642 SILC_LOG_DEBUG(("Updating server (local) %s",
643 server_entry->server_name ?
644 server_entry->server_name : ""));
645 server_entry->router = to;
646 server_entry->connection = to->connection;
650 if (!silc_idcache_list_next(list, &id_cache))
654 silc_idcache_list_free(list);
657 if (silc_idcache_get_all(server->global_list->servers, &list)) {
658 if (silc_idcache_list_first(list, &id_cache)) {
660 server_entry = (SilcServerEntry)id_cache->context;
662 /* If entry is local to us, do not switch it to anyone else,
664 if (SILC_IS_LOCAL(server_entry) || server_entry == server->id_entry ||
665 server_entry == from) {
666 if (!silc_idcache_list_next(list, &id_cache))
672 /* If we are standalone router, any server that is not directly
673 connected to cannot exist anymore. If we are not standalone
674 we update it correctly. */
675 if (server->server_type == SILC_ROUTER && server->standalone) {
676 silc_server_backup_del(server, server_entry);
677 silc_server_backup_replaced_del(server, server_entry);
678 silc_idlist_del_data(server_entry);
679 silc_idlist_del_server(server->global_list, server_entry);
680 server->stat.servers--;
681 server->stat.cell_servers--;
683 /* XXX if we are not standalone, do a check from local config
684 whether this server is in our cell, but not connected to
685 us (in which case we must remove it). */
687 if (server_entry->router == from) {
688 SILC_LOG_DEBUG(("Updating server (global) %s",
689 server_entry->server_name ?
690 server_entry->server_name : ""));
691 server_entry->router = to;
692 server_entry->connection = to->connection;
696 if (!silc_idcache_list_next(list, &id_cache))
700 silc_idcache_list_free(list);
705 /* Toggles the enabled/disabled status of local server connections. Packets
706 can be sent to the servers when `toggle_enabled' is TRUE and will be
707 dropped if `toggle_enabled' is FALSE, after this function is called. */
709 void silc_server_local_servers_toggle_enabled(SilcServer server,
712 SilcIDCacheList list = NULL;
713 SilcIDCacheEntry id_cache = NULL;
714 SilcServerEntry server_entry = NULL;
716 if (silc_idcache_get_all(server->local_list->servers, &list)) {
717 if (silc_idcache_list_first(list, &id_cache)) {
719 server_entry = (SilcServerEntry)id_cache->context;
720 if (!SILC_IS_LOCAL(server_entry) || server_entry == server->id_entry) {
721 if (!silc_idcache_list_next(list, &id_cache))
728 server_entry->data.status &= ~SILC_IDLIST_STATUS_DISABLED;
730 server_entry->data.status |= SILC_IDLIST_STATUS_DISABLED;
732 if (!silc_idcache_list_next(list, &id_cache))
736 silc_idcache_list_free(list);
739 if (silc_idcache_get_all(server->global_list->servers, &list)) {
740 if (silc_idcache_list_first(list, &id_cache)) {
742 server_entry = (SilcServerEntry)id_cache->context;
743 if (!SILC_IS_LOCAL(server_entry) || server_entry == server->id_entry) {
744 if (!silc_idcache_list_next(list, &id_cache))
751 server_entry->data.status &= ~SILC_IDLIST_STATUS_DISABLED;
753 server_entry->data.status |= SILC_IDLIST_STATUS_DISABLED;
755 if (!silc_idcache_list_next(list, &id_cache))
759 silc_idcache_list_free(list);
763 /* Removes servers that are originated from the `from'. The server
764 entry is deleted in this function. If `remove_clients' is TRUE then
765 all clients originated from the server are removed too, and server
766 signoff is sent. Note that this does not remove the `from'. This
767 also does not remove locally connected servers. */
769 void silc_server_remove_servers_by_server(SilcServer server,
770 SilcServerEntry from,
773 SilcIDCacheList list = NULL;
774 SilcIDCacheEntry id_cache = NULL;
775 SilcServerEntry server_entry = NULL;
777 SILC_LOG_DEBUG(("Removing servers by %s",
778 from->server_name ? from->server_name : "server"));
780 if (silc_idcache_get_all(server->local_list->servers, &list)) {
781 if (silc_idcache_list_first(list, &id_cache)) {
783 server_entry = (SilcServerEntry)id_cache->context;
784 if (SILC_IS_LOCAL(server_entry) || server_entry == server->id_entry ||
785 server_entry->router != from || server_entry == from) {
786 if (!silc_idcache_list_next(list, &id_cache))
792 /* Remove clients owned by this server */
794 silc_server_remove_clients_by_server(server, from, server_entry,
797 /* Remove the server */
798 silc_idlist_del_server(server->local_list, server_entry);
800 if (!silc_idcache_list_next(list, &id_cache))
804 silc_idcache_list_free(list);
807 if (silc_idcache_get_all(server->global_list->servers, &list)) {
808 if (silc_idcache_list_first(list, &id_cache)) {
810 server_entry = (SilcServerEntry)id_cache->context;
811 if (SILC_IS_LOCAL(server_entry) || server_entry == server->id_entry ||
812 server_entry->router != from || server_entry == from) {
813 if (!silc_idcache_list_next(list, &id_cache))
819 /* Remove clients owned by this server */
821 silc_server_remove_clients_by_server(server, from, server_entry,
824 /* Remove the server */
825 silc_idlist_del_server(server->global_list, server_entry);
827 if (!silc_idcache_list_next(list, &id_cache))
831 silc_idcache_list_free(list);
835 /* Removes channels that are from `from. */
837 void silc_server_remove_channels_by_server(SilcServer server,
838 SilcServerEntry from)
840 SilcIDCacheList list = NULL;
841 SilcIDCacheEntry id_cache = NULL;
842 SilcChannelEntry channel = NULL;
844 SILC_LOG_DEBUG(("Removing channels by server"));
846 if (silc_idcache_get_all(server->global_list->channels, &list)) {
847 if (silc_idcache_list_first(list, &id_cache)) {
849 channel = (SilcChannelEntry)id_cache->context;
850 if (channel->router == from)
851 silc_idlist_del_channel(server->global_list, channel);
852 if (!silc_idcache_list_next(list, &id_cache))
856 silc_idcache_list_free(list);
860 /* Updates channels that are from `from' to be originated from `to'. */
862 void silc_server_update_channels_by_server(SilcServer server,
863 SilcServerEntry from,
866 SilcIDCacheList list = NULL;
867 SilcIDCacheEntry id_cache = NULL;
868 SilcChannelEntry channel = NULL;
870 SILC_LOG_DEBUG(("Updating channels by server"));
872 if (silc_idcache_get_all(server->global_list->channels, &list)) {
873 if (silc_idcache_list_first(list, &id_cache)) {
875 channel = (SilcChannelEntry)id_cache->context;
876 if (channel->router == from)
877 channel->router = to;
878 if (!silc_idcache_list_next(list, &id_cache))
882 silc_idcache_list_free(list);
886 /* Checks whether given channel has global users. If it does this returns
887 TRUE and FALSE if there is only locally connected clients on the channel. */
889 bool silc_server_channel_has_global(SilcChannelEntry channel)
891 SilcChannelClientEntry chl;
892 SilcHashTableList htl;
894 silc_hash_table_list(channel->user_list, &htl);
895 while (silc_hash_table_get(&htl, NULL, (void **)&chl)) {
896 if (chl->client->router) {
897 silc_hash_table_list_reset(&htl);
901 silc_hash_table_list_reset(&htl);
906 /* Checks whether given channel has locally connected users. If it does this
907 returns TRUE and FALSE if there is not one locally connected client. */
909 bool silc_server_channel_has_local(SilcChannelEntry channel)
911 SilcChannelClientEntry chl;
912 SilcHashTableList htl;
914 silc_hash_table_list(channel->user_list, &htl);
915 while (silc_hash_table_get(&htl, NULL, (void **)&chl)) {
916 if (!chl->client->router) {
917 silc_hash_table_list_reset(&htl);
921 silc_hash_table_list_reset(&htl);
926 /* This function removes the channel and all users on the channel, unless
927 the channel is permanent. In this case the channel is disabled but all
928 users are removed from the channel. Returns TRUE if the channel is
929 destroyed totally, and FALSE if it is permanent and remains. */
931 bool silc_server_channel_delete(SilcServer server,
932 SilcChannelEntry channel)
934 SilcChannelClientEntry chl;
935 SilcHashTableList htl;
936 bool delchan = !(channel->mode & SILC_CHANNEL_MODE_FOUNDER_AUTH);
939 /* Update statistics */
940 if (server->server_type == SILC_ROUTER)
941 server->stat.chanclients -= channel->user_count;
943 /* Totally delete the channel and all users on the channel. The
944 users are deleted automatically in silc_idlist_del_channel. */
945 silc_schedule_task_del_by_context(server->schedule, channel->rekey);
946 if (silc_idlist_del_channel(server->local_list, channel)) {
947 server->stat.my_channels--;
948 if (server->server_type == SILC_ROUTER) {
949 server->stat.channels--;
950 server->stat.cell_channels--;
953 if (silc_idlist_del_channel(server->global_list, channel))
954 if (server->server_type == SILC_ROUTER)
955 server->stat.channels--;
961 /* Channel is permanent, do not remove it, remove only users */
962 channel->disabled = TRUE;
963 silc_hash_table_list(channel->user_list, &htl);
964 while (silc_hash_table_get(&htl, NULL, (void *)&chl)) {
965 silc_hash_table_del(chl->client->channels, channel);
966 silc_hash_table_del(channel->user_list, chl->client);
967 channel->user_count--;
969 /* Update statistics */
970 if (SILC_IS_LOCAL(chl->client))
971 server->stat.my_chanclients--;
972 if (server->server_type == SILC_ROUTER) {
973 server->stat.cell_chanclients--;
974 server->stat.chanclients--;
979 silc_hash_table_list_reset(&htl);
981 SILC_LOG_DEBUG(("Channel %s remains", channel->channel_name));
986 /* Returns TRUE if the given client is on the channel. FALSE if not.
987 This works because we assure that the user list on the channel is
988 always in up to date thus we can only check the channel list from
989 `client' which is faster than checking the user list from `channel'. */
991 bool silc_server_client_on_channel(SilcClientEntry client,
992 SilcChannelEntry channel,
993 SilcChannelClientEntry *chl)
995 if (!client || !channel)
998 return silc_hash_table_find(client->channels, channel, NULL,
1002 /* Checks string for bad characters and returns TRUE if they are found. */
1004 bool silc_server_name_bad_chars(const char *name, SilcUInt32 name_len)
1008 for (i = 0; i < name_len; i++) {
1009 if (!isascii(name[i]))
1011 if (name[i] <= 32) return TRUE;
1012 if (name[i] == ' ') return TRUE;
1013 if (name[i] == '*') return TRUE;
1014 if (name[i] == '?') return TRUE;
1015 if (name[i] == ',') return TRUE;
1021 /* Modifies the `name' if it includes bad characters and returns new
1022 allocated name that does not include bad characters. */
1024 char *silc_server_name_modify_bad(const char *name, SilcUInt32 name_len)
1027 char *newname = strdup(name);
1029 for (i = 0; i < name_len; i++) {
1030 if (!isascii(newname[i])) newname[i] = '_';
1031 if (newname[i] <= 32) newname[i] = '_';
1032 if (newname[i] == ' ') newname[i] = '_';
1033 if (newname[i] == '*') newname[i] = '_';
1034 if (newname[i] == '?') newname[i] = '_';
1035 if (newname[i] == ',') newname[i] = '_';
1041 /* Find number of sockets by IP address indicated by `ip'. Returns 0 if
1042 socket connections with the IP address does not exist. */
1044 SilcUInt32 silc_server_num_sockets_by_ip(SilcServer server, const char *ip,
1045 SilcSocketType type)
1049 for (i = 0, count = 0; i < server->config->param.connections_max; i++) {
1050 if (server->sockets[i] && !SILC_IS_LISTENER(server->sockets[i]) &&
1051 !strcmp(server->sockets[i]->ip, ip) &&
1052 server->sockets[i]->type == type)
1059 /* Find number of sockets by IP address indicated by remote host, indicatd
1060 by `ip' or `hostname', `port', and `type'. Returns 0 if socket connections
1061 does not exist. If `ip' is provided then `hostname' is ignored. */
1063 SilcUInt32 silc_server_num_sockets_by_remote(SilcServer server,
1065 const char *hostname,
1067 SilcSocketType type)
1071 if (!ip && !hostname)
1074 for (i = 0, count = 0; i < server->config->param.connections_max; i++) {
1075 if (server->sockets[i] && !SILC_IS_LISTENER(server->sockets[i]) &&
1076 ((ip && !strcmp(server->sockets[i]->ip, ip)) ||
1077 (hostname && !strcmp(server->sockets[i]->hostname, hostname))) &&
1078 server->sockets[i]->port == port &&
1079 server->sockets[i]->type == type)
1086 /* Finds locally cached public key by the public key received in the SKE.
1087 If we have it locally cached then we trust it and will use it in the
1088 authentication protocol. Returns the locally cached public key or NULL
1089 if we do not find the public key. */
1091 SilcPublicKey silc_server_find_public_key(SilcServer server,
1092 SilcHashTable local_public_keys,
1093 SilcPublicKey remote_public_key)
1095 SilcPublicKey cached_key;
1097 SILC_LOG_DEBUG(("Find remote public key (%d keys in local cache)",
1098 silc_hash_table_count(local_public_keys)));
1100 if (!silc_hash_table_find_ext(local_public_keys, remote_public_key,
1101 (void **)&cached_key, NULL,
1102 silc_hash_public_key, NULL,
1103 silc_hash_public_key_compare, NULL)) {
1104 SILC_LOG_ERROR(("Public key not found"));
1108 SILC_LOG_DEBUG(("Found public key"));
1113 /* This returns the first public key from the table of public keys. This
1114 is used only in cases where single public key exists in the table and
1115 we want to get a pointer to it. For public key tables that has multiple
1116 keys in it the silc_server_find_public_key must be used. */
1118 SilcPublicKey silc_server_get_public_key(SilcServer server,
1119 SilcHashTable local_public_keys)
1121 SilcPublicKey cached_key;
1122 SilcHashTableList htl;
1124 SILC_LOG_DEBUG(("Start"));
1126 assert(silc_hash_table_count(local_public_keys) < 2);
1128 silc_hash_table_list(local_public_keys, &htl);
1129 if (!silc_hash_table_get(&htl, NULL, (void **)&cached_key))
1131 silc_hash_table_list_reset(&htl);
1136 /* Check whether the connection `sock' is allowed to connect to us. This
1137 checks for example whether there is too much connections for this host,
1138 and required version for the host etc. */
1140 bool silc_server_connection_allowed(SilcServer server,
1141 SilcSocketConnection sock,
1142 SilcSocketType type,
1143 SilcServerConfigConnParams *global,
1144 SilcServerConfigConnParams *params,
1147 SilcUInt32 conn_number = (type == SILC_SOCKET_TYPE_CLIENT ?
1148 server->stat.my_clients :
1149 type == SILC_SOCKET_TYPE_SERVER ?
1150 server->stat.my_servers :
1151 server->stat.my_routers);
1152 SilcUInt32 num_sockets, max_hosts, max_per_host;
1153 SilcUInt32 r_protocol_version, l_protocol_version;
1154 SilcUInt32 r_software_version, l_software_version;
1155 char *r_vendor_version = NULL, *l_vendor_version;
1157 SILC_LOG_DEBUG(("Checking whether connection is allowed"));
1161 l_protocol_version =
1162 silc_version_to_num(params && params->version_protocol ?
1163 params->version_protocol :
1164 global->version_protocol);
1165 l_software_version =
1166 silc_version_to_num(params && params->version_software ?
1167 params->version_software :
1168 global->version_software);
1169 l_vendor_version = (params && params->version_software_vendor ?
1170 params->version_software_vendor :
1171 global->version_software_vendor);
1173 if (ske && silc_ske_parse_version(ske, &r_protocol_version, NULL,
1174 &r_software_version, NULL,
1175 &r_vendor_version)) {
1176 sock->version = r_protocol_version;
1178 /* Match protocol version */
1179 if (l_protocol_version && r_protocol_version &&
1180 r_protocol_version < l_protocol_version) {
1181 SILC_LOG_INFO(("Connection %s (%s) is too old version",
1182 sock->hostname, sock->ip));
1183 silc_server_disconnect_remote(server, sock,
1184 SILC_STATUS_ERR_BAD_VERSION,
1185 "You support too old protocol version");
1189 /* Math software version */
1190 if (l_software_version && r_software_version &&
1191 r_software_version < l_software_version) {
1192 SILC_LOG_INFO(("Connection %s (%s) is too old version",
1193 sock->hostname, sock->ip));
1194 silc_server_disconnect_remote(server, sock,
1195 SILC_STATUS_ERR_BAD_VERSION,
1196 "You support too old software version");
1200 /* Regex match vendor version */
1201 if (l_vendor_version && r_vendor_version &&
1202 !silc_string_match(l_vendor_version, r_vendor_version)) {
1203 SILC_LOG_INFO(("Connection %s (%s) is unsupported version",
1204 sock->hostname, sock->ip));
1205 silc_server_disconnect_remote(server, sock,
1206 SILC_STATUS_ERR_BAD_VERSION,
1207 "Your software is not supported");
1211 silc_free(r_vendor_version);
1213 /* Check for maximum connections limit */
1215 num_sockets = silc_server_num_sockets_by_ip(server, sock->ip, type);
1216 max_hosts = (params ? params->connections_max : global->connections_max);
1217 max_per_host = (params ? params->connections_max_per_host :
1218 global->connections_max_per_host);
1220 if (max_hosts && conn_number >= max_hosts) {
1221 SILC_LOG_INFO(("Server is full, closing %s (%s) connection",
1222 sock->hostname, sock->ip));
1223 silc_server_disconnect_remote(server, sock,
1224 SILC_STATUS_ERR_RESOURCE_LIMIT,
1225 "Server is full, try again later");
1229 if (num_sockets >= max_per_host) {
1230 SILC_LOG_INFO(("Too many connections from %s (%s), closing connection",
1231 sock->hostname, sock->ip));
1232 silc_server_disconnect_remote(server, sock,
1233 SILC_STATUS_ERR_RESOURCE_LIMIT,
1234 "Too many connections from your host");
1241 /* Checks that client has rights to add or remove channel modes. If any
1242 of the checks fails FALSE is returned. */
1244 bool silc_server_check_cmode_rights(SilcServer server,
1245 SilcChannelEntry channel,
1246 SilcChannelClientEntry client,
1249 bool is_op = client->mode & SILC_CHANNEL_UMODE_CHANOP;
1250 bool is_fo = client->mode & SILC_CHANNEL_UMODE_CHANFO;
1252 /* Check whether has rights to change anything */
1253 if (!is_op && !is_fo)
1256 /* Check whether has rights to change everything */
1260 /* Founder implies operator */
1264 /* We know that client is channel operator, check that they are not
1265 changing anything that requires channel founder rights. Rest of the
1266 modes are available automatically for channel operator. */
1268 if (mode & SILC_CHANNEL_MODE_PRIVKEY) {
1269 if (is_op && !is_fo)
1272 if (channel->mode & SILC_CHANNEL_MODE_PRIVKEY) {
1273 if (is_op && !is_fo)
1278 if (mode & SILC_CHANNEL_MODE_PASSPHRASE) {
1279 if (!(channel->mode & SILC_CHANNEL_MODE_PASSPHRASE)) {
1280 if (is_op && !is_fo)
1284 if (channel->mode & SILC_CHANNEL_MODE_PASSPHRASE) {
1285 if (is_op && !is_fo)
1290 if (mode & SILC_CHANNEL_MODE_CIPHER) {
1291 if (!(channel->mode & SILC_CHANNEL_MODE_CIPHER)) {
1292 if (is_op && !is_fo)
1296 if (channel->mode & SILC_CHANNEL_MODE_CIPHER) {
1297 if (is_op && !is_fo)
1302 if (mode & SILC_CHANNEL_MODE_FOUNDER_AUTH) {
1303 if (!(channel->mode & SILC_CHANNEL_MODE_FOUNDER_AUTH)) {
1304 if (is_op && !is_fo)
1308 if (channel->mode & SILC_CHANNEL_MODE_FOUNDER_AUTH) {
1309 if (is_op && !is_fo)
1314 if (mode & SILC_CHANNEL_MODE_SILENCE_USERS) {
1315 if (!(channel->mode & SILC_CHANNEL_MODE_SILENCE_USERS)) {
1316 if (is_op && !is_fo)
1320 if (channel->mode & SILC_CHANNEL_MODE_SILENCE_USERS) {
1321 if (is_op && !is_fo)
1326 if (mode & SILC_CHANNEL_MODE_SILENCE_OPERS) {
1327 if (!(channel->mode & SILC_CHANNEL_MODE_SILENCE_OPERS)) {
1328 if (is_op && !is_fo)
1332 if (channel->mode & SILC_CHANNEL_MODE_SILENCE_OPERS) {
1333 if (is_op && !is_fo)
1341 /* Check that the client has rights to change its user mode. Returns
1342 FALSE if setting some mode is not allowed. */
1344 bool silc_server_check_umode_rights(SilcServer server,
1345 SilcClientEntry client,
1348 bool server_op = FALSE, router_op = FALSE;
1350 if (mode & SILC_UMODE_SERVER_OPERATOR) {
1351 /* Cannot set server operator mode (must use OPER command) */
1352 if (!(client->mode & SILC_UMODE_SERVER_OPERATOR))
1355 /* Remove the server operator rights */
1356 if (client->mode & SILC_UMODE_SERVER_OPERATOR)
1360 if (mode & SILC_UMODE_ROUTER_OPERATOR) {
1361 /* Cannot set router operator mode (must use SILCOPER command) */
1362 if (!(client->mode & SILC_UMODE_ROUTER_OPERATOR))
1365 /* Remove the router operator rights */
1366 if (client->mode & SILC_UMODE_ROUTER_OPERATOR)
1371 SILC_UMODE_STATS_UPDATE(server, SILC_UMODE_SERVER_OPERATOR);
1373 SILC_UMODE_STATS_UPDATE(router, SILC_UMODE_ROUTER_OPERATOR);
1378 /* This function is used to send the notify packets and motd to the
1379 incoming client connection. */
1381 void silc_server_send_connect_notifys(SilcServer server,
1382 SilcSocketConnection sock,
1383 SilcClientEntry client)
1385 SilcIDListData idata = (SilcIDListData)client;
1387 SILC_LOG_DEBUG(("Send welcome notifys"));
1389 /* Send some nice info to the client */
1390 SILC_SERVER_SEND_NOTIFY(server, sock, SILC_NOTIFY_TYPE_NONE,
1391 ("Welcome to the SILC Network %s",
1393 SILC_SERVER_SEND_NOTIFY(server, sock, SILC_NOTIFY_TYPE_NONE,
1394 ("Your host is %s, running version %s",
1395 server->server_name, server_version));
1397 if (server->server_type == SILC_ROUTER) {
1398 SILC_SERVER_SEND_NOTIFY(server, sock, SILC_NOTIFY_TYPE_NONE,
1399 ("There are %d clients, %d servers and %d "
1400 "routers in SILC Network",
1401 server->stat.clients, server->stat.servers + 1,
1402 server->stat.routers));
1404 if (server->stat.clients && server->stat.servers + 1)
1405 SILC_SERVER_SEND_NOTIFY(server, sock, SILC_NOTIFY_TYPE_NONE,
1406 ("There are %d clients, %d servers and %d "
1407 "routers in SILC Network",
1408 server->stat.clients, server->stat.servers + 1,
1409 (server->standalone ? 0 :
1410 !server->stat.routers ? 1 :
1411 server->stat.routers)));
1414 if (server->stat.cell_clients && server->stat.cell_servers + 1)
1415 SILC_SERVER_SEND_NOTIFY(server, sock, SILC_NOTIFY_TYPE_NONE,
1416 ("There are %d clients on %d server in our cell",
1417 server->stat.cell_clients,
1418 server->stat.cell_servers + 1));
1419 if (server->server_type == SILC_ROUTER) {
1420 SILC_SERVER_SEND_NOTIFY(server, sock, SILC_NOTIFY_TYPE_NONE,
1421 ("I have %d clients, %d channels, %d servers and "
1423 server->stat.my_clients,
1424 server->stat.my_channels,
1425 server->stat.my_servers,
1426 server->stat.my_routers));
1428 SILC_SERVER_SEND_NOTIFY(server, sock, SILC_NOTIFY_TYPE_NONE,
1429 ("I have %d clients and %d channels formed",
1430 server->stat.my_clients,
1431 server->stat.my_channels));
1434 if (server->stat.server_ops || server->stat.router_ops)
1435 SILC_SERVER_SEND_NOTIFY(server, sock, SILC_NOTIFY_TYPE_NONE,
1436 ("There are %d server operators and %d router "
1438 server->stat.server_ops,
1439 server->stat.router_ops));
1440 if (server->stat.my_router_ops + server->stat.my_server_ops)
1441 SILC_SERVER_SEND_NOTIFY(server, sock, SILC_NOTIFY_TYPE_NONE,
1442 ("I have %d operators online",
1443 server->stat.my_router_ops +
1444 server->stat.my_server_ops));
1446 SILC_SERVER_SEND_NOTIFY(server, sock, SILC_NOTIFY_TYPE_NONE,
1447 ("Your connection is secured with %s cipher, "
1448 "key length %d bits",
1449 idata->send_key->cipher->name,
1450 idata->send_key->cipher->key_len));
1451 SILC_SERVER_SEND_NOTIFY(server, sock, SILC_NOTIFY_TYPE_NONE,
1452 ("Your current nickname is %s",
1456 silc_server_send_motd(server, sock);
1459 /* Kill the client indicated by `remote_client' sending KILLED notify
1460 to the client, to all channels client has joined and to primary
1461 router if needed. The killed client is also removed from all channels. */
1463 void silc_server_kill_client(SilcServer server,
1464 SilcClientEntry remote_client,
1465 const char *comment,
1467 SilcIdType killer_id_type)
1469 SilcBuffer killed, killer;
1471 SILC_LOG_DEBUG(("Killing client %s",
1472 silc_id_render(remote_client->id, SILC_ID_CLIENT)));
1474 /* Send the KILL notify packets. First send it to the channel, then
1475 to our primary router and then directly to the client who is being
1476 killed right now. */
1478 killed = silc_id_payload_encode(remote_client->id, SILC_ID_CLIENT);
1479 killer = silc_id_payload_encode(killer_id, killer_id_type);
1481 /* Send KILLED notify to the channels. It is not sent to the client
1482 as it will be sent differently destined directly to the client and not
1484 silc_server_send_notify_on_channels(server, remote_client,
1485 remote_client, SILC_NOTIFY_TYPE_KILLED,
1486 3, killed->data, killed->len,
1487 comment, comment ? strlen(comment) : 0,
1488 killer->data, killer->len);
1490 /* Send KILLED notify to primary route */
1491 silc_server_send_notify_killed(server, SILC_PRIMARY_ROUTE(server),
1492 SILC_BROADCAST(server), remote_client->id,
1493 comment, killer_id, killer_id_type);
1495 /* Send KILLED notify to the client directly */
1496 if (remote_client->connection || remote_client->router)
1497 silc_server_send_notify_killed(server, remote_client->connection ?
1498 remote_client->connection :
1499 remote_client->router->connection, FALSE,
1500 remote_client->id, comment,
1501 killer_id, killer_id_type);
1503 /* Remove the client from all channels. This generates new keys to the
1504 channels as well. */
1505 silc_server_remove_from_channels(server, NULL, remote_client, FALSE,
1508 /* Remove the client entry, If it is locally connected then we will also
1509 disconnect the client here */
1510 if (remote_client->connection) {
1511 /* Remove locally conneted client */
1512 SilcSocketConnection sock = remote_client->connection;
1513 silc_server_free_client_data(server, sock, remote_client, FALSE, NULL);
1514 silc_server_close_connection(server, sock);
1516 /* Update statistics */
1517 server->stat.clients--;
1518 if (server->stat.cell_clients)
1519 server->stat.cell_clients--;
1520 SILC_OPER_STATS_UPDATE(remote_client, server, SILC_UMODE_SERVER_OPERATOR);
1521 SILC_OPER_STATS_UPDATE(remote_client, router, SILC_UMODE_ROUTER_OPERATOR);
1523 if (SILC_IS_LOCAL(remote_client)) {
1524 server->stat.my_clients--;
1525 silc_schedule_task_del_by_context(server->schedule, remote_client);
1526 silc_idlist_del_data(remote_client);
1529 /* Remove remote client */
1530 silc_idlist_del_data(remote_client);
1531 if (!silc_idlist_del_client(server->global_list, remote_client)) {
1532 /* Remove this client from watcher list if it is */
1533 silc_server_del_from_watcher_list(server, remote_client);
1534 silc_idlist_del_client(server->local_list, remote_client);
1538 silc_buffer_free(killer);
1539 silc_buffer_free(killed);
1544 SilcClientEntry client;
1545 SilcNotifyType notify;
1546 const char *new_nick;
1547 } WatcherNotifyContext;
1550 silc_server_check_watcher_list_foreach(void *key, void *context,
1553 WatcherNotifyContext *notify = user_context;
1554 SilcClientEntry entry = context;
1555 SilcSocketConnection sock;
1557 if (entry == notify->client)
1560 sock = silc_server_get_client_route(notify->server, NULL, 0, entry->id,
1563 SILC_LOG_DEBUG(("Sending WATCH notify to %s",
1564 silc_id_render(entry->id, SILC_ID_CLIENT)));
1566 /* Send the WATCH notify */
1567 silc_server_send_notify_watch(notify->server, sock, entry,
1569 notify->new_nick ? notify->new_nick :
1570 (const char *)notify->client->nickname,
1575 /* This function checks whether the `client' nickname is being watched
1576 by someone, and notifies the watcher of the notify change of notify
1577 type indicated by `notify'. */
1579 bool silc_server_check_watcher_list(SilcServer server,
1580 SilcClientEntry client,
1581 const char *new_nick,
1582 SilcNotifyType notify)
1584 unsigned char hash[16];
1585 WatcherNotifyContext n;
1587 SILC_LOG_DEBUG(("Checking watcher list %s",
1588 client->nickname ? client->nickname : (unsigned char *)""));
1590 /* If the watching is rejected by the client do nothing */
1591 if (client->mode & SILC_UMODE_REJECT_WATCHING)
1594 /* Make hash from the nick, or take it from Client ID */
1595 if (client->nickname) {
1597 memset(nick, 0, sizeof(nick));
1598 silc_to_lower(client->nickname, nick, sizeof(nick) - 1);
1599 silc_hash_make(server->md5hash, nick, strlen(nick), hash);
1601 memset(hash, 0, sizeof(hash));
1602 memcpy(hash, client->id->hash, sizeof(client->id->hash));
1607 n.new_nick = new_nick;
1610 /* Send notify to all watchers */
1611 silc_hash_table_find_foreach(server->watcher_list, hash,
1612 silc_server_check_watcher_list_foreach, &n);
1617 /* Remove the `client' from watcher list. After calling this the `client'
1618 is not watching any nicknames. */
1620 bool silc_server_del_from_watcher_list(SilcServer server,
1621 SilcClientEntry client)
1623 SilcHashTableList htl;
1625 SilcClientEntry entry;
1628 silc_hash_table_list(server->watcher_list, &htl);
1629 while (silc_hash_table_get(&htl, &key, (void **)&entry)) {
1630 if (entry == client) {
1631 silc_hash_table_del_by_context(server->watcher_list, key, client);
1634 SILC_LOG_DEBUG(("Removing %s from WATCH list",
1635 silc_id_render(client->id, SILC_ID_CLIENT)));
1637 /* Now check whether there still exists entries with this key, if not
1638 then free the key to not leak memory. */
1639 if (!silc_hash_table_find(server->watcher_list, key, NULL, NULL))
1645 silc_hash_table_list_reset(&htl);
1650 /* Force the client indicated by `chl' to change the channel user mode
1651 on channel indicated by `channel' to `forced_mode'. */
1653 bool silc_server_force_cumode_change(SilcServer server,
1654 SilcSocketConnection sock,
1655 SilcChannelEntry channel,
1656 SilcChannelClientEntry chl,
1657 SilcUInt32 forced_mode)
1659 SilcBuffer idp1, idp2;
1660 unsigned char cumode[4];
1662 SILC_LOG_DEBUG(("Enforcing sender to change mode"));
1665 silc_server_send_notify_cumode(server, sock, FALSE, channel, forced_mode,
1666 server->id, SILC_ID_SERVER,
1667 chl->client->id, NULL);
1669 idp1 = silc_id_payload_encode(server->id, SILC_ID_SERVER);
1670 idp2 = silc_id_payload_encode(chl->client->id, SILC_ID_CLIENT);
1671 SILC_PUT32_MSB(forced_mode, cumode);
1672 silc_server_send_notify_to_channel(server, sock, channel, FALSE,
1673 SILC_NOTIFY_TYPE_CUMODE_CHANGE,
1674 3, idp1->data, idp1->len,
1675 cumode, sizeof(cumode),
1676 idp2->data, idp2->len);
1677 silc_buffer_free(idp1);
1678 silc_buffer_free(idp2);
1683 /* Find active socket connection by the IP address and port indicated by
1684 `ip' and `port', and socket connection type of `type'. */
1686 SilcSocketConnection
1687 silc_server_find_socket_by_host(SilcServer server,
1688 SilcSocketType type,
1689 const char *ip, SilcUInt16 port)
1693 for (i = 0; i < server->config->param.connections_max; i++) {
1694 if (!server->sockets[i])
1696 if (!strcmp(server->sockets[i]->ip, ip) &&
1697 (!port || server->sockets[i]->port == port) &&
1698 server->sockets[i]->type == type)
1699 return server->sockets[i];