From 086790d6331357022f4da17c76b26b9fc6e2ad90 Mon Sep 17 00:00:00 2001 From: Adam Date: Mon, 13 Feb 2012 00:10:45 -0500 Subject: [PATCH] Removed our RNG and just use the systems, it's not very widely used. Also made DNS query ids not random as they don't need to be. --- data/example.conf | 15 ---- include/config.h | 5 -- include/dns.h | 2 +- include/extern.h | 6 -- modules/commands/ns_register.cpp | 2 +- modules/commands/ns_resetpass.cpp | 2 +- modules/commands/ns_set_email.cpp | 2 +- modules/encryption/enc_sha256.cpp | 2 +- src/config.cpp | 7 -- src/dns.cpp | 15 ++-- src/init.cpp | 3 +- src/misc.cpp | 143 ------------------------------ src/operserv.cpp | 2 +- src/users.cpp | 2 +- 14 files changed, 17 insertions(+), 191 deletions(-) diff --git a/data/example.conf b/data/example.conf index 8e271dc87..73c6627a3 100644 --- a/data/example.conf +++ b/data/example.conf @@ -281,21 +281,6 @@ options */ passlen = 32 - /* - * These keys are used to initiate the random number generator. These numbers - * MUST be random as you want your passcodes to be random. Don't give these - * keys to anyone! Keep them private! - * - * NOTE: If you don't uncomment these or keep their default values, any talented - * programmer would be able to easily "guess" random strings used to mask - * information. Be safe, and come up with three different 7-digit numbers. - * - * These directives are optional, but highly recommended. - */ - #userkey1 = 9866235 - #userkey2 = 8362013 - #userkey3 = 2362899 - /* * Allows Services to continue file write operations (i.e. database saving) * even if the original file cannot be backed up. Enabling this option may diff --git a/include/config.h b/include/config.h index cb0029c0d..0482aac69 100644 --- a/include/config.h +++ b/include/config.h @@ -625,11 +625,6 @@ class CoreExport ServerConfig /* List of modules to autoload */ std::list ModulesAutoLoad; - /* User keys to use for generating random hashes for pass codes etc */ - unsigned long UserKey1; - unsigned long UserKey2; - unsigned long UserKey3; - /* Numeric */ Anope::string Numeric; /* Array of ulined servers */ diff --git a/include/dns.h b/include/dns.h index 23e779b67..7d668cda7 100644 --- a/include/dns.h +++ b/include/dns.h @@ -143,7 +143,7 @@ class CoreExport DNSManager : public Timer, public Socket sockaddrs addrs; public: std::deque packets; - std::map requests; + std::map requests; static const int DNSPort = 53; diff --git a/include/extern.h b/include/extern.h index c3f89f3f6..0670f285c 100644 --- a/include/extern.h +++ b/include/extern.h @@ -183,12 +183,6 @@ E Anope::string myStrGetTokenRemainder(const Anope::string &str, char dilim, int E int myNumToken(const Anope::string &str, char dilim); E bool nickIsServices(const Anope::string &nick, bool bot); -E void add_entropy_userkeys(); -E void rand_init(); -E unsigned char getrandom8(); -E uint16_t getrandom16(); -E uint32_t getrandom32(); - E std::list BuildStringList(const Anope::string &, char = ' '); E std::vector BuildStringVector(const Anope::string &, char = ' '); diff --git a/modules/commands/ns_register.cpp b/modules/commands/ns_register.cpp index 3010c0eb4..341397c67 100644 --- a/modules/commands/ns_register.cpp +++ b/modules/commands/ns_register.cpp @@ -337,7 +337,7 @@ static bool SendRegmail(User *u, NickAlias *na, BotInfo *bi) }; int idx, min = 1, max = 62; for (idx = 0; idx < 9; ++idx) - codebuf += chars[1 + static_cast((static_cast(max - min)) * getrandom16() / 65536.0) + min]; + codebuf += chars[1 + static_cast((static_cast(max - min)) * static_cast(rand()) / 65536.0) + min]; na->nc->Extend("ns_register_passcode", new ExtensibleString(codebuf)); } else diff --git a/modules/commands/ns_resetpass.cpp b/modules/commands/ns_resetpass.cpp index ab357f484..f3aa47630 100644 --- a/modules/commands/ns_resetpass.cpp +++ b/modules/commands/ns_resetpass.cpp @@ -132,7 +132,7 @@ static bool SendResetEmail(User *u, NickAlias *na, BotInfo *bi) Anope::string passcode; int idx; for (idx = 0; idx < 20; ++idx) - passcode += chars[1 + static_cast((static_cast(max - min)) * getrandom16() / 65536.0) + min]; + passcode += chars[1 + static_cast((static_cast(max - min)) * static_cast(rand()) / 65536.0) + min]; Anope::string subject = translate(na->nc, Config->MailResetSubject.c_str()); Anope::string message = translate(na->nc, Config->MailResetMessage.c_str()); diff --git a/modules/commands/ns_set_email.cpp b/modules/commands/ns_set_email.cpp index 6a4ece4ca..8ce71165e 100644 --- a/modules/commands/ns_set_email.cpp +++ b/modules/commands/ns_set_email.cpp @@ -25,7 +25,7 @@ static bool SendConfirmMail(User *u, BotInfo *bi) int idx, min = 1, max = 62; Anope::string code; for (idx = 0; idx < 9; ++idx) - code += chars[1 + static_cast((static_cast(max - min)) * getrandom16() / 65536.0) + min]; + code += chars[1 + static_cast((static_cast(max - min)) * static_cast(rand()) / 65536.0) + min]; u->Account()->Extend("ns_set_email_passcode", new ExtensibleString(code)); Anope::string subject = Config->MailEmailchangeSubject; diff --git a/modules/encryption/enc_sha256.cpp b/modules/encryption/enc_sha256.cpp index 4e9d50b96..d77cd072d 100644 --- a/modules/encryption/enc_sha256.cpp +++ b/modules/encryption/enc_sha256.cpp @@ -128,7 +128,7 @@ class ESHA256 : public Module void NewRandomIV() { for (int i = 0; i < 8; ++i) - iv[i] = getrandom32(); + iv[i] = static_cast(rand()); } /* returns the IV as base64-encrypted string */ diff --git a/src/config.cpp b/src/config.cpp index 952da263c..d35399896 100644 --- a/src/config.cpp +++ b/src/config.cpp @@ -154,10 +154,6 @@ ServerConfig::ServerConfig() : config_data(), NSDefFlags(NickCoreFlagStrings), C this->SessionAutoKillExpiry = 1800; /* 30 minutes */ } - /* Check the user keys */ - if (this->UserKey1 == this->UserKey2 || this->UserKey1 == this->UserKey3 || this->UserKey3 == this->UserKey2) - Log() << "Every UserKey must be different. It's for YOUR safety! Remember that!"; - SetDefaultMLock(this); if (IsFile(this->NameServer)) @@ -1132,9 +1128,6 @@ ConfigItems::ConfigItems(ServerConfig *conf) {"networkinfo", "userlen", "10", new ValueContainerUInt(&conf->UserLen), DT_UINTEGER | DT_NORELOAD, NoValidation}, {"networkinfo", "hostlen", "64", new ValueContainerUInt(&conf->HostLen), DT_UINTEGER | DT_NORELOAD, NoValidation}, {"options", "passlen", "32", new ValueContainerUInt(&conf->PassLen), DT_UINTEGER | DT_NORELOAD, NoValidation}, - {"options", "userkey1", "0", new ValueContainerLUInt(&conf->UserKey1), DT_LUINTEGER, NoValidation}, - {"options", "userkey2", "0", new ValueContainerLUInt(&conf->UserKey2), DT_LUINTEGER, NoValidation}, - {"options", "userkey3", "0", new ValueContainerLUInt(&conf->UserKey3), DT_LUINTEGER, NoValidation}, {"options", "nobackupokay", "no", new ValueContainerBool(&conf->NoBackupOkay), DT_BOOLEAN, NoValidation}, {"options", "strictpasswords", "no", new ValueContainerBool(&conf->StrictPasswords), DT_BOOLEAN, NoValidation}, {"options", "badpasslimit", "0", new ValueContainerUInt(&conf->BadPassLimit), DT_UINTEGER, NoValidation}, diff --git a/src/dns.cpp b/src/dns.cpp index 02df2aea4..fc1ab37df 100644 --- a/src/dns.cpp +++ b/src/dns.cpp @@ -52,8 +52,11 @@ DNSRequest::DNSRequest(const Anope::string &addr, QueryType qt, bool cache, Modu throw SocketException("DNS queue full"); do - this->id = getrandom16(); - while (!this->id || DNSEngine->requests.count(this->id)); + { + static unsigned short cur_id = 0; + this->id = cur_id++; + } + while (DNSEngine->requests.count(this->id)); DNSEngine->requests[this->id] = this; } @@ -457,7 +460,7 @@ DNSManager::~DNSManager() delete this->packets[i - 1]; this->packets.clear(); - for (std::map::iterator it = this->requests.begin(), it_end = this->requests.end(); it != it_end; ++it) + for (std::map::iterator it = this->requests.begin(), it_end = this->requests.end(); it != it_end; ++it) { DNSRequest *request = it->second; @@ -504,7 +507,7 @@ bool DNSManager::ProcessRead() return true; } - std::map::iterator it = DNSEngine->requests.find(recv_packet.id); + std::map::iterator it = DNSEngine->requests.find(recv_packet.id); if (it == DNSEngine->requests.end()) { Log(LOG_DEBUG_2) << "Resolver: Received an answer for something we didn't request"; @@ -652,9 +655,9 @@ void DNSManager::Tick(time_t now) void DNSManager::Cleanup(Module *mod) { - for (std::map::iterator it = this->requests.begin(), it_end = this->requests.end(); it != it_end;) + for (std::map::iterator it = this->requests.begin(), it_end = this->requests.end(); it != it_end;) { - short id = it->first; + unsigned short id = it->first; DNSRequest *req = it->second; ++it; diff --git a/src/init.cpp b/src/init.cpp index a9cbcc03a..2a87ade9c 100644 --- a/src/init.cpp +++ b/src/init.cpp @@ -459,8 +459,7 @@ void Init(int ac, char **av) InitLanguages(); /* Initialize random number generator */ - rand_init(); - add_entropy_userkeys(); + srand(time(NULL)); /* load modules */ Log() << "Loading modules..."; diff --git a/src/misc.cpp b/src/misc.cpp index 02b886d75..524095d88 100644 --- a/src/misc.cpp +++ b/src/misc.cpp @@ -27,13 +27,6 @@ void ExtensibleItem::OnDelete() delete this; } -struct arc4_stream -{ - uint8_t i; - uint8_t j; - uint8_t s[256]; -} rs; - /*************************************************************************/ /** Check if a file exists @@ -519,142 +512,6 @@ bool nickIsServices(const Anope::string &tempnick, bool bot) /*************************************************************************/ -/** - * arc4 init - * @return void - */ -static void arc4_init() -{ - for (int n = 0; n < 256; ++n) - rs.s[n] = n; - rs.i = 0; - rs.j = 0; -} - -/*************************************************************************/ - -/** - * arc4 addrandom - * @param data - * @param dalen Data Length - * @return void - */ -static void arc4_addrandom(void *dat, int datlen) -{ - --rs.i; - for (int n = 0; n < 256; ++n) - { - ++rs.i; - uint8_t si = rs.s[rs.i]; - rs.j = rs.j + si + (static_cast(dat))[n % datlen]; - rs.s[rs.i] = rs.s[rs.j]; - rs.s[rs.j] = si; - } -} - -/*************************************************************************/ - -/** - * random init - * @return void - */ -void rand_init() -{ - struct - { -#ifndef _WIN32 - struct timeval nowt; /* time */ - char rnd[32]; /* /dev/urandom */ -#else - MEMORYSTATUS mstat; /* memory status */ - struct _timeb nowt; /* time */ -#endif - } rdat; - - arc4_init(); - - /* Grab OS specific "random" data */ -#ifndef _WIN32 - /* unix/bsd: time */ - gettimeofday(&rdat.nowt, NULL); - /* unix/bsd: /dev/urandom */ - int fd = open("/dev/urandom", O_RDONLY); - if (fd) - { - read(fd, &rdat.rnd, sizeof(rdat.rnd)); - close(fd); - } -#else - /* win32: time */ - _ftime(&rdat.nowt); - /* win32: memory status */ - GlobalMemoryStatus(&rdat.mstat); -#endif - - arc4_addrandom(&rdat, sizeof(rdat)); -} - -/*************************************************************************/ - -/** - * Setup the random numbers - * @return void - */ -void add_entropy_userkeys() -{ - arc4_addrandom(&Config->UserKey1, sizeof(Config->UserKey1)); - arc4_addrandom(&Config->UserKey2, sizeof(Config->UserKey2)); - arc4_addrandom(&Config->UserKey3, sizeof(Config->UserKey3)); - /* UserKey3 is also used in mysql_rand() */ -} - -/*************************************************************************/ - -/** - * Get the random numbers 8 byte deep - * @return char - */ -unsigned char getrandom8() -{ - ++rs.i; - unsigned char si = rs.s[rs.i]; - rs.j += si; - unsigned char sj = rs.s[rs.j]; - rs.s[rs.i] = sj; - rs.s[rs.j] = si; - return rs.s[(si + sj) & 0xff]; -} - -/*************************************************************************/ - -/** - * Get the random numbers 16 byte deep - * @return char - */ -uint16_t getrandom16() -{ - uint16_t val = getrandom8() << 8; - val |= getrandom8(); - return val; -} - -/*************************************************************************/ - -/** - * Get the random numbers 32 byte deep - * @return char - */ -uint32_t getrandom32() -{ - uint32_t val = getrandom8() << 24; - val |= getrandom8() << 16; - val |= getrandom8() << 8; - val |= getrandom8(); - return val; -} - -/*************************************************************************/ - /** * Number of tokens in a string * @param str String diff --git a/src/operserv.cpp b/src/operserv.cpp index 6793581e0..e32937797 100644 --- a/src/operserv.cpp +++ b/src/operserv.cpp @@ -162,7 +162,7 @@ Anope::string XLineManager::GenerateUID() { char c; do - c = getrandom8(); + c = (random() % 75) + 48; while (!isupper(c) && !isdigit(c)); id += c; } diff --git a/src/users.cpp b/src/users.cpp index c5f8351a4..f15754b28 100644 --- a/src/users.cpp +++ b/src/users.cpp @@ -321,7 +321,7 @@ void User::Collide(NickAlias *na) int i = 0; do { - guestnick = Config->NSGuestNickPrefix + stringify(getrandom16()); + guestnick = Config->NSGuestNickPrefix + stringify(static_cast(rand())); } while (finduser(guestnick) && i++ < 10); if (i == 11)