gameServer2/src/server/handlers/loggingin.rs
author alfadur
Fri, 07 Sep 2018 04:16:05 +0300
changeset 13774 0118b7412570
parent 13771 4664da990556
child 13810 0463a4221327
permissions -rw-r--r--
use openssl for password message handling
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::{
09f4a30e50cc Rust 2018 conversion
alfadur
parents: 13450
diff changeset
     4
    server::{
13774
0118b7412570 use openssl for password message handling
alfadur
parents: 13771
diff changeset
     5
        client::HWClient,
13666
09f4a30e50cc Rust 2018 conversion
alfadur
parents: 13450
diff changeset
     6
        server::HWServer,
09f4a30e50cc Rust 2018 conversion
alfadur
parents: 13450
diff changeset
     7
        coretypes::ClientId,
09f4a30e50cc Rust 2018 conversion
alfadur
parents: 13450
diff changeset
     8
        actions::{Action, Action::*}
09f4a30e50cc Rust 2018 conversion
alfadur
parents: 13450
diff changeset
     9
    },
09f4a30e50cc Rust 2018 conversion
alfadur
parents: 13450
diff changeset
    10
    protocol::messages::{
09f4a30e50cc Rust 2018 conversion
alfadur
parents: 13450
diff changeset
    11
        HWProtocolMessage, HWServerMessage::*
09f4a30e50cc Rust 2018 conversion
alfadur
parents: 13450
diff changeset
    12
    },
09f4a30e50cc Rust 2018 conversion
alfadur
parents: 13450
diff changeset
    13
    utils::is_name_illegal
13419
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    14
};
13774
0118b7412570 use openssl for password message handling
alfadur
parents: 13771
diff changeset
    15
#[cfg(feature = "official-server")]
0118b7412570 use openssl for password message handling
alfadur
parents: 13771
diff changeset
    16
use openssl::sha::sha1;
0118b7412570 use openssl for password message handling
alfadur
parents: 13771
diff changeset
    17
use std::fmt::{Formatter, LowerHex};
0118b7412570 use openssl for password message handling
alfadur
parents: 13771
diff changeset
    18
0118b7412570 use openssl for password message handling
alfadur
parents: 13771
diff changeset
    19
#[derive(PartialEq)]
0118b7412570 use openssl for password message handling
alfadur
parents: 13771
diff changeset
    20
struct Sha1Digest([u8; 20]);
0118b7412570 use openssl for password message handling
alfadur
parents: 13771
diff changeset
    21
0118b7412570 use openssl for password message handling
alfadur
parents: 13771
diff changeset
    22
impl LowerHex for Sha1Digest {
0118b7412570 use openssl for password message handling
alfadur
parents: 13771
diff changeset
    23
    fn fmt(&self, f: &mut Formatter) -> Result<(), std::fmt::Error> {
0118b7412570 use openssl for password message handling
alfadur
parents: 13771
diff changeset
    24
        for byte in &self.0 {
0118b7412570 use openssl for password message handling
alfadur
parents: 13771
diff changeset
    25
            write!(f, "{:02x}", byte)?;
0118b7412570 use openssl for password message handling
alfadur
parents: 13771
diff changeset
    26
        }
0118b7412570 use openssl for password message handling
alfadur
parents: 13771
diff changeset
    27
        Ok(())
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
0118b7412570 use openssl for password message handling
alfadur
parents: 13771
diff changeset
    31
#[cfg(feature = "official-server")]
0118b7412570 use openssl for password message handling
alfadur
parents: 13771
diff changeset
    32
fn get_hash(client: &HWClient, salt1: &str, salt2: &str) -> Sha1Digest {
0118b7412570 use openssl for password message handling
alfadur
parents: 13771
diff changeset
    33
    let s = format!("{}{}{}{}{}", salt1, salt2,
0118b7412570 use openssl for password message handling
alfadur
parents: 13771
diff changeset
    34
                    client.web_password, client.protocol_number, "!hedgewars");
0118b7412570 use openssl for password message handling
alfadur
parents: 13771
diff changeset
    35
    Sha1Digest(sha1(s.as_bytes()))
0118b7412570 use openssl for password message handling
alfadur
parents: 13771
diff changeset
    36
}
12147
03ccb89820f3 Room creation halfplemented
unc0rr
parents:
diff changeset
    37
13419
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    38
pub fn handle(server: & mut HWServer, client_id: ClientId, message: HWProtocolMessage) {
12147
03ccb89820f3 Room creation halfplemented
unc0rr
parents:
diff changeset
    39
    match message {
13416
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
    40
        HWProtocolMessage::Nick(nick) => {
13666
09f4a30e50cc Rust 2018 conversion
alfadur
parents: 13450
diff changeset
    41
            let client = &mut server.clients[client_id];
09f4a30e50cc Rust 2018 conversion
alfadur
parents: 13450
diff changeset
    42
            debug!("{} {}", nick, is_name_illegal(&nick));
09f4a30e50cc Rust 2018 conversion
alfadur
parents: 13450
diff changeset
    43
            let actions = if client.room_id != None {
09f4a30e50cc Rust 2018 conversion
alfadur
parents: 13450
diff changeset
    44
                unreachable!()
09f4a30e50cc Rust 2018 conversion
alfadur
parents: 13450
diff changeset
    45
            }
09f4a30e50cc Rust 2018 conversion
alfadur
parents: 13450
diff changeset
    46
            else if !client.nick.is_empty() {
09f4a30e50cc Rust 2018 conversion
alfadur
parents: 13450
diff changeset
    47
                vec![ProtocolError("Nickname already provided.".to_string())]
13416
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
    48
            }
13774
0118b7412570 use openssl for password message handling
alfadur
parents: 13771
diff changeset
    49
            else if is_name_illegal(&nick) {
13666
09f4a30e50cc Rust 2018 conversion
alfadur
parents: 13450
diff changeset
    50
                vec![ByeClient("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())]
09f4a30e50cc Rust 2018 conversion
alfadur
parents: 13450
diff changeset
    51
            }
09f4a30e50cc Rust 2018 conversion
alfadur
parents: 13450
diff changeset
    52
            else {
09f4a30e50cc Rust 2018 conversion
alfadur
parents: 13450
diff changeset
    53
                client.nick = nick.clone();
09f4a30e50cc Rust 2018 conversion
alfadur
parents: 13450
diff changeset
    54
                vec![Nick(nick).send_self().action(),
09f4a30e50cc Rust 2018 conversion
alfadur
parents: 13450
diff changeset
    55
                     CheckRegistered]
09f4a30e50cc Rust 2018 conversion
alfadur
parents: 13450
diff changeset
    56
            };
09f4a30e50cc Rust 2018 conversion
alfadur
parents: 13450
diff changeset
    57
13419
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    58
            server.react(client_id, actions);
13771
4664da990556 Add official server feature to cargo
alfadur
parents: 13666
diff changeset
    59
        }
12147
03ccb89820f3 Room creation halfplemented
unc0rr
parents:
diff changeset
    60
        HWProtocolMessage::Proto(proto) => {
13666
09f4a30e50cc Rust 2018 conversion
alfadur
parents: 13450
diff changeset
    61
            let client = &mut server.clients[client_id];
09f4a30e50cc Rust 2018 conversion
alfadur
parents: 13450
diff changeset
    62
            let actions = if client.protocol_number != 0 {
09f4a30e50cc Rust 2018 conversion
alfadur
parents: 13450
diff changeset
    63
                vec![ProtocolError("Protocol already known.".to_string())]
13416
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
    64
            }
13666
09f4a30e50cc Rust 2018 conversion
alfadur
parents: 13450
diff changeset
    65
            else if proto == 0 {
09f4a30e50cc Rust 2018 conversion
alfadur
parents: 13450
diff changeset
    66
                vec![ProtocolError("Bad number.".to_string())]
09f4a30e50cc Rust 2018 conversion
alfadur
parents: 13450
diff changeset
    67
            }
09f4a30e50cc Rust 2018 conversion
alfadur
parents: 13450
diff changeset
    68
            else {
09f4a30e50cc Rust 2018 conversion
alfadur
parents: 13450
diff changeset
    69
                client.protocol_number = proto;
09f4a30e50cc Rust 2018 conversion
alfadur
parents: 13450
diff changeset
    70
                vec![Proto(proto).send_self().action(),
09f4a30e50cc Rust 2018 conversion
alfadur
parents: 13450
diff changeset
    71
                     CheckRegistered]
09f4a30e50cc Rust 2018 conversion
alfadur
parents: 13450
diff changeset
    72
            };
13419
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    73
            server.react(client_id, actions);
13771
4664da990556 Add official server feature to cargo
alfadur
parents: 13666
diff changeset
    74
        }
4664da990556 Add official server feature to cargo
alfadur
parents: 13666
diff changeset
    75
        #[cfg(feature = "official-server")]
13774
0118b7412570 use openssl for password message handling
alfadur
parents: 13771
diff changeset
    76
        HWProtocolMessage::Password(hash, salt) => {
0118b7412570 use openssl for password message handling
alfadur
parents: 13771
diff changeset
    77
            let c = &server.clients[client_id];
0118b7412570 use openssl for password message handling
alfadur
parents: 13771
diff changeset
    78
0118b7412570 use openssl for password message handling
alfadur
parents: 13771
diff changeset
    79
            let client_hash = get_hash(c, &salt, &c.server_salt);
0118b7412570 use openssl for password message handling
alfadur
parents: 13771
diff changeset
    80
            let server_hash = get_hash(c, &c.server_salt, &salt);
0118b7412570 use openssl for password message handling
alfadur
parents: 13771
diff changeset
    81
            let actions = if client_hash == server_hash {
0118b7412570 use openssl for password message handling
alfadur
parents: 13771
diff changeset
    82
                vec![ServerAuth(format!("{:x}", server_hash)).send_self().action(),
0118b7412570 use openssl for password message handling
alfadur
parents: 13771
diff changeset
    83
                     JoinLobby]
0118b7412570 use openssl for password message handling
alfadur
parents: 13771
diff changeset
    84
            } else {
0118b7412570 use openssl for password message handling
alfadur
parents: 13771
diff changeset
    85
                vec![ByeClient("Authentication failed".to_string())]
0118b7412570 use openssl for password message handling
alfadur
parents: 13771
diff changeset
    86
            };
0118b7412570 use openssl for password message handling
alfadur
parents: 13771
diff changeset
    87
            server.react(client_id, actions);
0118b7412570 use openssl for password message handling
alfadur
parents: 13771
diff changeset
    88
        }
0118b7412570 use openssl for password message handling
alfadur
parents: 13771
diff changeset
    89
        #[cfg(feature = "official-server")]
13771
4664da990556 Add official server feature to cargo
alfadur
parents: 13666
diff changeset
    90
        HWProtocolMessage::Checker(protocol, nick, password) => {
4664da990556 Add official server feature to cargo
alfadur
parents: 13666
diff changeset
    91
            let c = &mut server.clients[client_id];
4664da990556 Add official server feature to cargo
alfadur
parents: 13666
diff changeset
    92
            c.nick = nick;
4664da990556 Add official server feature to cargo
alfadur
parents: 13666
diff changeset
    93
            c.web_password = password;
4664da990556 Add official server feature to cargo
alfadur
parents: 13666
diff changeset
    94
            c.set_is_checker(true);
4664da990556 Add official server feature to cargo
alfadur
parents: 13666
diff changeset
    95
        }
12147
03ccb89820f3 Room creation halfplemented
unc0rr
parents:
diff changeset
    96
        _ => warn!("Incorrect command in logging-in state"),
03ccb89820f3 Room creation halfplemented
unc0rr
parents:
diff changeset
    97
    }
03ccb89820f3 Room creation halfplemented
unc0rr
parents:
diff changeset
    98
}