X-Git-Url: http://git.silcnet.org/gitweb/?a=blobdiff_plain;f=apps%2Fsilcd%2Fserverid.c;h=73641c65dd13449a7dce351be999dab4db56f420;hb=52e57c880aba9c5e89f59d962eb9af75670b76e0;hp=05cf1b35757d845041819ac146d5a2e0b7b29628;hpb=99865727f85eb96f5462158eed8dc935710d1a0b;p=silc.git diff --git a/apps/silcd/serverid.c b/apps/silcd/serverid.c index 05cf1b35..73641c65 100644 --- a/apps/silcd/serverid.c +++ b/apps/silcd/serverid.c @@ -1,16 +1,16 @@ /* - id.c + serverid.c - Author: Pekka Riikonen + Author: Pekka Riikonen - Copyright (C) 1997 - 2001 Pekka Riikonen + Copyright (C) 1997 - 2007 Pekka Riikonen This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. - + This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the @@ -25,7 +25,7 @@ /* Creates a Server ID. Newly created Server ID is returned to the new_id argument. */ -void silc_id_create_server_id(const char *ip, uint16 port, SilcRng rng, +void silc_id_create_server_id(const char *ip, SilcUInt16 port, SilcRng rng, SilcServerID **new_id) { SILC_LOG_DEBUG(("Creating new Server ID")); @@ -34,7 +34,7 @@ void silc_id_create_server_id(const char *ip, uint16 port, SilcRng rng, /* Create the ID */ - if (!silc_net_addr2bin(ip, (*new_id)->ip.data, + if (!silc_net_addr2bin(ip, (*new_id)->ip.data, sizeof((*new_id)->ip.data))) { silc_free(*new_id); *new_id = NULL; @@ -42,8 +42,8 @@ void silc_id_create_server_id(const char *ip, uint16 port, SilcRng rng, } (*new_id)->ip.data_len = silc_net_is_ip4(ip) ? 4 : 16; - (*new_id)->port = htons(port); - (*new_id)->rnd = silc_rng_get_rn16(rng); + (*new_id)->port = SILC_SWAB_16(port); + (*new_id)->rnd = 0xff; SILC_LOG_DEBUG(("New ID (%s)", silc_id_render(*new_id, SILC_ID_SERVER))); } @@ -54,20 +54,22 @@ void silc_id_create_server_id(const char *ip, uint16 port, SilcRng rng, caller must recall the function with different nickname. If this returns TRUE the new ID was created successfully. */ -bool silc_id_create_client_id(SilcServer server, - SilcServerID *server_id, SilcRng rng, - SilcHash md5hash, char *nickname, - SilcClientID **new_id) +SilcBool silc_id_create_client_id(SilcServer server, + SilcServerID *server_id, SilcRng rng, + SilcHash md5hash, + unsigned char *nickname, SilcUInt32 nick_len, + SilcClientID **new_id) { unsigned char hash[16]; - bool finding = FALSE; + SilcBool finding = FALSE; SILC_LOG_DEBUG(("Creating new Client ID")); *new_id = silc_calloc(1, sizeof(**new_id)); - /* Create hash of the nickanem */ - silc_hash_make(md5hash, nickname, strlen(nickname), hash); + /* Create hash of the nickname (it's already checked as valid identifier + string). */ + silc_hash_make(md5hash, nickname, nick_len, hash); /* Create the ID */ memcpy((*new_id)->ip.data, server_id->ip.data, server_id->ip.data_len); @@ -77,9 +79,9 @@ bool silc_id_create_client_id(SilcServer server, /* Assure that the ID does not exist already */ while (1) { - if (!silc_idlist_find_client_by_id(server->local_list, + if (!silc_idlist_find_client_by_id(server->local_list, *new_id, FALSE, NULL)) - if (!silc_idlist_find_client_by_id(server->global_list, + if (!silc_idlist_find_client_by_id(server->global_list, *new_id, FALSE, NULL)) break; @@ -105,11 +107,11 @@ bool silc_id_create_client_id(SilcServer server, /* Creates Channel ID */ -bool silc_id_create_channel_id(SilcServer server, - SilcServerID *router_id, SilcRng rng, - SilcChannelID **new_id) +SilcBool silc_id_create_channel_id(SilcServer server, + SilcServerID *router_id, SilcRng rng, + SilcChannelID **new_id) { - bool finding = TRUE; + SilcBool finding = TRUE; SILC_LOG_DEBUG(("Creating new Channel ID")); @@ -123,15 +125,15 @@ bool silc_id_create_channel_id(SilcServer server, /* Assure that the ID does not exist already */ while (1) { - if (!silc_idlist_find_channel_by_id(server->local_list, + if (!silc_idlist_find_channel_by_id(server->local_list, *new_id, NULL)) break; (*new_id)->rnd++; - + if (finding && (*new_id)->rnd == 0) return FALSE; - + if (!finding) { (*new_id)->rnd = 0; finding = TRUE; @@ -142,3 +144,29 @@ bool silc_id_create_channel_id(SilcServer server, return TRUE; } + +/* Checks whether the `server_id' is valid. It must be based to the + IP address provided in the `remote' socket connection. */ + +SilcBool silc_id_is_valid_server_id(SilcServer server, + SilcServerID *server_id, + SilcPacketStream remote) +{ + unsigned char ip[16]; + const char *remote_ip; + SilcStream stream = silc_packet_stream_get_stream(remote); + + silc_socket_stream_get_info(stream, NULL, NULL, &remote_ip, NULL); + if (!silc_net_addr2bin(remote_ip, ip, sizeof(ip))) + return FALSE; + + if (silc_net_is_ip4(remote_ip)) { + if (!memcmp(server_id->ip.data, ip, 4)) + return TRUE; + } else { + if (!memcmp(server_id->ip.data, ip, 16)) + return TRUE; + } + + return FALSE; +}