ds@1952: /* ds@1952: ** 2003 January 11 ds@1952: ** ds@1952: ** The author disclaims copyright to this source code. In place of ds@1952: ** a legal notice, here is a blessing: ds@1952: ** ds@1952: ** May you do good and not evil. ds@1952: ** May you find forgiveness for yourself and forgive others. ds@1952: ** May you share freely, never taking more than you give. ds@1952: ** ds@1952: ************************************************************************* ds@1952: ** This file contains code used to implement the sqlite3_set_authorizer() ds@1952: ** API. This facility is an optional feature of the library. Embedded ds@1952: ** systems that do not need this facility may omit it by recompiling ds@1952: ** the library with -DSQLITE_OMIT_AUTHORIZATION=1 ds@1952: ** ds@1952: ** $Id$ ds@1952: */ ds@1952: #include "sqliteInt.h" ds@1952: ds@1952: /* ds@1952: ** All of the code in this file may be omitted by defining a single ds@1952: ** macro. ds@1952: */ ds@1952: #ifndef SQLITE_OMIT_AUTHORIZATION ds@1952: ds@1952: /* ds@1952: ** Set or clear the access authorization function. ds@1952: ** ds@1952: ** The access authorization function is be called during the compilation ds@1952: ** phase to verify that the user has read and/or write access permission on ds@1952: ** various fields of the database. The first argument to the auth function ds@1952: ** is a copy of the 3rd argument to this routine. The second argument ds@1952: ** to the auth function is one of these constants: ds@1952: ** ds@1952: ** SQLITE_CREATE_INDEX ds@1952: ** SQLITE_CREATE_TABLE ds@1952: ** SQLITE_CREATE_TEMP_INDEX ds@1952: ** SQLITE_CREATE_TEMP_TABLE ds@1952: ** SQLITE_CREATE_TEMP_TRIGGER ds@1952: ** SQLITE_CREATE_TEMP_VIEW ds@1952: ** SQLITE_CREATE_TRIGGER ds@1952: ** SQLITE_CREATE_VIEW ds@1952: ** SQLITE_DELETE ds@1952: ** SQLITE_DROP_INDEX ds@1952: ** SQLITE_DROP_TABLE ds@1952: ** SQLITE_DROP_TEMP_INDEX ds@1952: ** SQLITE_DROP_TEMP_TABLE ds@1952: ** SQLITE_DROP_TEMP_TRIGGER ds@1952: ** SQLITE_DROP_TEMP_VIEW ds@1952: ** SQLITE_DROP_TRIGGER ds@1952: ** SQLITE_DROP_VIEW ds@1952: ** SQLITE_INSERT ds@1952: ** SQLITE_PRAGMA ds@1952: ** SQLITE_READ ds@1952: ** SQLITE_SELECT ds@1952: ** SQLITE_TRANSACTION ds@1952: ** SQLITE_UPDATE ds@1952: ** ds@1952: ** The third and fourth arguments to the auth function are the name of ds@1952: ** the table and the column that are being accessed. The auth function ds@1952: ** should return either SQLITE_OK, SQLITE_DENY, or SQLITE_IGNORE. If ds@1952: ** SQLITE_OK is returned, it means that access is allowed. SQLITE_DENY ds@1952: ** means that the SQL statement will never-run - the sqlite3_exec() call ds@1952: ** will return with an error. SQLITE_IGNORE means that the SQL statement ds@1952: ** should run but attempts to read the specified column will return NULL ds@1952: ** and attempts to write the column will be ignored. ds@1952: ** ds@1952: ** Setting the auth function to NULL disables this hook. The default ds@1952: ** setting of the auth function is NULL. ds@1952: */ ds@1952: int sqlite3_set_authorizer( ds@1952: sqlite3 *db, ds@1952: int (*xAuth)(void*,int,const char*,const char*,const char*,const char*), ds@1952: void *pArg ds@1952: ){ ds@1952: sqlite3_mutex_enter(db->mutex); ds@1952: db->xAuth = xAuth; ds@1952: db->pAuthArg = pArg; ds@1952: sqlite3ExpirePreparedStatements(db); ds@1952: sqlite3_mutex_leave(db->mutex); ds@1952: return SQLITE_OK; ds@1952: } ds@1952: ds@1952: /* ds@1952: ** Write an error message into pParse->zErrMsg that explains that the ds@1952: ** user-supplied authorization function returned an illegal value. ds@1952: */ ds@1952: static void sqliteAuthBadReturnCode(Parse *pParse, int rc){ ds@1952: sqlite3ErrorMsg(pParse, "illegal return value (%d) from the " ds@1952: "authorization function - should be SQLITE_OK, SQLITE_IGNORE, " ds@1952: "or SQLITE_DENY", rc); ds@1952: pParse->rc = SQLITE_ERROR; ds@1952: } ds@1952: ds@1952: /* ds@1952: ** The pExpr should be a TK_COLUMN expression. The table referred to ds@1952: ** is in pTabList or else it is the NEW or OLD table of a trigger. ds@1952: ** Check to see if it is OK to read this particular column. ds@1952: ** ds@1952: ** If the auth function returns SQLITE_IGNORE, change the TK_COLUMN ds@1952: ** instruction into a TK_NULL. If the auth function returns SQLITE_DENY, ds@1952: ** then generate an error. ds@1952: */ ds@1952: void sqlite3AuthRead( ds@1952: Parse *pParse, /* The parser context */ ds@1952: Expr *pExpr, /* The expression to check authorization on */ ds@1952: Schema *pSchema, /* The schema of the expression */ ds@1952: SrcList *pTabList /* All table that pExpr might refer to */ ds@1952: ){ ds@1952: sqlite3 *db = pParse->db; ds@1952: int rc; ds@1952: Table *pTab = 0; /* The table being read */ ds@1952: const char *zCol; /* Name of the column of the table */ ds@1952: int iSrc; /* Index in pTabList->a[] of table being read */ ds@1952: const char *zDBase; /* Name of database being accessed */ ds@1952: TriggerStack *pStack; /* The stack of current triggers */ ds@1952: int iDb; /* The index of the database the expression refers to */ ds@1952: ds@1952: if( db->xAuth==0 ) return; ds@1952: if( pExpr->op!=TK_COLUMN ) return; ds@1952: iDb = sqlite3SchemaToIndex(pParse->db, pSchema); ds@1952: if( iDb<0 ){ ds@1952: /* An attempt to read a column out of a subquery or other ds@1952: ** temporary table. */ ds@1952: return; ds@1952: } ds@1952: for(iSrc=0; pTabList && iSrcnSrc; iSrc++){ ds@1952: if( pExpr->iTable==pTabList->a[iSrc].iCursor ) break; ds@1952: } ds@1952: if( iSrc>=0 && pTabList && iSrcnSrc ){ ds@1952: pTab = pTabList->a[iSrc].pTab; ds@1952: }else if( (pStack = pParse->trigStack)!=0 ){ ds@1952: /* This must be an attempt to read the NEW or OLD pseudo-tables ds@1952: ** of a trigger. ds@1952: */ ds@1952: assert( pExpr->iTable==pStack->newIdx || pExpr->iTable==pStack->oldIdx ); ds@1952: pTab = pStack->pTab; ds@1952: } ds@1952: if( pTab==0 ) return; ds@1952: if( pExpr->iColumn>=0 ){ ds@1952: assert( pExpr->iColumnnCol ); ds@1952: zCol = pTab->aCol[pExpr->iColumn].zName; ds@1952: }else if( pTab->iPKey>=0 ){ ds@1952: assert( pTab->iPKeynCol ); ds@1952: zCol = pTab->aCol[pTab->iPKey].zName; ds@1952: }else{ ds@1952: zCol = "ROWID"; ds@1952: } ds@1952: assert( iDb>=0 && iDbnDb ); ds@1952: zDBase = db->aDb[iDb].zName; ds@1952: rc = db->xAuth(db->pAuthArg, SQLITE_READ, pTab->zName, zCol, zDBase, ds@1952: pParse->zAuthContext); ds@1952: if( rc==SQLITE_IGNORE ){ ds@1952: pExpr->op = TK_NULL; ds@1952: }else if( rc==SQLITE_DENY ){ ds@1952: if( db->nDb>2 || iDb!=0 ){ ds@1952: sqlite3ErrorMsg(pParse, "access to %s.%s.%s is prohibited", ds@1952: zDBase, pTab->zName, zCol); ds@1952: }else{ ds@1952: sqlite3ErrorMsg(pParse, "access to %s.%s is prohibited",pTab->zName,zCol); ds@1952: } ds@1952: pParse->rc = SQLITE_AUTH; ds@1952: }else if( rc!=SQLITE_OK ){ ds@1952: sqliteAuthBadReturnCode(pParse, rc); ds@1952: } ds@1952: } ds@1952: ds@1952: /* ds@1952: ** Do an authorization check using the code and arguments given. Return ds@1952: ** either SQLITE_OK (zero) or SQLITE_IGNORE or SQLITE_DENY. If SQLITE_DENY ds@1952: ** is returned, then the error count and error message in pParse are ds@1952: ** modified appropriately. ds@1952: */ ds@1952: int sqlite3AuthCheck( ds@1952: Parse *pParse, ds@1952: int code, ds@1952: const char *zArg1, ds@1952: const char *zArg2, ds@1952: const char *zArg3 ds@1952: ){ ds@1952: sqlite3 *db = pParse->db; ds@1952: int rc; ds@1952: ds@1952: /* Don't do any authorization checks if the database is initialising ds@1952: ** or if the parser is being invoked from within sqlite3_declare_vtab. ds@1952: */ ds@1952: if( db->init.busy || IN_DECLARE_VTAB ){ ds@1952: return SQLITE_OK; ds@1952: } ds@1952: ds@1952: if( db->xAuth==0 ){ ds@1952: return SQLITE_OK; ds@1952: } ds@1952: rc = db->xAuth(db->pAuthArg, code, zArg1, zArg2, zArg3, pParse->zAuthContext); ds@1952: if( rc==SQLITE_DENY ){ ds@1952: sqlite3ErrorMsg(pParse, "not authorized"); ds@1952: pParse->rc = SQLITE_AUTH; ds@1952: }else if( rc!=SQLITE_OK && rc!=SQLITE_IGNORE ){ ds@1952: rc = SQLITE_DENY; ds@1952: sqliteAuthBadReturnCode(pParse, rc); ds@1952: } ds@1952: return rc; ds@1952: } ds@1952: ds@1952: /* ds@1952: ** Push an authorization context. After this routine is called, the ds@1952: ** zArg3 argument to authorization callbacks will be zContext until ds@1952: ** popped. Or if pParse==0, this routine is a no-op. ds@1952: */ ds@1952: void sqlite3AuthContextPush( ds@1952: Parse *pParse, ds@1952: AuthContext *pContext, ds@1952: const char *zContext ds@1952: ){ ds@1952: pContext->pParse = pParse; ds@1952: if( pParse ){ ds@1952: pContext->zAuthContext = pParse->zAuthContext; ds@1952: pParse->zAuthContext = zContext; ds@1952: } ds@1952: } ds@1952: ds@1952: /* ds@1952: ** Pop an authorization context that was previously pushed ds@1952: ** by sqlite3AuthContextPush ds@1952: */ ds@1952: void sqlite3AuthContextPop(AuthContext *pContext){ ds@1952: if( pContext->pParse ){ ds@1952: pContext->pParse->zAuthContext = pContext->zAuthContext; ds@1952: pContext->pParse = 0; ds@1952: } ds@1952: } ds@1952: ds@1952: #endif /* SQLITE_OMIT_AUTHORIZATION */