extensions/sqlite/sqlite-source/auth.c
author David Anderson <dvander@alliedmods.net>
Sun Aug 30 01:54:45 2009 -0700 (2009-08-30)
changeset 2790 6dd851ba15fd
parent 1952 b7aa2b626807
permissions -rw-r--r--
Fixed extension version reporting.
ds@1952
     1
/*
ds@1952
     2
** 2003 January 11
ds@1952
     3
**
ds@1952
     4
** The author disclaims copyright to this source code.  In place of
ds@1952
     5
** a legal notice, here is a blessing:
ds@1952
     6
**
ds@1952
     7
**    May you do good and not evil.
ds@1952
     8
**    May you find forgiveness for yourself and forgive others.
ds@1952
     9
**    May you share freely, never taking more than you give.
ds@1952
    10
**
ds@1952
    11
*************************************************************************
ds@1952
    12
** This file contains code used to implement the sqlite3_set_authorizer()
ds@1952
    13
** API.  This facility is an optional feature of the library.  Embedded
ds@1952
    14
** systems that do not need this facility may omit it by recompiling
ds@1952
    15
** the library with -DSQLITE_OMIT_AUTHORIZATION=1
ds@1952
    16
**
ds@1952
    17
** $Id$
ds@1952
    18
*/
ds@1952
    19
#include "sqliteInt.h"
ds@1952
    20
ds@1952
    21
/*
ds@1952
    22
** All of the code in this file may be omitted by defining a single
ds@1952
    23
** macro.
ds@1952
    24
*/
ds@1952
    25
#ifndef SQLITE_OMIT_AUTHORIZATION
ds@1952
    26
ds@1952
    27
/*
ds@1952
    28
** Set or clear the access authorization function.
ds@1952
    29
**
ds@1952
    30
** The access authorization function is be called during the compilation
ds@1952
    31
** phase to verify that the user has read and/or write access permission on
ds@1952
    32
** various fields of the database.  The first argument to the auth function
ds@1952
    33
** is a copy of the 3rd argument to this routine.  The second argument
ds@1952
    34
** to the auth function is one of these constants:
ds@1952
    35
**
ds@1952
    36
**       SQLITE_CREATE_INDEX
ds@1952
    37
**       SQLITE_CREATE_TABLE
ds@1952
    38
**       SQLITE_CREATE_TEMP_INDEX
ds@1952
    39
**       SQLITE_CREATE_TEMP_TABLE
ds@1952
    40
**       SQLITE_CREATE_TEMP_TRIGGER
ds@1952
    41
**       SQLITE_CREATE_TEMP_VIEW
ds@1952
    42
**       SQLITE_CREATE_TRIGGER
ds@1952
    43
**       SQLITE_CREATE_VIEW
ds@1952
    44
**       SQLITE_DELETE
ds@1952
    45
**       SQLITE_DROP_INDEX
ds@1952
    46
**       SQLITE_DROP_TABLE
ds@1952
    47
**       SQLITE_DROP_TEMP_INDEX
ds@1952
    48
**       SQLITE_DROP_TEMP_TABLE
ds@1952
    49
**       SQLITE_DROP_TEMP_TRIGGER
ds@1952
    50
**       SQLITE_DROP_TEMP_VIEW
ds@1952
    51
**       SQLITE_DROP_TRIGGER
ds@1952
    52
**       SQLITE_DROP_VIEW
ds@1952
    53
**       SQLITE_INSERT
ds@1952
    54
**       SQLITE_PRAGMA
ds@1952
    55
**       SQLITE_READ
ds@1952
    56
**       SQLITE_SELECT
ds@1952
    57
**       SQLITE_TRANSACTION
ds@1952
    58
**       SQLITE_UPDATE
ds@1952
    59
**
ds@1952
    60
** The third and fourth arguments to the auth function are the name of
ds@1952
    61
** the table and the column that are being accessed.  The auth function
ds@1952
    62
** should return either SQLITE_OK, SQLITE_DENY, or SQLITE_IGNORE.  If
ds@1952
    63
** SQLITE_OK is returned, it means that access is allowed.  SQLITE_DENY
ds@1952
    64
** means that the SQL statement will never-run - the sqlite3_exec() call
ds@1952
    65
** will return with an error.  SQLITE_IGNORE means that the SQL statement
ds@1952
    66
** should run but attempts to read the specified column will return NULL
ds@1952
    67
** and attempts to write the column will be ignored.
ds@1952
    68
**
ds@1952
    69
** Setting the auth function to NULL disables this hook.  The default
ds@1952
    70
** setting of the auth function is NULL.
ds@1952
    71
*/
ds@1952
    72
int sqlite3_set_authorizer(
ds@1952
    73
  sqlite3 *db,
ds@1952
    74
  int (*xAuth)(void*,int,const char*,const char*,const char*,const char*),
ds@1952
    75
  void *pArg
ds@1952
    76
){
ds@1952
    77
  sqlite3_mutex_enter(db->mutex);
ds@1952
    78
  db->xAuth = xAuth;
ds@1952
    79
  db->pAuthArg = pArg;
ds@1952
    80
  sqlite3ExpirePreparedStatements(db);
ds@1952
    81
  sqlite3_mutex_leave(db->mutex);
ds@1952
    82
  return SQLITE_OK;
ds@1952
    83
}
ds@1952
    84
ds@1952
    85
/*
ds@1952
    86
** Write an error message into pParse->zErrMsg that explains that the
ds@1952
    87
** user-supplied authorization function returned an illegal value.
ds@1952
    88
*/
ds@1952
    89
static void sqliteAuthBadReturnCode(Parse *pParse, int rc){
ds@1952
    90
  sqlite3ErrorMsg(pParse, "illegal return value (%d) from the "
ds@1952
    91
    "authorization function - should be SQLITE_OK, SQLITE_IGNORE, "
ds@1952
    92
    "or SQLITE_DENY", rc);
ds@1952
    93
  pParse->rc = SQLITE_ERROR;
ds@1952
    94
}
ds@1952
    95
ds@1952
    96
/*
ds@1952
    97
** The pExpr should be a TK_COLUMN expression.  The table referred to
ds@1952
    98
** is in pTabList or else it is the NEW or OLD table of a trigger.  
ds@1952
    99
** Check to see if it is OK to read this particular column.
ds@1952
   100
**
ds@1952
   101
** If the auth function returns SQLITE_IGNORE, change the TK_COLUMN 
ds@1952
   102
** instruction into a TK_NULL.  If the auth function returns SQLITE_DENY,
ds@1952
   103
** then generate an error.
ds@1952
   104
*/
ds@1952
   105
void sqlite3AuthRead(
ds@1952
   106
  Parse *pParse,        /* The parser context */
ds@1952
   107
  Expr *pExpr,          /* The expression to check authorization on */
ds@1952
   108
  Schema *pSchema,      /* The schema of the expression */
ds@1952
   109
  SrcList *pTabList     /* All table that pExpr might refer to */
ds@1952
   110
){
ds@1952
   111
  sqlite3 *db = pParse->db;
ds@1952
   112
  int rc;
ds@1952
   113
  Table *pTab = 0;      /* The table being read */
ds@1952
   114
  const char *zCol;     /* Name of the column of the table */
ds@1952
   115
  int iSrc;             /* Index in pTabList->a[] of table being read */
ds@1952
   116
  const char *zDBase;   /* Name of database being accessed */
ds@1952
   117
  TriggerStack *pStack; /* The stack of current triggers */
ds@1952
   118
  int iDb;              /* The index of the database the expression refers to */
ds@1952
   119
ds@1952
   120
  if( db->xAuth==0 ) return;
ds@1952
   121
  if( pExpr->op!=TK_COLUMN ) return;
ds@1952
   122
  iDb = sqlite3SchemaToIndex(pParse->db, pSchema);
ds@1952
   123
  if( iDb<0 ){
ds@1952
   124
    /* An attempt to read a column out of a subquery or other
ds@1952
   125
    ** temporary table. */
ds@1952
   126
    return;
ds@1952
   127
  }
ds@1952
   128
  for(iSrc=0; pTabList && iSrc<pTabList->nSrc; iSrc++){
ds@1952
   129
    if( pExpr->iTable==pTabList->a[iSrc].iCursor ) break;
ds@1952
   130
  }
ds@1952
   131
  if( iSrc>=0 && pTabList && iSrc<pTabList->nSrc ){
ds@1952
   132
    pTab = pTabList->a[iSrc].pTab;
ds@1952
   133
  }else if( (pStack = pParse->trigStack)!=0 ){
ds@1952
   134
    /* This must be an attempt to read the NEW or OLD pseudo-tables
ds@1952
   135
    ** of a trigger.
ds@1952
   136
    */
ds@1952
   137
    assert( pExpr->iTable==pStack->newIdx || pExpr->iTable==pStack->oldIdx );
ds@1952
   138
    pTab = pStack->pTab;
ds@1952
   139
  }
ds@1952
   140
  if( pTab==0 ) return;
ds@1952
   141
  if( pExpr->iColumn>=0 ){
ds@1952
   142
    assert( pExpr->iColumn<pTab->nCol );
ds@1952
   143
    zCol = pTab->aCol[pExpr->iColumn].zName;
ds@1952
   144
  }else if( pTab->iPKey>=0 ){
ds@1952
   145
    assert( pTab->iPKey<pTab->nCol );
ds@1952
   146
    zCol = pTab->aCol[pTab->iPKey].zName;
ds@1952
   147
  }else{
ds@1952
   148
    zCol = "ROWID";
ds@1952
   149
  }
ds@1952
   150
  assert( iDb>=0 && iDb<db->nDb );
ds@1952
   151
  zDBase = db->aDb[iDb].zName;
ds@1952
   152
  rc = db->xAuth(db->pAuthArg, SQLITE_READ, pTab->zName, zCol, zDBase, 
ds@1952
   153
                 pParse->zAuthContext);
ds@1952
   154
  if( rc==SQLITE_IGNORE ){
ds@1952
   155
    pExpr->op = TK_NULL;
ds@1952
   156
  }else if( rc==SQLITE_DENY ){
ds@1952
   157
    if( db->nDb>2 || iDb!=0 ){
ds@1952
   158
      sqlite3ErrorMsg(pParse, "access to %s.%s.%s is prohibited", 
ds@1952
   159
         zDBase, pTab->zName, zCol);
ds@1952
   160
    }else{
ds@1952
   161
      sqlite3ErrorMsg(pParse, "access to %s.%s is prohibited",pTab->zName,zCol);
ds@1952
   162
    }
ds@1952
   163
    pParse->rc = SQLITE_AUTH;
ds@1952
   164
  }else if( rc!=SQLITE_OK ){
ds@1952
   165
    sqliteAuthBadReturnCode(pParse, rc);
ds@1952
   166
  }
ds@1952
   167
}
ds@1952
   168
ds@1952
   169
/*
ds@1952
   170
** Do an authorization check using the code and arguments given.  Return
ds@1952
   171
** either SQLITE_OK (zero) or SQLITE_IGNORE or SQLITE_DENY.  If SQLITE_DENY
ds@1952
   172
** is returned, then the error count and error message in pParse are
ds@1952
   173
** modified appropriately.
ds@1952
   174
*/
ds@1952
   175
int sqlite3AuthCheck(
ds@1952
   176
  Parse *pParse,
ds@1952
   177
  int code,
ds@1952
   178
  const char *zArg1,
ds@1952
   179
  const char *zArg2,
ds@1952
   180
  const char *zArg3
ds@1952
   181
){
ds@1952
   182
  sqlite3 *db = pParse->db;
ds@1952
   183
  int rc;
ds@1952
   184
ds@1952
   185
  /* Don't do any authorization checks if the database is initialising
ds@1952
   186
  ** or if the parser is being invoked from within sqlite3_declare_vtab.
ds@1952
   187
  */
ds@1952
   188
  if( db->init.busy || IN_DECLARE_VTAB ){
ds@1952
   189
    return SQLITE_OK;
ds@1952
   190
  }
ds@1952
   191
ds@1952
   192
  if( db->xAuth==0 ){
ds@1952
   193
    return SQLITE_OK;
ds@1952
   194
  }
ds@1952
   195
  rc = db->xAuth(db->pAuthArg, code, zArg1, zArg2, zArg3, pParse->zAuthContext);
ds@1952
   196
  if( rc==SQLITE_DENY ){
ds@1952
   197
    sqlite3ErrorMsg(pParse, "not authorized");
ds@1952
   198
    pParse->rc = SQLITE_AUTH;
ds@1952
   199
  }else if( rc!=SQLITE_OK && rc!=SQLITE_IGNORE ){
ds@1952
   200
    rc = SQLITE_DENY;
ds@1952
   201
    sqliteAuthBadReturnCode(pParse, rc);
ds@1952
   202
  }
ds@1952
   203
  return rc;
ds@1952
   204
}
ds@1952
   205
ds@1952
   206
/*
ds@1952
   207
** Push an authorization context.  After this routine is called, the
ds@1952
   208
** zArg3 argument to authorization callbacks will be zContext until
ds@1952
   209
** popped.  Or if pParse==0, this routine is a no-op.
ds@1952
   210
*/
ds@1952
   211
void sqlite3AuthContextPush(
ds@1952
   212
  Parse *pParse,
ds@1952
   213
  AuthContext *pContext, 
ds@1952
   214
  const char *zContext
ds@1952
   215
){
ds@1952
   216
  pContext->pParse = pParse;
ds@1952
   217
  if( pParse ){
ds@1952
   218
    pContext->zAuthContext = pParse->zAuthContext;
ds@1952
   219
    pParse->zAuthContext = zContext;
ds@1952
   220
  }
ds@1952
   221
}
ds@1952
   222
ds@1952
   223
/*
ds@1952
   224
** Pop an authorization context that was previously pushed
ds@1952
   225
** by sqlite3AuthContextPush
ds@1952
   226
*/
ds@1952
   227
void sqlite3AuthContextPop(AuthContext *pContext){
ds@1952
   228
  if( pContext->pParse ){
ds@1952
   229
    pContext->pParse->zAuthContext = pContext->zAuthContext;
ds@1952
   230
    pContext->pParse = 0;
ds@1952
   231
  }
ds@1952
   232
}
ds@1952
   233
ds@1952
   234
#endif /* SQLITE_OMIT_AUTHORIZATION */