17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate ** 2003 January 11
37c478bd9Sstevel@tonic-gate **
47c478bd9Sstevel@tonic-gate ** The author disclaims copyright to this source code. In place of
57c478bd9Sstevel@tonic-gate ** a legal notice, here is a blessing:
67c478bd9Sstevel@tonic-gate **
77c478bd9Sstevel@tonic-gate ** May you do good and not evil.
87c478bd9Sstevel@tonic-gate ** May you find forgiveness for yourself and forgive others.
97c478bd9Sstevel@tonic-gate ** May you share freely, never taking more than you give.
107c478bd9Sstevel@tonic-gate **
117c478bd9Sstevel@tonic-gate *************************************************************************
127c478bd9Sstevel@tonic-gate ** This file contains code used to implement the sqlite_set_authorizer()
137c478bd9Sstevel@tonic-gate ** API. This facility is an optional feature of the library. Embedded
147c478bd9Sstevel@tonic-gate ** systems that do not need this facility may omit it by recompiling
157c478bd9Sstevel@tonic-gate ** the library with -DSQLITE_OMIT_AUTHORIZATION=1
167c478bd9Sstevel@tonic-gate **
177c478bd9Sstevel@tonic-gate ** $Id: auth.c,v 1.12.2.1 2004/06/14 11:58:37 drh Exp $
187c478bd9Sstevel@tonic-gate */
197c478bd9Sstevel@tonic-gate #include "sqliteInt.h"
207c478bd9Sstevel@tonic-gate
217c478bd9Sstevel@tonic-gate /*
227c478bd9Sstevel@tonic-gate ** All of the code in this file may be omitted by defining a single
237c478bd9Sstevel@tonic-gate ** macro.
247c478bd9Sstevel@tonic-gate */
257c478bd9Sstevel@tonic-gate #ifndef SQLITE_OMIT_AUTHORIZATION
267c478bd9Sstevel@tonic-gate
277c478bd9Sstevel@tonic-gate /*
287c478bd9Sstevel@tonic-gate ** Set or clear the access authorization function.
297c478bd9Sstevel@tonic-gate **
307c478bd9Sstevel@tonic-gate ** The access authorization function is be called during the compilation
317c478bd9Sstevel@tonic-gate ** phase to verify that the user has read and/or write access permission on
327c478bd9Sstevel@tonic-gate ** various fields of the database. The first argument to the auth function
337c478bd9Sstevel@tonic-gate ** is a copy of the 3rd argument to this routine. The second argument
347c478bd9Sstevel@tonic-gate ** to the auth function is one of these constants:
357c478bd9Sstevel@tonic-gate **
367c478bd9Sstevel@tonic-gate ** SQLITE_COPY
377c478bd9Sstevel@tonic-gate ** SQLITE_CREATE_INDEX
387c478bd9Sstevel@tonic-gate ** SQLITE_CREATE_TABLE
397c478bd9Sstevel@tonic-gate ** SQLITE_CREATE_TEMP_INDEX
407c478bd9Sstevel@tonic-gate ** SQLITE_CREATE_TEMP_TABLE
417c478bd9Sstevel@tonic-gate ** SQLITE_CREATE_TEMP_TRIGGER
427c478bd9Sstevel@tonic-gate ** SQLITE_CREATE_TEMP_VIEW
437c478bd9Sstevel@tonic-gate ** SQLITE_CREATE_TRIGGER
447c478bd9Sstevel@tonic-gate ** SQLITE_CREATE_VIEW
457c478bd9Sstevel@tonic-gate ** SQLITE_DELETE
467c478bd9Sstevel@tonic-gate ** SQLITE_DROP_INDEX
477c478bd9Sstevel@tonic-gate ** SQLITE_DROP_TABLE
487c478bd9Sstevel@tonic-gate ** SQLITE_DROP_TEMP_INDEX
497c478bd9Sstevel@tonic-gate ** SQLITE_DROP_TEMP_TABLE
507c478bd9Sstevel@tonic-gate ** SQLITE_DROP_TEMP_TRIGGER
517c478bd9Sstevel@tonic-gate ** SQLITE_DROP_TEMP_VIEW
527c478bd9Sstevel@tonic-gate ** SQLITE_DROP_TRIGGER
537c478bd9Sstevel@tonic-gate ** SQLITE_DROP_VIEW
547c478bd9Sstevel@tonic-gate ** SQLITE_INSERT
557c478bd9Sstevel@tonic-gate ** SQLITE_PRAGMA
567c478bd9Sstevel@tonic-gate ** SQLITE_READ
577c478bd9Sstevel@tonic-gate ** SQLITE_SELECT
587c478bd9Sstevel@tonic-gate ** SQLITE_TRANSACTION
597c478bd9Sstevel@tonic-gate ** SQLITE_UPDATE
607c478bd9Sstevel@tonic-gate **
617c478bd9Sstevel@tonic-gate ** The third and fourth arguments to the auth function are the name of
627c478bd9Sstevel@tonic-gate ** the table and the column that are being accessed. The auth function
637c478bd9Sstevel@tonic-gate ** should return either SQLITE_OK, SQLITE_DENY, or SQLITE_IGNORE. If
647c478bd9Sstevel@tonic-gate ** SQLITE_OK is returned, it means that access is allowed. SQLITE_DENY
657c478bd9Sstevel@tonic-gate ** means that the SQL statement will never-run - the sqlite_exec() call
667c478bd9Sstevel@tonic-gate ** will return with an error. SQLITE_IGNORE means that the SQL statement
677c478bd9Sstevel@tonic-gate ** should run but attempts to read the specified column will return NULL
687c478bd9Sstevel@tonic-gate ** and attempts to write the column will be ignored.
697c478bd9Sstevel@tonic-gate **
707c478bd9Sstevel@tonic-gate ** Setting the auth function to NULL disables this hook. The default
717c478bd9Sstevel@tonic-gate ** setting of the auth function is NULL.
727c478bd9Sstevel@tonic-gate */
sqlite_set_authorizer(sqlite * db,int (* xAuth)(void *,int,const char *,const char *,const char *,const char *),void * pArg)737c478bd9Sstevel@tonic-gate int sqlite_set_authorizer(
747c478bd9Sstevel@tonic-gate sqlite *db,
757c478bd9Sstevel@tonic-gate int (*xAuth)(void*,int,const char*,const char*,const char*,const char*),
767c478bd9Sstevel@tonic-gate void *pArg
777c478bd9Sstevel@tonic-gate ){
787c478bd9Sstevel@tonic-gate db->xAuth = xAuth;
797c478bd9Sstevel@tonic-gate db->pAuthArg = pArg;
807c478bd9Sstevel@tonic-gate return SQLITE_OK;
817c478bd9Sstevel@tonic-gate }
827c478bd9Sstevel@tonic-gate
837c478bd9Sstevel@tonic-gate /*
847c478bd9Sstevel@tonic-gate ** Write an error message into pParse->zErrMsg that explains that the
857c478bd9Sstevel@tonic-gate ** user-supplied authorization function returned an illegal value.
867c478bd9Sstevel@tonic-gate */
sqliteAuthBadReturnCode(Parse * pParse,int rc)877c478bd9Sstevel@tonic-gate static void sqliteAuthBadReturnCode(Parse *pParse, int rc){
887c478bd9Sstevel@tonic-gate sqliteErrorMsg(pParse, "illegal return value (%d) from the "
897c478bd9Sstevel@tonic-gate "authorization function - should be SQLITE_OK, SQLITE_IGNORE, "
907c478bd9Sstevel@tonic-gate "or SQLITE_DENY", rc);
917c478bd9Sstevel@tonic-gate pParse->rc = SQLITE_MISUSE;
927c478bd9Sstevel@tonic-gate }
937c478bd9Sstevel@tonic-gate
947c478bd9Sstevel@tonic-gate /*
957c478bd9Sstevel@tonic-gate ** The pExpr should be a TK_COLUMN expression. The table referred to
96*1da57d55SToomas Soome ** is in pTabList or else it is the NEW or OLD table of a trigger.
977c478bd9Sstevel@tonic-gate ** Check to see if it is OK to read this particular column.
987c478bd9Sstevel@tonic-gate **
99*1da57d55SToomas Soome ** If the auth function returns SQLITE_IGNORE, change the TK_COLUMN
1007c478bd9Sstevel@tonic-gate ** instruction into a TK_NULL. If the auth function returns SQLITE_DENY,
1017c478bd9Sstevel@tonic-gate ** then generate an error.
1027c478bd9Sstevel@tonic-gate */
sqliteAuthRead(Parse * pParse,Expr * pExpr,SrcList * pTabList)1037c478bd9Sstevel@tonic-gate void sqliteAuthRead(
1047c478bd9Sstevel@tonic-gate Parse *pParse, /* The parser context */
1057c478bd9Sstevel@tonic-gate Expr *pExpr, /* The expression to check authorization on */
1067c478bd9Sstevel@tonic-gate SrcList *pTabList /* All table that pExpr might refer to */
1077c478bd9Sstevel@tonic-gate ){
1087c478bd9Sstevel@tonic-gate sqlite *db = pParse->db;
1097c478bd9Sstevel@tonic-gate int rc;
1107c478bd9Sstevel@tonic-gate Table *pTab; /* The table being read */
1117c478bd9Sstevel@tonic-gate const char *zCol; /* Name of the column of the table */
1127c478bd9Sstevel@tonic-gate int iSrc; /* Index in pTabList->a[] of table being read */
1137c478bd9Sstevel@tonic-gate const char *zDBase; /* Name of database being accessed */
1147c478bd9Sstevel@tonic-gate
1157c478bd9Sstevel@tonic-gate if( db->xAuth==0 ) return;
1167c478bd9Sstevel@tonic-gate assert( pExpr->op==TK_COLUMN );
1177c478bd9Sstevel@tonic-gate for(iSrc=0; iSrc<pTabList->nSrc; iSrc++){
1187c478bd9Sstevel@tonic-gate if( pExpr->iTable==pTabList->a[iSrc].iCursor ) break;
1197c478bd9Sstevel@tonic-gate }
1207c478bd9Sstevel@tonic-gate if( iSrc>=0 && iSrc<pTabList->nSrc ){
1217c478bd9Sstevel@tonic-gate pTab = pTabList->a[iSrc].pTab;
1227c478bd9Sstevel@tonic-gate }else{
1237c478bd9Sstevel@tonic-gate /* This must be an attempt to read the NEW or OLD pseudo-tables
1247c478bd9Sstevel@tonic-gate ** of a trigger.
1257c478bd9Sstevel@tonic-gate */
1267c478bd9Sstevel@tonic-gate TriggerStack *pStack; /* The stack of current triggers */
1277c478bd9Sstevel@tonic-gate pStack = pParse->trigStack;
1287c478bd9Sstevel@tonic-gate assert( pStack!=0 );
1297c478bd9Sstevel@tonic-gate assert( pExpr->iTable==pStack->newIdx || pExpr->iTable==pStack->oldIdx );
1307c478bd9Sstevel@tonic-gate pTab = pStack->pTab;
1317c478bd9Sstevel@tonic-gate }
1327c478bd9Sstevel@tonic-gate if( pTab==0 ) return;
1337c478bd9Sstevel@tonic-gate if( pExpr->iColumn>=0 ){
1347c478bd9Sstevel@tonic-gate assert( pExpr->iColumn<pTab->nCol );
1357c478bd9Sstevel@tonic-gate zCol = pTab->aCol[pExpr->iColumn].zName;
1367c478bd9Sstevel@tonic-gate }else if( pTab->iPKey>=0 ){
1377c478bd9Sstevel@tonic-gate assert( pTab->iPKey<pTab->nCol );
1387c478bd9Sstevel@tonic-gate zCol = pTab->aCol[pTab->iPKey].zName;
1397c478bd9Sstevel@tonic-gate }else{
1407c478bd9Sstevel@tonic-gate zCol = "ROWID";
1417c478bd9Sstevel@tonic-gate }
1427c478bd9Sstevel@tonic-gate assert( pExpr->iDb<db->nDb );
1437c478bd9Sstevel@tonic-gate zDBase = db->aDb[pExpr->iDb].zName;
144*1da57d55SToomas Soome rc = db->xAuth(db->pAuthArg, SQLITE_READ, pTab->zName, zCol, zDBase,
1457c478bd9Sstevel@tonic-gate pParse->zAuthContext);
1467c478bd9Sstevel@tonic-gate if( rc==SQLITE_IGNORE ){
1477c478bd9Sstevel@tonic-gate pExpr->op = TK_NULL;
1487c478bd9Sstevel@tonic-gate }else if( rc==SQLITE_DENY ){
1497c478bd9Sstevel@tonic-gate if( db->nDb>2 || pExpr->iDb!=0 ){
150*1da57d55SToomas Soome sqliteErrorMsg(pParse, "access to %s.%s.%s is prohibited",
1517c478bd9Sstevel@tonic-gate zDBase, pTab->zName, zCol);
1527c478bd9Sstevel@tonic-gate }else{
1537c478bd9Sstevel@tonic-gate sqliteErrorMsg(pParse, "access to %s.%s is prohibited", pTab->zName,zCol);
1547c478bd9Sstevel@tonic-gate }
1557c478bd9Sstevel@tonic-gate pParse->rc = SQLITE_AUTH;
1567c478bd9Sstevel@tonic-gate }else if( rc!=SQLITE_OK ){
1577c478bd9Sstevel@tonic-gate sqliteAuthBadReturnCode(pParse, rc);
1587c478bd9Sstevel@tonic-gate }
1597c478bd9Sstevel@tonic-gate }
1607c478bd9Sstevel@tonic-gate
1617c478bd9Sstevel@tonic-gate /*
1627c478bd9Sstevel@tonic-gate ** Do an authorization check using the code and arguments given. Return
1637c478bd9Sstevel@tonic-gate ** either SQLITE_OK (zero) or SQLITE_IGNORE or SQLITE_DENY. If SQLITE_DENY
1647c478bd9Sstevel@tonic-gate ** is returned, then the error count and error message in pParse are
1657c478bd9Sstevel@tonic-gate ** modified appropriately.
1667c478bd9Sstevel@tonic-gate */
sqliteAuthCheck(Parse * pParse,int code,const char * zArg1,const char * zArg2,const char * zArg3)1677c478bd9Sstevel@tonic-gate int sqliteAuthCheck(
1687c478bd9Sstevel@tonic-gate Parse *pParse,
1697c478bd9Sstevel@tonic-gate int code,
1707c478bd9Sstevel@tonic-gate const char *zArg1,
1717c478bd9Sstevel@tonic-gate const char *zArg2,
1727c478bd9Sstevel@tonic-gate const char *zArg3
1737c478bd9Sstevel@tonic-gate ){
1747c478bd9Sstevel@tonic-gate sqlite *db = pParse->db;
1757c478bd9Sstevel@tonic-gate int rc;
1767c478bd9Sstevel@tonic-gate
1777c478bd9Sstevel@tonic-gate if( db->init.busy || db->xAuth==0 ){
1787c478bd9Sstevel@tonic-gate return SQLITE_OK;
1797c478bd9Sstevel@tonic-gate }
1807c478bd9Sstevel@tonic-gate rc = db->xAuth(db->pAuthArg, code, zArg1, zArg2, zArg3, pParse->zAuthContext);
1817c478bd9Sstevel@tonic-gate if( rc==SQLITE_DENY ){
1827c478bd9Sstevel@tonic-gate sqliteErrorMsg(pParse, "not authorized");
1837c478bd9Sstevel@tonic-gate pParse->rc = SQLITE_AUTH;
1847c478bd9Sstevel@tonic-gate }else if( rc!=SQLITE_OK && rc!=SQLITE_IGNORE ){
1857c478bd9Sstevel@tonic-gate rc = SQLITE_DENY;
1867c478bd9Sstevel@tonic-gate sqliteAuthBadReturnCode(pParse, rc);
1877c478bd9Sstevel@tonic-gate }
1887c478bd9Sstevel@tonic-gate return rc;
1897c478bd9Sstevel@tonic-gate }
1907c478bd9Sstevel@tonic-gate
1917c478bd9Sstevel@tonic-gate /*
1927c478bd9Sstevel@tonic-gate ** Push an authorization context. After this routine is called, the
1937c478bd9Sstevel@tonic-gate ** zArg3 argument to authorization callbacks will be zContext until
1947c478bd9Sstevel@tonic-gate ** popped. Or if pParse==0, this routine is a no-op.
1957c478bd9Sstevel@tonic-gate */
sqliteAuthContextPush(Parse * pParse,AuthContext * pContext,const char * zContext)1967c478bd9Sstevel@tonic-gate void sqliteAuthContextPush(
1977c478bd9Sstevel@tonic-gate Parse *pParse,
198*1da57d55SToomas Soome AuthContext *pContext,
1997c478bd9Sstevel@tonic-gate const char *zContext
2007c478bd9Sstevel@tonic-gate ){
2017c478bd9Sstevel@tonic-gate pContext->pParse = pParse;
2027c478bd9Sstevel@tonic-gate if( pParse ){
2037c478bd9Sstevel@tonic-gate pContext->zAuthContext = pParse->zAuthContext;
2047c478bd9Sstevel@tonic-gate pParse->zAuthContext = zContext;
2057c478bd9Sstevel@tonic-gate }
2067c478bd9Sstevel@tonic-gate }
2077c478bd9Sstevel@tonic-gate
2087c478bd9Sstevel@tonic-gate /*
2097c478bd9Sstevel@tonic-gate ** Pop an authorization context that was previously pushed
2107c478bd9Sstevel@tonic-gate ** by sqliteAuthContextPush
2117c478bd9Sstevel@tonic-gate */
sqliteAuthContextPop(AuthContext * pContext)2127c478bd9Sstevel@tonic-gate void sqliteAuthContextPop(AuthContext *pContext){
2137c478bd9Sstevel@tonic-gate if( pContext->pParse ){
2147c478bd9Sstevel@tonic-gate pContext->pParse->zAuthContext = pContext->zAuthContext;
2157c478bd9Sstevel@tonic-gate pContext->pParse = 0;
2167c478bd9Sstevel@tonic-gate }
2177c478bd9Sstevel@tonic-gate }
2187c478bd9Sstevel@tonic-gate
2197c478bd9Sstevel@tonic-gate #endif /* SQLITE_OMIT_AUTHORIZATION */
220