SQLite Archiver

Check-in [06c26c027e]
Login

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

Overview
Comment:Add support for encrypted SQL archives using the SQLite Encryption Extension.
Downloads: Tarball | ZIP archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1: 06c26c027e211b985a733da2edac57f09d5132ec
User & Date: drh 2016-07-11 19:21:59.906
Context
2016-07-11
19:22
Update to the version 3.13.0 of SQLite. check-in: 52199b0fb4 user: drh tags: trunk
19:21
Add support for encrypted SQL archives using the SQLite Encryption Extension. check-in: 06c26c027e user: drh tags: trunk
2014-12-10
22:06
Add a missing "closedir()". check-in: 15adeb2f9a user: drh tags: trunk
Changes
Side-by-Side Diff Ignore Whitespace Patch
Changes to Makefile.
1
2
3
4
5
6

7
8
9
10
11
12

13
14
15

16
17
18
19
20
21
1
2
3
4
5

6
7
8
9
10
11

12
13
14

15
16
17
18
19
20
21





-
+





-
+


-
+






#!/bin/make

CC = gcc -g -I. -D_FILE_OFFSET_BITS=64 -Wall -Werror
ZLIB = -lz
FUSELIB = -lfuse
SQLITE_OPT = -DSQLITE_THREADSAFE=0 -DSQLITE_OMIT_LOAD_EXTENSION
SQLITE_OPT = $(OPT) -DSQLITE_THREADSAFE=0 -DSQLITE_OMIT_LOAD_EXTENSION

all: sqlar
	

sqlar:	sqlar.c sqlite3.o
	$(CC) -o sqlar sqlar.c sqlite3.o $(ZLIB)
	$(CC) -o sqlar $(OPT) sqlar.c sqlite3.o $(ZLIB)

sqlarfs:	sqlarfs.c sqlite3.o
	$(CC) -o sqlarfs sqlarfs.c sqlite3.o $(ZLIB) $(FUSELIB)
	$(CC) -o sqlarfs $(OPT) sqlarfs.c sqlite3.o $(ZLIB) $(FUSELIB)

sqlite3.o:	sqlite3.c sqlite3.h
	$(CC) -c sqlite3.c $(SQLITE_OPT) sqlite3.c

clean:	
	rm -f sqlar sqlarfs sqlite3.o
Changes to sqlar.c.
18
19
20
21
22
23
24





25
26
27
28
29
30
31
32
33
34
35







36
37
38
39
40
41
42
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35





36
37
38
39
40
41
42
43
44
45
46
47
48
49







+
+
+
+
+






-
-
-
-
-
+
+
+
+
+
+
+







#include <stdlib.h>
#include <zlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <dirent.h>
#include <string.h>
#include <assert.h>
#include <ctype.h>

/* Maximum length of a pass-phrase */
#define MX_PASSPHRASE  40

/*
** Show a help message and quit.
*/
static void showHelp(const char *argv0){
  fprintf(stderr, "Usage: %s [options] archive [files...]\n", argv0);
  fprintf(stderr, "Options:\n"
                  "   -l      List files in archive\n"
                  "   -n      Do not compress files\n"
                  "   -x      Extract files from archive\n"
                  "   -v      Verbose output\n"
  fprintf(stderr,
     "Options:\n"
     "   -e      Prompt for passphrase.  -ee to scramble the prompt\n"
     "   -l      List files in archive\n"
     "   -n      Do not compress files\n"
     "   -x      Extract files from archive\n"
     "   -v      Verbose output\n"
  );
  exit(1);
}

/*
** The database schema:
*/
87
88
89
90
91
92
93
94
95


































































































96
97
98

99
100
101
102
103
104
105
106
107
108













109
110
111
112
113
114
115
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202

203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233









+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+


-
+










+
+
+
+
+
+
+
+
+
+
+
+
+







  va_list ap;
  va_start(ap, zFormat);
  vfprintf(stderr, zFormat, ap);
  va_end(ap);
  db_close(0);
  exit(1);
}

/*
** Scramble substitution matrix:
*/
static char aSubst[256];

/*
** Descramble the password
*/
static void descramble(char *z){
  int i;
  for(i=0; z[i]; i++) z[i] = aSubst[(unsigned char)z[i]];
}

/* Print a string in 5-letter groups */
static void printFive(const unsigned char *z){
  int i;
  for(i=0; z[i]; i++){
    if( i>0 && (i%5)==0 ) putchar(' ');
    putchar(z[i]);
  }
  putchar('\n');
}

/* Return a pseudo-random integer between 0 and N-1 */
static int randint(int N){
  unsigned char x;
  assert( N<256 );
  sqlite3_randomness(1, &x);
  return x % N;
}

/*
** Generate and print a random scrambling of letters a through z (omitting x)
** and set up the aSubst[] matrix to descramble.
*/
static void generateScrambleCode(void){
  unsigned char zOrig[30];
  unsigned char zA[30];
  unsigned char zB[30];
  int nA = 25;
  int nB = 0;
  int i;
  memcpy(zOrig, "abcdefghijklmnopqrstuvwyz", nA+1);
  memcpy(zA, zOrig, nA+1);
  assert( nA==(int)strlen((char*)zA) );
  for(i=0; i<sizeof(aSubst); i++) aSubst[i] = i;
  printFive(zA);
  while( nA>0 ){
    int x = randint(nA);
    zB[nB++] = zA[x];
    zA[x] = zA[--nA];
  }
  assert( nB==25 );
  zB[nB] = 0;
  printFive(zB);
  for(i=0; i<nB; i++) aSubst[zB[i]] = zOrig[i];
}

/*
** Do a single prompt for a passphrase.  Store the results in the blob.
**
** If the FOSSIL_PWREADER environment variable is set, then it will
** be the name of a program that prompts the user for their password/
** passphrase in a secure manner.  The program should take one or more
** arguments which are the prompts and should output the acquired
** passphrase as a single line on stdout.  This function will read the
** output using popen().
**
** If FOSSIL_PWREADER is not set, or if it is not the name of an
** executable, then use the C-library getpass() routine.
**
** The return value is a pointer to a static buffer that is overwritten
** on subsequent calls to this same routine.
*/
static void prompt_for_passphrase(
  const char *zPrompt,    /* Passphrase prompt */
  int doScramble,         /* Scramble the input if true */
  char *zPassphrase       /* Write result here */
){
  char *z;
  int i;
  if( doScramble ){
    generateScrambleCode();
    z = getpass(zPrompt);
    if( z ) descramble(z);
    printf("\033[3A\033[J");  /* Erase previous three lines */
    fflush(stdout);
  }else{
    z = getpass(zPrompt);
  }
  while( isspace(z[0]) ) z++;
  for(i=0; i<MX_PASSPHRASE-1; i++){
    zPassphrase[i] = z[i];
  }
  while( i>0 && isspace(z[i-1]) ){ i--; }
  zPassphrase[i] = 0;
}

/*
** Open the database.
*/
static void db_open(const char *zArchive, int writeFlag){
static void db_open(const char *zArchive, int writeFlag, int seeFlag){
  int rc;
  int fg;
  if( writeFlag ){
    fg = SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE;
  }else{
    fg = SQLITE_OPEN_READONLY;
  }
  rc = sqlite3_open_v2(zArchive, &db, fg, 0);
  if( rc ) errorMsg("Cannot open archive [%s]: %s\n", zArchive,
                    sqlite3_errmsg(db));
  if( seeFlag ){
    char *zSql;
    char zPassPhrase[MX_PASSPHRASE+1];
#ifndef SQLITE_HAS_CODEC
    printf("WARNING:  The passphrase is a no-op because this build of\n"
           "sqlar is compiled without encryption capabilities.\n");
#endif
    memset(zPassPhrase, 0, sizeof(zPassPhrase));
    prompt_for_passphrase("passphrase: ", seeFlag>1, zPassPhrase);
    zSql = sqlite3_mprintf("PRAGMA key(%Q)", zPassPhrase);
    sqlite3_exec(db, zSql, 0, 0, 0);
    sqlite3_free(zSql);
  }
  sqlite3_exec(db, "BEGIN", 0, 0, 0);
  sqlite3_exec(db, zSchema, 0, 0, 0);
}

/*
** Prepare the pStmt statement.
*/
367
368
369
370
371
372
373

374
375
376
377
378
379
380
381
382
383
384
385
386

387
388
389
390
391
392
393
394
395
396
397
398
399
400
401

402
403
404
405
406
407
408
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520

521
522
523
524
525
526
527
528







+













+














-
+







  const char *zArchive = 0;
  char **azFiles = 0;
  int nFiles = 0;
  int listFlag = 0;
  int extractFlag = 0;
  int verboseFlag = 0;
  int noCompress = 0;
  int seeFlag = 0;
  int i, j;

  if( sqlite3_strglob("*/unsqlar", argv[0])==0 ){
    extractFlag = 1;
  }
  for(i=1; i<argc; i++){
    if( argv[i][0]=='-' ){
      for(j=1; argv[i][j]; j++){
        switch( argv[i][j] ){
          case 'l':   listFlag = 1;    break;
          case 'n':   noCompress = 1;  break;
          case 'v':   verboseFlag = 1; break;
          case 'x':   extractFlag = 1; break;
          case 'e':   seeFlag++;       break;
          case '-':   break;
          default:    showHelp(argv[0]);
        }
      }
    }else if( zArchive==0 ){
      zArchive = argv[i];
    }else{
      azFiles = &argv[i];
      nFiles = argc - i;
      break;
    }
  }
  if( zArchive==0 ) showHelp(argv[0]);
  if( listFlag ){
    db_open(zArchive, 0);
    db_open(zArchive, 0, seeFlag);
    if( verboseFlag ){
      db_prepare(
          "SELECT name, sz, length(data), mode, datetime(mtime,'unixepoch')"
          " FROM sqlar ORDER BY name"
      );
      while( sqlite3_step(pStmt)==SQLITE_ROW ){
        printf("%10d %10d %03o %s %s\n", 
419
420
421
422
423
424
425
426

427
428
429
430
431
432
433
539
540
541
542
543
544
545

546
547
548
549
550
551
552
553







-
+







      while( sqlite3_step(pStmt)==SQLITE_ROW ){
        printf("%s\n", sqlite3_column_text(pStmt,0));
      }
    }
    db_close(1);
  }else if( extractFlag ){
    const char *zSql;
    db_open(zArchive, 0);
    db_open(zArchive, 0, seeFlag);
    if( nFiles ){
      NameList x;
      x.azName = azFiles;
      x.nName = nFiles;
      sqlite3_create_function(db, "name_on_list", 1, SQLITE_UTF8,
                              (char*)&x, name_on_list, 0, 0);
      zSql = "SELECT name, mode, mtime, sz, data FROM sqlar"
451
452
453
454
455
456
457
458

459
460
461
462
463
464
465
571
572
573
574
575
576
577

578
579
580
581
582
583
584
585







-
+







                 sqlite3_column_int(pStmt,3),
                 sqlite3_column_blob(pStmt,4),
                 sqlite3_column_bytes(pStmt,4));
    }
    db_close(1);
  }else{
    if( azFiles==0 ) showHelp(argv[0]);
    db_open(zArchive, 1);
    db_open(zArchive, 1, seeFlag);
    for(i=0; i<nFiles; i++){
      add_file(azFiles[i], verboseFlag, noCompress);
    }
    db_close(1);
  }
  return 0;
}
Changes to sqlarfs.c.
13
14
15
16
17
18
19


20
21
22
23
24
25
26
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28







+
+







#include <errno.h>
#include <fcntl.h>
#include <zlib.h>
#include "sqlite3.h"
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <assert.h>
#include <ctype.h>

/*
** Global state information about the archive
*/
struct sGlobal {
  sqlite3 *db;           /* Open database connection */
  sqlite3_stmt *pStat;   /* Prepared statement to read stat info */
213
214
215
216
217
218
219



















































































































220
221




222

223

224
225













226

227

228

229
230
231
232













233
234
235
236
237
238
239
240
241
242
243

244
245
246
247
248
249
250
251
252
253
254
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344

345


346
347
348
349
350
351
352
353
354
355
356
357
358

359
360
361

362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389

390
391
392
393
394
395
396
397
398
399
400
401







+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+


+
+
+
+

+
-
+
-
-
+
+
+
+
+
+
+
+
+
+
+
+
+
-
+

+
-
+




+
+
+
+
+
+
+
+
+
+
+
+
+










-
+












static struct fuse_operations sqlarfs_methods = {
  .getattr = sqlarfs_getattr,
  .readdir = sqlarfs_readdir,
  .open   	= sqlarfs_open,
  .read    = sqlarfs_read,
};

/*
** Show a help message and quit.
*/
static void showHelp(const char *argv0){
  fprintf(stderr, "Usage: %s [options] archive mount-point\n", argv0);
  fprintf(stderr,
     "Options:\n"
     "   -e      Prompt for passphrase.  -ee to scramble the prompt\n"
  );
  exit(1);
}

/* Maximum length of a pass-phrase */
#define MX_PASSPHRASE  40

/*
** Scramble substitution matrix:
*/
static char aSubst[256];

/*
** Descramble the password
*/
static void descramble(char *z){
  int i;
  for(i=0; z[i]; i++) z[i] = aSubst[(unsigned char)z[i]];
}

/* Print a string in 5-letter groups */
static void printFive(const unsigned char *z){
  int i;
  for(i=0; z[i]; i++){
    if( i>0 && (i%5)==0 ) putchar(' ');
    putchar(z[i]);
  }
  putchar('\n');
}

/* Return a pseudo-random integer between 0 and N-1 */
static int randint(int N){
  unsigned char x;
  assert( N<256 );
  sqlite3_randomness(1, &x);
  return x % N;
}

/*
** Generate and print a random scrambling of letters a through z (omitting x)
** and set up the aSubst[] matrix to descramble.
*/
static void generateScrambleCode(void){
  unsigned char zOrig[30];
  unsigned char zA[30];
  unsigned char zB[30];
  int nA = 25;
  int nB = 0;
  int i;
  memcpy(zOrig, "abcdefghijklmnopqrstuvwyz", nA+1);
  memcpy(zA, zOrig, nA+1);
  assert( nA==(int)strlen((char*)zA) );
  for(i=0; i<sizeof(aSubst); i++) aSubst[i] = i;
  printFive(zA);
  while( nA>0 ){
    int x = randint(nA);
    zB[nB++] = zA[x];
    zA[x] = zA[--nA];
  }
  assert( nB==25 );
  zB[nB] = 0;
  printFive(zB);
  for(i=0; i<nB; i++) aSubst[zB[i]] = zOrig[i];
}

/*
** Do a single prompt for a passphrase.  Store the results in the blob.
**
** If the FOSSIL_PWREADER environment variable is set, then it will
** be the name of a program that prompts the user for their password/
** passphrase in a secure manner.  The program should take one or more
** arguments which are the prompts and should output the acquired
** passphrase as a single line on stdout.  This function will read the
** output using popen().
**
** If FOSSIL_PWREADER is not set, or if it is not the name of an
** executable, then use the C-library getpass() routine.
**
** The return value is a pointer to a static buffer that is overwritten
** on subsequent calls to this same routine.
*/
static void prompt_for_passphrase(
  const char *zPrompt,    /* Passphrase prompt */
  int doScramble,         /* Scramble the input if true */
  char *zPassphrase       /* Write result here */
){
  char *z;
  int i;
  if( doScramble ){
    generateScrambleCode();
    z = getpass(zPrompt);
    if( z ) descramble(z);
    printf("\033[3A\033[J");  /* Erase previous three lines */
    fflush(stdout);
  }else{
    z = getpass(zPrompt);
  }
  while( isspace(z[0]) ) z++;
  for(i=0; i<MX_PASSPHRASE-1; i++){
    zPassphrase[i] = z[i];
  }
  while( i>0 && isspace(z[i-1]) ){ i--; }
  zPassphrase[i] = 0;
}


int main(int argc, char **argv){
  int rc;
  int i, j;
  int seeFlag = 0;
  char *zArchive = 0;
  char *zMountPoint = 0;
  char *azNewArg[5];
  for(i=1; i<argc; i++){
  if( argc!=3 ){
    if( argv[i][0]=='-' ){
    fprintf(stderr, "Usage: %s SQLAR-ARCHIVE MOUNT-POINT\n",
            argv[0]);
      for(j=1; argv[i][j]; j++){
        switch( argv[i][j] ){
          case 'e':   seeFlag++;       break;
          case '-':   break;
          default:    showHelp(argv[0]);
        }
      }
    }else if( zArchive==0 ){
      zArchive = argv[i];
    }else if( zMountPoint==0 ){
      zMountPoint = argv[i];
    }else{
      showHelp(argv[0]);
    exit(1);
    }
  }
  if( zMountPoint==0 ) showHelp(argv[0]);
  rc = sqlite3_open(argv[1], &g.db);
  rc = sqlite3_open(zArchive, &g.db);
  if( rc!=SQLITE_OK ){
    fprintf(stderr, "Cannot open sqlar file [%s]\n", argv[1]);
    exit(1);
  }
  if( seeFlag ){
    char *zSql;
    char zPassPhrase[MX_PASSPHRASE+1];
#ifndef SQLITE_HAS_CODEC
    printf("WARNING:  The passphrase is a no-op because this build of\n"
           "sqlar is compiled without encryption capabilities.\n");
#endif
    memset(zPassPhrase, 0, sizeof(zPassPhrase));
    prompt_for_passphrase("passphrase: ", seeFlag>1, zPassPhrase);
    zSql = sqlite3_mprintf("PRAGMA key(%Q)", zPassPhrase);
    sqlite3_exec(g.db, zSql, 0, 0, 0);
    sqlite3_free(zSql);
  }
  rc = sqlite3_exec(g.db, "SELECT 1 FROM sqlar LIMIT 1", 0, 0, 0);
  if( rc!=SQLITE_OK ){
    fprintf(stderr, "File [%s] is not an SQLite archive\n", argv[1]);
    exit(1);
  }
  g.uid = getuid();
  g.gid = getgid();
  azNewArg[0] = argv[0];
  azNewArg[1] = "-f";
  azNewArg[2] = "-s";
  azNewArg[3] = argv[2];
  azNewArg[3] = zMountPoint;
  azNewArg[4] = 0;
  rc = fuse_main(4, azNewArg, &sqlarfs_methods, NULL);
  sqlite3_finalize(g.pStat);
  sqlite3_finalize(g.pFList);
  sqlite3_finalize(g.pExists);
  sqlite3_finalize(g.pRead);
  sqlite3_free(g.zCacheName);
  sqlite3_free(g.zCacheData);
  sqlite3_close(g.db);
  return rc;
}