rust/hedgewars-server/src/server/handlers/loggingin.rs
author alfadur
Thu, 07 Feb 2019 18:04:53 +0300
changeset 14695 216d39de1a44
parent 14693 6a2e13e36b7f
child 14696 8a45c90f4580
permissions -rw-r--r--
fix official server build & bump dependencies
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: 13478
diff changeset
     3
use crate::{
14457
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14415
diff changeset
     4
    protocol::messages::{HWProtocolMessage, HWServerMessage::*},
14693
6a2e13e36b7f add server anteroom
alfadur <mail@none>
parents: 14690
diff changeset
     5
    server::{
6a2e13e36b7f add server anteroom
alfadur <mail@none>
parents: 14690
diff changeset
     6
        core::{HWAnteClient, HWAnteroom},
6a2e13e36b7f add server anteroom
alfadur <mail@none>
parents: 14690
diff changeset
     7
        coretypes::ClientId,
6a2e13e36b7f add server anteroom
alfadur <mail@none>
parents: 14690
diff changeset
     8
    },
14457
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14415
diff changeset
     9
    utils::is_name_illegal,
13419
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    10
};
14457
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14415
diff changeset
    11
use log::*;
13800
0118b7412570 use openssl for password message handling
alfadur
parents: 13798
diff changeset
    12
#[cfg(feature = "official-server")]
0118b7412570 use openssl for password message handling
alfadur
parents: 13798
diff changeset
    13
use openssl::sha::sha1;
14693
6a2e13e36b7f add server anteroom
alfadur <mail@none>
parents: 14690
diff changeset
    14
use std::{
6a2e13e36b7f add server anteroom
alfadur <mail@none>
parents: 14690
diff changeset
    15
    fmt::{Formatter, LowerHex},
6a2e13e36b7f add server anteroom
alfadur <mail@none>
parents: 14690
diff changeset
    16
    num::NonZeroU16,
6a2e13e36b7f add server anteroom
alfadur <mail@none>
parents: 14690
diff changeset
    17
};
13800
0118b7412570 use openssl for password message handling
alfadur
parents: 13798
diff changeset
    18
0118b7412570 use openssl for password message handling
alfadur
parents: 13798
diff changeset
    19
#[derive(PartialEq)]
0118b7412570 use openssl for password message handling
alfadur
parents: 13798
diff changeset
    20
struct Sha1Digest([u8; 20]);
0118b7412570 use openssl for password message handling
alfadur
parents: 13798
diff changeset
    21
0118b7412570 use openssl for password message handling
alfadur
parents: 13798
diff changeset
    22
impl LowerHex for Sha1Digest {
0118b7412570 use openssl for password message handling
alfadur
parents: 13798
diff changeset
    23
    fn fmt(&self, f: &mut Formatter) -> Result<(), std::fmt::Error> {
0118b7412570 use openssl for password message handling
alfadur
parents: 13798
diff changeset
    24
        for byte in &self.0 {
0118b7412570 use openssl for password message handling
alfadur
parents: 13798
diff changeset
    25
            write!(f, "{:02x}", byte)?;
0118b7412570 use openssl for password message handling
alfadur
parents: 13798
diff changeset
    26
        }
0118b7412570 use openssl for password message handling
alfadur
parents: 13798
diff changeset
    27
        Ok(())
0118b7412570 use openssl for password message handling
alfadur
parents: 13798
diff changeset
    28
    }
0118b7412570 use openssl for password message handling
alfadur
parents: 13798
diff changeset
    29
}
0118b7412570 use openssl for password message handling
alfadur
parents: 13798
diff changeset
    30
0118b7412570 use openssl for password message handling
alfadur
parents: 13798
diff changeset
    31
#[cfg(feature = "official-server")]
14695
216d39de1a44 fix official server build & bump dependencies
alfadur
parents: 14693
diff changeset
    32
fn get_hash(protocol_number: u16, web_password: &str, salt1: &str, salt2: &str) -> Sha1Digest {
14457
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14415
diff changeset
    33
    let s = format!(
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14415
diff changeset
    34
        "{}{}{}{}{}",
14695
216d39de1a44 fix official server build & bump dependencies
alfadur
parents: 14693
diff changeset
    35
        salt1, salt2, web_password, protocol_number, "!hedgewars"
14457
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14415
diff changeset
    36
    );
13800
0118b7412570 use openssl for password message handling
alfadur
parents: 13798
diff changeset
    37
    Sha1Digest(sha1(s.as_bytes()))
0118b7412570 use openssl for password message handling
alfadur
parents: 13798
diff changeset
    38
}
12147
03ccb89820f3 Room creation halfplemented
unc0rr
parents:
diff changeset
    39
14693
6a2e13e36b7f add server anteroom
alfadur <mail@none>
parents: 14690
diff changeset
    40
pub enum LoginResult {
6a2e13e36b7f add server anteroom
alfadur <mail@none>
parents: 14690
diff changeset
    41
    Unchanged,
6a2e13e36b7f add server anteroom
alfadur <mail@none>
parents: 14690
diff changeset
    42
    Complete,
6a2e13e36b7f add server anteroom
alfadur <mail@none>
parents: 14690
diff changeset
    43
    Exit,
6a2e13e36b7f add server anteroom
alfadur <mail@none>
parents: 14690
diff changeset
    44
}
6a2e13e36b7f add server anteroom
alfadur <mail@none>
parents: 14690
diff changeset
    45
14671
455865ccd36c Server action refactoring part 2 of N
alfadur <mail@none>
parents: 14457
diff changeset
    46
pub fn handle(
14693
6a2e13e36b7f add server anteroom
alfadur <mail@none>
parents: 14690
diff changeset
    47
    anteroom: &mut HWAnteroom,
14671
455865ccd36c Server action refactoring part 2 of N
alfadur <mail@none>
parents: 14457
diff changeset
    48
    client_id: ClientId,
455865ccd36c Server action refactoring part 2 of N
alfadur <mail@none>
parents: 14457
diff changeset
    49
    response: &mut super::Response,
455865ccd36c Server action refactoring part 2 of N
alfadur <mail@none>
parents: 14457
diff changeset
    50
    message: HWProtocolMessage,
14693
6a2e13e36b7f add server anteroom
alfadur <mail@none>
parents: 14690
diff changeset
    51
) -> LoginResult {
12147
03ccb89820f3 Room creation halfplemented
unc0rr
parents:
diff changeset
    52
    match message {
14693
6a2e13e36b7f add server anteroom
alfadur <mail@none>
parents: 14690
diff changeset
    53
        HWProtocolMessage::Quit(_) => {
6a2e13e36b7f add server anteroom
alfadur <mail@none>
parents: 14690
diff changeset
    54
            response.add(Bye("User quit".to_string()).send_self());
6a2e13e36b7f add server anteroom
alfadur <mail@none>
parents: 14690
diff changeset
    55
            LoginResult::Exit
6a2e13e36b7f add server anteroom
alfadur <mail@none>
parents: 14690
diff changeset
    56
        }
13416
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
    57
        HWProtocolMessage::Nick(nick) => {
14693
6a2e13e36b7f add server anteroom
alfadur <mail@none>
parents: 14690
diff changeset
    58
            let client = &mut anteroom.clients[client_id];
13666
09f4a30e50cc Rust 2018 conversion
alfadur
parents: 13478
diff changeset
    59
            debug!("{} {}", nick, is_name_illegal(&nick));
14693
6a2e13e36b7f add server anteroom
alfadur <mail@none>
parents: 14690
diff changeset
    60
            if !client.nick.is_some() {
14672
6e6632068a33 Server action refactoring part 3 of N
alfadur <mail@none>
parents: 14671
diff changeset
    61
                response.add(Error("Nickname already provided.".to_string()).send_self());
14693
6a2e13e36b7f add server anteroom
alfadur <mail@none>
parents: 14690
diff changeset
    62
                LoginResult::Unchanged
14457
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14415
diff changeset
    63
            } else if is_name_illegal(&nick) {
14693
6a2e13e36b7f add server anteroom
alfadur <mail@none>
parents: 14690
diff changeset
    64
                response.add(Bye("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()).send_self());
6a2e13e36b7f add server anteroom
alfadur <mail@none>
parents: 14690
diff changeset
    65
                LoginResult::Exit
14457
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14415
diff changeset
    66
            } else {
14693
6a2e13e36b7f add server anteroom
alfadur <mail@none>
parents: 14690
diff changeset
    67
                client.nick = Some(nick.clone());
14672
6e6632068a33 Server action refactoring part 3 of N
alfadur <mail@none>
parents: 14671
diff changeset
    68
                response.add(Nick(nick).send_self());
14673
08a8605bafaf Server action refactoring part 4 of N
alfadur <mail@none>
parents: 14672
diff changeset
    69
14693
6a2e13e36b7f add server anteroom
alfadur <mail@none>
parents: 14690
diff changeset
    70
                if client.protocol_number.is_some() {
6a2e13e36b7f add server anteroom
alfadur <mail@none>
parents: 14690
diff changeset
    71
                    LoginResult::Complete
6a2e13e36b7f add server anteroom
alfadur <mail@none>
parents: 14690
diff changeset
    72
                } else {
6a2e13e36b7f add server anteroom
alfadur <mail@none>
parents: 14690
diff changeset
    73
                    LoginResult::Unchanged
14673
08a8605bafaf Server action refactoring part 4 of N
alfadur <mail@none>
parents: 14672
diff changeset
    74
                }
08a8605bafaf Server action refactoring part 4 of N
alfadur <mail@none>
parents: 14672
diff changeset
    75
            }
08a8605bafaf Server action refactoring part 4 of N
alfadur <mail@none>
parents: 14672
diff changeset
    76
        }
08a8605bafaf Server action refactoring part 4 of N
alfadur <mail@none>
parents: 14672
diff changeset
    77
        HWProtocolMessage::Proto(proto) => {
14693
6a2e13e36b7f add server anteroom
alfadur <mail@none>
parents: 14690
diff changeset
    78
            let client = &mut anteroom.clients[client_id];
6a2e13e36b7f add server anteroom
alfadur <mail@none>
parents: 14690
diff changeset
    79
            if client.protocol_number.is_some() {
14673
08a8605bafaf Server action refactoring part 4 of N
alfadur <mail@none>
parents: 14672
diff changeset
    80
                response.add(Error("Protocol already known.".to_string()).send_self());
14693
6a2e13e36b7f add server anteroom
alfadur <mail@none>
parents: 14690
diff changeset
    81
                LoginResult::Unchanged
14673
08a8605bafaf Server action refactoring part 4 of N
alfadur <mail@none>
parents: 14672
diff changeset
    82
            } else if proto == 0 {
08a8605bafaf Server action refactoring part 4 of N
alfadur <mail@none>
parents: 14672
diff changeset
    83
                response.add(Error("Bad number.".to_string()).send_self());
14693
6a2e13e36b7f add server anteroom
alfadur <mail@none>
parents: 14690
diff changeset
    84
                LoginResult::Unchanged
14673
08a8605bafaf Server action refactoring part 4 of N
alfadur <mail@none>
parents: 14672
diff changeset
    85
            } else {
14693
6a2e13e36b7f add server anteroom
alfadur <mail@none>
parents: 14690
diff changeset
    86
                client.protocol_number = NonZeroU16::new(proto);
14673
08a8605bafaf Server action refactoring part 4 of N
alfadur <mail@none>
parents: 14672
diff changeset
    87
                response.add(Proto(proto).send_self());
08a8605bafaf Server action refactoring part 4 of N
alfadur <mail@none>
parents: 14672
diff changeset
    88
14693
6a2e13e36b7f add server anteroom
alfadur <mail@none>
parents: 14690
diff changeset
    89
                if client.nick.is_some() {
6a2e13e36b7f add server anteroom
alfadur <mail@none>
parents: 14690
diff changeset
    90
                    LoginResult::Complete
6a2e13e36b7f add server anteroom
alfadur <mail@none>
parents: 14690
diff changeset
    91
                } else {
6a2e13e36b7f add server anteroom
alfadur <mail@none>
parents: 14690
diff changeset
    92
                    LoginResult::Unchanged
14673
08a8605bafaf Server action refactoring part 4 of N
alfadur <mail@none>
parents: 14672
diff changeset
    93
                }
14672
6e6632068a33 Server action refactoring part 3 of N
alfadur <mail@none>
parents: 14671
diff changeset
    94
            }
13798
4664da990556 Add official server feature to cargo
alfadur
parents: 13666
diff changeset
    95
        }
4664da990556 Add official server feature to cargo
alfadur
parents: 13666
diff changeset
    96
        #[cfg(feature = "official-server")]
13800
0118b7412570 use openssl for password message handling
alfadur
parents: 13798
diff changeset
    97
        HWProtocolMessage::Password(hash, salt) => {
14693
6a2e13e36b7f add server anteroom
alfadur <mail@none>
parents: 14690
diff changeset
    98
            let client = &anteroom.clients[client_id];
13800
0118b7412570 use openssl for password message handling
alfadur
parents: 13798
diff changeset
    99
14695
216d39de1a44 fix official server build & bump dependencies
alfadur
parents: 14693
diff changeset
   100
            if let (Some(protocol), Some(password)) = (client.protocol_number, client.web_password.as_ref()) {
216d39de1a44 fix official server build & bump dependencies
alfadur
parents: 14693
diff changeset
   101
                let client_hash = get_hash(protocol.get(), &password, &salt, &client.server_salt);
216d39de1a44 fix official server build & bump dependencies
alfadur
parents: 14693
diff changeset
   102
                let server_hash = get_hash(protocol.get(), &password, &client.server_salt, &salt);
216d39de1a44 fix official server build & bump dependencies
alfadur
parents: 14693
diff changeset
   103
                if client_hash == server_hash {
216d39de1a44 fix official server build & bump dependencies
alfadur
parents: 14693
diff changeset
   104
                    response.add(ServerAuth(format!("{:x}", server_hash)).send_self());
216d39de1a44 fix official server build & bump dependencies
alfadur
parents: 14693
diff changeset
   105
                    LoginResult::Complete
216d39de1a44 fix official server build & bump dependencies
alfadur
parents: 14693
diff changeset
   106
                } else {
216d39de1a44 fix official server build & bump dependencies
alfadur
parents: 14693
diff changeset
   107
                    response.add(Bye("No protocol provided.".to_string()).send_self());
216d39de1a44 fix official server build & bump dependencies
alfadur
parents: 14693
diff changeset
   108
                    LoginResult::Unchanged
216d39de1a44 fix official server build & bump dependencies
alfadur
parents: 14693
diff changeset
   109
                }
13800
0118b7412570 use openssl for password message handling
alfadur
parents: 13798
diff changeset
   110
            } else {
14695
216d39de1a44 fix official server build & bump dependencies
alfadur
parents: 14693
diff changeset
   111
                response.add(Bye("Authentication failed.".to_string()).send_self());
14693
6a2e13e36b7f add server anteroom
alfadur <mail@none>
parents: 14690
diff changeset
   112
                LoginResult::Exit
14686
9f98086de1b6 Server action refactoring part 9 of N
alfadur <mail@none>
parents: 14683
diff changeset
   113
            }
14672
6e6632068a33 Server action refactoring part 3 of N
alfadur <mail@none>
parents: 14671
diff changeset
   114
        }
13800
0118b7412570 use openssl for password message handling
alfadur
parents: 13798
diff changeset
   115
        #[cfg(feature = "official-server")]
13798
4664da990556 Add official server feature to cargo
alfadur
parents: 13666
diff changeset
   116
        HWProtocolMessage::Checker(protocol, nick, password) => {
14693
6a2e13e36b7f add server anteroom
alfadur <mail@none>
parents: 14690
diff changeset
   117
            let client = &mut anteroom.clients[client_id];
14695
216d39de1a44 fix official server build & bump dependencies
alfadur
parents: 14693
diff changeset
   118
            client.protocol_number = NonZeroU16::new(protocol);
14693
6a2e13e36b7f add server anteroom
alfadur <mail@none>
parents: 14690
diff changeset
   119
            client.nick = Some(nick);
14695
216d39de1a44 fix official server build & bump dependencies
alfadur
parents: 14693
diff changeset
   120
            client.web_password = Some(password);
14693
6a2e13e36b7f add server anteroom
alfadur <mail@none>
parents: 14690
diff changeset
   121
            //client.set_is_checker(true);
6a2e13e36b7f add server anteroom
alfadur <mail@none>
parents: 14690
diff changeset
   122
            LoginResult::Complete
13798
4664da990556 Add official server feature to cargo
alfadur
parents: 13666
diff changeset
   123
        }
14693
6a2e13e36b7f add server anteroom
alfadur <mail@none>
parents: 14690
diff changeset
   124
        _ => {
6a2e13e36b7f add server anteroom
alfadur <mail@none>
parents: 14690
diff changeset
   125
            warn!("Incorrect command in logging-in state");
6a2e13e36b7f add server anteroom
alfadur <mail@none>
parents: 14690
diff changeset
   126
            LoginResult::Unchanged
6a2e13e36b7f add server anteroom
alfadur <mail@none>
parents: 14690
diff changeset
   127
        }
12147
03ccb89820f3 Room creation halfplemented
unc0rr
parents:
diff changeset
   128
    }
03ccb89820f3 Room creation halfplemented
unc0rr
parents:
diff changeset
   129
}