You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

225 lines
7.0 KiB

  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. **
  17. ** $Id$
  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_COPY
  35. ** SQLITE_CREATE_INDEX
  36. ** SQLITE_CREATE_TABLE
  37. ** SQLITE_CREATE_TEMP_INDEX
  38. ** SQLITE_CREATE_TEMP_TABLE
  39. ** SQLITE_CREATE_TEMP_TRIGGER
  40. ** SQLITE_CREATE_TEMP_VIEW
  41. ** SQLITE_CREATE_TRIGGER
  42. ** SQLITE_CREATE_VIEW
  43. ** SQLITE_DELETE
  44. ** SQLITE_DROP_INDEX
  45. ** SQLITE_DROP_TABLE
  46. ** SQLITE_DROP_TEMP_INDEX
  47. ** SQLITE_DROP_TEMP_TABLE
  48. ** SQLITE_DROP_TEMP_TRIGGER
  49. ** SQLITE_DROP_TEMP_VIEW
  50. ** SQLITE_DROP_TRIGGER
  51. ** SQLITE_DROP_VIEW
  52. ** SQLITE_INSERT
  53. ** SQLITE_PRAGMA
  54. ** SQLITE_READ
  55. ** SQLITE_SELECT
  56. ** SQLITE_TRANSACTION
  57. ** SQLITE_UPDATE
  58. **
  59. ** The third and fourth arguments to the auth function are the name of
  60. ** the table and the column that are being accessed. The auth function
  61. ** should return either SQLITE_OK, SQLITE_DENY, or SQLITE_IGNORE. If
  62. ** SQLITE_OK is returned, it means that access is allowed. SQLITE_DENY
  63. ** means that the SQL statement will never-run - the sqlite_exec() call
  64. ** will return with an error. SQLITE_IGNORE means that the SQL statement
  65. ** should run but attempts to read the specified column will return NULL
  66. ** and attempts to write the column will be ignored.
  67. **
  68. ** Setting the auth function to NULL disables this hook. The default
  69. ** setting of the auth function is NULL.
  70. */
  71. int sqlite_set_authorizer(
  72. sqlite *db,
  73. int (*xAuth)(void*,int,const char*,const char*,const char*,const char*),
  74. void *pArg
  75. ){
  76. db->xAuth = xAuth;
  77. db->pAuthArg = pArg;
  78. return SQLITE_OK;
  79. }
  80. /*
  81. ** Write an error message into pParse->zErrMsg that explains that the
  82. ** user-supplied authorization function returned an illegal value.
  83. */
  84. static void sqliteAuthBadReturnCode(Parse *pParse, int rc){
  85. char zBuf[20];
  86. sprintf(zBuf, "(%d)", rc);
  87. sqliteSetString(&pParse->zErrMsg, "illegal return value ", zBuf,
  88. " from the authorization function - should be SQLITE_OK, "
  89. "SQLITE_IGNORE, or SQLITE_DENY", (char*)0);
  90. pParse->nErr++;
  91. pParse->rc = SQLITE_MISUSE;
  92. }
  93. /*
  94. ** The pExpr should be a TK_COLUMN expression. The table referred to
  95. ** is in pTabList or else it is the NEW or OLD table of a trigger.
  96. ** Check to see if it is OK to read this particular column.
  97. **
  98. ** If the auth function returns SQLITE_IGNORE, change the TK_COLUMN
  99. ** instruction into a TK_NULL. If the auth function returns SQLITE_DENY,
  100. ** then generate an error.
  101. */
  102. void sqliteAuthRead(
  103. Parse *pParse, /* The parser context */
  104. Expr *pExpr, /* The expression to check authorization on */
  105. SrcList *pTabList /* All table that pExpr might refer to */
  106. ){
  107. sqlite *db = pParse->db;
  108. int rc;
  109. Table *pTab; /* 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. if( db->xAuth==0 ) return;
  114. assert( pExpr->op==TK_COLUMN );
  115. for(iSrc=0; iSrc<pTabList->nSrc; iSrc++){
  116. if( pExpr->iTable==pTabList->a[iSrc].iCursor ) break;
  117. }
  118. if( iSrc>=0 && iSrc<pTabList->nSrc ){
  119. pTab = pTabList->a[iSrc].pTab;
  120. }else{
  121. /* This must be an attempt to read the NEW or OLD pseudo-tables
  122. ** of a trigger.
  123. */
  124. TriggerStack *pStack; /* The stack of current triggers */
  125. pStack = pParse->trigStack;
  126. assert( pStack!=0 );
  127. assert( pExpr->iTable==pStack->newIdx || pExpr->iTable==pStack->oldIdx );
  128. pTab = pStack->pTab;
  129. }
  130. if( pTab==0 ) return;
  131. if( pExpr->iColumn>=0 ){
  132. assert( pExpr->iColumn<pTab->nCol );
  133. zCol = pTab->aCol[pExpr->iColumn].zName;
  134. }else if( pTab->iPKey>=0 ){
  135. assert( pTab->iPKey<pTab->nCol );
  136. zCol = pTab->aCol[pTab->iPKey].zName;
  137. }else{
  138. zCol = "ROWID";
  139. }
  140. assert( pExpr->iDb<db->nDb );
  141. zDBase = db->aDb[pExpr->iDb].zName;
  142. rc = db->xAuth(db->pAuthArg, SQLITE_READ, pTab->zName, zCol, zDBase,
  143. pParse->zAuthContext);
  144. if( rc==SQLITE_IGNORE ){
  145. pExpr->op = TK_NULL;
  146. }else if( rc==SQLITE_DENY ){
  147. if( db->nDb>2 || pExpr->iDb!=0 ){
  148. sqliteSetString(&pParse->zErrMsg,"access to ", zDBase, ".",
  149. pTab->zName, ".", zCol, " is prohibited", (char*)0);
  150. }else{
  151. sqliteSetString(&pParse->zErrMsg,"access to ", pTab->zName, ".",
  152. zCol, " is prohibited", (char*)0);
  153. }
  154. pParse->nErr++;
  155. pParse->rc = SQLITE_AUTH;
  156. }else if( rc!=SQLITE_OK ){
  157. sqliteAuthBadReturnCode(pParse, rc);
  158. }
  159. }
  160. /*
  161. ** Do an authorization check using the code and arguments given. Return
  162. ** either SQLITE_OK (zero) or SQLITE_IGNORE or SQLITE_DENY. If SQLITE_DENY
  163. ** is returned, then the error count and error message in pParse are
  164. ** modified appropriately.
  165. */
  166. int sqliteAuthCheck(
  167. Parse *pParse,
  168. int code,
  169. const char *zArg1,
  170. const char *zArg2,
  171. const char *zArg3
  172. ){
  173. sqlite *db = pParse->db;
  174. int rc;
  175. if( db->xAuth==0 ){
  176. return SQLITE_OK;
  177. }
  178. rc = db->xAuth(db->pAuthArg, code, zArg1, zArg2, zArg3, pParse->zAuthContext);
  179. if( rc==SQLITE_DENY ){
  180. sqliteSetString(&pParse->zErrMsg, "not authorized", (char*)0);
  181. pParse->rc = SQLITE_AUTH;
  182. pParse->nErr++;
  183. }else if( rc!=SQLITE_OK && rc!=SQLITE_IGNORE ){
  184. rc = SQLITE_DENY;
  185. sqliteAuthBadReturnCode(pParse, rc);
  186. }
  187. return rc;
  188. }
  189. /*
  190. ** Push an authorization context. After this routine is called, the
  191. ** zArg3 argument to authorization callbacks will be zContext until
  192. ** popped. Or if pParse==0, this routine is a no-op.
  193. */
  194. void sqliteAuthContextPush(
  195. Parse *pParse,
  196. AuthContext *pContext,
  197. const char *zContext
  198. ){
  199. pContext->pParse = pParse;
  200. if( pParse ){
  201. pContext->zAuthContext = pParse->zAuthContext;
  202. pParse->zAuthContext = zContext;
  203. }
  204. }
  205. /*
  206. ** Pop an authorization context that was previously pushed
  207. ** by sqliteAuthContextPush
  208. */
  209. void sqliteAuthContextPop(AuthContext *pContext){
  210. if( pContext->pParse ){
  211. pContext->pParse->zAuthContext = pContext->zAuthContext;
  212. pContext->pParse = 0;
  213. }
  214. }
  215. #endif /* SQLITE_OMIT_AUTHORIZATION */