Fossil SCM

fossil-scm / src / main.c
Source Blame History 3857 lines
dbda8d6… drh 1 /*
c19f34c… drh 2 ** Copyright (c) 2006 D. Richard Hipp
dbda8d6… drh 3 **
dbda8d6… drh 4 ** This program is free software; you can redistribute it and/or
c06edd2… drh 5 ** modify it under the terms of the Simplified BSD License (also
c06edd2… drh 6 ** known as the "2-Clause License" or "FreeBSD License".)
3a610a3… drh 7 **
dbda8d6… drh 8 ** This program is distributed in the hope that it will be useful,
c06edd2… drh 9 ** but without any warranty; without even the implied warranty of
c06edd2… drh 10 ** merchantability or fitness for a particular purpose.
dbda8d6… drh 11 **
dbda8d6… drh 12 ** Author contact information:
dbda8d6… drh 13 ** [email protected]
dbda8d6… drh 14 ** http://www.hwaci.com/drh/
dbda8d6… drh 15 **
dbda8d6… drh 16 *******************************************************************************
dbda8d6… drh 17 **
dbda8d6… drh 18 ** This module codes the main() procedure that runs first when the
dbda8d6… drh 19 ** program is invoked.
dbda8d6… drh 20 */
b065aff… mistachkin 21 #include "VERSION.h"
dbda8d6… drh 22 #include "config.h"
7aeeb30… mistachkin 23 #if defined(_WIN32)
7aeeb30… mistachkin 24 # include <windows.h>
250fc00… mistachkin 25 # include <io.h>
e285341… mistachkin 26 # define GETPID (int)GetCurrentProcessId
7aeeb30… mistachkin 27 #endif
f41cf03… mistachkin 28
f41cf03… mistachkin 29 /* BUGBUG: This (PID_T) does not work inside of INTERFACE block. */
f41cf03… mistachkin 30 #if USE_SEE
f41cf03… mistachkin 31 #if defined(_WIN32)
f41cf03… mistachkin 32 typedef DWORD PID_T;
f41cf03… mistachkin 33 #else
f41cf03… mistachkin 34 typedef pid_t PID_T;
f41cf03… mistachkin 35 #endif
f41cf03… mistachkin 36 #endif
f41cf03… mistachkin 37
dbda8d6… drh 38 #include "main.h"
dbda8d6… drh 39 #include <string.h>
dbda8d6… drh 40 #include <time.h>
22cb1e1… drh 41 #include <fcntl.h>
7ba10f1… drh 42 #include <sys/types.h>
7ba10f1… drh 43 #include <sys/stat.h>
796dcfe… drh 44 #include <stdlib.h> /* atexit() */
f9c2d23… stephan 45 #include <zlib.h>
7aeeb30… mistachkin 46 #if !defined(_WIN32)
4d107b5… stephan 47 # include <errno.h> /* errno global */
e285341… mistachkin 48 # include <unistd.h>
e285341… mistachkin 49 # include <signal.h>
e285341… mistachkin 50 # define GETPID getpid
e452aa9… mistachkin 51 #endif
e452aa9… mistachkin 52 #ifdef FOSSIL_ENABLE_SSL
48f1239… jan.nijtmans 53 # include "openssl/crypto.h"
4d107b5… stephan 54 #endif
dbda8d6… drh 55 #if INTERFACE
e452aa9… mistachkin 56 #ifdef FOSSIL_ENABLE_TCL
e452aa9… mistachkin 57 # include "tcl.h"
e452aa9… mistachkin 58 #endif
796dcfe… drh 59 #ifdef FOSSIL_ENABLE_JSON
12ff5ff… drh 60 # include "cson_amalgamation.h" /* JSON API. */
796dcfe… drh 61 # include "json_detail.h"
a8d220b… drh 62 #endif
a8d220b… drh 63 #ifdef HAVE_BACKTRACE
a8d220b… drh 64 # include <execinfo.h>
a8d220b… drh 65 #endif
a8d220b… drh 66
a8d220b… drh 67 /*
e2bdc10… danield 68 ** Default length of a timeout for serving an HTTP request. Changeable
7979989… drh 69 ** using the "--timeout N" command-line option or via "timeout: N" in the
7979989… drh 70 ** CGI script.
7979989… drh 71 */
7979989… drh 72 #ifndef FOSSIL_DEFAULT_TIMEOUT
7979989… drh 73 # define FOSSIL_DEFAULT_TIMEOUT 600 /* 10 minutes */
7979989… drh 74 #endif
7979989… drh 75
7979989… drh 76 /*
55342eb… drh 77 ** Maximum number of auxiliary parameters on reports
55342eb… drh 78 */
55342eb… drh 79 #define MX_AUX 5
55342eb… drh 80
55342eb… drh 81 /*
b344d3c… drh 82 ** Holds flags for fossil user permissions.
b344d3c… drh 83 */
b344d3c… drh 84 struct FossilUserPerms {
b344d3c… drh 85 char Setup; /* s: use Setup screens on web interface */
b344d3c… drh 86 char Admin; /* a: administrative permission */
b344d3c… drh 87 char Password; /* p: change password */
30c107e… mistachkin 88 char Write; /* i: xfer inbound. check-in */
30c107e… mistachkin 89 char Read; /* o: xfer outbound. check-out */
433cde1… drh 90 char Hyperlink; /* h: enable the display of hyperlinks */
b344d3c… drh 91 char Clone; /* g: clone */
b344d3c… drh 92 char RdWiki; /* j: view wiki via web */
b344d3c… drh 93 char NewWiki; /* f: create new wiki via web */
b344d3c… drh 94 char ApndWiki; /* m: append to wiki via web */
b344d3c… drh 95 char WrWiki; /* k: edit wiki via web */
fc0bffd… drh 96 char ModWiki; /* l: approve and publish wiki content (Moderator) */
b344d3c… drh 97 char RdTkt; /* r: view tickets via web */
b344d3c… drh 98 char NewTkt; /* n: create new tickets */
b344d3c… drh 99 char ApndTkt; /* c: append to tickets via the web */
b344d3c… drh 100 char WrTkt; /* w: make changes to tickets via web */
fc0bffd… drh 101 char ModTkt; /* q: approve and publish ticket changes (Moderator) */
5079ffb… stephan 102 char Attach; /* b: add attachments to wiki or tickets */
b344d3c… drh 103 char TktFmt; /* t: create new ticket report formats */
b344d3c… drh 104 char RdAddr; /* e: read email addresses or other private data */
b344d3c… drh 105 char Zip; /* z: download zipped artifact via /zip URL */
b344d3c… drh 106 char Private; /* x: can send and receive private content */
27d743e… drh 107 char WrUnver; /* y: can push unversioned content */
9a2e5f4… drh 108 char RdForum; /* 2: Read forum posts */
9a2e5f4… drh 109 char WrForum; /* 3: Create new forum posts */
9a2e5f4… drh 110 char WrTForum; /* 4: Post to forums not subject to moderation */
9a2e5f4… drh 111 char ModForum; /* 5: Moderate (approve or reject) forum posts */
779ddef… wyoung 112 char AdminForum; /* 6: Grant capability 4 to other users */
25eafed… drh 113 char EmailAlert; /* 7: Sign up for email notifications */
65f5754… drh 114 char Announce; /* A: Send announcements */
5079ffb… stephan 115 char AttachForum; /* B: add attachments to forum */
e8ba89b… drh 116 char Chat; /* C: read or write the chatroom */
fd31983… drh 117 char Debug; /* D: show extra Fossil debugging features */
397d23c… drh 118 /* These last two are included to block infinite recursion */
397d23c… drh 119 char XReader; /* u: Inherit all privileges of "reader" */
397d23c… drh 120 char XDeveloper; /* v: Inherit all privileges of "developer" */
d8ed5a0… drh 121 };
d8ed5a0… drh 122
d8ed5a0… drh 123 #ifdef FOSSIL_ENABLE_TCL
d8ed5a0… drh 124 /*
d8ed5a0… drh 125 ** All Tcl related context information is in this structure. This structure
d8ed5a0… drh 126 ** definition has been copied from and should be kept in sync with the one in
d8ed5a0… drh 127 ** "th_tcl.c".
d8ed5a0… drh 128 */
d8ed5a0… drh 129 struct TclContext {
fa4e828… mistachkin 130 int argc; /* Number of original (expanded) arguments. */
fa4e828… mistachkin 131 char **argv; /* Full copy of the original (expanded) arguments. */
d7bcdcb… mistachkin 132 void *hLibrary; /* The Tcl library module handle. */
fa4e828… mistachkin 133 void *xFindExecutable; /* See tcl_FindExecutableProc in th_tcl.c. */
3fe737a… jan.nijtmans 134 #if TCL_MAJOR_VERSION>=9
3fe737a… jan.nijtmans 135 void *xZipfsAppHook; /* See TclZipfsAppHookProc in th_tcl.c. */
3fe737a… jan.nijtmans 136 #endif
fa4e828… mistachkin 137 void *xCreateInterp; /* See tcl_CreateInterpProc in th_tcl.c. */
fea0402… jan.nijtmans 138 void *xDeleteInterp; /* See tcl_DeleteInterpProc in th_tcl.c. */
6b58c67… mistachkin 139 void *xFinalize; /* See tcl_FinalizeProc in th_tcl.c. */
fa4e828… mistachkin 140 Tcl_Interp *interp; /* The on-demand created Tcl interpreter. */
6b58c67… mistachkin 141 int useObjProc; /* Non-zero if an objProc can be called directly. */
18fc492… mistachkin 142 int useTip285; /* Non-zero if TIP #285 is available. */
fa4e828… mistachkin 143 char *setup; /* The optional Tcl setup script. */
49c63f8… mistachkin 144 void *xPreEval; /* Optional, called before Tcl_Eval*(). */
49c63f8… mistachkin 145 void *pPreContext; /* Optional, provided to xPreEval(). */
49c63f8… mistachkin 146 void *xPostEval; /* Optional, called after Tcl_Eval*(). */
49c63f8… mistachkin 147 void *pPostContext; /* Optional, provided to xPostEval(). */
b344d3c… drh 148 };
d8ed5a0… drh 149 #endif
b344d3c… drh 150
dbda8d6… drh 151 struct Global {
dbda8d6… drh 152 int argc; char **argv; /* Command-line arguments to the program */
5caa3c5… drh 153 char **argvOrig; /* Original g.argv prior to removing options */
dacc694… jan.nijtmans 154 char *nameOfExe; /* Full path of executable. */
4727ef4… drh 155 const char *zErrlog; /* Log errors to this file, if not NULL */
b05a6c6… george 156 const char *zPhase; /* Phase of operation, for use by the error log
b05a6c6… george 157 ** and for deriving $canonical_page TH1 variable */
840b762… drh 158 int isConst; /* True if the output is unchanging & cacheable */
8fa8fe8… drh 159 int iResultCode; /* Process reply code for commands */
19de4b5… mistachkin 160 const char *zVfsName; /* The VFS to use for database connections */
dbda8d6… drh 161 sqlite3 *db; /* The connection to the databases */
00ac794… drh 162 sqlite3 *dbConfig; /* Separate connection for global_config table */
5dd8b2d… drh 163 char *zAuxSchema; /* Main repository aux-schema */
801eca6… drh 164 int dbIgnoreErrors; /* Ignore database errors if true */
79988f9… drh 165 char *zConfigDbName; /* Path of the config database. NULL if not open */
e0073f4… drh 166 sqlite3_int64 now; /* Seconds since 1970 */
dbda8d6… drh 167 int repositoryOpen; /* True if the main repository database is open */
752ea43… drh 168 unsigned iRepoDataVers; /* Initial data version for repository database */
912fce2… mistachkin 169 char *zRepositoryOption; /* Most recent cached repository option value */
06aec61… drh 170 char *zRepositoryName; /* Name of the repository database file */
06aec61… drh 171 char *zLocalDbName; /* Name of the local database file */
2c77d1d… andygoth 172 char *zOpenRevision; /* Check-in version to use during database open */
95a6a65… drh 173 const char *zCmdName; /* Name of the Fossil command currently running */
dbda8d6… drh 174 int localOpen; /* True if the local database is open */
dbda8d6… drh 175 char *zLocalRoot; /* The directory holding the local database */
8ad5e46… wyoung 176 int minPrefix; /* Number of digits needed for a distinct hash */
3c9dc14… jan.nijtmans 177 int eHashPolicy; /* Current hash policy. One of HPOLICY_* */
b811123… drh 178 int fSqlTrace; /* True if --sqltrace flag is present */
b811123… drh 179 int fSqlStats; /* True if --sqltrace or --sqlstats are present */
9a2e5f4… drh 180 int fSqlPrint; /* True if --sqlprint flag is present */
9a2e5f4… drh 181 int fCgiTrace; /* True if --cgitrace is enabled */
dccd27c… jeremy_c 182 int fQuiet; /* True if -quiet flag is present */
239b4c1… drh 183 int fJail; /* True if running with a chroot jail */
dbda8d6… drh 184 int fHttpTrace; /* Trace outbound HTTP requests */
af3d3b6… drh 185 int fAnyTrace; /* Any kind of tracing */
f6263bb… drh 186 int fAllowACME; /* Deliver files from .well-known */
5d536c5… drh 187 char *zHttpAuth; /* HTTP Authorization user:pass information */
5a4dc22… drh 188 int fSystemTrace; /* Trace calls to fossil_system(), --systemtrace */
00cf858… drh 189 int fSshTrace; /* Trace the SSH setup traffic */
dbb5e2d… drh 190 int fSshClient; /* HTTP client flags for SSH client */
cfc7aca… drh 191 int fNoHttpCompress; /* Do not compress HTTP traffic (for debugging) */
dbb5e2d… drh 192 char *zSshCmd; /* SSH command string */
3cd412e… drh 193 const char *zHttpCmd; /* External program to do HTTP requests */
b058c8a… mistachkin 194 int fNoSync; /* Do not do an autosync ever. --nosync */
6f55dbd… drh 195 int eIPvers; /* 0: any 1: ipv4-only 2: ipv6-only */
a44e3c7… george 196 char *zPath; /* Name of webpage being served (may be NULL) */
dbda8d6… drh 197 char *zExtra; /* Extra path information past the webpage name */
dbda8d6… drh 198 char *zBaseURL; /* Full text of the URL being served */
a4e7b86… drh 199 char *zHttpsURL; /* zBaseURL translated to https: */
d0305b3… aku 200 char *zTop; /* Parent directory of zPath */
acb4397… drh 201 int nExtraURL; /* Extra bytes added to SCRIPT_NAME */
ec56c69… drh 202 const char *zExtRoot; /* Document root for the /ext sub-website */
dbda8d6… drh 203 const char *zContentType; /* The content type of the input HTTP request */
dbda8d6… drh 204 int iErrPriority; /* Priority of current error message */
dbda8d6… drh 205 char *zErrMsg; /* Text of an error message */
be0e804… drh 206 int sslNotAvailable; /* SSL is not available. Do not redirect to https: */
dbda8d6… drh 207 Blob cgiIn; /* Input to an xfer www method */
fd31983… drh 208 int cgiOutput; /* 0: command-line 1: CGI. 2: after CGI */
2be82dc… drh 209 int xferPanic; /* Write error messages in XFER protocol */
dcc4866… drh 210 int fullHttpReply; /* True for full HTTP reply. False for CGI reply */
588bb7c… aku 211 Th_Interp *interp; /* The TH1 interpreter */
b058c8a… mistachkin 212 char *th1Setup; /* The TH1 post-creation setup script, if any */
142200b… mistachkin 213 int th1Flags; /* The TH1 integration state flags */
e2e016c… drh 214 FILE *httpIn; /* Accept HTTP input from here */
e2e016c… drh 215 FILE *httpOut; /* Send HTTP output here */
f6263bb… drh 216 int httpUseSSL; /* True to use an SSL codec for HTTP traffic */
f6263bb… drh 217 void *httpSSLConn; /* The SSL connection */
243e02b… drh 218 int xlinkClusterOnly; /* Set when cloning. Only process clusters */
0b36f02… drh 219 int fTimeFormat; /* 1 for UTC. 2 for localtime. 0 not yet selected */
e2e016c… drh 220 int *aCommitFile; /* Array of files to be committed */
02a584f… drh 221 int markPrivate; /* All new artifacts are private if true */
3a5caa8… drh 222 char *ckinLockFail; /* Check-in lock failure received from server */
c6a65cb… drh 223 int clockSkewSeen; /* True if clocks on client and server out of sync */
43b30dc… drh 224 int wikiFlags; /* Wiki conversion flags applied to %W */
db0c512… drh 225 char isHTTP; /* True if server/CGI modes, else assume CLI. */
df337eb… drh 226 char jsHref; /* If true, set href= using javascript, not HTML */
03f07b5… drh 227 Blob httpHeader; /* Complete text of the HTTP request header */
5fdad9b… drh 228 UrlData url; /* Information about current URL */
840b762… drh 229 const char *zLogin; /* Login name. NULL or "" if not logged in. */
dc1121f… drh 230 const char *zCkoutAlias; /* doc/ uses this branch as an alias for "ckout" */
73ca280… drh 231 const char *zMainMenuFile; /* --mainmenu FILE from server/ui/cgi */
12ff5ff… drh 232 const char *zSSLIdentity; /* Value of --ssl-identity option, filename of
12ff5ff… drh 233 ** SSL client identity */
e6b06b5… drh 234 const char *zCgiFile; /* Name of the CGI file */
a8e33fb… drh 235 const char *zReqType; /* Type of request: "HTTP", "CGI", "SCGI" */
f41cf03… mistachkin 236 #if USE_SEE
57d8a71… mistachkin 237 const char *zPidKey; /* Saved value of the --usepidkey option. Only
f41cf03… mistachkin 238 * applicable when using SEE on Windows or Linux. */
57d8a71… mistachkin 239 #endif
f7a3c6d… drh 240 int useLocalauth; /* No login required if from 127.0.0.1 */
dbda8d6… drh 241 int noPswd; /* Logged in without password (on 127.0.0.1) */
dbda8d6… drh 242 int userUid; /* Integer user id */
10006db… drh 243 int eAuthMethod; /* How the user authenticated to us */
10006db… drh 244 # define AUTH_NONE 0 /* Not authenticated */
10006db… drh 245 # define AUTH_COOKIE 1 /* Authentication by cookie */
10006db… drh 246 # define AUTH_LOCAL 2 /* Uses loopback */
10006db… drh 247 # define AUTH_PW 3 /* Authentication by password */
10006db… drh 248 # define AUTH_ENV 4 /* Authenticated by REMOTE_USER environment var */
10006db… drh 249 # define AUTH_HTTP 5 /* HTTP Basic Authentication */
16b3309… drh 250 int isRobot; /* True if the client is definitely a robot. False
16b3309… drh 251 ** negatives are common for this flag */
2476b81… drh 252 int comFmtFlags; /* Zero or more "COMMENT_PRINT_*" bit flags, should be
2476b81… drh 253 ** accessed through get_comment_format(). */
effdada… drh 254 const char *zSockName; /* Name of the unix-domain socket file */
effdada… drh 255 const char *zSockMode; /* File permissions for unix-domain socket */
effdada… drh 256 const char *zSockOwner; /* Owner, or owner:group for unix-domain socket */
dbda8d6… drh 257
dbda8d6… drh 258 /* Information used to populate the RCVFROM table */
dbda8d6… drh 259 int rcvid; /* The rcvid. 0 if not yet defined. */
dbda8d6… drh 260 char *zIpAddr; /* The remote IP address */
dbda8d6… drh 261 char *zNonce; /* The nonce used for login */
f668ff4… drh 262
653dd40… drh 263 /* permissions available to current user */
b344d3c… drh 264 struct FossilUserPerms perm;
653dd40… drh 265
653dd40… drh 266 /* permissions available to current user or to "anonymous".
653dd40… drh 267 ** This is the logical union of perm permissions above with
653dd40… drh 268 ** the value that perm would take if g.zLogin were "anonymous". */
653dd40… drh 269 struct FossilUserPerms anon;
d8ed5a0… drh 270
d8ed5a0… drh 271 #ifdef FOSSIL_ENABLE_TCL
d8ed5a0… drh 272 /* all Tcl related context necessary for integration */
d8ed5a0… drh 273 struct TclContext tcl;
d8ed5a0… drh 274 #endif
d8ed5a0… drh 275
0be5482… drh 276 /* For defense against Cross-site Request Forgery attacks */
920ace1… drh 277 char zCsrfToken[16]; /* Value of the anti-CSRF token */
920ace1… drh 278 int okCsrf; /* -1: unsafe
920ace1… drh 279 ** 0: unknown
920ace1… drh 280 ** 1: same origin
920ace1… drh 281 ** 2: same origin + is POST
920ace1… drh 282 ** 3: same origin, POST, valid csrf token */
0be5482… drh 283
f25e5e5… drh 284 int parseCnt[10]; /* Counts of artifacts parsed */
dbda8d6… drh 285 FILE *fDebug; /* Write debug information here, if the file exists */
1c528d3… mistachkin 286 #ifdef FOSSIL_ENABLE_TH1_HOOKS
1c528d3… mistachkin 287 int fNoThHook; /* Disable all TH1 command/webpage hooks */
1c528d3… mistachkin 288 #endif
f55c6a1… drh 289 int thTrace; /* True to enable TH1 debugging output */
f55c6a1… drh 290 Blob thLog; /* Text of the TH1 debugging output */
426a3ba… drh 291
426a3ba… drh 292 int isHome; /* True if rendering the "home" page */
55342eb… drh 293
55342eb… drh 294 /* Storage for the aux() and/or option() SQL function arguments */
55342eb… drh 295 int nAux; /* Number of distinct aux() or option() values */
55342eb… drh 296 const char *azAuxName[MX_AUX]; /* Name of each aux() or option() value */
55342eb… drh 297 char *azAuxParam[MX_AUX]; /* Param of each aux() or option() value */
55342eb… drh 298 const char *azAuxVal[MX_AUX]; /* Value of each aux() or option() value */
55342eb… drh 299 const char **azAuxOpt[MX_AUX]; /* Options of each option() value */
55342eb… drh 300 int anAuxCols[MX_AUX]; /* Number of columns for option() values */
e4f1c1f… drh 301 int allowSymlinks; /* Cached "allow-symlinks" option */
e28433a… stephan 302 int mainTimerId; /* Set to fossil_timer_start() */
05ec15c… drh 303 int nPendingRequest; /* # of HTTP requests in "fossil server" */
41c7caa… drh 304 int nRequest; /* Total # of HTTP request */
9116c6f… drh 305 int bAvoidDeltaManifests; /* Avoid using delta manifests if true */
1862890… stephan 306
1862890… stephan 307 /* State for communicating specific details between the inbound HTTP
1862890… stephan 308 ** header parser (cgi.c), xfer.c, and http.c. */
1862890… stephan 309 struct {
1862890… stephan 310 char *zLoginCard; /* Inbound "x-f-l-c" Cookie header. */
1862890… stephan 311 int fLoginCardMode; /* If non-0, emit login cards in outbound
1862890… stephan 312 ** requests as a HTTP cookie instead of as
1862890… stephan 313 ** part of the payload. Gets activated
1862890… stephan 314 ** on-demand based on xfer traffic
1862890… stephan 315 ** contents. Values, for
1862890… stephan 316 ** diagnostic/debugging purposes: 0x01=CLI
1862890… stephan 317 ** --flag, 0x02=cgi_setup_query_string(),
1862890… stephan 318 ** 0x04=page_xfer(),
1862890… stephan 319 ** 0x08=client_sync(). */
1862890… stephan 320 int remoteVersion; /* Remote fossil version. Used for negotiating
1862890… stephan 321 ** how to handle the login card. */
1862890… stephan 322 } syncInfo;
796dcfe… drh 323 #ifdef FOSSIL_ENABLE_JSON
796dcfe… drh 324 struct FossilJsonBits {
796dcfe… drh 325 int isJsonMode; /* True if running in JSON mode, else
796dcfe… drh 326 false. This changes how errors are
796dcfe… drh 327 reported. In JSON mode we try to
796dcfe… drh 328 always output JSON-form error
39bef92… stephan 329 responses and always (in CGI mode)
39bef92… stephan 330 exit() with code 0 to avoid an HTTP
39bef92… stephan 331 500 error.
796dcfe… drh 332 */
a588e55… mistachkin 333 int preserveRc; /* Do not convert error codes into 0.
a588e55… mistachkin 334 * This is primarily intended for use
a588e55… mistachkin 335 * by the test suite. */
12ff5ff… drh 336 int resultCode; /* used for passing back specific codes
12ff5ff… drh 337 ** from /json callbacks. */
796dcfe… drh 338 int errorDetailParanoia; /* 0=full error codes, 1=%10, 2=%100, 3=%1000 */
796dcfe… drh 339 cson_output_opt outOpt; /* formatting options for JSON mode. */
4e18dba… jan.nijtmans 340 cson_value *authToken; /* authentication token */
4e18dba… jan.nijtmans 341 const char *jsonp; /* Name of JSONP function wrapper. */
796dcfe… drh 342 unsigned char dispatchDepth /* Tells JSON command dispatching
796dcfe… drh 343 which argument we are currently
796dcfe… drh 344 working on. For this purpose, arg#0
796dcfe… drh 345 is the "json" path/CLI arg.
796dcfe… drh 346 */;
796dcfe… drh 347 struct { /* "garbage collector" */
4e18dba… jan.nijtmans 348 cson_value *v;
4e18dba… jan.nijtmans 349 cson_array *a;
796dcfe… drh 350 } gc;
796dcfe… drh 351 struct { /* JSON POST data. */
4e18dba… jan.nijtmans 352 cson_value *v;
4e18dba… jan.nijtmans 353 cson_array *a;
796dcfe… drh 354 int offset; /* Tells us which PATH_INFO/CLI args
796dcfe… drh 355 part holds the "json" command, so
796dcfe… drh 356 that we can account for sub-repos
796dcfe… drh 357 and path prefixes. This is handled
796dcfe… drh 358 differently for CLI and CGI modes.
796dcfe… drh 359 */
4e18dba… jan.nijtmans 360 const char *commandStr /*"command" request param.*/;
796dcfe… drh 361 } cmd;
796dcfe… drh 362 struct { /* JSON POST data. */
4e18dba… jan.nijtmans 363 cson_value *v;
4e18dba… jan.nijtmans 364 cson_object *o;
796dcfe… drh 365 } post;
38bf1d0… stephan 366 struct { /* GET/COOKIE params in JSON mode. */
4e18dba… jan.nijtmans 367 cson_value *v;
4e18dba… jan.nijtmans 368 cson_object *o;
796dcfe… drh 369 } param;
796dcfe… drh 370 struct {
4e18dba… jan.nijtmans 371 cson_value *v;
4e18dba… jan.nijtmans 372 cson_object *o;
796dcfe… drh 373 } reqPayload; /* request payload object (if any) */
4e18dba… jan.nijtmans 374 cson_array *warnings; /* response warnings */
e28433a… stephan 375 int timerId; /* fetched from fossil_timer_start() */
796dcfe… drh 376 } json;
796dcfe… drh 377 #endif /* FOSSIL_ENABLE_JSON */
3990518… george 378 int ftntsIssues[4]; /* Counts for misref, strayed, joined, overnested */
9bb61a4… drh 379 int diffCnt[3]; /* Counts for DIFF_NUMSTAT: files, ins, del */
dbda8d6… drh 380 };
dbda8d6… drh 381
dbda8d6… drh 382 /*
dbda8d6… drh 383 ** Macro for debugging:
dbda8d6… drh 384 */
dbda8d6… drh 385 #define CGIDEBUG(X) if( g.fDebug ) cgi_debug X
dbda8d6… drh 386
dbda8d6… drh 387 #endif
dbda8d6… drh 388
dbda8d6… drh 389 Global g;
dbda8d6… drh 390
dbda8d6… drh 391 /*
796dcfe… drh 392 ** atexit() handler which frees up "some" of the resources
796dcfe… drh 393 ** used by fossil.
796dcfe… drh 394 */
bb0cedb… drh 395 static void fossil_atexit(void) {
bc7683e… drh 396 static int once = 0;
bc7683e… drh 397 if( once++ ) return; /* Ensure that this routine only runs once */
a8484dc… mistachkin 398 #if USE_SEE
a8484dc… mistachkin 399 /*
a8484dc… mistachkin 400 ** Zero, unlock, and free the saved database encryption key now.
a8484dc… mistachkin 401 */
a8484dc… mistachkin 402 db_unsave_encryption_key();
a8484dc… mistachkin 403 #endif
999e33c… mistachkin 404 #if defined(_WIN32) || (defined(__BIONIC__) && !defined(FOSSIL_HAVE_GETPASS))
a8484dc… mistachkin 405 /*
a8484dc… mistachkin 406 ** Free the secure getpass() buffer now.
a8484dc… mistachkin 407 */
a8484dc… mistachkin 408 freepass();
a8484dc… mistachkin 409 #endif
ba779fa… mistachkin 410 #if defined(_WIN32) && !defined(_WIN64) && defined(FOSSIL_ENABLE_TCL) && \
ba779fa… mistachkin 411 defined(USE_TCL_STUBS)
6b58c67… mistachkin 412 /*
6b58c67… mistachkin 413 ** If Tcl is compiled on Windows using the latest MinGW, Fossil can crash
6b58c67… mistachkin 414 ** when exiting while a stubs-enabled Tcl is still loaded. This is due to
6b58c67… mistachkin 415 ** a bug in MinGW, see:
6b58c67… mistachkin 416 **
6b58c67… mistachkin 417 ** http://comments.gmane.org/gmane.comp.gnu.mingw.user/41724
6b58c67… mistachkin 418 **
6b58c67… mistachkin 419 ** The workaround is to manually unload the loaded Tcl library prior to
ba779fa… mistachkin 420 ** exiting the process. This issue does not impact 64-bit Windows.
6b58c67… mistachkin 421 */
6b58c67… mistachkin 422 unloadTcl(g.interp, &g.tcl);
da96f91… jan.nijtmans 423 #endif
796dcfe… drh 424 #ifdef FOSSIL_ENABLE_JSON
796dcfe… drh 425 cson_value_free(g.json.gc.v);
796dcfe… drh 426 memset(&g.json, 0, sizeof(g.json));
effdada… drh 427 #endif
effdada… drh 428 #if !defined(_WIN32)
effdada… drh 429 if( g.zSockName && file_issocket(g.zSockName) ){
effdada… drh 430 unlink(g.zSockName);
effdada… drh 431 }
5caa3c5… drh 432 #endif
796dcfe… drh 433 free(g.zErrMsg);
796dcfe… drh 434 if(g.db){
796dcfe… drh 435 db_close(0);
60acb31… mistachkin 436 }
4cf8dbe… stephan 437 manifest_clear_cache();
4cf8dbe… stephan 438 content_clear_cache(1);
4cf8dbe… stephan 439 rebuild_clear_cache();
60acb31… mistachkin 440 /*
60acb31… mistachkin 441 ** FIXME: The next two lines cannot always be enabled; however, they
e0f22dd… mistachkin 442 ** are very useful for tracking down TH1 memory leaks.
60acb31… mistachkin 443 */
e0f22dd… mistachkin 444 if( fossil_getenv("TH1_DELETE_INTERP")!=0 ){
e0f22dd… mistachkin 445 if( g.interp ){
e0f22dd… mistachkin 446 Th_DeleteInterp(g.interp); g.interp = 0;
e0f22dd… mistachkin 447 }
999e33c… mistachkin 448 #if defined(TH_MEMDEBUG)
999e33c… mistachkin 449 if( Th_GetOutstandingMalloc()!=0 ){
999e33c… mistachkin 450 fossil_print("Th_GetOutstandingMalloc() => %d\n",
999e33c… mistachkin 451 Th_GetOutstandingMalloc());
999e33c… mistachkin 452 }
999e33c… mistachkin 453 assert( Th_GetOutstandingMalloc()==0 );
999e33c… mistachkin 454 #endif
8205c01… drh 455 }
8205c01… drh 456 }
796dcfe… drh 457
796dcfe… drh 458 /*
f668ff4… drh 459 ** Convert all arguments from mbcs (or unicode) to UTF-8. Then
0c7ae64… drh 460 ** search g.argv for arguments "--args FILENAME". If found, then
c0274f9… drh 461 ** (1) remove the two arguments from g.argv
c0274f9… drh 462 ** (2) Read the file FILENAME
c0274f9… drh 463 ** (3) Use the contents of FILE to replace the two removed arguments:
c0274f9… drh 464 ** (a) Ignore blank lines in the file
c0274f9… drh 465 ** (b) Each non-empty line of the file is an argument, except
c0274f9… drh 466 ** (c) If the line begins with "-" and contains a space, it is broken
c0274f9… drh 467 ** into two arguments at the space.
c0274f9… drh 468 */
3a71292… drh 469 void expand_args_option(int argc, void *argv){
c0274f9… drh 470 Blob file = empty_blob; /* Content of the file */
c0274f9… drh 471 Blob line = empty_blob; /* One line of the file */
c0274f9… drh 472 unsigned int nLine; /* Number of lines in the file*/
c0274f9… drh 473 unsigned int i, j, k; /* Loop counters */
c0274f9… drh 474 int n; /* Number of bytes in one line */
14c1402… drh 475 unsigned int nArg; /* Number of new arguments */
f668ff4… drh 476 char *z; /* General use string pointer */
f668ff4… drh 477 char **newArgv; /* New expanded g.argv under construction */
4e18dba… jan.nijtmans 478 const char *zFileName; /* input file name */
4727ef4… drh 479 FILE *inFile; /* input FILE */
0c7ae64… drh 480
0c7ae64… drh 481 g.argc = argc;
0c7ae64… drh 482 g.argv = argv;
8205c01… drh 483 sqlite3_initialize();
8205c01… drh 484 #if defined(_WIN32) && defined(BROKEN_MINGW_CMDLINE)
53db40e… drh 485 for(i=0; (int)i<g.argc; i++) g.argv[i] = fossil_mbcs_to_utf8(g.argv[i]);
ab7ad23… stephan 486 #else
53db40e… drh 487 for(i=0; (int)i<g.argc; i++) g.argv[i] = fossil_path_to_utf8(g.argv[i]);
ab7ad23… stephan 488 #endif
ab7ad23… stephan 489 g.nameOfExe = file_fullexename(g.argv[0]);
53db40e… drh 490 for(i=1; (int)i<g.argc-1; i++){
c0274f9… drh 491 z = g.argv[i];
c0274f9… drh 492 if( z[0]!='-' ) continue;
c0274f9… drh 493 z++;
c0274f9… drh 494 if( z[0]=='-' ) z++;
62a1688… stephan 495 /* Maintenance reminder: we do not stop at a "--" flag here,
62a1688… stephan 496 ** instead delegating that to find_option(). Doing it here
62a1688… stephan 497 ** introduces some weird corner cases, as covered in forum thread
62a1688… stephan 498 ** 4382bbc66757c39f. e.g. (fossil -U -- --args ...) is handled
62a1688… stephan 499 ** differently when we stop at "--" here. */
c0274f9… drh 500 if( fossil_strcmp(z, "args")==0 ) break;
c0274f9… drh 501 }
5caa3c5… drh 502 if( (int)i>=g.argc-1 ){
5caa3c5… drh 503 g.argvOrig = fossil_malloc( sizeof(char*)*(g.argc+1) );
5caa3c5… drh 504 memcpy(g.argvOrig, g.argv, sizeof(g.argv[0])*(g.argc+1));
5caa3c5… drh 505 return;
5caa3c5… drh 506 }
c0274f9… drh 507
080d27a… stephan 508 zFileName = g.argv[i+1];
14c1402… drh 509 if( strcmp(zFileName,"-")==0 ){
14c1402… drh 510 inFile = stdin;
14c1402… drh 511 }else if( !file_isfile(zFileName, ExtFILE) ){
14c1402… drh 512 fossil_fatal("Not an ordinary file: \"%s\"", zFileName);
080d27a… stephan 513 }else{
14c1402… drh 514 inFile = fossil_fopen(zFileName,"rb");
14c1402… drh 515 if( inFile==0 ){
14c1402… drh 516 fossil_fatal("Cannot open -args file [%s]", zFileName);
080d27a… stephan 517 }
080d27a… stephan 518 }
14c1402… drh 519 blob_read_from_channel(&file, inFile, -1);
14c1402… drh 520 if(stdin != inFile){
14c1402… drh 521 fclose(inFile);
14c1402… drh 522 }
14c1402… drh 523 inFile = NULL;
9b800ee… mistachkin 524 blob_to_utf8_no_bom(&file, 1);
c0274f9… drh 525 z = blob_str(&file);
c0274f9… drh 526 for(k=0, nLine=1; z[k]; k++) if( z[k]=='\n' ) nLine++;
14c1402… drh 527 if( nLine>100000000 ) fossil_fatal("too many command-line arguments");
14c1402… drh 528 nArg = g.argc + nLine*2;
5caa3c5… drh 529 newArgv = fossil_malloc( sizeof(char*)*nArg*2 + 2);
c0274f9… drh 530 for(j=0; j<i; j++) newArgv[j] = g.argv[j];
f668ff4… drh 531
c0274f9… drh 532 blob_rewind(&file);
e120c2a… stephan 533 while( nLine-->0 && (n = blob_line(&file, &line))>0 ){
e120c2a… stephan 534 /* Reminder: ^^^ nLine check avoids that embedded NUL bytes in the
e120c2a… stephan 535 ** --args file causes nLine to be less than blob_line() will end
04a9e74… stephan 536 ** up reporting, as such a miscount leads to an illegal memory
e120c2a… stephan 537 ** write. See forum post
e120c2a… stephan 538 ** https://fossil-scm.org/forum/forumpost/7b34eecc1b8c for
e120c2a… stephan 539 ** details */
14c1402… drh 540 if( n<1 ){
14c1402… drh 541 /* Reminder: corner-case: a line with 1 byte and no newline. */
14c1402… drh 542 continue;
14c1402… drh 543 }
c0274f9… drh 544 z = blob_buffer(&line);
ee990eb… stephan 545 if('\n'==z[n-1]){
ee990eb… stephan 546 z[n-1] = 0;
ee990eb… stephan 547 }
ee990eb… stephan 548
080d27a… stephan 549 if((n>1) && ('\r'==z[n-2])){
080d27a… stephan 550 if(n==2) continue /*empty line*/;
080d27a… stephan 551 z[n-2] = 0;
ee990eb… stephan 552 }
ee990eb… stephan 553 if(!z[0]) continue;
14c1402… drh 554 if( j>=nArg ){
14c1402… drh 555 fossil_fatal("malformed command-line arguments");
14c1402… drh 556 }
c0274f9… drh 557 newArgv[j++] = z;
c0274f9… drh 558 if( z[0]=='-' ){
c0274f9… drh 559 for(k=1; z[k] && !fossil_isspace(z[k]); k++){}
c0274f9… drh 560 if( z[k] ){
c0274f9… drh 561 z[k] = 0;
c0274f9… drh 562 k++;
c0274f9… drh 563 if( z[k] ) newArgv[j++] = &z[k];
c0274f9… drh 564 }
c0274f9… drh 565 }
c0274f9… drh 566 }
c0274f9… drh 567 i += 2;
53db40e… drh 568 while( (int)i<g.argc ) newArgv[j++] = g.argv[i++];
c0274f9… drh 569 newArgv[j] = 0;
c0274f9… drh 570 g.argc = j;
c0274f9… drh 571 g.argv = newArgv;
5caa3c5… drh 572 g.argvOrig = &g.argv[j+1];
5caa3c5… drh 573 memcpy(g.argvOrig, g.argv, sizeof(g.argv[0])*(j+1));
fa4e828… mistachkin 574 }
fa4e828… mistachkin 575
0c37874… drh 576 #ifdef FOSSIL_ENABLE_TCL
fa4e828… mistachkin 577 /*
fa4e828… mistachkin 578 ** Make a deep copy of the provided argument array and return it.
fa4e828… mistachkin 579 */
fa4e828… mistachkin 580 static char **copy_args(int argc, char **argv){
fa4e828… mistachkin 581 char **zNewArgv;
fa4e828… mistachkin 582 int i;
fa4e828… mistachkin 583 zNewArgv = fossil_malloc( sizeof(char*)*(argc+1) );
fa4e828… mistachkin 584 memset(zNewArgv, 0, sizeof(char*)*(argc+1));
fa4e828… mistachkin 585 for(i=0; i<argc; i++){
fa4e828… mistachkin 586 zNewArgv[i] = fossil_strdup(argv[i]);
fa4e828… mistachkin 587 }
fa4e828… mistachkin 588 return zNewArgv;
fa4e828… mistachkin 589 }
0c37874… drh 590 #endif
0c37874… drh 591
bb0cedb… drh 592 /*
2212ac4… mistachkin 593 ** Returns a name for a SQLite return code.
bb0cedb… drh 594 */
2212ac4… mistachkin 595 static const char *fossil_sqlite_return_code_name(int rc){
bb0cedb… drh 596 static char zCode[30];
2212ac4… mistachkin 597 switch( rc & 0xff ){
bb0cedb… drh 598 case SQLITE_OK: return "SQLITE_OK";
bb0cedb… drh 599 case SQLITE_ERROR: return "SQLITE_ERROR";
2212ac4… mistachkin 600 case SQLITE_INTERNAL: return "SQLITE_INTERNAL";
bb0cedb… drh 601 case SQLITE_PERM: return "SQLITE_PERM";
bb0cedb… drh 602 case SQLITE_ABORT: return "SQLITE_ABORT";
bb0cedb… drh 603 case SQLITE_BUSY: return "SQLITE_BUSY";
2212ac4… mistachkin 604 case SQLITE_LOCKED: return "SQLITE_LOCKED";
bb0cedb… drh 605 case SQLITE_NOMEM: return "SQLITE_NOMEM";
bb0cedb… drh 606 case SQLITE_READONLY: return "SQLITE_READONLY";
bb0cedb… drh 607 case SQLITE_INTERRUPT: return "SQLITE_INTERRUPT";
bb0cedb… drh 608 case SQLITE_IOERR: return "SQLITE_IOERR";
bb0cedb… drh 609 case SQLITE_CORRUPT: return "SQLITE_CORRUPT";
2212ac4… mistachkin 610 case SQLITE_NOTFOUND: return "SQLITE_NOTFOUND";
bb0cedb… drh 611 case SQLITE_FULL: return "SQLITE_FULL";
bb0cedb… drh 612 case SQLITE_CANTOPEN: return "SQLITE_CANTOPEN";
bb0cedb… drh 613 case SQLITE_PROTOCOL: return "SQLITE_PROTOCOL";
bb0cedb… drh 614 case SQLITE_EMPTY: return "SQLITE_EMPTY";
bb0cedb… drh 615 case SQLITE_SCHEMA: return "SQLITE_SCHEMA";
2212ac4… mistachkin 616 case SQLITE_TOOBIG: return "SQLITE_TOOBIG";
bb0cedb… drh 617 case SQLITE_CONSTRAINT: return "SQLITE_CONSTRAINT";
bb0cedb… drh 618 case SQLITE_MISMATCH: return "SQLITE_MISMATCH";
bb0cedb… drh 619 case SQLITE_MISUSE: return "SQLITE_MISUSE";
bb0cedb… drh 620 case SQLITE_NOLFS: return "SQLITE_NOLFS";
2212ac4… mistachkin 621 case SQLITE_AUTH: return "SQLITE_AUTH";
bb0cedb… drh 622 case SQLITE_FORMAT: return "SQLITE_FORMAT";
bb0cedb… drh 623 case SQLITE_RANGE: return "SQLITE_RANGE";
bb0cedb… drh 624 case SQLITE_NOTADB: return "SQLITE_NOTADB";
2212ac4… mistachkin 625 case SQLITE_NOTICE: return "SQLITE_NOTICE";
14b1e90… drh 626 case SQLITE_WARNING: return "SQLITE_WARNING";
2212ac4… mistachkin 627 case SQLITE_ROW: return "SQLITE_ROW";
2212ac4… mistachkin 628 case SQLITE_DONE: return "SQLITE_DONE";
bb0cedb… drh 629 default: {
2212ac4… mistachkin 630 sqlite3_snprintf(sizeof(zCode), zCode, "SQLite return code %d", rc);
bb0cedb… drh 631 }
bb0cedb… drh 632 }
bb0cedb… drh 633 return zCode;
bb0cedb… drh 634 }
bb0cedb… drh 635
bb0cedb… drh 636 /* Error logs from SQLite */
bb0cedb… drh 637 static void fossil_sqlite_log(void *notUsed, int iCode, const char *zErrmsg){
c6ecf21… drh 638 sqlite3_stmt *p;
c6ecf21… drh 639 Blob msg;
14b1e90… drh 640 #ifdef __APPLE__
14b1e90… drh 641 /* Disable the file alias warning on apple products because Time Machine
c6ecf21… drh 642 ** creates lots of aliases and the warnings alarm people. */
14b1e90… drh 643 if( iCode==SQLITE_WARNING ) return;
1336c4a… drh 644 #endif
1336c4a… drh 645 #ifndef FOSSIL_DEBUG
1336c4a… drh 646 /* Disable the automatic index warning except in FOSSIL_DEBUG builds. */
1336c4a… drh 647 if( iCode==SQLITE_WARNING_AUTOINDEX ) return;
801eca6… drh 648 #endif
d239479… drh 649 if( iCode==SQLITE_SCHEMA ) return;
801eca6… drh 650 if( g.dbIgnoreErrors ) return;
500b0a2… drh 651 #ifdef SQLITE_READONLY_DIRECTORY
500b0a2… drh 652 if( iCode==SQLITE_READONLY_DIRECTORY ){
500b0a2… drh 653 zErrmsg = "database is in a read-only directory";
500b0a2… drh 654 }
500b0a2… drh 655 #endif
c6ecf21… drh 656 blob_init(&msg, 0, 0);
d66f322… drh 657 blob_appendf(&msg, "%s(%d): %s",
d66f322… drh 658 fossil_sqlite_return_code_name(iCode), iCode, zErrmsg);
c6ecf21… drh 659 if( g.db ){
c6ecf21… drh 660 for(p=sqlite3_next_stmt(g.db, 0); p; p=sqlite3_next_stmt(g.db,p)){
c6ecf21… drh 661 const char *zSql;
c6ecf21… drh 662 if( !sqlite3_stmt_busy(p) ) continue;
c6ecf21… drh 663 zSql = sqlite3_sql(p);
c6ecf21… drh 664 if( zSql==0 ) continue;
c6ecf21… drh 665 blob_appendf(&msg, "\nSQL: %s", zSql);
c6ecf21… drh 666 }
c6ecf21… drh 667 }
c6ecf21… drh 668 fossil_warning("%s", blob_str(&msg));
c6ecf21… drh 669 blob_reset(&msg);
b6c9d35… mistachkin 670 }
b6c9d35… mistachkin 671
b6c9d35… mistachkin 672 /*
6382e25… drh 673 ** Initialize the g.comFmtFlags global variable.
6382e25… drh 674 **
6382e25… drh 675 ** Global command-line options --comfmtflags or --comment-format can be
6382e25… drh 676 ** used for this. However, those command-line options are undocumented
6382e25… drh 677 ** and deprecated. They are here for backwards compatibility only.
b6c9d35… mistachkin 678 */
b6c9d35… mistachkin 679 static void fossil_init_flags_from_options(void){
6382e25… drh 680 const char *zValue = find_option("comfmtflags", 0, 1);
6382e25… drh 681 if( zValue==0 ){
6382e25… drh 682 zValue = find_option("comment-format", 0, 1);
6382e25… drh 683 }
6382e25… drh 684 if( zValue ){
6382e25… drh 685 g.comFmtFlags = atoi(zValue);
6382e25… drh 686 }else{
6382e25… drh 687 g.comFmtFlags = COMMENT_PRINT_UNSET; /* Command-line option not found. */
6382e25… drh 688 }
37b2eb9… drh 689 }
37b2eb9… drh 690
37b2eb9… drh 691 /*
37b2eb9… drh 692 ** Check to see if the Fossil binary contains an appended repository
37b2eb9… drh 693 ** file using the appendvfs extension. If so, change command-line arguments
37b2eb9… drh 694 ** to cause Fossil to launch with "fossil ui" on that repo.
37b2eb9… drh 695 */
37b2eb9… drh 696 static int fossilExeHasAppendedRepo(void){
7674265… drh 697 int rc;
7674265… drh 698 /* Warning: dedeuceDatabaseType is an internal function of the SQLite
7674265… drh 699 ** CLI and is subject to change. Verify the following function template
7674265… drh 700 ** when updating to a newer SQLite */
7674265… drh 701 extern int deduceDatabaseType(const char*,int,int);
7674265… drh 702
7674265… drh 703 g.dbIgnoreErrors++;
7674265… drh 704 rc = deduceDatabaseType(g.nameOfExe,0,SQLITE_OPEN_READWRITE);
7674265… drh 705 g.dbIgnoreErrors--;
7674265… drh 706 if( rc ){
37b2eb9… drh 707 static char *azAltArgv[] = { 0, "ui", 0, 0 };
37b2eb9… drh 708 azAltArgv[0] = g.nameOfExe;
37b2eb9… drh 709 azAltArgv[2] = g.nameOfExe;
37b2eb9… drh 710 g.argv = azAltArgv;
37b2eb9… drh 711 g.argc = 3;
37b2eb9… drh 712 return 1;
37b2eb9… drh 713 }else{
37b2eb9… drh 714 return 0;
b6c9d35… mistachkin 715 }
bb0cedb… drh 716 }
8205c01… drh 717
8205c01… drh 718 /*
796dcfe… drh 719 ** This procedure runs first.
796dcfe… drh 720 */
3a71292… drh 721 #if defined(FOSSIL_FUZZ)
3a71292… drh 722 /* Do not include a main() procedure when building for fuzz testing.
3a71292… drh 723 ** libFuzzer will supply main(). */
3a71292… drh 724 #elif defined(_WIN32) && !defined(BROKEN_MINGW_CMDLINE)
3a71292… drh 725 int _dowildcard = -1; /* This turns on command-line globbing in MinGW-w64 */
094fb2e… mistachkin 726 int wmain(int argc, wchar_t **argv){ return fossil_main(argc,(char**)argv); }
3a71292… drh 727 #elif defined(_WIN32)
3a71292… drh 728 int _CRT_glob = 0x0001; /* See MinGW bug #2062 */
3a71292… drh 729 int main(int argc, char **argv){ return fossil_main(argc, argv); }
8205c01… drh 730 #else
3a71292… drh 731 int main(int argc, char **argv){ return fossil_main(argc, argv); }
3a71292… drh 732 #endif
07a5a21… drh 733
07a5a21… drh 734 /* All the work of main() is done by a separate procedure "fossil_main()".
07a5a21… drh 735 ** We have to break this out, because fossil_main() is sometimes called
07a5a21… drh 736 ** separately (by the "shell" command) but we do not want atwait() handlers
07a5a21… drh 737 ** being called by separate invocations of fossil_main().
07a5a21… drh 738 */
07a5a21… drh 739 int fossil_main(int argc, char **argv){
796dcfe… drh 740 const char *zCmdName = "unknown";
e6aab1a… drh 741 const CmdOrPage *pCmd = 0;
796dcfe… drh 742 int rc;
e37db2b… drh 743 #ifdef _WIN32
e37db2b… drh 744 DWORD mode;
e37db2b… drh 745 HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
e37db2b… drh 746 GetConsoleMode(hOut, &mode);
e37db2b… drh 747 SetConsoleMode(hOut, mode | ENABLE_VIRTUAL_TERMINAL_PROCESSING);
e37db2b… drh 748 #endif
3f193ba… drh 749
48c06b0… drh 750 g.zPhase = "init";
e285341… mistachkin 751 #if !defined(_WIN32_WCE)
e285341… mistachkin 752 if( fossil_getenv("FOSSIL_BREAK") ){
898a70c… stephan 753 if( fossil_isatty(0) && fossil_isatty(2) ){
e285341… mistachkin 754 fprintf(stderr,
e285341… mistachkin 755 "attach debugger to process %d and press any key to continue.\n",
e285341… mistachkin 756 GETPID());
6b2cc46… danield 757 (void)fgetc(stdin);
e285341… mistachkin 758 }else{
e285341… mistachkin 759 #if defined(_WIN32) || defined(WIN32)
e285341… mistachkin 760 DebugBreak();
e285341… mistachkin 761 #elif defined(SIGTRAP)
e285341… mistachkin 762 raise(SIGTRAP);
e285341… mistachkin 763 #endif
e285341… mistachkin 764 }
e285341… mistachkin 765 }
e285341… mistachkin 766 #endif
e285341… mistachkin 767
008fc92… drh 768 fossil_printf_selfcheck();
3f193ba… drh 769 fossil_limit_memory(1);
7018d7c… drh 770
7018d7c… drh 771 /* When updating the minimum SQLite version, change the number here,
7018d7c… drh 772 ** and also MINIMUM_SQLITE_VERSION value set in ../auto.def. Take
7018d7c… drh 773 ** care that both places agree! */
a13a024… drh 774 if( sqlite3_libversion_number()<3054000
a13a024… drh 775 || strncmp(sqlite3_sourceid(),"2025-07-01",10)<0
50d433e… drh 776 ){
a13a024… drh 777 fossil_panic("Unsuitable SQLite version %s, must be at least 3.54.0",
aee2922… mistachkin 778 sqlite3_libversion());
aee2922… mistachkin 779 }
7018d7c… drh 780
d4044fe… jan.nijtmans 781 sqlite3_config(SQLITE_CONFIG_MULTITHREAD);
796dcfe… drh 782 sqlite3_config(SQLITE_CONFIG_LOG, fossil_sqlite_log, 0);
796dcfe… drh 783 memset(&g, 0, sizeof(g));
796dcfe… drh 784 g.now = time(0);
d8f716e… drh 785 g.httpHeader = empty_blob;
796dcfe… drh 786 #ifdef FOSSIL_ENABLE_JSON
796dcfe… drh 787 #if defined(NDEBUG)
796dcfe… drh 788 g.json.errorDetailParanoia = 2 /* FIXME: make configurable
796dcfe… drh 789 One problem we have here is that this
796dcfe… drh 790 code is needed before the db is opened,
796dcfe… drh 791 so we can't sql for it.*/;
796dcfe… drh 792 #else
796dcfe… drh 793 g.json.errorDetailParanoia = 0;
796dcfe… drh 794 #endif
796dcfe… drh 795 g.json.outOpt = cson_output_opt_empty;
796dcfe… drh 796 g.json.outOpt.addNewline = 1;
796dcfe… drh 797 g.json.outOpt.indentation = 1 /* in CGI/server mode this can be configured */;
796dcfe… drh 798 #endif /* FOSSIL_ENABLE_JSON */
0c7ae64… drh 799 expand_args_option(argc, argv);
b6a7e52… drh 800 #ifdef FOSSIL_ENABLE_TCL
fa4e828… mistachkin 801 memset(&g.tcl, 0, sizeof(TclContext));
b6a7e52… drh 802 g.tcl.argc = g.argc;
fa4e828… mistachkin 803 g.tcl.argv = copy_args(g.argc, g.argv); /* save full arguments */
b6a7e52… drh 804 #endif
e28433a… stephan 805 g.mainTimerId = fossil_timer_start();
0c51600… mistachkin 806 capture_case_sensitive_option();
1862890… stephan 807 g.syncInfo.fLoginCardMode =
1862890… stephan 808 /* The undocumented/unsupported --login-card-header provides a way
1862890… stephan 809 ** to force use of the feature added by the xfer-login-card branch
1862890… stephan 810 ** in 2025-07, intended for assisting in debugging any related
1862890… stephan 811 ** issues. It can be removed once we reach the level of "implicit
1862890… stephan 812 ** trust" in that feature. */
1862890… stephan 813 find_option("login-card-header",0,0) ? 0x01 : 0;
19de4b5… mistachkin 814 g.zVfsName = find_option("vfs",0,1);
19de4b5… mistachkin 815 if( g.zVfsName==0 ){
19de4b5… mistachkin 816 g.zVfsName = fossil_getenv("FOSSIL_VFS");
19de4b5… mistachkin 817 }
19de4b5… mistachkin 818 if( g.zVfsName ){
19de4b5… mistachkin 819 sqlite3_vfs *pVfs = sqlite3_vfs_find(g.zVfsName);
eb3899c… jan.nijtmans 820 if( pVfs ){
eb3899c… jan.nijtmans 821 sqlite3_vfs_register(pVfs, 1);
eb3899c… jan.nijtmans 822 }else{
99fcc43… drh 823 fossil_fatal("no such VFS: \"%s\"", g.zVfsName);
eb3899c… jan.nijtmans 824 }
eb3899c… jan.nijtmans 825 }
8594e7a… drh 826 if( !find_option("nocgi", 0, 0) && fossil_getenv("GATEWAY_INTERFACE")!=0){
796dcfe… drh 827 zCmdName = "cgi";
796dcfe… drh 828 g.isHTTP = 1;
37b2eb9… drh 829 }else if( g.argc<2 && !fossilExeHasAppendedRepo() ){
796dcfe… drh 830 fossil_print(
796dcfe… drh 831 "Usage: %s COMMAND ...\n"
796dcfe… drh 832 " or: %s help -- for a list of common commands\n"
b894afa… jan.nijtmans 833 " or: %s help COMMAND -- for help with the named command\n",
b6a7e52… drh 834 g.argv[0], g.argv[0], g.argv[0]);
0e5bbad… stephan 835 fossil_print(
0e5bbad… stephan 836 "\nCommands and filenames may be passed on to fossil from a file\n"
0e5bbad… stephan 837 "by using:\n"
0e5bbad… stephan 838 "\n %s --args FILENAME ...\n",
0e5bbad… stephan 839 g.argv[0]
0e5bbad… stephan 840 );
0e5bbad… stephan 841 fossil_print(
24e259d… stephan 842 "\nEach line of the file is assumed to be a filename unless it starts\n"
0e5bbad… stephan 843 "with '-' and contains a space, in which case it is assumed to be\n"
0e5bbad… stephan 844 "another flag and is treated as such. --args FILENAME may be used\n"
0e5bbad… stephan 845 "in conjunction with any other flags.\n");
796dcfe… drh 846 fossil_exit(1);
796dcfe… drh 847 }else{
42f4d14… drh 848 const char *zChdir = find_option("chdir",0,1);
796dcfe… drh 849 g.isHTTP = 0;
440ed5d… andybradford 850 g.rcvid = 0;
74d5ce3… florian 851 g.fQuiet = find_option("quiet", "q", 0)!=0;
796dcfe… drh 852 g.fSqlTrace = find_option("sqltrace", 0, 0)!=0;
796dcfe… drh 853 g.fSqlStats = find_option("sqlstats", 0, 0)!=0;
796dcfe… drh 854 g.fSystemTrace = find_option("systemtrace", 0, 0)!=0;
00cf858… drh 855 g.fSshTrace = find_option("sshtrace", 0, 0)!=0;
9a2e5f4… drh 856 g.fCgiTrace = find_option("cgitrace", 0, 0)!=0;
dbb5e2d… drh 857 g.fSshClient = 0;
dbb5e2d… drh 858 g.zSshCmd = 0;
796dcfe… drh 859 if( g.fSqlTrace ) g.fSqlStats = 1;
a588e55… mistachkin 860 #ifdef FOSSIL_ENABLE_JSON
a588e55… mistachkin 861 g.json.preserveRc = find_option("json-preserve-rc", 0, 0)!=0;
a588e55… mistachkin 862 #endif
796dcfe… drh 863 g.fHttpTrace = find_option("httptrace", 0, 0)!=0;
1c528d3… mistachkin 864 #ifdef FOSSIL_ENABLE_TH1_HOOKS
1c528d3… mistachkin 865 g.fNoThHook = find_option("no-th-hook", 0, 0)!=0;
1c528d3… mistachkin 866 #endif
9a2e5f4… drh 867 g.fAnyTrace = g.fSqlTrace|g.fSystemTrace|g.fSshTrace|
9a2e5f4… drh 868 g.fHttpTrace|g.fCgiTrace;
5d536c5… drh 869 g.zHttpAuth = 0;
796dcfe… drh 870 g.zLogin = find_option("user", "U", 1);
796dcfe… drh 871 g.zSSLIdentity = find_option("ssl-identity", 0, 1);
4727ef4… drh 872 g.zErrlog = find_option("errorlog", 0, 1);
b6c9d35… mistachkin 873 fossil_init_flags_from_options();
41c3c19… drh 874 if( find_option("utc",0,0) ) g.fTimeFormat = 1;
41c3c19… drh 875 if( find_option("localtime",0,0) ) g.fTimeFormat = 2;
12bc63a… jan.nijtmans 876 if( zChdir && file_chdir(zChdir, 0) ){
99fcc43… drh 877 fossil_fatal("unable to change directories to %s", zChdir);
42f4d14… drh 878 }
f41cf03… mistachkin 879 #if USE_SEE
f41cf03… mistachkin 880 db_maybe_handle_saved_encryption_key_for_process(SEE_KEY_READ);
57d8a71… mistachkin 881 #endif
30eb891… stephan 882 if( find_option("help","?",0)!=0 ){
100ac83… andygoth 883 /* If --help is found anywhere on the command line, translate the command
100ac83… andygoth 884 * to "fossil help cmdname" where "cmdname" is the first argument that
100ac83… andygoth 885 * does not begin with a "-" character. If all arguments start with "-",
100ac83… andygoth 886 * translate to "fossil help argv[1] argv[2]...". */
100ac83… andygoth 887 int i, nNewArgc;
3091605… drh 888 char **zNewArgv = fossil_malloc( sizeof(char*)*(g.argc+3) );
b6a7e52… drh 889 zNewArgv[0] = g.argv[0];
796dcfe… drh 890 zNewArgv[1] = "help";
3091605… drh 891 zNewArgv[2] = "-c";
100ac83… andygoth 892 for(i=1; i<g.argc; i++){
100ac83… andygoth 893 if( g.argv[i][0]!='-' ){
3091605… drh 894 nNewArgc = 4;
3091605… drh 895 zNewArgv[3] = g.argv[i];
3091605… drh 896 zNewArgv[4] = 0;
100ac83… andygoth 897 break;
100ac83… andygoth 898 }
100ac83… andygoth 899 }
100ac83… andygoth 900 if( i==g.argc ){
100ac83… andygoth 901 for(i=1; i<g.argc; i++) zNewArgv[i+1] = g.argv[i];
100ac83… andygoth 902 nNewArgc = g.argc+1;
100ac83… andygoth 903 zNewArgv[i+1] = 0;
100ac83… andygoth 904 }
100ac83… andygoth 905 g.argc = nNewArgc;
100ac83… andygoth 906 g.argv = zNewArgv;
aad4c24… drh 907 #if 0
aad4c24… drh 908 }else if( g.argc==2 && file_is_repository(g.argv[1]) ){
aad4c24… drh 909 char **zNewArgv = fossil_malloc( sizeof(char*)*4 );
aad4c24… drh 910 zNewArgv[0] = g.argv[0];
aad4c24… drh 911 zNewArgv[1] = "ui";
aad4c24… drh 912 zNewArgv[2] = g.argv[1];
aad4c24… drh 913 zNewArgv[3] = 0;
aad4c24… drh 914 g.argc = 3;
aad4c24… drh 915 g.argv = zNewArgv;
aad4c24… drh 916 #endif
275da70… danield 917 }
796dcfe… drh 918 zCmdName = g.argv[1];
796dcfe… drh 919 }
4ef3708… drh 920 #ifndef _WIN32
6dbaa67… drh 921 /* There is a bug in stunnel4 in which it sometimes starts up client
6dbaa67… drh 922 ** processes without first opening file descriptor 2 (standard error).
6dbaa67… drh 923 ** If this happens, and a subsequent open() of a database returns file
6dbaa67… drh 924 ** descriptor 2, and then an assert() fires and writes on fd 2, that
6dbaa67… drh 925 ** can corrupt the data file. To avoid this problem, make sure open()
6dbaa67… drh 926 ** will never return file descriptor 2 or less. */
6dbaa67… drh 927 if( !is_valid_fd(2) ){
6dbaa67… drh 928 int nTry = 0;
6dbaa67… drh 929 int fd = 0;
fdfc039… drh 930 int x = 0;
fdfc039… drh 931 do{
fdfc039… drh 932 fd = open("/dev/null",O_WRONLY);
fdfc039… drh 933 if( fd>=2 ) break;
fdfc039… drh 934 if( fd<0 ) x = errno;
fdfc039… drh 935 }while( nTry++ < 2 );
fdfc039… drh 936 if( fd<2 ){
35ab16b… drh 937 g.cgiOutput = 1;
35ab16b… drh 938 g.httpOut = stdout;
35ab16b… drh 939 g.fullHttpReply = !g.isHTTP;
3f5ab71… drh 940 fossil_panic("file descriptor 2 is not open. (fd=%d, errno=%d)",
fdfc039… drh 941 fd, x);
a3ade26… drh 942 }
a3ade26… drh 943 }
4ef3708… drh 944 #endif
a3bc655… drh 945 g.zCmdName = zCmdName;
e6aab1a… drh 946 rc = dispatch_name_search(zCmdName, CMDFLAG_COMMAND|CMDFLAG_PREFIX, &pCmd);
aad4c24… drh 947 if( rc==1 && g.argc==2 && file_is_repository(g.argv[1]) ){
aad4c24… drh 948 /* If the command-line is "fossil ABC" and "ABC" is no a valid command,
aad4c24… drh 949 ** but "ABC" is the name of a repository file, make the command be
aad4c24… drh 950 ** "fossil ui ABC" instead.
aad4c24… drh 951 */
aad4c24… drh 952 char **zNewArgv = fossil_malloc( sizeof(char*)*4 );
aad4c24… drh 953 zNewArgv[0] = g.argv[0];
aad4c24… drh 954 zNewArgv[1] = "ui";
aad4c24… drh 955 zNewArgv[2] = g.argv[1];
aad4c24… drh 956 zNewArgv[3] = 0;
aad4c24… drh 957 g.argc = 3;
aad4c24… drh 958 g.argv = zNewArgv;
aad4c24… drh 959 g.zCmdName = zCmdName = "ui";
aad4c24… drh 960 rc = dispatch_name_search(zCmdName, CMDFLAG_COMMAND|CMDFLAG_PREFIX, &pCmd);
aad4c24… drh 961 }
796dcfe… drh 962 if( rc==1 ){
1c528d3… mistachkin 963 #ifdef FOSSIL_ENABLE_TH1_HOOKS
1c528d3… mistachkin 964 if( !g.isHTTP && !g.fNoThHook ){
1c528d3… mistachkin 965 rc = Th_CommandHook(zCmdName, 0);
1c528d3… mistachkin 966 }else{
1c528d3… mistachkin 967 rc = TH_OK;
1c528d3… mistachkin 968 }
1c528d3… mistachkin 969 if( rc==TH_OK || rc==TH_RETURN || rc==TH_CONTINUE ){
1c528d3… mistachkin 970 if( rc==TH_OK || rc==TH_RETURN ){
1c528d3… mistachkin 971 #endif
99fcc43… drh 972 fossil_fatal("%s: unknown command: %s\n"
e6aab1a… drh 973 "%s: use \"help\" for more information",
1c528d3… mistachkin 974 g.argv[0], zCmdName, g.argv[0]);
1c528d3… mistachkin 975 #ifdef FOSSIL_ENABLE_TH1_HOOKS
1c528d3… mistachkin 976 }
1c528d3… mistachkin 977 if( !g.isHTTP && !g.fNoThHook && (rc==TH_OK || rc==TH_CONTINUE) ){
1c528d3… mistachkin 978 Th_CommandNotify(zCmdName, 0);
1c528d3… mistachkin 979 }
1c528d3… mistachkin 980 }
1c528d3… mistachkin 981 fossil_exit(0);
1c528d3… mistachkin 982 #endif
796dcfe… drh 983 }else if( rc==2 ){
796dcfe… drh 984 Blob couldbe;
e6aab1a… drh 985 blob_init(&couldbe,0,0);
512905e… mgagnon 986 dispatch_matching_names(zCmdName, CMDFLAG_COMMAND, &couldbe);
796dcfe… drh 987 fossil_print("%s: ambiguous command prefix: %s\n"
796dcfe… drh 988 "%s: could be any of:%s\n"
796dcfe… drh 989 "%s: use \"help\" for more information\n",
b6a7e52… drh 990 g.argv[0], zCmdName, g.argv[0], blob_str(&couldbe), g.argv[0]);
796dcfe… drh 991 fossil_exit(1);
796dcfe… drh 992 }
39bef92… stephan 993 #ifdef FOSSIL_ENABLE_JSON
39bef92… stephan 994 else if( rc==0 && strcmp("json",pCmd->zName)==0 ){
39bef92… stephan 995 g.json.isJsonMode = 1;
39bef92… stephan 996 }else{
39bef92… stephan 997 assert(!g.json.isJsonMode && "JSON-mode misconfiguration.");
39bef92… stephan 998 }
39bef92… stephan 999 #endif
796dcfe… drh 1000 atexit( fossil_atexit );
1c528d3… mistachkin 1001 #ifdef FOSSIL_ENABLE_TH1_HOOKS
1c528d3… mistachkin 1002 /*
1c528d3… mistachkin 1003 ** The TH1 return codes from the hook will be handled as follows:
1c528d3… mistachkin 1004 **
1c528d3… mistachkin 1005 ** TH_OK: The xFunc() and the TH1 notification will both be executed.
1c528d3… mistachkin 1006 **
36f0e47… mistachkin 1007 ** TH_ERROR: The xFunc() will be skipped, the TH1 notification will be
1c528d3… mistachkin 1008 ** skipped. If the xFunc() is being hooked, the error message
1c528d3… mistachkin 1009 ** will be emitted.
1c528d3… mistachkin 1010 **
1c528d3… mistachkin 1011 ** TH_BREAK: The xFunc() and the TH1 notification will both be skipped.
1c528d3… mistachkin 1012 **
1c528d3… mistachkin 1013 ** TH_RETURN: The xFunc() will be executed, the TH1 notification will be
1c528d3… mistachkin 1014 ** skipped.
1c528d3… mistachkin 1015 **
1c528d3… mistachkin 1016 ** TH_CONTINUE: The xFunc() will be skipped, the TH1 notification will be
1c528d3… mistachkin 1017 ** executed.
1c528d3… mistachkin 1018 */
1c528d3… mistachkin 1019 if( !g.isHTTP && !g.fNoThHook ){
9595cfc… mistachkin 1020 rc = Th_CommandHook(pCmd->zName, pCmd->eCmdFlags);
1c528d3… mistachkin 1021 }else{
1c528d3… mistachkin 1022 rc = TH_OK;
1c528d3… mistachkin 1023 }
1c528d3… mistachkin 1024 if( rc==TH_OK || rc==TH_RETURN || rc==TH_CONTINUE ){
1c528d3… mistachkin 1025 if( rc==TH_OK || rc==TH_RETURN ){
1c528d3… mistachkin 1026 #endif
48c06b0… drh 1027 g.zPhase = pCmd->zName;
e6aab1a… drh 1028 pCmd->xFunc();
48c06b0… drh 1029 g.zPhase = "shutdown";
1c528d3… mistachkin 1030 #ifdef FOSSIL_ENABLE_TH1_HOOKS
1c528d3… mistachkin 1031 }
1c528d3… mistachkin 1032 if( !g.isHTTP && !g.fNoThHook && (rc==TH_OK || rc==TH_CONTINUE) ){
9595cfc… mistachkin 1033 Th_CommandNotify(pCmd->zName, pCmd->eCmdFlags);
1c528d3… mistachkin 1034 }
1c528d3… mistachkin 1035 }
1c528d3… mistachkin 1036 #endif
8fa8fe8… drh 1037 fossil_exit(g.iResultCode);
796dcfe… drh 1038 /*NOT_REACHED*/
796dcfe… drh 1039 return 0;
dbda8d6… drh 1040 }
dbda8d6… drh 1041
dbda8d6… drh 1042 /*
dbda8d6… drh 1043 ** Print a usage comment and quit
dbda8d6… drh 1044 */
dbda8d6… drh 1045 void usage(const char *zFormat){
99fcc43… drh 1046 fossil_fatal("Usage: %s %s %s", g.argv[0], g.argv[1], zFormat);
dbda8d6… drh 1047 }
dbda8d6… drh 1048
dbda8d6… drh 1049 /*
dbda8d6… drh 1050 ** Remove n elements from g.argv beginning with the i-th element.
dbda8d6… drh 1051 */
bb0cedb… drh 1052 static void remove_from_argv(int i, int n){
5bec7a6… drh 1053 memmove(&g.argv[i], &g.argv[i+n], sizeof(g.argv[i])*(g.argc-i-n));
5bec7a6… drh 1054 g.argc -= n;
dbda8d6… drh 1055 }
dbda8d6… drh 1056
dbda8d6… drh 1057
dbda8d6… drh 1058 /*
5cca464… stephan 1059 ** Look for a command-line option. If present, remove it from the
5cca464… stephan 1060 ** argument list and return a pointer to either the flag's name (if
5cca464… stephan 1061 ** hasArg==0), sans leading - or --, or its value (if hasArg==1).
5cca464… stephan 1062 ** Return NULL if the flag is not found.
5cca464… stephan 1063 **
5cca464… stephan 1064 ** zLong is the "long" form of the flag and zShort is the
5cca464… stephan 1065 ** short/abbreviated form (typically a single letter, but it may be
5cca464… stephan 1066 ** longer). zLong must not be NULL, but zShort may be.
dbda8d6… drh 1067 **
dbda8d6… drh 1068 ** hasArg==0 means the option is a flag. It is either present or not.
5cca464… stephan 1069 ** hasArg==1 means the option has an argument, in which case a pointer
5cca464… stephan 1070 ** to the argument's value is returned. For zLong, a flag value (if
5cca464… stephan 1071 ** hasValue==1) may either be in the form (--flag=value) or (--flag
5cca464… stephan 1072 ** value). For zShort, only the latter form is accepted.
5cca464… stephan 1073 **
5cca464… stephan 1074 ** If a standalone argument of "--" is encountered in the argument
5cca464… stephan 1075 ** list while searching for the given flag(s), this routine stops
5cca464… stephan 1076 ** searching and NULL is returned.
dbda8d6… drh 1077 */
dbda8d6… drh 1078 const char *find_option(const char *zLong, const char *zShort, int hasArg){
dbda8d6… drh 1079 int i;
be75813… drh 1080 int nLong;
dbda8d6… drh 1081 const char *zReturn = 0;
dbda8d6… drh 1082 assert( hasArg==0 || hasArg==1 );
be75813… drh 1083 nLong = strlen(zLong);
a00888f… drh 1084 for(i=1; i<g.argc; i++){
d63f87c… drh 1085 char *z;
d63f87c… drh 1086 z = g.argv[i];
dbda8d6… drh 1087 if( z[0]!='-' ) continue;
dbda8d6… drh 1088 z++;
dbda8d6… drh 1089 if( z[0]=='-' ){
dbda8d6… drh 1090 if( z[1]==0 ){
5cca464… stephan 1091 /* Stop processing at "--" without consuming it.
5cca464… stephan 1092 verify_all_options() will consume this flag. */
dbda8d6… drh 1093 break;
dbda8d6… drh 1094 }
dbda8d6… drh 1095 z++;
dbda8d6… drh 1096 }
be75813… drh 1097 if( strncmp(z,zLong,nLong)==0 ){
be75813… drh 1098 if( hasArg && z[nLong]=='=' ){
be75813… drh 1099 zReturn = &z[nLong+1];
be75813… drh 1100 remove_from_argv(i, 1);
be75813… drh 1101 break;
be75813… drh 1102 }else if( z[nLong]==0 ){
ad0b5f0… preben 1103 if( i+hasArg >= g.argc ) break;
be75813… drh 1104 zReturn = g.argv[i+hasArg];
be75813… drh 1105 remove_from_argv(i, 1+hasArg);
be75813… drh 1106 break;
be75813… drh 1107 }
31c52c7… drh 1108 }else if( fossil_strcmp(z,zShort)==0 ){
ad0b5f0… preben 1109 if( i+hasArg >= g.argc ) break;
dbda8d6… drh 1110 zReturn = g.argv[i+hasArg];
dbda8d6… drh 1111 remove_from_argv(i, 1+hasArg);
dbda8d6… drh 1112 break;
dbda8d6… drh 1113 }
dbda8d6… drh 1114 }
dbda8d6… drh 1115 return zReturn;
2655a2d… drh 1116 }
2655a2d… drh 1117
2655a2d… drh 1118 /* Return true if zOption exists in the command-line arguments,
2655a2d… drh 1119 ** but do not remove it from the list or otherwise process it.
2655a2d… drh 1120 */
2655a2d… drh 1121 int has_option(const char *zOption){
2655a2d… drh 1122 int i;
2655a2d… drh 1123 int n = (int)strlen(zOption);
2655a2d… drh 1124 for(i=1; i<g.argc; i++){
2655a2d… drh 1125 char *z = g.argv[i];
2655a2d… drh 1126 if( z[0]!='-' ) continue;
2655a2d… drh 1127 z++;
5cca464… stephan 1128 if( z[0]=='-' ){
5cca464… stephan 1129 if( z[1]==0 ){
5cca464… stephan 1130 /* Stop processing at "--" */
5cca464… stephan 1131 break;
5cca464… stephan 1132 }
5cca464… stephan 1133 z++;
5cca464… stephan 1134 }
2655a2d… drh 1135 if( strncmp(z,zOption,n)==0 && (z[n]==0 || z[n]=='=') ) return 1;
2655a2d… drh 1136 }
2655a2d… drh 1137 return 0;
a6fd491… jan.nijtmans 1138 }
a6fd491… jan.nijtmans 1139
c73c95c… andybradford 1140 /*
a6fd491… jan.nijtmans 1141 ** Look for multiple occurrences of a command-line option with the
c73c95c… andybradford 1142 ** corresponding argument.
c73c95c… andybradford 1143 **
c73c95c… andybradford 1144 ** Return a malloc allocated array of pointers to the arguments.
c73c95c… andybradford 1145 **
c73c95c… andybradford 1146 ** pnUsedArgs is used to store the number of matched arguments.
c73c95c… andybradford 1147 **
5cca464… stephan 1148 ** Caller is responsible for freeing allocated memory by passing the
5cca464… stephan 1149 ** head of the array (not each entry) to fossil_free(). (The
5cca464… stephan 1150 ** individual entries have the same lifetime as values returned from
5cca464… stephan 1151 ** find_option().)
c73c95c… andybradford 1152 */
c73c95c… andybradford 1153 const char **find_repeatable_option(
c73c95c… andybradford 1154 const char *zLong,
c73c95c… andybradford 1155 const char *zShort,
c73c95c… andybradford 1156 int *pnUsedArgs
c73c95c… andybradford 1157 ){
c73c95c… andybradford 1158 const char *zOption;
c73c95c… andybradford 1159 const char **pzArgs = 0;
c73c95c… andybradford 1160 int nAllocArgs = 0;
c73c95c… andybradford 1161 int nUsedArgs = 0;
c73c95c… andybradford 1162
c64989d… drh 1163 while( (zOption = find_option(zLong, zShort, 1))!=0 ){
c73c95c… andybradford 1164 if( pzArgs==0 && nAllocArgs==0 ){
c73c95c… andybradford 1165 nAllocArgs = 1;
c73c95c… andybradford 1166 pzArgs = fossil_malloc( nAllocArgs*sizeof(pzArgs[0]) );
c73c95c… andybradford 1167 }else if( nAllocArgs<=nUsedArgs ){
c73c95c… andybradford 1168 nAllocArgs = nAllocArgs*2;
52e3d9d… mistachkin 1169 pzArgs = fossil_realloc( (void *)pzArgs, nAllocArgs*sizeof(pzArgs[0]) );
c73c95c… andybradford 1170 }
c73c95c… andybradford 1171 pzArgs[nUsedArgs++] = zOption;
c73c95c… andybradford 1172 }
c73c95c… andybradford 1173 *pnUsedArgs = nUsedArgs;
c73c95c… andybradford 1174 return pzArgs;
912fce2… mistachkin 1175 }
912fce2… mistachkin 1176
912fce2… mistachkin 1177 /*
912fce2… mistachkin 1178 ** Look for a repository command-line option. If present, [re-]cache it in
912fce2… mistachkin 1179 ** the global state and return the new pointer, freeing any previous value.
912fce2… mistachkin 1180 ** If absent and there is no cached value, return NULL.
912fce2… mistachkin 1181 */
912fce2… mistachkin 1182 const char *find_repository_option(){
912fce2… mistachkin 1183 const char *zRepository = find_option("repository", "R", 1);
912fce2… mistachkin 1184 if( zRepository ){
912fce2… mistachkin 1185 if( g.zRepositoryOption ) fossil_free(g.zRepositoryOption);
4c3e172… danield 1186 g.zRepositoryOption = fossil_strdup(zRepository);
912fce2… mistachkin 1187 }
912fce2… mistachkin 1188 return g.zRepositoryOption;
912fce2… mistachkin 1189 }
912fce2… mistachkin 1190
6aff11f… drh 1191 /*
da4f0c2… aku 1192 ** Verify that there are no unprocessed command-line options. If
6aff11f… drh 1193 ** Any remaining command-line argument begins with "-" print
6aff11f… drh 1194 ** an error message and quit.
5cca464… stephan 1195 **
5cca464… stephan 1196 ** Exception: if "--" is encountered, it is consumed from the argument
5cca464… stephan 1197 ** list and this function immediately returns. The effect is to treat
5cca464… stephan 1198 ** all arguments after "--" as non-flags (conventionally used to
5cca464… stephan 1199 ** enable passing-in of filenames which start with a dash).
5cca464… stephan 1200 **
5cca464… stephan 1201 ** This function must normally only be called one time per app
e2bdc10… danield 1202 ** invocation. The exception is commands which process their
5cca464… stephan 1203 ** arguments, call this to confirm that there are no extraneous flags,
5cca464… stephan 1204 ** then modify the arguments list for forwarding to another
5cca464… stephan 1205 ** (sub)command (which itself will call this to confirm its own
5cca464… stephan 1206 ** arguments).
6aff11f… drh 1207 */
6aff11f… drh 1208 void verify_all_options(void){
6aff11f… drh 1209 int i;
6aff11f… drh 1210 for(i=1; i<g.argc; i++){
5cca464… stephan 1211 const char * arg = g.argv[i];
5cca464… stephan 1212 if( arg[0]=='-' ){
5cca464… stephan 1213 if( arg[1]=='-' && arg[2]==0 ){
5cca464… stephan 1214 /* Remove "--" from the list and treat all following
5cca464… stephan 1215 ** arguments as non-flags. */
5cca464… stephan 1216 remove_from_argv(i, 1);
5cca464… stephan 1217 break;
5cca464… stephan 1218 }else if( arg[1]!=0 ){
5cca464… stephan 1219 fossil_fatal(
5cca464… stephan 1220 "unrecognized command-line option or missing argument: %s",
5cca464… stephan 1221 arg);
5cca464… stephan 1222 }
e6aab1a… drh 1223 }
e6aab1a… drh 1224 }
e6aab1a… drh 1225 }
c0242ad… mistachkin 1226
c0242ad… mistachkin 1227 /*
c0242ad… mistachkin 1228 ** This function returns a human readable version string.
c0242ad… mistachkin 1229 */
c0242ad… mistachkin 1230 const char *get_version(){
c0242ad… mistachkin 1231 static const char version[] = RELEASE_VERSION " " MANIFEST_VERSION " "
c0242ad… mistachkin 1232 MANIFEST_DATE " UTC";
c0242ad… mistachkin 1233 return version;
f7cd247… mistachkin 1234 }
f7cd247… mistachkin 1235
f7cd247… mistachkin 1236 /*
f7cd247… mistachkin 1237 ** This function populates a blob with version information. It is used by
f7cd247… mistachkin 1238 ** the "version" command and "test-version" web page. It assumes the blob
f7cd247… mistachkin 1239 ** passed to it is uninitialized; otherwise, it will leak memory.
f7cd247… mistachkin 1240 */
1da8afd… drh 1241 void fossil_version_blob(
f7cd247… mistachkin 1242 Blob *pOut, /* Write the manifest here */
bbbd7ef… drh 1243 int eVerbose /* 0: brief. 1: more text, 2: lots of text */
f7cd247… mistachkin 1244 ){
f7cd247… mistachkin 1245 #if defined(FOSSIL_ENABLE_TCL)
f7cd247… mistachkin 1246 int rc;
f7cd247… mistachkin 1247 const char *zRc;
f7cd247… mistachkin 1248 #endif
9c97d23… drh 1249 Stmt q;
d5ca538… mistachkin 1250 size_t pageSize = 0;
f7cd247… mistachkin 1251 blob_zero(pOut);
f7cd247… mistachkin 1252 blob_appendf(pOut, "This is fossil version %s\n", get_version());
bbbd7ef… drh 1253 if( eVerbose<=0 ) return;
bbbd7ef… drh 1254
f7cd247… mistachkin 1255 blob_appendf(pOut, "Compiled on %s %s using %s (%d-bit)\n",
f7cd247… mistachkin 1256 __DATE__, __TIME__, COMPILER_NAME, sizeof(void*)*8);
bbbd7ef… drh 1257 blob_appendf(pOut, "SQLite %s %.30s\n", sqlite3_libversion(),
bbbd7ef… drh 1258 sqlite3_sourceid());
bbbd7ef… drh 1259 #if defined(FOSSIL_ENABLE_SSL)
bbbd7ef… drh 1260 blob_appendf(pOut, "SSL (%s)\n", SSLeay_version(SSLEAY_VERSION));
bbbd7ef… drh 1261 #endif
bbbd7ef… drh 1262 blob_appendf(pOut, "zlib %s, loaded %s\n", ZLIB_VERSION, zlibVersion());
bbbd7ef… drh 1263 #if defined(FOSSIL_HAVE_FUSEFS)
bbbd7ef… drh 1264 blob_appendf(pOut, "libfuse %s, loaded %s\n", fusefs_inc_version(),
bbbd7ef… drh 1265 fusefs_lib_version());
bbbd7ef… drh 1266 #endif
bbbd7ef… drh 1267 #if defined(FOSSIL_ENABLE_TCL)
bbbd7ef… drh 1268 Th_FossilInit(TH_INIT_DEFAULT | TH_INIT_FORCE_TCL);
bbbd7ef… drh 1269 rc = Th_Eval(g.interp, 0, "tclInvoke info patchlevel", -1);
bbbd7ef… drh 1270 zRc = Th_ReturnCodeName(rc, 0);
bbbd7ef… drh 1271 blob_appendf(pOut, "TCL (Tcl %s, loaded %s: %s)\n",
bbbd7ef… drh 1272 TCL_PATCH_LEVEL, zRc, Th_GetResult(g.interp, 0)
bbbd7ef… drh 1273 );
bbbd7ef… drh 1274 #endif
bbbd7ef… drh 1275 if( eVerbose<=1 ) return;
bbbd7ef… drh 1276
d5ca538… mistachkin 1277 blob_appendf(pOut, "Schema version %s\n", AUX_SCHEMA_MAX);
d5ca538… mistachkin 1278 fossil_get_page_size(&pageSize);
d5ca538… mistachkin 1279 blob_appendf(pOut, "Detected memory page size is %lu bytes\n",
d5ca538… mistachkin 1280 (unsigned long)pageSize);
d5ca538… mistachkin 1281 #if FOSSIL_HARDENED_SHA1
d5ca538… mistachkin 1282 blob_appendf(pOut, "hardened-SHA1 by Marc Stevens and Dan Shumow\n");
d3a46b2… drh 1283 #endif
3192b13… drh 1284 #if defined(FOSSIL_DEBUG)
3192b13… drh 1285 blob_append(pOut, "FOSSIL_DEBUG\n", -1);
3192b13… drh 1286 #endif
d3a46b2… drh 1287 #if defined(FOSSIL_ENABLE_DELTA_CKSUM_TEST)
d3a46b2… drh 1288 blob_append(pOut, "FOSSIL_ENABLE_DELTA_CKSUM_TEST\n", -1);
3192b13… drh 1289 #endif
4a880a1… mistachkin 1290 blob_append(pOut, "FOSSIL_ENABLE_LEGACY_MV_RM\n", -1);
3192b13… drh 1291 #if defined(FOSSIL_ENABLE_EXEC_REL_PATHS)
3192b13… drh 1292 blob_append(pOut, "FOSSIL_ENABLE_EXEC_REL_PATHS\n", -1);
3192b13… drh 1293 #endif
3192b13… drh 1294 #if defined(FOSSIL_ENABLE_TH1_DOCS)
3192b13… drh 1295 blob_append(pOut, "FOSSIL_ENABLE_TH1_DOCS\n", -1);
3192b13… drh 1296 #endif
3192b13… drh 1297 #if defined(FOSSIL_ENABLE_TH1_HOOKS)
3192b13… drh 1298 blob_append(pOut, "FOSSIL_ENABLE_TH1_HOOKS\n", -1);
3192b13… drh 1299 #endif
f7cd247… mistachkin 1300 #if defined(USE_TCL_STUBS)
f7cd247… mistachkin 1301 blob_append(pOut, "USE_TCL_STUBS\n", -1);
f7cd247… mistachkin 1302 #endif
f7cd247… mistachkin 1303 #if defined(FOSSIL_ENABLE_TCL_STUBS)
3192b13… drh 1304 blob_append(pOut, "FOSSIL_TCL_STUBS\n", -1);
f7cd247… mistachkin 1305 #endif
f7cd247… mistachkin 1306 #if defined(FOSSIL_ENABLE_TCL_PRIVATE_STUBS)
3192b13… drh 1307 blob_append(pOut, "FOSSIL_ENABLE_TCL_PRIVATE_STUBS\n", -1);
f7cd247… mistachkin 1308 #endif
f7cd247… mistachkin 1309 #if defined(FOSSIL_ENABLE_JSON)
f7cd247… mistachkin 1310 blob_appendf(pOut, "JSON (API %s)\n", FOSSIL_JSON_API_VERSION);
f7cd247… mistachkin 1311 #endif
750879e… mistachkin 1312 blob_append(pOut, "MARKDOWN\n", -1);
f7cd247… mistachkin 1313 #if defined(BROKEN_MINGW_CMDLINE)
f7cd247… mistachkin 1314 blob_append(pOut, "MBCS_COMMAND_LINE\n", -1);
f7cd247… mistachkin 1315 #else
f7cd247… mistachkin 1316 blob_append(pOut, "UNICODE_COMMAND_LINE\n", -1);
f7cd247… mistachkin 1317 #endif
f7cd247… mistachkin 1318 #if defined(FOSSIL_DYNAMIC_BUILD)
28c7a35… drh 1319 blob_append(pOut, "FOSSIL_DYNAMIC_BUILD\n", -1);
f7cd247… mistachkin 1320 #else
28c7a35… drh 1321 blob_append(pOut, "FOSSIL_STATIC_BUILD\n", -1);
28c7a35… drh 1322 #endif
4471e93… drh 1323 #if defined(HAVE_PLEDGE)
4471e93… drh 1324 blob_append(pOut, "HAVE_PLEDGE\n", -1);
4471e93… drh 1325 #endif
d5ca538… mistachkin 1326 #if defined(USE_MMAN_H)
d5ca538… mistachkin 1327 blob_append(pOut, "USE_MMAN_H\n", -1);
d5ca538… mistachkin 1328 #endif
9129f6f… mistachkin 1329 #if defined(USE_SEE)
f41cf03… mistachkin 1330 blob_appendf(pOut, "USE_SEE (%s)\n",
f41cf03… mistachkin 1331 db_have_saved_encryption_key() ? "SET" : "UNSET");
3192b13… drh 1332 #endif
3192b13… drh 1333 #if defined(FOSSIL_ALLOW_OUT_OF_ORDER_DATES)
3192b13… drh 1334 blob_append(pOut, "FOSSIL_ALLOW_OUT_OF_ORDER_DATES\n");
f7cd247… mistachkin 1335 #endif
bbbd7ef… drh 1336
9c97d23… drh 1337 if( g.db==0 ) sqlite3_open(":memory:", &g.db);
9c97d23… drh 1338 db_prepare(&q,
042d6a7… jan.nijtmans 1339 "pragma compile_options");
9c97d23… drh 1340 while( db_step(&q)==SQLITE_ROW ){
042d6a7… jan.nijtmans 1341 const char *text = db_column_text(&q, 0);
042d6a7… jan.nijtmans 1342 if( strncmp(text, "COMPILER", 8) ){
042d6a7… jan.nijtmans 1343 blob_appendf(pOut, "SQLITE_%s\n", text);
042d6a7… jan.nijtmans 1344 }
9c97d23… drh 1345 }
9c97d23… drh 1346 db_finalize(&q);
1311841… jan.nijtmans 1347 }
1311841… jan.nijtmans 1348
1311841… jan.nijtmans 1349 /*
1311841… jan.nijtmans 1350 ** This function returns the user-agent string for Fossil, for
1311841… jan.nijtmans 1351 ** use in HTTP(S) requests.
1311841… jan.nijtmans 1352 */
1311841… jan.nijtmans 1353 const char *get_user_agent(){
1311841… jan.nijtmans 1354 static const char version[] = "Fossil/" RELEASE_VERSION " (" MANIFEST_DATE
1311841… jan.nijtmans 1355 " " MANIFEST_VERSION ")";
1311841… jan.nijtmans 1356 return version;
1311841… jan.nijtmans 1357 }
1311841… jan.nijtmans 1358
f7cd247… mistachkin 1359
1311841… jan.nijtmans 1360 /*
a1f727b… drh 1361 ** COMMAND: version
a1f727b… drh 1362 **
ba7b431… stephan 1363 ** Usage: %fossil version ?-v|--verbose?
a1f727b… drh 1364 **
a1f727b… drh 1365 ** Print the source code version number for the fossil executable.
1be15a5… stephan 1366 ** If the verbose option is specified, additional details will
1be15a5… stephan 1367 ** be output about what optional features this binary was compiled
bbbd7ef… drh 1368 ** with.
bbbd7ef… drh 1369 **
bbbd7ef… drh 1370 ** Repeat the -v option or use -vv for even more information.
a1f727b… drh 1371 */
a1f727b… drh 1372 void version_cmd(void){
f7cd247… mistachkin 1373 Blob versionInfo;
bbbd7ef… drh 1374 int verboseFlag = 0;
bbbd7ef… drh 1375
bbbd7ef… drh 1376 while( find_option("verbose","v",0)!=0 ) verboseFlag++;
bbbd7ef… drh 1377 while( find_option("vv",0,0)!=0 ) verboseFlag += 2;
d4acf5c… jan.nijtmans 1378
74ac0c9… drh 1379 /* We should be done with options.. */
74ac0c9… drh 1380 verify_all_options();
1da8afd… drh 1381 fossil_version_blob(&versionInfo, verboseFlag);
f7cd247… mistachkin 1382 fossil_print("%s", blob_str(&versionInfo));
d564056… drh 1383 blob_reset(&versionInfo);
f7cd247… mistachkin 1384 }
f7cd247… mistachkin 1385
f7cd247… mistachkin 1386
f7cd247… mistachkin 1387 /*
286effd… drh 1388 ** WEBPAGE: version
f7cd247… mistachkin 1389 **
f7cd247… mistachkin 1390 ** Show the version information for Fossil.
f7cd247… mistachkin 1391 **
f7cd247… mistachkin 1392 ** Query parameters:
f7cd247… mistachkin 1393 **
9c97d23… drh 1394 ** verbose Show details
f7cd247… mistachkin 1395 */
f7cd247… mistachkin 1396 void test_version_page(void){
f7cd247… mistachkin 1397 Blob versionInfo;
9c97d23… drh 1398 int verboseFlag;
f7cd247… mistachkin 1399
83b2acb… mistachkin 1400 login_check_credentials();
83b2acb… mistachkin 1401 if( !g.perm.Read ){ login_needed(g.anon.Read); return; }
bbbd7ef… drh 1402 verboseFlag = P("verbose")!=0 ? 2 : 1;
f7cd247… mistachkin 1403 style_header("Version Information");
286effd… drh 1404 style_submenu_element("Stat", "stat");
1da8afd… drh 1405 fossil_version_blob(&versionInfo, verboseFlag);
9c97d23… drh 1406 @ <pre>
9c97d23… drh 1407 @ %h(blob_str(&versionInfo))
9c97d23… drh 1408 @ </pre>
112c713… drh 1409 style_finish_page();
e6aab1a… drh 1410 }
e6aab1a… drh 1411
e6aab1a… drh 1412
e6aab1a… drh 1413 /*
e2bdc10… danield 1414 ** Set the g.zBaseURL value to the full URL for the top level of
14c19fb… drh 1415 ** the fossil tree. Set g.zTop to g.zBaseURL without the
68c24b1… drh 1416 ** leading "http://" and the host and port.
ecb85f6… drh 1417 **
ecb85f6… drh 1418 ** The g.zBaseURL is normally set based on HTTP_HOST and SCRIPT_NAME
ecb85f6… drh 1419 ** environment variables. However, if zAltBase is not NULL then it
ecb85f6… drh 1420 ** is the argument to the --baseurl option command-line option and
ecb85f6… drh 1421 ** g.zBaseURL and g.zTop is set from that instead.
68c24b1… drh 1422 */
8f5399b… drh 1423 void set_base_url(const char *zAltBase){
68c24b1… drh 1424 int i;
122a31d… drh 1425 const char *zHost;
122a31d… drh 1426 const char *zMode;
122a31d… drh 1427 const char *zCur;
122a31d… drh 1428
122a31d… drh 1429 if( g.zBaseURL!=0 ) return;
ecb85f6… drh 1430 if( zAltBase ){
ecb85f6… drh 1431 int i, n, c;
4c3e172… danield 1432 g.zTop = g.zBaseURL = fossil_strdup(zAltBase);
9e816f0… drh 1433 i = (int)strlen(g.zBaseURL);
9e816f0… drh 1434 while( i>3 && g.zBaseURL[i-1]=='/' ){ i--; }
9e816f0… drh 1435 g.zBaseURL[i] = 0;
4110f52… mistachkin 1436 if( strncmp(g.zTop, "http://", 7)==0 ){
4110f52… mistachkin 1437 /* it is HTTP, replace prefix with HTTPS. */
4110f52… mistachkin 1438 g.zHttpsURL = mprintf("https://%s", &g.zTop[7]);
4110f52… mistachkin 1439 }else if( strncmp(g.zTop, "https://", 8)==0 ){
4110f52… mistachkin 1440 /* it is already HTTPS, use it. */
4c3e172… danield 1441 g.zHttpsURL = fossil_strdup(g.zTop);
4110f52… mistachkin 1442 }else{
99fcc43… drh 1443 fossil_fatal("argument to --baseurl should be 'http://host/path'"
ecb85f6… drh 1444 " or 'https://host/path'");
ecb85f6… drh 1445 }
ecb85f6… drh 1446 for(i=n=0; (c = g.zTop[i])!=0; i++){
ecb85f6… drh 1447 if( c=='/' ){
ecb85f6… drh 1448 n++;
ecb85f6… drh 1449 if( n==3 ){
ecb85f6… drh 1450 g.zTop += i;
ecb85f6… drh 1451 break;
ecb85f6… drh 1452 }
ecb85f6… drh 1453 }
ecb85f6… drh 1454 }
9e816f0… drh 1455 if( n==2 ) g.zTop = "";
ecb85f6… drh 1456 if( g.zTop==g.zBaseURL ){
99fcc43… drh 1457 fossil_fatal("argument to --baseurl should be 'http://host/path'"
ecb85f6… drh 1458 " or 'https://host/path'");
ecb85f6… drh 1459 }
ecb85f6… drh 1460 if( g.zTop[1]==0 ) g.zTop++;
0205148… drh 1461 }else{
7e93701… drh 1462 char *z;
add3f52… drh 1463 zMode = PD("HTTPS","off");
7e93701… drh 1464 zHost = PD("HTTP_HOST","");
7e93701… drh 1465 z = fossil_strdup(zHost);
7e93701… drh 1466 for(i=0; z[i]; i++){
7e93701… drh 1467 if( z[i]<='Z' && z[i]>='A' ) z[i] += 'a' - 'A';
7e93701… drh 1468 }
add3f52… drh 1469 if( fossil_strcmp(zMode,"on")==0 ){
add3f52… drh 1470 /* Remove trailing ":443" from the HOST, if any */
add3f52… drh 1471 if( i>4 && z[i-1]=='3' && z[i-2]=='4' && z[i-3]=='4' && z[i-4]==':' ){
add3f52… drh 1472 i -= 4;
add3f52… drh 1473 }
add3f52… drh 1474 }else{
add3f52… drh 1475 /* Remove trailing ":80" from the HOST */
add3f52… drh 1476 if( i>3 && z[i-1]=='0' && z[i-2]=='8' && z[i-3]==':' ) i -= 3;
275da70… danield 1477 }
7e93701… drh 1478 if( i && z[i-1]=='.' ) i--;
7e93701… drh 1479 z[i] = 0;
ecb85f6… drh 1480 zCur = PD("SCRIPT_NAME","/");
ecb85f6… drh 1481 i = strlen(zCur);
ecb85f6… drh 1482 while( i>0 && zCur[i-1]=='/' ) i--;
ecb85f6… drh 1483 if( fossil_stricmp(zMode,"on")==0 ){
7e93701… drh 1484 g.zBaseURL = mprintf("https://%s%.*s", z, i, zCur);
7e93701… drh 1485 g.zTop = &g.zBaseURL[8+strlen(z)];
a4e7b86… drh 1486 g.zHttpsURL = g.zBaseURL;
ecb85f6… drh 1487 }else{
7e93701… drh 1488 g.zBaseURL = mprintf("http://%s%.*s", z, i, zCur);
7e93701… drh 1489 g.zTop = &g.zBaseURL[7+strlen(z)];
7e93701… drh 1490 g.zHttpsURL = mprintf("https://%s%.*s", z, i, zCur);
f741baa… drh 1491 }
7e93701… drh 1492 fossil_free(z);
f741baa… drh 1493 }
a965221… drh 1494
a965221… drh 1495 /* Try to record the base URL as a CONFIG table entry with a name
a965221… drh 1496 ** of the form: "baseurl:BASE". This keeps a record of how the
a965221… drh 1497 ** the repository is used as a server, to help in answering questions
a965221… drh 1498 ** like "where is the CGI script that references this repository?"
a965221… drh 1499 **
a965221… drh 1500 ** This is just a logging hint. So don't worry if it cannot be done.
a965221… drh 1501 ** Don't try this if the repository database is not writable, for
a965221… drh 1502 ** example.
a965221… drh 1503 **
a965221… drh 1504 ** If g.useLocalauth is set, that (probably) means that we are running
a965221… drh 1505 ** "fossil ui" and there is no point in logging those cases either.
a965221… drh 1506 */
a965221… drh 1507 if( db_is_writeable("repository") && !g.useLocalauth ){
acb4397… drh 1508 int nBase = (int)strlen(g.zBaseURL);
acb4397… drh 1509 char *zBase = g.zBaseURL;
acb4397… drh 1510 if( g.nExtraURL>0 && g.nExtraURL<nBase-6 ){
acb4397… drh 1511 zBase = fossil_strndup(g.zBaseURL, nBase - g.nExtraURL);
acb4397… drh 1512 }
f741baa… drh 1513 db_unprotect(PROTECT_CONFIG);
acb4397… drh 1514 if( !db_exists("SELECT 1 FROM config WHERE name='baseurl:%q'", zBase)){
0205148… drh 1515 db_multi_exec("INSERT INTO config(name,value,mtime)"
acb4397… drh 1516 "VALUES('baseurl:%q',1,now())", zBase);
0205148… drh 1517 }else{
0205148… drh 1518 db_optional_sql("repository",
0205148… drh 1519 "REPLACE INTO config(name,value,mtime)"
acb4397… drh 1520 "VALUES('baseurl:%q',1,now())", zBase
0205148… drh 1521 );
0205148… drh 1522 }
f741baa… drh 1523 db_protect_pop();
acb4397… drh 1524 if( zBase!=g.zBaseURL ) fossil_free(zBase);
c7d6e33… drh 1525 }
68c24b1… drh 1526 }
68c24b1… drh 1527
68c24b1… drh 1528 /*
68c24b1… drh 1529 ** Send an HTTP redirect back to the designated Index Page.
68c24b1… drh 1530 */
86d2b4e… drh 1531 NORETURN void fossil_redirect_home(void){
1b6ec17… stephan 1532 /* In order for ?skin=... to work when visiting the site from
5feac63… stephan 1533 ** a typical external link, we have to process it here, as
1b6ec17… stephan 1534 ** that parameter gets lost during the redirect. We "could"
1b6ec17… stephan 1535 ** pass the whole query string along instead, but that seems
1b6ec17… stephan 1536 ** unnecessary. */
1862890… stephan 1537 if(cgi_setup_query_string() & 0x02){
1b6ec17… stephan 1538 cookie_render();
1b6ec17… stephan 1539 }
a40e8a0… drh 1540 cgi_redirectf("%R%s", db_get("index-page", "/index"));
14c19fb… drh 1541 }
14c19fb… drh 1542
14c19fb… drh 1543 /*
14c19fb… drh 1544 ** If running as root, chroot to the directory containing the
14c19fb… drh 1545 ** repository zRepo and then drop root privileges. Return the
14c19fb… drh 1546 ** new repository name.
14c19fb… drh 1547 **
6f92ad9… wyoung 1548 ** zRepo can be a directory. If so and if the repo name was saved
6f92ad9… wyoung 1549 ** to g.zRepositoryName before we were called, we canonicalize the
6f92ad9… wyoung 1550 ** two paths and check that one is the prefix of the other, else you
6f92ad9… wyoung 1551 ** won't be able to open the repo inside the jail. If it all works
6f92ad9… wyoung 1552 ** out, we return the "jailed" version of the repo name.
14c19fb… drh 1553 **
14c19fb… drh 1554 ** Assume the user-id and group-id of the repository, or if zRepo
14c19fb… drh 1555 ** is a directory, of that directory.
c5c9f16… drh 1556 **
c5c9f16… drh 1557 ** The noJail flag means that the chroot jail is not entered. But
d76a980… drh 1558 ** privileges are still lowered to that of the user-id and group-id
d76a980… drh 1559 ** of the repository file.
14c19fb… drh 1560 */
6f92ad9… wyoung 1561 static char *enter_chroot_jail(const char *zRepo, int noJail){
3564af0… drh 1562 #if !defined(_WIN32)
14c19fb… drh 1563 if( getuid()==0 ){
14c19fb… drh 1564 int i;
14c19fb… drh 1565 struct stat sStat;
14c19fb… drh 1566 Blob dir;
14c19fb… drh 1567 char *zDir;
effdada… drh 1568 size_t nDir;
e65162b… stephan 1569 if( g.db!=0 ){
e65162b… stephan 1570 db_close(1);
e65162b… stephan 1571 }
14c19fb… drh 1572
135ed93… drh 1573 file_canonical_name(zRepo, &dir, 0);
14c19fb… drh 1574 zDir = blob_str(&dir);
effdada… drh 1575 nDir = blob_size(&dir);
c5c9f16… drh 1576 if( !noJail ){
1772357… drh 1577 if( file_isdir(zDir, ExtFILE)==1 ){
effdada… drh 1578 /* Translate the repository name to the new root */
6f92ad9… wyoung 1579 if( g.zRepositoryName ){
6f92ad9… wyoung 1580 Blob repo;
6f92ad9… wyoung 1581 file_canonical_name(g.zRepositoryName, &repo, 0);
6f92ad9… wyoung 1582 zRepo = blob_str(&repo);
effdada… drh 1583 if( strncmp(zRepo, zDir, nDir)!=0 ){
6f92ad9… wyoung 1584 fossil_fatal("repo %s not under chroot dir %s", zRepo, zDir);
6f92ad9… wyoung 1585 }
effdada… drh 1586 zRepo += nDir;
6f92ad9… wyoung 1587 if( *zRepo == '\0' ) zRepo = "/";
6f92ad9… wyoung 1588 }else {
6f92ad9… wyoung 1589 zRepo = "/";
effdada… drh 1590 }
effdada… drh 1591 /* If a unix socket is defined, try to translate its name into
effdada… drh 1592 ** the new root so that it can be delete by atexit(). If unable,
effdada… drh 1593 ** just zero out the socket name. */
effdada… drh 1594 if( g.zSockName ){
effdada… drh 1595 if( strncmp(g.zSockName, zDir, nDir)==0
effdada… drh 1596 && g.zSockName[nDir]=='/'
effdada… drh 1597 ){
effdada… drh 1598 g.zSockName += nDir;
effdada… drh 1599 }else{
effdada… drh 1600 g.zSockName = 0;
effdada… drh 1601 }
e946211… wyoung 1602 }
e946211… wyoung 1603 if( file_chdir(zDir, 1) ){
e946211… wyoung 1604 fossil_panic("unable to chroot into %s", zDir);
c5c9f16… drh 1605 }
c5c9f16… drh 1606 }else{
c5c9f16… drh 1607 for(i=strlen(zDir)-1; i>0 && zDir[i]!='/'; i--){}
31c7bdb… larrybr 1608 if( zDir[i]!='/' ) fossil_fatal("bad repository name: %s", zRepo);
c5c9f16… drh 1609 if( i>0 ){
c5c9f16… drh 1610 zDir[i] = 0;
c5c9f16… drh 1611 if( file_chdir(zDir, 1) ){
31c7bdb… larrybr 1612 fossil_fatal("unable to chroot into %s", zDir);
c5c9f16… drh 1613 }
c5c9f16… drh 1614 zDir[i] = '/';
c5c9f16… drh 1615 }
c5c9f16… drh 1616 zRepo = &zDir[i];
c5c9f16… drh 1617 }
14c19fb… drh 1618 }
14c19fb… drh 1619 if( stat(zRepo, &sStat)!=0 ){
99fcc43… drh 1620 fossil_fatal("cannot stat() repository: %s", zRepo);
14c19fb… drh 1621 }
4d107b5… stephan 1622 i = setgid(sStat.st_gid);
4d107b5… stephan 1623 i = i || setuid(sStat.st_uid);
4d107b5… stephan 1624 if(i){
99fcc43… drh 1625 fossil_fatal("setgid/uid() failed with errno %d", errno);
4d107b5… stephan 1626 }
1772357… drh 1627 if( g.db==0 && file_isfile(zRepo, ExtFILE) ){
5e47d55… stephan 1628 db_open_repository(zRepo);
5e47d55… stephan 1629 }
5e47d55… stephan 1630 }
5e47d55… stephan 1631 #endif
6f92ad9… wyoung 1632 return (char*)zRepo; /* no longer const: always reassigned from blob_str() */
c670925… drh 1633 }
c670925… drh 1634
c670925… drh 1635 /*
c670925… drh 1636 ** Called whenever a crash is encountered while processing a webpage.
c670925… drh 1637 */
c670925… drh 1638 void sigsegv_handler(int x){
a8d220b… drh 1639 #if HAVE_BACKTRACE
a8d220b… drh 1640 void *array[20];
a8d220b… drh 1641 size_t size;
a8d220b… drh 1642 char **strings;
a8d220b… drh 1643 size_t i;
a8d220b… drh 1644 Blob out;
a8d220b… drh 1645 size = backtrace(array, sizeof(array)/sizeof(array[0]));
a8d220b… drh 1646 strings = backtrace_symbols(array, size);
a8d220b… drh 1647 blob_init(&out, 0, 0);
f858061… stephan 1648 blob_appendf(&out, "Segfault during %s in fossil %s",
f858061… stephan 1649 g.zPhase, MANIFEST_VERSION);
a8d220b… drh 1650 for(i=0; i<size; i++){
daaf9ee… drh 1651 size_t len;
daaf9ee… drh 1652 const char *z = strings[i];
daaf9ee… drh 1653 if( i==0 ) blob_appendf(&out, "\nBacktrace:");
daaf9ee… drh 1654 len = strlen(strings[i]);
daaf9ee… drh 1655 if( z[0]=='[' && z[len-1]==']' ){
daaf9ee… drh 1656 blob_appendf(&out, " %.*s", (int)(len-2), &z[1]);
daaf9ee… drh 1657 }else{
daaf9ee… drh 1658 blob_appendf(&out, " %s", z);
daaf9ee… drh 1659 }
a8d220b… drh 1660 }
a8d220b… drh 1661 fossil_panic("%s", blob_str(&out));
a8d220b… drh 1662 #else
f3cac52… stephan 1663 fossil_panic("Segfault during %s in fossil %s",
f3cac52… stephan 1664 g.zPhase, MANIFEST_VERSION);
a8d220b… drh 1665 #endif
83b171b… drh 1666 exit(1);
83b171b… drh 1667 }
83b171b… drh 1668
83b171b… drh 1669 /*
83b171b… drh 1670 ** Called if a server gets a SIGPIPE. This often happens when a client
83b171b… drh 1671 ** webbrowser opens a connection but never sends the HTTP request
83b171b… drh 1672 */
83b171b… drh 1673 void sigpipe_handler(int x){
83b171b… drh 1674 #ifndef _WIN32
83b171b… drh 1675 if( g.fAnyTrace ){
f525b6d… drh 1676 fprintf(stderr,"/***** sigpipe received by subprocess %d ****\n", getpid());
f525b6d… drh 1677 }
f525b6d… drh 1678 #endif
48c06b0… drh 1679 g.zPhase = "sigpipe shutdown";
f525b6d… drh 1680 db_panic_close();
f525b6d… drh 1681 exit(1);
4aba9ea… drh 1682 }
4aba9ea… drh 1683
4aba9ea… drh 1684 /*
4aba9ea… drh 1685 ** Return true if it is appropriate to redirect requests to HTTPS.
4aba9ea… drh 1686 **
4aba9ea… drh 1687 ** Redirect to https is appropriate if all of the above are true:
277437d… stephan 1688 ** (1) The redirect-to-https flag has a value of iLevel or greater.
4aba9ea… drh 1689 ** (2) The current connection is http, not https or ssh
4aba9ea… drh 1690 ** (3) The sslNotAvailable flag is clear
4aba9ea… drh 1691 */
4aba9ea… drh 1692 int fossil_wants_https(int iLevel){
4aba9ea… drh 1693 if( g.sslNotAvailable ) return 0;
4aba9ea… drh 1694 if( db_get_int("redirect-to-https",0)<iLevel ) return 0;
4aba9ea… drh 1695 if( P("HTTPS")!=0 ) return 0;
4aba9ea… drh 1696 return 1;
4aba9ea… drh 1697 }
4aba9ea… drh 1698
4aba9ea… drh 1699 /*
4aba9ea… drh 1700 ** Redirect to the equivalent HTTPS request if the current connection is
275da70… danield 1701 ** insecure and if the redirect-to-https flag greater than or equal to
4aba9ea… drh 1702 ** iLevel. iLevel is 1 for /login pages and 2 for every other page.
4aba9ea… drh 1703 */
4aba9ea… drh 1704 int fossil_redirect_to_https_if_needed(int iLevel){
4aba9ea… drh 1705 if( fossil_wants_https(iLevel) ){
4aba9ea… drh 1706 const char *zQS = P("QUERY_STRING");
6b2cc46… danield 1707 char *zURL = 0;
4aba9ea… drh 1708 if( zQS==0 || zQS[0]==0 ){
810842f… drh 1709 zURL = mprintf("%s%T", g.zHttpsURL, P("PATH_INFO"));
4aba9ea… drh 1710 }else if( zQS[0]!=0 ){
810842f… drh 1711 zURL = mprintf("%s%T?%s", g.zHttpsURL, P("PATH_INFO"), zQS);
4aba9ea… drh 1712 }
4aba9ea… drh 1713 cgi_redirect_with_status(zURL, 301, "Moved Permanently");
4aba9ea… drh 1714 return 1;
4aba9ea… drh 1715 }
4aba9ea… drh 1716 return 0;
de320cc… drh 1717 }
de320cc… drh 1718
de320cc… drh 1719 /*
de320cc… drh 1720 ** Send a 404 Not Found reply
de320cc… drh 1721 */
de320cc… drh 1722 void fossil_not_found_page(void){
de320cc… drh 1723 #ifdef FOSSIL_ENABLE_JSON
de320cc… drh 1724 if(g.json.isJsonMode){
de320cc… drh 1725 json_err(FSL_JSON_E_RESOURCE_NOT_FOUND,NULL,1);
de320cc… drh 1726 return;
de320cc… drh 1727 }
de320cc… drh 1728 #endif
de320cc… drh 1729 @ <html><head>
de320cc… drh 1730 @ <meta name="viewport" \
de320cc… drh 1731 @ content="width=device-width, initial-scale=1.0">
de320cc… drh 1732 @ </head><body>
de320cc… drh 1733 @ <h1>Not Found</h1>
de320cc… drh 1734 @ </body>
de320cc… drh 1735 cgi_set_status(404, "Not Found");
de320cc… drh 1736 cgi_reply();
b2ac218… stephan 1737 }
dbda8d6… drh 1738
dbda8d6… drh 1739 /*
dbda8d6… drh 1740 ** Preconditions:
dbda8d6… drh 1741 **
14c19fb… drh 1742 ** * Environment variables are set up according to the CGI standard.
14c19fb… drh 1743 **
14c19fb… drh 1744 ** If the repository is known, it has already been opened. If unknown,
14c19fb… drh 1745 ** then g.zRepositoryName holds the directory that contains the repository
14c19fb… drh 1746 ** and the actual repository is taken from the first element of PATH_INFO.
f668ff4… drh 1747 **
dbda8d6… drh 1748 ** Process the webpage specified by the PATH_INFO or REQUEST_URI
dbda8d6… drh 1749 ** environment variable.
2c8557c… drh 1750 **
d76a980… drh 1751 ** If the repository is not known, then a search is done through the
2c8557c… drh 1752 ** file hierarchy rooted at g.zRepositoryName for a suitable repository
2c8557c… drh 1753 ** with a name of $prefix.fossil, where $prefix is any prefix of PATH_INFO.
2c8557c… drh 1754 ** Or, if an ordinary file named $prefix is found, and $prefix matches
2c8557c… drh 1755 ** pFileGlob and $prefix does not match "*.fossil*" and the mimetype of
2c8557c… drh 1756 ** $prefix can be determined from its suffix, then the file $prefix is
2c8557c… drh 1757 ** returned as static text.
2c8557c… drh 1758 **
2c8557c… drh 1759 ** If no suitable webpage is found, try to redirect to zNotFound.
da4f0c2… aku 1760 */
87508e0… drh 1761 static void process_one_web_page(
87508e0… drh 1762 const char *zNotFound, /* Redirect here on a 404 if not NULL */
87508e0… drh 1763 Glob *pFileGlob, /* Deliver static files matching */
87508e0… drh 1764 int allowRepoList /* Send repo list for "/" URL */
87508e0… drh 1765 ){
239b4c1… drh 1766 const char *zPathInfo = PD("PATH_INFO", "");
da4f0c2… aku 1767 char *zPath = NULL;
c7c81df… drh 1768 int i;
e6aab1a… drh 1769 const CmdOrPage *pCmd = 0;
239b4c1… drh 1770 const char *zBase = g.zRepositoryName;
f8363db… drh 1771 int isReadonly = 0;
c670925… drh 1772
48c06b0… drh 1773 g.zPhase = "process_one_web_page";
c670925… drh 1774 #if !defined(_WIN32)
c670925… drh 1775 signal(SIGSEGV, sigsegv_handler);
c670925… drh 1776 #endif
f8363db… drh 1777
362a7b7… drh 1778 /* Decode %HH escapes in PATHINFO */
362a7b7… drh 1779 if( strchr(zPathInfo,'%') ){
362a7b7… drh 1780 char *z = fossil_strdup(zPathInfo);
362a7b7… drh 1781 dehttpize(z);
362a7b7… drh 1782 zPathInfo = z;
362a7b7… drh 1783 }
362a7b7… drh 1784
ea63a2d… drh 1785 /* Handle universal query parameters */
ea63a2d… drh 1786 if( PB("utc") ){
ea63a2d… drh 1787 g.fTimeFormat = 1;
ea63a2d… drh 1788 }else if( PB("localtime") ){
ea63a2d… drh 1789 g.fTimeFormat = 2;
ea63a2d… drh 1790 }
39bef92… stephan 1791 #ifdef FOSSIL_ENABLE_JSON
39bef92… stephan 1792 /*
39bef92… stephan 1793 ** Ensure that JSON mode is set up if we're visiting /json, to allow
39bef92… stephan 1794 ** us to customize some following behaviour (error handling and only
39bef92… stephan 1795 ** process JSON-mode POST data if we're actually in a /json
39bef92… stephan 1796 ** page). This is normally set up before this routine is called, but
39bef92… stephan 1797 ** it looks like the ssh_request_loop() approach to dispatching
39bef92… stephan 1798 ** might bypass that.
39bef92… stephan 1799 */
e7f13b8… stephan 1800 if( g.json.isJsonMode==0 && json_request_is_json_api(zPathInfo)!=0 ){
23e138e… stephan 1801 g.json.isJsonMode = 1;
e7f13b8… stephan 1802 json_bootstrap_early();
39bef92… stephan 1803 }
39bef92… stephan 1804 #endif
14c19fb… drh 1805 /* If the repository has not been opened already, then find the
14c19fb… drh 1806 ** repository based on the first element of PATH_INFO and open it.
14c19fb… drh 1807 */
14c19fb… drh 1808 if( !g.repositoryOpen ){
f41cf03… mistachkin 1809 char zBuf[24];
f41cf03… mistachkin 1810 const char *zRepoExt = ".fossil";
239b4c1… drh 1811 char *zRepo; /* Candidate repository name */
239b4c1… drh 1812 char *zToFree = 0; /* Malloced memory that needs to be freed */
239b4c1… drh 1813 const char *zCleanRepo; /* zRepo with surplus leading "/" removed */
239b4c1… drh 1814 const char *zOldScript = PD("SCRIPT_NAME", ""); /* Original SCRIPT_NAME */
239b4c1… drh 1815 char *zNewScript; /* Revised SCRIPT_NAME after processing */
239b4c1… drh 1816 int j, k; /* Loop variables */
239b4c1… drh 1817 i64 szFile; /* File size of the candidate repository */
d04fa1e… drh 1818
0fc9b5e… drh 1819 i = zPathInfo[0]!=0;
239b4c1… drh 1820 if( fossil_strcmp(g.zRepositoryName, "/")==0 ){
239b4c1… drh 1821 zBase++;
eb9b231… jan.nijtmans 1822 #if defined(_WIN32) || defined(__CYGWIN__)
239b4c1… drh 1823 if( sqlite3_strglob("/[a-zA-Z]:/*", zPathInfo)==0 ) i = 4;
239b4c1… drh 1824 #endif
239b4c1… drh 1825 }
d04fa1e… drh 1826 while( 1 ){
f6263bb… drh 1827 size_t nBase = strlen(zBase);
d04fa1e… drh 1828 while( zPathInfo[i] && zPathInfo[i]!='/' ){ i++; }
239b4c1… drh 1829
239b4c1… drh 1830 /* The candidate repository name is some prefix of the PATH_INFO
239b4c1… drh 1831 ** with ".fossil" appended */
f41cf03… mistachkin 1832 zRepo = zToFree = mprintf("%s%.*s%s",zBase,i,zPathInfo,zRepoExt);
239b4c1… drh 1833 if( g.fHttpTrace ){
239b4c1… drh 1834 @ <!-- Looking for repository named "%h(zRepo)" -->
239b4c1… drh 1835 fprintf(stderr, "# looking for repository named \"%s\"\n", zRepo);
239b4c1… drh 1836 }
239b4c1… drh 1837
87d6aa3… drh 1838
362a7b7… drh 1839 /* Restrictions on the URI for security:
362a7b7… drh 1840 **
1862890… stephan 1841 ** 1. Reject characters that are not ASCII alphanumerics,
362a7b7… drh 1842 ** "-", "_", ".", "/", or unicode (above ASCII).
362a7b7… drh 1843 ** In other words: No ASCII punctuation or control characters
362a7b7… drh 1844 ** other than "-", "_", "." and "/".
1862890… stephan 1845 ** 2. Exception to rule 1: Allow /X:/ where X is any ASCII
362a7b7… drh 1846 ** alphabetic character at the beginning of the name on windows.
362a7b7… drh 1847 ** 3. "-" may not occur immediately after "/"
362a7b7… drh 1848 ** 4. "." may not be adjacent to another "." or to "/"
362a7b7… drh 1849 **
362a7b7… drh 1850 ** Any character does not satisfy these constraints a Not Found
362a7b7… drh 1851 ** error is returned.
1862890… stephan 1852 */
c71e096… drh 1853 szFile = 0;
f6263bb… drh 1854 for(j=nBase+1, k=0; zRepo[j] && k<i-1; j++, k++){
87d6aa3… drh 1855 char c = zRepo[j];
362a7b7… drh 1856 if( c>='a' && c<='z' ) continue;
362a7b7… drh 1857 if( c>='A' && c<='Z' ) continue;
362a7b7… drh 1858 if( c>='0' && c<='9' ) continue;
362a7b7… drh 1859 if( (c&0x80)==0x80 ) continue;
eb9b231… jan.nijtmans 1860 #if defined(_WIN32) || defined(__CYGWIN__)
239b4c1… drh 1861 /* Allow names to begin with "/X:/" on windows */
239b4c1… drh 1862 if( c==':' && j==2 && sqlite3_strglob("/[a-zA-Z]:/*", zRepo)==0 ){
239b4c1… drh 1863 continue;
239b4c1… drh 1864 }
239b4c1… drh 1865 #endif
c71e096… drh 1866 if( c=='/' ) continue;
c71e096… drh 1867 if( c=='_' ) continue;
c71e096… drh 1868 if( c=='-' && zRepo[j-1]!='/' ) continue;
362a7b7… drh 1869 if( c=='.'
362a7b7… drh 1870 && zRepo[j-1]!='.' && zRepo[j-1]!='/'
362a7b7… drh 1871 && zRepo[j+1]!='.' && zRepo[j+1]!='/'
362a7b7… drh 1872 ){
f6263bb… drh 1873 continue;
f6263bb… drh 1874 }
53db40e… drh 1875 if( c=='.' && g.fAllowACME && j==(int)nBase+1
f6263bb… drh 1876 && strncmp(&zRepo[j-1],"/.well-known/",12)==0
f6263bb… drh 1877 ){
f6263bb… drh 1878 /* We allow .well-known as the top-level directory for ACME */
c71e096… drh 1879 continue;
c71e096… drh 1880 }
239b4c1… drh 1881 /* If we reach this point, it means that the request URI contains
239b4c1… drh 1882 ** an illegal character or character combination. Provoke a
239b4c1… drh 1883 ** "Not Found" error. */
c71e096… drh 1884 szFile = 1;
239b4c1… drh 1885 if( g.fHttpTrace ){
239b4c1… drh 1886 @ <!-- Unsafe pathname rejected: "%h(zRepo)" -->
239b4c1… drh 1887 fprintf(stderr, "# unsafe pathname rejected: %s\n", zRepo);
239b4c1… drh 1888 }
c71e096… drh 1889 break;
c71e096… drh 1890 }
239b4c1… drh 1891
239b4c1… drh 1892 /* Check to see if a file name zRepo exists. If a file named zRepo
239b4c1… drh 1893 ** does not exist, szFile will become -1. If the file does exist,
239b4c1… drh 1894 ** then szFile will become zero (for an empty file) or positive.
239b4c1… drh 1895 ** Special case: Assume any file with a basename of ".fossil" does
239b4c1… drh 1896 ** not exist.
239b4c1… drh 1897 */
239b4c1… drh 1898 zCleanRepo = file_cleanup_fullpath(zRepo);
63fc62d… drh 1899 if( szFile==0 && sqlite3_strglob("*/.fossil",zRepo)!=0 ){
1772357… drh 1900 szFile = file_size(zCleanRepo, ExtFILE);
dc21949… drh 1901 if( szFile>0 && !file_isfile(zCleanRepo, ExtFILE) ){
dc21949… drh 1902 /* Only let szFile be non-negative if zCleanRepo really is a file
dc21949… drh 1903 ** and not a directory or some other filesystem object. */
dc21949… drh 1904 szFile = -1;
dc21949… drh 1905 }
239b4c1… drh 1906 if( g.fHttpTrace ){
6828868… jan.nijtmans 1907 sqlite3_snprintf(sizeof(zBuf), zBuf, "%lld", szFile);
6828868… jan.nijtmans 1908 @ <!-- file_size(%h(zCleanRepo)) is %s(zBuf) -->
6828868… jan.nijtmans 1909 fprintf(stderr, "# file_size(%s) = %s\n", zCleanRepo, zBuf);
3eb0594… drh 1910 }
87d6aa3… drh 1911 }
239b4c1… drh 1912
239b4c1… drh 1913 /* If no file named by zRepo exists, remove the added ".fossil" suffix
239b4c1… drh 1914 ** and check to see if there is a file or directory with the same
239b4c1… drh 1915 ** name as the raw PATH_INFO text.
239b4c1… drh 1916 */
ac8e9e5… drh 1917 if( szFile<0 && i>0 ){
4a5e972… drh 1918 const char *zMimetype;
f41cf03… mistachkin 1919 assert( file_is_repository_extension(&zRepo[j]) );
239b4c1… drh 1920 zRepo[j] = 0; /* Remove the ".fossil" suffix */
239b4c1… drh 1921
239b4c1… drh 1922 /* The PATH_INFO prefix seen so far is a valid directory.
239b4c1… drh 1923 ** Continue the loop with the next element of the PATH_INFO */
1772357… drh 1924 if( zPathInfo[i]=='/' && file_isdir(zCleanRepo, ExtFILE)==1 ){
d04fa1e… drh 1925 fossil_free(zToFree);
d04fa1e… drh 1926 i++;
d04fa1e… drh 1927 continue;
d04fa1e… drh 1928 }
239b4c1… drh 1929
239b4c1… drh 1930 /* If zRepo is the name of an ordinary file that matches the
239b4c1… drh 1931 ** "--file GLOB" pattern, then the CGI reply is the text of
239b4c1… drh 1932 ** of the file.
239b4c1… drh 1933 **
239b4c1… drh 1934 ** For safety, do not allow any file whose name contains ".fossil"
239b4c1… drh 1935 ** to be returned this way, to prevent complete repositories from
239b4c1… drh 1936 ** being delivered accidently. This is not intended to be a
239b4c1… drh 1937 ** general-purpose web server. The "--file GLOB" mechanism is
239b4c1… drh 1938 ** designed to allow the delivery of a few static images or HTML
239b4c1… drh 1939 ** pages.
239b4c1… drh 1940 */
2c8557c… drh 1941 if( pFileGlob!=0
1772357… drh 1942 && file_isfile(zCleanRepo, ExtFILE)
f6263bb… drh 1943 && glob_match(pFileGlob, file_cleanup_fullpath(zRepo+nBase))
f41cf03… mistachkin 1944 && !file_contains_repository_extension(zRepo)
4a5e972… drh 1945 && (zMimetype = mimetype_from_name(zRepo))!=0
4a5e972… drh 1946 && strcmp(zMimetype, "application/x-fossil-artifact")!=0
4a5e972… drh 1947 ){
4a5e972… drh 1948 Blob content;
1772357… drh 1949 blob_read_from_file(&content, file_cleanup_fullpath(zRepo), ExtFILE);
4a5e972… drh 1950 cgi_set_content_type(zMimetype);
f6263bb… drh 1951 cgi_set_content(&content);
f6263bb… drh 1952 cgi_reply();
f6263bb… drh 1953 return;
f6263bb… drh 1954 }
f6263bb… drh 1955
f6263bb… drh 1956 /* In support of the ACME protocol, files under the .well-known/
f6263bb… drh 1957 ** directory is always accepted.
f6263bb… drh 1958 */
f6263bb… drh 1959 if( g.fAllowACME
f6263bb… drh 1960 && strncmp(&zRepo[nBase],"/.well-known/",12)==0
f6263bb… drh 1961 && file_isfile(zCleanRepo, ExtFILE)
f6263bb… drh 1962 ){
f6263bb… drh 1963 Blob content;
f6263bb… drh 1964 blob_read_from_file(&content, file_cleanup_fullpath(zRepo), ExtFILE);
f6263bb… drh 1965 cgi_set_content_type(mimetype_from_name(zRepo));
39bef92… stephan 1966 cgi_set_content(&content);
39bef92… stephan 1967 cgi_reply();
39bef92… stephan 1968 return;
39bef92… stephan 1969 }
d04fa1e… drh 1970 zRepo[j] = '.';
d04fa1e… drh 1971 }
d04fa1e… drh 1972
239b4c1… drh 1973 /* If we reach this point, it means that the search of the PATH_INFO
239b4c1… drh 1974 ** string is finished. Either zRepo contains the name of the
39bef92… stephan 1975 ** repository to be used, or else no repository could be found and
239b4c1… drh 1976 ** some kind of error response is required.
239b4c1… drh 1977 */
d04fa1e… drh 1978 if( szFile<1024 ){
f41cf03… mistachkin 1979 #if USE_SEE
f41cf03… mistachkin 1980 if( strcmp(zRepoExt,".fossil")==0 ){
f41cf03… mistachkin 1981 fossil_free(zToFree);
f41cf03… mistachkin 1982 zRepoExt = ".efossil";
f41cf03… mistachkin 1983 continue;
f41cf03… mistachkin 1984 }
f41cf03… mistachkin 1985 #endif
ecb85f6… drh 1986 set_base_url(0);
534b060… drh 1987 if( (zPathInfo[0]==0 || strcmp(zPathInfo,"/")==0)
d976b47… drh 1988 && allowRepoList
d976b47… drh 1989 && repo_list_page() ){
87508e0… drh 1990 /* Will return a list of repositories */
434f7e1… drh 1991 }else if( zNotFound ){
434f7e1… drh 1992 cgi_redirect(zNotFound);
d04fa1e… drh 1993 }else{
de320cc… drh 1994 fossil_not_found_page();
d04fa1e… drh 1995 }
d04fa1e… drh 1996 return;
d04fa1e… drh 1997 }
d04fa1e… drh 1998 break;
d04fa1e… drh 1999 }
239b4c1… drh 2000
239b4c1… drh 2001 /* Add the repository name (without the ".fossil" suffix) to the end
239b4c1… drh 2002 ** of SCRIPT_NAME and g.zTop and g.zBaseURL and remove the repository
239b4c1… drh 2003 ** name from the beginning of PATH_INFO.
239b4c1… drh 2004 */
14c19fb… drh 2005 zNewScript = mprintf("%s%.*s", zOldScript, i, zPathInfo);
a40e8a0… drh 2006 if( g.zTop ) g.zTop = mprintf("%R%.*s", i, zPathInfo);
239b4c1… drh 2007 if( g.zBaseURL ) g.zBaseURL = mprintf("%s%.*s", g.zBaseURL, i, zPathInfo);
14c19fb… drh 2008 cgi_replace_parameter("PATH_INFO", &zPathInfo[i+1]);
14c19fb… drh 2009 zPathInfo += i;
14c19fb… drh 2010 cgi_replace_parameter("SCRIPT_NAME", zNewScript);
f41cf03… mistachkin 2011 #if USE_SEE
f41cf03… mistachkin 2012 if( zPathInfo ){
f41cf03… mistachkin 2013 if( g.fHttpTrace ){
f41cf03… mistachkin 2014 sqlite3_snprintf(sizeof(zBuf), zBuf, "%d", i);
f41cf03… mistachkin 2015 @ <!-- see_path_info(%s(zBuf)) is %h(zPathInfo) -->
f41cf03… mistachkin 2016 fprintf(stderr, "# see_path_info(%d) = %s\n", i, zPathInfo);
f41cf03… mistachkin 2017 }
f41cf03… mistachkin 2018 if( strcmp(zPathInfo,"/setseekey")==0
f41cf03… mistachkin 2019 && strcmp(zRepoExt,".efossil")==0
f41cf03… mistachkin 2020 && !db_have_saved_encryption_key() ){
f41cf03… mistachkin 2021 db_set_see_key_page();
f41cf03… mistachkin 2022 cgi_reply();
f41cf03… mistachkin 2023 fossil_exit(0);
f41cf03… mistachkin 2024 }
f41cf03… mistachkin 2025 }
f41cf03… mistachkin 2026 #endif
239b4c1… drh 2027 db_open_repository(file_cleanup_fullpath(zRepo));
14c19fb… drh 2028 if( g.fHttpTrace ){
239b4c1… drh 2029 @ <!-- repository: "%h(zRepo)" -->
239b4c1… drh 2030 @ <!-- translated PATH_INFO: "%h(zPathInfo)" -->
239b4c1… drh 2031 @ <!-- translated SCRIPT_NAME: "%h(zNewScript)" -->
f668ff4… drh 2032 fprintf(stderr,
14c19fb… drh 2033 "# repository: [%s]\n"
239b4c1… drh 2034 "# translated PATH_INFO = [%s]\n"
239b4c1… drh 2035 "# translated SCRIPT_NAME = [%s]\n",
14c19fb… drh 2036 zRepo, zPathInfo, zNewScript);
239b4c1… drh 2037 if( g.zTop ){
239b4c1… drh 2038 @ <!-- translated g.zTop: "%h(g.zTop)" -->
239b4c1… drh 2039 fprintf(stderr, "# translated g.zTop = [%s]\n", g.zTop);
239b4c1… drh 2040 }
239b4c1… drh 2041 if( g.zBaseURL ){
239b4c1… drh 2042 @ <!-- translated g.zBaseURL: "%h(g.zBaseURL)" -->
239b4c1… drh 2043 fprintf(stderr, "# translated g.zBaseURL = [%s]\n", g.zBaseURL);
239b4c1… drh 2044 }
239b4c1… drh 2045 }
239b4c1… drh 2046 }
239b4c1… drh 2047
239b4c1… drh 2048 /* At this point, the appropriate repository database file will have
106fe61… drh 2049 ** been opened.
7b028d7… drh 2050 */
7b028d7… drh 2051
7b028d7… drh 2052 /*
9d845eb… stephan 2053 ** Check to see if the first term of PATH_INFO specifies an
9d845eb… stephan 2054 ** alternative skin. This will be the case if the first term of
9d845eb… stephan 2055 ** PATH_INFO begins with "draftN/" where N is an integer between 1
9d845eb… stephan 2056 ** and 9. If so, activate the skin associated with that draft.
106fe61… drh 2057 */
106fe61… drh 2058 if( zPathInfo && strncmp(zPathInfo,"/draft",6)==0
106fe61… drh 2059 && zPathInfo[6]>='1' && zPathInfo[6]<='9'
106fe61… drh 2060 && (zPathInfo[7]=='/' || zPathInfo[7]==0)
106fe61… drh 2061 ){
106fe61… drh 2062 int iSkin = zPathInfo[6] - '0';
106fe61… drh 2063 char *zNewScript;
de320cc… drh 2064 if( db_int(0,"SELECT count(*) FROM config WHERE name GLOB 'draft%d-*'",
de320cc… drh 2065 iSkin)<5 ){
de320cc… drh 2066 fossil_not_found_page();
de320cc… drh 2067 fossil_exit(0);
de320cc… drh 2068 }
106fe61… drh 2069 skin_use_draft(iSkin);
bb9233a… drh 2070 zNewScript = mprintf("%T/draft%d", P("SCRIPT_NAME"), iSkin);
a40e8a0… drh 2071 if( g.zTop ) g.zTop = mprintf("%R/draft%d", iSkin);
106fe61… drh 2072 if( g.zBaseURL ) g.zBaseURL = mprintf("%s/draft%d", g.zBaseURL, iSkin);
106fe61… drh 2073 zPathInfo += 7;
acb4397… drh 2074 g.nExtraURL += 7;
106fe61… drh 2075 cgi_replace_parameter("PATH_INFO", zPathInfo);
106fe61… drh 2076 cgi_replace_parameter("SCRIPT_NAME", zNewScript);
03643a6… drh 2077 etag_cancel();
3339420… drh 2078 }
3339420… drh 2079
275da70… danield 2080 /* If the content type is application/x-fossil or
106fe61… drh 2081 ** application/x-fossil-debug, then a sync/push/pull/clone is
106fe61… drh 2082 ** desired, so default the PATH_INFO to /xfer
14c19fb… drh 2083 */
dbb5e2d… drh 2084 if( g.zContentType &&
dbb5e2d… drh 2085 strncmp(g.zContentType, "application/x-fossil", 20)==0 ){
239b4c1… drh 2086 /* Special case: If the content mimetype shows that it is "fossil sync"
239b4c1… drh 2087 ** payload, then pretend that the PATH_INFO is /xfer so that we always
239b4c1… drh 2088 ** invoke the sync page. */
14c19fb… drh 2089 zPathInfo = "/xfer";
14c19fb… drh 2090 }
106fe61… drh 2091
106fe61… drh 2092 /* Use the first element of PATH_INFO as the page name
106fe61… drh 2093 ** and deliver the appropriate page back to the user.
106fe61… drh 2094 */
ecb85f6… drh 2095 set_base_url(0);
4aba9ea… drh 2096 if( fossil_redirect_to_https_if_needed(2) ) return;
f668ff4… drh 2097 if( zPathInfo==0 || zPathInfo[0]==0
c7c81df… drh 2098 || (zPathInfo[0]=='/' && zPathInfo[1]==0) ){
33c1cfd… drh 2099 /* Second special case: If the PATH_INFO is blank, issue a
33c1cfd… drh 2100 ** temporary 302 redirect:
58849c4… drh 2101 ** (1) to "/ckout" if g.useLocalauth and g.localOpen are both set.
58849c4… drh 2102 ** (2) to the home page identified by the "index-page" setting
58849c4… drh 2103 ** in the repository CONFIG table
58849c4… drh 2104 ** (3) to "/index" if there no "index-page" setting in CONFIG
58849c4… drh 2105 */
796dcfe… drh 2106 #ifdef FOSSIL_ENABLE_JSON
796dcfe… drh 2107 if(g.json.isJsonMode){
796dcfe… drh 2108 json_err(FSL_JSON_E_RESOURCE_NOT_FOUND,NULL,1);
796dcfe… drh 2109 fossil_exit(0);
796dcfe… drh 2110 }
796dcfe… drh 2111 #endif
58849c4… drh 2112 if( g.useLocalauth && g.localOpen ){
58849c4… drh 2113 cgi_redirectf("%R/ckout");
58849c4… drh 2114 }else{
58849c4… drh 2115 fossil_redirect_home() /*does not return*/;
58849c4… drh 2116 }
da4f0c2… aku 2117 }else{
4c3e172… danield 2118 zPath = fossil_strdup(zPathInfo);
da4f0c2… aku 2119 }
da4f0c2… aku 2120
ab48825… drh 2121 /* Make g.zPath point to the first element of the path. Make
ab48825… drh 2122 ** g.zExtra point to everything past that point.
dbda8d6… drh 2123 */
225abb3… drh 2124 g.zPath = &zPath[1];
225abb3… drh 2125 for(i=1; zPath[i] && zPath[i]!='/'; i++){}
225abb3… drh 2126 if( zPath[i]=='/' ){
225abb3… drh 2127 zPath[i] = 0;
225abb3… drh 2128 g.zExtra = &zPath[i+1];
225abb3… drh 2129 }else{
225abb3… drh 2130 g.zExtra = 0;
39bef92… stephan 2131 }
d0305b3… aku 2132 if( g.zExtra ){
d0305b3… aku 2133 /* CGI parameters get this treatment elsewhere, but places like getfile
d0305b3… aku 2134 ** will use g.zExtra directly.
796dcfe… drh 2135 ** Reminder: the login mechanism uses 'name' differently, and may
796dcfe… drh 2136 ** eventually have a problem/collision with this.
ccd6c87… stephan 2137 **
ccd6c87… stephan 2138 ** Disabled by stephan when running in JSON mode because this
ccd6c87… stephan 2139 ** particular parameter name is very common and i have had no end
ccd6c87… stephan 2140 ** of grief with this handling. The JSON API never relies on the
db0c512… drh 2141 ** handling below, and by disabling it in JSON mode I can remove
ccd6c87… stephan 2142 ** lots of special-case handling in several JSON handlers.
16e703b… drh 2143 */
ccd6c87… stephan 2144 #ifdef FOSSIL_ENABLE_JSON
e7f13b8… stephan 2145 if(g.json.isJsonMode==0){
ccd6c87… stephan 2146 #endif
ccd6c87… stephan 2147 dehttpize(g.zExtra);
f8a2aa0… drh 2148 cgi_set_parameter_nocopy("name", g.zExtra, 1);
ccd6c87… stephan 2149 #ifdef FOSSIL_ENABLE_JSON
ccd6c87… stephan 2150 }
ccd6c87… stephan 2151 #endif
16e703b… drh 2152 }
0ca3644… drh 2153
dbda8d6… drh 2154 /* Locate the method specified by the path and execute the function
dbda8d6… drh 2155 ** that implements that method.
dbda8d6… drh 2156 */
8131f1c… drh 2157 if( dispatch_name_search(g.zPath-1, CMDFLAG_WEBPAGE, &pCmd)
8131f1c… drh 2158 && dispatch_alias(g.zPath-1, &pCmd)
8131f1c… drh 2159 ){
796dcfe… drh 2160 #ifdef FOSSIL_ENABLE_JSON
e7f13b8… stephan 2161 if(g.json.isJsonMode!=0){
796dcfe… drh 2162 json_err(FSL_JSON_E_RESOURCE_NOT_FOUND,NULL,0);
796dcfe… drh 2163 }else
796dcfe… drh 2164 #endif
796dcfe… drh 2165 {
1c528d3… mistachkin 2166 #ifdef FOSSIL_ENABLE_TH1_HOOKS
1c528d3… mistachkin 2167 int rc;
1c528d3… mistachkin 2168 if( !g.fNoThHook ){
1c528d3… mistachkin 2169 rc = Th_WebpageHook(g.zPath, 0);
1c528d3… mistachkin 2170 }else{
1c528d3… mistachkin 2171 rc = TH_OK;
1c528d3… mistachkin 2172 }
1c528d3… mistachkin 2173 if( rc==TH_OK || rc==TH_RETURN || rc==TH_CONTINUE ){
1c528d3… mistachkin 2174 if( rc==TH_OK || rc==TH_RETURN ){
1c528d3… mistachkin 2175 #endif
1c528d3… mistachkin 2176 cgi_set_status(404,"Not Found");
1c528d3… mistachkin 2177 @ <h1>Not Found</h1>
1c528d3… mistachkin 2178 @ <p>Page not found: %h(g.zPath)</p>
1c528d3… mistachkin 2179 #ifdef FOSSIL_ENABLE_TH1_HOOKS
1c528d3… mistachkin 2180 }
1c528d3… mistachkin 2181 if( !g.fNoThHook && (rc==TH_OK || rc==TH_CONTINUE) ){
1c528d3… mistachkin 2182 Th_WebpageNotify(g.zPath, 0);
1c528d3… mistachkin 2183 }
1c528d3… mistachkin 2184 }
1c528d3… mistachkin 2185 #endif
796dcfe… drh 2186 }
e6aab1a… drh 2187 }else if( pCmd->xFunc!=page_xfer && db_schema_is_outofdate() ){
796dcfe… drh 2188 #ifdef FOSSIL_ENABLE_JSON
e7f13b8… stephan 2189 if(g.json.isJsonMode!=0){
796dcfe… drh 2190 json_err(FSL_JSON_E_DB_NEEDS_REBUILD,NULL,0);
796dcfe… drh 2191 }else
796dcfe… drh 2192 #endif
796dcfe… drh 2193 {
796dcfe… drh 2194 @ <h1>Server Configuration Error</h1>
796dcfe… drh 2195 @ <p>The database schema on the server is out-of-date. Please ask
796dcfe… drh 2196 @ the administrator to run <b>fossil rebuild</b>.</p>
796dcfe… drh 2197 }
dbda8d6… drh 2198 }else{
9413395… drh 2199 if(0==(CMDFLAG_LDAVG_EXEMPT & pCmd->eCmdFlags)){
9413395… drh 2200 load_control();
9413395… drh 2201 }
a588e55… mistachkin 2202 #ifdef FOSSIL_ENABLE_JSON
19a01ac… danield 2203 {
19a01ac… danield 2204 static int jsonOnce = 0;
19a01ac… danield 2205 if( jsonOnce==0 && g.json.isJsonMode!=0 ){
19a01ac… danield 2206 assert(json_is_bootstrapped_early());
19a01ac… danield 2207 json_bootstrap_late();
19a01ac… danield 2208 jsonOnce = 1;
19a01ac… danield 2209 }
a588e55… mistachkin 2210 }
a588e55… mistachkin 2211 #endif
ec56c69… drh 2212 if( (pCmd->eCmdFlags & CMDFLAG_RAWCONTENT)==0 ){
ec56c69… drh 2213 cgi_decode_post_parameters();
2269ac6… drh 2214 if( !cgi_same_origin(0) ){
f8363db… drh 2215 isReadonly = 1;
7c71f00… drh 2216 db_protect(PROTECT_READONLY);
7c71f00… drh 2217 }
ec56c69… drh 2218 }
9a2e5f4… drh 2219 if( g.fCgiTrace ){
9a2e5f4… drh 2220 fossil_trace("######## Calling %s #########\n", pCmd->zName);
0204f4a… drh 2221 cgi_print_all(1, 1, 0);
ea2e515… mistachkin 2222 }
ea2e515… mistachkin 2223 #ifdef FOSSIL_ENABLE_TH1_HOOKS
ea2e515… mistachkin 2224 {
ea2e515… mistachkin 2225 /*
ea2e515… mistachkin 2226 ** The TH1 return codes from the hook will be handled as follows:
ea2e515… mistachkin 2227 **
ea2e515… mistachkin 2228 ** TH_OK: The xFunc() and the TH1 notification will both be executed.
ea2e515… mistachkin 2229 **
ea2e515… mistachkin 2230 ** TH_ERROR: The xFunc() will be skipped, the TH1 notification will be
ea2e515… mistachkin 2231 ** skipped. If the xFunc() is being hooked, the error message
ea2e515… mistachkin 2232 ** will be emitted.
ea2e515… mistachkin 2233 **
ea2e515… mistachkin 2234 ** TH_BREAK: The xFunc() and the TH1 notification will both be skipped.
ea2e515… mistachkin 2235 **
ea2e515… mistachkin 2236 ** TH_RETURN: The xFunc() will be executed, the TH1 notification will be
ea2e515… mistachkin 2237 ** skipped.
ea2e515… mistachkin 2238 **
ea2e515… mistachkin 2239 ** TH_CONTINUE: The xFunc() will be skipped, the TH1 notification will be
ea2e515… mistachkin 2240 ** executed.
ea2e515… mistachkin 2241 */
ea2e515… mistachkin 2242 int rc;
ea2e515… mistachkin 2243 if( !g.fNoThHook ){
ea2e515… mistachkin 2244 rc = Th_WebpageHook(pCmd->zName+1, pCmd->eCmdFlags);
ea2e515… mistachkin 2245 }else{
ea2e515… mistachkin 2246 rc = TH_OK;
ea2e515… mistachkin 2247 }
ea2e515… mistachkin 2248 if( rc==TH_OK || rc==TH_RETURN || rc==TH_CONTINUE ){
ea2e515… mistachkin 2249 if( rc==TH_OK || rc==TH_RETURN ){
ea2e515… mistachkin 2250 #endif
48c06b0… drh 2251 g.zPhase = pCmd->zName;
ea2e515… mistachkin 2252 pCmd->xFunc();
ea2e515… mistachkin 2253 #ifdef FOSSIL_ENABLE_TH1_HOOKS
ea2e515… mistachkin 2254 }
ea2e515… mistachkin 2255 if( !g.fNoThHook && (rc==TH_OK || rc==TH_CONTINUE) ){
ea2e515… mistachkin 2256 Th_WebpageNotify(pCmd->zName+1, pCmd->eCmdFlags);
ea2e515… mistachkin 2257 }
ea2e515… mistachkin 2258 }
ea2e515… mistachkin 2259 }
ea2e515… mistachkin 2260 #endif
f8363db… drh 2261 if( isReadonly ){
f8363db… drh 2262 db_protect_pop();
f8363db… drh 2263 }
dbda8d6… drh 2264 }
dbda8d6… drh 2265
dbda8d6… drh 2266 /* Return the result.
dbda8d6… drh 2267 */
48c06b0… drh 2268 g.zPhase = "web-page reply";
dbda8d6… drh 2269 cgi_reply();
dbda8d6… drh 2270 }
dbda8d6… drh 2271
dbc2098… drh 2272 /* If the CGI program contains one or more lines of the form
dbc2098… drh 2273 **
dbc2098… drh 2274 ** redirect: repository-filename http://hostname/path/%s
dbc2098… drh 2275 **
f668ff4… drh 2276 ** then control jumps here. Search each repository for an artifact ID
9501049… drh 2277 ** or ticket ID that matches the "name" query parameter. If there is
9501049… drh 2278 ** no "name" query parameter, use PATH_INFO instead. If a match is
9501049… drh 2279 ** found, redirect to the corresponding URL. Substitute "%s" in the
9501049… drh 2280 ** URL with the value of the name query parameter before the redirect.
dbc2098… drh 2281 **
dbc2098… drh 2282 ** If there is a line of the form:
dbc2098… drh 2283 **
dbc2098… drh 2284 ** redirect: * URL
dbc2098… drh 2285 **
9501049… drh 2286 ** Then a redirect is made to URL if no match is found. If URL contains
9501049… drh 2287 ** "%s" then substitute the "name" query parameter. If REPO is "*" and
9501049… drh 2288 ** URL does not contains "%s" and does not contain "?" then append
9501049… drh 2289 ** PATH_INFO and QUERY_STRING to the URL prior to the redirect.
9501049… drh 2290 **
9501049… drh 2291 ** If no matches are found and if there is no "*" entry, then generate
9501049… drh 2292 ** a primitive error message.
9501049… drh 2293 **
9501049… drh 2294 ** USE CASES:
9501049… drh 2295 **
9501049… drh 2296 ** (1) Suppose you have two related projects projA and projB. You can
9501049… drh 2297 ** use this feature to set up an /info page that covers both
9501049… drh 2298 ** projects.
9501049… drh 2299 **
9501049… drh 2300 ** redirect: /fossils/projA.fossil /proj-a/info/%s
9501049… drh 2301 ** redirect: /fossils/projB.fossil /proj-b/info/%s
9501049… drh 2302 **
9501049… drh 2303 ** Then visits to the /info/HASH page will redirect to the
9501049… drh 2304 ** first project that contains that hash.
9501049… drh 2305 **
9501049… drh 2306 ** (2) Use the "*" form for to redirect legacy URLs. On the Fossil
9501049… drh 2307 ** website we have an CGI at http://fossil.com/index.html (note
9501049… drh 2308 ** ".com" instead of ".org") that looks like this:
9501049… drh 2309 **
9501049… drh 2310 ** #!/usr/bin/fossil
9501049… drh 2311 ** redirect: * https://fossil-scm.org/home
9501049… drh 2312 **
9501049… drh 2313 ** Thus requests to the .com website redirect to the .org website.
33c1cfd… drh 2314 ** This form uses a 301 Permanent redirect.
33c1cfd… drh 2315 **
33c1cfd… drh 2316 ** On a "*" redirect, the PATH_INFO and QUERY_STRING of the query
33c1cfd… drh 2317 ** that provoked the redirect are appended to the target. So, for
33c1cfd… drh 2318 ** example, if the input URL for the redirect above were
33c1cfd… drh 2319 ** "http://www.fossil.com/index.html/timeline?c=20250404", then
33c1cfd… drh 2320 ** the redirect would be to:
33c1cfd… drh 2321 **
33c1cfd… drh 2322 ** https://fossil-scm.org/home/timeline?c=20250404
33c1cfd… drh 2323 ** ^^^^^^^^^^^^^^^^^^^^
33c1cfd… drh 2324 ** Copied from input URL
dbc2098… drh 2325 */
bb0cedb… drh 2326 static void redirect_web_page(int nRedirect, char **azRedirect){
dbc2098… drh 2327 int i; /* Loop counter */
dbc2098… drh 2328 const char *zNotFound = 0; /* Not found URL */
dbc2098… drh 2329 const char *zName = P("name");
ecb85f6… drh 2330 set_base_url(0);
122a31d… drh 2331 if( zName==0 ){
dd6b298… drh 2332 zName = P("PATH_INFO");
122a31d… drh 2333 if( zName && zName[0]=='/' ) zName++;
122a31d… drh 2334 }
d6f9c56… drh 2335 if( zName ){
dbc2098… drh 2336 for(i=0; i<nRedirect; i++){
32ad9a1… drh 2337 if( fossil_strcmp(azRedirect[i*2],"*")==0 ){
dbc2098… drh 2338 zNotFound = azRedirect[i*2+1];
dbc2098… drh 2339 continue;
d6f9c56… drh 2340 }else if( validate16(zName, strlen(zName)) ){
d6f9c56… drh 2341 db_open_repository(azRedirect[i*2]);
d6f9c56… drh 2342 if( db_exists("SELECT 1 FROM blob WHERE uuid GLOB '%q*'", zName) ||
d6f9c56… drh 2343 db_exists("SELECT 1 FROM ticket WHERE tkt_uuid GLOB '%q*'",zName) ){
d6f9c56… drh 2344 cgi_redirectf(azRedirect[i*2+1] /*works-like:"%s"*/, zName);
d6f9c56… drh 2345 return;
d6f9c56… drh 2346 }
d6f9c56… drh 2347 db_close(1);
d6f9c56… drh 2348 }
dbc2098… drh 2349 }
dbc2098… drh 2350 }
dbc2098… drh 2351 if( zNotFound ){
9501049… drh 2352 Blob to;
9501049… drh 2353 const char *z;
9501049… drh 2354 if( strstr(zNotFound, "%s") ){
33c1cfd… drh 2355 char *zTarget = mprintf(zNotFound /*works-like:"%s"*/, zName);
33c1cfd… drh 2356 cgi_redirect_perm(zTarget);
9501049… drh 2357 }
9501049… drh 2358 if( strchr(zNotFound, '?') ){
33c1cfd… drh 2359 cgi_redirect_perm(zNotFound);
9501049… drh 2360 }
9501049… drh 2361 blob_init(&to, zNotFound, -1);
9501049… drh 2362 z = P("PATH_INFO");
9501049… drh 2363 if( z && z[0]=='/' ) blob_append(&to, z, -1);
9501049… drh 2364 z = P("QUERY_STRING");
9501049… drh 2365 if( z && z[0]!=0 ) blob_appendf(&to, "?%s", z);
33c1cfd… drh 2366 cgi_redirect_perm(blob_str(&to));
dbc2098… drh 2367 }else{
dbc2098… drh 2368 @ <html>
dbc2098… drh 2369 @ <head><title>No Such Object</title></head>
dbc2098… drh 2370 @ <body>
dbc2098… drh 2371 @ <p>No such object: <b>%h(zName)</b></p>
dbc2098… drh 2372 @ </body>
dbc2098… drh 2373 cgi_reply();
dbc2098… drh 2374 }
dbc2098… drh 2375 }
dbc2098… drh 2376
dbda8d6… drh 2377 /*
bb0cedb… drh 2378 ** COMMAND: cgi*
bb0cedb… drh 2379 **
66d0038… drh 2380 ** Usage: %fossil ?cgi? FILE
66d0038… drh 2381 **
66d0038… drh 2382 ** This command causes Fossil to generate reply to a CGI request.
bb0cedb… drh 2383 **
66d0038… drh 2384 ** The FILE argument is the name of a control file that provides Fossil
66d0038… drh 2385 ** with important information such as where to find its repository. In
66d0038… drh 2386 ** a typical CGI deployment, FILE is the name of the CGI script and will
66d0038… drh 2387 ** typically look something like this:
bb0cedb… drh 2388 **
bb0cedb… drh 2389 ** #!/usr/bin/fossil
bb0cedb… drh 2390 ** repository: /home/somebody/project.db
bb0cedb… drh 2391 **
66d0038… drh 2392 ** The command name, "cgi", may be omitted if the GATEWAY_INTERFACE
66d0038… drh 2393 ** environment variable is set to "CGI", which should always be the
66d0038… drh 2394 ** case for CGI scripts run by a webserver. Fossil ignores any lines
66d0038… drh 2395 ** that begin with "#".
66d0038… drh 2396 **
66d0038… drh 2397 ** The following control lines are recognized:
66d0038… drh 2398 **
66d0038… drh 2399 ** repository: PATH Name of the Fossil repository
66d0038… drh 2400 **
66d0038… drh 2401 ** directory: PATH Name of a directory containing many Fossil
66d0038… drh 2402 ** repositories whose names all end with ".fossil".
66d0038… drh 2403 ** There should only be one of "repository:"
66d0038… drh 2404 ** or "directory:"
66d0038… drh 2405 **
66d0038… drh 2406 ** notfound: URL When in "directory:" mode, redirect to
66d0038… drh 2407 ** URL if no suitable repository is found.
66d0038… drh 2408 **
20e9731… rberteig 2409 ** repolist When in "directory:" mode, display a page
20e9731… rberteig 2410 ** showing a list of available repositories if
fbd7731… drh 2411 ** the URL is "/". Some control over the display
fbd7731… drh 2412 ** is accomplished using environment variables.
fbd7731… drh 2413 ** FOSSIL_REPOLIST_TITLE is the tital of the page.
fbd7731… drh 2414 ** FOSSIL_REPOLIST_SHOW cause the "Description"
fbd7731… drh 2415 ** column to display if it contains "description" as
fbd7731… drh 2416 ** as a substring, and causes the Login-Group column
fbd7731… drh 2417 ** to display if it contains the "login-group"
fbd7731… drh 2418 ** substring.
20e9731… rberteig 2419 **
66d0038… drh 2420 ** localauth Grant administrator privileges to connections
66d0038… drh 2421 ** from 127.0.0.1 or ::1.
66d0038… drh 2422 **
3915262… florian 2423 ** nossl Signal that no SSL connections are available.
3915262… florian 2424 **
3915262… florian 2425 ** nocompress Do not compress HTTP replies.
3915262… florian 2426 **
66d0038… drh 2427 ** skin: LABEL Use the built-in skin called LABEL rather than
699b9ca… stephan 2428 ** the default, or the default if LABEL is empty.
699b9ca… stephan 2429 ** If there are no skins called LABEL then this
699b9ca… stephan 2430 ** line is a no-op.
66d0038… drh 2431 **
66d0038… drh 2432 ** files: GLOBLIST GLOBLIST is a comma-separated list of GLOB
66d0038… drh 2433 ** patterns that specify files that can be
66d0038… drh 2434 ** returned verbatim. This feature allows Fossil
66d0038… drh 2435 ** to act as a web server returning static
66d0038… drh 2436 ** content.
66d0038… drh 2437 **
66d0038… drh 2438 ** setenv: NAME VALUE Set environment variable NAME to VALUE. Or
66d0038… drh 2439 ** if VALUE is omitted, unset NAME.
66d0038… drh 2440 **
66d0038… drh 2441 ** HOME: PATH Shorthand for "setenv: HOME PATH"
66d0038… drh 2442 **
e3dbb7d… wyoung 2443 ** cgi-debug: FILE Causing debugging information to be written
66d0038… drh 2444 ** into FILE.
66d0038… drh 2445 **
66d0038… drh 2446 ** errorlog: FILE Warnings, errors, and panics written to FILE.
859d6b1… drh 2447 **
859d6b1… drh 2448 ** timeout: SECONDS Do not run for longer than SECONDS. The default
7979989… drh 2449 ** timeout is FOSSIL_DEFAULT_TIMEOUT (600) seconds.
ec56c69… drh 2450 **
ec56c69… drh 2451 ** extroot: DIR Directory that is the root of the sub-CGI tree
ec56c69… drh 2452 ** on the /ext page.
66d0038… drh 2453 **
66d0038… drh 2454 ** redirect: REPO URL Extract the "name" query parameter and search
66d0038… drh 2455 ** REPO for a check-in or ticket that matches the
66d0038… drh 2456 ** value of "name", then redirect to URL. There
66d0038… drh 2457 ** can be multiple "redirect:" lines that are
66d0038… drh 2458 ** processed in order. If the REPO is "*", then
66d0038… drh 2459 ** an unconditional redirect to URL is taken.
33c1cfd… drh 2460 ** When "*" is used a 301 permanent redirect is
33c1cfd… drh 2461 ** issued and the tail and query string from the
7db623e… danield 2462 ** original query are appended onto URL.
19f2753… stephan 2463 **
517223e… drh 2464 ** jsmode: VALUE Specifies the delivery mode for JavaScript
19f2753… stephan 2465 ** files. See the help text for the --jsmode
19f2753… stephan 2466 ** flag of the http command.
517223e… drh 2467 **
73ca280… drh 2468 ** mainmenu: FILE Override the mainmenu config setting with the
73ca280… drh 2469 ** contents of the given file.
73ca280… drh 2470 **
66d0038… drh 2471 ** Most CGI files contain only a "repository:" line. It is uncommon to
66d0038… drh 2472 ** use any other option.
66d0038… drh 2473 **
8d3176f… stephan 2474 ** The lines are processed in the order they are read, which is most
8d3176f… stephan 2475 ** significant for "errorlog:", which should be set before "repository:"
8d3176f… stephan 2476 ** so that any warnings from the database when opening the repository
8d3176f… stephan 2477 ** go to that log file.
8d3176f… stephan 2478 **
e2c161b… danield 2479 ** See also: [[http]], [[server]], [[winsrv]] [Windows only]
bb0cedb… drh 2480 */
bb0cedb… drh 2481 void cmd_cgi(void){
bb0cedb… drh 2482 const char *zNotFound = 0;
bb0cedb… drh 2483 char **azRedirect = 0; /* List of repositories to redirect to */
bb0cedb… drh 2484 int nRedirect = 0; /* Number of entries in azRedirect */
bb0cedb… drh 2485 Glob *pFileGlob = 0; /* Pattern for files */
87508e0… drh 2486 int allowRepoList = 0; /* Allow lists of repository files */
bb0cedb… drh 2487 Blob config, line, key, value, value2;
c06e0b2… drh 2488 /* Initialize the CGI environment. */
bb0cedb… drh 2489 g.httpOut = stdout;
bb0cedb… drh 2490 g.httpIn = stdin;
bb0cedb… drh 2491 fossil_binary_mode(g.httpOut);
bb0cedb… drh 2492 fossil_binary_mode(g.httpIn);
bb0cedb… drh 2493 g.cgiOutput = 1;
a8e33fb… drh 2494 g.zReqType = "CGI";
7979989… drh 2495 fossil_set_timeout(FOSSIL_DEFAULT_TIMEOUT);
20dc009… drh 2496 /* Find the name of the CGI control file */
c06e0b2… drh 2497 if( g.argc==3 && fossil_strcmp(g.argv[1],"cgi")==0 ){
e6b06b5… drh 2498 g.zCgiFile = g.argv[2];
c06e0b2… drh 2499 }else if( g.argc>=2 ){
e6b06b5… drh 2500 g.zCgiFile = g.argv[1];
c06e0b2… drh 2501 }else{
c06e0b2… drh 2502 cgi_panic("No CGI control file specified");
c06e0b2… drh 2503 }
c06e0b2… drh 2504 /* Read and parse the CGI control file. */
e6b06b5… drh 2505 blob_read_from_file(&config, g.zCgiFile, ExtFILE);
bb0cedb… drh 2506 while( blob_line(&config, &line) ){
bb0cedb… drh 2507 if( !blob_token(&line, &key) ) continue;
bb0cedb… drh 2508 if( blob_buffer(&key)[0]=='#' ) continue;
bb0cedb… drh 2509 if( blob_eq(&key, "repository:") && blob_tail(&line, &value) ){
236087b… drh 2510 /* repository: FILENAME
236087b… drh 2511 **
236087b… drh 2512 ** The name of the Fossil repository to be served via CGI. Most
236087b… drh 2513 ** fossil CGI scripts have a single non-comment line that contains
236087b… drh 2514 ** this one entry.
236087b… drh 2515 */
bb0cedb… drh 2516 blob_trim(&value);
bb0cedb… drh 2517 db_open_repository(blob_str(&value));
bb0cedb… drh 2518 blob_reset(&value);
bb0cedb… drh 2519 continue;
bb0cedb… drh 2520 }
bb0cedb… drh 2521 if( blob_eq(&key, "directory:") && blob_token(&line, &value) ){
236087b… drh 2522 /* directory: DIRECTORY
236087b… drh 2523 **
236087b… drh 2524 ** If repository: is omitted, then terms of the PATH_INFO cgi parameter
236087b… drh 2525 ** are appended to DIRECTORY looking for a repository (whose name ends
236087b… drh 2526 ** in ".fossil") or a file in "files:".
236087b… drh 2527 */
bb0cedb… drh 2528 db_close(1);
4c3e172… danield 2529 g.zRepositoryName = fossil_strdup(blob_str(&value));
bb0cedb… drh 2530 blob_reset(&value);
bb0cedb… drh 2531 continue;
bb0cedb… drh 2532 }
bb0cedb… drh 2533 if( blob_eq(&key, "notfound:") && blob_token(&line, &value) ){
236087b… drh 2534 /* notfound: URL
236087b… drh 2535 **
236087b… drh 2536 ** If using directory: and no suitable repository or file is found,
236087b… drh 2537 ** then redirect to URL.
236087b… drh 2538 */
4c3e172… danield 2539 zNotFound = fossil_strdup(blob_str(&value));
bb0cedb… drh 2540 blob_reset(&value);
bb0cedb… drh 2541 continue;
bb0cedb… drh 2542 }
bb0cedb… drh 2543 if( blob_eq(&key, "localauth") ){
236087b… drh 2544 /* localauth
236087b… drh 2545 **
236087b… drh 2546 ** Grant "administrator" privileges to users connecting with HTTP
236087b… drh 2547 ** from IP address 127.0.0.1. Do not bother checking credentials.
236087b… drh 2548 */
bb0cedb… drh 2549 g.useLocalauth = 1;
3915262… florian 2550 continue;
3915262… florian 2551 }
3915262… florian 2552 if( blob_eq(&key, "nossl") ){
3915262… florian 2553 /* nossl
3915262… florian 2554 **
3915262… florian 2555 ** Signal that no SSL connections are available.
3915262… florian 2556 */
3915262… florian 2557 g.sslNotAvailable = 1;
3915262… florian 2558 continue;
3915262… florian 2559 }
3915262… florian 2560 if( blob_eq(&key, "nocompress") ){
3915262… florian 2561 /* nocompress
3915262… florian 2562 **
3915262… florian 2563 ** Do not compress HTTP replies.
3915262… florian 2564 */
3915262… florian 2565 g.fNoHttpCompress = 1;
87508e0… drh 2566 continue;
87508e0… drh 2567 }
87508e0… drh 2568 if( blob_eq(&key, "repolist") ){
87508e0… drh 2569 /* repolist
87508e0… drh 2570 **
87508e0… drh 2571 ** If using "directory:" and the URL is "/" then generate a page
87508e0… drh 2572 ** showing a list of available repositories.
87508e0… drh 2573 */
87508e0… drh 2574 allowRepoList = 1;
87508e0… drh 2575 continue;
87508e0… drh 2576 }
bb0cedb… drh 2577 if( blob_eq(&key, "redirect:") && blob_token(&line, &value)
bb0cedb… drh 2578 && blob_token(&line, &value2) ){
236087b… drh 2579 /* See the header comment on the redirect_web_page() function
236087b… drh 2580 ** above for details. */
bb0cedb… drh 2581 nRedirect++;
bb0cedb… drh 2582 azRedirect = fossil_realloc(azRedirect, 2*nRedirect*sizeof(char*));
4c3e172… danield 2583 azRedirect[nRedirect*2-2] = fossil_strdup(blob_str(&value));
4c3e172… danield 2584 azRedirect[nRedirect*2-1] = fossil_strdup(blob_str(&value2));
bb0cedb… drh 2585 blob_reset(&value);
bb0cedb… drh 2586 blob_reset(&value2);
bb0cedb… drh 2587 continue;
bb0cedb… drh 2588 }
bb0cedb… drh 2589 if( blob_eq(&key, "files:") && blob_token(&line, &value) ){
236087b… drh 2590 /* files: GLOBLIST
236087b… drh 2591 **
236087b… drh 2592 ** GLOBLIST is a comma-separated list of filename globs. For
236087b… drh 2593 ** example: *.html,*.css,*.js
236087b… drh 2594 **
236087b… drh 2595 ** If the repository: line is omitted and then PATH_INFO is searched
236087b… drh 2596 ** for files that match any of these GLOBs and if any such file is
236087b… drh 2597 ** found it is returned verbatim. This feature allows "fossil server"
236087b… drh 2598 ** to function as a primitive web-server delivering arbitrary content.
236087b… drh 2599 */
bb0cedb… drh 2600 pFileGlob = glob_create(blob_str(&value));
1829fa8… mistachkin 2601 blob_reset(&value);
0af7024… mistachkin 2602 continue;
0af7024… mistachkin 2603 }
72df287… drh 2604 if( blob_eq(&key, "setenv:") && blob_token(&line, &value) ){
236087b… drh 2605 /* setenv: NAME VALUE
72df287… drh 2606 ** setenv: NAME
236087b… drh 2607 **
72df287… drh 2608 ** Sets environment variable NAME to VALUE. If VALUE is omitted, then
72df287… drh 2609 ** the environment variable is unset.
236087b… drh 2610 */
9443f61… drh 2611 char *zValue;
9443f61… drh 2612 blob_tail(&line,&value2);
9443f61… drh 2613 blob_trim(&value2);
9443f61… drh 2614 zValue = blob_str(&value2);
9443f61… drh 2615 while( fossil_isspace(zValue[0]) ){ zValue++; }
9443f61… drh 2616 fossil_setenv(blob_str(&value), zValue);
0af7024… mistachkin 2617 blob_reset(&value);
0af7024… mistachkin 2618 blob_reset(&value2);
236087b… drh 2619 continue;
236087b… drh 2620 }
236087b… drh 2621 if( blob_eq(&key, "errorlog:") && blob_token(&line, &value) ){
236087b… drh 2622 /* errorlog: FILENAME
236087b… drh 2623 **
236087b… drh 2624 ** Causes messages from warnings, errors, and panics to be appended
236087b… drh 2625 ** to FILENAME.
236087b… drh 2626 */
4c3e172… danield 2627 g.zErrlog = fossil_strdup(blob_str(&value));
ec56c69… drh 2628 blob_reset(&value);
ec56c69… drh 2629 continue;
ec56c69… drh 2630 }
ec56c69… drh 2631 if( blob_eq(&key, "extroot:") && blob_token(&line, &value) ){
ec56c69… drh 2632 /* extroot: DIRECTORY
ec56c69… drh 2633 **
ec56c69… drh 2634 ** Enables the /ext webpage to use sub-cgi rooted at DIRECTORY
ec56c69… drh 2635 */
4c3e172… danield 2636 g.zExtRoot = fossil_strdup(blob_str(&value));
236087b… drh 2637 blob_reset(&value);
859d6b1… drh 2638 continue;
859d6b1… drh 2639 }
859d6b1… drh 2640 if( blob_eq(&key, "timeout:") && blob_token(&line, &value) ){
859d6b1… drh 2641 /* timeout: SECONDS
859d6b1… drh 2642 **
859d6b1… drh 2643 ** Set an alarm() that kills the process after SECONDS. The
7979989… drh 2644 ** default value is FOSSIL_DEFAULT_TIMEOUT (600) seconds.
859d6b1… drh 2645 */
859d6b1… drh 2646 fossil_set_timeout(atoi(blob_str(&value)));
236087b… drh 2647 continue;
236087b… drh 2648 }
236087b… drh 2649 if( blob_eq(&key, "HOME:") && blob_token(&line, &value) ){
236087b… drh 2650 /* HOME: VALUE
236087b… drh 2651 **
236087b… drh 2652 ** Set CGI parameter "HOME" to VALUE. This is legacy. Use
236087b… drh 2653 ** setenv: instead.
236087b… drh 2654 */
236087b… drh 2655 cgi_setenv("HOME", blob_str(&value));
236087b… drh 2656 blob_reset(&value);
236087b… drh 2657 continue;
236087b… drh 2658 }
6362a98… stephan 2659 if( blob_eq(&key, "skin:") ){
ed36e2e… drh 2660 /* skin: LABEL
ed36e2e… drh 2661 **
ed36e2e… drh 2662 ** Use one of the built-in skins defined by LABEL. LABEL is the
ed36e2e… drh 2663 ** name of the subdirectory under the skins/ directory that holds
ed36e2e… drh 2664 ** the elements of the built-in skin. If LABEL does not match,
816f6c0… stephan 2665 ** this directive is a silent no-op. It may alternately be
816f6c0… stephan 2666 ** an absolute path to a directory which holds skin definition
6362a98… stephan 2667 ** files (header.txt, footer.txt, etc.). If LABEL is empty,
6362a98… stephan 2668 ** the skin stored in the CONFIG db table is used.
036a9d5… drh 2669 */
6362a98… stephan 2670 blob_token(&line, &value);
24e015d… drh 2671 fossil_free(skin_use_alternative(blob_str(&value), 1, SKIN_FROM_CGI));
036a9d5… drh 2672 blob_reset(&value);
036a9d5… drh 2673 continue;
036a9d5… drh 2674 }
036a9d5… drh 2675 if( blob_eq(&key, "jsmode:") && blob_token(&line, &value) ){
036a9d5… drh 2676 /* jsmode: MODE
036a9d5… drh 2677 **
19f2753… stephan 2678 ** Change how JavaScript resources are delivered with each HTML
036a9d5… drh 2679 ** page. MODE is "inline" to put all JS inline, or "separate" to
036a9d5… drh 2680 ** cause each JS file to be requested using a separate HTTP request,
036a9d5… drh 2681 ** or "bundled" to have all JS files to be fetched with a single
19f2753… stephan 2682 ** auxiliary HTTP request. Noting, however, that "single" might
19f2753… stephan 2683 ** actually mean more than one, depending on the script-timing
19f2753… stephan 2684 ** requirements of any given page.
036a9d5… drh 2685 */
036a9d5… drh 2686 builtin_set_js_delivery_mode(blob_str(&value),0);
19f2753… stephan 2687 blob_reset(&value);
19f2753… stephan 2688 continue;
19f2753… stephan 2689 }
73ca280… drh 2690 if( blob_eq(&key, "mainmenu:") && blob_token(&line, &value) ){
73ca280… drh 2691 /* mainmenu: FILENAME
73ca280… drh 2692 **
73ca280… drh 2693 ** Use the contents of FILENAME as the value of the site's
73ca280… drh 2694 ** "mainmenu" setting, overriding the contents (for this
73ca280… drh 2695 ** request) of the db-side setting or the hard-coded default.
73ca280… drh 2696 */
4c3e172… danield 2697 g.zMainMenuFile = fossil_strdup(blob_str(&value));
73ca280… drh 2698 blob_reset(&value);
73ca280… drh 2699 continue;
73ca280… drh 2700 }
4a7760e… drh 2701 if( blob_eq(&key, "cgi-debug:") && blob_token(&line, &value) ){
4a7760e… drh 2702 /* cgi-debug: FILENAME
20dc009… drh 2703 **
20dc009… drh 2704 ** Causes output from cgi_debug() and CGIDEBUG(()) calls to go
4a7760e… drh 2705 ** into FILENAME. Useful for debugging CGI configuration problems.
ed36e2e… drh 2706 */
4a7760e… drh 2707 char *zNow = cgi_iso8601_datestamp();
4a7760e… drh 2708 cgi_load_environment();
20dc009… drh 2709 g.fDebug = fossil_fopen(blob_str(&value), "ab");
ed36e2e… drh 2710 blob_reset(&value);
4a7760e… drh 2711 cgi_debug("-------- BEGIN cgi at %s --------\n", zNow);
4a7760e… drh 2712 fossil_free(zNow);
0204f4a… drh 2713 cgi_print_all(1,2,0);
ed36e2e… drh 2714 continue;
ed36e2e… drh 2715 }
bb0cedb… drh 2716 }
bb0cedb… drh 2717 blob_reset(&config);
bb0cedb… drh 2718 if( g.db==0 && g.zRepositoryName==0 && nRedirect==0 ){
bb0cedb… drh 2719 cgi_panic("Unable to find or open the project repository");
bb0cedb… drh 2720 }
bb0cedb… drh 2721 cgi_init();
bb0cedb… drh 2722 if( nRedirect ){
bb0cedb… drh 2723 redirect_web_page(nRedirect, azRedirect);
bb0cedb… drh 2724 }else{
87508e0… drh 2725 process_one_web_page(zNotFound, pFileGlob, allowRepoList);
bb0cedb… drh 2726 }
bb0cedb… drh 2727 }
bb0cedb… drh 2728
bb0cedb… drh 2729 /*
f62bedf… drh 2730 ** If g.argv[arg] exists then it is either the name of a repository
14c19fb… drh 2731 ** that will be used by a server, or else it is a directory that
f62bedf… drh 2732 ** contains multiple repositories that can be served. If g.argv[arg]
14c19fb… drh 2733 ** is a directory, the repositories it contains must be named
d76a980… drh 2734 ** "*.fossil". If g.argv[arg] does not exist, then we must be within
861f4b0… drh 2735 ** an open check-out and the repository to serve is the repository of
14c19fb… drh 2736 ** that check-out.
14c19fb… drh 2737 **
f62bedf… drh 2738 ** Open the repository to be served if it is known. If g.argv[arg] is
14c19fb… drh 2739 ** a directory full of repositories, then set g.zRepositoryName to
14c19fb… drh 2740 ** the name of that directory and the specific repository will be
14c19fb… drh 2741 ** opened later by process_one_web_page() based on the content of
14c19fb… drh 2742 ** the PATH_INFO variable.
a6e2ceb… drh 2743 **
a6e2ceb… drh 2744 ** If the fCreate flag is set, then create the repository if it
3c9dc14… jan.nijtmans 2745 ** does not already exist. Always use "auto" hash-policy in this case.
14c19fb… drh 2746 */
a6e2ceb… drh 2747 static void find_server_repository(int arg, int fCreate){
f62bedf… drh 2748 if( g.argc<=arg ){
14c19fb… drh 2749 db_must_be_within_tree();
a6e2ceb… drh 2750 }else{
a6e2ceb… drh 2751 const char *zRepo = g.argv[arg];
1772357… drh 2752 int isDir = file_isdir(zRepo, ExtFILE);
a6e2ceb… drh 2753 if( isDir==1 ){
4c3e172… danield 2754 g.zRepositoryName = fossil_strdup(zRepo);
a6e2ceb… drh 2755 file_simplify_name(g.zRepositoryName, -1, 0);
a6e2ceb… drh 2756 }else{
a6e2ceb… drh 2757 if( isDir==0 && fCreate ){
2ec9af6… jan.nijtmans 2758 const char *zPassword;
a6e2ceb… drh 2759 db_create_repository(zRepo);
a6e2ceb… drh 2760 db_open_repository(zRepo);
e1962ef… drh 2761 db_begin_transaction();
b4520f5… drh 2762 g.eHashPolicy = HPOLICY_SHA3;
b4520f5… drh 2763 db_set_int("hash-policy", HPOLICY_SHA3, 0);
5811ecd… jan.nijtmans 2764 db_initial_setup(0, "now", g.zLogin);
a6e2ceb… drh 2765 db_end_transaction(0);
2ec9af6… jan.nijtmans 2766 fossil_print("project-id: %s\n", db_get("project-code", 0));
2ec9af6… jan.nijtmans 2767 fossil_print("server-id: %s\n", db_get("server-code", 0));
2ec9af6… jan.nijtmans 2768 zPassword = db_text(0, "SELECT pw FROM user WHERE login=%Q", g.zLogin);
2ec9af6… jan.nijtmans 2769 fossil_print("admin-user: %s (initial password is \"%s\")\n",
2ec9af6… jan.nijtmans 2770 g.zLogin, zPassword);
593e801… preben 2771 hash_user_password(g.zLogin);
a6e2ceb… drh 2772 cache_initialize();
a6e2ceb… drh 2773 g.zLogin = 0;
a6e2ceb… drh 2774 g.userUid = 0;
a6e2ceb… drh 2775 }else{
a6e2ceb… drh 2776 db_open_repository(zRepo);
a6e2ceb… drh 2777 }
a6e2ceb… drh 2778 }
a6e2ceb… drh 2779 }
a6e2ceb… drh 2780 }
a6e2ceb… drh 2781
f41cf03… mistachkin 2782 #if USE_SEE
7aeeb30… mistachkin 2783 /*
7aeeb30… mistachkin 2784 ** This function attempts to parse a string value in the following
7aeeb30… mistachkin 2785 ** format:
7aeeb30… mistachkin 2786 **
7aeeb30… mistachkin 2787 ** "%lu:%p:%u"
7aeeb30… mistachkin 2788 **
7aeeb30… mistachkin 2789 ** There are three parts, which must be delimited by colons. The
7aeeb30… mistachkin 2790 ** first part is an unsigned long integer in base-10 (decimal) format.
7aeeb30… mistachkin 2791 ** The second part is a numerical representation of a native pointer,
7aeeb30… mistachkin 2792 ** in the appropriate implementation defined format. The third part
7aeeb30… mistachkin 2793 ** is an unsigned integer in base-10 (decimal) format.
7aeeb30… mistachkin 2794 **
7aeeb30… mistachkin 2795 ** If the specified value cannot be parsed, for any reason, a fatal
7aeeb30… mistachkin 2796 ** error will be raised and the process will be terminated.
7aeeb30… mistachkin 2797 */
7aeeb30… mistachkin 2798 void parse_pid_key_value(
f41cf03… mistachkin 2799 const char *zPidKey, /* The value to be parsed. */
f41cf03… mistachkin 2800 PID_T *pProcessId, /* The extracted process identifier. */
f41cf03… mistachkin 2801 LPVOID *ppAddress, /* The extracted pointer value. */
f41cf03… mistachkin 2802 SIZE_T *pnSize /* The extracted size value. */
7aeeb30… mistachkin 2803 ){
f41cf03… mistachkin 2804 unsigned long processId = 0;
7aeeb30… mistachkin 2805 unsigned int nSize = 0;
f41cf03… mistachkin 2806 if( sscanf(zPidKey, "%lu:%p:%u", &processId, ppAddress, &nSize)==3 ){
f41cf03… mistachkin 2807 *pProcessId = (PID_T)processId;
7aeeb30… mistachkin 2808 *pnSize = (SIZE_T)nSize;
7aeeb30… mistachkin 2809 }else{
99fcc43… drh 2810 fossil_fatal("failed to parse pid key");
7aeeb30… mistachkin 2811 }
7aeeb30… mistachkin 2812 }
99fcc43… drh 2813 #endif
99fcc43… drh 2814
99fcc43… drh 2815 /*
57d8a71… mistachkin 2816 ** WEBPAGE: test-pid
57d8a71… mistachkin 2817 **
57d8a71… mistachkin 2818 ** Return the process identifier of the running Fossil server instance.
57d8a71… mistachkin 2819 **
57d8a71… mistachkin 2820 ** Query parameters:
57d8a71… mistachkin 2821 **
57d8a71… mistachkin 2822 ** usepidkey When present and available, also return the
57d8a71… mistachkin 2823 ** address and size, within this server process,
57d8a71… mistachkin 2824 ** of the saved database encryption key. This
f41cf03… mistachkin 2825 ** is only supported when using SEE on Windows
f41cf03… mistachkin 2826 ** or Linux.
57d8a71… mistachkin 2827 */
57d8a71… mistachkin 2828 void test_pid_page(void){
57d8a71… mistachkin 2829 login_check_credentials();
57d8a71… mistachkin 2830 if( !g.perm.Setup ){ login_needed(0); return; }
f41cf03… mistachkin 2831 #if USE_SEE
57d8a71… mistachkin 2832 if( P("usepidkey")!=0 ){
57d8a71… mistachkin 2833 if( g.zPidKey ){
57d8a71… mistachkin 2834 @ %s(g.zPidKey)
57d8a71… mistachkin 2835 return;
57d8a71… mistachkin 2836 }else{
57d8a71… mistachkin 2837 const char *zSavedKey = db_get_saved_encryption_key();
57d8a71… mistachkin 2838 size_t savedKeySize = db_get_saved_encryption_key_size();
57d8a71… mistachkin 2839 if( zSavedKey!=0 && savedKeySize>0 ){
f41cf03… mistachkin 2840 @ %lu(GETPID()):%p(zSavedKey):%u(savedKeySize)
57d8a71… mistachkin 2841 return;
57d8a71… mistachkin 2842 }
57d8a71… mistachkin 2843 }
57d8a71… mistachkin 2844 }
57d8a71… mistachkin 2845 #endif
57d8a71… mistachkin 2846 @ %d(GETPID())
57d8a71… mistachkin 2847 }
57d8a71… mistachkin 2848
57d8a71… mistachkin 2849 /*
f6263bb… drh 2850 ** Check for options to "fossil server" or "fossil ui" that imply that
f6263bb… drh 2851 ** SSL should be used, and initialize the SSL decoder.
f6263bb… drh 2852 */
f6263bb… drh 2853 static void decode_ssl_options(void){
f6263bb… drh 2854 #if FOSSIL_ENABLE_SSL
f6263bb… drh 2855 const char *zCertFile = 0;
d7008b3… drh 2856 const char *zKeyFile = 0;
d7008b3… drh 2857 zCertFile = find_option("cert",0,1);
d7008b3… drh 2858 zKeyFile = find_option("pkey",0,1);
f6263bb… drh 2859 if( zCertFile ){
f6263bb… drh 2860 g.httpUseSSL = 1;
d7008b3… drh 2861 ssl_init_server(zCertFile, zKeyFile);
d7008b3… drh 2862 }else if( zKeyFile ){
d7008b3… drh 2863 fossil_fatal("--pkey without a corresponding --cert");
f6263bb… drh 2864 }
f6263bb… drh 2865 #endif
f6263bb… drh 2866 }
f6263bb… drh 2867
f6263bb… drh 2868 /*
841772c… drh 2869 ** COMMAND: http*
841772c… drh 2870 **
f62bedf… drh 2871 ** Usage: %fossil http ?REPOSITORY? ?OPTIONS?
6607844… drh 2872 **
6607844… drh 2873 ** Handle a single HTTP request appearing on stdin. The resulting webpage
6607844… drh 2874 ** is delivered on stdout. This method is used to launch an HTTP request
42c4d04… drh 2875 ** handler from inetd, for example. The REPOSITORY argument is the name of
42c4d04… drh 2876 ** the repository.
841772c… drh 2877 **
2c8557c… drh 2878 ** If REPOSITORY is a directory that contains one or more repositories,
d76a980… drh 2879 ** either directly in REPOSITORY itself or in subdirectories, and
d76a980… drh 2880 ** with names of the form "*.fossil" then a prefix of the URL pathname
2c8557c… drh 2881 ** selects from among the various repositories. If the pathname does
841772c… drh 2882 ** not select a valid repository and the --notfound option is available,
841772c… drh 2883 ** then the server redirects (HTTP code 302) to the URL of --notfound.
99ab9fb… drh 2884 ** When REPOSITORY is a directory, the pathname must contain only
99ab9fb… drh 2885 ** alphanumerics, "_", "/", "-" and "." and no "-" may occur after a "/"
99ab9fb… drh 2886 ** and every "." must be surrounded on both sides by alphanumerics or else
99ab9fb… drh 2887 ** a 404 error is returned. Static content files in the directory are
33d3bf3… km 2888 ** returned if they match comma-separated GLOB pattern specified by --files
2c8557c… drh 2889 ** and do not match "*.fossil*" and have a well-known suffix.
75ea5ac… drh 2890 **
75ea5ac… drh 2891 ** Options:
f6263bb… drh 2892 ** --acme Deliver files from the ".well-known" subdirectory
2e56ef4… km 2893 ** --baseurl URL Base URL (useful with reverse proxies)
d7008b3… drh 2894 ** --cert FILE Use TLS (HTTPS) encryption with the certificate (the
d7008b3… drh 2895 ** fullchain.pem) taken from FILE.
f6263bb… drh 2896 ** --chroot DIR Use directory for chroot instead of repository path.
f6263bb… drh 2897 ** --ckout-alias N Treat URIs of the form /doc/N/... as if they were
f6263bb… drh 2898 ** /doc/ckout/...
42c4d04… drh 2899 ** --extroot DIR Document root for the /ext extension mechanism
33d3bf3… km 2900 ** --files GLOB Comma-separated glob patterns for static files to serve
42c4d04… drh 2901 ** --host NAME DNS Hostname of the server
42c4d04… drh 2902 ** --https The HTTP request originated from https but has already
42c4d04… drh 2903 ** been decoded by a reverse proxy. Hence, URLs created
42c4d04… drh 2904 ** by Fossil should use "https:" rather than "http:".
f6263bb… drh 2905 ** --in FILE Take input from FILE instead of standard input
f6263bb… drh 2906 ** --ipaddr ADDR Assume the request comes from the given IP address
19f2753… stephan 2907 ** --jsmode MODE Determine how JavaScript is delivered with pages.
036a9d5… drh 2908 ** Mode can be one of:
19f2753… stephan 2909 ** inline All JavaScript is inserted inline at
19f2753… stephan 2910 ** one or more points in the HTML file.
036a9d5… drh 2911 ** separate Separate HTTP requests are made for
19f2753… stephan 2912 ** each JavaScript file.
19f2753… stephan 2913 ** bundled Groups JavaScript files into one or
19f2753… stephan 2914 ** more bundled requests which
19f2753… stephan 2915 ** concatenate scripts together.
19f2753… stephan 2916 ** Depending on the needs of any given page, inline
19f2753… stephan 2917 ** and bundled modes might result in a single
19f2753… stephan 2918 ** amalgamated script or several, but both approaches
19f2753… stephan 2919 ** result in fewer HTTP requests than the separate mode.
42c4d04… drh 2920 ** --localauth Connections from localhost are given "setup"
2e56ef4… km 2921 ** privileges without having to log in
f6263bb… drh 2922 ** --mainmenu FILE Override the mainmenu config setting with the contents
2e56ef4… km 2923 ** of the given file
42c4d04… drh 2924 ** --nocompress Do not compress HTTP replies
42c4d04… drh 2925 ** --nodelay Omit backoffice processing if it would delay
f6263bb… drh 2926 ** process exit
42c4d04… drh 2927 ** --nojail Drop root privilege but do not enter the chroot jail
42c4d04… drh 2928 ** --nossl Do not do http: to https: redirects, regardless of
42c4d04… drh 2929 ** the redirect-to-https setting.
2e56ef4… km 2930 ** --notfound URL Use URL as the "HTTP 404, object not found" page
275da70… danield 2931 ** --out FILE Write the HTTP reply to FILE instead of to
42c4d04… drh 2932 ** standard output
2e56ef4… km 2933 ** --pkey FILE Read the private key used for TLS from FILE
f6263bb… drh 2934 ** --repolist If REPOSITORY is directory, URL "/" lists all repos
f6263bb… drh 2935 ** --scgi Interpret input as SCGI rather than HTTP
816f6c0… stephan 2936 ** --skin LABEL Use override skin LABEL. Use an empty string ("")
816f6c0… stephan 2937 ** to force use of the current local skin config.
42c4d04… drh 2938 ** --th-trace Trace TH1 execution (for debugging purposes)
f6263bb… drh 2939 ** --usepidkey Use saved encryption key from parent process. This is
f41cf03… mistachkin 2940 ** only necessary when using SEE on Windows or Linux.
75ea5ac… drh 2941 **
e2c161b… danield 2942 ** See also: [[cgi]], [[server]], [[winsrv]] [Windows only]
841772c… drh 2943 */
841772c… drh 2944 void cmd_http(void){
afc2f5e… drh 2945 const char *zIpAddr = 0;
841772c… drh 2946 const char *zNotFound;
841772c… drh 2947 const char *zHost;
ecb85f6… drh 2948 const char *zAltBase;
2c8557c… drh 2949 const char *zFileGlob;
5294302… drh 2950 const char *zInFile;
5294302… drh 2951 const char *zOutFile;
bf6be1e… mistachkin 2952 const char *zChRoot;
a2e7472… drh 2953 int useSCGI;
c5c9f16… drh 2954 int noJail;
87508e0… drh 2955 int allowRepoList;
11d3d72… mistachkin 2956
11d3d72… mistachkin 2957 Th_InitTraceLog();
036a9d5… drh 2958 builtin_set_js_delivery_mode(find_option("jsmode",0,1),0);
2c8557c… drh 2959
2c8557c… drh 2960 /* The winhttp module passes the --files option as --files-urlenc with
3ba9a4d… jan.nijtmans 2961 ** the argument being URL encoded, to avoid wildcard expansion in the
2c8557c… drh 2962 ** shell. This option is for internal use and is undocumented.
2c8557c… drh 2963 */
2c8557c… drh 2964 zFileGlob = find_option("files-urlenc",0,1);
2c8557c… drh 2965 if( zFileGlob ){
4c3e172… danield 2966 char *z = fossil_strdup(zFileGlob);
2c8557c… drh 2967 dehttpize(z);
2c8557c… drh 2968 zFileGlob = z;
2c8557c… drh 2969 }else{
2c8557c… drh 2970 zFileGlob = find_option("files",0,1);
2c8557c… drh 2971 }
5ce5ff5… drh 2972 skin_override();
dbb5e2d… drh 2973 zNotFound = find_option("notfound", 0, 1);
bf6be1e… mistachkin 2974 zChRoot = find_option("chroot",0,1);
c5c9f16… drh 2975 noJail = find_option("nojail",0,0)!=0;
87508e0… drh 2976 allowRepoList = find_option("repolist",0,0)!=0;
dbb5e2d… drh 2977 g.useLocalauth = find_option("localauth", 0, 0)!=0;
dbb5e2d… drh 2978 g.sslNotAvailable = find_option("nossl", 0, 0)!=0;
cfc7aca… drh 2979 g.fNoHttpCompress = find_option("nocompress",0,0)!=0;
ec56c69… drh 2980 g.zExtRoot = find_option("extroot",0,1);
dc1121f… drh 2981 g.zCkoutAlias = find_option("ckout-alias",0,1);
a8e33fb… drh 2982 g.zReqType = "HTTP";
5294302… drh 2983 zInFile = find_option("in",0,1);
5294302… drh 2984 if( zInFile ){
afb1e5f… drh 2985 backoffice_disable();
5294302… drh 2986 g.httpIn = fossil_fopen(zInFile, "rb");
5294302… drh 2987 if( g.httpIn==0 ) fossil_fatal("cannot open \"%s\" for reading", zInFile);
5294302… drh 2988 }else{
5294302… drh 2989 g.httpIn = stdin;
be1c4d4… drh 2990 #if defined(_WIN32)
be1c4d4… drh 2991 _setmode(_fileno(stdin), _O_BINARY);
be1c4d4… drh 2992 #endif
5294302… drh 2993 }
5294302… drh 2994 zOutFile = find_option("out",0,1);
5294302… drh 2995 if( zOutFile ){
5294302… drh 2996 g.httpOut = fossil_fopen(zOutFile, "wb");
5294302… drh 2997 if( g.httpOut==0 ) fossil_fatal("cannot open \"%s\" for writing", zOutFile);
5294302… drh 2998 }else{
5294302… drh 2999 g.httpOut = stdout;
be1c4d4… drh 3000 #if defined(_WIN32)
be1c4d4… drh 3001 _setmode(_fileno(stdout), _O_BINARY);
be1c4d4… drh 3002 #endif
5294302… drh 3003 }
5294302… drh 3004 zIpAddr = find_option("ipaddr",0,1);
f38866e… drh 3005 #if defined(_WIN32)
f38866e… drh 3006 /* The undocumented option "--as NAME" causes NAME to become
f38866e… drh 3007 ** the fake command name. This only happens on Windows and only
f38866e… drh 3008 ** if preceded by --in, --out, and --ipaddr. It is a work-around
f38866e… drh 3009 ** to get the original command-name down into the "http" command that
f38866e… drh 3010 ** is run in a subprocess to manage HTTP requests on Windows for
f38866e… drh 3011 ** commands like "fossil ui" and "fossil server".
f38866e… drh 3012 */
f38866e… drh 3013 if( zInFile && zOutFile && zIpAddr ){
f38866e… drh 3014 const char *z = find_option("as",0,1);
f38866e… drh 3015 if( z ) g.zCmdName = z;
f38866e… drh 3016 }
f38866e… drh 3017 #endif
d4acf5c… jan.nijtmans 3018 useSCGI = find_option("scgi", 0, 0)!=0;
a8e33fb… drh 3019 if( useSCGI ) g.zReqType = "SCGI";
d4acf5c… jan.nijtmans 3020 zAltBase = find_option("baseurl", 0, 1);
5294302… drh 3021 if( find_option("nodelay",0,0)!=0 ) backoffice_no_delay();
d4acf5c… jan.nijtmans 3022 if( zAltBase ) set_base_url(zAltBase);
afc2f5e… drh 3023 if( find_option("https",0,0)!=0 ){
afc2f5e… drh 3024 zIpAddr = fossil_getenv("REMOTE_HOST"); /* From stunnel */
afc2f5e… drh 3025 cgi_replace_parameter("HTTPS","on");
afc2f5e… drh 3026 }
d4acf5c… jan.nijtmans 3027 zHost = find_option("host", 0, 1);
d4acf5c… jan.nijtmans 3028 if( zHost ) cgi_replace_parameter("HTTP_HOST",zHost);
73ca280… drh 3029 g.zMainMenuFile = find_option("mainmenu",0,1);
73ca280… drh 3030 if( g.zMainMenuFile!=0 && file_size(g.zMainMenuFile,ExtFILE)<0 ){
73ca280… drh 3031 fossil_fatal("Cannot read --mainmenu file %s", g.zMainMenuFile);
73ca280… drh 3032 }
f6263bb… drh 3033 decode_ssl_options();
f6263bb… drh 3034 if( find_option("acme",0,0)!=0 ) g.fAllowACME = 1;
d4acf5c… jan.nijtmans 3035
d4acf5c… jan.nijtmans 3036 /* We should be done with options.. */
d4acf5c… jan.nijtmans 3037 verify_all_options();
f6263bb… drh 3038 if( g.httpUseSSL ){
f6263bb… drh 3039 if( useSCGI ){
f6263bb… drh 3040 fossil_fatal("SSL not (yet) supported for SCGI");
f6263bb… drh 3041 }
f6263bb… drh 3042 if( g.fSshClient & CGI_SSH_CLIENT ){
f6263bb… drh 3043 fossil_fatal("SSL not compatible with SSH");
f6263bb… drh 3044 }
f6263bb… drh 3045 if( zInFile || zOutFile ){
f6263bb… drh 3046 fossil_fatal("SSL usable only on a socket");
f6263bb… drh 3047 }
f6263bb… drh 3048 cgi_replace_parameter("HTTPS","on");
f6263bb… drh 3049 }
d4acf5c… jan.nijtmans 3050
5294302… drh 3051 if( g.argc!=2 && g.argc!=3 ) usage("?REPOSITORY?");
648dc3e… drh 3052 g.cgiOutput = 1;
d4acf5c… jan.nijtmans 3053 g.fullHttpReply = 1;
5294302… drh 3054 find_server_repository(2, 0);
dbb5e2d… drh 3055 if( zIpAddr==0 ){
dbb5e2d… drh 3056 zIpAddr = cgi_ssh_remote_addr(0);
dbb5e2d… drh 3057 if( zIpAddr && zIpAddr[0] ){
dbb5e2d… drh 3058 g.fSshClient |= CGI_SSH_CLIENT;
dbb5e2d… drh 3059 }
dbb5e2d… drh 3060 }
6f92ad9… wyoung 3061 g.zRepositoryName = enter_chroot_jail(
6f92ad9… wyoung 3062 zChRoot ? zChRoot : g.zRepositoryName, noJail);
dbb5e2d… drh 3063 if( useSCGI ){
dbb5e2d… drh 3064 cgi_handle_scgi_request();
dbb5e2d… drh 3065 }else if( g.fSshClient & CGI_SSH_CLIENT ){
dbb5e2d… drh 3066 ssh_request_loop(zIpAddr, glob_create(zFileGlob));
dbb5e2d… drh 3067 }else{
f6263bb… drh 3068 #if FOSSIL_ENABLE_SSL
f6263bb… drh 3069 if( g.httpUseSSL ){
7a3bf55… drh 3070 g.httpSSLConn = ssl_new_server(0);
f6263bb… drh 3071 }
f6263bb… drh 3072 #endif
dbb5e2d… drh 3073 cgi_handle_http_request(zIpAddr);
dbb5e2d… drh 3074 }
87508e0… drh 3075 process_one_web_page(zNotFound, glob_create(zFileGlob), allowRepoList);
f6263bb… drh 3076 #if FOSSIL_ENABLE_SSL
f6263bb… drh 3077 if( g.httpUseSSL && g.httpSSLConn ){
f6263bb… drh 3078 ssl_close_server(g.httpSSLConn);
f6263bb… drh 3079 g.httpSSLConn = 0;
f6263bb… drh 3080 }
f6263bb… drh 3081 #endif /* FOSSIL_ENABLE_SSL */
dbb5e2d… drh 3082 }
dbb5e2d… drh 3083
dbb5e2d… drh 3084 /*
dbb5e2d… drh 3085 ** Process all requests in a single SSH connection if possible.
dbb5e2d… drh 3086 */
dbb5e2d… drh 3087 void ssh_request_loop(const char *zIpAddr, Glob *FileGlob){
a138dc9… drh 3088 blob_zero(&g.cgiIn);
dbb5e2d… drh 3089 do{
dbb5e2d… drh 3090 cgi_handle_ssh_http_request(zIpAddr);
87508e0… drh 3091 process_one_web_page(0, FileGlob, 0);
dbb5e2d… drh 3092 blob_reset(&g.cgiIn);
dbb5e2d… drh 3093 } while ( g.fSshClient & CGI_SSH_FOSSIL ||
dbb5e2d… drh 3094 g.fSshClient & CGI_SSH_COMPAT );
dbb5e2d… drh 3095 }
dbb5e2d… drh 3096
dbb5e2d… drh 3097 /*
dbb5e2d… drh 3098 ** COMMAND: test-http
52e3d9d… mistachkin 3099 **
9834d4d… drh 3100 ** Works like the [[http]] command but gives setup permission to all users,
9834d4d… drh 3101 ** or whatever permission is described by "--usercap CAP".
9834d4d… drh 3102 **
9834d4d… drh 3103 ** This command can used for interactive debugging of web pages. For
9834d4d… drh 3104 ** example, one can put a simple HTTP request in a file like this:
9834d4d… drh 3105 **
9834d4d… drh 3106 ** echo 'GET /timeline' >request.txt
9834d4d… drh 3107 **
9834d4d… drh 3108 ** Then run (in a debugger) a command like this:
9834d4d… drh 3109 **
9cabc13… florian 3110 ** fossil test-http <request.txt
9834d4d… drh 3111 **
9834d4d… drh 3112 ** This command is also used internally by the "ssh" sync protocol. Some
9834d4d… drh 3113 ** special processing to support sync happens when this command is run
9834d4d… drh 3114 ** and the SSH_CONNECTION environment variable is set. Use the --test
9834d4d… drh 3115 ** option on interactive sessions to avoid that special processing when
9834d4d… drh 3116 ** using this command interactively over SSH. A better solution would be
9834d4d… drh 3117 ** to use a different command for "ssh" sync, but we cannot do that without
9834d4d… drh 3118 ** breaking legacy.
dbb5e2d… drh 3119 **
52e3d9d… mistachkin 3120 ** Options:
33d3bf3… km 3121 ** --csrf-safe N Set cgi_csrf_safe() to return N
1a0b304… drh 3122 ** --nobody Pretend to be user "nobody"
2f9920a… drh 3123 ** --ssh-sim Pretend to be over an SSH connection
9834d4d… drh 3124 ** --test Do not do special "sync" processing when operating
2512d2d… km 3125 ** over an SSH link
4cb50c4… stephan 3126 ** --th-trace Trace TH1 execution (for debugging purposes)
ec5efce… wyoung 3127 ** --usercap CAP User capability string (Default: "sxy")
dbb5e2d… drh 3128 */
dbb5e2d… drh 3129 void cmd_test_http(void){
dbb5e2d… drh 3130 const char *zIpAddr; /* IP address of remote client */
c00e912… drh 3131 const char *zUserCap;
9834d4d… drh 3132 int bTest = 0;
056c83d… drh 3133 const char *zCsrfSafe = find_option("csrf-safe",0,1);
dbb5e2d… drh 3134
2f9920a… drh 3135 if( find_option("ssh-sim",0,0)!=0 ){
2f9920a… drh 3136 putenv("SSH_CONNECTION=127.0.0.1 12345 127.0.0.2 23456");
2f9920a… drh 3137 }
dbb5e2d… drh 3138 Th_InitTraceLog();
056c83d… drh 3139 if( zCsrfSafe ) g.okCsrf = atoi(zCsrfSafe);
c00e912… drh 3140 zUserCap = find_option("usercap",0,1);
1a0b304… drh 3141 if( !find_option("nobody",0,0) ){
1a0b304… drh 3142 if( zUserCap==0 ){
1a0b304… drh 3143 g.useLocalauth = 1;
1a0b304… drh 3144 zUserCap = "sxy";
1a0b304… drh 3145 }
1a0b304… drh 3146 login_set_capabilities(zUserCap, 0);
c00e912… drh 3147 }
9834d4d… drh 3148 bTest = find_option("test",0,0)!=0;
dbb5e2d… drh 3149 g.httpIn = stdin;
dbb5e2d… drh 3150 g.httpOut = stdout;
28b15b4… drh 3151 fossil_binary_mode(g.httpOut);
28b15b4… drh 3152 fossil_binary_mode(g.httpIn);
ec56c69… drh 3153 g.zExtRoot = find_option("extroot",0,1);
a6e2ceb… drh 3154 find_server_repository(2, 0);
a8e33fb… drh 3155 g.zReqType = "HTTP";
dbb5e2d… drh 3156 g.cgiOutput = 1;
cfc7aca… drh 3157 g.fNoHttpCompress = 1;
dbb5e2d… drh 3158 g.fullHttpReply = 1;
ea6365a… drh 3159 g.sslNotAvailable = 1; /* Avoid attempts to redirect */
9834d4d… drh 3160 zIpAddr = bTest ? 0 : cgi_ssh_remote_addr(0);
dbb5e2d… drh 3161 if( zIpAddr && zIpAddr[0] ){
dbb5e2d… drh 3162 g.fSshClient |= CGI_SSH_CLIENT;
dbb5e2d… drh 3163 ssh_request_loop(zIpAddr, 0);
dbb5e2d… drh 3164 }else{
dbb5e2d… drh 3165 cgi_set_parameter("REMOTE_ADDR", "127.0.0.1");
dbb5e2d… drh 3166 cgi_handle_http_request(0);
8f5399b… drh 3167 process_one_web_page(0, 0, 1);
8f5399b… drh 3168 }
8f5399b… drh 3169 }
8f5399b… drh 3170
859d6b1… drh 3171 /*
859d6b1… drh 3172 ** Respond to a SIGALRM by writing a message to the error log (if there
859d6b1… drh 3173 ** is one) and exiting.
859d6b1… drh 3174 */
1d7afcd… drh 3175 #ifndef _WIN32
6c3d370… drh 3176 static int nAlarmSeconds = 0;
859d6b1… drh 3177 static void sigalrm_handler(int x){
6c3d370… drh 3178 sqlite3_uint64 tmUser = 0, tmKernel = 0;
6c3d370… drh 3179 fossil_cpu_times(&tmUser, &tmKernel);
af2f7d1… drh 3180 if( fossil_strcmp(g.zPhase, "web-page reply")==0
d6fdfd3… drh 3181 && tmUser+tmKernel<10000000
af2f7d1… drh 3182 ){
af2f7d1… drh 3183 /* Do not log time-outs during web-page reply unless more than
d6fdfd3… drh 3184 ** 10 seconds of CPU time has been consumed */
af2f7d1… drh 3185 return;
af2f7d1… drh 3186 }
48c06b0… drh 3187 fossil_panic("Timeout after %d seconds during %s"
48c06b0… drh 3188 " - user %,llu µs, sys %,llu µs",
48c06b0… drh 3189 nAlarmSeconds, g.zPhase, tmUser, tmKernel);
859d6b1… drh 3190 }
1d7afcd… drh 3191 #endif
859d6b1… drh 3192
859d6b1… drh 3193 /*
859d6b1… drh 3194 ** Arrange to timeout using SIGALRM after N seconds. Or if N==0, cancel
859d6b1… drh 3195 ** any pending timeout.
859d6b1… drh 3196 **
859d6b1… drh 3197 ** Bugs:
859d6b1… drh 3198 ** (1) This only works on unix systems.
859d6b1… drh 3199 ** (2) Any call to sleep() or sqlite3_sleep() will cancel the alarm.
859d6b1… drh 3200 */
859d6b1… drh 3201 void fossil_set_timeout(int N){
859d6b1… drh 3202 #ifndef _WIN32
859d6b1… drh 3203 signal(SIGALRM, sigalrm_handler);
859d6b1… drh 3204 alarm(N);
6c3d370… drh 3205 nAlarmSeconds = N;
859d6b1… drh 3206 #endif
05ec15c… drh 3207 }
841772c… drh 3208
841772c… drh 3209 /*
841772c… drh 3210 ** COMMAND: server*
841772c… drh 3211 ** COMMAND: ui
841772c… drh 3212 **
75ea5ac… drh 3213 ** Usage: %fossil server ?OPTIONS? ?REPOSITORY?
8bfd995… rberteig 3214 ** or: %fossil ui ?OPTIONS? ?REPOSITORY?
841772c… drh 3215 **
841772c… drh 3216 ** Open a socket and begin listening and responding to HTTP requests on
841772c… drh 3217 ** TCP port 8080, or on any other TCP port defined by the -P or
301bc21… drh 3218 ** --port option. The optional REPOSITORY argument is the name of the
301bc21… drh 3219 ** Fossil repository to be served. The REPOSITORY argument may be omitted
bc36fdc… danield 3220 ** if the working directory is within an open check-out, in which case the
bc36fdc… danield 3221 ** repository associated with that check-out is used.
dfb6897… drh 3222 **
dfb6897… drh 3223 ** The "ui" command automatically starts a web browser after initializing
0e42cc1… drh 3224 ** the web server. The "ui" command also binds to 127.0.0.1 and so will
0e42cc1… drh 3225 ** only process HTTP traffic from the local machine.
0e42cc1… drh 3226 **
301bc21… drh 3227 ** If REPOSITORY is a directory name which is the root of a
bc36fdc… danield 3228 ** check-out, then use the repository associated with that check-out.
301bc21… drh 3229 ** This only works for the "fossil ui" command, not the "fossil server"
301bc21… drh 3230 ** command.
301bc21… drh 3231 **
301bc21… drh 3232 ** If REPOSITORY begins with a "HOST:" or "USER@HOST:" prefix, then
301bc21… drh 3233 ** the command is run on the remote host specified and the results are
301bc21… drh 3234 ** tunneled back to the local machine via SSH. This feature only works for
d22e2b3… drh 3235 ** the "fossil ui" command, not the "fossil server" command. The name of the
b2b373f… andybradford 3236 ** fossil executable on the remote host is specified by the --fossilcmd
b2b373f… andybradford 3237 ** option, or if there is no --fossilcmd, it first tries "fossil" and if it
b2b373f… andybradford 3238 ** is not found in the default $PATH set by SSH on the remote, it then adds
b2b373f… andybradford 3239 ** "$HOME/bin:/usr/local/bin:/opt/homebrew/bin" to the PATH and tries again to
b2b373f… andybradford 3240 ** run "fossil".
301bc21… drh 3241 **
301bc21… drh 3242 ** REPOSITORY may also be a directory (aka folder) that contains one or
b0592fe… andygoth 3243 ** more repositories with names ending in ".fossil". In this case, a
b0592fe… andygoth 3244 ** prefix of the URL pathname is used to search the directory for an
2c8557c… drh 3245 ** appropriate repository. To thwart mischief, the pathname in the URL must
99ab9fb… drh 3246 ** contain only alphanumerics, "_", "/", "-", and ".", and no "-" may
99ab9fb… drh 3247 ** occur after "/", and every "." must be surrounded on both sides by
99ab9fb… drh 3248 ** alphanumerics. Any pathname that does not satisfy these constraints
2c8557c… drh 3249 ** results in a 404 error. Files in REPOSITORY that match the comma-separated
2c8557c… drh 3250 ** list of glob patterns given by --files and that have known suffixes
2c8557c… drh 3251 ** such as ".txt" or ".html" or ".jpeg" and do not match the pattern
2c8557c… drh 3252 ** "*.fossil*" will be served as static content. With the "ui" command,
2c8557c… drh 3253 ** the REPOSITORY can only be a directory if the --notfound option is
2c8557c… drh 3254 ** also present.
2c8557c… drh 3255 **
f3acbe4… drh 3256 ** For the special case REPOSITORY name of "/", the global configuration
239b4c1… drh 3257 ** database is consulted for a list of all known repositories. The --repolist
76f65b4… drh 3258 ** option is implied by this special case. The "fossil ui /" command is
76f65b4… drh 3259 ** equivalent to "fossil all ui". To see all repositories owned by "user"
76f65b4… drh 3260 ** on machine "remote" via ssh, run "fossil ui user@remote:/".
239b4c1… drh 3261 **
f7a3c6d… drh 3262 ** By default, the "ui" command provides full administrative access without
be7fdc7… drh 3263 ** having to log in. This can be disabled by turning off the "localauth"
be7fdc7… drh 3264 ** setting. Automatic login for the "server" command is available if the
be7fdc7… drh 3265 ** --localauth option is present and the "localauth" setting is off and the
d976b47… drh 3266 ** connection is from localhost. The "ui" command also enables --repolist
d976b47… drh 3267 ** by default.
75ea5ac… drh 3268 **
75ea5ac… drh 3269 ** Options:
2e56ef4… km 3270 ** --acme Deliver files from the ".well-known" subdirectory
ecb85f6… drh 3271 ** --baseurl URL Use URL as the base (useful for reverse proxies)
d7008b3… drh 3272 ** --cert FILE Use TLS (HTTPS) encryption with the certificate (the
d7008b3… drh 3273 ** fullchain.pem) taken from FILE.
2e56ef4… km 3274 ** --chroot DIR Use directory for chroot instead of repository path
dc1121f… drh 3275 ** --ckout-alias NAME Treat URIs of the form /doc/NAME/... as if they were
dc1121f… drh 3276 ** /doc/ckout/...
a6e2ceb… drh 3277 ** --create Create a new REPOSITORY if it does not already exist
1e64c2c… drh 3278 ** --errorlog FILE Append HTTP error messages to FILE
36c7984… drh 3279 ** --extpage FILE Shortcut for "--extroot DIR --page ext/TAIL" where
36c7984… drh 3280 ** DIR is the directory holding FILE and TAIL is the
36c7984… drh 3281 ** filename at the end of FILE. Only works for "ui".
ec56c69… drh 3282 ** --extroot DIR Document root for the /ext extension mechanism
2c8557c… drh 3283 ** --files GLOBLIST Comma-separated list of glob patterns for static files
d22e2b3… drh 3284 ** --fossilcmd PATH The pathname of the "fossil" executable on the remote
d22e2b3… drh 3285 ** system when REPOSITORY is remote.
763758d… drh 3286 ** --from PATH Use PATH as the diff baseline for the /ckout page
2e56ef4… km 3287 ** --localauth Enable automatic login for requests from localhost
2e56ef4… km 3288 ** --localhost Listen on 127.0.0.1 only (always true for "ui")
3c602dd… drh 3289 ** --https Indicates that the input is coming through a reverse
3c602dd… drh 3290 ** proxy that has already translated HTTPS into HTTP.
19f2753… stephan 3291 ** --jsmode MODE Determine how JavaScript is delivered with pages.
036a9d5… drh 3292 ** Mode can be one of:
19f2753… stephan 3293 ** inline All JavaScript is inserted inline at
036a9d5… drh 3294 ** the end of the HTML file.
036a9d5… drh 3295 ** separate Separate HTTP requests are made for
19f2753… stephan 3296 ** each JavaScript file.
036a9d5… drh 3297 ** bundled One single separate HTTP fetches all
19f2753… stephan 3298 ** JavaScript concatenated together.
19f2753… stephan 3299 ** Depending on the needs of any given page, inline
19f2753… stephan 3300 ** and bundled modes might result in a single
19f2753… stephan 3301 ** amalgamated script or several, but both approaches
19f2753… stephan 3302 ** result in fewer HTTP requests than the separate mode.
76f65b4… drh 3303 ** --mainmenu FILE Override the mainmenu config setting with the contents
2e56ef4… km 3304 ** of the given file
05ec15c… drh 3305 ** --max-latency N Do not let any single HTTP request run for more than N
05ec15c… drh 3306 ** seconds (only works on unix)
1431eba… florian 3307 ** -B|--nobrowser Do not automatically launch a web-browser for the
2e56ef4… km 3308 ** "fossil ui" command
cfc7aca… drh 3309 ** --nocompress Do not compress HTTP replies
c5c9f16… drh 3310 ** --nojail Drop root privileges but do not enter the chroot jail
2e56ef4… km 3311 ** --nossl Do not force redirects to SSL even if the repository
d7008b3… drh 3312 ** setting "redirect-to-https" requests it. This is set
d7008b3… drh 3313 ** by default for the "ui" command.
d7008b3… drh 3314 ** --notfound URL Redirect to URL if a page is not found.
1431eba… florian 3315 ** -p|--page PAGE Start "ui" on PAGE. ex: --page "timeline?y=ci"
2e56ef4… km 3316 ** --pkey FILE Read the private key used for TLS from FILE
3815c85… stephan 3317 ** -P|--port [IP:]PORT Listen on the given IP (optional) and port
2e56ef4… km 3318 ** --repolist If REPOSITORY is dir, URL "/" lists repos
a2e7472… drh 3319 ** --scgi Accept SCGI rather than HTTP
699b9ca… stephan 3320 ** --skin LABEL Use override skin LABEL, or the site's default skin if
699b9ca… stephan 3321 ** LABEL is an empty string.
effdada… drh 3322 ** --socket-mode MODE File permissions to set for the unix socket created
effdada… drh 3323 ** by the --socket-name option.
effdada… drh 3324 ** --socket-name NAME Use a unix-domain socket called NAME instead of a
effdada… drh 3325 ** TCP/IP socket.
effdada… drh 3326 ** --socket-owner USR Try to set the owner of the unix socket to USR.
effdada… drh 3327 ** USR can be of the form USER:GROUP to set both
effdada… drh 3328 ** user and group.
2e56ef4… km 3329 ** --th-trace Trace TH1 execution (for debugging purposes)
7aeeb30… mistachkin 3330 ** --usepidkey Use saved encryption key from parent process. This is
f41cf03… mistachkin 3331 ** only necessary when using SEE on Windows or Linux.
75ea5ac… drh 3332 **
e2c161b… danield 3333 ** See also: [[cgi]], [[http]], [[winsrv]] [Windows only]
6607844… drh 3334 */
6607844… drh 3335 void cmd_webserver(void){
14c19fb… drh 3336 int iPort, mxPort; /* Range of TCP ports allowed */
14c19fb… drh 3337 const char *zPort; /* Value of the --port option */
f668ff4… drh 3338 const char *zBrowser; /* Name of web browser program */
14c19fb… drh 3339 char *zBrowserCmd = 0; /* Command to launch the web browser */
14c19fb… drh 3340 int isUiCmd; /* True if command is "ui", not "server' */
14c19fb… drh 3341 const char *zNotFound; /* The --notfound option or NULL */
0e42cc1… drh 3342 int flags = 0; /* Server flags */
c9db8c7… mistachkin 3343 #if !defined(_WIN32)
bf6be1e… mistachkin 3344 const char *zChRoot; /* Use for chroot instead of repository path */
c5c9f16… drh 3345 int noJail; /* Do not enter the chroot jail */
7979989… drh 3346 const char *zTimeout = 0; /* Max runtime of any single HTTP request */
c9db8c7… mistachkin 3347 #endif
cbde2cf… drh 3348 int allowRepoList; /* List repositories on URL "/" */
cbde2cf… drh 3349 const char *zAltBase; /* Argument to the --baseurl option */
cbde2cf… drh 3350 const char *zFileGlob; /* Static content must match this */
7fc2902… drh 3351 char *zIpAddr = 0; /* Bind to this IP address or UN socket */
cbde2cf… drh 3352 int fCreate = 0; /* The --create flag */
f3acbe4… drh 3353 int fNoBrowser = 0; /* Do not auto-launch web-browser */
cbde2cf… drh 3354 const char *zInitPage = 0; /* Start on this page. --page option */
9ec744e… drh 3355 int findServerArg = 2; /* argv index for find_server_repository() */
f3acbe4… drh 3356 char *zRemote = 0; /* Remote host on which to run "fossil ui" */
c359589… drh 3357 const char *zJsMode; /* The --jsmode parameter */
a7aa779… drh 3358 const char *zFossilCmd =0; /* Name of "fossil" binary on remote system */
763758d… drh 3359 const char *zFrom; /* Value for --from */
36c7984… drh 3360 const char *zExtPage = 0; /* Argument to --extpage */
275da70… danield 3361
f41cf03… mistachkin 3362
f41cf03… mistachkin 3363 #if USE_SEE
f41cf03… mistachkin 3364 db_setup_for_saved_encryption_key();
f41cf03… mistachkin 3365 #endif
23c0d16… drh 3366
3564af0… drh 3367 #if defined(_WIN32)
23c0d16… drh 3368 const char *zStopperFile; /* Name of file used to terminate server */
23c0d16… drh 3369 zStopperFile = find_option("stopper", 0, 1);
23c0d16… drh 3370 #endif
23c0d16… drh 3371
f87fb02… drh 3372 if( g.zErrlog==0 ){
f87fb02… drh 3373 g.zErrlog = "-";
f87fb02… drh 3374 }
ec56c69… drh 3375 g.zExtRoot = find_option("extroot",0,1);
c359589… drh 3376 zJsMode = find_option("jsmode",0,1);
c359589… drh 3377 builtin_set_js_delivery_mode(zJsMode,0);
608f0b2… mistachkin 3378 zFileGlob = find_option("files-urlenc",0,1);
608f0b2… mistachkin 3379 if( zFileGlob ){
4c3e172… danield 3380 char *z = fossil_strdup(zFileGlob);
608f0b2… mistachkin 3381 dehttpize(z);
608f0b2… mistachkin 3382 zFileGlob = z;
608f0b2… mistachkin 3383 }else{
608f0b2… mistachkin 3384 zFileGlob = find_option("files",0,1);
608f0b2… mistachkin 3385 }
5ce5ff5… drh 3386 skin_override();
c9db8c7… mistachkin 3387 #if !defined(_WIN32)
bf6be1e… mistachkin 3388 zChRoot = find_option("chroot",0,1);
c5c9f16… drh 3389 noJail = find_option("nojail",0,0)!=0;
859d6b1… drh 3390 zTimeout = find_option("max-latency",0,1);
c9db8c7… mistachkin 3391 #endif
f7a3c6d… drh 3392 g.useLocalauth = find_option("localauth", 0, 0)!=0;
0264475… mistachkin 3393 Th_InitTraceLog();
14c19fb… drh 3394 zPort = find_option("port", "P", 1);
f55f64e… drh 3395 isUiCmd = g.argv[1][0]=='u';
f55f64e… drh 3396 if( isUiCmd ){
763758d… drh 3397 zFrom = find_option("from", 0, 1);
5c72725… drh 3398 if( zFrom && zFrom==file_tail(zFrom) ){
5c72725… drh 3399 fossil_fatal("the argument to --from must be a pathname for"
5c72725… drh 3400 " the \"ui\" command");
5c72725… drh 3401 }
36c7984… drh 3402 zExtPage = find_option("extpage",0,1);
36c7984… drh 3403 if( zExtPage ){
36c7984… drh 3404 char *zFullPath = file_canonical_name_dup(zExtPage);
36c7984… drh 3405 g.zExtRoot = file_dirname(zFullPath);
36c7984… drh 3406 zInitPage = mprintf("ext/%s",file_tail(zFullPath));
36c7984… drh 3407 fossil_free(zFullPath);
36c7984… drh 3408 }else{
36c7984… drh 3409 zInitPage = find_option("page", "p", 1);
36c7984… drh 3410 if( zInitPage && zInitPage[0]=='/' ) zInitPage++;
36c7984… drh 3411 }
c359589… drh 3412 zFossilCmd = find_option("fossilcmd", 0, 1);
763758d… drh 3413 if( zFrom && zInitPage==0 ){
593ceca… drh 3414 zInitPage = mprintf("ckout?exbase=%H", zFrom);
3807891… drh 3415 }
f55f64e… drh 3416 }
ecb85f6… drh 3417 zNotFound = find_option("notfound", 0, 1);
87508e0… drh 3418 allowRepoList = find_option("repolist",0,0)!=0;
cfc7aca… drh 3419 if( find_option("nocompress",0,0)!=0 ) g.fNoHttpCompress = 1;
ecb85f6… drh 3420 zAltBase = find_option("baseurl", 0, 1);
a6e2ceb… drh 3421 fCreate = find_option("create",0,0)!=0;
a8e33fb… drh 3422 g.zReqType = "HTTP";
a8e33fb… drh 3423 if( find_option("scgi", 0, 0)!=0 ){
a8e33fb… drh 3424 g.zReqType = "SCGI";
a8e33fb… drh 3425 flags |= HTTP_SERVER_SCGI;
a8e33fb… drh 3426 }
ecb85f6… drh 3427 if( zAltBase ){
ecb85f6… drh 3428 set_base_url(zAltBase);
b8c7af5… mistachkin 3429 }
4aba9ea… drh 3430 g.sslNotAvailable = find_option("nossl", 0, 0)!=0 || isUiCmd;
1431eba… florian 3431 fNoBrowser = find_option("nobrowser", "B", 0)!=0;
f6263bb… drh 3432 decode_ssl_options();
f6263bb… drh 3433 if( find_option("https",0,0)!=0 || g.httpUseSSL ){
b8c7af5… mistachkin 3434 cgi_replace_parameter("HTTPS","on");
4e18dba… jan.nijtmans 3435 }
4e18dba… jan.nijtmans 3436 if( find_option("localhost", 0, 0)!=0 ){
74ac0c9… drh 3437 flags |= HTTP_SERVER_LOCALHOST;
74ac0c9… drh 3438 }
dc1121f… drh 3439 g.zCkoutAlias = find_option("ckout-alias",0,1);
73ca280… drh 3440 g.zMainMenuFile = find_option("mainmenu",0,1);
73ca280… drh 3441 if( g.zMainMenuFile!=0 && file_size(g.zMainMenuFile,ExtFILE)<0 ){
73ca280… drh 3442 fossil_fatal("Cannot read --mainmenu file %s", g.zMainMenuFile);
73ca280… drh 3443 }
f6263bb… drh 3444 if( find_option("acme",0,0)!=0 ) g.fAllowACME = 1;
effdada… drh 3445 g.zSockMode = find_option("socket-mode",0,1);
effdada… drh 3446 g.zSockName = find_option("socket-name",0,1);
effdada… drh 3447 g.zSockOwner = find_option("socket-owner",0,1);
effdada… drh 3448 if( g.zSockName ){
7fc2902… drh 3449 #if defined(_WIN32)
7fc2902… drh 3450 fossil_fatal("unix sockets are not supported on Windows");
7fc2902… drh 3451 #endif
7fc2902… drh 3452 if( zPort ){
7fc2902… drh 3453 fossil_fatal("cannot specify a port number for a unix socket");
7fc2902… drh 3454 }
7fc2902… drh 3455 if( isUiCmd && !fNoBrowser ){
7fc2902… drh 3456 fossil_fatal("cannot start a web-browser on a unix socket");
7fc2902… drh 3457 }
effdada… drh 3458 flags |= HTTP_SERVER_UNIXSOCKET;
7fc2902… drh 3459 }
f6263bb… drh 3460
f6263bb… drh 3461 /* Undocumented option: --debug-nofork
f6263bb… drh 3462 **
f6263bb… drh 3463 ** This sets the HTTP_SERVER_NOFORK flag, which causes only the
f6263bb… drh 3464 ** very first incoming TCP/IP connection to be processed. Used for
f6263bb… drh 3465 ** debugging, since debugging across a fork() can be tricky
f6263bb… drh 3466 */
f6263bb… drh 3467 if( find_option("debug-nofork",0,0)!=0 ){
f6263bb… drh 3468 flags |= HTTP_SERVER_NOFORK;
f6263bb… drh 3469 #if !defined(_WIN32)
f6263bb… drh 3470 /* Disable the timeout during debugging */
f6263bb… drh 3471 zTimeout = "100000000";
f6263bb… drh 3472 #endif
f6263bb… drh 3473 }
74ac0c9… drh 3474 /* We should be done with options.. */
74ac0c9… drh 3475 verify_all_options();
74ac0c9… drh 3476
74ac0c9… drh 3477 if( g.argc!=2 && g.argc!=3 ) usage("?REPOSITORY?");
f6263bb… drh 3478 if( g.httpUseSSL && (flags & HTTP_SERVER_SCGI)!=0 ){
f6263bb… drh 3479 fossil_fatal("SCGI does not (yet) support TLS-encrypted connections");
f6263bb… drh 3480 }
9ec744e… drh 3481 if( isUiCmd && 3==g.argc && file_isdir(g.argv[2], ExtFILE)>0 ){
bc36fdc… danield 3482 /* If REPOSITORY arg is the root of a check-out,
bc36fdc… danield 3483 ** chdir to that check-out so that the current version
9ec744e… drh 3484 ** gets highlighted in the timeline by default. */
4f095cd… stephan 3485 const char * zDir = g.argv[2];
4f095cd… stephan 3486 if(dir_has_ckout_db(zDir)){
4f095cd… stephan 3487 if(0!=file_chdir(zDir, 0)){
4f095cd… stephan 3488 fossil_fatal("Cannot chdir to %s", zDir);
4f095cd… stephan 3489 }
de6f88a… drh 3490 findServerArg = g.argc;
9ec744e… drh 3491 fCreate = 0;
9ec744e… drh 3492 g.argv[2] = 0;
9ec744e… drh 3493 --g.argc;
9ec744e… drh 3494 }
f3acbe4… drh 3495 }
f3acbe4… drh 3496 if( isUiCmd && 3==g.argc
f3acbe4… drh 3497 && (zRemote = (char*)file_skip_userhost(g.argv[2]))!=0
f3acbe4… drh 3498 ){
f3acbe4… drh 3499 /* The REPOSITORY argument has a USER@HOST: or HOST: prefix */
f3acbe4… drh 3500 const char *zRepoTail = file_skip_userhost(g.argv[2]);
f3acbe4… drh 3501 unsigned x;
f3acbe4… drh 3502 int n;
f3acbe4… drh 3503 sqlite3_randomness(2,&x);
f3acbe4… drh 3504 zPort = mprintf("%d", 8100+(x%32000));
f3acbe4… drh 3505 n = (int)(zRepoTail - g.argv[2]) - 1;
f3acbe4… drh 3506 zRemote = mprintf("%.*s", n, g.argv[2]);
f3acbe4… drh 3507 g.argv[2] = (char*)zRepoTail;
9ec744e… drh 3508 }
74ac0c9… drh 3509 if( isUiCmd ){
d976b47… drh 3510 flags |= HTTP_SERVER_LOCALHOST|HTTP_SERVER_REPOLIST;
74ac0c9… drh 3511 g.useLocalauth = 1;
87508e0… drh 3512 allowRepoList = 1;
87508e0… drh 3513 }
f3acbe4… drh 3514 if( !zRemote ){
f3acbe4… drh 3515 find_server_repository(findServerArg, fCreate);
f3acbe4… drh 3516 }
3602486… drh 3517 if( zInitPage==0 ){
58849c4… drh 3518 zInitPage = "";
3602486… drh 3519 }
74ac0c9… drh 3520 if( zPort ){
4d3cb0d… drh 3521 if( strchr(zPort,':') ){
f8f2c8d… drh 3522 int i;
f8f2c8d… drh 3523 for(i=strlen(zPort)-1; i>=0 && zPort[i]!=':'; i--){}
f8f2c8d… drh 3524 if( i>0 ){
f8f2c8d… drh 3525 if( zPort[0]=='[' && zPort[i-1]==']' ){
f8f2c8d… drh 3526 zIpAddr = mprintf("%.*s", i-2, zPort+1);
f8f2c8d… drh 3527 }else{
f8f2c8d… drh 3528 zIpAddr = mprintf("%.*s", i, zPort);
f8f2c8d… drh 3529 }
f8f2c8d… drh 3530 zPort += i+1;
f8f2c8d… drh 3531 }
74ac0c9… drh 3532 }
74ac0c9… drh 3533 iPort = mxPort = atoi(zPort);
1431eba… florian 3534 if( iPort<=0 ) fossil_fatal("port number must be greater than zero");
02f09cd… drh 3535 }else{
02f09cd… drh 3536 iPort = db_get_int("http-port", 8080);
d8ceb4a… drh 3537 mxPort = iPort+100;
d8ceb4a… drh 3538 }
f3acbe4… drh 3539 if( isUiCmd && !fNoBrowser ){
f3acbe4… drh 3540 char *zBrowserArg;
f6263bb… drh 3541 const char *zProtocol = g.httpUseSSL ? "https" : "http";
200247b… preben 3542 db_open_config(0,0);
0d8c928… drh 3543 zBrowser = fossil_web_browser();
3e0731f… drh 3544 if( fossil_strcmp(g.argv[2],"/")==0 ){
3e0731f… drh 3545 zInitPage = mprintf("?home=%T", zInitPage);
3e0731f… drh 3546 }
0d8c928… drh 3547 if( zIpAddr==0 ){
f6263bb… drh 3548 zBrowserArg = mprintf("%s://localhost:%%d/%s", zProtocol, zInitPage);
0d8c928… drh 3549 }else if( strchr(zIpAddr,':') ){
f6263bb… drh 3550 zBrowserArg = mprintf("%s://[%s]:%%d/%s", zProtocol, zIpAddr, zInitPage);
0d8c928… drh 3551 }else{
f6263bb… drh 3552 zBrowserArg = mprintf("%s://%s:%%d/%s", zProtocol, zIpAddr, zInitPage);
0d8c928… drh 3553 }
36bd542… drh 3554 zBrowserCmd = mprintf("%s %!$ &", zBrowser, zBrowserArg);
f3acbe4… drh 3555 fossil_free(zBrowserArg);
f3acbe4… drh 3556 }
f3acbe4… drh 3557 if( zRemote ){
f3acbe4… drh 3558 /* If a USER@HOST:REPO argument is supplied, then use SSH to run
f3acbe4… drh 3559 ** "fossil ui --nobrowser" on the remote system and to set up a
f3acbe4… drh 3560 ** tunnel from the local machine to the remote. */
f3acbe4… drh 3561 FILE *sshIn;
f3acbe4… drh 3562 Blob ssh;
ed6495b… drh 3563 int bRunning = 0; /* True when fossil starts up on the remote */
1862890… stephan 3564 int isRetry; /* True if on the second attempt */
3e0731f… drh 3565 const char *zUrl = g.argv[2];
f3acbe4… drh 3566 char zLine[1000];
ed6495b… drh 3567
f3acbe4… drh 3568 blob_init(&ssh, 0, 0);
ed6495b… drh 3569 for(isRetry=0; isRetry<2 && !bRunning; isRetry++){
ed6495b… drh 3570 blob_reset(&ssh);
ed6495b… drh 3571 transport_ssh_command(&ssh);
ed6495b… drh 3572 blob_appendf(&ssh,
ed6495b… drh 3573 " -t -L 127.0.0.1:%d:127.0.0.1:%d %!$",
ed6495b… drh 3574 iPort, iPort, zRemote
ed6495b… drh 3575 );
ed6495b… drh 3576 if( zFossilCmd==0 ){
ed6495b… drh 3577 if( ssh_needs_path_argument(zRemote,-1) ^ isRetry ){
ed6495b… drh 3578 ssh_add_path_argument(&ssh);
ed6495b… drh 3579 }
ed6495b… drh 3580 blob_append_escaped_arg(&ssh, "fossil", 1);
ed6495b… drh 3581 }else{
ed6495b… drh 3582 blob_appendf(&ssh, " %$", zFossilCmd);
ed6495b… drh 3583 }
af78e28… drh 3584 blob_appendf(&ssh, " ui --nobrowser --localauth --port 127.0.0.1:%d",
af78e28… drh 3585 iPort);
ed6495b… drh 3586 if( zNotFound ) blob_appendf(&ssh, " --notfound %!$", zNotFound);
ed6495b… drh 3587 if( zFileGlob ) blob_appendf(&ssh, " --files-urlenc %T", zFileGlob);
ed6495b… drh 3588 if( g.zCkoutAlias ) blob_appendf(&ssh," --ckout-alias %!$",g.zCkoutAlias);
48eea5e… drh 3589 if( zExtPage ){
48eea5e… drh 3590 if( !file_is_absolute_path(zExtPage) ){
48eea5e… drh 3591 zExtPage = mprintf("%s/%s", g.argv[2], zExtPage);
48eea5e… drh 3592 }
48eea5e… drh 3593 blob_appendf(&ssh, " --extpage %$", zExtPage);
48eea5e… drh 3594 }else if( g.zExtRoot ){
48eea5e… drh 3595 blob_appendf(&ssh, " --extroot %$", g.zExtRoot);
48eea5e… drh 3596 }
ed6495b… drh 3597 if( skin_in_use() ) blob_appendf(&ssh, " --skin %s", skin_in_use());
ed6495b… drh 3598 if( zJsMode ) blob_appendf(&ssh, " --jsmode %s", zJsMode);
ed6495b… drh 3599 if( fCreate ) blob_appendf(&ssh, " --create");
3e0731f… drh 3600 blob_appendf(&ssh, " %$", zUrl);
ed6495b… drh 3601 if( isRetry ){
ed6495b… drh 3602 fossil_print("First attempt to run \"fossil\" on %s failed\n"
ed6495b… drh 3603 "Retry: ", zRemote);
1862890… stephan 3604 }
ed6495b… drh 3605 fossil_print("%s\n", blob_str(&ssh));
ed6495b… drh 3606 sshIn = popen(blob_str(&ssh), "r");
ed6495b… drh 3607 if( sshIn==0 ){
ed6495b… drh 3608 fossil_fatal("unable to %s", blob_str(&ssh));
ed6495b… drh 3609 }
ed6495b… drh 3610 while( fgets(zLine, sizeof(zLine), sshIn) ){
ed6495b… drh 3611 fputs(zLine, stdout);
ed6495b… drh 3612 fflush(stdout);
ed6495b… drh 3613 if( !bRunning && sqlite3_strglob("*Listening for HTTP*",zLine)==0 ){
ed6495b… drh 3614 bRunning = 1;
ed6495b… drh 3615 if( isRetry ){
ed6495b… drh 3616 ssh_needs_path_argument(zRemote,99);
ed6495b… drh 3617 }
ed6495b… drh 3618 db_close_config();
ed6495b… drh 3619 if( zBrowserCmd ){
ed6495b… drh 3620 char *zCmd = mprintf(zBrowserCmd/*works-like:"%d"*/,iPort);
ed6495b… drh 3621 fossil_system(zCmd);
ed6495b… drh 3622 fossil_free(zCmd);
ed6495b… drh 3623 fossil_free(zBrowserCmd);
ed6495b… drh 3624 zBrowserCmd = 0;
ed6495b… drh 3625 }
ed6495b… drh 3626 }
ed6495b… drh 3627 }
ed6495b… drh 3628 pclose(sshIn);
ed6495b… drh 3629 }
f3acbe4… drh 3630 fossil_free(zBrowserCmd);
f3acbe4… drh 3631 return;
239b4c1… drh 3632 }
239b4c1… drh 3633 if( g.repositoryOpen ) flags |= HTTP_SERVER_HAD_REPOSITORY;
239b4c1… drh 3634 if( g.localOpen ) flags |= HTTP_SERVER_HAD_CHECKOUT;
239b4c1… drh 3635 db_close(1);
f572f75… florian 3636 #if !defined(_WIN32)
effdada… drh 3637 if( 1 ){
d3c55fe… wyoung 3638 /* Modern kernels suppress SIGTERM to PID 1 to prevent root from
d3c55fe… wyoung 3639 ** rebooting the system by nuking the init system. The only way
d3c55fe… wyoung 3640 ** Fossil becomes that PID 1 is when it's running solo in a Linux
d3c55fe… wyoung 3641 ** container or similar, so we do want to exit immediately, to
d3c55fe… wyoung 3642 ** allow the container to shut down quickly.
d3c55fe… wyoung 3643 **
d3c55fe… wyoung 3644 ** This has to happen ahead of the other signal() calls below.
d3c55fe… wyoung 3645 ** They apply after the HTTP hit is handled, but this one needs
d3c55fe… wyoung 3646 ** to be registered while we're waiting for that to occur.
d3c55fe… wyoung 3647 **/
d3c55fe… wyoung 3648 signal(SIGTERM, fossil_exit);
a0b0d8f… wyoung 3649 signal(SIGINT, fossil_exit);
d3c55fe… wyoung 3650 }
f572f75… florian 3651 #endif /* !WIN32 */
2f122aa… drh 3652
2f122aa… drh 3653 /* Start up an HTTP server
2f122aa… drh 3654 */
ba95498… stephan 3655 fossil_setenv("SERVER_SOFTWARE", "fossil version " RELEASE_VERSION
ba95498… stephan 3656 " " MANIFEST_VERSION " " MANIFEST_DATE);
2f122aa… drh 3657 #if !defined(_WIN32)
2f122aa… drh 3658 /* Unix implementation */
239b4c1… drh 3659 if( cgi_http_server(iPort, mxPort, zBrowserCmd, zIpAddr, flags) ){
effdada… drh 3660 fossil_fatal("unable to listen on CGI socket");
05ec15c… drh 3661 }
c546212… drh 3662 /* For the parent process, the cgi_http_server() command above never
c546212… drh 3663 ** returns (except in the case of an error). Instead, for each incoming
c546212… drh 3664 ** client connection, a child process is created, file descriptors 0
c546212… drh 3665 ** and 1 are bound to that connection, and the child returns.
c546212… drh 3666 **
c546212… drh 3667 ** So, when control reaches this point, we are running as a
c546212… drh 3668 ** child process, the HTTP or SCGI request is pending on file
c546212… drh 3669 ** descriptor 0 and the reply should be written to file descriptor 1.
c546212… drh 3670 */
859d6b1… drh 3671 if( zTimeout ){
859d6b1… drh 3672 fossil_set_timeout(atoi(zTimeout));
7979989… drh 3673 }else{
7979989… drh 3674 fossil_set_timeout(FOSSIL_DEFAULT_TIMEOUT);
239b4c1… drh 3675 }
239b4c1… drh 3676 g.httpIn = stdin;
239b4c1… drh 3677 g.httpOut = stdout;
83b171b… drh 3678 signal(SIGSEGV, sigsegv_handler);
83b171b… drh 3679 signal(SIGPIPE, sigpipe_handler);
83b171b… drh 3680 if( g.fAnyTrace ){
83b171b… drh 3681 fprintf(stderr, "/***** Subprocess %d *****/\n", getpid());
239b4c1… drh 3682 }
239b4c1… drh 3683 g.cgiOutput = 1;
239b4c1… drh 3684 find_server_repository(2, 0);
239b4c1… drh 3685 if( fossil_strcmp(g.zRepositoryName,"/")==0 ){
239b4c1… drh 3686 allowRepoList = 1;
239b4c1… drh 3687 }else{
6f92ad9… wyoung 3688 g.zRepositoryName = enter_chroot_jail(
6f92ad9… wyoung 3689 zChRoot ? zChRoot : g.zRepositoryName, noJail);
239b4c1… drh 3690 }
239b4c1… drh 3691 if( flags & HTTP_SERVER_SCGI ){
239b4c1… drh 3692 cgi_handle_scgi_request();
f6263bb… drh 3693 }else if( g.httpUseSSL ){
f6263bb… drh 3694 #if FOSSIL_ENABLE_SSL
7a3bf55… drh 3695 g.httpSSLConn = ssl_new_server(0);
f6263bb… drh 3696 #endif
f6263bb… drh 3697 cgi_handle_http_request(0);
239b4c1… drh 3698 }else{
239b4c1… drh 3699 cgi_handle_http_request(0);
239b4c1… drh 3700 }
239b4c1… drh 3701 process_one_web_page(zNotFound, glob_create(zFileGlob), allowRepoList);
83b171b… drh 3702 if( g.fAnyTrace ){
83b171b… drh 3703 fprintf(stderr, "/***** Webpage finished in subprocess %d *****/\n",
83b171b… drh 3704 getpid());
83b171b… drh 3705 }
f6263bb… drh 3706 #if FOSSIL_ENABLE_SSL
f6263bb… drh 3707 if( g.httpUseSSL && g.httpSSLConn ){
f6263bb… drh 3708 ssl_close_server(g.httpSSLConn);
f6263bb… drh 3709 g.httpSSLConn = 0;
f6263bb… drh 3710 }
f6263bb… drh 3711 #endif /* FOSSIL_ENABLE_SSL */
f6263bb… drh 3712
f6263bb… drh 3713 #else /* WIN32 */
28436ab… mgagnon 3714 /* Win32 implementation */
0a473cd… mgagnon 3715 if( fossil_strcmp(g.zRepositoryName,"/")==0 ){
0a473cd… mgagnon 3716 allowRepoList = 1;
0a473cd… mgagnon 3717 }
6f35075… jan.nijtmans 3718 if( allowRepoList ){
6f35075… jan.nijtmans 3719 flags |= HTTP_SERVER_REPOLIST;
6f35075… jan.nijtmans 3720 }
6f35075… jan.nijtmans 3721 if( win32_http_service(iPort, zAltBase, zNotFound, zFileGlob, flags) ){
6f35075… jan.nijtmans 3722 win32_http_server(iPort, mxPort, zBrowserCmd, zStopperFile,
6f35075… jan.nijtmans 3723 zAltBase, zNotFound, zFileGlob, zIpAddr, flags);
06e9ca2… drh 3724 }
cbf8782… drh 3725 #endif
cbf8782… drh 3726 }
cbf8782… drh 3727
cbf8782… drh 3728 /*
26eef7f… rberteig 3729 ** COMMAND: test-echo
ca72844… drh 3730 **
ca72844… drh 3731 ** Usage: %fossil test-echo [--hex] ARGS...
cbf8782… drh 3732 **
cbf8782… drh 3733 ** Echo all command-line arguments (enclosed in [...]) to the screen so that
cbf8782… drh 3734 ** wildcard expansion behavior of the host shell can be investigated.
ca72844… drh 3735 **
ca72844… drh 3736 ** With the --hex option, show the output as hexadecimal. This can be used
9571b68… drh 3737 ** to verify the fossil_path_to_utf8() routine on Windows and Mac.
cbf8782… drh 3738 */
cbf8782… drh 3739 void test_echo_cmd(void){
ca72844… drh 3740 int i, j;
ca72844… drh 3741 if( find_option("hex",0,0)==0 ){
5ed566f… drh 3742 fossil_print("g.nameOfExe = [%s]\n", g.nameOfExe);
ca72844… drh 3743 for(i=0; i<g.argc; i++){
ca72844… drh 3744 fossil_print("argv[%d] = [%s]\n", i, g.argv[i]);
ca72844… drh 3745 }
ca72844… drh 3746 }else{
ca72844… drh 3747 unsigned char *z, c;
ca72844… drh 3748 for(i=0; i<g.argc; i++){
ca72844… drh 3749 fossil_print("argv[%d] = [", i);
ca72844… drh 3750 z = (unsigned char*)g.argv[i];
ca72844… drh 3751 for(j=0; (c = z[j])!=0; j++){
ca72844… drh 3752 fossil_print("%02x", c);
ca72844… drh 3753 }
ca72844… drh 3754 fossil_print("]\n");
ca72844… drh 3755 }
ca72844… drh 3756 }
f87fb02… drh 3757 }
f87fb02… drh 3758
f87fb02… drh 3759 /*
f87fb02… drh 3760 ** WEBPAGE: test-warning
f87fb02… drh 3761 **
f87fb02… drh 3762 ** Test error and warning log operation. This webpage is accessible to
f87fb02… drh 3763 ** the administrator only.
f87fb02… drh 3764 **
f87fb02… drh 3765 ** case=1 Issue a fossil_warning() while generating the page.
f87fb02… drh 3766 ** case=2 Extra db_begin_transaction()
f87fb02… drh 3767 ** case=3 Extra db_end_transaction()
a8d220b… drh 3768 ** case=4 Error during SQL processing
a8d220b… drh 3769 ** case=5 Call the segfault handler
99fcc43… drh 3770 ** case=6 Call webpage_assert()
99fcc43… drh 3771 ** case=7 Call webpage_error()
2116238… drh 3772 ** case=8 Simulate a timeout
2116238… drh 3773 ** case=9 Simulate a TH1 XSS vulnerability
2116238… drh 3774 ** case=10 Simulate a TH1 SQL-injection vulnerability
f87fb02… drh 3775 */
f87fb02… drh 3776 void test_warning_page(void){
c931dd7… drh 3777 int iCase = atoi(PD("case","0"));
8d9ad75… drh 3778 int i;
f87fb02… drh 3779 login_check_credentials();
42c3364… wyoung 3780 if( !g.perm.Admin ){
c931dd7… drh 3781 login_needed(0);
c931dd7… drh 3782 return;
c931dd7… drh 3783 }
112c713… drh 3784 style_set_current_feature("test");
f87fb02… drh 3785 style_header("Warning Test Page");
c931dd7… drh 3786 style_submenu_element("Error Log","%R/errorlog");
2116238… drh 3787 @ <p>This page will generate various kinds of errors to test Fossil's
2116238… drh 3788 @ reaction. Depending on settings, a message might be written
2116238… drh 3789 @ into the <a href="%R/errorlog">error log</a>. Click on
2116238… drh 3790 @ one of the following hyperlinks to generate a simulated error:
2116238… drh 3791 for(i=1; i<=10; i++){
8d9ad75… drh 3792 @ <a href='./test-warning?case=%d(i)'>[%d(i)]</a>
8d9ad75… drh 3793 }
8d9ad75… drh 3794 @ </p>
8d9ad75… drh 3795 @ <p><ol>
f87fb02… drh 3796 @ <li value='1'> Call fossil_warning()
f87fb02… drh 3797 if( iCase==1 ){
f87fb02… drh 3798 fossil_warning("Test warning message from /test-warning");
f87fb02… drh 3799 }
f87fb02… drh 3800 @ <li value='2'> Call db_begin_transaction()
f87fb02… drh 3801 if( iCase==2 ){
f87fb02… drh 3802 db_begin_transaction();
f87fb02… drh 3803 }
f87fb02… drh 3804 @ <li value='3'> Call db_end_transaction()
f87fb02… drh 3805 if( iCase==3 ){
f87fb02… drh 3806 db_end_transaction(0);
f87fb02… drh 3807 }
c6ecf21… drh 3808 @ <li value='4'> warning during SQL
c6ecf21… drh 3809 if( iCase==4 ){
c6ecf21… drh 3810 Stmt q;
c6ecf21… drh 3811 db_prepare(&q, "SELECT uuid FROM blob LIMIT 5");
c6ecf21… drh 3812 db_step(&q);
c6ecf21… drh 3813 sqlite3_log(SQLITE_ERROR, "Test warning message during SQL");
c6ecf21… drh 3814 db_finalize(&q);
c6ecf21… drh 3815 }
a8d220b… drh 3816 @ <li value='5'> simulate segfault handling
a8d220b… drh 3817 if( iCase==5 ){
a8d220b… drh 3818 sigsegv_handler(0);
a8d220b… drh 3819 }
99fcc43… drh 3820 @ <li value='6'> call webpage_assert(0)
99fcc43… drh 3821 if( iCase==6 ){
99fcc43… drh 3822 webpage_assert( 5==7 );
99fcc43… drh 3823 }
2116238… drh 3824 @ <li value='7'> call webpage_error()
99fcc43… drh 3825 if( iCase==7 ){
99fcc43… drh 3826 cgi_reset_content();
99fcc43… drh 3827 webpage_error("Case 7 from /test-warning");
48c06b0… drh 3828 }
2116238… drh 3829 @ <li value='8'> simulated timeout
48c06b0… drh 3830 if( iCase==8 ){
48c06b0… drh 3831 fossil_set_timeout(1);
48c06b0… drh 3832 cgi_reset_content();
48c06b0… drh 3833 sqlite3_sleep(1100);
2116238… drh 3834 }
2116238… drh 3835 @ <li value='9'> simulated TH1 XSS vulnerability
2116238… drh 3836 @ <li value='10'> simulated TH1 SQL-injection vulnerability
2116238… drh 3837 if( iCase==9 || iCase==10 ){
2116238… drh 3838 const char *zR;
2116238… drh 3839 int n, rc;
2116238… drh 3840 static const char *zTH1[] = {
2116238… drh 3841 /* case 9 */ "html [taint {<b>XSS</b>}]",
2116238… drh 3842 /* case 10 */ "query [taint {SELECT 'SQL-injection' AS msg}] {\n"
2116238… drh 3843 " html \"<b>[htmlize $msg]</b>\"\n"
2116238… drh 3844 "}"
2116238… drh 3845 };
2116238… drh 3846 rc = Th_Eval(g.interp, 0, zTH1[iCase==10], -1);
2116238… drh 3847 zR = Th_GetResult(g.interp, &n);
2116238… drh 3848 if( rc==TH_OK ){
2116238… drh 3849 @ <pre class="th1result">%h(zR)</pre>
2116238… drh 3850 }else{
2116238… drh 3851 @ <pre class="th1error">%h(zR)</pre>
2116238… drh 3852 }
112c713… drh 3853 }
f87fb02… drh 3854 @ </ol>
f87fb02… drh 3855 @ <p>End of test</p>
112c713… drh 3856 style_finish_page();
dbda8d6… drh 3857 }

Keyboard Shortcuts

Open search /
Next entry (timeline) j
Previous entry (timeline) k
Open focused entry Enter
Show this help ?
Toggle theme Top nav button