The Battle for Wesnoth  1.15.0+dev
user_handler.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2008 - 2018 by Thomas Baumhauer <thomas.baumhauer@NOSPAMgmail.com>
3  Part of the Battle for Wesnoth Project https://www.wesnoth.org/
4 
5  This program is free software; you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation; either version 2 of the License, or
8  (at your option) any later version.
9  This program is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY.
11 
12  See the COPYING file for more details.
13 */
14 
15 #include "server/user_handler.hpp"
16 #include "config.hpp"
17 #include "random.hpp"
18 #include "serialization/base64.hpp"
19 
20 #ifndef __APPLE__
21 #include <openssl/rand.h>
22 #else
23 #include <cstdlib>
24 #endif
25 
26 #include <array>
27 #include <ctime>
28 #include <sstream>
29 
30 bool user_handler::send_mail(const std::string& to_user,
31  const std::string& /*subject*/, const std::string& /*message*/) {
32 
33  //If this user is registered at all
34  if(!user_exists(to_user)) {
35  throw error("Could not send email. No user with the name '" + to_user + "' exists.");
36  }
37 
38  // If this user did not provide an email
39  if(get_mail(to_user).empty()) {
40  throw error("Could not send email. The email address of the user '" + to_user + "' is empty.");
41  }
42 
43  throw user_handler::error("This server is configured not to send email.");
44 }
45 
47 }
48 
49 std::string user_handler::create_unsecure_nonce(int length) {
50  srand(static_cast<unsigned>(std::time(nullptr)));
51 
52  std::stringstream ss;
53 
54  for(int i = 0; i < length; i++) {
56  }
57 
58  return ss.str();
59 }
60 
61 #ifndef __APPLE__
62 namespace {
63  class RAND_bytes_exception: public std::exception
64  {
65  };
66 }
67 #endif
68 
70 {
71  // Must be full base64 encodings (3 bytes = 4 chars) else we skew the PRNG results
72  std::array<unsigned char, (3 * 32) / 4> buf;
73 
74 #ifndef __APPLE__
75  if(!RAND_bytes(buf.data(), buf.size())) {
76  throw RAND_bytes_exception();
77  }
78 #else
79  arc4random_buf(buf.data(), buf.size());
80 #endif
81 
82  return base64::encode({buf.data(), buf.size()});
83 }
84 
std::string create_unsecure_nonce(int length=8)
Create a random string of digits for password encryption.
std::string encode(utils::byte_string_view bytes)
Definition: base64.cpp:219
virtual std::string get_mail(const std::string &user)=0
Used in send_mail().
static l_noret error(LoadState *S, const char *why)
Definition: lundump.cpp:39
bool send_mail(const std::string &to_user, const std::string &subject, const std::string &message)
Sends an email to the specified address.
Definitions for the interface to Wesnoth Markup Language (WML).
void init_mailer(const config &c)
Initiate the mailer object.
virtual bool user_exists(const std::string &name)=0
Returns true if a user with the given name exists.
std::size_t i
Definition: function.cpp:933
int get_random_int(int min, int max)
This helper method provides a random int from the underlying generator, using results of next_random...
Definition: random.hpp:51
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:92
static rng & default_instance()
Definition: random.cpp:73
std::string create_secure_nonce()