auth.c

Go to the documentation of this file.
00001 /*
00002 ** 2003 January 11
00003 **
00004 ** The author disclaims copyright to this source code.  In place of
00005 ** a legal notice, here is a blessing:
00006 **
00007 **    May you do good and not evil.
00008 **    May you find forgiveness for yourself and forgive others.
00009 **    May you share freely, never taking more than you give.
00010 **
00011 *************************************************************************
00012 ** This file contains code used to implement the sqlite3_set_authorizer()
00013 ** API.  This facility is an optional feature of the library.  Embedded
00014 ** systems that do not need this facility may omit it by recompiling
00015 ** the library with -DSQLITE_OMIT_AUTHORIZATION=1
00016 **
00017 ** $Id: auth.c,v 1.29 2007/09/18 15:55:07 drh Exp $
00018 */
00019 #include "sqliteInt.h"
00020 
00021 /*
00022 ** All of the code in this file may be omitted by defining a single
00023 ** macro.
00024 */
00025 #ifndef SQLITE_OMIT_AUTHORIZATION
00026 
00027 /*
00028 ** Set or clear the access authorization function.
00029 **
00030 ** The access authorization function is be called during the compilation
00031 ** phase to verify that the user has read and/or write access permission on
00032 ** various fields of the database.  The first argument to the auth function
00033 ** is a copy of the 3rd argument to this routine.  The second argument
00034 ** to the auth function is one of these constants:
00035 **
00036 **       SQLITE_CREATE_INDEX
00037 **       SQLITE_CREATE_TABLE
00038 **       SQLITE_CREATE_TEMP_INDEX
00039 **       SQLITE_CREATE_TEMP_TABLE
00040 **       SQLITE_CREATE_TEMP_TRIGGER
00041 **       SQLITE_CREATE_TEMP_VIEW
00042 **       SQLITE_CREATE_TRIGGER
00043 **       SQLITE_CREATE_VIEW
00044 **       SQLITE_DELETE
00045 **       SQLITE_DROP_INDEX
00046 **       SQLITE_DROP_TABLE
00047 **       SQLITE_DROP_TEMP_INDEX
00048 **       SQLITE_DROP_TEMP_TABLE
00049 **       SQLITE_DROP_TEMP_TRIGGER
00050 **       SQLITE_DROP_TEMP_VIEW
00051 **       SQLITE_DROP_TRIGGER
00052 **       SQLITE_DROP_VIEW
00053 **       SQLITE_INSERT
00054 **       SQLITE_PRAGMA
00055 **       SQLITE_READ
00056 **       SQLITE_SELECT
00057 **       SQLITE_TRANSACTION
00058 **       SQLITE_UPDATE
00059 **
00060 ** The third and fourth arguments to the auth function are the name of
00061 ** the table and the column that are being accessed.  The auth function
00062 ** should return either SQLITE_OK, SQLITE_DENY, or SQLITE_IGNORE.  If
00063 ** SQLITE_OK is returned, it means that access is allowed.  SQLITE_DENY
00064 ** means that the SQL statement will never-run - the sqlite3_exec() call
00065 ** will return with an error.  SQLITE_IGNORE means that the SQL statement
00066 ** should run but attempts to read the specified column will return NULL
00067 ** and attempts to write the column will be ignored.
00068 **
00069 ** Setting the auth function to NULL disables this hook.  The default
00070 ** setting of the auth function is NULL.
00071 */
00072 int sqlite3_set_authorizer(
00073   sqlite3 *db,
00074   int (*xAuth)(void*,int,const char*,const char*,const char*,const char*),
00075   void *pArg
00076 ){
00077   sqlite3_mutex_enter(db->mutex);
00078   db->xAuth = xAuth;
00079   db->pAuthArg = pArg;
00080   sqlite3ExpirePreparedStatements(db);
00081   sqlite3_mutex_leave(db->mutex);
00082   return SQLITE_OK;
00083 }
00084 
00085 /*
00086 ** Write an error message into pParse->zErrMsg that explains that the
00087 ** user-supplied authorization function returned an illegal value.
00088 */
00089 static void sqliteAuthBadReturnCode(Parse *pParse, int rc){
00090   sqlite3ErrorMsg(pParse, "illegal return value (%d) from the "
00091     "authorization function - should be SQLITE_OK, SQLITE_IGNORE, "
00092     "or SQLITE_DENY", rc);
00093   pParse->rc = SQLITE_ERROR;
00094 }
00095 
00096 /*
00097 ** The pExpr should be a TK_COLUMN expression.  The table referred to
00098 ** is in pTabList or else it is the NEW or OLD table of a trigger.  
00099 ** Check to see if it is OK to read this particular column.
00100 **
00101 ** If the auth function returns SQLITE_IGNORE, change the TK_COLUMN 
00102 ** instruction into a TK_NULL.  If the auth function returns SQLITE_DENY,
00103 ** then generate an error.
00104 */
00105 void sqlite3AuthRead(
00106   Parse *pParse,        /* The parser context */
00107   Expr *pExpr,          /* The expression to check authorization on */
00108   Schema *pSchema,      /* The schema of the expression */
00109   SrcList *pTabList     /* All table that pExpr might refer to */
00110 ){
00111   sqlite3 *db = pParse->db;
00112   int rc;
00113   Table *pTab = 0;      /* The table being read */
00114   const char *zCol;     /* Name of the column of the table */
00115   int iSrc;             /* Index in pTabList->a[] of table being read */
00116   const char *zDBase;   /* Name of database being accessed */
00117   TriggerStack *pStack; /* The stack of current triggers */
00118   int iDb;              /* The index of the database the expression refers to */
00119 
00120   if( db->xAuth==0 ) return;
00121   if( pExpr->op!=TK_COLUMN ) return;
00122   iDb = sqlite3SchemaToIndex(pParse->db, pSchema);
00123   if( iDb<0 ){
00124     /* An attempt to read a column out of a subquery or other
00125     ** temporary table. */
00126     return;
00127   }
00128   for(iSrc=0; pTabList && iSrc<pTabList->nSrc; iSrc++){
00129     if( pExpr->iTable==pTabList->a[iSrc].iCursor ) break;
00130   }
00131   if( iSrc>=0 && pTabList && iSrc<pTabList->nSrc ){
00132     pTab = pTabList->a[iSrc].pTab;
00133   }else if( (pStack = pParse->trigStack)!=0 ){
00134     /* This must be an attempt to read the NEW or OLD pseudo-tables
00135     ** of a trigger.
00136     */
00137     assert( pExpr->iTable==pStack->newIdx || pExpr->iTable==pStack->oldIdx );
00138     pTab = pStack->pTab;
00139   }
00140   if( pTab==0 ) return;
00141   if( pExpr->iColumn>=0 ){
00142     assert( pExpr->iColumn<pTab->nCol );
00143     zCol = pTab->aCol[pExpr->iColumn].zName;
00144   }else if( pTab->iPKey>=0 ){
00145     assert( pTab->iPKey<pTab->nCol );
00146     zCol = pTab->aCol[pTab->iPKey].zName;
00147   }else{
00148     zCol = "ROWID";
00149   }
00150   assert( iDb>=0 && iDb<db->nDb );
00151   zDBase = db->aDb[iDb].zName;
00152   rc = db->xAuth(db->pAuthArg, SQLITE_READ, pTab->zName, zCol, zDBase, 
00153                  pParse->zAuthContext);
00154   if( rc==SQLITE_IGNORE ){
00155     pExpr->op = TK_NULL;
00156   }else if( rc==SQLITE_DENY ){
00157     if( db->nDb>2 || iDb!=0 ){
00158       sqlite3ErrorMsg(pParse, "access to %s.%s.%s is prohibited", 
00159          zDBase, pTab->zName, zCol);
00160     }else{
00161       sqlite3ErrorMsg(pParse, "access to %s.%s is prohibited",pTab->zName,zCol);
00162     }
00163     pParse->rc = SQLITE_AUTH;
00164   }else if( rc!=SQLITE_OK ){
00165     sqliteAuthBadReturnCode(pParse, rc);
00166   }
00167 }
00168 
00169 /*
00170 ** Do an authorization check using the code and arguments given.  Return
00171 ** either SQLITE_OK (zero) or SQLITE_IGNORE or SQLITE_DENY.  If SQLITE_DENY
00172 ** is returned, then the error count and error message in pParse are
00173 ** modified appropriately.
00174 */
00175 int sqlite3AuthCheck(
00176   Parse *pParse,
00177   int code,
00178   const char *zArg1,
00179   const char *zArg2,
00180   const char *zArg3
00181 ){
00182   sqlite3 *db = pParse->db;
00183   int rc;
00184 
00185   /* Don't do any authorization checks if the database is initialising
00186   ** or if the parser is being invoked from within sqlite3_declare_vtab.
00187   */
00188   if( db->init.busy || IN_DECLARE_VTAB ){
00189     return SQLITE_OK;
00190   }
00191 
00192   if( db->xAuth==0 ){
00193     return SQLITE_OK;
00194   }
00195   rc = db->xAuth(db->pAuthArg, code, zArg1, zArg2, zArg3, pParse->zAuthContext);
00196   if( rc==SQLITE_DENY ){
00197     sqlite3ErrorMsg(pParse, "not authorized");
00198     pParse->rc = SQLITE_AUTH;
00199   }else if( rc!=SQLITE_OK && rc!=SQLITE_IGNORE ){
00200     rc = SQLITE_DENY;
00201     sqliteAuthBadReturnCode(pParse, rc);
00202   }
00203   return rc;
00204 }
00205 
00206 /*
00207 ** Push an authorization context.  After this routine is called, the
00208 ** zArg3 argument to authorization callbacks will be zContext until
00209 ** popped.  Or if pParse==0, this routine is a no-op.
00210 */
00211 void sqlite3AuthContextPush(
00212   Parse *pParse,
00213   AuthContext *pContext, 
00214   const char *zContext
00215 ){
00216   pContext->pParse = pParse;
00217   if( pParse ){
00218     pContext->zAuthContext = pParse->zAuthContext;
00219     pParse->zAuthContext = zContext;
00220   }
00221 }
00222 
00223 /*
00224 ** Pop an authorization context that was previously pushed
00225 ** by sqlite3AuthContextPush
00226 */
00227 void sqlite3AuthContextPop(AuthContext *pContext){
00228   if( pContext->pParse ){
00229     pContext->pParse->zAuthContext = pContext->zAuthContext;
00230     pContext->pParse = 0;
00231   }
00232 }
00233 
00234 #endif /* SQLITE_OMIT_AUTHORIZATION */

ContextLogger2—ContextLogger2 Logger Daemon Internals—Generated on Mon May 2 13:49:51 2011 by Doxygen 1.6.1