internal static void HandleClient(NetStream stream, IPEndPoint ip)
        {
            try {
                var ssl = new SslStream(stream);
                ssl.AuthenticateAsServer(Certificate);
                stream = ssl;
            } catch (Exception e) {
                stream.Write("ERR_SSL\r\n");
                stream.Write(e.ToString());
                return;
            }

            stream.Encoding = Encoding.ASCII;
            var encdodingstring = stream.Read();
            int codepage;
            if (int.TryParse(encdodingstring, out codepage)) {
                try {
                    var cpenc = Encoding.GetEncoding(codepage);
                    stream.Encoding = cpenc;
                    stream.Write("success");
                } catch {
                    stream.Write("ERR_ENCODING_NOT_FOUND");
                    stream.Close();
                    return;
                }
            } else {
                try {
                    var encoding = Encoding.GetEncoding(encdodingstring);
                    stream.Encoding = encoding;
                    stream.Write("success");
                } catch {
                    stream.Write("ERR_ENCODING_NOT_FOUND");
                    stream.Close();
                    return;
                }
            }

            try {
                var cred = Authenticate(stream);
                if (!cred.Successful) {
                    stream.Close();
                    return;
                }
                stream.Write("LOGIN_SUCCESS");
                ClientLoop.BeginClient(new Client(stream, cred.Username, cred.SecurityLevel, ip));
            } catch (Exception ex) {
                stream.Write("ERR\r\n");
                stream.Write(ex.ToString());
            }
        }
        private static NewClientCredentials ConfigAuth(NetStream stream)
        {
            var ncc = new NewClientCredentials { Successful = false, SecurityLevel = -1, Username = null };
            var userpass = stream.Read();
            var ups = userpass.Split(new[] { ':' }, 2);
            if (ups.Length < 2) {
                stream.Write("ERR_AUTH_INVALID_USER_FORMAT");
                stream.Close();
                return ncc;
            }
            if (Userconf["user." + ups[0] + ".enabled"] != "true") {
                stream.Write("ERR_AUTH_INVALID_CREDENTIALS");
                return ncc;
            }
            var validhash = Userconf["user." + ups[0] + ".hash"];
            var thishash = Hash(ups[1]);
            if (validhash != thishash) {
                stream.Write("ERR_AUTH_INVALID_CREDENTIALS");
                return ncc;
            }
            var seclvlstr = Userconf["user." + ups[0] + ".sec"];
            int sec;
            if (!int.TryParse(seclvlstr, out sec)) {
                stream.Write("ERR_ACCOUNT_NOT_AUTHORIZED");
                return ncc;
            }

            ncc.Username = ups[0];
            ncc.SecurityLevel = sec;
            ncc.Successful = true;

            Thread.CurrentThread.Name = ncc.Username;

            return ncc;
        }
Beispiel #3
0
        private async void ConnectClick(object sender, EventArgs e) {
            hostbox.Enabled = false;
            userbox.Enabled = false;
            passbox.Enabled = false;
            var hsplit = hostbox.Text.Split(':');
            var host = hsplit[0];
            if (string.IsNullOrWhiteSpace(host)) {
                Invoke(new Action(() => MessageBox.Show(this, "Please enter a host")));
                hostbox.Enabled = true;
                userbox.Enabled = true;
                passbox.Enabled = true;
                return;
            }
            var port = 3000;
            if (hsplit.Length > 1) {
                if (!int.TryParse(hsplit[1], out port)) {
                    Invoke(new Action(() => MessageBox.Show(this, "Port must be a number")));
                    hostbox.Enabled = true;
                    userbox.Enabled = true;
                    passbox.Enabled = true;
                    return;
                }
            }
            SslStream ssl;
            try {
                var tcp = new TcpClient();
                await tcp.ConnectAsync(host, port);
                ssl = new SslStream(tcp);
            } catch {
                Invoke(new Action(() => MessageBox.Show(this, "Error connecting")));
                hostbox.Enabled = true;
                userbox.Enabled = true;
                passbox.Enabled = true;
                return;
            }
            try {
                await ssl.AuthenticateAsClientAsync(host, ValidationCallback);
            } catch {
                Invoke(new Action(() => MessageBox.Show(this, "Error securing connection")));
                hostbox.Enabled = true;
                userbox.Enabled = true;
                passbox.Enabled = true;
                return;
            }
            Stream = ssl;

            Stream.Encoding = Encoding.UTF8;
            Stream.Write("utf-8");
            Stream.Read();
            Stream.Write(userbox.Text + ":" + passbox.Text);
            var res = Stream.Read();
            if (res.StartsWith("ERR")) {
                if (res == "ERR_AUTH_INVALID_CREDENTIALS") {
                    Invoke(new Action(() => MessageBox.Show(this, "Bad username or password")));
                } else if (res == "ERR_ACCOUNT_NOT_AUTHORIZED") {
                    Invoke(new Action(() => MessageBox.Show(this, "Account not authorized")));
                } else {
                    Invoke(new Action(() => MessageBox.Show(this, "An unkown error occured during authentication")));
                }
                hostbox.Enabled = true;
                userbox.Enabled = true;
                passbox.Enabled = true;
                return;
            }
            Properties.Settings.Default.hostname = hostbox.Text;
            Properties.Settings.Default.username = userbox.Text;

#pragma warning disable 665
            if (Properties.Settings.Default.remeberpass = rempass.Checked) {
#pragma warning restore 665
                Properties.Settings.Default.password = passbox.Text;
            } else {
                Properties.Settings.Default.password = "";
            }

            Properties.Settings.Default.Save();

            Hide();
            new ConsoleForm().ShowDialog();
            Close();
        }