public ConnectRequest(ConnectionEndPoint localEndPoint, ConnectionEndPoint remoteEndPoint) { if (localEndPoint == null) throw new ArgumentNullException("localEndPoint"); if (remoteEndPoint == null) throw new ArgumentNullException("remoteEndPoint"); LocalEndPoint = localEndPoint; RemoteEndPoint = remoteEndPoint; }
public static User Authenticate(this IQueryContext queryContext, string username, string password, ConnectionEndPoint endPoint) { try { var table = queryContext.GetTable(SystemSchema.PasswordTableName); var unameColumn = table.GetResolvedColumnName(0); var typeColumn = table.GetResolvedColumnName(1); var passwColumn = table.GetResolvedColumnName(2); var saltColumn = table.GetResolvedColumnName(3); var hashColumn = table.GetResolvedColumnName(4); var t = table.SimpleSelect(queryContext, unameColumn, SqlExpressionType.Equal, SqlExpression.Constant(username)); if (t.RowCount == 0) return null; var type = t.GetValue(0, typeColumn); if (type == 1) { // Clear-text password ... var pass = t.GetValue(0, passwColumn); if (pass.IsNull || !pass.Equals(DataObject.String(password))) return null; } else if (type == 2) { #if PCL throw new NotSupportedException("Hashed passwords are not currently supported in PCL"); #else // Hashed password ... var pass = t.GetValue(0, passwColumn); var salt = t.GetValue(0, saltColumn); var hash = t.GetValue(0, hashColumn); if (pass == null || salt == null || hash == null) return null; var crypto = PasswordCrypto.Parse(hash); if (!crypto.Verify(pass, password, salt)) return null; #endif } else if (type == 3) { // External authenticator ... // TODO: } // Now check if this user is permitted to connect from the given // host. if (!UserCanAccessFromHost(queryContext, username, endPoint)) return null; // Successfully authenticated... return new User(username); } catch (Exception ex) { throw new DatabaseSystemException("Could not authenticate user.", ex); } }
public static User Authenticate(this IDatabase database, string username, string password, ConnectionEndPoint endPoint) { // Create a temporary connection for authentication only... using (var session = database.CreateSystemSession()) { session.CurrentSchema(SystemSchema.Name); session.ExclusiveLock(); using (var queryContext = new SessionQueryContext(session)) { return queryContext.Authenticate(username, password, endPoint); } } }
public SessionInfo(int commitId, User user, TransactionIsolation isolation, ConnectionEndPoint endPoint) { if (user == null) throw new ArgumentNullException("user"); if (endPoint == null) throw new ArgumentNullException("endPoint"); CommitId = commitId; User = user; EndPoint = endPoint; Isolation = isolation; StartedOn = DateTimeOffset.UtcNow; }
public ConnectRequest(ConnectionEndPoint localEndPoint, ConnectionEndPoint remoteEndPoint) { if (localEndPoint == null) { throw new ArgumentNullException("localEndPoint"); } if (remoteEndPoint == null) { throw new ArgumentNullException("remoteEndPoint"); } LocalEndPoint = localEndPoint; RemoteEndPoint = remoteEndPoint; }
protected override NetworkStream CreateNetworkStream(ConnectionEndPoint remoteEndPoint, FileAccess access) { if (remoteEndPoint == null) throw new ArgumentNullException("remoteEndPoint"); if (remoteEndPoint.Protocol != KnownConnectionProtocols.TcpIp) throw new ArgumentException(); var endPoint = ParseEndPoint(remoteEndPoint.Address); var sockect = new Socket(endPoint.AddressFamily, SocketType.Stream, ProtocolType.Tcp); sockect.SendTimeout = Timeout; sockect.ReceiveTimeout = Timeout; sockect.Connect(endPoint); return new NetworkStream(sockect, access, true); }
protected void OpenConnector(ConnectionEndPoint remoteEndPoint, string databaseName) { try { RemoteEndPoint = remoteEndPoint; Database = DatabaseHandler.GetDatabase(databaseName); if (Database == null) { throw new InvalidOperationException(); } OnConnectorOpen(); ChangeState(ConnectorState.Open); } catch (Exception) { // TODO: Log the error... throw; } }
protected void OpenConnector(ConnectionEndPoint remoteEndPoint, string databaseName) { try { RemoteEndPoint = remoteEndPoint; Database = DatabaseHandler.GetDatabase(databaseName); if (Database == null) { throw new DatabaseException(); } OnConnectorOpen(); ChangeState(ConnectorState.Open); } catch (Exception ex) { Logger.Error(this, "Error when opening the connector."); Logger.Error(this, ex); throw; } }
protected void OpenConnector(ConnectionEndPoint remoteEndPoint) { try { RemoteEndPoint = remoteEndPoint; var readStream = CreateNetworkStream(remoteEndPoint, FileAccess.Read); var writeStream = CreateNetworkStream(remoteEndPoint, FileAccess.Write); InputStream = new BufferedStream(readStream, 1024 * 3); OutputStream = new BufferedStream(writeStream, 1024 * 3); OnConnectorOpen(); ChangeState(ConnectorState.Open); envelopeReceiver.Start(); } catch (Exception ex) { //TODO: log somehwere ... throw; } }
protected override NetworkStream CreateNetworkStream(ConnectionEndPoint remoteEndPoint, FileAccess access) { if (remoteEndPoint == null) { throw new ArgumentNullException("remoteEndPoint"); } if (remoteEndPoint.Protocol != KnownConnectionProtocols.TcpIp) { throw new ArgumentException(); } var endPoint = ParseEndPoint(remoteEndPoint.Address); var sockect = new Socket(endPoint.AddressFamily, SocketType.Stream, ProtocolType.Tcp); sockect.SendTimeout = Timeout; sockect.ReceiveTimeout = Timeout; sockect.Connect(endPoint); return(new NetworkStream(sockect, access, true)); }
protected void OpenConnector(ConnectionEndPoint remoteEndPoint) { try { RemoteEndPoint = remoteEndPoint; var readStream = CreateNetworkStream(remoteEndPoint, FileAccess.Read); var writeStream = CreateNetworkStream(remoteEndPoint, FileAccess.Write); InputStream = new BufferedStream(readStream, 1024*3); OutputStream = new BufferedStream(writeStream, 1024*3); OnConnectorOpen(); ChangeState(ConnectorState.Open); envelopeReceiver.Start(); } catch (Exception ex) { //TODO: log somehwere ... throw; } }
public static IUserSession CreateUserSession(this IDatabase database, string userName, string password, ConnectionEndPoint endPoint) { var user = database.Authenticate(userName, password, endPoint); if (user == null) throw new InvalidOperationException(String.Format("Unable to create a session for user '{0}': not authenticated.", userName)); return database.CreateUserSession(user); }
private static bool UserCanAccessFromHost(this IQueryContext queryContext, string username, ConnectionEndPoint endPoint) { // The system user is not allowed to login if (String.Equals(username, User.SystemName, StringComparison.OrdinalIgnoreCase)) return false; // What's the protocol? string protocol = endPoint.Protocol; string host = endPoint.Address; // The table to check var connectPriv = queryContext.GetTable(SystemSchema.UserConnectPrivilegesTableName); var unCol = connectPriv.GetResolvedColumnName(0); var protoCol = connectPriv.GetResolvedColumnName(1); var hostCol = connectPriv.GetResolvedColumnName(2); var accessCol = connectPriv.GetResolvedColumnName(3); // Query: where UserName = %username% var t = connectPriv.SimpleSelect(queryContext, unCol, SqlExpressionType.Equal, SqlExpression.Constant(username)); // Query: where %protocol% like Protocol var exp = SqlExpression.Binary(SqlExpression.Constant(protocol), SqlExpressionType.Like, SqlExpression.Reference(protoCol)); t = t.ExhaustiveSelect(queryContext, exp); // Query: where %host% like Host exp = SqlExpression.Binary(SqlExpression.Constant(host), SqlExpressionType.Like, SqlExpression.Reference(hostCol)); t = t.ExhaustiveSelect(queryContext, exp); // Those that are DENY var t2 = t.SimpleSelect(queryContext, accessCol, SqlExpressionType.Equal, SqlExpression.Constant(DataObject.BooleanFalse)); if (t2.RowCount > 0) return false; // Those that are ALLOW var t3 = t.SimpleSelect(queryContext, accessCol, SqlExpressionType.Equal, SqlExpression.Constant(DataObject.BooleanTrue)); if (t3.RowCount > 0) return true; // No DENY or ALLOW entries for this host so deny access. return false; }
public SessionInfo(User user, TransactionIsolation isolation, ConnectionEndPoint endPoint) : this(-1, user, isolation, endPoint) { }
protected abstract NetworkStream CreateNetworkStream(ConnectionEndPoint remoteEndPoint, FileAccess access);
protected void OpenConnector(ConnectionEndPoint remoteEndPoint, string databaseName) { try { RemoteEndPoint = remoteEndPoint; Database = DatabaseHandler.GetDatabase(databaseName); if (Database == null) throw new InvalidOperationException(); OnConnectorOpen(); ChangeState(ConnectorState.Open); } catch (Exception ex) { // TODO: Log the error... throw; } }
/// <summary> /// Initializes a new instance of the <see cref="SessionInfo" /> class. /// </summary> /// <param name="user">The user that owns the session.</param> /// <param name="isolation">The isolation level of the transaction.</param> /// <param name="endPoint">The source end point of the session.</param> public SessionInfo(User user, IsolationLevel isolation, ConnectionEndPoint endPoint) : this(-1, user, isolation, endPoint) { }
protected void OpenConnector(ConnectionEndPoint remoteEndPoint, string databaseName) { try { RemoteEndPoint = remoteEndPoint; Database = DatabaseHandler.GetDatabase(databaseName); if (Database == null) throw new DatabaseException(); OnConnectorOpen(); ChangeState(ConnectorState.Open); } catch (Exception ex) { Logger.Error(this, "Error when opening the connector."); Logger.Error(this, ex); throw; } }
public void Connect() { if (Connector == null) { Connector = CreateConnector(); Processor = Connector.CreateProcessor(); } remoteEndPoint = MakeRemoteEndPoint(); var request = new ConnectRequest(Connector.LocalEndPoint, remoteEndPoint) { DatabaseName = Settings.Database, Timeout = Settings.QueryTimeout, IgnoreIdentifiersCase = Settings.IgnoreIdentifiersCase, ParameterStyle = Settings.ParameterStyle, AutoCommit = Settings.AutoCommit }; var response = SendMessage(request) as ConnectResponse; if (response == null) throw new ProtocolException("The returned message is invalid"); if (!response.Opened) throw new DeveelDbServerException("Was not able to open the connection on the server.", -1, -1); if (response.IsEncryted) Connector.SetEncrypton(response.EncryptionData); IsClosed = false; ServerVersion = response.Version; }