auth.c 7.5 KB

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