drh | e6d01c3 | 2003-01-12 18:07:48 +0000 | [diff] [blame] | 1 | /* |
| 2 | ** 2003 January 11 |
| 3 | ** |
| 4 | ** The author disclaims copyright to this source code. In place of |
| 5 | ** a legal notice, here is a blessing: |
| 6 | ** |
| 7 | ** May you do good and not evil. |
| 8 | ** May you find forgiveness for yourself and forgive others. |
| 9 | ** May you share freely, never taking more than you give. |
| 10 | ** |
| 11 | ************************************************************************* |
| 12 | ** This file contains code used to implement the sqlite_set_authorizer() |
| 13 | ** API. This facility is an optional feature of the library. Embedded |
| 14 | ** systems that do not need this facility may omit it by recompiling |
| 15 | ** the library with -DSQLITE_OMIT_AUTHORIZATION=1 |
| 16 | ** |
drh | e22a334 | 2003-04-22 20:30:37 +0000 | [diff] [blame^] | 17 | ** $Id: auth.c,v 1.6 2003/04/22 20:30:38 drh Exp $ |
drh | e6d01c3 | 2003-01-12 18:07:48 +0000 | [diff] [blame] | 18 | */ |
| 19 | #include "sqliteInt.h" |
| 20 | |
| 21 | /* |
| 22 | ** All of the code in this file may be omitted by defining a single |
| 23 | ** macro. |
| 24 | */ |
| 25 | #ifndef SQLITE_OMIT_AUTHORIZATION |
| 26 | |
| 27 | /* |
| 28 | ** Set or clear the access authorization function. |
| 29 | ** |
| 30 | ** The access authorization function is be called during the compilation |
| 31 | ** phase to verify that the user has read and/or write access permission |
| 32 | ** various fields of the database. The first argument to the auth function |
| 33 | ** is a copy of the 3rd argument to this routine. The second argument |
| 34 | ** to the auth function is one of these constants: |
| 35 | ** |
| 36 | ** SQLITE_READ_COLUMN |
| 37 | ** SQLITE_WRITE_COLUMN |
| 38 | ** SQLITE_DELETE_ROW |
| 39 | ** SQLITE_INSERT_ROW |
| 40 | ** |
| 41 | ** The third and fourth arguments to the auth function are the name of |
| 42 | ** the table and the column that are being accessed. The auth function |
| 43 | ** should return either SQLITE_OK, SQLITE_DENY, or SQLITE_IGNORE. If |
| 44 | ** SQLITE_OK is returned, it means that access is allowed. SQLITE_DENY |
| 45 | ** means that the SQL statement will never-run - the sqlite_exec() call |
| 46 | ** will return with an error. SQLITE_IGNORE means that the SQL statement |
| 47 | ** should run but attempts to read the specified column will return NULL |
| 48 | ** and attempts to write the column will be ignored. |
| 49 | ** |
| 50 | ** Setting the auth function to NULL disables this hook. The default |
| 51 | ** setting of the auth function is NULL. |
| 52 | */ |
| 53 | int sqlite_set_authorizer( |
| 54 | sqlite *db, |
drh | e22a334 | 2003-04-22 20:30:37 +0000 | [diff] [blame^] | 55 | int (*xAuth)(void*,int,const char*,const char*,const char*,const char*), |
drh | e6d01c3 | 2003-01-12 18:07:48 +0000 | [diff] [blame] | 56 | void *pArg |
| 57 | ){ |
| 58 | db->xAuth = xAuth; |
| 59 | db->pAuthArg = pArg; |
| 60 | return SQLITE_OK; |
| 61 | } |
| 62 | |
| 63 | /* |
| 64 | ** Write an error message into pParse->zErrMsg that explains that the |
| 65 | ** user-supplied authorization function returned an illegal value. |
| 66 | */ |
| 67 | static void sqliteAuthBadReturnCode(Parse *pParse, int rc){ |
| 68 | char zBuf[20]; |
| 69 | sprintf(zBuf, "(%d)", rc); |
| 70 | sqliteSetString(&pParse->zErrMsg, "illegal return value ", zBuf, |
drh | 1962bda | 2003-01-12 19:33:52 +0000 | [diff] [blame] | 71 | " from the authorization function - should be SQLITE_OK, " |
drh | e6d01c3 | 2003-01-12 18:07:48 +0000 | [diff] [blame] | 72 | "SQLITE_IGNORE, or SQLITE_DENY", 0); |
| 73 | pParse->nErr++; |
drh | dcd997e | 2003-01-31 17:21:49 +0000 | [diff] [blame] | 74 | pParse->rc = SQLITE_MISUSE; |
drh | e6d01c3 | 2003-01-12 18:07:48 +0000 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | /* |
| 78 | ** The pExpr should be a TK_COLUMN expression. The table referred to |
| 79 | ** is in pTabList with an offset of base. Check to see if it is OK to read |
| 80 | ** this particular column. |
| 81 | ** |
| 82 | ** If the auth function returns SQLITE_IGNORE, change the TK_COLUMN |
| 83 | ** instruction into a TK_NULL. If the auth function returns SQLITE_DENY, |
| 84 | ** then generate an error. |
| 85 | */ |
| 86 | void sqliteAuthRead( |
| 87 | Parse *pParse, /* The parser context */ |
| 88 | Expr *pExpr, /* The expression to check authorization on */ |
| 89 | SrcList *pTabList, /* All table that pExpr might refer to */ |
| 90 | int base /* Offset of pTabList relative to pExpr */ |
| 91 | ){ |
| 92 | sqlite *db = pParse->db; |
| 93 | int rc; |
drh | 027850b | 2003-04-16 20:24:52 +0000 | [diff] [blame] | 94 | Table *pTab; /* The table being read */ |
| 95 | const char *zCol; /* Name of the column of the table */ |
| 96 | int iSrc; /* Index in pTabList->a[] of table being read */ |
drh | e22a334 | 2003-04-22 20:30:37 +0000 | [diff] [blame^] | 97 | const char *zDBase; /* Name of database being accessed */ |
| 98 | const char *zTrig; /* Name of the trigger doing the accessing */ |
| 99 | TriggerStack *pStack; /* The stack of current triggers */ |
drh | 027850b | 2003-04-16 20:24:52 +0000 | [diff] [blame] | 100 | |
drh | e22a334 | 2003-04-22 20:30:37 +0000 | [diff] [blame^] | 101 | pStack = pParse->trigStack; |
| 102 | zTrig = pStack ? pStack->pTrigger->name : 0; |
drh | e6d01c3 | 2003-01-12 18:07:48 +0000 | [diff] [blame] | 103 | if( db->xAuth==0 ) return; |
| 104 | assert( pExpr->op==TK_COLUMN ); |
drh | 027850b | 2003-04-16 20:24:52 +0000 | [diff] [blame] | 105 | iSrc = pExpr->iTable - base; |
| 106 | if( iSrc>=0 && iSrc<pTabList->nSrc ){ |
| 107 | pTab = pTabList->a[iSrc].pTab; |
| 108 | }else{ |
| 109 | /* This must be an attempt to read the NEW or OLD pseudo-tables |
| 110 | ** of a trigger. |
| 111 | */ |
drh | 027850b | 2003-04-16 20:24:52 +0000 | [diff] [blame] | 112 | assert( pStack!=0 ); |
| 113 | assert( pExpr->iTable==pStack->newIdx || pExpr->iTable==pStack->oldIdx ); |
| 114 | pTab = pStack->pTab; |
| 115 | } |
drh | e6d01c3 | 2003-01-12 18:07:48 +0000 | [diff] [blame] | 116 | if( pTab==0 ) return; |
| 117 | if( pExpr->iColumn>=0 ){ |
| 118 | assert( pExpr->iColumn<pTab->nCol ); |
| 119 | zCol = pTab->aCol[pExpr->iColumn].zName; |
| 120 | }else if( pTab->iPKey>=0 ){ |
| 121 | assert( pTab->iPKey<pTab->nCol ); |
| 122 | zCol = pTab->aCol[pTab->iPKey].zName; |
| 123 | }else{ |
| 124 | zCol = "ROWID"; |
| 125 | } |
drh | e22a334 | 2003-04-22 20:30:37 +0000 | [diff] [blame^] | 126 | assert( pExpr->iDb>=0 && pExpr->iDb<db->nDb ); |
| 127 | zDBase = db->aDb[pExpr->iDb].zName; |
| 128 | rc = db->xAuth(db->pAuthArg, SQLITE_READ, pTab->zName, zCol, zDBase, zTrig); |
drh | e6d01c3 | 2003-01-12 18:07:48 +0000 | [diff] [blame] | 129 | if( rc==SQLITE_IGNORE ){ |
| 130 | pExpr->op = TK_NULL; |
| 131 | }else if( rc==SQLITE_DENY ){ |
drh | e22a334 | 2003-04-22 20:30:37 +0000 | [diff] [blame^] | 132 | if( db->nDb>2 || pExpr->iDb!=0 ){ |
| 133 | sqliteSetString(&pParse->zErrMsg,"access to ", zDBase, ".", |
| 134 | pTab->zName, ".", zCol, " is prohibited", 0); |
| 135 | }else{ |
| 136 | sqliteSetString(&pParse->zErrMsg,"access to ", pTab->zName, ".", |
| 137 | zCol, " is prohibited", 0); |
| 138 | } |
drh | e6d01c3 | 2003-01-12 18:07:48 +0000 | [diff] [blame] | 139 | pParse->nErr++; |
drh | dcd997e | 2003-01-31 17:21:49 +0000 | [diff] [blame] | 140 | pParse->rc = SQLITE_AUTH; |
drh | e6d01c3 | 2003-01-12 18:07:48 +0000 | [diff] [blame] | 141 | }else if( rc!=SQLITE_OK ){ |
| 142 | sqliteAuthBadReturnCode(pParse, rc); |
| 143 | } |
| 144 | } |
| 145 | |
| 146 | /* |
drh | e5f9c64 | 2003-01-13 23:27:31 +0000 | [diff] [blame] | 147 | ** Do an authorization check using the code and arguments given. Return |
| 148 | ** either SQLITE_OK (zero) or SQLITE_IGNORE or SQLITE_DENY. If SQLITE_DENY |
| 149 | ** is returned, then the error count and error message in pParse are |
| 150 | ** modified appropriately. |
drh | e6d01c3 | 2003-01-12 18:07:48 +0000 | [diff] [blame] | 151 | */ |
drh | e5f9c64 | 2003-01-13 23:27:31 +0000 | [diff] [blame] | 152 | int sqliteAuthCheck( |
| 153 | Parse *pParse, |
| 154 | int code, |
| 155 | const char *zArg1, |
drh | e22a334 | 2003-04-22 20:30:37 +0000 | [diff] [blame^] | 156 | const char *zArg2, |
| 157 | const char *zArg3 |
drh | e5f9c64 | 2003-01-13 23:27:31 +0000 | [diff] [blame] | 158 | ){ |
drh | e6d01c3 | 2003-01-12 18:07:48 +0000 | [diff] [blame] | 159 | sqlite *db = pParse->db; |
| 160 | int rc; |
drh | e22a334 | 2003-04-22 20:30:37 +0000 | [diff] [blame^] | 161 | const char *zTrigName; |
| 162 | |
drh | e6d01c3 | 2003-01-12 18:07:48 +0000 | [diff] [blame] | 163 | if( db->xAuth==0 ){ |
| 164 | return SQLITE_OK; |
| 165 | } |
drh | e22a334 | 2003-04-22 20:30:37 +0000 | [diff] [blame^] | 166 | zTrigName = pParse->trigStack ? pParse->trigStack->pTrigger->name : 0; |
| 167 | rc = db->xAuth(db->pAuthArg, code, zArg1, zArg2, zArg3, zTrigName); |
drh | e5f9c64 | 2003-01-13 23:27:31 +0000 | [diff] [blame] | 168 | if( rc==SQLITE_DENY ){ |
| 169 | sqliteSetString(&pParse->zErrMsg, "not authorized", 0); |
drh | dcd997e | 2003-01-31 17:21:49 +0000 | [diff] [blame] | 170 | pParse->rc = SQLITE_AUTH; |
drh | e6d01c3 | 2003-01-12 18:07:48 +0000 | [diff] [blame] | 171 | pParse->nErr++; |
| 172 | }else if( rc!=SQLITE_OK && rc!=SQLITE_IGNORE ){ |
| 173 | rc = SQLITE_DENY; |
| 174 | sqliteAuthBadReturnCode(pParse, rc); |
| 175 | } |
| 176 | return rc; |
| 177 | } |
| 178 | |
drh | e6d01c3 | 2003-01-12 18:07:48 +0000 | [diff] [blame] | 179 | #endif /* SQLITE_OMIT_AUTHORIZATION */ |