|
b4687be…
|
drh
|
1 |
/* |
|
c19f34c…
|
drh
|
2 |
** Copyright (c) 2010 D. Richard Hipp |
|
b4687be…
|
drh
|
3 |
** |
|
b4687be…
|
drh
|
4 |
** This program is free software; you can redistribute it and/or |
|
b4687be…
|
drh
|
5 |
** modify it under the terms of the Simplified BSD License (also |
|
b4687be…
|
drh
|
6 |
** known as the "2-Clause License" or "FreeBSD License".) |
|
b4687be…
|
drh
|
7 |
|
|
b4687be…
|
drh
|
8 |
** This program is distributed in the hope that it will be useful, |
|
b4687be…
|
drh
|
9 |
** but without any warranty; without even the implied warranty of |
|
b4687be…
|
drh
|
10 |
** merchantability or fitness for a particular purpose. |
|
b4687be…
|
drh
|
11 |
** |
|
b4687be…
|
drh
|
12 |
** Author contact information: |
|
b4687be…
|
drh
|
13 |
** [email protected] |
|
b4687be…
|
drh
|
14 |
** http://www.hwaci.com/drh/ |
|
b4687be…
|
drh
|
15 |
** |
|
b4687be…
|
drh
|
16 |
******************************************************************************* |
|
b4687be…
|
drh
|
17 |
** |
|
b4687be…
|
drh
|
18 |
** This module contains the code that initializes the "sqlite3" command-line |
|
b4687be…
|
drh
|
19 |
** shell against the repository database. The command-line shell itself |
|
b4687be…
|
drh
|
20 |
** is a copy of the "shell.c" code from SQLite. This file contains logic |
|
b4687be…
|
drh
|
21 |
** to initialize the code in shell.c. |
|
b4687be…
|
drh
|
22 |
*/ |
|
b4687be…
|
drh
|
23 |
#include "config.h" |
|
b4687be…
|
drh
|
24 |
#include "sqlcmd.h" |
|
97ff702…
|
mistachkin
|
25 |
#include <stdlib.h> /* atexit() */ |
|
f9c2d23…
|
stephan
|
26 |
#include <zlib.h> |
|
03b6869…
|
andygoth
|
27 |
#ifndef _WIN32 |
|
03b6869…
|
andygoth
|
28 |
# include "linenoise.h" |
|
66ae70a…
|
drh
|
29 |
#endif |
|
6ade354…
|
drh
|
30 |
|
|
6ade354…
|
drh
|
31 |
/* |
|
6ade354…
|
drh
|
32 |
** True if the "fossil sql" command has the --test flag. False otherwise. |
|
6ade354…
|
drh
|
33 |
*/ |
|
6ade354…
|
drh
|
34 |
static int local_bSqlCmdTest = 0; |
|
b4687be…
|
drh
|
35 |
|
|
b4687be…
|
drh
|
36 |
/* |
|
b4687be…
|
drh
|
37 |
** Implementation of the "content(X)" SQL function. Return the complete |
|
b4687be…
|
drh
|
38 |
** content of artifact identified by X as a blob. |
|
b4687be…
|
drh
|
39 |
*/ |
|
b4687be…
|
drh
|
40 |
static void sqlcmd_content( |
|
b4687be…
|
drh
|
41 |
sqlite3_context *context, |
|
b4687be…
|
drh
|
42 |
int argc, |
|
b4687be…
|
drh
|
43 |
sqlite3_value **argv |
|
b4687be…
|
drh
|
44 |
){ |
|
b4687be…
|
drh
|
45 |
int rid; |
|
b4687be…
|
drh
|
46 |
Blob cx; |
|
f105bc1…
|
drh
|
47 |
const char *zName; |
|
b4687be…
|
drh
|
48 |
assert( argc==1 ); |
|
f105bc1…
|
drh
|
49 |
zName = (const char*)sqlite3_value_text(argv[0]); |
|
b4687be…
|
drh
|
50 |
if( zName==0 ) return; |
|
63e6cb4…
|
drh
|
51 |
g.db = sqlite3_context_db_handle(context); |
|
63e6cb4…
|
drh
|
52 |
g.repositoryOpen = 1; |
|
b4687be…
|
drh
|
53 |
rid = name_to_rid(zName); |
|
b4687be…
|
drh
|
54 |
if( rid==0 ) return; |
|
b4687be…
|
drh
|
55 |
if( content_get(rid, &cx) ){ |
|
45f3516…
|
jan.nijtmans
|
56 |
sqlite3_result_blob(context, blob_buffer(&cx), blob_size(&cx), |
|
b4687be…
|
drh
|
57 |
SQLITE_TRANSIENT); |
|
b4687be…
|
drh
|
58 |
blob_reset(&cx); |
|
b4687be…
|
drh
|
59 |
} |
|
b4687be…
|
drh
|
60 |
} |
|
b4687be…
|
drh
|
61 |
|
|
b4687be…
|
drh
|
62 |
/* |
|
91d6484…
|
drh
|
63 |
** Implementation of the "compress(X)" SQL function. The input X is |
|
91d6484…
|
drh
|
64 |
** compressed using zLib and the output is returned. |
|
91d6484…
|
drh
|
65 |
*/ |
|
91d6484…
|
drh
|
66 |
static void sqlcmd_compress( |
|
91d6484…
|
drh
|
67 |
sqlite3_context *context, |
|
91d6484…
|
drh
|
68 |
int argc, |
|
91d6484…
|
drh
|
69 |
sqlite3_value **argv |
|
91d6484…
|
drh
|
70 |
){ |
|
91d6484…
|
drh
|
71 |
const unsigned char *pIn; |
|
91d6484…
|
drh
|
72 |
unsigned char *pOut; |
|
91d6484…
|
drh
|
73 |
unsigned int nIn; |
|
91d6484…
|
drh
|
74 |
unsigned long int nOut; |
|
abef6cf…
|
jan.nijtmans
|
75 |
int rc; |
|
91d6484…
|
drh
|
76 |
|
|
91d6484…
|
drh
|
77 |
pIn = sqlite3_value_blob(argv[0]); |
|
91d6484…
|
drh
|
78 |
nIn = sqlite3_value_bytes(argv[0]); |
|
91d6484…
|
drh
|
79 |
nOut = 13 + nIn + (nIn+999)/1000; |
|
91d6484…
|
drh
|
80 |
pOut = sqlite3_malloc( nOut+4 ); |
|
91d6484…
|
drh
|
81 |
pOut[0] = nIn>>24 & 0xff; |
|
91d6484…
|
drh
|
82 |
pOut[1] = nIn>>16 & 0xff; |
|
91d6484…
|
drh
|
83 |
pOut[2] = nIn>>8 & 0xff; |
|
91d6484…
|
drh
|
84 |
pOut[3] = nIn & 0xff; |
|
abef6cf…
|
jan.nijtmans
|
85 |
rc = compress(&pOut[4], &nOut, pIn, nIn); |
|
abef6cf…
|
jan.nijtmans
|
86 |
if( rc==Z_OK ){ |
|
abef6cf…
|
jan.nijtmans
|
87 |
sqlite3_result_blob(context, pOut, nOut+4, sqlite3_free); |
|
20abe25…
|
stephan
|
88 |
}else if( rc==Z_MEM_ERROR ){ |
|
20abe25…
|
stephan
|
89 |
sqlite3_free(pOut); |
|
20abe25…
|
stephan
|
90 |
sqlite3_result_error_nomem(context); |
|
abef6cf…
|
jan.nijtmans
|
91 |
}else{ |
|
abef6cf…
|
jan.nijtmans
|
92 |
sqlite3_free(pOut); |
|
abef6cf…
|
jan.nijtmans
|
93 |
sqlite3_result_error(context, "input cannot be zlib compressed", -1); |
|
abef6cf…
|
jan.nijtmans
|
94 |
} |
|
91d6484…
|
drh
|
95 |
} |
|
91d6484…
|
drh
|
96 |
|
|
91d6484…
|
drh
|
97 |
/* |
|
648e74d…
|
stephan
|
98 |
** Implementation of the "decompress(X)" SQL function. The argument X |
|
91d6484…
|
drh
|
99 |
** is a blob which was obtained from compress(Y). The output will be |
|
91d6484…
|
drh
|
100 |
** the value Y. |
|
91d6484…
|
drh
|
101 |
*/ |
|
91d6484…
|
drh
|
102 |
static void sqlcmd_decompress( |
|
91d6484…
|
drh
|
103 |
sqlite3_context *context, |
|
91d6484…
|
drh
|
104 |
int argc, |
|
91d6484…
|
drh
|
105 |
sqlite3_value **argv |
|
91d6484…
|
drh
|
106 |
){ |
|
91d6484…
|
drh
|
107 |
const unsigned char *pIn; |
|
91d6484…
|
drh
|
108 |
unsigned char *pOut; |
|
91d6484…
|
drh
|
109 |
unsigned int nIn; |
|
91d6484…
|
drh
|
110 |
unsigned long int nOut; |
|
91d6484…
|
drh
|
111 |
int rc; |
|
91d6484…
|
drh
|
112 |
|
|
91d6484…
|
drh
|
113 |
pIn = sqlite3_value_blob(argv[0]); |
|
449ab5d…
|
drh
|
114 |
if( pIn==0 ) return; |
|
91d6484…
|
drh
|
115 |
nIn = sqlite3_value_bytes(argv[0]); |
|
449ab5d…
|
drh
|
116 |
if( nIn<4 ) return; |
|
91d6484…
|
drh
|
117 |
nOut = (pIn[0]<<24) + (pIn[1]<<16) + (pIn[2]<<8) + pIn[3]; |
|
91d6484…
|
drh
|
118 |
pOut = sqlite3_malloc( nOut+1 ); |
|
91d6484…
|
drh
|
119 |
rc = uncompress(pOut, &nOut, &pIn[4], nIn-4); |
|
91d6484…
|
drh
|
120 |
if( rc==Z_OK ){ |
|
91d6484…
|
drh
|
121 |
sqlite3_result_blob(context, pOut, nOut, sqlite3_free); |
|
20abe25…
|
stephan
|
122 |
}else if( rc==Z_MEM_ERROR ){ |
|
20abe25…
|
stephan
|
123 |
sqlite3_free(pOut); |
|
20abe25…
|
stephan
|
124 |
sqlite3_result_error_nomem(context); |
|
91d6484…
|
drh
|
125 |
}else{ |
|
abef6cf…
|
jan.nijtmans
|
126 |
sqlite3_free(pOut); |
|
91d6484…
|
drh
|
127 |
sqlite3_result_error(context, "input is not zlib compressed", -1); |
|
91d6484…
|
drh
|
128 |
} |
|
a81a47f…
|
drh
|
129 |
} |
|
a81a47f…
|
drh
|
130 |
|
|
a81a47f…
|
drh
|
131 |
/* |
|
6e890f7…
|
drh
|
132 |
** Implementation of the "gather_artifact_stats(X)" SQL function. |
|
6e890f7…
|
drh
|
133 |
** That function merely calls the gather_artifact_stats() function |
|
6e890f7…
|
drh
|
134 |
** in stat.c to populate the ARTSTAT temporary table. |
|
6e890f7…
|
drh
|
135 |
*/ |
|
6e890f7…
|
drh
|
136 |
static void sqlcmd_gather_artifact_stats( |
|
6e890f7…
|
drh
|
137 |
sqlite3_context *context, |
|
6e890f7…
|
drh
|
138 |
int argc, |
|
6e890f7…
|
drh
|
139 |
sqlite3_value **argv |
|
6e890f7…
|
drh
|
140 |
){ |
|
6e890f7…
|
drh
|
141 |
gather_artifact_stats(1); |
|
6e890f7…
|
drh
|
142 |
} |
|
6e890f7…
|
drh
|
143 |
|
|
6e890f7…
|
drh
|
144 |
/* |
|
d0a8582…
|
stephan
|
145 |
** Add the content(), compress(), decompress(), and |
|
d0a8582…
|
stephan
|
146 |
** gather_artifact_stats() SQL functions to database connection db. |
|
9d50322…
|
drh
|
147 |
** |
|
9d50322…
|
drh
|
148 |
** Also add whatis(). |
|
a81a47f…
|
drh
|
149 |
*/ |
|
a81a47f…
|
drh
|
150 |
int add_content_sql_commands(sqlite3 *db){ |
|
a81a47f…
|
drh
|
151 |
sqlite3_create_function(db, "content", 1, SQLITE_UTF8, 0, |
|
a81a47f…
|
drh
|
152 |
sqlcmd_content, 0, 0); |
|
a81a47f…
|
drh
|
153 |
sqlite3_create_function(db, "compress", 1, SQLITE_UTF8, 0, |
|
a81a47f…
|
drh
|
154 |
sqlcmd_compress, 0, 0); |
|
a81a47f…
|
drh
|
155 |
sqlite3_create_function(db, "decompress", 1, SQLITE_UTF8, 0, |
|
a81a47f…
|
drh
|
156 |
sqlcmd_decompress, 0, 0); |
|
6e890f7…
|
drh
|
157 |
sqlite3_create_function(db, "gather_artifact_stats", 0, SQLITE_UTF8, 0, |
|
6e890f7…
|
drh
|
158 |
sqlcmd_gather_artifact_stats, 0, 0); |
|
9d50322…
|
drh
|
159 |
sqlite3_create_function(db, "whatis", 1, SQLITE_UTF8|SQLITE_RESULT_SUBTYPE, 0, |
|
9d50322…
|
drh
|
160 |
whatis_sql_function, 0, 0); |
|
a81a47f…
|
drh
|
161 |
return SQLITE_OK; |
|
f741baa…
|
drh
|
162 |
} |
|
f741baa…
|
drh
|
163 |
|
|
f741baa…
|
drh
|
164 |
/* |
|
f741baa…
|
drh
|
165 |
** Undocumented test SQL functions: |
|
f741baa…
|
drh
|
166 |
** |
|
f741baa…
|
drh
|
167 |
** db_protect(X) |
|
f741baa…
|
drh
|
168 |
** db_protect_pop(X) |
|
f741baa…
|
drh
|
169 |
** |
|
6ade354…
|
drh
|
170 |
** These invoke the corresponding C routines. |
|
6ade354…
|
drh
|
171 |
** |
|
6ade354…
|
drh
|
172 |
** WARNING: |
|
6ade354…
|
drh
|
173 |
** Do not instantiate these functions for any Fossil webpage or command |
|
d0a8582…
|
stephan
|
174 |
** method other than the "fossil sql" command. If an attacker gains access |
|
6ade354…
|
drh
|
175 |
** to these functions, he will be able to disable other defense mechanisms. |
|
6ade354…
|
drh
|
176 |
** |
|
e2bdc10…
|
danield
|
177 |
** This routines are for interactive testing only. They are experimental |
|
6ade354…
|
drh
|
178 |
** and undocumented (apart from this comments) and might go away or change |
|
6ade354…
|
drh
|
179 |
** in future releases. |
|
6ade354…
|
drh
|
180 |
** |
|
d0a8582…
|
stephan
|
181 |
** 2020-11-29: These functions are now only available if the "fossil sql" |
|
6ade354…
|
drh
|
182 |
** command is started with the --test option. |
|
f741baa…
|
drh
|
183 |
*/ |
|
f741baa…
|
drh
|
184 |
static void sqlcmd_db_protect( |
|
f741baa…
|
drh
|
185 |
sqlite3_context *context, |
|
f741baa…
|
drh
|
186 |
int argc, |
|
f741baa…
|
drh
|
187 |
sqlite3_value **argv |
|
f741baa…
|
drh
|
188 |
){ |
|
f741baa…
|
drh
|
189 |
unsigned mask = 0; |
|
f741baa…
|
drh
|
190 |
const char *z = (const char*)sqlite3_value_text(argv[0]); |
|
6ade354…
|
drh
|
191 |
if( z!=0 && local_bSqlCmdTest ){ |
|
6ade354…
|
drh
|
192 |
if( sqlite3_stricmp(z,"user")==0 ) mask |= PROTECT_USER; |
|
6ade354…
|
drh
|
193 |
if( sqlite3_stricmp(z,"config")==0 ) mask |= PROTECT_CONFIG; |
|
6ade354…
|
drh
|
194 |
if( sqlite3_stricmp(z,"sensitive")==0 ) mask |= PROTECT_SENSITIVE; |
|
6ade354…
|
drh
|
195 |
if( sqlite3_stricmp(z,"readonly")==0 ) mask |= PROTECT_READONLY; |
|
6ade354…
|
drh
|
196 |
if( sqlite3_stricmp(z,"all")==0 ) mask |= PROTECT_ALL; |
|
6ade354…
|
drh
|
197 |
db_protect(mask); |
|
6ade354…
|
drh
|
198 |
} |
|
f741baa…
|
drh
|
199 |
} |
|
f741baa…
|
drh
|
200 |
static void sqlcmd_db_protect_pop( |
|
f741baa…
|
drh
|
201 |
sqlite3_context *context, |
|
f741baa…
|
drh
|
202 |
int argc, |
|
f741baa…
|
drh
|
203 |
sqlite3_value **argv |
|
f741baa…
|
drh
|
204 |
){ |
|
6ade354…
|
drh
|
205 |
if( !local_bSqlCmdTest ) db_protect_pop(); |
|
db0c512…
|
drh
|
206 |
} |
|
db0c512…
|
drh
|
207 |
|
|
db0c512…
|
drh
|
208 |
/* |
|
db0c512…
|
drh
|
209 |
** This is the "automatic extension" initializer that runs right after |
|
b4687be…
|
drh
|
210 |
** the connection to the repository database is opened. Set up the |
|
b4687be…
|
drh
|
211 |
** database connection to be more useful to the human operator. |
|
b4687be…
|
drh
|
212 |
*/ |
|
b4687be…
|
drh
|
213 |
static int sqlcmd_autoinit( |
|
b4687be…
|
drh
|
214 |
sqlite3 *db, |
|
b4687be…
|
drh
|
215 |
const char **pzErrMsg, |
|
b4687be…
|
drh
|
216 |
const void *notUsed |
|
b4687be…
|
drh
|
217 |
){ |
|
fbb15cc…
|
drh
|
218 |
int mTrace = SQLITE_TRACE_CLOSE; |
|
a81a47f…
|
drh
|
219 |
add_content_sql_commands(db); |
|
ba1429c…
|
drh
|
220 |
db_add_aux_functions(db); |
|
e2552de…
|
drh
|
221 |
re_add_sql_func(db); |
|
046d743…
|
drh
|
222 |
search_sql_setup(db); |
|
7a588fe…
|
drh
|
223 |
foci_register(db); |
|
effa893…
|
drh
|
224 |
deltafunc_init(db); |
|
b2dacfc…
|
drh
|
225 |
helptext_vtab_register(db); |
|
c6359b4…
|
drh
|
226 |
builtin_vtab_register(db); |
|
16ab9f2…
|
drh
|
227 |
g.repositoryOpen = 1; |
|
16ab9f2…
|
drh
|
228 |
g.db = db; |
|
49df85a…
|
drh
|
229 |
sqlite3_busy_timeout(db, 10000); |
|
73e3217…
|
drh
|
230 |
if( g.zRepositoryName ){ |
|
73e3217…
|
drh
|
231 |
sqlite3_db_config(db, SQLITE_DBCONFIG_MAINDBNAME, "repository"); |
|
73e3217…
|
drh
|
232 |
db_maybe_set_encryption_key(db, g.zRepositoryName); |
|
73e3217…
|
drh
|
233 |
} |
|
0708155…
|
drh
|
234 |
if( g.zLocalDbName ){ |
|
00dfbdb…
|
mistachkin
|
235 |
char *zSql = sqlite3_mprintf("ATTACH %Q AS 'localdb' KEY ''", |
|
00dfbdb…
|
mistachkin
|
236 |
g.zLocalDbName); |
|
0708155…
|
drh
|
237 |
sqlite3_exec(db, zSql, 0, 0, 0); |
|
0708155…
|
drh
|
238 |
sqlite3_free(zSql); |
|
0708155…
|
drh
|
239 |
} |
|
0708155…
|
drh
|
240 |
if( g.zConfigDbName ){ |
|
00dfbdb…
|
mistachkin
|
241 |
char *zSql = sqlite3_mprintf("ATTACH %Q AS 'configdb' KEY ''", |
|
00dfbdb…
|
mistachkin
|
242 |
g.zConfigDbName); |
|
0708155…
|
drh
|
243 |
sqlite3_exec(db, zSql, 0, 0, 0); |
|
0708155…
|
drh
|
244 |
sqlite3_free(zSql); |
|
fbb15cc…
|
drh
|
245 |
} |
|
6ab9994…
|
drh
|
246 |
(void)timeline_query_for_tty(); /* Registers wiki_to_text() as side-effect */ |
|
fbb15cc…
|
drh
|
247 |
/* Arrange to trace close operations so that static prepared statements |
|
fbb15cc…
|
drh
|
248 |
** will get cleaned up when the shell closes the database connection */ |
|
fbb15cc…
|
drh
|
249 |
if( g.fSqlTrace ) mTrace |= SQLITE_TRACE_PROFILE; |
|
fbb15cc…
|
drh
|
250 |
sqlite3_trace_v2(db, mTrace, db_sql_trace, 0); |
|
73e3217…
|
drh
|
251 |
if( g.zRepositoryName ){ |
|
73e3217…
|
drh
|
252 |
db_protect_only(PROTECT_NONE); |
|
73e3217…
|
drh
|
253 |
sqlite3_set_authorizer(db, db_top_authorizer, db); |
|
73e3217…
|
drh
|
254 |
if( local_bSqlCmdTest ){ |
|
73e3217…
|
drh
|
255 |
sqlite3_create_function(db, "db_protect", 1, SQLITE_UTF8, 0, |
|
73e3217…
|
drh
|
256 |
sqlcmd_db_protect, 0, 0); |
|
73e3217…
|
drh
|
257 |
sqlite3_create_function(db, "db_protect_pop", 0, SQLITE_UTF8, 0, |
|
73e3217…
|
drh
|
258 |
sqlcmd_db_protect_pop, 0, 0); |
|
73e3217…
|
drh
|
259 |
sqlite3_create_function(db, "shared_secret", 2, SQLITE_UTF8, 0, |
|
73e3217…
|
drh
|
260 |
sha1_shared_secret_sql_function, 0, 0); |
|
73e3217…
|
drh
|
261 |
} |
|
6ade354…
|
drh
|
262 |
} |
|
a81a47f…
|
drh
|
263 |
return SQLITE_OK; |
|
a81a47f…
|
drh
|
264 |
} |
|
a81a47f…
|
drh
|
265 |
|
|
a81a47f…
|
drh
|
266 |
/* |
|
97ff702…
|
mistachkin
|
267 |
** atexit() handler that cleans up global state modified by this module. |
|
97ff702…
|
mistachkin
|
268 |
*/ |
|
97ff702…
|
mistachkin
|
269 |
static void sqlcmd_atexit(void) { |
|
97ff702…
|
mistachkin
|
270 |
g.zConfigDbName = 0; /* prevent panic */ |
|
97ff702…
|
mistachkin
|
271 |
} |
|
97ff702…
|
mistachkin
|
272 |
|
|
97ff702…
|
mistachkin
|
273 |
/* |
|
de76c92…
|
drh
|
274 |
** This routine is called by the sqlite3 command-line shell to |
|
de76c92…
|
drh
|
275 |
** to load the name the Fossil repository database. |
|
1c6a6fa…
|
mistachkin
|
276 |
*/ |
|
de76c92…
|
drh
|
277 |
void sqlcmd_get_dbname(const char **pzRepoName){ |
|
1c6a6fa…
|
mistachkin
|
278 |
*pzRepoName = g.zRepositoryName; |
|
de76c92…
|
drh
|
279 |
} |
|
de76c92…
|
drh
|
280 |
|
|
de76c92…
|
drh
|
281 |
/* |
|
de76c92…
|
drh
|
282 |
** This routine is called by the sqlite3 command-line shell to do |
|
de76c92…
|
drh
|
283 |
** extra initialization prior to starting up the shell. |
|
de76c92…
|
drh
|
284 |
*/ |
|
de76c92…
|
drh
|
285 |
void sqlcmd_init_proc(void){ |
|
de76c92…
|
drh
|
286 |
sqlite3_initialize(); |
|
de76c92…
|
drh
|
287 |
sqlite3_auto_extension((void(*)(void))sqlcmd_autoinit); |
|
00dfbdb…
|
mistachkin
|
288 |
} |
|
00dfbdb…
|
mistachkin
|
289 |
|
|
00dfbdb…
|
mistachkin
|
290 |
#if USE_SEE |
|
00dfbdb…
|
mistachkin
|
291 |
/* |
|
00dfbdb…
|
mistachkin
|
292 |
** This routine is called by the patched sqlite3 command-line shell in order |
|
00dfbdb…
|
mistachkin
|
293 |
** to load the encryption key for the open Fossil database. The memory that |
|
3b4a455…
|
mistachkin
|
294 |
** is pointed to by the value placed in pzKey must be obtained from malloc. |
|
00dfbdb…
|
mistachkin
|
295 |
*/ |
|
00dfbdb…
|
mistachkin
|
296 |
void fossil_key(const char **pzKey, int *pnKey){ |
|
00dfbdb…
|
mistachkin
|
297 |
char *zSavedKey = db_get_saved_encryption_key(); |
|
00dfbdb…
|
mistachkin
|
298 |
char *zKey; |
|
00dfbdb…
|
mistachkin
|
299 |
size_t savedKeySize = db_get_saved_encryption_key_size(); |
|
00dfbdb…
|
mistachkin
|
300 |
|
|
f41cf03…
|
mistachkin
|
301 |
if( !db_is_valid_saved_encryption_key(zSavedKey, savedKeySize) ) return; |
|
5314349…
|
mistachkin
|
302 |
zKey = (char*)malloc( savedKeySize ); |
|
00dfbdb…
|
mistachkin
|
303 |
if( zKey ){ |
|
5314349…
|
mistachkin
|
304 |
memcpy(zKey, zSavedKey, savedKeySize); |
|
00dfbdb…
|
mistachkin
|
305 |
*pzKey = zKey; |
|
00dfbdb…
|
mistachkin
|
306 |
if( fossil_getenv("FOSSIL_USE_SEE_TEXTKEY")==0 ){ |
|
00dfbdb…
|
mistachkin
|
307 |
*pnKey = (int)strlen(zKey); |
|
00dfbdb…
|
mistachkin
|
308 |
}else{ |
|
00dfbdb…
|
mistachkin
|
309 |
*pnKey = -1; |
|
00dfbdb…
|
mistachkin
|
310 |
} |
|
00dfbdb…
|
mistachkin
|
311 |
}else{ |
|
5314349…
|
mistachkin
|
312 |
fossil_fatal("failed to allocate %u bytes for key", savedKeySize); |
|
00dfbdb…
|
mistachkin
|
313 |
} |
|
00dfbdb…
|
mistachkin
|
314 |
} |
|
00dfbdb…
|
mistachkin
|
315 |
#endif |
|
f9c14d7…
|
mistachkin
|
316 |
|
|
f9c14d7…
|
mistachkin
|
317 |
/* |
|
f9c14d7…
|
mistachkin
|
318 |
** This routine closes the Fossil databases and/or invalidates the global |
|
f9c14d7…
|
mistachkin
|
319 |
** state variables that keep track of them. |
|
d68cd57…
|
jan.nijtmans
|
320 |
*/ |
|
1c6a6fa…
|
mistachkin
|
321 |
static void fossil_close(int bDb, int noRepository){ |
|
f9c14d7…
|
mistachkin
|
322 |
if( bDb ) db_close(1); |
|
f9c14d7…
|
mistachkin
|
323 |
if( noRepository ) g.zRepositoryName = 0; |
|
d68cd57…
|
jan.nijtmans
|
324 |
g.db = 0; |
|
b8b0376…
|
mistachkin
|
325 |
g.repositoryOpen = 0; |
|
b8b0376…
|
mistachkin
|
326 |
g.localOpen = 0; |
|
73e3217…
|
drh
|
327 |
} |
|
73e3217…
|
drh
|
328 |
|
|
73e3217…
|
drh
|
329 |
/* |
|
87881e8…
|
drh
|
330 |
** This routine overrides some of the prompt generation behavior in the |
|
87881e8…
|
drh
|
331 |
** SQLite shell. Always return a static string. For invalid inputs, |
|
87881e8…
|
drh
|
332 |
** return a zero-length string. Valid inputs: |
|
87881e8…
|
drh
|
333 |
** |
|
87881e8…
|
drh
|
334 |
** 1 Default main prompt. |
|
87881e8…
|
drh
|
335 |
** 2 Default continuation prompt. |
|
87881e8…
|
drh
|
336 |
** 3 Environment variable to override main prompt ("FOSSIL_PS1") |
|
87881e8…
|
drh
|
337 |
** 4 Environment variable to override continuatio ("FOSSIL_PS2") |
|
87881e8…
|
drh
|
338 |
** 'A' The name of the application. (Nominally "Fossil") |
|
87881e8…
|
drh
|
339 |
** 'V' Version number including patch. (ex: "2.29.1") |
|
87881e8…
|
drh
|
340 |
** 'v' Version number without patch. (ex: "2.29") |
|
9b67f46…
|
drh
|
341 |
*/ |
|
9b67f46…
|
drh
|
342 |
const char *sqlcmd_ps_appdef(int c){ |
|
87881e8…
|
drh
|
343 |
switch( c ){ |
|
87881e8…
|
drh
|
344 |
/* Default prompt strings */ |
|
5c3150b…
|
drh
|
345 |
case 1: return "/e[1;34m/A-/v /e[1;/x33/:36/;m/f/e[0m-> "; |
|
5c3150b…
|
drh
|
346 |
case 2: return "/B/e[1;/x33/:36/;m/C/e[0m-> "; |
|
87881e8…
|
drh
|
347 |
|
|
87881e8…
|
drh
|
348 |
/* Names of environment variables to override cases 1 and 2 */ |
|
87881e8…
|
drh
|
349 |
case 3: return "FOSSIL_PS1"; |
|
87881e8…
|
drh
|
350 |
case 4: return "FOSSIL_PS2"; |
|
87881e8…
|
drh
|
351 |
|
|
87881e8…
|
drh
|
352 |
/* Application name */ |
|
87881e8…
|
drh
|
353 |
case 'A': return "Fossil"; |
|
87881e8…
|
drh
|
354 |
|
|
87881e8…
|
drh
|
355 |
/* Version numbers */ |
|
87881e8…
|
drh
|
356 |
case 'V': return RELEASE_VERSION; |
|
87881e8…
|
drh
|
357 |
case 'v': { |
|
87881e8…
|
drh
|
358 |
const char *zFull = RELEASE_VERSION; |
|
87881e8…
|
drh
|
359 |
const char *zD1, *zD2; |
|
87881e8…
|
drh
|
360 |
size_t i; |
|
87881e8…
|
drh
|
361 |
static char zRelease[32]; |
|
87881e8…
|
drh
|
362 |
zD2 = strrchr(zFull,'.'); |
|
87881e8…
|
drh
|
363 |
zD1 = strchr(zFull,'.'); |
|
87881e8…
|
drh
|
364 |
if( zD2==0 || zD2==zD1 ){ |
|
87881e8…
|
drh
|
365 |
return zFull; |
|
87881e8…
|
drh
|
366 |
} |
|
87881e8…
|
drh
|
367 |
i = zD2 - zFull; |
|
87881e8…
|
drh
|
368 |
if( i>sizeof(zRelease)-1 ) return zFull; |
|
87881e8…
|
drh
|
369 |
memcpy(zRelease, zFull, i); |
|
87881e8…
|
drh
|
370 |
zRelease[i] = 0; |
|
87881e8…
|
drh
|
371 |
return zRelease; |
|
87881e8…
|
drh
|
372 |
} |
|
9b67f46…
|
drh
|
373 |
} |
|
9b67f46…
|
drh
|
374 |
return ""; |
|
9b67f46…
|
drh
|
375 |
} |
|
9b67f46…
|
drh
|
376 |
|
|
9b67f46…
|
drh
|
377 |
/* |
|
e02f4bf…
|
drh
|
378 |
** COMMAND: sql |
|
e02f4bf…
|
drh
|
379 |
** COMMAND: sqlite3* |
|
1c6a6fa…
|
mistachkin
|
380 |
** |
|
1c6a6fa…
|
mistachkin
|
381 |
** Usage: %fossil sql ?OPTIONS? |
|
1c6a6fa…
|
mistachkin
|
382 |
** |
|
a370043…
|
drh
|
383 |
** Run the sqlite3 command-line shell on the Fossil repository |
|
a370043…
|
drh
|
384 |
** identified by the -R option, or on the current repository. |
|
a370043…
|
drh
|
385 |
** See https://www.sqlite.org/cli.html for additional information about |
|
a370043…
|
drh
|
386 |
** the sqlite3 command-line shell. |
|
1c6a6fa…
|
mistachkin
|
387 |
** |
|
1c6a6fa…
|
mistachkin
|
388 |
** WARNING: Careless use of this command can corrupt a Fossil repository |
|
1c6a6fa…
|
mistachkin
|
389 |
** in ways that are unrecoverable. Be sure you know what you are doing before |
|
a370043…
|
drh
|
390 |
** running any SQL commands that modify the repository database. Use the |
|
a370043…
|
drh
|
391 |
** --readonly option to prevent accidental damage to the repository. |
|
a370043…
|
drh
|
392 |
** |
|
a370043…
|
drh
|
393 |
** Options: |
|
2e56ef4…
|
km
|
394 |
** --no-repository Skip opening the repository database |
|
a370043…
|
drh
|
395 |
** --readonly Open the repository read-only. No changes |
|
a370043…
|
drh
|
396 |
** are allowed. This is a recommended safety |
|
a370043…
|
drh
|
397 |
** precaution to prevent repository damage. |
|
a370043…
|
drh
|
398 |
** -R REPOSITORY Use REPOSITORY as the repository database |
|
6ade354…
|
drh
|
399 |
** --test Enable some testing and analysis features |
|
6ade354…
|
drh
|
400 |
** that are normally disabled. |
|
a370043…
|
drh
|
401 |
** |
|
a370043…
|
drh
|
402 |
** All of the standard sqlite3 command-line shell options should also |
|
a370043…
|
drh
|
403 |
** work. |
|
a370043…
|
drh
|
404 |
** |
|
a370043…
|
drh
|
405 |
** The following SQL extensions are provided with this Fossil-enhanced |
|
a370043…
|
drh
|
406 |
** version of the sqlite3 command-line shell: |
|
c6359b4…
|
drh
|
407 |
** |
|
c6359b4…
|
drh
|
408 |
** builtin A virtual table that contains one row for |
|
c6359b4…
|
drh
|
409 |
** each datafile that is built into the Fossil |
|
c6359b4…
|
drh
|
410 |
** binary. |
|
1c6a6fa…
|
mistachkin
|
411 |
** |
|
1c6a6fa…
|
mistachkin
|
412 |
** checkin_mtime(X,Y) Return the mtime for the file Y (a BLOB.RID) |
|
1c6a6fa…
|
mistachkin
|
413 |
** found in check-in X (another BLOB.RID value). |
|
1c6a6fa…
|
mistachkin
|
414 |
** |
|
a370043…
|
drh
|
415 |
** compress(X) Compress text X with the same algorithm used |
|
a370043…
|
drh
|
416 |
** to compress artifacts in the BLOB table. |
|
effa893…
|
drh
|
417 |
** |
|
effa893…
|
drh
|
418 |
** content(X) Return the content of artifact X. X can be an |
|
effa893…
|
drh
|
419 |
** artifact hash or hash prefix or a tag. Artifacts |
|
effa893…
|
drh
|
420 |
** are stored compressed and deltaed. This function |
|
effa893…
|
drh
|
421 |
** does all necessary decompression and undeltaing. |
|
effa893…
|
drh
|
422 |
** |
|
effa893…
|
drh
|
423 |
** decompress(X) Decompress text X. Undoes the work of |
|
effa893…
|
drh
|
424 |
** compress(X). |
|
effa893…
|
drh
|
425 |
** |
|
effa893…
|
drh
|
426 |
** delta_apply(X,D) Apply delta D to source blob X and return |
|
effa893…
|
drh
|
427 |
** the result. |
|
effa893…
|
drh
|
428 |
** |
|
effa893…
|
drh
|
429 |
** delta_create(X,Y) Create and return a delta that will convert |
|
effa893…
|
drh
|
430 |
** X into Y. |
|
1c6a6fa…
|
mistachkin
|
431 |
** |
|
effa893…
|
drh
|
432 |
** delta_output_size(D) Return the number of bytes of output to expect |
|
effa893…
|
drh
|
433 |
** when applying delta D |
|
1c6a6fa…
|
mistachkin
|
434 |
** |
|
effa893…
|
drh
|
435 |
** delta_parse(D) A table-valued function that deconstructs |
|
effa893…
|
drh
|
436 |
** delta D and returns rows for each element of |
|
effa893…
|
drh
|
437 |
** that delta. |
|
1c6a6fa…
|
mistachkin
|
438 |
** |
|
1c6a6fa…
|
mistachkin
|
439 |
** files_of_checkin(X) A table-valued function that returns info on |
|
1c6a6fa…
|
mistachkin
|
440 |
** all files contained in check-in X. Example: |
|
a370043…
|
drh
|
441 |
** |
|
a370043…
|
drh
|
442 |
** SELECT * FROM files_of_checkin('trunk'); |
|
a370043…
|
drh
|
443 |
** |
|
c6359b4…
|
drh
|
444 |
** helptext A virtual table with one row for each command, |
|
c6359b4…
|
drh
|
445 |
** webpage, and setting together with the built-in |
|
275da70…
|
danield
|
446 |
** help text. |
|
c6359b4…
|
drh
|
447 |
** |
|
effa893…
|
drh
|
448 |
** now() Return the number of seconds since 1970. |
|
effa893…
|
drh
|
449 |
** |
|
a370043…
|
drh
|
450 |
** obscure(T) Obfuscate the text password T so that its |
|
a370043…
|
drh
|
451 |
** original value is not readily visible. Fossil |
|
a370043…
|
drh
|
452 |
** uses this same algorithm when storing passwords |
|
a370043…
|
drh
|
453 |
** of remote URLs. |
|
a370043…
|
drh
|
454 |
** |
|
a370043…
|
drh
|
455 |
** regexp The REGEXP operator works, unlike in |
|
effa893…
|
drh
|
456 |
** standard SQLite. |
|
effa893…
|
drh
|
457 |
** |
|
effa893…
|
drh
|
458 |
** symbolic_name_to_rid(X) Return the BLOB.RID corresponding to symbolic |
|
effa893…
|
drh
|
459 |
** name X. |
|
1c6a6fa…
|
mistachkin
|
460 |
*/ |
|
1c6a6fa…
|
mistachkin
|
461 |
void cmd_sqlite3(void){ |
|
1c6a6fa…
|
mistachkin
|
462 |
int noRepository; |
|
79988f9…
|
drh
|
463 |
char *zConfigDb; |
|
1c6a6fa…
|
mistachkin
|
464 |
extern int sqlite3_shell(int, char**); |
|
1c6a6fa…
|
mistachkin
|
465 |
#ifdef FOSSIL_ENABLE_TH1_HOOKS |
|
1c6a6fa…
|
mistachkin
|
466 |
g.fNoThHook = 1; |
|
1c6a6fa…
|
mistachkin
|
467 |
#endif |
|
1c6a6fa…
|
mistachkin
|
468 |
noRepository = find_option("no-repository", 0, 0)!=0; |
|
6ade354…
|
drh
|
469 |
local_bSqlCmdTest = find_option("test",0,0)!=0; |
|
1c6a6fa…
|
mistachkin
|
470 |
if( !noRepository ){ |
|
1c6a6fa…
|
mistachkin
|
471 |
db_find_and_open_repository(OPEN_ANY_SCHEMA, 0); |
|
1c6a6fa…
|
mistachkin
|
472 |
} |
|
1c6a6fa…
|
mistachkin
|
473 |
db_open_config(1,0); |
|
79988f9…
|
drh
|
474 |
zConfigDb = fossil_strdup(g.zConfigDbName); |
|
1c6a6fa…
|
mistachkin
|
475 |
fossil_close(1, noRepository); |
|
1c6a6fa…
|
mistachkin
|
476 |
sqlite3_shutdown(); |
|
ddba8ea…
|
wyoung
|
477 |
#if !defined(_WIN32) && !defined(BROKEN_MINGW_CMDLINE) |
|
1c6a6fa…
|
mistachkin
|
478 |
linenoiseSetMultiLine(1); |
|
1c6a6fa…
|
mistachkin
|
479 |
#endif |
|
1c6a6fa…
|
mistachkin
|
480 |
atexit(sqlcmd_atexit); |
|
1c6a6fa…
|
mistachkin
|
481 |
g.zConfigDbName = zConfigDb; |
|
4f76459…
|
drh
|
482 |
g.argv[1] = "--noinit"; |
|
953aa7b…
|
drh
|
483 |
sqlite3_shell(g.argc, g.argv); |
|
1c6a6fa…
|
mistachkin
|
484 |
sqlite3_cancel_auto_extension((void(*)(void))sqlcmd_autoinit); |
|
1c6a6fa…
|
mistachkin
|
485 |
fossil_close(0, noRepository); |
|
d68cd57…
|
jan.nijtmans
|
486 |
} |