Back to index

lightning-sunbird  0.9+nobinonly
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.24 2006/01/13 13:55:45 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   db->xAuth = xAuth;
00078   db->pAuthArg = pArg;
00079   sqlite3ExpirePreparedStatements(db);
00080   return SQLITE_OK;
00081 }
00082 
00083 /*
00084 ** Write an error message into pParse->zErrMsg that explains that the
00085 ** user-supplied authorization function returned an illegal value.
00086 */
00087 static void sqliteAuthBadReturnCode(Parse *pParse, int rc){
00088   sqlite3ErrorMsg(pParse, "illegal return value (%d) from the "
00089     "authorization function - should be SQLITE_OK, SQLITE_IGNORE, "
00090     "or SQLITE_DENY", rc);
00091   pParse->rc = SQLITE_ERROR;
00092 }
00093 
00094 /*
00095 ** The pExpr should be a TK_COLUMN expression.  The table referred to
00096 ** is in pTabList or else it is the NEW or OLD table of a trigger.  
00097 ** Check to see if it is OK to read this particular column.
00098 **
00099 ** If the auth function returns SQLITE_IGNORE, change the TK_COLUMN 
00100 ** instruction into a TK_NULL.  If the auth function returns SQLITE_DENY,
00101 ** then generate an error.
00102 */
00103 void sqlite3AuthRead(
00104   Parse *pParse,        /* The parser context */
00105   Expr *pExpr,          /* The expression to check authorization on */
00106   SrcList *pTabList     /* All table that pExpr might refer to */
00107 ){
00108   sqlite3 *db = pParse->db;
00109   int rc;
00110   Table *pTab;          /* The table being read */
00111   const char *zCol;     /* Name of the column of the table */
00112   int iSrc;             /* Index in pTabList->a[] of table being read */
00113   const char *zDBase;   /* Name of database being accessed */
00114   TriggerStack *pStack; /* The stack of current triggers */
00115   int iDb;              /* The index of the database the expression refers to */
00116 
00117   if( db->xAuth==0 ) return;
00118   if( pExpr->op==TK_AS ) return;
00119   assert( pExpr->op==TK_COLUMN );
00120   iDb = sqlite3SchemaToIndex(pParse->db, pExpr->pSchema);
00121   if( iDb<0 ){
00122     /* An attempt to read a column out of a subquery or other
00123     ** temporary table. */
00124     return;
00125   }
00126   for(iSrc=0; pTabList && iSrc<pTabList->nSrc; iSrc++){
00127     if( pExpr->iTable==pTabList->a[iSrc].iCursor ) break;
00128   }
00129   if( iSrc>=0 && pTabList && iSrc<pTabList->nSrc ){
00130     pTab = pTabList->a[iSrc].pTab;
00131   }else if( (pStack = pParse->trigStack)!=0 ){
00132     /* This must be an attempt to read the NEW or OLD pseudo-tables
00133     ** of a trigger.
00134     */
00135     assert( pExpr->iTable==pStack->newIdx || pExpr->iTable==pStack->oldIdx );
00136     pTab = pStack->pTab;
00137   }else{
00138     return;
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   if( db->init.busy ){
00187     return SQLITE_OK;
00188   }
00189 
00190   if( db->xAuth==0 ){
00191     return SQLITE_OK;
00192   }
00193   rc = db->xAuth(db->pAuthArg, code, zArg1, zArg2, zArg3, pParse->zAuthContext);
00194   if( rc==SQLITE_DENY ){
00195     sqlite3ErrorMsg(pParse, "not authorized");
00196     pParse->rc = SQLITE_AUTH;
00197   }else if( rc!=SQLITE_OK && rc!=SQLITE_IGNORE ){
00198     rc = SQLITE_DENY;
00199     sqliteAuthBadReturnCode(pParse, rc);
00200   }
00201   return rc;
00202 }
00203 
00204 /*
00205 ** Push an authorization context.  After this routine is called, the
00206 ** zArg3 argument to authorization callbacks will be zContext until
00207 ** popped.  Or if pParse==0, this routine is a no-op.
00208 */
00209 void sqlite3AuthContextPush(
00210   Parse *pParse,
00211   AuthContext *pContext, 
00212   const char *zContext
00213 ){
00214   pContext->pParse = pParse;
00215   if( pParse ){
00216     pContext->zAuthContext = pParse->zAuthContext;
00217     pParse->zAuthContext = zContext;
00218   }
00219 }
00220 
00221 /*
00222 ** Pop an authorization context that was previously pushed
00223 ** by sqlite3AuthContextPush
00224 */
00225 void sqlite3AuthContextPop(AuthContext *pContext){
00226   if( pContext->pParse ){
00227     pContext->pParse->zAuthContext = pContext->zAuthContext;
00228     pContext->pParse = 0;
00229   }
00230 }
00231 
00232 #endif /* SQLITE_OMIT_AUTHORIZATION */