ConnectionInfo ci = new ConnectionInfo(db); ci.setOriginalURL(originalURL); ci.setUserName(transfer.readString()); ci.setUserPasswordHash(transfer.readBytes()); ci.setFilePasswordHash(transfer.readBytes()); int len = transfer.readInt(); for (int i = 0; i < len; i++) { ci.setProperty(transfer.readString(), transfer.readString()); ci.setBaseDir(baseDir); ci.setProperty("IFEXISTS", "TRUE"); transfer.flush(); if (clientVersion >= Constants.TCP_PROTOCOL_VERSION_13) { if (ci.getFilePasswordHash() != null) { ci.setFileEncryptionKey(transfer.readBytes()); server.addConnection(threadId, originalURL, ci.getUserName()); trace("Connected"); } catch (Throwable e) {
private void connectServer(ConnectionInfo ci) { String name = ci.getName(); if (name.startsWith("//")) { name = name.substring("//".length()); throw ci.getFormatException(); String traceLevelFile = ci.getProperty( SetTypes.TRACE_LEVEL_FILE, null); if (traceLevelFile != null) { String traceLevelSystemOut = ci.getProperty( SetTypes.TRACE_LEVEL_SYSTEM_OUT, null); if (traceLevelSystemOut != null) { if (server.indexOf(',') >= 0) { serverList = StringUtils.quoteStringSQL(server); ci.setProperty("CLUSTER", Constants.CLUSTERING_ENABLED); autoReconnect = ci.getProperty("AUTO_RECONNECT", false); boolean autoServer = ci.getProperty("AUTO_SERVER", false); if (autoServer && serverList != null) { throw DbException String className = ci.getProperty("DATABASE_EVENT_LISTENER"); if (className != null) { className = StringUtils.trim(className, true, true, "'"); cipher = ci.getProperty("CIPHER");
public Database(ConnectionInfo ci, String cipher) { META_LOCK_DEBUGGING.set(null); META_LOCK_DEBUGGING_STACK.set(null); String name = ci.getName(); this.dbSettings = ci.getDbSettings(); this.reconnectCheckDelayNs = TimeUnit.MILLISECONDS.toNanos(dbSettings.reconnectCheckDelay); this.compareMode = CompareMode.getInstance(null, 0); this.persistent = ci.isPersistent(); this.filePasswordHash = ci.getFilePasswordHash(); this.fileEncryptionKey = ci.getFileEncryptionKey(); this.databaseName = name; this.databaseShortName = parseDatabaseShortName(); this.maxLengthInplaceLob = Constants.DEFAULT_MAX_LENGTH_INPLACE_LOB; this.cipher = cipher; String lockMethodName = ci.getProperty("FILE_LOCK", null); this.accessModeData = StringUtils.toLowerEnglish( ci.getProperty("ACCESS_MODE_DATA", "rw")); this.autoServerMode = ci.getProperty("AUTO_SERVER", false); this.autoServerPort = ci.getProperty("AUTO_SERVER_PORT", 0); int defaultCacheSize = Utils.scaleForAvailableMemory( Constants.CACHE_SIZE_DEFAULT); this.cacheSize = ci.getProperty("CACHE_SIZE", defaultCacheSize); this.pageSize = ci.getProperty("PAGE_SIZE", Constants.DEFAULT_PAGE_SIZE); if ("r".equals(accessModeData)) { this.databaseURL = ci.getURL(); String listener = ci.removeProperty("DATABASE_EVENT_LISTENER", null); if (listener != null) {
/** * Get the value of the given property. * * @param key the property key * @param defaultValue the default value * @return the value as a String */ public String getProperty(String key, String defaultValue) { if (SysProperties.CHECK && !isKnownSetting(key)) { DbException.throwInternalError(key); } String s = getProperty(key); return s == null ? defaultValue : s; }
/** * Split the password property into file password and user password if * necessary, and convert them to the internal hash format. */ private void convertPasswords() { char[] password = removePassword(); boolean passwordHash = removeProperty("PASSWORD_HASH", false); if (getProperty("CIPHER", null) != null) { // split password into (filePassword+' '+userPassword) int space = -1; for (int i = 0, len = password.length; i < len; i++) { if (password[i] == ' ') { space = i; break; } } if (space < 0) { throw DbException.get(ErrorCode.WRONG_PASSWORD_FORMAT); } char[] np = Arrays.copyOfRange(password, space + 1, password.length); char[] filePassword = Arrays.copyOf(password, space); Arrays.fill(password, (char) 0); password = np; fileEncryptionKey = FilePathEncrypt.getPasswordBytes(filePassword); filePasswordHash = hashPassword(passwordHash, "file", filePassword); } userPasswordHash = hashPassword(passwordHash, user, password); }
private Transfer initTransfer(ConnectionInfo ci, String db, String server) throws IOException { Socket socket = NetUtils.createSocket(server, Constants.DEFAULT_TCP_PORT, ci.isSSL()); Transfer trans = new Transfer(this, socket); trans.setSSL(ci.isSSL()); trans.init(); trans.writeInt(Constants.TCP_PROTOCOL_VERSION_MIN_SUPPORTED); trans.writeInt(Constants.TCP_PROTOCOL_VERSION_MAX_SUPPORTED); trans.writeString(db); trans.writeString(ci.getOriginalURL()); trans.writeString(ci.getUserName()); trans.writeBytes(ci.getUserPasswordHash()); trans.writeBytes(ci.getFilePasswordHash()); String[] keys = ci.getKeys(); trans.writeInt(keys.length); for (String key : keys) { trans.writeString(key).writeString(ci.getProperty(key)); trans.setVersion(clientVersion); if (clientVersion >= Constants.TCP_PROTOCOL_VERSION_14) { if (ci.getFileEncryptionKey() != null) { trans.writeBytes(ci.getFileEncryptionKey());
private Session openSession(ConnectionInfo ci, boolean ifExists, String cipher) { String name = ci.getName(); Database database; ci.removeProperty("NO_UPGRADE", false); boolean openNew = ci.getProperty("OPEN_NEW", false); boolean opened = false; User user = null; synchronized (DATABASES) { if (openNew || ci.isUnnamedInMemory()) { database = null; } else { ci.getUserName(), false); user.setAdmin(true); user.setUserPasswordHash(ci.getUserPasswordHash()); database.setMasterUser(user); if (!ci.isUnnamedInMemory()) { DATABASES.put(name, database); if (database.validateFilePasswordHash(cipher, ci.getFilePasswordHash())) { user = database.findUser(ci.getUserName()); if (user != null) { if (!user.validateUserPasswordHash(ci.getUserPasswordHash())) { user = null; DbException er = DbException.get(ErrorCode.WRONG_USER_OR_PASSWORD); database.getTrace(Trace.DATABASE).error(er, "wrong user or password; user: \"" + ci.getUserName() + "\"");
String baseDir = SysProperties.getBaseDir(); if (baseDir != null) { ci.setBaseDir(baseDir); int id = getNextId(TraceObject.CONNECTION); setTrace(trace, TraceObject.CONNECTION, id); this.user = ci.getUserName(); if (isInfoEnabled()) { trace.infoCode("Connection " + getTraceObjectName() + " = DriverManager.getConnection(" + quote(ci.getOriginalURL()) + ", " + quote(user) + ", \"\");"); this.url = ci.getURL(); scopeGeneratedKeys = ci.getProperty("SCOPE_GENERATED_KEYS", false); closeOld(); watcher = CloseWatcher.register(this, session, keepOpenStackTrace);
ConnectionInfo ci = new ConnectionInfo(url, info); if (ci.isRemote() || !ci.isPersistent()) { return null; String name = ci.getName(); if (FileUtils.exists(name + Constants.SUFFIX_PAGE_FILE)) { return null; return null; if (ci.removeProperty("NO_UPGRADE", false)) { return connectWithOldVersion(url, info);
if (ci.isRemote()) { connectServer(ci); return this; boolean autoServerMode = ci.getProperty("AUTO_SERVER", false); ConnectionInfo backup = null; try { if (autoServerMode) { backup = ci.clone(); connectionInfo = ci.clone(); ci.setProperty("OPEN_NEW", "true"); getSQL(); if (serverKey != null) { backup.setServerKey(serverKey); backup.removeProperty("OPEN_NEW", null); connectServer(backup); return this;
public Database(String name, ConnectionInfo ci, String cipher) throws SQLException { this.compareMode = new CompareMode(null, null, 0); this.persistent = ci.isPersistent(); this.filePasswordHash = ci.getFilePasswordHash(); this.databaseName = name; this.databaseShortName = parseDatabaseShortName(); this.cipher = cipher; String lockMethodName = ci.removeProperty("FILE_LOCK", null); this.accessModeLog = ci.removeProperty("ACCESS_MODE_LOG", "rw").toLowerCase(); this.accessModeData = ci.removeProperty("ACCESS_MODE_DATA", "rw").toLowerCase(); if ("r".equals(accessModeData)) { readOnly = true; this.textStorage = ci.getTextStorage(); this.databaseURL = ci.getURL(); this.eventListener = ci.removeDatabaseEventListenerObject(); if (eventListener == null) { String listener = ci.removeProperty("DATABASE_EVENT_LISTENER", null); if (listener != null) { if (listener.startsWith("'")) { String log = ci.getProperty(SetTypes.LOG, null); if (log != null) { this.logIndexChanges = "2".equals(log); String ignoreSummary = ci.getProperty("RECOVER", null); if (ignoreSummary != null) { this.recovery = true; this.multiVersion = ci.removeProperty("MVCC", false);
private synchronized Session openSession(ConnectionInfo ci) { boolean ifExists = ci.removeProperty("IFEXISTS", false); boolean ignoreUnknownSetting = ci.removeProperty( "IGNORE_UNKNOWN_SETTINGS", false); String cipher = ci.removeProperty("CIPHER", null); String init = ci.removeProperty("INIT", null); Session session; for (int i = 0;; i++) { session.setAllowLiterals(true); DbSettings defaultSettings = DbSettings.getDefaultSettings(); for (String setting : ci.getKeys()) { if (defaultSettings.containsKey(setting)) { String value = ci.getProperty(setting); try { CommandInterface command = session.prepareCommand( if (e.getErrorCode() == ErrorCode.ADMIN_RIGHTS_REQUIRED) { session.getTrace().error(e, "admin rights required; user: \"" + ci.getUserName() + "\""); } else { session.getTrace().error(e, "");
String password = readString(); try { ConnectionInfo ci = new ConnectionInfo(databaseName); String baseDir = server.getBaseDir(); if (baseDir == null) { ci.setBaseDir(baseDir); ci.setProperty("IFEXISTS", "TRUE"); ci.setProperty("MODE", "PostgreSQL"); ci.setOriginalURL("jdbc:h2:" + databaseName + ";MODE=PostgreSQL"); ci.setUserName(userName); ci.setProperty("PASSWORD", password); ci.readPasswords(); conn = new JdbcConnection(ci, false);
info.put("PASSWORD", password); String url = "jdbc:h2:" + databaseName; ConnectionInfo ci = new ConnectionInfo(url, info); String baseDir = server.getBaseDir(); if (baseDir == null) { ci.setBaseDir(baseDir); ci.setProperty("IFEXISTS", "TRUE");
private Session openSession(ConnectionInfo ci, boolean ifExists, String cipher) throws SQLException { String name = ci.getName(); Database database; if (ci.isUnnamed()) { database = null; } else { user = new User(database, database.allocateObjectId(false, true), ci.getUserName(), false); user.setAdmin(true); user.setUserPasswordHash(ci.getUserPasswordHash()); database.setMasterUser(user); if (!ci.isUnnamed()) { databases.put(name, database); if (database.validateFilePasswordHash(cipher, ci.getFilePasswordHash())) { user = database.findUser(ci.getUserName()); if (user != null) { if (!user.validateUserPasswordHash(ci.getUserPasswordHash())) { user = null;
private static void upgrade(ConnectionInfo ci, Properties info) throws SQLException { String name = ci.getName(); String data = name + Constants.SUFFIX_OLD_DATABASE_FILE; String index = name + ".index.db"; String cipher = ci.getProperty("CIPHER", null); if (cipher != null) { oldUrl += ";CIPHER=" + cipher; FileUtils.move(lobs, backupLobs); ci.removeProperty("IFEXISTS", false); conn = new JdbcConnection(ci, true); stat = conn.createStatement();
private Session createSessionAndValidate(ConnectionInfo ci) { try { ConnectionInfo backup = null; String lockMethodName = ci.getProperty("FILE_LOCK", null); FileLockMethod fileLockMethod = FileLock.getFileLockMethod(lockMethodName); if (fileLockMethod == FileLockMethod.SERIALIZED) { // In serialized mode, database instance sharing is not possible ci.setProperty("OPEN_NEW", "TRUE"); try { backup = ci.clone(); } catch (CloneNotSupportedException e) { throw DbException.convert(e); } } Session session = openSession(ci); validateUserAndPassword(true); if (backup != null) { session.setConnectionInfo(backup); } return session; } catch (DbException e) { if (e.getErrorCode() == ErrorCode.WRONG_USER_OR_PASSWORD) { validateUserAndPassword(false); } throw e; } }
/** * INTERNAL */ public JdbcConnection(String url, Properties info) throws SQLException { this(new ConnectionInfo(url, info), true); }
/** * Get a boolean property if it is set and return the value. * * @param key the property name * @param defaultValue the default value * @return the value */ public boolean getProperty(String key, boolean defaultValue) { return Utils.parseBoolean(getProperty(key, null), defaultValue, false); }
private synchronized Session openSession(ConnectionInfo ci) throws SQLException { boolean ifExists = ci.removeProperty("IFEXISTS", false); boolean ignoreUnknownSetting = ci.removeProperty("IGNORE_UNKNOWN_SETTINGS", false); String cipher = ci.removeProperty("CIPHER", null); Session session; while (true) { String[] keys = ci.getKeys(); session.setAllowLiterals(true); for (int i = 0; i < keys.length; i++) { String setting = keys[i]; String value = ci.getProperty(setting); try { CommandInterface command = session.prepareCommand("SET " + Parser.quoteIdentifier(setting) + " "