5 Author: Pekka Riikonen <priikone@poseidon.pspt.fi>
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.
22 #include "serverincludes.h"
25 /******************************************************************************
29 ******************************************************************************/
31 /* This function is used to add keys and stuff to common ID entry data
34 void silc_idlist_add_data(void *entry, SilcIDListData idata)
36 SilcIDListData data = (SilcIDListData)entry;
37 data->send_key = idata->send_key;
38 data->receive_key = idata->receive_key;
39 data->rekey = idata->rekey;
40 data->hash = idata->hash;
41 data->hmac_send = idata->hmac_send;
42 data->hmac_receive = idata->hmac_receive;
43 data->public_key = idata->public_key;
44 data->last_receive = idata->last_receive;
45 data->last_sent = idata->last_sent;
46 data->status = idata->status;
49 /* Free's all data in the common ID entry data structure. */
51 void silc_idlist_del_data(void *entry)
53 SilcIDListData idata = (SilcIDListData)entry;
55 silc_cipher_free(idata->send_key);
56 if (idata->receive_key)
57 silc_cipher_free(idata->receive_key);
59 if (idata->rekey->send_enc_key) {
60 memset(idata->rekey->send_enc_key, 0, idata->rekey->enc_key_len);
61 silc_free(idata->rekey->send_enc_key);
63 silc_free(idata->rekey);
65 if (idata->hmac_send) /* Same as idata->hmac_receive */
66 silc_hmac_free(idata->hmac_send);
67 if (idata->public_key)
68 silc_pkcs_public_key_free(idata->public_key);
73 SILC_TASK_CALLBACK_GLOBAL(silc_idlist_purge)
75 SilcIDListPurge i = (SilcIDListPurge)context;
77 SILC_LOG_DEBUG(("Start"));
79 silc_idcache_purge(i->cache);
80 silc_schedule_task_add(i->schedule, 0,
83 SILC_TASK_TIMEOUT, SILC_TASK_PRI_LOW);
86 /******************************************************************************
88 Server entry functions
90 ******************************************************************************/
92 /* Add new server entry. This adds the new server entry to ID cache and
93 returns the allocated entry object or NULL on error. This is called
94 when new server connects to us. We also add ourselves to cache with
98 silc_idlist_add_server(SilcIDList id_list,
99 char *server_name, int server_type,
100 SilcServerID *id, SilcServerEntry router,
103 SilcServerEntry server;
105 SILC_LOG_DEBUG(("Adding new server entry"));
107 server = silc_calloc(1, sizeof(*server));
108 server->server_name = server_name;
109 server->server_type = server_type;
111 server->router = router;
112 server->connection = connection;
114 if (!silc_idcache_add(id_list->servers, server->server_name,
115 (void *)server->id, (void *)server, FALSE)) {
123 /* Finds server by Server ID */
126 silc_idlist_find_server_by_id(SilcIDList id_list, SilcServerID *id,
127 bool registered, SilcIDCacheEntry *ret_entry)
129 SilcIDCacheEntry id_cache = NULL;
130 SilcServerEntry server;
135 SILC_LOG_DEBUG(("Server ID (%s)",
136 silc_id_render(id, SILC_ID_SERVER)));
138 if (!silc_idcache_find_by_id_one(id_list->servers, (void *)id,
142 server = (SilcServerEntry)id_cache->context;
145 *ret_entry = id_cache;
147 if (server && registered &&
148 !(server->data.status & SILC_IDLIST_STATUS_REGISTERED))
151 SILC_LOG_DEBUG(("Found"));
156 /* Find server by name */
159 silc_idlist_find_server_by_name(SilcIDList id_list, char *name,
160 bool registered, SilcIDCacheEntry *ret_entry)
162 SilcIDCacheEntry id_cache = NULL;
163 SilcServerEntry server;
165 SILC_LOG_DEBUG(("Server by name `%s'", name));
167 if (!silc_idcache_find_by_name_one(id_list->servers, name, &id_cache))
170 server = (SilcServerEntry)id_cache->context;
173 *ret_entry = id_cache;
175 if (server && registered &&
176 !(server->data.status & SILC_IDLIST_STATUS_REGISTERED))
179 SILC_LOG_DEBUG(("Found"));
184 /* Find server by connection parameters, hostname and port */
187 silc_idlist_find_server_by_conn(SilcIDList id_list, char *hostname,
188 int port, bool registered,
189 SilcIDCacheEntry *ret_entry)
191 SilcIDCacheList list = NULL;
192 SilcIDCacheEntry id_cache = NULL;
193 SilcServerEntry server = NULL;
194 SilcSocketConnection sock;
196 SILC_LOG_DEBUG(("Server by hostname %s and port %d", hostname, port));
198 if (!silc_idcache_get_all(id_list->servers, &list))
201 if (!silc_idcache_list_first(list, &id_cache)) {
202 silc_idcache_list_free(list);
207 server = (SilcServerEntry)id_cache->context;
208 sock = (SilcSocketConnection)server->connection;
210 if (sock && ((sock->hostname && !strcasecmp(sock->hostname, hostname)) ||
211 (sock->ip && !strcasecmp(sock->ip, hostname)))
212 && sock->port == port)
218 if (!silc_idcache_list_next(list, &id_cache))
222 silc_idcache_list_free(list);
225 *ret_entry = id_cache;
227 if (server && registered &&
228 !(server->data.status & SILC_IDLIST_STATUS_REGISTERED))
231 SILC_LOG_DEBUG(("Found"));
236 /* Replaces old Server ID with new one */
239 silc_idlist_replace_server_id(SilcIDList id_list, SilcServerID *old_id,
240 SilcServerID *new_id)
242 SilcIDCacheEntry id_cache = NULL;
243 SilcServerEntry server;
245 if (!old_id || !new_id)
248 SILC_LOG_DEBUG(("Replacing Server ID"));
250 if (!silc_idcache_find_by_id_one(id_list->servers, (void *)old_id,
254 server = (SilcServerEntry)id_cache->context;
256 /* Remove the old entry and add a new one */
258 silc_idcache_del_by_id(id_list->servers, (void *)server->id);
260 silc_free(server->id);
263 silc_idcache_add(id_list->servers, server->server_name, server->id,
266 SILC_LOG_DEBUG(("Found"));
271 /* Removes and free's server entry from ID list */
273 int silc_idlist_del_server(SilcIDList id_list, SilcServerEntry entry)
275 SILC_LOG_DEBUG(("Start"));
278 /* Remove from cache */
280 if (!silc_idcache_del_by_id(id_list->servers, (void *)entry->id))
284 if (entry->server_name)
285 silc_free(entry->server_name);
287 silc_free(entry->id);
289 memset(entry, 'F', sizeof(*entry));
297 /******************************************************************************
299 Client entry functions
301 ******************************************************************************/
303 /* Add new client entry. This adds the client entry to ID cache system
304 and returns the allocated client entry or NULL on error. This is
305 called when new client connection is accepted to the server. If The
306 `router' is provided then the all server routines assume that the client
307 is not directly connected local client but it has router set and is
308 remote. If this is the case then `connection' must be NULL. If, on the
309 other hand, the `connection' is provided then the client is assumed
310 to be directly connected local client and `router' must be NULL. */
313 silc_idlist_add_client(SilcIDList id_list, char *nickname, char *username,
314 char *userinfo, SilcClientID *id,
315 SilcServerEntry router, void *connection)
317 SilcClientEntry client;
319 SILC_LOG_DEBUG(("Adding new client entry"));
321 client = silc_calloc(1, sizeof(*client));
322 client->nickname = nickname;
323 client->username = username;
324 client->userinfo = userinfo;
326 client->router = router;
327 client->connection = connection;
328 client->channels = silc_hash_table_alloc(3, silc_hash_ptr, NULL,
329 NULL, NULL, NULL, NULL, TRUE);
331 if (!silc_idcache_add(id_list->clients, nickname, (void *)client->id,
332 (void *)client, FALSE)) {
333 silc_hash_table_free(client->channels);
341 /* Free client entry. This free's everything and removes the entry
342 from ID cache. Call silc_idlist_del_data before calling this one. */
344 int silc_idlist_del_client(SilcIDList id_list, SilcClientEntry entry)
346 SILC_LOG_DEBUG(("Start"));
349 /* Remove from cache */
351 if (!silc_idcache_del_by_context(id_list->clients, entry))
356 silc_free(entry->nickname);
358 silc_free(entry->username);
360 silc_free(entry->userinfo);
362 silc_free(entry->id);
364 memset(entry, 'F', sizeof(*entry));
373 /* Returns all clients matching requested nickname. Number of clients is
374 returned to `clients_count'. Caller must free the returned table. */
376 int silc_idlist_get_clients_by_nickname(SilcIDList id_list, char *nickname,
378 SilcClientEntry **clients,
379 uint32 *clients_count)
381 SilcIDCacheList list = NULL;
382 SilcIDCacheEntry id_cache = NULL;
384 SILC_LOG_DEBUG(("Start"));
386 if (!silc_idcache_find_by_name(id_list->clients, nickname, &list))
389 *clients = silc_realloc(*clients,
390 (silc_idcache_list_count(list) + *clients_count) *
393 silc_idcache_list_first(list, &id_cache);
394 (*clients)[(*clients_count)++] = (SilcClientEntry)id_cache->context;
396 while (silc_idcache_list_next(list, &id_cache))
397 (*clients)[(*clients_count)++] = (SilcClientEntry)id_cache->context;
399 silc_idcache_list_free(list);
401 SILC_LOG_DEBUG(("Found total %d clients", *clients_count));
406 /* Returns all clients matching requested nickname hash. Number of clients
407 is returned to `clients_count'. Caller must free the returned table. */
409 int silc_idlist_get_clients_by_hash(SilcIDList id_list, char *nickname,
411 SilcClientEntry **clients,
412 uint32 *clients_count)
414 SilcIDCacheList list = NULL;
415 SilcIDCacheEntry id_cache = NULL;
416 unsigned char hash[32];
417 SilcClientID client_id;
419 SILC_LOG_DEBUG(("Start"));
421 silc_hash_make(md5hash, nickname, strlen(nickname), hash);
423 /* As the Client ID is hashed in the ID cache by hashing only the hash
424 from the Client ID, we can do a lookup with only the hash not the
425 other parts of the ID and get all the clients with that hash, ie.
426 with that nickname, as the hash is from the nickname. */
427 memset(&client_id, 0, sizeof(client_id));
428 memcpy(&client_id.hash, hash, sizeof(client_id.hash));
429 if (!silc_idcache_find_by_id(id_list->clients, &client_id, &list))
432 *clients = silc_realloc(*clients,
433 (silc_idcache_list_count(list) + *clients_count) *
436 silc_idcache_list_first(list, &id_cache);
437 (*clients)[(*clients_count)++] = (SilcClientEntry)id_cache->context;
439 while (silc_idcache_list_next(list, &id_cache))
440 (*clients)[(*clients_count)++] = (SilcClientEntry)id_cache->context;
442 silc_idcache_list_free(list);
444 SILC_LOG_DEBUG(("Found total %d clients", *clients_count));
449 /* Finds client by Client ID */
452 silc_idlist_find_client_by_id(SilcIDList id_list, SilcClientID *id,
453 bool registered, SilcIDCacheEntry *ret_entry)
455 SilcIDCacheEntry id_cache = NULL;
456 SilcClientEntry client;
461 SILC_LOG_DEBUG(("Client ID (%s)",
462 silc_id_render(id, SILC_ID_CLIENT)));
464 /* Do extended search since the normal ID comparison function for
465 Client ID's compares only the hash from the Client ID and not the
466 entire ID. The silc_hash_client_id_compare compares the entire
467 Client ID as we want to find one specific Client ID. */
468 if (!silc_idcache_find_by_id_one_ext(id_list->clients, (void *)id,
470 silc_hash_client_id_compare, NULL,
474 client = (SilcClientEntry)id_cache->context;
477 *ret_entry = id_cache;
479 if (client && registered &&
480 !(client->data.status & SILC_IDLIST_STATUS_REGISTERED))
483 SILC_LOG_DEBUG(("Found"));
488 /* Replaces old Client ID with new one */
491 silc_idlist_replace_client_id(SilcIDList id_list, SilcClientID *old_id,
492 SilcClientID *new_id)
494 SilcIDCacheEntry id_cache = NULL;
495 SilcClientEntry client;
497 if (!old_id || !new_id)
500 SILC_LOG_DEBUG(("Replacing Client ID"));
502 /* Do extended search since the normal ID comparison function for
503 Client ID's compares only the hash from the Client ID and not the
504 entire ID. The silc_hash_client_id_compare compares the entire
505 Client ID as we want to find one specific Client ID. */
506 if (!silc_idcache_find_by_id_one_ext(id_list->clients, (void *)old_id,
508 silc_hash_client_id_compare, NULL,
512 client = (SilcClientEntry)id_cache->context;
514 /* Remove the old entry and add a new one */
516 silc_idcache_del_by_context(id_list->clients, client);
518 silc_free(client->id);
521 silc_idcache_add(id_list->clients, NULL, client->id, client, FALSE);
523 SILC_LOG_DEBUG(("Replaced"));
528 /* Client cache entry destructor that is called when the cache is purged. */
530 void silc_idlist_client_destructor(SilcIDCache cache,
531 SilcIDCacheEntry entry)
533 SilcClientEntry client;
535 SILC_LOG_DEBUG(("Start"));
537 client = (SilcClientEntry)entry->context;
539 if (client->nickname)
540 silc_free(client->nickname);
541 if (client->username)
542 silc_free(client->username);
543 if (client->userinfo)
544 silc_free(client->userinfo);
546 silc_free(client->id);
548 memset(client, 'F', sizeof(*client));
553 /******************************************************************************
555 Channel entry functions
557 ******************************************************************************/
559 /* Add new channel entry. This add the new channel entry to the ID cache
560 system and returns the allocated entry or NULL on error. */
563 silc_idlist_add_channel(SilcIDList id_list, char *channel_name, int mode,
564 SilcChannelID *id, SilcServerEntry router,
565 SilcCipher channel_key, SilcHmac hmac)
567 SilcChannelEntry channel;
569 SILC_LOG_DEBUG(("Adding new channel entry"));
571 channel = silc_calloc(1, sizeof(*channel));
572 channel->channel_name = channel_name;
573 channel->mode = mode;
575 channel->router = router;
576 channel->channel_key = channel_key;
577 channel->hmac = hmac;
579 if (!silc_hmac_alloc("hmac-sha1-96", NULL, &channel->hmac)) {
584 channel->user_list = silc_hash_table_alloc(3, silc_hash_ptr, NULL, NULL,
585 NULL, NULL, NULL, TRUE);
587 if (!silc_idcache_add(id_list->channels, channel->channel_name,
588 (void *)channel->id, (void *)channel, FALSE)) {
589 silc_hmac_free(channel->hmac);
590 silc_hash_table_free(channel->user_list);
598 /* Foreach callbcak to free all users from the channel when deleting a
601 static void silc_idlist_del_channel_foreach(void *key, void *context,
604 SilcChannelClientEntry chl = (SilcChannelClientEntry)context;
606 /* Remove the context from the client's channel hash table as that
607 table and channel's user_list hash table share this same context. */
608 silc_hash_table_del(chl->client->channels, chl->channel);
612 /* Free channel entry. This free's everything. */
614 int silc_idlist_del_channel(SilcIDList id_list, SilcChannelEntry entry)
616 SILC_LOG_DEBUG(("Start"));
619 /* Remove from cache */
621 if (!silc_idcache_del_by_id(id_list->channels, (void *)entry->id))
625 if (entry->channel_name)
626 silc_free(entry->channel_name);
628 silc_free(entry->id);
630 silc_free(entry->topic);
631 if (entry->channel_key)
632 silc_cipher_free(entry->channel_key);
634 memset(entry->key, 0, entry->key_len / 8);
635 silc_free(entry->key);
638 silc_free(entry->cipher);
639 if (entry->hmac_name)
640 silc_free(entry->hmac_name);
642 silc_free(entry->rekey);
644 /* Free all client entrys from the users list. The silc_hash_table_free
645 will free all the entries so they are not freed at the foreach
647 silc_hash_table_foreach(entry->user_list, silc_idlist_del_channel_foreach,
649 silc_hash_table_free(entry->user_list);
651 memset(entry, 'F', sizeof(*entry));
659 /* Finds channel by channel name. Channel names are unique and they
660 are not case-sensitive. */
663 silc_idlist_find_channel_by_name(SilcIDList id_list, char *name,
664 SilcIDCacheEntry *ret_entry)
666 SilcIDCacheEntry id_cache = NULL;
668 SILC_LOG_DEBUG(("Channel by name"));
670 if (!silc_idcache_find_by_name_one(id_list->channels, name, &id_cache))
674 *ret_entry = id_cache;
676 SILC_LOG_DEBUG(("Found"));
678 return id_cache->context;
681 /* Finds channel by Channel ID. */
684 silc_idlist_find_channel_by_id(SilcIDList id_list, SilcChannelID *id,
685 SilcIDCacheEntry *ret_entry)
687 SilcIDCacheEntry id_cache = NULL;
688 SilcChannelEntry channel;
693 SILC_LOG_DEBUG(("Channel ID (%s)",
694 silc_id_render(id, SILC_ID_CHANNEL)));
696 if (!silc_idcache_find_by_id_one(id_list->channels, (void *)id, &id_cache))
699 channel = (SilcChannelEntry)id_cache->context;
702 *ret_entry = id_cache;
704 SILC_LOG_DEBUG(("Found"));
709 /* Replaces old Channel ID with new one. This is done when router forces
710 normal server to change Channel ID. */
713 silc_idlist_replace_channel_id(SilcIDList id_list, SilcChannelID *old_id,
714 SilcChannelID *new_id)
716 SilcIDCacheEntry id_cache = NULL;
717 SilcChannelEntry channel;
719 if (!old_id || !new_id)
722 SILC_LOG_DEBUG(("Replacing Channel ID"));
724 if (!silc_idcache_find_by_id_one(id_list->channels, (void *)old_id,
728 channel = (SilcChannelEntry)id_cache->context;
730 /* Remove the old entry and add a new one */
732 silc_idcache_del_by_id(id_list->channels, (void *)channel->id);
734 silc_free(channel->id);
735 channel->id = new_id;
737 silc_idcache_add(id_list->channels, channel->channel_name, channel->id,
740 SILC_LOG_DEBUG(("Replaced"));
745 /* Returns channels from the ID list. If the `channel_id' is NULL then
746 all channels are returned. */
749 silc_idlist_get_channels(SilcIDList id_list, SilcChannelID *channel_id,
750 uint32 *channels_count)
752 SilcIDCacheList list = NULL;
753 SilcIDCacheEntry id_cache = NULL;
754 SilcChannelEntry *channels = NULL;
757 SILC_LOG_DEBUG(("Start"));
760 if (!silc_idcache_get_all(id_list->channels, &list))
763 channels = silc_calloc(silc_idcache_list_count(list), sizeof(*channels));
766 silc_idcache_list_first(list, &id_cache);
767 channels[i++] = (SilcChannelEntry)id_cache->context;
769 while (silc_idcache_list_next(list, &id_cache))
770 channels[i++] = (SilcChannelEntry)id_cache->context;
772 silc_idcache_list_free(list);
774 if (!silc_idcache_find_by_id_one(id_list->channels, channel_id, &id_cache))
778 channels = silc_calloc(1, sizeof(*channels));
779 channels[0] = (SilcChannelEntry)id_cache->context;