rust/hedgewars-server/src/server/handlers/loggingin.rs
author alfadur <mail@none>
Wed, 06 Feb 2019 00:14:04 +0300
changeset 14707 9f98086de1b6
parent 14704 932ff7683653
child 14711 f61ce544d436
permissions -rw-r--r--
Server action refactoring part 9 of N
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
12147
03ccb89820f3 Room creation halfplemented
unc0rr
parents:
diff changeset
     1
use mio;
03ccb89820f3 Room creation halfplemented
unc0rr
parents:
diff changeset
     2
13666
09f4a30e50cc Rust 2018 conversion
alfadur
parents: 13450
diff changeset
     3
use crate::{
14478
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14436
diff changeset
     4
    protocol::messages::{HWProtocolMessage, HWServerMessage::*},
13666
09f4a30e50cc Rust 2018 conversion
alfadur
parents: 13450
diff changeset
     5
    server::{
14478
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14436
diff changeset
     6
        actions::{Action, Action::*},
13774
0118b7412570 use openssl for password message handling
alfadur
parents: 13771
diff changeset
     7
        client::HWClient,
14395
e5db279308d7 dispose of server mods
alfadur
parents: 13810
diff changeset
     8
        core::HWServer,
13666
09f4a30e50cc Rust 2018 conversion
alfadur
parents: 13450
diff changeset
     9
        coretypes::ClientId,
09f4a30e50cc Rust 2018 conversion
alfadur
parents: 13450
diff changeset
    10
    },
14478
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14436
diff changeset
    11
    utils::is_name_illegal,
13419
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    12
};
14478
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14436
diff changeset
    13
use log::*;
13774
0118b7412570 use openssl for password message handling
alfadur
parents: 13771
diff changeset
    14
#[cfg(feature = "official-server")]
0118b7412570 use openssl for password message handling
alfadur
parents: 13771
diff changeset
    15
use openssl::sha::sha1;
0118b7412570 use openssl for password message handling
alfadur
parents: 13771
diff changeset
    16
use std::fmt::{Formatter, LowerHex};
0118b7412570 use openssl for password message handling
alfadur
parents: 13771
diff changeset
    17
0118b7412570 use openssl for password message handling
alfadur
parents: 13771
diff changeset
    18
#[derive(PartialEq)]
0118b7412570 use openssl for password message handling
alfadur
parents: 13771
diff changeset
    19
struct Sha1Digest([u8; 20]);
0118b7412570 use openssl for password message handling
alfadur
parents: 13771
diff changeset
    20
0118b7412570 use openssl for password message handling
alfadur
parents: 13771
diff changeset
    21
impl LowerHex for Sha1Digest {
0118b7412570 use openssl for password message handling
alfadur
parents: 13771
diff changeset
    22
    fn fmt(&self, f: &mut Formatter) -> Result<(), std::fmt::Error> {
0118b7412570 use openssl for password message handling
alfadur
parents: 13771
diff changeset
    23
        for byte in &self.0 {
0118b7412570 use openssl for password message handling
alfadur
parents: 13771
diff changeset
    24
            write!(f, "{:02x}", byte)?;
0118b7412570 use openssl for password message handling
alfadur
parents: 13771
diff changeset
    25
        }
0118b7412570 use openssl for password message handling
alfadur
parents: 13771
diff changeset
    26
        Ok(())
0118b7412570 use openssl for password message handling
alfadur
parents: 13771
diff changeset
    27
    }
0118b7412570 use openssl for password message handling
alfadur
parents: 13771
diff changeset
    28
}
0118b7412570 use openssl for password message handling
alfadur
parents: 13771
diff changeset
    29
0118b7412570 use openssl for password message handling
alfadur
parents: 13771
diff changeset
    30
#[cfg(feature = "official-server")]
0118b7412570 use openssl for password message handling
alfadur
parents: 13771
diff changeset
    31
fn get_hash(client: &HWClient, salt1: &str, salt2: &str) -> Sha1Digest {
14478
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14436
diff changeset
    32
    let s = format!(
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14436
diff changeset
    33
        "{}{}{}{}{}",
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14436
diff changeset
    34
        salt1, salt2, client.web_password, client.protocol_number, "!hedgewars"
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14436
diff changeset
    35
    );
13774
0118b7412570 use openssl for password message handling
alfadur
parents: 13771
diff changeset
    36
    Sha1Digest(sha1(s.as_bytes()))
0118b7412570 use openssl for password message handling
alfadur
parents: 13771
diff changeset
    37
}
12147
03ccb89820f3 Room creation halfplemented
unc0rr
parents:
diff changeset
    38
14692
455865ccd36c Server action refactoring part 2 of N
alfadur <mail@none>
parents: 14478
diff changeset
    39
pub fn handle(
455865ccd36c Server action refactoring part 2 of N
alfadur <mail@none>
parents: 14478
diff changeset
    40
    server: &mut HWServer,
455865ccd36c Server action refactoring part 2 of N
alfadur <mail@none>
parents: 14478
diff changeset
    41
    client_id: ClientId,
455865ccd36c Server action refactoring part 2 of N
alfadur <mail@none>
parents: 14478
diff changeset
    42
    response: &mut super::Response,
455865ccd36c Server action refactoring part 2 of N
alfadur <mail@none>
parents: 14478
diff changeset
    43
    message: HWProtocolMessage,
455865ccd36c Server action refactoring part 2 of N
alfadur <mail@none>
parents: 14478
diff changeset
    44
) {
12147
03ccb89820f3 Room creation halfplemented
unc0rr
parents:
diff changeset
    45
    match message {
13416
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
    46
        HWProtocolMessage::Nick(nick) => {
13666
09f4a30e50cc Rust 2018 conversion
alfadur
parents: 13450
diff changeset
    47
            let client = &mut server.clients[client_id];
09f4a30e50cc Rust 2018 conversion
alfadur
parents: 13450
diff changeset
    48
            debug!("{} {}", nick, is_name_illegal(&nick));
14693
6e6632068a33 Server action refactoring part 3 of N
alfadur <mail@none>
parents: 14692
diff changeset
    49
            if client.room_id != None {
13666
09f4a30e50cc Rust 2018 conversion
alfadur
parents: 13450
diff changeset
    50
                unreachable!()
14478
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14436
diff changeset
    51
            } else if !client.nick.is_empty() {
14693
6e6632068a33 Server action refactoring part 3 of N
alfadur <mail@none>
parents: 14692
diff changeset
    52
                response.add(Error("Nickname already provided.".to_string()).send_self());
14478
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14436
diff changeset
    53
            } else if is_name_illegal(&nick) {
14694
08a8605bafaf Server action refactoring part 4 of N
alfadur <mail@none>
parents: 14693
diff changeset
    54
                super::common::remove_client(server, response, "Illegal nickname! Nicknames must be between 1-40 characters long, must not have a trailing or leading space and must not have any of these characters: $()*+?[]^{|}".to_string())
14478
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14436
diff changeset
    55
            } else {
13666
09f4a30e50cc Rust 2018 conversion
alfadur
parents: 13450
diff changeset
    56
                client.nick = nick.clone();
14693
6e6632068a33 Server action refactoring part 3 of N
alfadur <mail@none>
parents: 14692
diff changeset
    57
                response.add(Nick(nick).send_self());
14694
08a8605bafaf Server action refactoring part 4 of N
alfadur <mail@none>
parents: 14693
diff changeset
    58
08a8605bafaf Server action refactoring part 4 of N
alfadur <mail@none>
parents: 14693
diff changeset
    59
                if client.protocol_number > 0 {
14704
932ff7683653 Server action refactoring part 8 of N
alfadur <mail@none>
parents: 14694
diff changeset
    60
                    super::common::process_login(server, response);
14694
08a8605bafaf Server action refactoring part 4 of N
alfadur <mail@none>
parents: 14693
diff changeset
    61
                }
08a8605bafaf Server action refactoring part 4 of N
alfadur <mail@none>
parents: 14693
diff changeset
    62
            }
08a8605bafaf Server action refactoring part 4 of N
alfadur <mail@none>
parents: 14693
diff changeset
    63
        }
08a8605bafaf Server action refactoring part 4 of N
alfadur <mail@none>
parents: 14693
diff changeset
    64
        HWProtocolMessage::Proto(proto) => {
08a8605bafaf Server action refactoring part 4 of N
alfadur <mail@none>
parents: 14693
diff changeset
    65
            let client = &mut server.clients[client_id];
08a8605bafaf Server action refactoring part 4 of N
alfadur <mail@none>
parents: 14693
diff changeset
    66
            if client.protocol_number != 0 {
08a8605bafaf Server action refactoring part 4 of N
alfadur <mail@none>
parents: 14693
diff changeset
    67
                response.add(Error("Protocol already known.".to_string()).send_self());
08a8605bafaf Server action refactoring part 4 of N
alfadur <mail@none>
parents: 14693
diff changeset
    68
            } else if proto == 0 {
08a8605bafaf Server action refactoring part 4 of N
alfadur <mail@none>
parents: 14693
diff changeset
    69
                response.add(Error("Bad number.".to_string()).send_self());
08a8605bafaf Server action refactoring part 4 of N
alfadur <mail@none>
parents: 14693
diff changeset
    70
            } else {
08a8605bafaf Server action refactoring part 4 of N
alfadur <mail@none>
parents: 14693
diff changeset
    71
                client.protocol_number = proto;
08a8605bafaf Server action refactoring part 4 of N
alfadur <mail@none>
parents: 14693
diff changeset
    72
                response.add(Proto(proto).send_self());
08a8605bafaf Server action refactoring part 4 of N
alfadur <mail@none>
parents: 14693
diff changeset
    73
08a8605bafaf Server action refactoring part 4 of N
alfadur <mail@none>
parents: 14693
diff changeset
    74
                if client.nick != "" {
14704
932ff7683653 Server action refactoring part 8 of N
alfadur <mail@none>
parents: 14694
diff changeset
    75
                    super::common::process_login(server, response);
14694
08a8605bafaf Server action refactoring part 4 of N
alfadur <mail@none>
parents: 14693
diff changeset
    76
                }
14693
6e6632068a33 Server action refactoring part 3 of N
alfadur <mail@none>
parents: 14692
diff changeset
    77
            }
13771
4664da990556 Add official server feature to cargo
alfadur
parents: 13666
diff changeset
    78
        }
4664da990556 Add official server feature to cargo
alfadur
parents: 13666
diff changeset
    79
        #[cfg(feature = "official-server")]
13774
0118b7412570 use openssl for password message handling
alfadur
parents: 13771
diff changeset
    80
        HWProtocolMessage::Password(hash, salt) => {
0118b7412570 use openssl for password message handling
alfadur
parents: 13771
diff changeset
    81
            let c = &server.clients[client_id];
0118b7412570 use openssl for password message handling
alfadur
parents: 13771
diff changeset
    82
0118b7412570 use openssl for password message handling
alfadur
parents: 13771
diff changeset
    83
            let client_hash = get_hash(c, &salt, &c.server_salt);
0118b7412570 use openssl for password message handling
alfadur
parents: 13771
diff changeset
    84
            let server_hash = get_hash(c, &c.server_salt, &salt);
14693
6e6632068a33 Server action refactoring part 3 of N
alfadur <mail@none>
parents: 14692
diff changeset
    85
            if client_hash == server_hash {
6e6632068a33 Server action refactoring part 3 of N
alfadur <mail@none>
parents: 14692
diff changeset
    86
                response.add(ServerAuth(format!("{:x}", server_hash)).send_self());
14704
932ff7683653 Server action refactoring part 8 of N
alfadur <mail@none>
parents: 14694
diff changeset
    87
            //TODO enter lobby
13774
0118b7412570 use openssl for password message handling
alfadur
parents: 13771
diff changeset
    88
            } else {
14694
08a8605bafaf Server action refactoring part 4 of N
alfadur <mail@none>
parents: 14693
diff changeset
    89
                super::common::remove_client(server, response, "Authentication failed".to_string())
14707
9f98086de1b6 Server action refactoring part 9 of N
alfadur <mail@none>
parents: 14704
diff changeset
    90
            }
14693
6e6632068a33 Server action refactoring part 3 of N
alfadur <mail@none>
parents: 14692
diff changeset
    91
        }
13774
0118b7412570 use openssl for password message handling
alfadur
parents: 13771
diff changeset
    92
        #[cfg(feature = "official-server")]
13771
4664da990556 Add official server feature to cargo
alfadur
parents: 13666
diff changeset
    93
        HWProtocolMessage::Checker(protocol, nick, password) => {
4664da990556 Add official server feature to cargo
alfadur
parents: 13666
diff changeset
    94
            let c = &mut server.clients[client_id];
4664da990556 Add official server feature to cargo
alfadur
parents: 13666
diff changeset
    95
            c.nick = nick;
4664da990556 Add official server feature to cargo
alfadur
parents: 13666
diff changeset
    96
            c.web_password = password;
4664da990556 Add official server feature to cargo
alfadur
parents: 13666
diff changeset
    97
            c.set_is_checker(true);
4664da990556 Add official server feature to cargo
alfadur
parents: 13666
diff changeset
    98
        }
12147
03ccb89820f3 Room creation halfplemented
unc0rr
parents:
diff changeset
    99
        _ => warn!("Incorrect command in logging-in state"),
03ccb89820f3 Room creation halfplemented
unc0rr
parents:
diff changeset
   100
    }
03ccb89820f3 Room creation halfplemented
unc0rr
parents:
diff changeset
   101
}