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