/// <summary>
        /// Creates the command packet's header and returns a BinaryWriter so the caller
        /// can write the command packet's payload.
        /// </summary>
        /// <param name="ms">A MemoryStream to write the command packet.</param>
        /// <param name="command">A DatabaseAuthProtocolCommands that specifies the command.</param>
        /// <returns>A BinaryWriter that contains the command packet's header.</returns>
        protected BinaryWriter GetBinaryWriter(MemoryStream ms, DatabaseAuthProtocolCommands command)
        {
            BinaryWriter bw = new BinaryWriter(ms, Encoding.UTF8);

            bw.Write(DatabaseAuthProtocol.PROTOCOL_IDENTIFIER);
            bw.Write((byte)command);
            return(bw);
        }
Exemple #2
0
 public DatabaseAuthProtocolException(DatabaseAuthProtocolCommands errorCode)
     : base()
 {
     ErrorCode = errorCode;
 }
Exemple #3
0
        /// <summary>
        /// Handles the <see cref="US.OpenServer.Protocols.DatabaseAuth.DatabaseAuthProtocolCommands.AUTHENTICATE"/>
        /// command packet request.
        /// </summary>
        /// <param name="br">A BinaryReader that contains the command packet.</param>
        public override void OnPacketReceived(BinaryReader br)
        {
            lock (this)
            {
                if (Session == null)
                {
                    return;
                }

                DatabaseAuthProtocolCommands command = (DatabaseAuthProtocolCommands)br.ReadByte();
                try
                {
                    switch (command)
                    {
                    case DatabaseAuthProtocolCommands.AUTHENTICATE:

                        string userName = br.ReadString();
                        string password = br.ReadString();

                        try
                        {
                            DB db = new DB("DB");
                            UserId = db.Authenticate(userName, password);

                            UserName        = userName;
                            IsAuthenticated = true;

                            Session.UserName               = userName;
                            Session.IsAuthenticated        = true;
                            Session.AuthenticationProtocol = this;

                            Log(Level.Info, string.Format(@"Authenticated {0}.", userName));

                            MemoryStream ms = new MemoryStream();
                            BinaryWriter bw = GetBinaryWriter(ms, DatabaseAuthProtocolCommands.AUTHENTICATED);
                            bw.Write((int)UserId);
                            Session.Send(ms);
                        }
                        catch (DatabaseAuthProtocolException ex)
                        {
                            Log(Level.Notice, string.Format(@"Access denied.  {0}.  User: {1}", ex.Message, userName));

                            MemoryStream ms = new MemoryStream();
                            BinaryWriter bw = GetBinaryWriter(ms, ex.ErrorCode);
                            Session.Send(ms);
                        }
                        catch (Exception ex)
                        {
                            Log(Level.Notice, string.Format(@"Access denied.  {0}.  User: {1}", ex.Message, userName));

                            MemoryStream ms = new MemoryStream();
                            GetBinaryWriter(ms, DatabaseAuthProtocolCommands.ACCESS_DENIED);
                            Session.Send(ms);
                        }
                        break;

                    default:
                        throw new Exception("Invalid or unsupported command.");
                    }
                }
                catch (Exception ex)
                {
                    Log(Level.Error, string.Format("{0}  Command: {1}", ex.Message, command));

                    MemoryStream ms = new MemoryStream();
                    BinaryWriter bw = GetBinaryWriter(ms, DatabaseAuthProtocolCommands.ERROR);
                    bw.Write(ex.Message);
                    Session.Send(ms);
                }
            }
        }