Пример #1
0
void SQLiteDatabase::enableAuthorizer(bool enable)
{
    if (m_authorizer && enable)
        sqlite3_set_authorizer(m_db, SQLiteDatabase::authorizerFunction, m_authorizer.get());
    else
        sqlite3_set_authorizer(m_db, NULL, 0);
}
Пример #2
0
bool PDOSqliteConnection::create(CArrRef options) {
  String filename = data_source.substr(0,1) == ":" ? String(data_source) :
                    File::TranslatePath(data_source);
  if (filename.empty()) {
    throw_pdo_exception(0, Array(),
                        "safe_mode/open_basedir prohibits opening %s",
                        data_source.c_str());
    return false;
  }

  if (sqlite3_open(filename.data(), &m_db) != SQLITE_OK) {
    handleError(__FILE__, __LINE__);
    return false;
  }

  sqlite3_set_authorizer(m_db, authorizer, NULL);

  long timeout = 60;
  if (options.exists(PDO_ATTR_TIMEOUT)) {
    timeout = options[PDO_ATTR_TIMEOUT].toInt64();
  }
  sqlite3_busy_timeout(m_db, timeout * 1000);

  return true;
}
Пример #3
0
static int pdo_sqlite_handle_factory(pdo_dbh_t *dbh, zval *driver_options) /* {{{ */
{
	pdo_sqlite_db_handle *H;
	int i, ret = 0;
	zend_long timeout = 60, flags;
	char *filename;

	H = pecalloc(1, sizeof(pdo_sqlite_db_handle), dbh->is_persistent);

	H->einfo.errcode = 0;
	H->einfo.errmsg = NULL;
	dbh->driver_data = H;

	filename = make_filename_safe(dbh->data_source);

	if (!filename) {
		zend_throw_exception_ex(php_pdo_get_exception(), 0,
			"open_basedir prohibits opening %s",
			dbh->data_source);
		goto cleanup;
	}

	flags = pdo_attr_lval(driver_options, PDO_SQLITE_ATTR_OPEN_FLAGS, SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE);

#if SQLITE_VERSION_NUMBER >= 3005000
	i = sqlite3_open_v2(filename, &H->db, flags, NULL);
#else
	i = sqlite3_open(filename, &H->db);
#endif

	efree(filename);

	if (i != SQLITE_OK) {
		pdo_sqlite_error(dbh);
		goto cleanup;
	}

	if (PG(open_basedir) && *PG(open_basedir)) {
		sqlite3_set_authorizer(H->db, authorizer, NULL);
	}

	if (driver_options) {
		timeout = pdo_attr_lval(driver_options, PDO_ATTR_TIMEOUT, timeout);
	}
	sqlite3_busy_timeout(H->db, timeout * 1000);

	dbh->alloc_own_columns = 1;
	dbh->max_escaped_char_length = 2;

	ret = 1;

cleanup:
	dbh->methods = &sqlite_methods;

	return ret;
}
Пример #4
0
int main(int argc, char **argv)
{
    sqlite3 *db;
    int rc;
    char *sql;
    int is_fine = 1;

    if (access("demo.db", R_OK|W_OK)) {
        is_fine = 0;
    }

    rc = sqlite3_open("demo.db", &db);

    if(rc) {
        fprintf(stderr, "Can't open database: %s\n", sqlite3_errmsg(db));
        goto quit;
    }

    /* initialize db */
    if (!is_fine) {
        rc = _db_init(db);
        if (rc) {
            goto quit;
        }
    }

    _db_show_all(db);
    char name[32];
    db_get_account_name_by_id(db, name, 2);
    fprintf(stdout, "name of id %d is %s\n", 2, name);
    
    /* register hooks */
    char *data = "this is a hook test";
    sqlite3_commit_hook(db, _db_commit_hook, data);
    sqlite3_update_hook(db, _db_update_hook, data);
    sqlite3_set_authorizer(db, _db_auth_hook, data);

    puts("insert");
    sql = "insert into account (name,rank) values ('guest',2);";
    rc = sqlite3_exec(db, sql, NULL, NULL, NULL);
    _db_show_all(db);

    puts("update");
    sql = "update account set name='test' where id=4;";
    rc = sqlite3_exec(db, sql, NULL, NULL, NULL);
    _db_show_all(db);

    puts("delete");
    sql = "delete from account where name='test';";
    rc = sqlite3_exec(db, sql, NULL, NULL, NULL);
    _db_show_all(db);

quit:
    sqlite3_close(db);
    return 0;
}
Пример #5
0
/* call-seq: set_authorizer = auth
 *
 * Set the authorizer for this database.  +auth+ must respond to +call+, and
 * +call+ must take 5 arguments.
 *
 * Installs (or removes) a block that will be invoked for every access
 * to the database. If the block returns 0 (or +true+), the statement
 * is allowed to proceed. Returning 1 or false causes an authorization error to
 * occur, and returning 2 or nil causes the access to be silently denied.
 */
static VALUE set_authorizer(VALUE self, VALUE authorizer)
{
  sqlite3RubyPtr ctx;
  int status;

  Data_Get_Struct(self, sqlite3Ruby, ctx);
  REQUIRE_OPEN_DB(ctx);

  status = sqlite3_set_authorizer(
      ctx->db, NIL_P(authorizer) ? NULL : rb_sqlite3_auth, (void *)self
  );

  CHECK(ctx->db, status);

  rb_iv_set(self, "@authorizer", authorizer);

  return self;
}
Пример #6
0
 void database::set_authorize_handler(authorize_handler h)
 {
     ah_ = h;
     sqlite3_set_authorizer(db_, ah_ ? authorizer_impl : 0, &ah_);
 }
Пример #7
0
/*-------------------------------------------------------------------------*/
svalue_t * 
f_sl_open (svalue_t *sp) 

/* EFUN sl_open
 *
 *   int sl_open(string filename)
 *
 * Opens the file <filename> for use as a SQLite database.
 * If the file doesn't exists it will be created.
 * Only one open file per object is allowed. On success this
 * function returns 1, otherwise usually an error is thrown.
 */

{
    string_t *file;
    sqlite3 *db;
    sqlite_dbs_t *tmp;
    int err;
   
    file = check_valid_path(sp->u.str, current_object, STR_SQLITE_OPEN , MY_TRUE);
    if (!file)
        errorf("Illegal use of sl_open('%s')\n", get_txt(sp->u.str));
   
    tmp = find_db (current_object);
    if (tmp)
    {
        free_mstring(file);
        errorf("The current object already has a database open.\n");
    }

    err = sqlite3_open (get_txt(file), &db);
    free_mstring(file);
    if (err)
    {
        const char* msg = sqlite3_errmsg(db);
        sqlite3_close(db);
        errorf("sl_open: %s\n", msg );
        /* NOTREACHED */
    }

    /* create a new chain link and hang on the old chain */
    tmp = new_db(); 
    if(!tmp)
    {
        sqlite3_close(db);
        errorf("(sl_open) Out of memory: (%lu bytes)\n",
                (unsigned long) sizeof(*tmp));
    }
   
    tmp->db = db;
    tmp->obj = current_object;
    current_object->open_sqlite_db = MY_TRUE;

    /* Synchronous is damn slow. Forget it. */
    sqlite3_exec(db, "PRAGMA synchronous = OFF", NULL, NULL, NULL);
    sqlite3_set_authorizer(db, my_sqlite3_authorizer, NULL);
  
    free_string_svalue (sp);
    put_number (sp, 1);
    return sp;
} /* f_sl_open() */
Пример #8
0
DLL_FUNCTION(int32_t) BU_SQLite_Set_Authorizer(sqlite3* db,
											  int(*xAuth)(void*, int, const char*, const char*, const char*, const char*),
											  void *pArg) {
#pragma comment(linker, "/EXPORT:BU_SQLite_Set_Authorizer=_BU_SQLite_Set_Authorizer@12")
	return sqlite3_set_authorizer(db, xAuth, pArg);
}
Пример #9
0
/**
@SYMTestCaseID			SYSLIB-SQLITE3-UT-4004
@SYMTestCaseDesc		Database handle SQLITE3 tests.
						List of called SQLITE3 functions:
						 - sqlite3_config;
						 - sqlite3_initialize;
						 - sqlite3_threadsafe;
						 - sqlite3_vfs_find;
						 - sqlite3_open;
						 - sqlite3_db_config;
						 - sqlite3_libversion;
						 - sqlite3_libversion_number;
						 - sqlite3_set_authorizer;
						 - sqlite3_commit_hook;
						 - sqlite3_rollback_hook;
						 - sqlite3_update_hook;
						 - sqlite3_close;
						 - sqlite3_shutdown;
@SYMTestPriority		High
@SYMTestActions			Database handle SQLITE3 tests.
@SYMTestExpectedResults Test must not fail
@SYMREQ					REQ8782
*/
static void TestSqliteApi()
	{
	void* prev = 0;
	const char* libverstr = 0;
	int libvernum = 0;
	int err;
	int threadSafe = -1;
	sqlite3_vfs* vfs = 0;

	TEST(!TheDb);
	
	TestStart("@SYMTestCaseID:SYSLIB-SQLITE3-UT-4004: Test \"sqlite3_config()\"");
	err = sqlite3_config(SQLITE_CONFIG_MEMSTATUS, 0);
	TEST2(err, SQLITE_OK);
	
	TestNext("Test \"sqlite3_initialize()\"");
	err = sqlite3_initialize();
	TEST2(err, SQLITE_OK);

	TestNext("Test \"sqlite3_threadsafe()\"");
	threadSafe = sqlite3_threadsafe();
	PrintI("SQLITE_THREADSAFE=%d\r\n", threadSafe);
	
	vfs = sqlite3_vfs_find(0);
	TEST(vfs != NULL);
	PrintS("Vfs name=\"%s\"\r\n", vfs->zName);
	
	err = sqlite3_open(TheTestDbName, &TheDb);
	TEST2(err, SQLITE_OK);
	TEST(TheDb != 0);
	
	err = sqlite3_db_config(TheDb, SQLITE_DBCONFIG_LOOKASIDE, 0, 128, 100);
	TEST2(err, SQLITE_OK);
	
	libverstr = sqlite3_libversion();
	libvernum = sqlite3_libversion_number();
	PrintSI("SQLITE version: \"%s\", Number: %d\r\n", libverstr, libvernum);
	
	err = sqlite3_set_authorizer(TheDb, &authorizer_callback, 0);
	TEST2(err, SQLITE_OK);
	
	prev = sqlite3_commit_hook(TheDb, &commit_hook, 0);
	TEST(!prev);
	prev = sqlite3_rollback_hook(TheDb, &rollback_hook, 0);
	TEST(!prev);
	prev = sqlite3_update_hook(TheDb, &update_hook, 0);
	TEST(!prev);
	
	TestNext("@SYMTestCaseID:SYSLIB-SQLITE3-UT-4001: Test \"sqlite3\" handle API");
	TestExec();
	TestNext("@SYMTestCaseID:SYSLIB-SQLITE3-UT-4002: Test \"sqlite3_stmt\" handle API-1");
	TestStatement1();
	TestNext("@SYMTestCaseID:SYSLIB-SQLITE3-UT-4003: Test \"sqlite3_stmt\" handle API-2");
	TestStatement2();
	TestNext("@SYMTestCaseID:PDS-SQLITE3-UT-4038: Test more sqlite3 API");
	TestSqliteApi2();
	TestNext("@SYMTestCaseID:PDS-SQLITE3-UT-4039: Test blob API");
	TestSqliteBlobApi();
	TestNext("@SYMTestCaseID:PDS-SQLITE3-UT-4040: Test mutex API");
	TestSqliteMutexApi();

	err = sqlite3_close(TheDb);
	TEST2(err, SQLITE_OK);
	TheDb = 0;
	
	TestNext("Test \"sqlite3_shutdown()\"");
	err = sqlite3_shutdown();
	TEST2(err, SQLITE_OK);
	
	err = remove(TheTestDbName);
	TEST2(err, 0);
	}