/ Check-in [2e01096b]
Login

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:Honor key query parameters for SEE on the URI filename for ATTACH and VACUUM INTO.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: 2e01096b8933a2315e3dffcd7d0de84c744c1b4be1d909388c61f0fd636ddb99
User & Date: drh 2019-02-02 15:59:49
Context
2019-02-02
18:24
New testcase added to test/fuzzdata8.db. check-in: 2f5c1925 user: drh tags: trunk
15:59
Honor key query parameters for SEE on the URI filename for ATTACH and VACUUM INTO. check-in: 2e01096b user: drh tags: trunk
15:05
Give a link to the URI Filename document in the documentation to the various sqlite3_uri_parameter() interfaces. check-in: 69b0b2ee user: drh tags: trunk
Changes
Hide Diffs Side-by-Side Diffs Show Whitespace Changes Patch

Changes to src/attach.c.

   151    151         sqlite3_result_error(context, zErr, -1);
   152    152         sqlite3_free(zErr);
   153    153         return;
   154    154       }
   155    155       assert( pVfs );
   156    156       flags |= SQLITE_OPEN_MAIN_DB;
   157    157       rc = sqlite3BtreeOpen(pVfs, zPath, db, &pNew->pBt, 0, flags);
   158         -    sqlite3_free( zPath );
   159    158       db->nDb++;
          159  +    pNew->zDbSName = sqlite3DbStrDup(db, zName);
   160    160     }
   161    161     db->noSharedCache = 0;
   162    162     if( rc==SQLITE_CONSTRAINT ){
   163    163       rc = SQLITE_ERROR;
   164    164       zErrDyn = sqlite3MPrintf(db, "database is already attached");
   165    165     }else if( rc==SQLITE_OK ){
   166    166       Pager *pPager;
................................................................................
   180    180   #ifndef SQLITE_OMIT_PAGER_PRAGMAS
   181    181       sqlite3BtreeSetPagerFlags(pNew->pBt,
   182    182                         PAGER_SYNCHRONOUS_FULL | (db->flags & PAGER_FLAGS_MASK));
   183    183   #endif
   184    184       sqlite3BtreeLeave(pNew->pBt);
   185    185     }
   186    186     pNew->safety_level = SQLITE_DEFAULT_SYNCHRONOUS+1;
   187         -  if( !REOPEN_AS_MEMDB(db) ) pNew->zDbSName = sqlite3DbStrDup(db, zName);
   188    187     if( rc==SQLITE_OK && pNew->zDbSName==0 ){
   189    188       rc = SQLITE_NOMEM_BKPT;
   190    189     }
   191    190   
   192    191   
   193    192   #ifdef SQLITE_HAS_CODEC
   194    193     if( rc==SQLITE_OK ){
................................................................................
   208    207         case SQLITE_BLOB:
   209    208           nKey = sqlite3_value_bytes(argv[2]);
   210    209           zKey = (char *)sqlite3_value_blob(argv[2]);
   211    210           rc = sqlite3CodecAttach(db, db->nDb-1, zKey, nKey);
   212    211           break;
   213    212   
   214    213         case SQLITE_NULL:
   215         -        /* No key specified.  Use the key from the main database */
          214  +        /* No key specified.  Use the key from URI filename, or if none,
          215  +        ** use the key from the main database. */
          216  +        if( sqlite3CodecQueryParameters(db, zName, zPath)==0 ){
   216    217           sqlite3CodecGetKey(db, 0, (void**)&zKey, &nKey);
   217    218           if( nKey || sqlite3BtreeGetOptimalReserve(db->aDb[0].pBt)>0 ){
   218    219             rc = sqlite3CodecAttach(db, db->nDb-1, zKey, nKey);
   219    220           }
          221  +        }
   220    222           break;
   221    223       }
   222    224     }
   223    225   #endif
          226  +  sqlite3_free( zPath );
   224    227   
   225    228     /* If the file was opened successfully, read the schema for the new database.
   226    229     ** If this fails, or if opening the file failed, then close the file and 
   227    230     ** remove the entry from the db->aDb[] array. i.e. put everything back the
   228    231     ** way we found it.
   229    232     */
   230    233     if( rc==SQLITE_OK ){

Changes to src/main.c.

  2938   2938       zFile = 0;
  2939   2939     }
  2940   2940     *pFlags = flags;
  2941   2941     *pzFile = zFile;
  2942   2942     return rc;
  2943   2943   }
  2944   2944   
         2945  +#if defined(SQLITE_HAS_CODEC)
         2946  +/*
         2947  +** Process URI filename query parameters relevant to the SQLite Encryption
         2948  +** Extension.  Return true if any of the relevant query parameters are
         2949  +** seen and return false if not.
         2950  +*/
         2951  +int sqlite3CodecQueryParameters(
         2952  +  sqlite3 *db,           /* Database connection */
         2953  +  const char *zDb,       /* Which schema is being created/attached */
         2954  +  const char *zUri       /* URI filename */
         2955  +){
         2956  +  const char *zKey;
         2957  +  if( (zKey = sqlite3_uri_parameter(zUri, "hexkey"))!=0 && zKey[0] ){
         2958  +    u8 iByte;
         2959  +    int i;
         2960  +    char zDecoded[40];
         2961  +    for(i=0, iByte=0; i<sizeof(zDecoded)*2 && sqlite3Isxdigit(zKey[i]); i++){
         2962  +      iByte = (iByte<<4) + sqlite3HexToInt(zKey[i]);
         2963  +      if( (i&1)!=0 ) zDecoded[i/2] = iByte;
         2964  +    }
         2965  +    sqlite3_key_v2(db, zDb, zDecoded, i/2);
         2966  +    return 1;
         2967  +  }else if( (zKey = sqlite3_uri_parameter(zUri, "key"))!=0 ){
         2968  +    sqlite3_key_v2(db, zDb, zKey, sqlite3Strlen30(zKey));
         2969  +    return 1;
         2970  +  }else if( (zKey = sqlite3_uri_parameter(zUri, "textkey"))!=0 ){
         2971  +    sqlite3_key_v2(db, zDb, zKey, -1);
         2972  +    return 1;
         2973  +  }else{
         2974  +    return 0;
         2975  +  }
         2976  +}
         2977  +#endif
         2978  +
  2945   2979   
  2946   2980   /*
  2947   2981   ** This routine does the work of opening a database on behalf of
  2948   2982   ** sqlite3_open() and sqlite3_open16(). The database filename "zFilename"  
  2949   2983   ** is UTF-8 encoded.
  2950   2984   */
  2951   2985   static int openDatabase(
................................................................................
  3283   3317     if( sqlite3GlobalConfig.xSqllog ){
  3284   3318       /* Opening a db handle. Fourth parameter is passed 0. */
  3285   3319       void *pArg = sqlite3GlobalConfig.pSqllogArg;
  3286   3320       sqlite3GlobalConfig.xSqllog(pArg, db, zFilename, 0);
  3287   3321     }
  3288   3322   #endif
  3289   3323   #if defined(SQLITE_HAS_CODEC)
  3290         -  if( rc==SQLITE_OK ){
  3291         -    const char *zKey;
  3292         -    if( (zKey = sqlite3_uri_parameter(zOpen, "hexkey"))!=0 && zKey[0] ){
  3293         -      u8 iByte;
  3294         -      int i;
  3295         -      char zDecoded[40];
  3296         -      for(i=0, iByte=0; i<sizeof(zDecoded)*2 && sqlite3Isxdigit(zKey[i]); i++){
  3297         -        iByte = (iByte<<4) + sqlite3HexToInt(zKey[i]);
  3298         -        if( (i&1)!=0 ) zDecoded[i/2] = iByte;
  3299         -      }
  3300         -      sqlite3_key_v2(db, 0, zDecoded, i/2);
  3301         -    }else if( (zKey = sqlite3_uri_parameter(zOpen, "key"))!=0 ){
  3302         -      sqlite3_key_v2(db, 0, zKey, sqlite3Strlen30(zKey));
  3303         -    }
  3304         -  }
         3324  +  if( rc==SQLITE_OK ) sqlite3CodecQueryParameters(db, 0, zOpen);
  3305   3325   #endif
  3306   3326     sqlite3_free(zOpen);
  3307   3327     return rc & 0xff;
  3308   3328   }
         3329  +
  3309   3330   
  3310   3331   /*
  3311   3332   ** Open a new database handle.
  3312   3333   */
  3313   3334   int sqlite3_open(
  3314   3335     const char *zFilename, 
  3315   3336     sqlite3 **ppDb 

Changes to src/sqliteInt.h.

  3878   3878   void sqlite3AddPrimaryKey(Parse*, ExprList*, int, int, int);
  3879   3879   void sqlite3AddCheckConstraint(Parse*, Expr*);
  3880   3880   void sqlite3AddDefaultValue(Parse*,Expr*,const char*,const char*);
  3881   3881   void sqlite3AddCollateType(Parse*, Token*);
  3882   3882   void sqlite3EndTable(Parse*,Token*,Token*,u8,Select*);
  3883   3883   int sqlite3ParseUri(const char*,const char*,unsigned int*,
  3884   3884                       sqlite3_vfs**,char**,char **);
         3885  +#ifdef SQLITE_HAS_CODEC
         3886  +  int sqlite3CodecQueryParameters(sqlite3*,const char*,const char*);
         3887  +#else
         3888  +# define sqlite3CodecQueryParameters(A,B,C) 0
         3889  +#endif
  3885   3890   Btree *sqlite3DbNameToBtree(sqlite3*,const char*);
  3886   3891   
  3887   3892   #ifdef SQLITE_UNTESTABLE
  3888   3893   # define sqlite3FaultSim(X) SQLITE_OK
  3889   3894   #else
  3890   3895     int sqlite3FaultSim(int);
  3891   3896   #endif