From: drh Date: Thu, 11 Sep 2014 16:19:31 +0000 (+0000) Subject: Enhance the sqlite3_user_add() interface to initialize the user X-Git-Tag: version-3.8.7~120^2~2 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=7883ecfcd40c8bcf60cb69a36459f6f4f6242824;p=thirdparty%2Fsqlite.git Enhance the sqlite3_user_add() interface to initialize the user authentication logic. Add test cases for the extra argument on the end of the authorizer callback. FossilOrigin-Name: 842c6da8f1a62bd13a1b4089a98b0835a46a2285 --- diff --git a/ext/userauth/userauth.c b/ext/userauth/userauth.c index 21d33ce80f..d368df8f9d 100644 --- a/ext/userauth/userauth.c +++ b/ext/userauth/userauth.c @@ -118,6 +118,8 @@ int sqlite3UserAuthCheckLogin( ){ int rc; u8 savedAuthLevel; + assert( zDb!=0 ); + assert( peAuth!=0 ); savedAuthLevel = db->auth.authLevel; db->auth.authLevel = UAUTH_Admin; rc = userAuthCheckLogin(db, zDb, peAuth); @@ -125,6 +127,19 @@ int sqlite3UserAuthCheckLogin( return rc; } +/* +** If the current authLevel is UAUTH_Unknown, the take actions to figure +** out what authLevel should be +*/ +void sqlite3UserAuthInit(sqlite3 *db){ + if( db->auth.authLevel==UAUTH_Unknown ){ + u8 authLevel = UAUTH_Fail; + sqlite3UserAuthCheckLogin(db, "main", &authLevel); + db->auth.authLevel = authLevel; + if( authLevelflags &= ~SQLITE_WriteSchema; + } +} + /* ** Implementation of the sqlite_crypt(X,Y) function. ** @@ -223,6 +238,7 @@ int sqlite3_user_add( ){ sqlite3_stmt *pStmt; int rc; + sqlite3UserAuthInit(db); if( db->auth.authLevelnTableLock>0 && db->init.busy==0 ){ + sqlite3UserAuthInit(db); if( db->auth.authLevelauth.authLevel==UAUTH_Unknown ){ - u8 authLevel = UAUTH_Fail; - sqlite3UserAuthCheckLogin(db, "main", &authLevel); - db->auth.authLevel = authLevel; - if( authLevelflags &= ~SQLITE_WriteSchema; - } - if( db->auth.authLevelrc = SQLITE_AUTH_USER; - sqlite3ErrorMsg(pParse, "user not authenticated"); - return; - } + pParse->rc = SQLITE_AUTH_USER; + sqlite3ErrorMsg(pParse, "user not authenticated"); + return; } } #endif diff --git a/src/shell.c b/src/shell.c index b8ab2dbe9f..ec83b13910 100644 --- a/src/shell.c +++ b/src/shell.c @@ -3445,6 +3445,7 @@ static int do_meta_command(char *zLine, ShellState *p){ rc = 1; goto meta_command_exit; } + open_db(p, 0); if( strcmp(azArg[1],"login")==0 ){ if( nArg!=4 ){ fprintf(stderr, "Usage: .user login USER PASSWORD\n"); diff --git a/src/sqliteInt.h b/src/sqliteInt.h index a9f2001457..805c925f79 100644 --- a/src/sqliteInt.h +++ b/src/sqliteInt.h @@ -1010,6 +1010,7 @@ struct sqlite3_userauth { /* Functions used only by user authorization logic */ int sqlite3UserAuthTable(const char*); int sqlite3UserAuthCheckLogin(sqlite3*,const char*,u8*); +void sqlite3UserAuthInit(sqlite3*); void sqlite3CryptFunc(sqlite3_context*,int,sqlite3_value**); #endif /* SQLITE_USER_AUTHENTICATION */ diff --git a/test/userauth01.test b/test/userauth01.test index a4621dc72f..644937b192 100644 --- a/test/userauth01.test +++ b/test/userauth01.test @@ -209,14 +209,14 @@ do_test userauth01-1.51 { do_test userauth01-1.60 { forcedelete test3.db sqlite3 db3 test3.db + sqlite3_user_add db3 alice xyzzy-alice 1 +} {SQLITE_OK} +do_test userauth01-1.61 { db3 eval { CREATE TABLE t3(a,b,c); INSERT INTO t3 VALUES(1,2,3); SELECT * FROM t3; } } {1 2 3} -do_test userauth01-1.61 { - sqlite3_user_add db3 alice xyzzy-alice 1 -} {SQLITE_OK} do_test userauth01-1.62 { db eval { ATTACH 'test3.db' AS aux; @@ -238,4 +238,20 @@ do_test userauth01-1.65 { db eval {PRAGMA database_list} } {~/test3.db/} +# The sqlite3_set_authorizer() callback is modified to take a 7th parameter +# which is the username of the currently logged in user, or NULL for a +# no-authentication-required database. +# +proc auth {args} { + lappend ::authargs $args + return SQLITE_OK +} +do_test authuser01-2.1 { + unset -nocomplain ::authargs + db auth auth + db eval {SELECT x FROM t1} + set ::authargs +} {/SQLITE_SELECT {} {} {} {} alice/} + + finish_test