SQLite

Hex Artifact Content
Login

Artifact 32a5bbe3b755169ab6c66311c5225a3cd4f75a46c041f7fb117e0cbb68055114:


0000: 2f 2a 0a 2a 2a 20 32 30 30 33 20 4a 61 6e 75 61  /*.** 2003 Janua
0010: 72 79 20 31 31 0a 2a 2a 0a 2a 2a 20 54 68 65 20  ry 11.**.** The 
0020: 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73  author disclaims
0030: 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68   copyright to th
0040: 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20  is source code. 
0050: 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20   In place of.** 
0060: 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20  a legal notice, 
0070: 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 69  here is a blessi
0080: 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79  ng:.**.**    May
0090: 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64   you do good and
00a0: 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20   not evil..**   
00b0: 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f   May you find fo
00c0: 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f  rgiveness for yo
00d0: 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69  urself and forgi
00e0: 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20  ve others..**   
00f0: 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 66   May you share f
0100: 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b  reely, never tak
0110: 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f  ing more than yo
0120: 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a  u give..**.*****
0130: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
0140: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
0150: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
0160: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
0170: 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c  ****.** This fil
0180: 65 20 63 6f 6e 74 61 69 6e 73 20 63 6f 64 65 20  e contains code 
0190: 75 73 65 64 20 74 6f 20 69 6d 70 6c 65 6d 65 6e  used to implemen
01a0: 74 20 74 68 65 20 73 71 6c 69 74 65 33 5f 73 65  t the sqlite3_se
01b0: 74 5f 61 75 74 68 6f 72 69 7a 65 72 28 29 0a 2a  t_authorizer().*
01c0: 2a 20 41 50 49 2e 20 20 54 68 69 73 20 66 61 63  * API.  This fac
01d0: 69 6c 69 74 79 20 69 73 20 61 6e 20 6f 70 74 69  ility is an opti
01e0: 6f 6e 61 6c 20 66 65 61 74 75 72 65 20 6f 66 20  onal feature of 
01f0: 74 68 65 20 6c 69 62 72 61 72 79 2e 20 20 45 6d  the library.  Em
0200: 62 65 64 64 65 64 0a 2a 2a 20 73 79 73 74 65 6d  bedded.** system
0210: 73 20 74 68 61 74 20 64 6f 20 6e 6f 74 20 6e 65  s that do not ne
0220: 65 64 20 74 68 69 73 20 66 61 63 69 6c 69 74 79  ed this facility
0230: 20 6d 61 79 20 6f 6d 69 74 20 69 74 20 62 79 20   may omit it by 
0240: 72 65 63 6f 6d 70 69 6c 69 6e 67 0a 2a 2a 20 74  recompiling.** t
0250: 68 65 20 6c 69 62 72 61 72 79 20 77 69 74 68 20  he library with 
0260: 2d 44 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55  -DSQLITE_OMIT_AU
0270: 54 48 4f 52 49 5a 41 54 49 4f 4e 3d 31 0a 2a 2f  THORIZATION=1.*/
0280: 0a 23 69 6e 63 6c 75 64 65 20 22 73 71 6c 69 74  .#include "sqlit
0290: 65 49 6e 74 2e 68 22 0a 0a 2f 2a 0a 2a 2a 20 41  eInt.h"../*.** A
02a0: 6c 6c 20 6f 66 20 74 68 65 20 63 6f 64 65 20 69  ll of the code i
02b0: 6e 20 74 68 69 73 20 66 69 6c 65 20 6d 61 79 20  n this file may 
02c0: 62 65 20 6f 6d 69 74 74 65 64 20 62 79 20 64 65  be omitted by de
02d0: 66 69 6e 69 6e 67 20 61 20 73 69 6e 67 6c 65 0a  fining a single.
02e0: 2a 2a 20 6d 61 63 72 6f 2e 0a 2a 2f 0a 23 69 66  ** macro..*/.#if
02f0: 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
0300: 5f 41 55 54 48 4f 52 49 5a 41 54 49 4f 4e 0a 0a  _AUTHORIZATION..
0310: 2f 2a 0a 2a 2a 20 53 65 74 20 6f 72 20 63 6c 65  /*.** Set or cle
0320: 61 72 20 74 68 65 20 61 63 63 65 73 73 20 61 75  ar the access au
0330: 74 68 6f 72 69 7a 61 74 69 6f 6e 20 66 75 6e 63  thorization func
0340: 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  tion..**.** The 
0350: 61 63 63 65 73 73 20 61 75 74 68 6f 72 69 7a 61  access authoriza
0360: 74 69 6f 6e 20 66 75 6e 63 74 69 6f 6e 20 69 73  tion function is
0370: 20 62 65 20 63 61 6c 6c 65 64 20 64 75 72 69 6e   be called durin
0380: 67 20 74 68 65 20 63 6f 6d 70 69 6c 61 74 69 6f  g the compilatio
0390: 6e 0a 2a 2a 20 70 68 61 73 65 20 74 6f 20 76 65  n.** phase to ve
03a0: 72 69 66 79 20 74 68 61 74 20 74 68 65 20 75 73  rify that the us
03b0: 65 72 20 68 61 73 20 72 65 61 64 20 61 6e 64 2f  er has read and/
03c0: 6f 72 20 77 72 69 74 65 20 61 63 63 65 73 73 20  or write access 
03d0: 70 65 72 6d 69 73 73 69 6f 6e 20 6f 6e 0a 2a 2a  permission on.**
03e0: 20 76 61 72 69 6f 75 73 20 66 69 65 6c 64 73 20   various fields 
03f0: 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 65 2e  of the database.
0400: 20 20 54 68 65 20 66 69 72 73 74 20 61 72 67 75    The first argu
0410: 6d 65 6e 74 20 74 6f 20 74 68 65 20 61 75 74 68  ment to the auth
0420: 20 66 75 6e 63 74 69 6f 6e 0a 2a 2a 20 69 73 20   function.** is 
0430: 61 20 63 6f 70 79 20 6f 66 20 74 68 65 20 33 72  a copy of the 3r
0440: 64 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 74 68  d argument to th
0450: 69 73 20 72 6f 75 74 69 6e 65 2e 20 20 54 68 65  is routine.  The
0460: 20 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74   second argument
0470: 0a 2a 2a 20 74 6f 20 74 68 65 20 61 75 74 68 20  .** to the auth 
0480: 66 75 6e 63 74 69 6f 6e 20 69 73 20 6f 6e 65 20  function is one 
0490: 6f 66 20 74 68 65 73 65 20 63 6f 6e 73 74 61 6e  of these constan
04a0: 74 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 20  ts:.**.**       
04b0: 53 51 4c 49 54 45 5f 43 52 45 41 54 45 5f 49 4e  SQLITE_CREATE_IN
04c0: 44 45 58 0a 2a 2a 20 20 20 20 20 20 20 53 51 4c  DEX.**       SQL
04d0: 49 54 45 5f 43 52 45 41 54 45 5f 54 41 42 4c 45  ITE_CREATE_TABLE
04e0: 0a 2a 2a 20 20 20 20 20 20 20 53 51 4c 49 54 45  .**       SQLITE
04f0: 5f 43 52 45 41 54 45 5f 54 45 4d 50 5f 49 4e 44  _CREATE_TEMP_IND
0500: 45 58 0a 2a 2a 20 20 20 20 20 20 20 53 51 4c 49  EX.**       SQLI
0510: 54 45 5f 43 52 45 41 54 45 5f 54 45 4d 50 5f 54  TE_CREATE_TEMP_T
0520: 41 42 4c 45 0a 2a 2a 20 20 20 20 20 20 20 53 51  ABLE.**       SQ
0530: 4c 49 54 45 5f 43 52 45 41 54 45 5f 54 45 4d 50  LITE_CREATE_TEMP
0540: 5f 54 52 49 47 47 45 52 0a 2a 2a 20 20 20 20 20  _TRIGGER.**     
0550: 20 20 53 51 4c 49 54 45 5f 43 52 45 41 54 45 5f    SQLITE_CREATE_
0560: 54 45 4d 50 5f 56 49 45 57 0a 2a 2a 20 20 20 20  TEMP_VIEW.**    
0570: 20 20 20 53 51 4c 49 54 45 5f 43 52 45 41 54 45     SQLITE_CREATE
0580: 5f 54 52 49 47 47 45 52 0a 2a 2a 20 20 20 20 20  _TRIGGER.**     
0590: 20 20 53 51 4c 49 54 45 5f 43 52 45 41 54 45 5f    SQLITE_CREATE_
05a0: 56 49 45 57 0a 2a 2a 20 20 20 20 20 20 20 53 51  VIEW.**       SQ
05b0: 4c 49 54 45 5f 44 45 4c 45 54 45 0a 2a 2a 20 20  LITE_DELETE.**  
05c0: 20 20 20 20 20 53 51 4c 49 54 45 5f 44 52 4f 50       SQLITE_DROP
05d0: 5f 49 4e 44 45 58 0a 2a 2a 20 20 20 20 20 20 20  _INDEX.**       
05e0: 53 51 4c 49 54 45 5f 44 52 4f 50 5f 54 41 42 4c  SQLITE_DROP_TABL
05f0: 45 0a 2a 2a 20 20 20 20 20 20 20 53 51 4c 49 54  E.**       SQLIT
0600: 45 5f 44 52 4f 50 5f 54 45 4d 50 5f 49 4e 44 45  E_DROP_TEMP_INDE
0610: 58 0a 2a 2a 20 20 20 20 20 20 20 53 51 4c 49 54  X.**       SQLIT
0620: 45 5f 44 52 4f 50 5f 54 45 4d 50 5f 54 41 42 4c  E_DROP_TEMP_TABL
0630: 45 0a 2a 2a 20 20 20 20 20 20 20 53 51 4c 49 54  E.**       SQLIT
0640: 45 5f 44 52 4f 50 5f 54 45 4d 50 5f 54 52 49 47  E_DROP_TEMP_TRIG
0650: 47 45 52 0a 2a 2a 20 20 20 20 20 20 20 53 51 4c  GER.**       SQL
0660: 49 54 45 5f 44 52 4f 50 5f 54 45 4d 50 5f 56 49  ITE_DROP_TEMP_VI
0670: 45 57 0a 2a 2a 20 20 20 20 20 20 20 53 51 4c 49  EW.**       SQLI
0680: 54 45 5f 44 52 4f 50 5f 54 52 49 47 47 45 52 0a  TE_DROP_TRIGGER.
0690: 2a 2a 20 20 20 20 20 20 20 53 51 4c 49 54 45 5f  **       SQLITE_
06a0: 44 52 4f 50 5f 56 49 45 57 0a 2a 2a 20 20 20 20  DROP_VIEW.**    
06b0: 20 20 20 53 51 4c 49 54 45 5f 49 4e 53 45 52 54     SQLITE_INSERT
06c0: 0a 2a 2a 20 20 20 20 20 20 20 53 51 4c 49 54 45  .**       SQLITE
06d0: 5f 50 52 41 47 4d 41 0a 2a 2a 20 20 20 20 20 20  _PRAGMA.**      
06e0: 20 53 51 4c 49 54 45 5f 52 45 41 44 0a 2a 2a 20   SQLITE_READ.** 
06f0: 20 20 20 20 20 20 53 51 4c 49 54 45 5f 53 45 4c        SQLITE_SEL
0700: 45 43 54 0a 2a 2a 20 20 20 20 20 20 20 53 51 4c  ECT.**       SQL
0710: 49 54 45 5f 54 52 41 4e 53 41 43 54 49 4f 4e 0a  ITE_TRANSACTION.
0720: 2a 2a 20 20 20 20 20 20 20 53 51 4c 49 54 45 5f  **       SQLITE_
0730: 55 50 44 41 54 45 0a 2a 2a 0a 2a 2a 20 54 68 65  UPDATE.**.** The
0740: 20 74 68 69 72 64 20 61 6e 64 20 66 6f 75 72 74   third and fourt
0750: 68 20 61 72 67 75 6d 65 6e 74 73 20 74 6f 20 74  h arguments to t
0760: 68 65 20 61 75 74 68 20 66 75 6e 63 74 69 6f 6e  he auth function
0770: 20 61 72 65 20 74 68 65 20 6e 61 6d 65 20 6f 66   are the name of
0780: 0a 2a 2a 20 74 68 65 20 74 61 62 6c 65 20 61 6e  .** the table an
0790: 64 20 74 68 65 20 63 6f 6c 75 6d 6e 20 74 68 61  d the column tha
07a0: 74 20 61 72 65 20 62 65 69 6e 67 20 61 63 63 65  t are being acce
07b0: 73 73 65 64 2e 20 20 54 68 65 20 61 75 74 68 20  ssed.  The auth 
07c0: 66 75 6e 63 74 69 6f 6e 0a 2a 2a 20 73 68 6f 75  function.** shou
07d0: 6c 64 20 72 65 74 75 72 6e 20 65 69 74 68 65 72  ld return either
07e0: 20 53 51 4c 49 54 45 5f 4f 4b 2c 20 53 51 4c 49   SQLITE_OK, SQLI
07f0: 54 45 5f 44 45 4e 59 2c 20 6f 72 20 53 51 4c 49  TE_DENY, or SQLI
0800: 54 45 5f 49 47 4e 4f 52 45 2e 20 20 49 66 0a 2a  TE_IGNORE.  If.*
0810: 2a 20 53 51 4c 49 54 45 5f 4f 4b 20 69 73 20 72  * SQLITE_OK is r
0820: 65 74 75 72 6e 65 64 2c 20 69 74 20 6d 65 61 6e  eturned, it mean
0830: 73 20 74 68 61 74 20 61 63 63 65 73 73 20 69 73  s that access is
0840: 20 61 6c 6c 6f 77 65 64 2e 20 20 53 51 4c 49 54   allowed.  SQLIT
0850: 45 5f 44 45 4e 59 0a 2a 2a 20 6d 65 61 6e 73 20  E_DENY.** means 
0860: 74 68 61 74 20 74 68 65 20 53 51 4c 20 73 74 61  that the SQL sta
0870: 74 65 6d 65 6e 74 20 77 69 6c 6c 20 6e 65 76 65  tement will neve
0880: 72 2d 72 75 6e 20 2d 20 74 68 65 20 73 71 6c 69  r-run - the sqli
0890: 74 65 33 5f 65 78 65 63 28 29 20 63 61 6c 6c 0a  te3_exec() call.
08a0: 2a 2a 20 77 69 6c 6c 20 72 65 74 75 72 6e 20 77  ** will return w
08b0: 69 74 68 20 61 6e 20 65 72 72 6f 72 2e 20 20 53  ith an error.  S
08c0: 51 4c 49 54 45 5f 49 47 4e 4f 52 45 20 6d 65 61  QLITE_IGNORE mea
08d0: 6e 73 20 74 68 61 74 20 74 68 65 20 53 51 4c 20  ns that the SQL 
08e0: 73 74 61 74 65 6d 65 6e 74 0a 2a 2a 20 73 68 6f  statement.** sho
08f0: 75 6c 64 20 72 75 6e 20 62 75 74 20 61 74 74 65  uld run but atte
0900: 6d 70 74 73 20 74 6f 20 72 65 61 64 20 74 68 65  mpts to read the
0910: 20 73 70 65 63 69 66 69 65 64 20 63 6f 6c 75 6d   specified colum
0920: 6e 20 77 69 6c 6c 20 72 65 74 75 72 6e 20 4e 55  n will return NU
0930: 4c 4c 0a 2a 2a 20 61 6e 64 20 61 74 74 65 6d 70  LL.** and attemp
0940: 74 73 20 74 6f 20 77 72 69 74 65 20 74 68 65 20  ts to write the 
0950: 63 6f 6c 75 6d 6e 20 77 69 6c 6c 20 62 65 20 69  column will be i
0960: 67 6e 6f 72 65 64 2e 0a 2a 2a 0a 2a 2a 20 53 65  gnored..**.** Se
0970: 74 74 69 6e 67 20 74 68 65 20 61 75 74 68 20 66  tting the auth f
0980: 75 6e 63 74 69 6f 6e 20 74 6f 20 4e 55 4c 4c 20  unction to NULL 
0990: 64 69 73 61 62 6c 65 73 20 74 68 69 73 20 68 6f  disables this ho
09a0: 6f 6b 2e 20 20 54 68 65 20 64 65 66 61 75 6c 74  ok.  The default
09b0: 0a 2a 2a 20 73 65 74 74 69 6e 67 20 6f 66 20 74  .** setting of t
09c0: 68 65 20 61 75 74 68 20 66 75 6e 63 74 69 6f 6e  he auth function
09d0: 20 69 73 20 4e 55 4c 4c 2e 0a 2a 2f 0a 69 6e 74   is NULL..*/.int
09e0: 20 73 71 6c 69 74 65 33 5f 73 65 74 5f 61 75 74   sqlite3_set_aut
09f0: 68 6f 72 69 7a 65 72 28 0a 20 20 73 71 6c 69 74  horizer(.  sqlit
0a00: 65 33 20 2a 64 62 2c 0a 20 20 69 6e 74 20 28 2a  e3 *db,.  int (*
0a10: 78 41 75 74 68 29 28 76 6f 69 64 2a 2c 69 6e 74  xAuth)(void*,int
0a20: 2c 63 6f 6e 73 74 20 63 68 61 72 2a 2c 63 6f 6e  ,const char*,con
0a30: 73 74 20 63 68 61 72 2a 2c 63 6f 6e 73 74 20 63  st char*,const c
0a40: 68 61 72 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a  har*,const char*
0a50: 29 2c 0a 20 20 76 6f 69 64 20 2a 70 41 72 67 0a  ),.  void *pArg.
0a60: 29 7b 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45  ){.#ifdef SQLITE
0a70: 5f 45 4e 41 42 4c 45 5f 41 50 49 5f 41 52 4d 4f  _ENABLE_API_ARMO
0a80: 52 0a 20 20 69 66 28 20 21 73 71 6c 69 74 65 33  R.  if( !sqlite3
0a90: 53 61 66 65 74 79 43 68 65 63 6b 4f 6b 28 64 62  SafetyCheckOk(db
0aa0: 29 20 29 20 72 65 74 75 72 6e 20 53 51 4c 49 54  ) ) return SQLIT
0ab0: 45 5f 4d 49 53 55 53 45 5f 42 4b 50 54 3b 0a 23  E_MISUSE_BKPT;.#
0ac0: 65 6e 64 69 66 0a 20 20 73 71 6c 69 74 65 33 5f  endif.  sqlite3_
0ad0: 6d 75 74 65 78 5f 65 6e 74 65 72 28 64 62 2d 3e  mutex_enter(db->
0ae0: 6d 75 74 65 78 29 3b 0a 20 20 64 62 2d 3e 78 41  mutex);.  db->xA
0af0: 75 74 68 20 3d 20 28 73 71 6c 69 74 65 33 5f 78  uth = (sqlite3_x
0b00: 61 75 74 68 29 78 41 75 74 68 3b 0a 20 20 64 62  auth)xAuth;.  db
0b10: 2d 3e 70 41 75 74 68 41 72 67 20 3d 20 70 41 72  ->pAuthArg = pAr
0b20: 67 3b 0a 20 20 73 71 6c 69 74 65 33 45 78 70 69  g;.  sqlite3Expi
0b30: 72 65 50 72 65 70 61 72 65 64 53 74 61 74 65 6d  rePreparedStatem
0b40: 65 6e 74 73 28 64 62 2c 20 30 29 3b 0a 20 20 73  ents(db, 0);.  s
0b50: 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61  qlite3_mutex_lea
0b60: 76 65 28 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20  ve(db->mutex);. 
0b70: 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   return SQLITE_O
0b80: 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 57 72 69 74  K;.}../*.** Writ
0b90: 65 20 61 6e 20 65 72 72 6f 72 20 6d 65 73 73 61  e an error messa
0ba0: 67 65 20 69 6e 74 6f 20 70 50 61 72 73 65 2d 3e  ge into pParse->
0bb0: 7a 45 72 72 4d 73 67 20 74 68 61 74 20 65 78 70  zErrMsg that exp
0bc0: 6c 61 69 6e 73 20 74 68 61 74 20 74 68 65 0a 2a  lains that the.*
0bd0: 2a 20 75 73 65 72 2d 73 75 70 70 6c 69 65 64 20  * user-supplied 
0be0: 61 75 74 68 6f 72 69 7a 61 74 69 6f 6e 20 66 75  authorization fu
0bf0: 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e 65 64 20  nction returned 
0c00: 61 6e 20 69 6c 6c 65 67 61 6c 20 76 61 6c 75 65  an illegal value
0c10: 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64  ..*/.static void
0c20: 20 73 71 6c 69 74 65 41 75 74 68 42 61 64 52 65   sqliteAuthBadRe
0c30: 74 75 72 6e 43 6f 64 65 28 50 61 72 73 65 20 2a  turnCode(Parse *
0c40: 70 50 61 72 73 65 29 7b 0a 20 20 73 71 6c 69 74  pParse){.  sqlit
0c50: 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73  e3ErrorMsg(pPars
0c60: 65 2c 20 22 61 75 74 68 6f 72 69 7a 65 72 20 6d  e, "authorizer m
0c70: 61 6c 66 75 6e 63 74 69 6f 6e 22 29 3b 0a 20 20  alfunction");.  
0c80: 70 50 61 72 73 65 2d 3e 72 63 20 3d 20 53 51 4c  pParse->rc = SQL
0c90: 49 54 45 5f 45 52 52 4f 52 3b 0a 7d 0a 0a 2f 2a  ITE_ERROR;.}../*
0ca0: 0a 2a 2a 20 49 6e 76 6f 6b 65 20 74 68 65 20 61  .** Invoke the a
0cb0: 75 74 68 6f 72 69 7a 61 74 69 6f 6e 20 63 61 6c  uthorization cal
0cc0: 6c 62 61 63 6b 20 66 6f 72 20 70 65 72 6d 69 73  lback for permis
0cd0: 73 69 6f 6e 20 74 6f 20 72 65 61 64 20 63 6f 6c  sion to read col
0ce0: 75 6d 6e 20 7a 43 6f 6c 20 66 72 6f 6d 0a 2a 2a  umn zCol from.**
0cf0: 20 74 61 62 6c 65 20 7a 54 61 62 20 69 6e 20 64   table zTab in d
0d00: 61 74 61 62 61 73 65 20 7a 44 62 2e 20 54 68 69  atabase zDb. Thi
0d10: 73 20 66 75 6e 63 74 69 6f 6e 20 61 73 73 75 6d  s function assum
0d20: 65 73 20 74 68 61 74 20 61 6e 20 61 75 74 68 6f  es that an autho
0d30: 72 69 7a 61 74 69 6f 6e 0a 2a 2a 20 63 61 6c 6c  rization.** call
0d40: 62 61 63 6b 20 68 61 73 20 62 65 65 6e 20 72 65  back has been re
0d50: 67 69 73 74 65 72 65 64 20 28 69 2e 65 2e 20 74  gistered (i.e. t
0d60: 68 61 74 20 73 71 6c 69 74 65 33 2e 78 41 75 74  hat sqlite3.xAut
0d70: 68 20 69 73 20 6e 6f 74 20 4e 55 4c 4c 29 2e 0a  h is not NULL)..
0d80: 2a 2a 0a 2a 2a 20 49 66 20 53 51 4c 49 54 45 5f  **.** If SQLITE_
0d90: 49 47 4e 4f 52 45 20 69 73 20 72 65 74 75 72 6e  IGNORE is return
0da0: 65 64 20 61 6e 64 20 70 45 78 70 72 20 69 73 20  ed and pExpr is 
0db0: 6e 6f 74 20 4e 55 4c 4c 2c 20 74 68 65 6e 20 70  not NULL, then p
0dc0: 45 78 70 72 20 69 73 20 63 68 61 6e 67 65 64 0a  Expr is changed.
0dd0: 2a 2a 20 74 6f 20 61 6e 20 53 51 4c 20 4e 55 4c  ** to an SQL NUL
0de0: 4c 20 65 78 70 72 65 73 73 69 6f 6e 2e 20 4f 74  L expression. Ot
0df0: 68 65 72 77 69 73 65 2c 20 69 66 20 70 45 78 70  herwise, if pExp
0e00: 72 20 69 73 20 4e 55 4c 4c 2c 20 74 68 65 6e 20  r is NULL, then 
0e10: 53 51 4c 49 54 45 5f 49 47 4e 4f 52 45 0a 2a 2a  SQLITE_IGNORE.**
0e20: 20 69 73 20 74 72 65 61 74 65 64 20 61 73 20 53   is treated as S
0e30: 51 4c 49 54 45 5f 44 45 4e 59 2e 20 49 6e 20 74  QLITE_DENY. In t
0e40: 68 69 73 20 63 61 73 65 20 61 6e 20 65 72 72 6f  his case an erro
0e50: 72 20 69 73 20 6c 65 66 74 20 69 6e 20 70 50 61  r is left in pPa
0e60: 72 73 65 2e 0a 2a 2f 0a 69 6e 74 20 73 71 6c 69  rse..*/.int sqli
0e70: 74 65 33 41 75 74 68 52 65 61 64 43 6f 6c 28 0a  te3AuthReadCol(.
0e80: 20 20 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c    Parse *pParse,
0e90: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
0ea0: 20 20 2f 2a 20 54 68 65 20 70 61 72 73 65 72 20    /* The parser 
0eb0: 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 63 6f 6e  context */.  con
0ec0: 73 74 20 63 68 61 72 20 2a 7a 54 61 62 2c 20 20  st char *zTab,  
0ed0: 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
0ee0: 54 61 62 6c 65 20 6e 61 6d 65 20 2a 2f 0a 20 20  Table name */.  
0ef0: 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 43 6f 6c  const char *zCol
0f00: 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
0f10: 2f 2a 20 43 6f 6c 75 6d 6e 20 6e 61 6d 65 20 2a  /* Column name *
0f20: 2f 0a 20 20 69 6e 74 20 69 44 62 20 20 20 20 20  /.  int iDb     
0f30: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
0f40: 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 6f 66 20      /* Index of 
0f50: 63 6f 6e 74 61 69 6e 69 6e 67 20 64 61 74 61 62  containing datab
0f60: 61 73 65 2e 20 2a 2f 0a 29 7b 0a 20 20 73 71 6c  ase. */.){.  sql
0f70: 69 74 65 33 20 2a 64 62 20 3d 20 70 50 61 72 73  ite3 *db = pPars
0f80: 65 2d 3e 64 62 3b 20 20 20 20 20 20 20 20 20 20  e->db;          
0f90: 2f 2a 20 44 61 74 61 62 61 73 65 20 68 61 6e 64  /* Database hand
0fa0: 6c 65 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 44  le */.  char *zD
0fb0: 62 20 3d 20 64 62 2d 3e 61 44 62 5b 69 44 62 5d  b = db->aDb[iDb]
0fc0: 2e 7a 44 62 53 4e 61 6d 65 3b 20 2f 2a 20 53 63  .zDbSName; /* Sc
0fd0: 68 65 6d 61 20 6e 61 6d 65 20 6f 66 20 61 74 74  hema name of att
0fe0: 61 63 68 65 64 20 64 61 74 61 62 61 73 65 20 2a  ached database *
0ff0: 2f 0a 20 20 69 6e 74 20 72 63 3b 20 20 20 20 20  /.  int rc;     
1000: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1010: 20 20 20 20 20 20 20 2f 2a 20 41 75 74 68 20 63         /* Auth c
1020: 61 6c 6c 62 61 63 6b 20 72 65 74 75 72 6e 20 63  allback return c
1030: 6f 64 65 20 2a 2f 0a 0a 20 20 69 66 28 20 64 62  ode */..  if( db
1040: 2d 3e 69 6e 69 74 2e 62 75 73 79 20 29 20 72 65  ->init.busy ) re
1050: 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  turn SQLITE_OK;.
1060: 20 20 72 63 20 3d 20 64 62 2d 3e 78 41 75 74 68    rc = db->xAuth
1070: 28 64 62 2d 3e 70 41 75 74 68 41 72 67 2c 20 53  (db->pAuthArg, S
1080: 51 4c 49 54 45 5f 52 45 41 44 2c 20 7a 54 61 62  QLITE_READ, zTab
1090: 2c 7a 43 6f 6c 2c 7a 44 62 2c 70 50 61 72 73 65  ,zCol,zDb,pParse
10a0: 2d 3e 7a 41 75 74 68 43 6f 6e 74 65 78 74 0a 23  ->zAuthContext.#
10b0: 69 66 64 65 66 20 53 51 4c 49 54 45 5f 55 53 45  ifdef SQLITE_USE
10c0: 52 5f 41 55 54 48 45 4e 54 49 43 41 54 49 4f 4e  R_AUTHENTICATION
10d0: 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
10e0: 20 20 2c 64 62 2d 3e 61 75 74 68 2e 7a 41 75 74    ,db->auth.zAut
10f0: 68 55 73 65 72 0a 23 65 6e 64 69 66 0a 20 20 20  hUser.#endif.   
1100: 20 20 20 20 20 20 20 20 20 20 20 20 20 29 3b 0a               );.
1110: 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45    if( rc==SQLITE
1120: 5f 44 45 4e 59 20 29 7b 0a 20 20 20 20 63 68 61  _DENY ){.    cha
1130: 72 20 2a 7a 20 3d 20 73 71 6c 69 74 65 33 5f 6d  r *z = sqlite3_m
1140: 70 72 69 6e 74 66 28 22 25 73 2e 25 73 22 2c 20  printf("%s.%s", 
1150: 7a 54 61 62 2c 20 7a 43 6f 6c 29 3b 0a 20 20 20  zTab, zCol);.   
1160: 20 69 66 28 20 64 62 2d 3e 6e 44 62 3e 32 20 7c   if( db->nDb>2 |
1170: 7c 20 69 44 62 21 3d 30 20 29 20 7a 20 3d 20 73  | iDb!=0 ) z = s
1180: 71 6c 69 74 65 33 5f 6d 70 72 69 6e 74 66 28 22  qlite3_mprintf("
1190: 25 73 2e 25 7a 22 2c 20 7a 44 62 2c 20 7a 29 3b  %s.%z", zDb, z);
11a0: 0a 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f  .    sqlite3Erro
11b0: 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22 61 63  rMsg(pParse, "ac
11c0: 63 65 73 73 20 74 6f 20 25 7a 20 69 73 20 70 72  cess to %z is pr
11d0: 6f 68 69 62 69 74 65 64 22 2c 20 7a 29 3b 0a 20  ohibited", z);. 
11e0: 20 20 20 70 50 61 72 73 65 2d 3e 72 63 20 3d 20     pParse->rc = 
11f0: 53 51 4c 49 54 45 5f 41 55 54 48 3b 0a 20 20 7d  SQLITE_AUTH;.  }
1200: 65 6c 73 65 20 69 66 28 20 72 63 21 3d 53 51 4c  else if( rc!=SQL
1210: 49 54 45 5f 49 47 4e 4f 52 45 20 26 26 20 72 63  ITE_IGNORE && rc
1220: 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20  !=SQLITE_OK ){. 
1230: 20 20 20 73 71 6c 69 74 65 41 75 74 68 42 61 64     sqliteAuthBad
1240: 52 65 74 75 72 6e 43 6f 64 65 28 70 50 61 72 73  ReturnCode(pPars
1250: 65 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e  e);.  }.  return
1260: 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68   rc;.}../*.** Th
1270: 65 20 70 45 78 70 72 20 73 68 6f 75 6c 64 20 62  e pExpr should b
1280: 65 20 61 20 54 4b 5f 43 4f 4c 55 4d 4e 20 65 78  e a TK_COLUMN ex
1290: 70 72 65 73 73 69 6f 6e 2e 20 20 54 68 65 20 74  pression.  The t
12a0: 61 62 6c 65 20 72 65 66 65 72 72 65 64 20 74 6f  able referred to
12b0: 0a 2a 2a 20 69 73 20 69 6e 20 70 54 61 62 4c 69  .** is in pTabLi
12c0: 73 74 20 6f 72 20 65 6c 73 65 20 69 74 20 69 73  st or else it is
12d0: 20 74 68 65 20 4e 45 57 20 6f 72 20 4f 4c 44 20   the NEW or OLD 
12e0: 74 61 62 6c 65 20 6f 66 20 61 20 74 72 69 67 67  table of a trigg
12f0: 65 72 2e 20 20 0a 2a 2a 20 43 68 65 63 6b 20 74  er.  .** Check t
1300: 6f 20 73 65 65 20 69 66 20 69 74 20 69 73 20 4f  o see if it is O
1310: 4b 20 74 6f 20 72 65 61 64 20 74 68 69 73 20 70  K to read this p
1320: 61 72 74 69 63 75 6c 61 72 20 63 6f 6c 75 6d 6e  articular column
1330: 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 61  ..**.** If the a
1340: 75 74 68 20 66 75 6e 63 74 69 6f 6e 20 72 65 74  uth function ret
1350: 75 72 6e 73 20 53 51 4c 49 54 45 5f 49 47 4e 4f  urns SQLITE_IGNO
1360: 52 45 2c 20 63 68 61 6e 67 65 20 74 68 65 20 54  RE, change the T
1370: 4b 5f 43 4f 4c 55 4d 4e 20 0a 2a 2a 20 69 6e 73  K_COLUMN .** ins
1380: 74 72 75 63 74 69 6f 6e 20 69 6e 74 6f 20 61 20  truction into a 
1390: 54 4b 5f 4e 55 4c 4c 2e 20 20 49 66 20 74 68 65  TK_NULL.  If the
13a0: 20 61 75 74 68 20 66 75 6e 63 74 69 6f 6e 20 72   auth function r
13b0: 65 74 75 72 6e 73 20 53 51 4c 49 54 45 5f 44 45  eturns SQLITE_DE
13c0: 4e 59 2c 0a 2a 2a 20 74 68 65 6e 20 67 65 6e 65  NY,.** then gene
13d0: 72 61 74 65 20 61 6e 20 65 72 72 6f 72 2e 0a 2a  rate an error..*
13e0: 2f 0a 76 6f 69 64 20 73 71 6c 69 74 65 33 41 75  /.void sqlite3Au
13f0: 74 68 52 65 61 64 28 0a 20 20 50 61 72 73 65 20  thRead(.  Parse 
1400: 2a 70 50 61 72 73 65 2c 20 20 20 20 20 20 20 20  *pParse,        
1410: 2f 2a 20 54 68 65 20 70 61 72 73 65 72 20 63 6f  /* The parser co
1420: 6e 74 65 78 74 20 2a 2f 0a 20 20 45 78 70 72 20  ntext */.  Expr 
1430: 2a 70 45 78 70 72 2c 20 20 20 20 20 20 20 20 20  *pExpr,         
1440: 20 2f 2a 20 54 68 65 20 65 78 70 72 65 73 73 69   /* The expressi
1450: 6f 6e 20 74 6f 20 63 68 65 63 6b 20 61 75 74 68  on to check auth
1460: 6f 72 69 7a 61 74 69 6f 6e 20 6f 6e 20 2a 2f 0a  orization on */.
1470: 20 20 53 63 68 65 6d 61 20 2a 70 53 63 68 65 6d    Schema *pSchem
1480: 61 2c 20 20 20 20 20 20 2f 2a 20 54 68 65 20 73  a,      /* The s
1490: 63 68 65 6d 61 20 6f 66 20 74 68 65 20 65 78 70  chema of the exp
14a0: 72 65 73 73 69 6f 6e 20 2a 2f 0a 20 20 53 72 63  ression */.  Src
14b0: 4c 69 73 74 20 2a 70 54 61 62 4c 69 73 74 20 20  List *pTabList  
14c0: 20 20 20 2f 2a 20 41 6c 6c 20 74 61 62 6c 65 20     /* All table 
14d0: 74 68 61 74 20 70 45 78 70 72 20 6d 69 67 68 74  that pExpr might
14e0: 20 72 65 66 65 72 20 74 6f 20 2a 2f 0a 29 7b 0a   refer to */.){.
14f0: 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20    sqlite3 *db = 
1500: 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20 54 61  pParse->db;.  Ta
1510: 62 6c 65 20 2a 70 54 61 62 20 3d 20 30 3b 20 20  ble *pTab = 0;  
1520: 20 20 20 20 2f 2a 20 54 68 65 20 74 61 62 6c 65      /* The table
1530: 20 62 65 69 6e 67 20 72 65 61 64 20 2a 2f 0a 20   being read */. 
1540: 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 43 6f   const char *zCo
1550: 6c 3b 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f  l;     /* Name o
1560: 66 20 74 68 65 20 63 6f 6c 75 6d 6e 20 6f 66 20  f the column of 
1570: 74 68 65 20 74 61 62 6c 65 20 2a 2f 0a 20 20 69  the table */.  i
1580: 6e 74 20 69 53 72 63 3b 20 20 20 20 20 20 20 20  nt iSrc;        
1590: 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 69 6e       /* Index in
15a0: 20 70 54 61 62 4c 69 73 74 2d 3e 61 5b 5d 20 6f   pTabList->a[] o
15b0: 66 20 74 61 62 6c 65 20 62 65 69 6e 67 20 72 65  f table being re
15c0: 61 64 20 2a 2f 0a 20 20 69 6e 74 20 69 44 62 3b  ad */.  int iDb;
15d0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
15e0: 20 54 68 65 20 69 6e 64 65 78 20 6f 66 20 74 68   The index of th
15f0: 65 20 64 61 74 61 62 61 73 65 20 74 68 65 20 65  e database the e
1600: 78 70 72 65 73 73 69 6f 6e 20 72 65 66 65 72 73  xpression refers
1610: 20 74 6f 20 2a 2f 0a 20 20 69 6e 74 20 69 43 6f   to */.  int iCo
1620: 6c 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  l;             /
1630: 2a 20 49 6e 64 65 78 20 6f 66 20 63 6f 6c 75 6d  * Index of colum
1640: 6e 20 69 6e 20 74 61 62 6c 65 20 2a 2f 0a 0a 20  n in table */.. 
1650: 20 61 73 73 65 72 74 28 20 70 45 78 70 72 2d 3e   assert( pExpr->
1660: 6f 70 3d 3d 54 4b 5f 43 4f 4c 55 4d 4e 20 7c 7c  op==TK_COLUMN ||
1670: 20 70 45 78 70 72 2d 3e 6f 70 3d 3d 54 4b 5f 54   pExpr->op==TK_T
1680: 52 49 47 47 45 52 20 29 3b 0a 20 20 69 66 28 20  RIGGER );.  if( 
1690: 64 62 2d 3e 78 41 75 74 68 3d 3d 30 20 29 20 72  db->xAuth==0 ) r
16a0: 65 74 75 72 6e 3b 0a 20 20 69 44 62 20 3d 20 73  eturn;.  iDb = s
16b0: 71 6c 69 74 65 33 53 63 68 65 6d 61 54 6f 49 6e  qlite3SchemaToIn
16c0: 64 65 78 28 70 50 61 72 73 65 2d 3e 64 62 2c 20  dex(pParse->db, 
16d0: 70 53 63 68 65 6d 61 29 3b 0a 20 20 69 66 28 20  pSchema);.  if( 
16e0: 69 44 62 3c 30 20 29 7b 0a 20 20 20 20 2f 2a 20  iDb<0 ){.    /* 
16f0: 41 6e 20 61 74 74 65 6d 70 74 20 74 6f 20 72 65  An attempt to re
1700: 61 64 20 61 20 63 6f 6c 75 6d 6e 20 6f 75 74 20  ad a column out 
1710: 6f 66 20 61 20 73 75 62 71 75 65 72 79 20 6f 72  of a subquery or
1720: 20 6f 74 68 65 72 0a 20 20 20 20 2a 2a 20 74 65   other.    ** te
1730: 6d 70 6f 72 61 72 79 20 74 61 62 6c 65 2e 20 2a  mporary table. *
1740: 2f 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20  /.    return;.  
1750: 7d 0a 0a 20 20 69 66 28 20 70 45 78 70 72 2d 3e  }..  if( pExpr->
1760: 6f 70 3d 3d 54 4b 5f 54 52 49 47 47 45 52 20 29  op==TK_TRIGGER )
1770: 7b 0a 20 20 20 20 70 54 61 62 20 3d 20 70 50 61  {.    pTab = pPa
1780: 72 73 65 2d 3e 70 54 72 69 67 67 65 72 54 61 62  rse->pTriggerTab
1790: 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 61  ;.  }else{.    a
17a0: 73 73 65 72 74 28 20 70 54 61 62 4c 69 73 74 20  ssert( pTabList 
17b0: 29 3b 0a 20 20 20 20 66 6f 72 28 69 53 72 63 3d  );.    for(iSrc=
17c0: 30 3b 20 41 4c 57 41 59 53 28 69 53 72 63 3c 70  0; ALWAYS(iSrc<p
17d0: 54 61 62 4c 69 73 74 2d 3e 6e 53 72 63 29 3b 20  TabList->nSrc); 
17e0: 69 53 72 63 2b 2b 29 7b 0a 20 20 20 20 20 20 69  iSrc++){.      i
17f0: 66 28 20 70 45 78 70 72 2d 3e 69 54 61 62 6c 65  f( pExpr->iTable
1800: 3d 3d 70 54 61 62 4c 69 73 74 2d 3e 61 5b 69 53  ==pTabList->a[iS
1810: 72 63 5d 2e 69 43 75 72 73 6f 72 20 29 7b 0a 20  rc].iCursor ){. 
1820: 20 20 20 20 20 20 20 70 54 61 62 20 3d 20 70 54         pTab = pT
1830: 61 62 4c 69 73 74 2d 3e 61 5b 69 53 72 63 5d 2e  abList->a[iSrc].
1840: 70 54 61 62 3b 0a 20 20 20 20 20 20 20 20 62 72  pTab;.        br
1850: 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  eak;.      }.   
1860: 20 7d 0a 20 20 7d 0a 20 20 69 43 6f 6c 20 3d 20   }.  }.  iCol = 
1870: 70 45 78 70 72 2d 3e 69 43 6f 6c 75 6d 6e 3b 0a  pExpr->iColumn;.
1880: 20 20 69 66 28 20 4e 45 56 45 52 28 70 54 61 62    if( NEVER(pTab
1890: 3d 3d 30 29 20 29 20 72 65 74 75 72 6e 3b 0a 0a  ==0) ) return;..
18a0: 20 20 69 66 28 20 69 43 6f 6c 3e 3d 30 20 29 7b    if( iCol>=0 ){
18b0: 0a 20 20 20 20 61 73 73 65 72 74 28 20 69 43 6f  .    assert( iCo
18c0: 6c 3c 70 54 61 62 2d 3e 6e 43 6f 6c 20 29 3b 0a  l<pTab->nCol );.
18d0: 20 20 20 20 7a 43 6f 6c 20 3d 20 70 54 61 62 2d      zCol = pTab-
18e0: 3e 61 43 6f 6c 5b 69 43 6f 6c 5d 2e 7a 4e 61 6d  >aCol[iCol].zNam
18f0: 65 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 70  e;.  }else if( p
1900: 54 61 62 2d 3e 69 50 4b 65 79 3e 3d 30 20 29 7b  Tab->iPKey>=0 ){
1910: 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 54 61  .    assert( pTa
1920: 62 2d 3e 69 50 4b 65 79 3c 70 54 61 62 2d 3e 6e  b->iPKey<pTab->n
1930: 43 6f 6c 20 29 3b 0a 20 20 20 20 7a 43 6f 6c 20  Col );.    zCol 
1940: 3d 20 70 54 61 62 2d 3e 61 43 6f 6c 5b 70 54 61  = pTab->aCol[pTa
1950: 62 2d 3e 69 50 4b 65 79 5d 2e 7a 4e 61 6d 65 3b  b->iPKey].zName;
1960: 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 7a 43  .  }else{.    zC
1970: 6f 6c 20 3d 20 22 52 4f 57 49 44 22 3b 0a 20 20  ol = "ROWID";.  
1980: 7d 0a 20 20 61 73 73 65 72 74 28 20 69 44 62 3e  }.  assert( iDb>
1990: 3d 30 20 26 26 20 69 44 62 3c 64 62 2d 3e 6e 44  =0 && iDb<db->nD
19a0: 62 20 29 3b 0a 20 20 69 66 28 20 53 51 4c 49 54  b );.  if( SQLIT
19b0: 45 5f 49 47 4e 4f 52 45 3d 3d 73 71 6c 69 74 65  E_IGNORE==sqlite
19c0: 33 41 75 74 68 52 65 61 64 43 6f 6c 28 70 50 61  3AuthReadCol(pPa
19d0: 72 73 65 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d 65  rse, pTab->zName
19e0: 2c 20 7a 43 6f 6c 2c 20 69 44 62 29 20 29 7b 0a  , zCol, iDb) ){.
19f0: 20 20 20 20 70 45 78 70 72 2d 3e 6f 70 20 3d 20      pExpr->op = 
1a00: 54 4b 5f 4e 55 4c 4c 3b 0a 20 20 7d 0a 7d 0a 0a  TK_NULL;.  }.}..
1a10: 2f 2a 0a 2a 2a 20 44 6f 20 61 6e 20 61 75 74 68  /*.** Do an auth
1a20: 6f 72 69 7a 61 74 69 6f 6e 20 63 68 65 63 6b 20  orization check 
1a30: 75 73 69 6e 67 20 74 68 65 20 63 6f 64 65 20 61  using the code a
1a40: 6e 64 20 61 72 67 75 6d 65 6e 74 73 20 67 69 76  nd arguments giv
1a50: 65 6e 2e 20 20 52 65 74 75 72 6e 0a 2a 2a 20 65  en.  Return.** e
1a60: 69 74 68 65 72 20 53 51 4c 49 54 45 5f 4f 4b 20  ither SQLITE_OK 
1a70: 28 7a 65 72 6f 29 20 6f 72 20 53 51 4c 49 54 45  (zero) or SQLITE
1a80: 5f 49 47 4e 4f 52 45 20 6f 72 20 53 51 4c 49 54  _IGNORE or SQLIT
1a90: 45 5f 44 45 4e 59 2e 20 20 49 66 20 53 51 4c 49  E_DENY.  If SQLI
1aa0: 54 45 5f 44 45 4e 59 0a 2a 2a 20 69 73 20 72 65  TE_DENY.** is re
1ab0: 74 75 72 6e 65 64 2c 20 74 68 65 6e 20 74 68 65  turned, then the
1ac0: 20 65 72 72 6f 72 20 63 6f 75 6e 74 20 61 6e 64   error count and
1ad0: 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20 69   error message i
1ae0: 6e 20 70 50 61 72 73 65 20 61 72 65 0a 2a 2a 20  n pParse are.** 
1af0: 6d 6f 64 69 66 69 65 64 20 61 70 70 72 6f 70 72  modified appropr
1b00: 69 61 74 65 6c 79 2e 0a 2a 2f 0a 69 6e 74 20 73  iately..*/.int s
1b10: 71 6c 69 74 65 33 41 75 74 68 43 68 65 63 6b 28  qlite3AuthCheck(
1b20: 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 73 65  .  Parse *pParse
1b30: 2c 0a 20 20 69 6e 74 20 63 6f 64 65 2c 0a 20 20  ,.  int code,.  
1b40: 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 41 72 67  const char *zArg
1b50: 31 2c 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20  1,.  const char 
1b60: 2a 7a 41 72 67 32 2c 0a 20 20 63 6f 6e 73 74 20  *zArg2,.  const 
1b70: 63 68 61 72 20 2a 7a 41 72 67 33 0a 29 7b 0a 20  char *zArg3.){. 
1b80: 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70   sqlite3 *db = p
1b90: 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20 69 6e 74  Parse->db;.  int
1ba0: 20 72 63 3b 0a 0a 20 20 2f 2a 20 44 6f 6e 27 74   rc;..  /* Don't
1bb0: 20 64 6f 20 61 6e 79 20 61 75 74 68 6f 72 69 7a   do any authoriz
1bc0: 61 74 69 6f 6e 20 63 68 65 63 6b 73 20 69 66 20  ation checks if 
1bd0: 74 68 65 20 64 61 74 61 62 61 73 65 20 69 73 20  the database is 
1be0: 69 6e 69 74 69 61 6c 69 73 69 6e 67 0a 20 20 2a  initialising.  *
1bf0: 2a 20 6f 72 20 69 66 20 74 68 65 20 70 61 72 73  * or if the pars
1c00: 65 72 20 69 73 20 62 65 69 6e 67 20 69 6e 76 6f  er is being invo
1c10: 6b 65 64 20 66 72 6f 6d 20 77 69 74 68 69 6e 20  ked from within 
1c20: 73 71 6c 69 74 65 33 5f 64 65 63 6c 61 72 65 5f  sqlite3_declare_
1c30: 76 74 61 62 2e 0a 20 20 2a 2f 0a 20 20 69 66 28  vtab..  */.  if(
1c40: 20 64 62 2d 3e 69 6e 69 74 2e 62 75 73 79 20 7c   db->init.busy |
1c50: 7c 20 49 4e 5f 53 50 45 43 49 41 4c 5f 50 41 52  | IN_SPECIAL_PAR
1c60: 53 45 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e  SE ){.    return
1c70: 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a   SQLITE_OK;.  }.
1c80: 0a 20 20 69 66 28 20 64 62 2d 3e 78 41 75 74 68  .  if( db->xAuth
1c90: 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65 74 75 72  ==0 ){.    retur
1ca0: 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d  n SQLITE_OK;.  }
1cb0: 0a 0a 20 20 2f 2a 20 45 56 49 44 45 4e 43 45 2d  ..  /* EVIDENCE-
1cc0: 4f 46 3a 20 52 2d 34 33 32 34 39 2d 31 39 38 38  OF: R-43249-1988
1cd0: 32 20 54 68 65 20 74 68 69 72 64 20 74 68 72 6f  2 The third thro
1ce0: 75 67 68 20 73 69 78 74 68 20 70 61 72 61 6d 65  ugh sixth parame
1cf0: 74 65 72 73 20 74 6f 20 74 68 65 0a 20 20 2a 2a  ters to the.  **
1d00: 20 63 61 6c 6c 62 61 63 6b 20 61 72 65 20 65 69   callback are ei
1d10: 74 68 65 72 20 4e 55 4c 4c 20 70 6f 69 6e 74 65  ther NULL pointe
1d20: 72 73 20 6f 72 20 7a 65 72 6f 2d 74 65 72 6d 69  rs or zero-termi
1d30: 6e 61 74 65 64 20 73 74 72 69 6e 67 73 20 74 68  nated strings th
1d40: 61 74 0a 20 20 2a 2a 20 63 6f 6e 74 61 69 6e 20  at.  ** contain 
1d50: 61 64 64 69 74 69 6f 6e 61 6c 20 64 65 74 61 69  additional detai
1d60: 6c 73 20 61 62 6f 75 74 20 74 68 65 20 61 63 74  ls about the act
1d70: 69 6f 6e 20 74 6f 20 62 65 20 61 75 74 68 6f 72  ion to be author
1d80: 69 7a 65 64 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20  ized..  **.  ** 
1d90: 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 74 65  The following te
1da0: 73 74 63 61 73 65 28 29 20 6d 61 63 72 6f 73 20  stcase() macros 
1db0: 73 68 6f 77 20 74 68 61 74 20 61 6e 79 20 6f 66  show that any of
1dc0: 20 74 68 65 20 33 72 64 20 74 68 72 6f 75 67 68   the 3rd through
1dd0: 20 36 74 68 0a 20 20 2a 2a 20 70 61 72 61 6d 65   6th.  ** parame
1de0: 74 65 72 73 20 63 61 6e 20 62 65 20 65 69 74 68  ters can be eith
1df0: 65 72 20 4e 55 4c 4c 20 6f 72 20 61 20 73 74 72  er NULL or a str
1e00: 69 6e 67 2e 20 2a 2f 0a 20 20 74 65 73 74 63 61  ing. */.  testca
1e10: 73 65 28 20 7a 41 72 67 31 3d 3d 30 20 29 3b 0a  se( zArg1==0 );.
1e20: 20 20 74 65 73 74 63 61 73 65 28 20 7a 41 72 67    testcase( zArg
1e30: 32 3d 3d 30 20 29 3b 0a 20 20 74 65 73 74 63 61  2==0 );.  testca
1e40: 73 65 28 20 7a 41 72 67 33 3d 3d 30 20 29 3b 0a  se( zArg3==0 );.
1e50: 20 20 74 65 73 74 63 61 73 65 28 20 70 50 61 72    testcase( pPar
1e60: 73 65 2d 3e 7a 41 75 74 68 43 6f 6e 74 65 78 74  se->zAuthContext
1e70: 3d 3d 30 20 29 3b 0a 0a 20 20 72 63 20 3d 20 64  ==0 );..  rc = d
1e80: 62 2d 3e 78 41 75 74 68 28 64 62 2d 3e 70 41 75  b->xAuth(db->pAu
1e90: 74 68 41 72 67 2c 20 63 6f 64 65 2c 20 7a 41 72  thArg, code, zAr
1ea0: 67 31 2c 20 7a 41 72 67 32 2c 20 7a 41 72 67 33  g1, zArg2, zArg3
1eb0: 2c 20 70 50 61 72 73 65 2d 3e 7a 41 75 74 68 43  , pParse->zAuthC
1ec0: 6f 6e 74 65 78 74 0a 23 69 66 64 65 66 20 53 51  ontext.#ifdef SQ
1ed0: 4c 49 54 45 5f 55 53 45 52 5f 41 55 54 48 45 4e  LITE_USER_AUTHEN
1ee0: 54 49 43 41 54 49 4f 4e 0a 20 20 20 20 20 20 20  TICATION.       
1ef0: 20 20 20 20 20 20 20 20 20 20 2c 64 62 2d 3e 61            ,db->a
1f00: 75 74 68 2e 7a 41 75 74 68 55 73 65 72 0a 23 65  uth.zAuthUser.#e
1f10: 6e 64 69 66 0a 20 20 20 20 20 20 20 20 20 20 20  ndif.           
1f20: 20 20 20 20 20 29 3b 0a 20 20 69 66 28 20 72 63       );.  if( rc
1f30: 3d 3d 53 51 4c 49 54 45 5f 44 45 4e 59 20 29 7b  ==SQLITE_DENY ){
1f40: 0a 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f  .    sqlite3Erro
1f50: 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22 6e 6f  rMsg(pParse, "no
1f60: 74 20 61 75 74 68 6f 72 69 7a 65 64 22 29 3b 0a  t authorized");.
1f70: 20 20 20 20 70 50 61 72 73 65 2d 3e 72 63 20 3d      pParse->rc =
1f80: 20 53 51 4c 49 54 45 5f 41 55 54 48 3b 0a 20 20   SQLITE_AUTH;.  
1f90: 7d 65 6c 73 65 20 69 66 28 20 72 63 21 3d 53 51  }else if( rc!=SQ
1fa0: 4c 49 54 45 5f 4f 4b 20 26 26 20 72 63 21 3d 53  LITE_OK && rc!=S
1fb0: 51 4c 49 54 45 5f 49 47 4e 4f 52 45 20 29 7b 0a  QLITE_IGNORE ){.
1fc0: 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f      rc = SQLITE_
1fd0: 44 45 4e 59 3b 0a 20 20 20 20 73 71 6c 69 74 65  DENY;.    sqlite
1fe0: 41 75 74 68 42 61 64 52 65 74 75 72 6e 43 6f 64  AuthBadReturnCod
1ff0: 65 28 70 50 61 72 73 65 29 3b 0a 20 20 7d 0a 20  e(pParse);.  }. 
2000: 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f   return rc;.}../
2010: 2a 0a 2a 2a 20 50 75 73 68 20 61 6e 20 61 75 74  *.** Push an aut
2020: 68 6f 72 69 7a 61 74 69 6f 6e 20 63 6f 6e 74 65  horization conte
2030: 78 74 2e 20 20 41 66 74 65 72 20 74 68 69 73 20  xt.  After this 
2040: 72 6f 75 74 69 6e 65 20 69 73 20 63 61 6c 6c 65  routine is calle
2050: 64 2c 20 74 68 65 0a 2a 2a 20 7a 41 72 67 33 20  d, the.** zArg3 
2060: 61 72 67 75 6d 65 6e 74 20 74 6f 20 61 75 74 68  argument to auth
2070: 6f 72 69 7a 61 74 69 6f 6e 20 63 61 6c 6c 62 61  orization callba
2080: 63 6b 73 20 77 69 6c 6c 20 62 65 20 7a 43 6f 6e  cks will be zCon
2090: 74 65 78 74 20 75 6e 74 69 6c 0a 2a 2a 20 70 6f  text until.** po
20a0: 70 70 65 64 2e 20 20 4f 72 20 69 66 20 70 50 61  pped.  Or if pPa
20b0: 72 73 65 3d 3d 30 2c 20 74 68 69 73 20 72 6f 75  rse==0, this rou
20c0: 74 69 6e 65 20 69 73 20 61 20 6e 6f 2d 6f 70 2e  tine is a no-op.
20d0: 0a 2a 2f 0a 76 6f 69 64 20 73 71 6c 69 74 65 33  .*/.void sqlite3
20e0: 41 75 74 68 43 6f 6e 74 65 78 74 50 75 73 68 28  AuthContextPush(
20f0: 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 73 65  .  Parse *pParse
2100: 2c 0a 20 20 41 75 74 68 43 6f 6e 74 65 78 74 20  ,.  AuthContext 
2110: 2a 70 43 6f 6e 74 65 78 74 2c 20 0a 20 20 63 6f  *pContext, .  co
2120: 6e 73 74 20 63 68 61 72 20 2a 7a 43 6f 6e 74 65  nst char *zConte
2130: 78 74 0a 29 7b 0a 20 20 61 73 73 65 72 74 28 20  xt.){.  assert( 
2140: 70 50 61 72 73 65 20 29 3b 0a 20 20 70 43 6f 6e  pParse );.  pCon
2150: 74 65 78 74 2d 3e 70 50 61 72 73 65 20 3d 20 70  text->pParse = p
2160: 50 61 72 73 65 3b 0a 20 20 70 43 6f 6e 74 65 78  Parse;.  pContex
2170: 74 2d 3e 7a 41 75 74 68 43 6f 6e 74 65 78 74 20  t->zAuthContext 
2180: 3d 20 70 50 61 72 73 65 2d 3e 7a 41 75 74 68 43  = pParse->zAuthC
2190: 6f 6e 74 65 78 74 3b 0a 20 20 70 50 61 72 73 65  ontext;.  pParse
21a0: 2d 3e 7a 41 75 74 68 43 6f 6e 74 65 78 74 20 3d  ->zAuthContext =
21b0: 20 7a 43 6f 6e 74 65 78 74 3b 0a 7d 0a 0a 2f 2a   zContext;.}../*
21c0: 0a 2a 2a 20 50 6f 70 20 61 6e 20 61 75 74 68 6f  .** Pop an autho
21d0: 72 69 7a 61 74 69 6f 6e 20 63 6f 6e 74 65 78 74  rization context
21e0: 20 74 68 61 74 20 77 61 73 20 70 72 65 76 69 6f   that was previo
21f0: 75 73 6c 79 20 70 75 73 68 65 64 0a 2a 2a 20 62  usly pushed.** b
2200: 79 20 73 71 6c 69 74 65 33 41 75 74 68 43 6f 6e  y sqlite3AuthCon
2210: 74 65 78 74 50 75 73 68 0a 2a 2f 0a 76 6f 69 64  textPush.*/.void
2220: 20 73 71 6c 69 74 65 33 41 75 74 68 43 6f 6e 74   sqlite3AuthCont
2230: 65 78 74 50 6f 70 28 41 75 74 68 43 6f 6e 74 65  extPop(AuthConte
2240: 78 74 20 2a 70 43 6f 6e 74 65 78 74 29 7b 0a 20  xt *pContext){. 
2250: 20 69 66 28 20 70 43 6f 6e 74 65 78 74 2d 3e 70   if( pContext->p
2260: 50 61 72 73 65 20 29 7b 0a 20 20 20 20 70 43 6f  Parse ){.    pCo
2270: 6e 74 65 78 74 2d 3e 70 50 61 72 73 65 2d 3e 7a  ntext->pParse->z
2280: 41 75 74 68 43 6f 6e 74 65 78 74 20 3d 20 70 43  AuthContext = pC
2290: 6f 6e 74 65 78 74 2d 3e 7a 41 75 74 68 43 6f 6e  ontext->zAuthCon
22a0: 74 65 78 74 3b 0a 20 20 20 20 70 43 6f 6e 74 65  text;.    pConte
22b0: 78 74 2d 3e 70 50 61 72 73 65 20 3d 20 30 3b 0a  xt->pParse = 0;.
22c0: 20 20 7d 0a 7d 0a 0a 23 65 6e 64 69 66 20 2f 2a    }.}..#endif /*
22d0: 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54   SQLITE_OMIT_AUT
22e0: 48 4f 52 49 5a 41 54 49 4f 4e 20 2a 2f 0a        HORIZATION */.