protected Processor(DbController controller, string hostString) { this.hostString = hostString; dbInterfaces = new Dictionary<string, DatabaseInterface>(); this.controller = controller; state = 0; authenticationTries = 0; dbCallback = OnDatabaseEvent; }
/// <inheritdoc/> public bool Login(string defaultSchema, string user, string password, DatabaseEventCallback callback) { try { // Do some handshaking, MemoryStream bout = new MemoryStream(); BinaryWriter output = new BinaryWriter(bout, Encoding.ASCII); // Write output the magic number output.Write(0x0ced007); // Write output the driver version Version clientVersion = ClientVersion; output.Write(clientVersion.Major); output.Write(clientVersion.Minor); output.Write(initialDatabase); byte[] arr = bout.ToArray(); SendCommand(arr, 0, arr.Length); byte[] response = ReceiveCommand(0); int ack = ByteBuffer.ReadInt4(response, 0); if (ack == ProtocolConstants.Acknowledgement) { // Is there anything more to Read? if (response.Length > 4 && response[4] == 1) { // Yes so Read the server version int serverMajorVersion = ByteBuffer.ReadInt4(response, 5); int serverMinorVersion = ByteBuffer.ReadInt4(response, 9); serverVersion = new Version(serverMajorVersion, serverMinorVersion); } // Send the username and password to the server // SECURITY: username/password sent as plain text. This is okay // if we are connecting to localhost, but not good if we connecting // over the internet. We could encrypt this, but it would probably // be better if we WriteByte the entire stream through an encyption // protocol. bout = new MemoryStream(); output = new BinaryWriter(bout); output.Write(defaultSchema); output.Write(user); output.Write(password); arr = bout.ToArray(); SendCommand(arr, 0, arr.Length); response = ReceiveCommand(0); int result = ByteBuffer.ReadInt4(response, 0); if (result == ProtocolConstants.UserAuthenticationPassed) { // Set the callback, databaseCallback = callback; // User authentication passed so we successfully logged input now. connectionThread = new ConnectionThread(this); connectionThread.Start(); return true; } if (result == ProtocolConstants.UserAuthenticationFailed) throw new DataException("User Authentication failed."); if (result == ProtocolConstants.DatabaseNotFound) throw new DataException("The database specified was not found."); throw new DataException("Unexpected response."); } if (ack == ProtocolConstants.DatabaseNotFound) throw new DataException("The database was not found."); throw new DataException("No acknowledgement received from server."); } catch (IOException e) { LogException(e); throw new DataException("IOException: " + e.Message); } }
/// <summary> /// Tries to authenticate the username and password against the given database. /// </summary> /// <param name="database"></param> /// <param name="default_schema"></param> /// <param name="username"></param> /// <param name="password"></param> /// <param name="database_call_back"></param> /// <remarks> /// If successful, alters the state of this object to reflect the fact the user has logged in. /// </remarks> /// <returns> /// Returns <b>true</b> if we are successful. /// </returns> private bool Authenticate(IDatabase database, String default_schema, String username, String password, DatabaseEventCallback database_call_back) { // If the 'user' variable is null, no one is currently logged in to this // connection. if (User != null) throw new Exception("Attempt to authenticate user twice"); if (database.Context.Logger.IsInterestedIn(LogLevel.Debug)) { // Output the instruction to the _queries log. database.Context.Logger.Debug(this, String.Format("[CLIENT] [{0}][{1}] - Log in", username, host_name)); } // Write debug message, if (Logger.IsInterestedIn(LogLevel.Info)) { Logger.Info(this, "Authenticate User: "******"Couldn't change to '" + default_schema + "' schema."); // If we can't change to the schema then change to the APP schema database_connection.SetDefaultSchema("APP"); } } finally { try { // Make sure we commit the connection. database_connection.Commit(); } catch (TransactionException e) { // Just issue a warning... Logger.Warning(this, e); } finally { // Guarentee that we unluck from EXCLUSIVE locker.FinishMode(LockingMode.Exclusive); } } } // If we have a user object, then init the object, if (thisUser != null) { Init(thisUser, database_connection); return true; } // Otherwise, return false. return false; }
// ---------- Implemented from IDatabaseInterface ---------- /// <inheritdoc/> public abstract bool Login(string defaultSchema, string username, string password, DatabaseEventCallback callback);
// ---------- Implemented from IDatabaseInterface ---------- public override bool Login(String defaultSchema, String username, String password, DatabaseEventCallback databaseCallback) { IDatabase database = Database; return Authenticate(database, defaultSchema, username, password, databaseCallback); }