| ︙ | | | ︙ | |
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
|
#include "config.h"
#include "main.h"
#include <string.h>
#include <time.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#if INTERFACE
/*
** Number of elements in an array
*/
#define count(X) (sizeof(X)/sizeof(X[0]))
/*
|
|
>
|
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
|
#include "config.h"
#include "main.h"
#include <string.h>
#include <time.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <stdlib.h> /* atexit() */
#if INTERFACE
#include "cson_amalgamation.h" /* JSON API */
/*
** Number of elements in an array
*/
#define count(X) (sizeof(X)/sizeof(X[0]))
/*
|
| ︙ | | | ︙ | |
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
|
FILE *httpIn; /* Accept HTTP input from here */
FILE *httpOut; /* Send HTTP output here */
int xlinkClusterOnly; /* Set when cloning. Only process clusters */
int fTimeFormat; /* 1 for UTC. 2 for localtime. 0 not yet selected */
int *aCommitFile; /* Array of files to be committed */
int markPrivate; /* All new artifacts are private if true */
int clockSkewSeen; /* True if clocks on client and server out of sync */
int urlIsFile; /* True if a "file:" url */
int urlIsHttps; /* True if a "https:" url */
int urlIsSsh; /* True if an "ssh:" url */
char *urlName; /* Hostname for http: or filename for file: */
char *urlHostname; /* The HOST: parameter on http headers */
char *urlProtocol; /* "http" or "https" */
int urlPort; /* TCP port number for http: or https: */
int urlDfltPort; /* The default port for the given protocol */
char *urlPath; /* Pathname for http: */
char *urlUser; /* User id for http: */
char *urlPasswd; /* Password for http: */
char *urlCanonical; /* Canonical representation of the URL */
char *urlProxyAuth; /* Proxy-Authorizer: string */
char *urlFossil; /* The path of the ?fossil=path suffix on ssh: */
int dontKeepUrl; /* Do not persist the URL */
const char *zLogin; /* Login name. "" if not logged in. */
const char *zSSLIdentity; /* Value of --ssl-identity option, filename of SSL client identity */
int useLocalauth; /* No login required if from 127.0.0.1 */
int noPswd; /* Logged in without password (on 127.0.0.1) */
int userUid; /* Integer user id */
/* Information used to populate the RCVFROM table */
|
>
|
|
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
|
FILE *httpIn; /* Accept HTTP input from here */
FILE *httpOut; /* Send HTTP output here */
int xlinkClusterOnly; /* Set when cloning. Only process clusters */
int fTimeFormat; /* 1 for UTC. 2 for localtime. 0 not yet selected */
int *aCommitFile; /* Array of files to be committed */
int markPrivate; /* All new artifacts are private if true */
int clockSkewSeen; /* True if clocks on client and server out of sync */
int isCGI; /* True if running in HTTP/CGI mode, else assume CLI. */
int urlIsFile; /* True if a "file:" url */
int urlIsHttps; /* True if a "https:" url */
int urlIsSsh; /* True if an "ssh:" url */
char *urlName; /* Hostname for http: or filename for file: */
char *urlHostname; /* The HOST: parameter on http headers */
char *urlProtocol; /* "http" or "https" */
int urlPort; /* TCP port number for http: or https: */
int urlDfltPort; /* The default port for the given protocol */
char *urlPath; /* Pathname for http: */
char *urlUser; /* User id for http: */
char *urlPasswd; /* Password for http: */
char *urlCanonical; /* Canonical representation of the URL */
char *urlProxyAuth; /* Proxy-Authorizer: string */
char *urlFossil; /* The path of the ?fossil=path suffix on ssh: */
int dontKeepUrl; /* Do not persist the URL */
const char *zLogin; /* Login name. "" if not logged in. */
const char *zSSLIdentity; /* Value of --ssl-identity option, filename of SSL client identity */
int useLocalauth; /* No login required if from 127.0.0.1 */
int noPswd; /* Logged in without password (on 127.0.0.1) */
int userUid; /* Integer user id */
/* Information used to populate the RCVFROM table */
|
| ︙ | | | ︙ | |
163
164
165
166
167
168
169
170
171
172
173
174
175
176
|
const char *azAuxName[MX_AUX]; /* Name of each aux() or option() value */
char *azAuxParam[MX_AUX]; /* Param of each aux() or option() value */
const char *azAuxVal[MX_AUX]; /* Value of each aux() or option() value */
const char **azAuxOpt[MX_AUX]; /* Options of each option() value */
int anAuxCols[MX_AUX]; /* Number of columns for option() values */
int allowSymlinks; /* Cached "allow-symlinks" option */
};
/*
** Macro for debugging:
*/
#define CGIDEBUG(X) if( g.fDebug ) cgi_debug X
|
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
|
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
|
const char *azAuxName[MX_AUX]; /* Name of each aux() or option() value */
char *azAuxParam[MX_AUX]; /* Param of each aux() or option() value */
const char *azAuxVal[MX_AUX]; /* Value of each aux() or option() value */
const char **azAuxOpt[MX_AUX]; /* Options of each option() value */
int anAuxCols[MX_AUX]; /* Number of columns for option() values */
int allowSymlinks; /* Cached "allow-symlinks" option */
struct FossilJsonBits {
int isJsonMode; /* True if running in JSON mode, else false. This changes
how errors are reported. In JSON mode we try to always
output JSON-form error responses.
*/
int resultCode; /* used for passing back specific codes from /json callbacks. */
int errorDetailParanoia; /* 0=full error codes, 1=%10, 2=%100, 3=%1000 */
cson_cgi_cx cgiCx; /* cson_cgi context */
cson_output_opt outOpt; /* formatting options for JSON mode. */
cson_value * authToken; /* authentication token */
struct {
cson_value * v;
cson_object * o;
} reqPayload; /* request payload object (if any) */
} json;
};
/*
** Macro for debugging:
*/
#define CGIDEBUG(X) if( g.fDebug ) cgi_debug X
|
| ︙ | | | ︙ | |
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
|
if( cnt==1 ){
*pIndex = m;
return 0;
}
return 1+(cnt>1);
}
/*
** This procedure runs first.
*/
int main(int argc, char **argv){
const char *zCmdName = "unknown";
int idx;
int rc;
int i;
sqlite3_config(SQLITE_CONFIG_LOG, fossil_sqlite_log, 0);
g.now = time(0);
g.argc = argc;
g.argv = argv;
for(i=0; i<argc; i++) g.argv[i] = fossil_mbcs_to_utf8(argv[i]);
if( getenv("GATEWAY_INTERFACE")!=0 && !find_option("nocgi", 0, 0)){
zCmdName = "cgi";
}else if( argc<2 ){
fossil_fatal("Usage: %s COMMAND ...\n"
"\"%s help\" for a list of available commands\n"
"\"%s help COMMAND\" for specific details\n",
argv[0], argv[0], argv[0]);
}else{
g.fQuiet = find_option("quiet", 0, 0)!=0;
g.fSqlTrace = find_option("sqltrace", 0, 0)!=0;
g.fSqlStats = find_option("sqlstats", 0, 0)!=0;
g.fSystemTrace = find_option("systemtrace", 0, 0)!=0;
if( g.fSqlTrace ) g.fSqlStats = 1;
g.fSqlPrint = find_option("sqlprint", 0, 0)!=0;
g.fHttpTrace = find_option("httptrace", 0, 0)!=0;
|
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
|
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
|
if( cnt==1 ){
*pIndex = m;
return 0;
}
return 1+(cnt>1);
}
/*
** atexit() handler which frees up "some" of the resources
** used by fossil.
*/
void fossil_atexit() {
cson_cgi_cx_clean(&g.json.cgiCx);
if(g.db){
db_close(0);
}
}
/*
** This procedure runs first.
*/
int main(int argc, char **argv){
const char *zCmdName = "unknown";
int idx;
int rc;
int i;
sqlite3_config(SQLITE_CONFIG_LOG, fossil_sqlite_log, 0);
memset(&g, 0, sizeof(g));
g.now = time(0);
g.argc = argc;
g.argv = argv;
g.json.errorDetailParanoia = 0 /* FIXME: make configurable */;
g.json.cgiCx = cson_cgi_cx_empty;
g.json.outOpt = cson_output_opt_empty;
g.json.outOpt.addNewline = 1;
g.json.outOpt.indentation = 1 /* FIXME: make configurable */;
for(i=0; i<argc; i++) g.argv[i] = fossil_mbcs_to_utf8(argv[i]);
if( getenv("GATEWAY_INTERFACE")!=0 && !find_option("nocgi", 0, 0)){
zCmdName = "cgi";
g.isCGI = 1;
}else if( argc<2 ){
fossil_fatal("Usage: %s COMMAND ...\n"
"\"%s help\" for a list of available commands\n"
"\"%s help COMMAND\" for specific details\n",
argv[0], argv[0], argv[0]);
}else{
g.isCGI = 0;
g.fQuiet = find_option("quiet", 0, 0)!=0;
g.fSqlTrace = find_option("sqltrace", 0, 0)!=0;
g.fSqlStats = find_option("sqlstats", 0, 0)!=0;
g.fSystemTrace = find_option("systemtrace", 0, 0)!=0;
if( g.fSqlTrace ) g.fSqlStats = 1;
g.fSqlPrint = find_option("sqlprint", 0, 0)!=0;
g.fHttpTrace = find_option("httptrace", 0, 0)!=0;
|
| ︙ | | | ︙ | |
294
295
296
297
298
299
300
301
302
303
304
305
306
307
|
}
}
fossil_fatal("%s: ambiguous command prefix: %s\n"
"%s: could be any of:%s\n"
"%s: use \"help\" for more information\n",
argv[0], zCmdName, argv[0], blob_str(&couldbe), argv[0]);
}
aCommand[idx].xFunc();
fossil_exit(0);
/*NOT_REACHED*/
return 0;
}
/*
|
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
|
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
|
}
}
fossil_fatal("%s: ambiguous command prefix: %s\n"
"%s: could be any of:%s\n"
"%s: use \"help\" for more information\n",
argv[0], zCmdName, argv[0], blob_str(&couldbe), argv[0]);
}
rc = cson_cgi_init(&g.json.cgiCx, g.argc, (char const * const *)g.argv, NULL)
/* Reminder: cson_cgi_init() may process the POST data before
fossil gets to, but it is configured to only read
application/[json/javascript] and text/plain. form-urlencoded
and x-fossil-* data will be consumed by fossil's cgi_init().
Note that we set up the CGI bits even when not running in CGI
mode because some of cson_cgi's facilities are useful in
non-CGI contexts and we use those in the CLI variants of the
JSON commands.
FIXME: do some analysis of the request path (HTTP mode) or
CLI args (CLI mode) and only call this if the command is
a JSON-mode command. We can only do that easily from here
if we use e.g. /json/foo instead of /foo.json, since we
have a common prefix.
*/
;
if(rc){
fossil_fatal("%s: unrecoverable error while initializing JSON CGI bits: "
"cson error code #%d (%s)\n",
argv[0], rc, cson_rc_string(rc));
}else{
if( NULL != cson_cgi_env_get_obj( &g.json.cgiCx, 'p', 0 ) ){
/* if cson_cgi read the POST data then we're certainly in JSON
mode. If it didn't then we have to delay this decision until
the JSON family of callbacks is called.
*/
g.json.isJsonMode = 1;
}
atexit( fossil_atexit );
}
aCommand[idx].xFunc();
fossil_exit(0);
/*NOT_REACHED*/
return 0;
}
/*
|
| ︙ | | | ︙ | |
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
|
/*
** Print an error message, rollback all databases, and quit. These
** routines never return.
*/
void fossil_panic(const char *zFormat, ...){
char *z;
va_list ap;
static int once = 1;
mainInFatalError = 1;
va_start(ap, zFormat);
z = vmprintf(zFormat, ap);
va_end(ap);
if( g.cgiOutput && once ){
once = 0;
cgi_printf("<p class=\"generalError\">%h</p>", z);
cgi_reply();
}else{
char *zOut = mprintf("%s: %s\n", fossil_nameofexe(), z);
fossil_puts(zOut, 1);
}
db_force_rollback();
fossil_exit(1);
}
void fossil_fatal(const char *zFormat, ...){
char *z;
va_list ap;
mainInFatalError = 1;
va_start(ap, zFormat);
z = vmprintf(zFormat, ap);
va_end(ap);
if( g.cgiOutput ){
g.cgiOutput = 0;
cgi_printf("<p class=\"generalError\">%h</p>", z);
cgi_reply();
}else{
char *zOut = mprintf("\r%s: %s\n", fossil_nameofexe(), z);
fossil_puts(zOut, 1);
}
db_force_rollback();
fossil_exit(1);
}
/* This routine works like fossil_fatal() except that if called
** recursively, the recursive call is a no-op.
**
** Use this in places where an error might occur while doing
** fatal error shutdown processing. Unlike fossil_panic() and
|
>
>
>
>
>
>
|
>
|
>
>
>
>
>
>
>
>
|
|
|
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
|
/*
** Print an error message, rollback all databases, and quit. These
** routines never return.
*/
void fossil_panic(const char *zFormat, ...){
char *z;
va_list ap;
int rc = 1;
static int once = 1;
mainInFatalError = 1;
va_start(ap, zFormat);
z = vmprintf(zFormat, ap);
va_end(ap);
if( g.json.isJsonMode ){
json_err( 0, z, 1 );
if( g.isCGI ){
rc = 0 /* avoid HTTP 500 */;
}
}else if( g.cgiOutput && once ){
once = 0;
cgi_printf("<p class=\"generalError\">%h</p>", z);
cgi_reply();
}else{
char *zOut = mprintf("%s: %s\n", fossil_nameofexe(), z);
fossil_puts(zOut, 1);
}
free(z);
db_force_rollback();
fossil_exit(rc);
}
void fossil_fatal(const char *zFormat, ...){
char *z;
int rc = 1;
va_list ap;
mainInFatalError = 1;
va_start(ap, zFormat);
z = vmprintf(zFormat, ap);
va_end(ap);
if( g.json.isJsonMode ){
json_err( 0, z, 1 );
if( g.isCGI ){
rc = 0 /* avoid HTTP 500 */;
}
}
else if( g.cgiOutput ){
g.cgiOutput = 0;
cgi_printf("<p class=\"generalError\">%h</p>", z);
cgi_reply();
}else{
char *zOut = mprintf("\r%s: %s\n", fossil_nameofexe(), z);
fossil_puts(zOut, 1);
}
db_force_rollback();
fossil_exit(rc);
}
/* This routine works like fossil_fatal() except that if called
** recursively, the recursive call is a no-op.
**
** Use this in places where an error might occur while doing
** fatal error shutdown processing. Unlike fossil_panic() and
|
| ︙ | | | ︙ | |