main.c 63 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578
  1. /* Copyright (c) 2013-2015 the Civetweb developers
  2. * Copyright (c) 2004-2013 Sergey Lyubka
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy
  5. * of this software and associated documentation files (the "Software"), to deal
  6. * in the Software without restriction, including without limitation the rights
  7. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. * copies of the Software, and to permit persons to whom the Software is
  9. * furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. * THE SOFTWARE.
  21. */
  22. #if defined(_WIN32)
  23. #ifndef _CRT_SECURE_NO_WARNINGS
  24. #define _CRT_SECURE_NO_WARNINGS /* Disable deprecation warning in VS2005 */
  25. #endif
  26. #ifndef _CRT_SECURE_NO_DEPRECATE
  27. #define _CRT_SECURE_NO_DEPRECATE
  28. #endif
  29. #else
  30. #define _XOPEN_SOURCE 600 /* For PATH_MAX on linux */
  31. /* This should also be sufficient for "realpath", according to
  32. * http://man7.org/linux/man-pages/man3/realpath.3.html, but in
  33. * reality it does not seem to work. */
  34. /* In case this causes a problem, disable the warning:
  35. * #pragma GCC diagnostic ignored "-Wimplicit-function-declaration"
  36. * #pragma clang diagnostic ignored "-Wimplicit-function-declaration"
  37. */
  38. #endif
  39. #ifndef IGNORE_UNUSED_RESULT
  40. #define IGNORE_UNUSED_RESULT(a) ((void)((a) && 1))
  41. #endif
  42. #if defined(__cplusplus) && (__cplusplus >= 201103L)
  43. #define NO_RETURN [[noreturn]]
  44. #elif defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 201112L)
  45. #define NO_RETURN _Noreturn
  46. #else
  47. #define NO_RETURN
  48. #endif
  49. #include <sys/stat.h>
  50. #include <stdio.h>
  51. #include <stdint.h>
  52. #include <limits.h>
  53. #include <stdlib.h>
  54. #include <signal.h>
  55. #include <string.h>
  56. #include <errno.h>
  57. #include <stddef.h>
  58. #include <stdarg.h>
  59. #include <ctype.h>
  60. #include <assert.h>
  61. #include "civetweb.h"
  62. #define printf \
  63. DO_NOT_USE_THIS_FUNCTION__USE_fprintf /* Required for unit testing */
  64. #if defined(_WIN32) \
  65. && !defined(__SYMBIAN32__) /* WINDOWS / UNIX include block */
  66. #ifndef _WIN32_WINNT
  67. #define _WIN32_WINNT 0x0501 /* for tdm-gcc so we can use getconsolewindow */
  68. #endif
  69. #undef UNICODE
  70. #include <windows.h>
  71. #include <winsvc.h>
  72. #include <shlobj.h>
  73. #include <io.h>
  74. #define getcwd(a, b) (_getcwd(a, b))
  75. #if !defined(__MINGW32__)
  76. extern char *_getcwd(char *buf, size_t size);
  77. #endif
  78. static int guard = 0; /* test if any dialog is already open */
  79. #ifndef PATH_MAX
  80. #define PATH_MAX MAX_PATH
  81. #endif
  82. #ifndef S_ISDIR
  83. #define S_ISDIR(x) ((x)&_S_IFDIR)
  84. #endif
  85. #define DIRSEP '\\'
  86. #define snprintf _snprintf
  87. #define vsnprintf _vsnprintf
  88. #define sleep(x) (Sleep((x)*1000))
  89. #define WINCDECL __cdecl
  90. #define abs_path(rel, abs, abs_size) (_fullpath((abs), (rel), (abs_size)))
  91. #else /* defined(_WIN32) && !defined(__SYMBIAN32__) - WINDOWS / UNIX include \
  92. block */
  93. #include <sys/utsname.h>
  94. #include <sys/wait.h>
  95. #include <unistd.h>
  96. #define DIRSEP '/'
  97. #define WINCDECL
  98. #define abs_path(rel, abs, abs_size) (realpath((rel), (abs)))
  99. #endif /* defined(_WIN32) && !defined(__SYMBIAN32__) - WINDOWS / UNIX include \
  100. block */
  101. #ifndef PATH_MAX
  102. #define PATH_MAX (1024)
  103. #endif
  104. #define MAX_OPTIONS (50)
  105. #define MAX_CONF_FILE_LINE_SIZE (8 * 1024)
  106. struct tuser_data {
  107. char *first_message;
  108. };
  109. static int g_exit_flag = 0; /* Main loop should exit */
  110. static char g_server_base_name[40]; /* Set by init_server_name() */
  111. static char *g_server_name; /* Set by init_server_name() */
  112. static char *g_icon_name; /* Set by init_server_name() */
  113. static char g_config_file[PATH_MAX] =
  114. ""; /* Set by process_command_line_arguments() */
  115. static struct mg_context *g_ctx; /* Set by start_civetweb() */
  116. static struct tuser_data
  117. g_user_data; /* Passed to mg_start() by start_civetweb() */
  118. #if !defined(CONFIG_FILE)
  119. #define CONFIG_FILE "civetweb.conf"
  120. #endif /* !CONFIG_FILE */
  121. #if !defined(PASSWORDS_FILE_NAME)
  122. #define PASSWORDS_FILE_NAME ".htpasswd"
  123. #endif
  124. /* backup config file */
  125. #if !defined(CONFIG_FILE2) && defined(__linux__)
  126. #define CONFIG_FILE2 "/usr/local/etc/civetweb.conf"
  127. #endif
  128. enum { OPTION_TITLE, OPTION_ICON, NUM_MAIN_OPTIONS };
  129. static struct mg_option main_config_options[] = {
  130. {"title", CONFIG_TYPE_STRING, NULL},
  131. {"icon", CONFIG_TYPE_STRING, NULL},
  132. {NULL, CONFIG_TYPE_UNKNOWN, NULL}};
  133. static void WINCDECL
  134. signal_handler(int sig_num)
  135. {
  136. g_exit_flag = sig_num;
  137. }
  138. static NO_RETURN void
  139. die(const char *fmt, ...)
  140. {
  141. va_list ap;
  142. char msg[200] = "";
  143. va_start(ap, fmt);
  144. (void)vsnprintf(msg, sizeof(msg) - 1, fmt, ap);
  145. msg[sizeof(msg) - 1] = 0;
  146. va_end(ap);
  147. #if defined(_WIN32)
  148. MessageBox(NULL, msg, "Error", MB_OK);
  149. #else
  150. fprintf(stderr, "%s\n", msg);
  151. #endif
  152. exit(EXIT_FAILURE);
  153. }
  154. #ifdef WIN32
  155. static int MakeConsole(void);
  156. #endif
  157. static void
  158. show_server_name(void)
  159. {
  160. #ifdef WIN32
  161. (void)MakeConsole();
  162. #endif
  163. fprintf(stderr, "CivetWeb v%s, built on %s\n", mg_version(), __DATE__);
  164. }
  165. static NO_RETURN void
  166. show_usage_and_exit(const char *exeName)
  167. {
  168. const struct mg_option *options;
  169. int i;
  170. if (exeName == 0 || *exeName == 0) {
  171. exeName = "civetweb";
  172. }
  173. show_server_name();
  174. fprintf(stderr, "\nUsage:\n");
  175. fprintf(stderr, " Start server with a set of options:\n");
  176. fprintf(stderr, " %s [config_file]\n", exeName);
  177. fprintf(stderr, " %s [-option value ...]\n", exeName);
  178. fprintf(stderr, " Show system information:\n");
  179. fprintf(stderr, " %s -I\n", exeName);
  180. fprintf(stderr, " Add user/change password:\n");
  181. fprintf(stderr,
  182. " %s -A <htpasswd_file> <realm> <user> <passwd>\n",
  183. exeName);
  184. fprintf(stderr, " Remove user:\n");
  185. fprintf(stderr, " %s -R <htpasswd_file> <realm> <user>\n", exeName);
  186. fprintf(stderr, "\nOPTIONS:\n");
  187. options = mg_get_valid_options();
  188. for (i = 0; options[i].name != NULL; i++) {
  189. fprintf(stderr,
  190. " -%s %s\n",
  191. options[i].name,
  192. ((options[i].default_value == NULL)
  193. ? "<empty>"
  194. : options[i].default_value));
  195. }
  196. options = main_config_options;
  197. for (i = 0; options[i].name != NULL; i++) {
  198. fprintf(stderr,
  199. " -%s %s\n",
  200. options[i].name,
  201. ((options[i].default_value == NULL)
  202. ? "<empty>"
  203. : options[i].default_value));
  204. }
  205. exit(EXIT_FAILURE);
  206. }
  207. #if defined(_WIN32) || defined(USE_COCOA)
  208. static const char *config_file_top_comment =
  209. "# Civetweb web server configuration file.\n"
  210. "# For detailed description of every option, visit\n"
  211. "# https://github.com/civetweb/civetweb/blob/master/docs/UserManual.md\n"
  212. "# Lines starting with '#' and empty lines are ignored.\n"
  213. "# To make a change, remove leading '#', modify option's value,\n"
  214. "# save this file and then restart Civetweb.\n\n";
  215. static const char *
  216. get_url_to_first_open_port(const struct mg_context *ctx)
  217. {
  218. static char url[100];
  219. const char *open_ports = mg_get_option(ctx, "listening_ports");
  220. int a, b, c, d, port, n;
  221. if (sscanf(open_ports, "%d.%d.%d.%d:%d%n", &a, &b, &c, &d, &port, &n)
  222. == 5) {
  223. snprintf(url,
  224. sizeof(url),
  225. "%s://%d.%d.%d.%d:%d",
  226. open_ports[n] == 's' ? "https" : "http",
  227. a,
  228. b,
  229. c,
  230. d,
  231. port);
  232. } else if (sscanf(open_ports, "%d%n", &port, &n) == 1) {
  233. snprintf(url,
  234. sizeof(url),
  235. "%s://localhost:%d",
  236. open_ports[n] == 's' ? "https" : "http",
  237. port);
  238. } else {
  239. snprintf(url, sizeof(url), "%s", "http://localhost:8080");
  240. }
  241. return url;
  242. }
  243. #ifdef ENABLE_CREATE_CONFIG_FILE
  244. static void
  245. create_config_file(const struct mg_context *ctx, const char *path)
  246. {
  247. const struct mg_option *options;
  248. const char *value;
  249. FILE *fp;
  250. int i;
  251. /* Create config file if it is not present yet */
  252. if ((fp = fopen(path, "r")) != NULL) {
  253. fclose(fp);
  254. } else if ((fp = fopen(path, "a+")) != NULL) {
  255. fprintf(fp, "%s", config_file_top_comment);
  256. options = mg_get_valid_options();
  257. for (i = 0; options[i].name != NULL; i++) {
  258. value = mg_get_option(ctx, options[i].name);
  259. fprintf(fp,
  260. "# %s %s\n",
  261. options[i].name,
  262. value ? value : "<value>");
  263. }
  264. fclose(fp);
  265. }
  266. }
  267. #endif
  268. #endif
  269. static char *
  270. sdup(const char *str)
  271. {
  272. size_t len;
  273. char *p;
  274. len = strlen(str) + 1;
  275. if ((p = (char *)malloc(len)) != NULL) {
  276. memcpy(p, str, len);
  277. }
  278. return p;
  279. }
  280. static const char *
  281. get_option(char **options, const char *option_name)
  282. {
  283. int i = 0;
  284. const char *opt_value = NULL;
  285. /* TODO (low, api makeover): options should be an array of key-value-pairs,
  286. * like
  287. * struct {const char * key, const char * value} options[]
  288. * but it currently is an array with
  289. * options[2*i] = key, options[2*i + 1] = value
  290. * (probably with a MG_LEGACY_INTERFACE definition)
  291. */
  292. while (options[2 * i] != NULL) {
  293. if (strcmp(options[2 * i], option_name) == 0) {
  294. opt_value = options[2 * i + 1];
  295. break;
  296. }
  297. i++;
  298. }
  299. return opt_value;
  300. }
  301. static int
  302. set_option(char **options, const char *name, const char *value)
  303. {
  304. int i, type;
  305. const struct mg_option *default_options = mg_get_valid_options();
  306. for (i = 0; main_config_options[i].name != NULL; i++) {
  307. if (0 == strcmp(name, main_config_options[i].name)) {
  308. /* This option is evaluated by main.c, not civetweb.c - just skip it
  309. * and return OK */
  310. return 1;
  311. }
  312. }
  313. type = CONFIG_TYPE_UNKNOWN;
  314. for (i = 0; default_options[i].name != NULL; i++) {
  315. if (!strcmp(default_options[i].name, name)) {
  316. type = default_options[i].type;
  317. }
  318. }
  319. switch (type) {
  320. case CONFIG_TYPE_UNKNOWN:
  321. /* unknown option */
  322. return 0;
  323. case CONFIG_TYPE_NUMBER:
  324. /* integer number > 0, e.g. number of threads */
  325. if (atol(value) < 0) {
  326. /* invalid number */
  327. return 0;
  328. }
  329. break;
  330. case CONFIG_TYPE_STRING:
  331. /* any text */
  332. break;
  333. case CONFIG_TYPE_BOOLEAN:
  334. /* boolean value, yes or no */
  335. if ((0 != strcmp(value, "yes")) && (0 != strcmp(value, "no"))) {
  336. /* invalid boolean */
  337. return 0;
  338. }
  339. break;
  340. case CONFIG_TYPE_FILE:
  341. case CONFIG_TYPE_DIRECTORY:
  342. /* TODO (low): check this option when it is set, instead of calling
  343. * verify_existence later */
  344. break;
  345. case CONFIG_TYPE_EXT_PATTERN:
  346. /* list of file extentions */
  347. break;
  348. default:
  349. die("Unknown option type - option %s", name);
  350. }
  351. for (i = 0; i < MAX_OPTIONS; i++) {
  352. if (options[2 * i] == NULL) {
  353. options[2 * i] = sdup(name);
  354. options[2 * i + 1] = sdup(value);
  355. options[2 * i + 2] = NULL;
  356. break;
  357. } else if (!strcmp(options[2 * i], name)) {
  358. free(options[2 * i + 1]);
  359. options[2 * i + 1] = sdup(value);
  360. break;
  361. }
  362. }
  363. if (i == MAX_OPTIONS) {
  364. die("Too many options specified");
  365. }
  366. if (options[2 * i] == NULL || options[2 * i + 1] == NULL) {
  367. die("Out of memory");
  368. }
  369. /* option set correctly */
  370. return 1;
  371. }
  372. static void
  373. read_config_file(const char *config_file, char **options)
  374. {
  375. char line[MAX_CONF_FILE_LINE_SIZE], *p;
  376. FILE *fp = NULL;
  377. size_t i, j, line_no = 0;
  378. fp = fopen(config_file, "r");
  379. /* If config file was set in command line and open failed, die */
  380. if (fp == NULL) {
  381. die("Cannot open config file %s: %s", config_file, strerror(errno));
  382. }
  383. /* Load config file settings first */
  384. if (fp != NULL) {
  385. fprintf(stderr, "Loading config file %s\n", config_file);
  386. /* Loop over the lines in config file */
  387. while (fgets(line, sizeof(line), fp) != NULL) {
  388. if (!line_no && !memcmp(line, "\xEF\xBB\xBF", 3)) {
  389. /* strip UTF-8 BOM */
  390. p = line + 3;
  391. } else {
  392. p = line;
  393. }
  394. line_no++;
  395. /* Ignore empty lines and comments */
  396. for (i = 0; isspace(*(unsigned char *)&line[i]);)
  397. i++;
  398. if (p[i] == '#' || p[i] == '\0') {
  399. continue;
  400. }
  401. /* Skip spaces, \r and \n at the end of the line */
  402. for (j = strlen(line) - 1;
  403. isspace(*(unsigned char *)&line[j])
  404. || iscntrl(*(unsigned char *)&line[j]);)
  405. line[j--] = 0;
  406. /* Find the space character between option name and value */
  407. for (j = i; !isspace(*(unsigned char *)&line[j]) && (line[j] != 0);)
  408. j++;
  409. /* Terminate the string - then the string at (line+i) contains the
  410. * option name */
  411. line[j] = 0;
  412. j++;
  413. /* Trim additional spaces between option name and value - then
  414. * (line+j) contains the option value */
  415. while (isspace(line[j]))
  416. j++;
  417. /* Set option */
  418. if (!set_option(options, line + i, line + j)) {
  419. fprintf(stderr,
  420. "%s: line %d is invalid, ignoring it:\n %s",
  421. config_file,
  422. (int)line_no,
  423. p);
  424. }
  425. }
  426. (void)fclose(fp);
  427. }
  428. }
  429. static void
  430. process_command_line_arguments(char *argv[], char **options)
  431. {
  432. char *p;
  433. size_t i, cmd_line_opts_start = 1;
  434. #ifdef CONFIG_FILE2
  435. FILE *fp = NULL;
  436. #endif
  437. /* Should we use a config file ? */
  438. if (argv[1] != NULL && argv[1][0] != '-') {
  439. snprintf(g_config_file, sizeof(g_config_file) - 1, "%s", argv[1]);
  440. cmd_line_opts_start = 2;
  441. } else if ((p = strrchr(argv[0], DIRSEP)) == NULL) {
  442. /* No command line flags specified. Look where binary lives */
  443. snprintf(g_config_file, sizeof(g_config_file) - 1, "%s", CONFIG_FILE);
  444. } else {
  445. snprintf(g_config_file,
  446. sizeof(g_config_file) - 1,
  447. "%.*s%c%s",
  448. (int)(p - argv[0]),
  449. argv[0],
  450. DIRSEP,
  451. CONFIG_FILE);
  452. }
  453. g_config_file[sizeof(g_config_file) - 1] = 0;
  454. #ifdef CONFIG_FILE2
  455. fp = fopen(g_config_file, "r");
  456. /* try alternate config file */
  457. if (fp == NULL) {
  458. fp = fopen(CONFIG_FILE2, "r");
  459. if (fp != NULL) {
  460. strcpy(g_config_file, CONFIG_FILE2);
  461. }
  462. }
  463. if (fp != NULL) {
  464. fclose(fp);
  465. }
  466. #endif
  467. /* read all configurations from a config file */
  468. (void)read_config_file(g_config_file, options);
  469. /* If we're under MacOS and started by launchd, then the second
  470. argument is process serial number, -psn_.....
  471. In this case, don't process arguments at all. */
  472. if (argv[1] == NULL || memcmp(argv[1], "-psn_", 5) != 0) {
  473. /* Handle command line flags.
  474. They override config file and default settings. */
  475. for (i = cmd_line_opts_start; argv[i] != NULL; i += 2) {
  476. if (argv[i][0] != '-' || argv[i + 1] == NULL) {
  477. show_usage_and_exit(argv[0]);
  478. }
  479. if (!set_option(options, &argv[i][1], argv[i + 1])) {
  480. fprintf(
  481. stderr,
  482. "command line option is invalid, ignoring it:\n %s %s\n",
  483. argv[i],
  484. argv[i + 1]);
  485. }
  486. }
  487. }
  488. }
  489. static void
  490. init_server_name(int argc, const char *argv[])
  491. {
  492. int i;
  493. assert(sizeof(main_config_options) / sizeof(main_config_options[0])
  494. == NUM_MAIN_OPTIONS + 1);
  495. assert((strlen(mg_version()) + 12) < sizeof(g_server_base_name));
  496. snprintf(g_server_base_name,
  497. sizeof(g_server_base_name),
  498. "CivetWeb V%s",
  499. mg_version());
  500. g_server_name = g_server_base_name;
  501. for (i = 0; i < argc - 1; i++) {
  502. if ((argv[i][0] == '-')
  503. && (0 == strcmp(argv[i] + 1,
  504. main_config_options[OPTION_TITLE].name))) {
  505. g_server_name = (char *)(argv[i + 1]);
  506. }
  507. }
  508. g_icon_name = NULL;
  509. for (i = 0; i < argc - 1; i++) {
  510. if ((argv[i][0] == '-')
  511. && (0 == strcmp(argv[i] + 1,
  512. main_config_options[OPTION_ICON].name))) {
  513. g_icon_name = (char *)(argv[i + 1]);
  514. }
  515. }
  516. }
  517. static int
  518. log_message(const struct mg_connection *conn, const char *message)
  519. {
  520. const struct mg_context *ctx = mg_get_context(conn);
  521. struct tuser_data *ud = (struct tuser_data *)mg_get_user_data(ctx);
  522. fprintf(stderr, "%s\n", message);
  523. if (ud->first_message == NULL) {
  524. ud->first_message = sdup(message);
  525. }
  526. return 0;
  527. }
  528. static int
  529. is_path_absolute(const char *path)
  530. {
  531. #ifdef _WIN32
  532. return path != NULL
  533. && ((path[0] == '\\' && path[1] == '\\') || /* UNC path, e.g.
  534. \\server\dir */
  535. (isalpha(path[0]) && path[1] == ':'
  536. && path[2] == '\\')); /* E.g. X:\dir */
  537. #else
  538. return path != NULL && path[0] == '/';
  539. #endif
  540. }
  541. static void
  542. verify_existence(char **options, const char *option_name, int must_be_dir)
  543. {
  544. struct stat st;
  545. const char *path = get_option(options, option_name);
  546. #ifdef _WIN32
  547. wchar_t wbuf[1024];
  548. char mbbuf[1024];
  549. int len;
  550. if (path) {
  551. memset(wbuf, 0, sizeof(wbuf));
  552. memset(mbbuf, 0, sizeof(mbbuf));
  553. len = MultiByteToWideChar(CP_UTF8,
  554. 0,
  555. path,
  556. -1,
  557. wbuf,
  558. (int)sizeof(wbuf) / sizeof(wbuf[0]) - 1);
  559. wcstombs(mbbuf, wbuf, sizeof(mbbuf) - 1);
  560. path = mbbuf;
  561. (void)len;
  562. }
  563. #endif
  564. if (path != NULL && (stat(path, &st) != 0
  565. || ((S_ISDIR(st.st_mode) ? 1 : 0) != must_be_dir))) {
  566. die("Invalid path for %s: [%s]: (%s). Make sure that path is either "
  567. "absolute, or it is relative to civetweb executable.",
  568. option_name,
  569. path,
  570. strerror(errno));
  571. }
  572. }
  573. static void
  574. set_absolute_path(char *options[],
  575. const char *option_name,
  576. const char *path_to_civetweb_exe)
  577. {
  578. char path[PATH_MAX] = "", absolute[PATH_MAX] = "";
  579. const char *option_value;
  580. const char *p;
  581. /* Check whether option is already set */
  582. option_value = get_option(options, option_name);
  583. /* If option is already set and it is an absolute path,
  584. leave it as it is -- it's already absolute. */
  585. if (option_value != NULL && !is_path_absolute(option_value)) {
  586. /* Not absolute. Use the directory where civetweb executable lives
  587. be the relative directory for everything.
  588. Extract civetweb executable directory into path. */
  589. if ((p = strrchr(path_to_civetweb_exe, DIRSEP)) == NULL) {
  590. IGNORE_UNUSED_RESULT(getcwd(path, sizeof(path)));
  591. } else {
  592. snprintf(path,
  593. sizeof(path) - 1,
  594. "%.*s",
  595. (int)(p - path_to_civetweb_exe),
  596. path_to_civetweb_exe);
  597. path[sizeof(path) - 1] = 0;
  598. }
  599. strncat(path, "/", sizeof(path) - strlen(path) - 1);
  600. strncat(path, option_value, sizeof(path) - strlen(path) - 1);
  601. /* Absolutize the path, and set the option */
  602. IGNORE_UNUSED_RESULT(abs_path(path, absolute, sizeof(absolute)));
  603. set_option(options, option_name, absolute);
  604. }
  605. }
  606. #ifdef USE_LUA
  607. #include "civetweb_lua.h"
  608. #include "civetweb_private_lua.h"
  609. static int
  610. run_lua(const char *file_name)
  611. {
  612. struct lua_State *L;
  613. int lua_ret;
  614. int func_ret = EXIT_FAILURE;
  615. const char *lua_err_txt;
  616. #ifdef WIN32
  617. (void)MakeConsole();
  618. #endif
  619. L = luaL_newstate();
  620. if (L == NULL) {
  621. fprintf(stderr, "Error: Cannot create Lua state\n");
  622. return EXIT_FAILURE;
  623. }
  624. civetweb_open_lua_libs(L);
  625. lua_ret = luaL_loadfile(L, file_name);
  626. if (lua_ret != LUA_OK) {
  627. /* Error when loading the file (e.g. file not found, out of memory, ...)
  628. */
  629. lua_err_txt = lua_tostring(L, -1);
  630. fprintf(stderr, "Error loading file %s: %s\n", file_name, lua_err_txt);
  631. } else {
  632. /* The script file is loaded, now call it */
  633. lua_ret = lua_pcall(L,
  634. /* no arguments */ 0,
  635. /* zero or one return value */ 1,
  636. /* errors as strint return value */ 0);
  637. if (lua_ret != LUA_OK) {
  638. /* Error when executing the script */
  639. lua_err_txt = lua_tostring(L, -1);
  640. fprintf(stderr,
  641. "Error running file %s: %s\n",
  642. file_name,
  643. lua_err_txt);
  644. } else {
  645. /* Script executed */
  646. if (lua_type(L, -1) == LUA_TNUMBER) {
  647. func_ret = (int)lua_tonumber(L, -1);
  648. } else {
  649. func_ret = EXIT_SUCCESS;
  650. }
  651. }
  652. }
  653. lua_close(L);
  654. return func_ret;
  655. }
  656. #endif
  657. #ifdef USE_DUKTAPE
  658. #include "duktape.h"
  659. static int
  660. run_duktape(const char *file_name)
  661. {
  662. duk_context *ctx = NULL;
  663. #ifdef WIN32
  664. (void)MakeConsole();
  665. #endif
  666. ctx = duk_create_heap_default();
  667. if (!ctx) {
  668. fprintf(stderr, "Failed to create a Duktape heap.\n");
  669. goto finished;
  670. }
  671. if (duk_peval_file(ctx, file_name) != 0) {
  672. fprintf(stderr, "%s\n", duk_safe_to_string(ctx, -1));
  673. goto finished;
  674. }
  675. duk_pop(ctx); /* ignore result */
  676. finished:
  677. duk_destroy_heap(ctx);
  678. return 0;
  679. }
  680. #endif
  681. #if defined(__MINGW32__) || defined(__MINGW64__)
  682. /* For __MINGW32/64_MAJOR/MINOR_VERSION define */
  683. #include <_mingw.h>
  684. #endif
  685. static void
  686. start_civetweb(int argc, char *argv[])
  687. {
  688. struct mg_callbacks callbacks;
  689. char *options[2 * MAX_OPTIONS + 1];
  690. int i;
  691. /* Show system information and exit */
  692. if (argc > 1 && !strcmp(argv[1], "-I")) {
  693. const char *version = mg_version();
  694. #if defined(_WIN32)
  695. #if !defined(__SYMBIAN32__)
  696. DWORD dwVersion = 0;
  697. DWORD dwMajorVersion = 0;
  698. DWORD dwMinorVersion = 0;
  699. SYSTEM_INFO si;
  700. GetSystemInfo(&si);
  701. #ifdef _MSC_VER
  702. #pragma warning(push)
  703. // GetVersion was declared deprecated
  704. #pragma warning(disable : 4996)
  705. #endif
  706. dwVersion = GetVersion();
  707. #ifdef _MSC_VER
  708. #pragma warning(pop)
  709. #endif
  710. dwMajorVersion = (DWORD)(LOBYTE(LOWORD(dwVersion)));
  711. dwMinorVersion = (DWORD)(HIBYTE(LOWORD(dwVersion)));
  712. (void)MakeConsole();
  713. fprintf(stdout, "\n%s\n", g_server_name);
  714. fprintf(stdout,
  715. "%s - Windows %u.%u\n",
  716. g_server_base_name,
  717. (unsigned)dwMajorVersion,
  718. (unsigned)dwMinorVersion);
  719. fprintf(stdout,
  720. "CPU: type %u, cores %u, mask %x\n",
  721. (unsigned)si.wProcessorArchitecture,
  722. (unsigned)si.dwNumberOfProcessors,
  723. (unsigned)si.dwActiveProcessorMask);
  724. #else
  725. fprintf(stdout, "\n%s\n", g_server_name);
  726. fprintf(stdout, "%s - Symbian\n", g_server_base_name);
  727. #endif
  728. #else
  729. struct utsname name;
  730. memset(&name, 0, sizeof(name));
  731. uname(&name);
  732. fprintf(stdout, "\n%s\n", g_server_name);
  733. fprintf(stdout,
  734. "%s - %s %s (%s) - %s\n",
  735. g_server_base_name,
  736. name.sysname,
  737. name.version,
  738. name.release,
  739. name.machine);
  740. #endif
  741. fprintf(stdout, "Features:");
  742. if (mg_check_feature(1)) {
  743. fprintf(stdout, " Files");
  744. }
  745. if (mg_check_feature(2)) {
  746. fprintf(stdout, " HTTPS");
  747. }
  748. if (mg_check_feature(4)) {
  749. fprintf(stdout, " CGI");
  750. }
  751. if (mg_check_feature(8)) {
  752. fprintf(stdout, " IPv6");
  753. }
  754. if (mg_check_feature(16)) {
  755. fprintf(stdout, " WebSockets");
  756. }
  757. if (mg_check_feature(32)) {
  758. fprintf(stdout, " Lua");
  759. }
  760. fprintf(stdout, "\n");
  761. #ifdef USE_LUA
  762. fprintf(stdout,
  763. "Lua Version: %u (%s)\n",
  764. (unsigned)LUA_VERSION_NUM,
  765. LUA_RELEASE);
  766. #endif
  767. fprintf(stdout, "Version: %s\n", version);
  768. fprintf(stdout, "Build: %s\n", __DATE__);
  769. /* http://sourceforge.net/p/predef/wiki/Compilers/ */
  770. #if defined(_MSC_VER)
  771. fprintf(stdout,
  772. "MSC: %u (%u)\n",
  773. (unsigned)_MSC_VER,
  774. (unsigned)_MSC_FULL_VER);
  775. #elif defined(__MINGW64__)
  776. fprintf(stdout,
  777. "MinGW64: %u.%u\n",
  778. (unsigned)__MINGW64_VERSION_MAJOR,
  779. (unsigned)__MINGW64_VERSION_MINOR);
  780. fprintf(stdout,
  781. "MinGW32: %u.%u\n",
  782. (unsigned)__MINGW32_MAJOR_VERSION,
  783. (unsigned)__MINGW32_MINOR_VERSION);
  784. #elif defined(__MINGW32__)
  785. fprintf(stdout,
  786. "MinGW32: %u.%u\n",
  787. (unsigned)__MINGW32_MAJOR_VERSION,
  788. (unsigned)__MINGW32_MINOR_VERSION);
  789. #elif defined(__clang__)
  790. fprintf(stdout,
  791. "clang: %u.%u.%u (%s)\n",
  792. __clang_major__,
  793. __clang_minor__,
  794. __clang_patchlevel__,
  795. __clang_version__);
  796. #elif defined(__GNUC__)
  797. fprintf(stdout,
  798. "gcc: %u.%u.%u\n",
  799. (unsigned)__GNUC__,
  800. (unsigned)__GNUC_MINOR__,
  801. (unsigned)__GNUC_PATCHLEVEL__);
  802. #elif defined(__INTEL_COMPILER)
  803. fprintf(stdout, "Intel C/C++: %u\n", (unsigned)__INTEL_COMPILER);
  804. #elif defined(__BORLANDC__)
  805. fprintf(stdout, "Borland C: 0x%x\n", (unsigned)__BORLANDC__);
  806. #elif defined(__SUNPRO_C)
  807. fprintf(stdout, "Solaris: 0x%x\n", (unsigned)__SUNPRO_C);
  808. #else
  809. fprintf(stdout, "Other\n");
  810. #endif
  811. exit(EXIT_SUCCESS);
  812. }
  813. /* Edit passwords file: Add user or change password, if -A option is
  814. * specified */
  815. if (argc > 1 && !strcmp(argv[1], "-A")) {
  816. if (argc != 6) {
  817. show_usage_and_exit(argv[0]);
  818. }
  819. exit(mg_modify_passwords_file(argv[2], argv[3], argv[4], argv[5])
  820. ? EXIT_SUCCESS
  821. : EXIT_FAILURE);
  822. }
  823. /* Edit passwords file: Remove user, if -R option is specified */
  824. if (argc > 1 && !strcmp(argv[1], "-R")) {
  825. if (argc != 5) {
  826. show_usage_and_exit(argv[0]);
  827. }
  828. exit(mg_modify_passwords_file(argv[2], argv[3], argv[4], NULL)
  829. ? EXIT_SUCCESS
  830. : EXIT_FAILURE);
  831. }
  832. /* Call Lua with additional CivetWeb specific Lua functions, if -L option
  833. * is specified */
  834. if (argc > 1 && !strcmp(argv[1], "-L")) {
  835. #ifdef USE_LUA
  836. if (argc != 3) {
  837. show_usage_and_exit(argv[0]);
  838. }
  839. exit(run_lua(argv[2]));
  840. #else
  841. show_server_name();
  842. fprintf(stderr, "\nError: Lua support not enabled\n");
  843. exit(EXIT_FAILURE);
  844. #endif
  845. }
  846. /* Call Duktape, if -E option is specified */
  847. if (argc > 1 && !strcmp(argv[1], "-E")) {
  848. #ifdef USE_DUKTAPE
  849. if (argc != 3) {
  850. show_usage_and_exit(argv[0]);
  851. }
  852. exit(run_duktape(argv[2]));
  853. #else
  854. show_server_name();
  855. fprintf(stderr, "\nError: Ecmascript support not enabled\n");
  856. exit(EXIT_FAILURE);
  857. #endif
  858. }
  859. /* Show usage if -h or --help options are specified */
  860. if (argc == 2 && (!strcmp(argv[1], "-h") || !strcmp(argv[1], "-H")
  861. || !strcmp(argv[1], "--help"))) {
  862. show_usage_and_exit(argv[0]);
  863. }
  864. options[0] = NULL;
  865. set_option(options, "document_root", ".");
  866. /* Update config based on command line arguments */
  867. process_command_line_arguments(argv, options);
  868. /* Make sure we have absolute paths for files and directories */
  869. set_absolute_path(options, "document_root", argv[0]);
  870. set_absolute_path(options, "put_delete_auth_file", argv[0]);
  871. set_absolute_path(options, "cgi_interpreter", argv[0]);
  872. set_absolute_path(options, "access_log_file", argv[0]);
  873. set_absolute_path(options, "error_log_file", argv[0]);
  874. set_absolute_path(options, "global_auth_file", argv[0]);
  875. #ifdef USE_LUA
  876. set_absolute_path(options, "lua_preload_file", argv[0]);
  877. #endif
  878. set_absolute_path(options, "ssl_certificate", argv[0]);
  879. /* Make extra verification for certain options */
  880. verify_existence(options, "document_root", 1);
  881. verify_existence(options, "cgi_interpreter", 0);
  882. verify_existence(options, "ssl_certificate", 0);
  883. verify_existence(options, "ssl_ca_path", 1);
  884. verify_existence(options, "ssl_ca_file", 0);
  885. #ifdef USE_LUA
  886. verify_existence(options, "lua_preload_file", 0);
  887. #endif
  888. /* Setup signal handler: quit on Ctrl-C */
  889. signal(SIGTERM, signal_handler);
  890. signal(SIGINT, signal_handler);
  891. /* Initialize user data */
  892. memset(&g_user_data, 0, sizeof(g_user_data));
  893. /* Start Civetweb */
  894. memset(&callbacks, 0, sizeof(callbacks));
  895. callbacks.log_message = &log_message;
  896. g_ctx = mg_start(&callbacks, &g_user_data, (const char **)options);
  897. for (i = 0; options[i] != NULL; i++) {
  898. free(options[i]);
  899. }
  900. if (g_ctx == NULL) {
  901. die("Failed to start Civetweb:\n%s",
  902. (g_user_data.first_message == NULL) ? "unknown reason"
  903. : g_user_data.first_message);
  904. }
  905. }
  906. static void
  907. stop_civetweb(void)
  908. {
  909. mg_stop(g_ctx);
  910. free(g_user_data.first_message);
  911. g_user_data.first_message = NULL;
  912. }
  913. #ifdef _WIN32
  914. enum {
  915. ID_ICON = 100,
  916. ID_QUIT,
  917. ID_SETTINGS,
  918. ID_SEPARATOR,
  919. ID_INSTALL_SERVICE,
  920. ID_REMOVE_SERVICE,
  921. ID_STATIC,
  922. ID_GROUP,
  923. ID_PASSWORD,
  924. ID_SAVE,
  925. ID_RESET_DEFAULTS,
  926. ID_RESET_FILE,
  927. ID_RESET_ACTIVE,
  928. ID_STATUS,
  929. ID_CONNECT,
  930. ID_ADD_USER,
  931. ID_ADD_USER_NAME,
  932. ID_ADD_USER_REALM,
  933. ID_INPUT_LINE,
  934. /* All dynamically created text boxes for options have IDs starting from
  935. ID_CONTROLS, incremented by one. */
  936. ID_CONTROLS = 200,
  937. /* Text boxes for files have "..." buttons to open file browser. These
  938. buttons have IDs that are ID_FILE_BUTTONS_DELTA higher than associated
  939. text box ID. */
  940. ID_FILE_BUTTONS_DELTA = 1000
  941. };
  942. static HICON hIcon;
  943. static SERVICE_STATUS ss;
  944. static SERVICE_STATUS_HANDLE hStatus;
  945. static const char *service_magic_argument = "--";
  946. static NOTIFYICONDATA TrayIcon;
  947. static void WINAPI
  948. ControlHandler(DWORD code)
  949. {
  950. if (code == SERVICE_CONTROL_STOP || code == SERVICE_CONTROL_SHUTDOWN) {
  951. ss.dwWin32ExitCode = 0;
  952. ss.dwCurrentState = SERVICE_STOPPED;
  953. }
  954. SetServiceStatus(hStatus, &ss);
  955. }
  956. static void WINAPI
  957. ServiceMain(void)
  958. {
  959. ss.dwServiceType = SERVICE_WIN32;
  960. ss.dwCurrentState = SERVICE_RUNNING;
  961. ss.dwControlsAccepted = SERVICE_ACCEPT_STOP | SERVICE_ACCEPT_SHUTDOWN;
  962. hStatus = RegisterServiceCtrlHandler(g_server_name, ControlHandler);
  963. SetServiceStatus(hStatus, &ss);
  964. while (ss.dwCurrentState == SERVICE_RUNNING) {
  965. Sleep(1000);
  966. }
  967. stop_civetweb();
  968. ss.dwCurrentState = SERVICE_STOPPED;
  969. ss.dwWin32ExitCode = (DWORD)-1;
  970. SetServiceStatus(hStatus, &ss);
  971. }
  972. static void
  973. show_error(void)
  974. {
  975. char buf[256];
  976. FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
  977. NULL,
  978. GetLastError(),
  979. MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
  980. buf,
  981. sizeof(buf),
  982. NULL);
  983. MessageBox(NULL, buf, "Error", MB_OK);
  984. }
  985. static void *
  986. align(void *ptr, DWORD alig)
  987. {
  988. uintptr_t ul = (uintptr_t)ptr;
  989. ul += alig;
  990. ul &= ~alig;
  991. return ((void *)ul);
  992. }
  993. static void
  994. save_config(HWND hDlg, FILE *fp)
  995. {
  996. char value[2000] = "";
  997. const char *default_value;
  998. const struct mg_option *options;
  999. int i, id;
  1000. fprintf(fp, "%s", config_file_top_comment);
  1001. options = mg_get_valid_options();
  1002. for (i = 0; options[i].name != NULL; i++) {
  1003. id = ID_CONTROLS + i;
  1004. if (options[i].type == CONFIG_TYPE_BOOLEAN) {
  1005. snprintf(value,
  1006. sizeof(value) - 1,
  1007. "%s",
  1008. IsDlgButtonChecked(hDlg, id) ? "yes" : "no");
  1009. value[sizeof(value) - 1] = 0;
  1010. } else {
  1011. GetDlgItemText(hDlg, id, value, sizeof(value));
  1012. }
  1013. default_value =
  1014. options[i].default_value == NULL ? "" : options[i].default_value;
  1015. /* If value is the same as default, skip it */
  1016. if (strcmp(value, default_value) != 0) {
  1017. fprintf(fp, "%s %s\n", options[i].name, value);
  1018. }
  1019. }
  1020. }
  1021. static INT_PTR CALLBACK
  1022. SettingsDlgProc(HWND hDlg, UINT msg, WPARAM wParam, LPARAM lParam)
  1023. {
  1024. FILE *fp;
  1025. int i, j;
  1026. const char *name, *value;
  1027. const struct mg_option *default_options = mg_get_valid_options();
  1028. char *file_options[MAX_OPTIONS * 2 + 1] = {0};
  1029. char *title;
  1030. (void)lParam;
  1031. switch (msg) {
  1032. case WM_CLOSE:
  1033. DestroyWindow(hDlg);
  1034. break;
  1035. case WM_COMMAND:
  1036. switch (LOWORD(wParam)) {
  1037. case ID_SAVE:
  1038. EnableWindow(GetDlgItem(hDlg, ID_SAVE), FALSE);
  1039. if ((fp = fopen(g_config_file, "w+")) != NULL) {
  1040. save_config(hDlg, fp);
  1041. fclose(fp);
  1042. stop_civetweb();
  1043. start_civetweb(__argc, __argv);
  1044. }
  1045. EnableWindow(GetDlgItem(hDlg, ID_SAVE), TRUE);
  1046. break;
  1047. case ID_RESET_DEFAULTS:
  1048. for (i = 0; default_options[i].name != NULL; i++) {
  1049. name = default_options[i].name;
  1050. value = default_options[i].default_value == NULL
  1051. ? ""
  1052. : default_options[i].default_value;
  1053. if (default_options[i].type == CONFIG_TYPE_BOOLEAN) {
  1054. CheckDlgButton(hDlg,
  1055. ID_CONTROLS + i,
  1056. !strcmp(value, "yes") ? BST_CHECKED
  1057. : BST_UNCHECKED);
  1058. } else {
  1059. SetWindowText(GetDlgItem(hDlg, ID_CONTROLS + i), value);
  1060. }
  1061. }
  1062. break;
  1063. case ID_RESET_FILE:
  1064. read_config_file(g_config_file, file_options);
  1065. for (i = 0; default_options[i].name != NULL; i++) {
  1066. name = default_options[i].name;
  1067. value = default_options[i].default_value;
  1068. for (j = 0; file_options[j * 2] != NULL; j++) {
  1069. if (!strcmp(name, file_options[j * 2])) {
  1070. value = file_options[j * 2 + 1];
  1071. }
  1072. }
  1073. if (value == NULL) {
  1074. value = "";
  1075. }
  1076. if (default_options[i].type == CONFIG_TYPE_BOOLEAN) {
  1077. CheckDlgButton(hDlg,
  1078. ID_CONTROLS + i,
  1079. !strcmp(value, "yes") ? BST_CHECKED
  1080. : BST_UNCHECKED);
  1081. } else {
  1082. SetWindowText(GetDlgItem(hDlg, ID_CONTROLS + i), value);
  1083. }
  1084. }
  1085. for (i = 0; i < MAX_OPTIONS; i++) {
  1086. free(file_options[2 * i]);
  1087. free(file_options[2 * i + 1]);
  1088. }
  1089. break;
  1090. case ID_RESET_ACTIVE:
  1091. for (i = 0; default_options[i].name != NULL; i++) {
  1092. name = default_options[i].name;
  1093. value = mg_get_option(g_ctx, name);
  1094. if (default_options[i].type == CONFIG_TYPE_BOOLEAN) {
  1095. CheckDlgButton(hDlg,
  1096. ID_CONTROLS + i,
  1097. !strcmp(value, "yes") ? BST_CHECKED
  1098. : BST_UNCHECKED);
  1099. } else {
  1100. SetDlgItemText(hDlg,
  1101. ID_CONTROLS + i,
  1102. value == NULL ? "" : value);
  1103. }
  1104. }
  1105. break;
  1106. }
  1107. for (i = 0; default_options[i].name != NULL; i++) {
  1108. name = default_options[i].name;
  1109. if (((default_options[i].type == CONFIG_TYPE_FILE)
  1110. || (default_options[i].type == CONFIG_TYPE_DIRECTORY))
  1111. && LOWORD(wParam) == ID_CONTROLS + i + ID_FILE_BUTTONS_DELTA) {
  1112. OPENFILENAME of;
  1113. BROWSEINFO bi;
  1114. char path[PATH_MAX] = "";
  1115. memset(&of, 0, sizeof(of));
  1116. of.lStructSize = sizeof(of);
  1117. of.hwndOwner = (HWND)hDlg;
  1118. of.lpstrFile = path;
  1119. of.nMaxFile = sizeof(path);
  1120. of.lpstrInitialDir = mg_get_option(g_ctx, "document_root");
  1121. of.Flags =
  1122. OFN_CREATEPROMPT | OFN_NOCHANGEDIR | OFN_HIDEREADONLY;
  1123. memset(&bi, 0, sizeof(bi));
  1124. bi.hwndOwner = (HWND)hDlg;
  1125. bi.lpszTitle = "Choose WWW root directory:";
  1126. bi.ulFlags = BIF_RETURNONLYFSDIRS;
  1127. if (default_options[i].type == CONFIG_TYPE_DIRECTORY) {
  1128. SHGetPathFromIDList(SHBrowseForFolder(&bi), path);
  1129. } else {
  1130. GetOpenFileName(&of);
  1131. }
  1132. if (path[0] != '\0') {
  1133. SetWindowText(GetDlgItem(hDlg, ID_CONTROLS + i), path);
  1134. }
  1135. }
  1136. }
  1137. break;
  1138. case WM_INITDIALOG:
  1139. SendMessage(hDlg, WM_SETICON, (WPARAM)ICON_SMALL, (LPARAM)hIcon);
  1140. SendMessage(hDlg, WM_SETICON, (WPARAM)ICON_BIG, (LPARAM)hIcon);
  1141. title = malloc(strlen(g_server_name) + 16);
  1142. if (title) {
  1143. strcpy(title, g_server_name);
  1144. strcat(title, " settings");
  1145. SetWindowText(hDlg, title);
  1146. free(title);
  1147. }
  1148. SetFocus(GetDlgItem(hDlg, ID_SAVE));
  1149. /* Init dialog with active settings */
  1150. SendMessage(hDlg, WM_COMMAND, ID_RESET_ACTIVE, 0);
  1151. /* alternative: SendMessage(hDlg, WM_COMMAND, ID_RESET_FILE, 0); */
  1152. break;
  1153. default:
  1154. break;
  1155. }
  1156. return FALSE;
  1157. }
  1158. struct tstring_input_buf {
  1159. unsigned buflen;
  1160. char *buffer;
  1161. };
  1162. static INT_PTR CALLBACK
  1163. InputDlgProc(HWND hDlg, UINT msg, WPARAM wParam, LPARAM lP)
  1164. {
  1165. static struct tstring_input_buf *inBuf = 0;
  1166. WORD ctrlId;
  1167. switch (msg) {
  1168. case WM_CLOSE:
  1169. inBuf = 0;
  1170. DestroyWindow(hDlg);
  1171. break;
  1172. case WM_COMMAND:
  1173. ctrlId = LOWORD(wParam);
  1174. if (ctrlId == IDOK) {
  1175. /* Add user */
  1176. GetWindowText(GetDlgItem(hDlg, ID_INPUT_LINE),
  1177. inBuf->buffer,
  1178. (int)inBuf->buflen);
  1179. if (strlen(inBuf->buffer) > 0) {
  1180. EndDialog(hDlg, IDOK);
  1181. }
  1182. } else if (ctrlId == IDCANCEL) {
  1183. EndDialog(hDlg, IDCANCEL);
  1184. }
  1185. break;
  1186. case WM_INITDIALOG:
  1187. inBuf = (struct tstring_input_buf *)lP;
  1188. assert(inBuf != NULL);
  1189. assert((inBuf->buffer != NULL) && (inBuf->buflen != 0));
  1190. assert(strlen(inBuf->buffer) < inBuf->buflen);
  1191. SendMessage(hDlg, WM_SETICON, (WPARAM)ICON_SMALL, (LPARAM)hIcon);
  1192. SendMessage(hDlg, WM_SETICON, (WPARAM)ICON_BIG, (LPARAM)hIcon);
  1193. SendDlgItemMessage(
  1194. hDlg, ID_INPUT_LINE, EM_LIMITTEXT, inBuf->buflen - 1, 0);
  1195. SetWindowText(GetDlgItem(hDlg, ID_INPUT_LINE), inBuf->buffer);
  1196. SetWindowText(hDlg, "Modify password");
  1197. SetFocus(GetDlgItem(hDlg, ID_INPUT_LINE));
  1198. break;
  1199. default:
  1200. break;
  1201. }
  1202. return FALSE;
  1203. }
  1204. static void
  1205. suggest_passwd(char *passwd)
  1206. {
  1207. unsigned u;
  1208. char *p;
  1209. union {
  1210. FILETIME ft;
  1211. LARGE_INTEGER li;
  1212. } num;
  1213. /* valid characters are 32 to 126 */
  1214. GetSystemTimeAsFileTime(&num.ft);
  1215. num.li.HighPart |= (LONG)GetCurrentProcessId();
  1216. p = passwd;
  1217. while (num.li.QuadPart) {
  1218. u = (unsigned)(num.li.QuadPart % 95);
  1219. num.li.QuadPart -= u;
  1220. num.li.QuadPart /= 95;
  1221. *p = (char)(u + 32);
  1222. p++;
  1223. }
  1224. }
  1225. static void add_control(unsigned char **mem,
  1226. DLGTEMPLATE *dia,
  1227. WORD type,
  1228. WORD id,
  1229. DWORD style,
  1230. short x,
  1231. short y,
  1232. short cx,
  1233. short cy,
  1234. const char *caption);
  1235. static int
  1236. get_password(const char *user,
  1237. const char *realm,
  1238. char *passwd,
  1239. unsigned passwd_len)
  1240. {
  1241. #define HEIGHT (15)
  1242. #define WIDTH (280)
  1243. #define LABEL_WIDTH (90)
  1244. unsigned char mem[4096], *p;
  1245. DLGTEMPLATE *dia = (DLGTEMPLATE *)mem;
  1246. int ok;
  1247. short y;
  1248. struct tstring_input_buf dlgprms;
  1249. static struct {
  1250. DLGTEMPLATE template; /* 18 bytes */
  1251. WORD menu, class;
  1252. wchar_t caption[1];
  1253. WORD fontsiz;
  1254. wchar_t fontface[7];
  1255. } dialog_header = {{WS_CAPTION | WS_POPUP | WS_SYSMENU | WS_VISIBLE
  1256. | DS_SETFONT | WS_DLGFRAME,
  1257. WS_EX_TOOLWINDOW,
  1258. 0,
  1259. 200,
  1260. 200,
  1261. WIDTH,
  1262. 0},
  1263. 0,
  1264. 0,
  1265. L"",
  1266. 8,
  1267. L"Tahoma"};
  1268. dlgprms.buffer = passwd;
  1269. dlgprms.buflen = passwd_len;
  1270. assert((user != NULL) && (realm != NULL) && (passwd != NULL));
  1271. if (guard < 100) {
  1272. guard += 100;
  1273. } else {
  1274. return 0;
  1275. }
  1276. /* Create a password suggestion */
  1277. memset(passwd, 0, passwd_len);
  1278. suggest_passwd(passwd);
  1279. /* Create the dialog */
  1280. (void)memset(mem, 0, sizeof(mem));
  1281. (void)memcpy(mem, &dialog_header, sizeof(dialog_header));
  1282. p = mem + sizeof(dialog_header);
  1283. y = HEIGHT;
  1284. add_control(&p,
  1285. dia,
  1286. 0x82,
  1287. ID_STATIC,
  1288. WS_VISIBLE | WS_CHILD,
  1289. 10,
  1290. y,
  1291. LABEL_WIDTH,
  1292. HEIGHT,
  1293. "User:");
  1294. add_control(&p,
  1295. dia,
  1296. 0x81,
  1297. ID_CONTROLS + 1,
  1298. WS_CHILD | WS_VISIBLE | WS_BORDER | ES_AUTOHSCROLL
  1299. | WS_DISABLED,
  1300. 15 + LABEL_WIDTH,
  1301. y,
  1302. WIDTH - LABEL_WIDTH - 25,
  1303. HEIGHT,
  1304. user);
  1305. y += HEIGHT;
  1306. add_control(&p,
  1307. dia,
  1308. 0x82,
  1309. ID_STATIC,
  1310. WS_VISIBLE | WS_CHILD,
  1311. 10,
  1312. y,
  1313. LABEL_WIDTH,
  1314. HEIGHT,
  1315. "Realm:");
  1316. add_control(&p,
  1317. dia,
  1318. 0x81,
  1319. ID_CONTROLS + 2,
  1320. WS_CHILD | WS_VISIBLE | WS_BORDER | ES_AUTOHSCROLL
  1321. | WS_DISABLED,
  1322. 15 + LABEL_WIDTH,
  1323. y,
  1324. WIDTH - LABEL_WIDTH - 25,
  1325. HEIGHT,
  1326. realm);
  1327. y += HEIGHT;
  1328. add_control(&p,
  1329. dia,
  1330. 0x82,
  1331. ID_STATIC,
  1332. WS_VISIBLE | WS_CHILD,
  1333. 10,
  1334. y,
  1335. LABEL_WIDTH,
  1336. HEIGHT,
  1337. "Password:");
  1338. add_control(&p,
  1339. dia,
  1340. 0x81,
  1341. ID_INPUT_LINE,
  1342. WS_CHILD | WS_VISIBLE | WS_BORDER | ES_AUTOHSCROLL | WS_TABSTOP,
  1343. 15 + LABEL_WIDTH,
  1344. y,
  1345. WIDTH - LABEL_WIDTH - 25,
  1346. HEIGHT,
  1347. "");
  1348. y += (WORD)(HEIGHT * 2);
  1349. add_control(&p,
  1350. dia,
  1351. 0x80,
  1352. IDOK,
  1353. WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON | WS_TABSTOP,
  1354. 80,
  1355. y,
  1356. 55,
  1357. 12,
  1358. "Ok");
  1359. add_control(&p,
  1360. dia,
  1361. 0x80,
  1362. IDCANCEL,
  1363. WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON | WS_TABSTOP,
  1364. 140,
  1365. y,
  1366. 55,
  1367. 12,
  1368. "Cancel");
  1369. assert((intptr_t)p - (intptr_t)mem < (intptr_t)sizeof(mem));
  1370. dia->cy = y + (WORD)(HEIGHT * 1.5);
  1371. ok = (IDOK == DialogBoxIndirectParam(
  1372. NULL, dia, NULL, InputDlgProc, (LPARAM)&dlgprms));
  1373. guard -= 100;
  1374. return ok;
  1375. #undef HEIGHT
  1376. #undef WIDTH
  1377. #undef LABEL_WIDTH
  1378. }
  1379. static INT_PTR CALLBACK
  1380. PasswordDlgProc(HWND hDlg, UINT msg, WPARAM wParam, LPARAM lP)
  1381. {
  1382. static const char *passfile = 0;
  1383. char domain[256], user[256], password[256];
  1384. WORD ctrlId;
  1385. switch (msg) {
  1386. case WM_CLOSE:
  1387. passfile = 0;
  1388. DestroyWindow(hDlg);
  1389. break;
  1390. case WM_COMMAND:
  1391. ctrlId = LOWORD(wParam);
  1392. if (ctrlId == ID_ADD_USER) {
  1393. /* Add user */
  1394. GetWindowText(GetDlgItem(hDlg, ID_ADD_USER_NAME),
  1395. user,
  1396. sizeof(user));
  1397. GetWindowText(GetDlgItem(hDlg, ID_ADD_USER_REALM),
  1398. domain,
  1399. sizeof(domain));
  1400. if (get_password(user, domain, password, sizeof(password))) {
  1401. mg_modify_passwords_file(passfile, domain, user, password);
  1402. EndDialog(hDlg, IDOK);
  1403. }
  1404. } else if ((ctrlId >= (ID_CONTROLS + ID_FILE_BUTTONS_DELTA * 3))
  1405. && (ctrlId < (ID_CONTROLS + ID_FILE_BUTTONS_DELTA * 4))) {
  1406. /* Modify password */
  1407. GetWindowText(GetDlgItem(hDlg, ctrlId - ID_FILE_BUTTONS_DELTA * 3),
  1408. user,
  1409. sizeof(user));
  1410. GetWindowText(GetDlgItem(hDlg, ctrlId - ID_FILE_BUTTONS_DELTA * 2),
  1411. domain,
  1412. sizeof(domain));
  1413. if (get_password(user, domain, password, sizeof(password))) {
  1414. mg_modify_passwords_file(passfile, domain, user, password);
  1415. EndDialog(hDlg, IDOK);
  1416. }
  1417. } else if ((ctrlId >= (ID_CONTROLS + ID_FILE_BUTTONS_DELTA * 2))
  1418. && (ctrlId < (ID_CONTROLS + ID_FILE_BUTTONS_DELTA * 3))) {
  1419. /* Remove user */
  1420. GetWindowText(GetDlgItem(hDlg, ctrlId - ID_FILE_BUTTONS_DELTA * 2),
  1421. user,
  1422. sizeof(user));
  1423. GetWindowText(GetDlgItem(hDlg, ctrlId - ID_FILE_BUTTONS_DELTA),
  1424. domain,
  1425. sizeof(domain));
  1426. mg_modify_passwords_file(passfile, domain, user, NULL);
  1427. EndDialog(hDlg, IDOK);
  1428. }
  1429. break;
  1430. case WM_INITDIALOG:
  1431. passfile = (const char *)lP;
  1432. SendMessage(hDlg, WM_SETICON, (WPARAM)ICON_SMALL, (LPARAM)hIcon);
  1433. SendMessage(hDlg, WM_SETICON, (WPARAM)ICON_BIG, (LPARAM)hIcon);
  1434. SetWindowText(hDlg, passfile);
  1435. SetFocus(GetDlgItem(hDlg, ID_ADD_USER_NAME));
  1436. break;
  1437. default:
  1438. break;
  1439. }
  1440. return FALSE;
  1441. }
  1442. static void
  1443. add_control(unsigned char **mem,
  1444. DLGTEMPLATE *dia,
  1445. WORD type,
  1446. WORD id,
  1447. DWORD style,
  1448. short x,
  1449. short y,
  1450. short cx,
  1451. short cy,
  1452. const char *caption)
  1453. {
  1454. DLGITEMTEMPLATE *tp;
  1455. LPWORD p;
  1456. dia->cdit++;
  1457. *mem = align(*mem, 3);
  1458. tp = (DLGITEMTEMPLATE *)*mem;
  1459. tp->id = id;
  1460. tp->style = style;
  1461. tp->dwExtendedStyle = 0;
  1462. tp->x = x;
  1463. tp->y = y;
  1464. tp->cx = cx;
  1465. tp->cy = cy;
  1466. p = align(*mem + sizeof(*tp), 1);
  1467. *p++ = 0xffff;
  1468. *p++ = type;
  1469. while (*caption != '\0') {
  1470. *p++ = (WCHAR)*caption++;
  1471. }
  1472. *p++ = 0;
  1473. p = align(p, 1);
  1474. *p++ = 0;
  1475. *mem = (unsigned char *)p;
  1476. }
  1477. static void
  1478. show_settings_dialog()
  1479. {
  1480. #define HEIGHT (15)
  1481. #define WIDTH (460)
  1482. #define LABEL_WIDTH (90)
  1483. unsigned char mem[8192], *p;
  1484. const struct mg_option *options;
  1485. DWORD style;
  1486. DLGTEMPLATE *dia = (DLGTEMPLATE *)mem;
  1487. WORD i, cl, nelems = 0;
  1488. short width, x, y;
  1489. static struct {
  1490. DLGTEMPLATE template; /* 18 bytes */
  1491. WORD menu, class;
  1492. wchar_t caption[1];
  1493. WORD fontsiz;
  1494. wchar_t fontface[7];
  1495. } dialog_header = {{WS_CAPTION | WS_POPUP | WS_SYSMENU | WS_VISIBLE
  1496. | DS_SETFONT | WS_DLGFRAME,
  1497. WS_EX_TOOLWINDOW,
  1498. 0,
  1499. 200,
  1500. 200,
  1501. WIDTH,
  1502. 0},
  1503. 0,
  1504. 0,
  1505. L"",
  1506. 8,
  1507. L"Tahoma"};
  1508. if (guard == 0) {
  1509. guard++;
  1510. } else {
  1511. return;
  1512. }
  1513. (void)memset(mem, 0, sizeof(mem));
  1514. (void)memcpy(mem, &dialog_header, sizeof(dialog_header));
  1515. p = mem + sizeof(dialog_header);
  1516. options = mg_get_valid_options();
  1517. for (i = 0; options[i].name != NULL; i++) {
  1518. style = WS_CHILD | WS_VISIBLE | WS_TABSTOP;
  1519. x = 10 + (WIDTH / 2) * (nelems % 2);
  1520. y = (nelems / 2 + 1) * HEIGHT + 5;
  1521. width = WIDTH / 2 - 20 - LABEL_WIDTH;
  1522. if (options[i].type == CONFIG_TYPE_NUMBER) {
  1523. style |= ES_NUMBER;
  1524. cl = 0x81;
  1525. style |= WS_BORDER | ES_AUTOHSCROLL;
  1526. } else if (options[i].type == CONFIG_TYPE_BOOLEAN) {
  1527. cl = 0x80;
  1528. style |= BS_AUTOCHECKBOX;
  1529. } else if ((options[i].type == CONFIG_TYPE_FILE)
  1530. || (options[i].type == CONFIG_TYPE_DIRECTORY)) {
  1531. style |= WS_BORDER | ES_AUTOHSCROLL;
  1532. width -= 20;
  1533. cl = 0x81;
  1534. add_control(&p,
  1535. dia,
  1536. 0x80,
  1537. ID_CONTROLS + i + ID_FILE_BUTTONS_DELTA,
  1538. WS_VISIBLE | WS_CHILD | BS_PUSHBUTTON,
  1539. x + width + LABEL_WIDTH + 5,
  1540. y,
  1541. 15,
  1542. 12,
  1543. "...");
  1544. } else {
  1545. cl = 0x81;
  1546. style |= WS_BORDER | ES_AUTOHSCROLL;
  1547. }
  1548. add_control(&p,
  1549. dia,
  1550. 0x82,
  1551. ID_STATIC,
  1552. WS_VISIBLE | WS_CHILD,
  1553. x,
  1554. y,
  1555. LABEL_WIDTH,
  1556. HEIGHT,
  1557. options[i].name);
  1558. add_control(&p,
  1559. dia,
  1560. cl,
  1561. ID_CONTROLS + i,
  1562. style,
  1563. x + LABEL_WIDTH,
  1564. y,
  1565. width,
  1566. 12,
  1567. "");
  1568. nelems++;
  1569. assert(((intptr_t)p - (intptr_t)mem) < (intptr_t)sizeof(mem));
  1570. }
  1571. y = (((nelems + 1) / 2 + 1) * HEIGHT + 5);
  1572. add_control(&p,
  1573. dia,
  1574. 0x80,
  1575. ID_GROUP,
  1576. WS_CHILD | WS_VISIBLE | BS_GROUPBOX,
  1577. 5,
  1578. 5,
  1579. WIDTH - 10,
  1580. y,
  1581. " Settings ");
  1582. y += 10;
  1583. add_control(&p,
  1584. dia,
  1585. 0x80,
  1586. ID_SAVE,
  1587. WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON | WS_TABSTOP,
  1588. WIDTH - 70,
  1589. y,
  1590. 65,
  1591. 12,
  1592. "Save Settings");
  1593. add_control(&p,
  1594. dia,
  1595. 0x80,
  1596. ID_RESET_DEFAULTS,
  1597. WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON | WS_TABSTOP,
  1598. WIDTH - 140,
  1599. y,
  1600. 65,
  1601. 12,
  1602. "Reset to defaults");
  1603. add_control(&p,
  1604. dia,
  1605. 0x80,
  1606. ID_RESET_FILE,
  1607. WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON | WS_TABSTOP,
  1608. WIDTH - 210,
  1609. y,
  1610. 65,
  1611. 12,
  1612. "Reload from file");
  1613. add_control(&p,
  1614. dia,
  1615. 0x80,
  1616. ID_RESET_ACTIVE,
  1617. WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON | WS_TABSTOP,
  1618. WIDTH - 280,
  1619. y,
  1620. 65,
  1621. 12,
  1622. "Reload active");
  1623. add_control(&p,
  1624. dia,
  1625. 0x82,
  1626. ID_STATIC,
  1627. WS_CHILD | WS_VISIBLE | WS_DISABLED,
  1628. 5,
  1629. y,
  1630. 100,
  1631. 12,
  1632. g_server_base_name);
  1633. assert(((intptr_t)p - (intptr_t)mem) < (intptr_t)sizeof(mem));
  1634. dia->cy = ((nelems + 1) / 2 + 1) * HEIGHT + 30;
  1635. DialogBoxIndirectParam(NULL, dia, NULL, SettingsDlgProc, (LPARAM)NULL);
  1636. guard--;
  1637. #undef HEIGHT
  1638. #undef WIDTH
  1639. #undef LABEL_WIDTH
  1640. }
  1641. static void
  1642. change_password_file()
  1643. {
  1644. #define HEIGHT (15)
  1645. #define WIDTH (320)
  1646. #define LABEL_WIDTH (90)
  1647. OPENFILENAME of;
  1648. char path[PATH_MAX] = PASSWORDS_FILE_NAME;
  1649. char strbuf[256], u[256], d[256];
  1650. HWND hDlg = NULL;
  1651. FILE *f;
  1652. short y, nelems;
  1653. unsigned char mem[4096], *p;
  1654. DLGTEMPLATE *dia = (DLGTEMPLATE *)mem;
  1655. const char *domain = mg_get_option(g_ctx, "authentication_domain");
  1656. static struct {
  1657. DLGTEMPLATE template; /* 18 bytes */
  1658. WORD menu, class;
  1659. wchar_t caption[1];
  1660. WORD fontsiz;
  1661. wchar_t fontface[7];
  1662. } dialog_header = {{WS_CAPTION | WS_POPUP | WS_SYSMENU | WS_VISIBLE
  1663. | DS_SETFONT | WS_DLGFRAME,
  1664. WS_EX_TOOLWINDOW,
  1665. 0,
  1666. 200,
  1667. 200,
  1668. WIDTH,
  1669. 0},
  1670. 0,
  1671. 0,
  1672. L"",
  1673. 8,
  1674. L"Tahoma"};
  1675. if (guard == 0) {
  1676. guard++;
  1677. } else {
  1678. return;
  1679. }
  1680. memset(&of, 0, sizeof(of));
  1681. of.lStructSize = sizeof(of);
  1682. of.hwndOwner = (HWND)hDlg;
  1683. of.lpstrFile = path;
  1684. of.nMaxFile = sizeof(path);
  1685. of.lpstrInitialDir = mg_get_option(g_ctx, "document_root");
  1686. of.Flags = OFN_CREATEPROMPT | OFN_NOCHANGEDIR | OFN_HIDEREADONLY;
  1687. if (IDOK != GetSaveFileName(&of)) {
  1688. guard--;
  1689. return;
  1690. }
  1691. f = fopen(path, "a+");
  1692. if (f) {
  1693. fclose(f);
  1694. } else {
  1695. MessageBox(NULL, path, "Can not open file", MB_ICONERROR);
  1696. guard--;
  1697. return;
  1698. }
  1699. do {
  1700. (void)memset(mem, 0, sizeof(mem));
  1701. (void)memcpy(mem, &dialog_header, sizeof(dialog_header));
  1702. p = mem + sizeof(dialog_header);
  1703. f = fopen(path, "r+");
  1704. if (!f) {
  1705. MessageBox(NULL, path, "Can not open file", MB_ICONERROR);
  1706. guard--;
  1707. return;
  1708. }
  1709. nelems = 0;
  1710. while (fgets(strbuf, sizeof(strbuf), f)) {
  1711. if (sscanf(strbuf, "%255[^:]:%255[^:]:%*s", u, d) != 2) {
  1712. continue;
  1713. }
  1714. u[255] = 0;
  1715. d[255] = 0;
  1716. y = (nelems + 1) * HEIGHT + 5;
  1717. add_control(&p,
  1718. dia,
  1719. 0x80,
  1720. ID_CONTROLS + nelems + ID_FILE_BUTTONS_DELTA * 3,
  1721. WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON | WS_TABSTOP,
  1722. 10,
  1723. y,
  1724. 65,
  1725. 12,
  1726. "Modify password");
  1727. add_control(&p,
  1728. dia,
  1729. 0x80,
  1730. ID_CONTROLS + nelems + ID_FILE_BUTTONS_DELTA * 2,
  1731. WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON | WS_TABSTOP,
  1732. 80,
  1733. y,
  1734. 55,
  1735. 12,
  1736. "Remove user");
  1737. add_control(&p,
  1738. dia,
  1739. 0x81,
  1740. ID_CONTROLS + nelems + ID_FILE_BUTTONS_DELTA,
  1741. WS_CHILD | WS_VISIBLE | WS_BORDER | ES_AUTOHSCROLL
  1742. | WS_DISABLED,
  1743. 245,
  1744. y,
  1745. 60,
  1746. 12,
  1747. d);
  1748. add_control(&p,
  1749. dia,
  1750. 0x81,
  1751. ID_CONTROLS + nelems,
  1752. WS_CHILD | WS_VISIBLE | WS_BORDER | ES_AUTOHSCROLL
  1753. | WS_DISABLED,
  1754. 140,
  1755. y,
  1756. 100,
  1757. 12,
  1758. u);
  1759. nelems++;
  1760. assert(((intptr_t)p - (intptr_t)mem) < (intptr_t)sizeof(mem));
  1761. }
  1762. fclose(f);
  1763. y = (nelems + 1) * HEIGHT + 10;
  1764. add_control(&p,
  1765. dia,
  1766. 0x80,
  1767. ID_ADD_USER,
  1768. WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON | WS_TABSTOP,
  1769. 80,
  1770. y,
  1771. 55,
  1772. 12,
  1773. "Add user");
  1774. add_control(&p,
  1775. dia,
  1776. 0x81,
  1777. ID_ADD_USER_NAME,
  1778. WS_CHILD | WS_VISIBLE | WS_BORDER | ES_AUTOHSCROLL
  1779. | WS_TABSTOP,
  1780. 140,
  1781. y,
  1782. 100,
  1783. 12,
  1784. "");
  1785. add_control(&p,
  1786. dia,
  1787. 0x81,
  1788. ID_ADD_USER_REALM,
  1789. WS_CHILD | WS_VISIBLE | WS_BORDER | ES_AUTOHSCROLL
  1790. | WS_TABSTOP,
  1791. 245,
  1792. y,
  1793. 60,
  1794. 12,
  1795. domain);
  1796. y = (nelems + 2) * HEIGHT + 10;
  1797. add_control(&p,
  1798. dia,
  1799. 0x80,
  1800. ID_GROUP,
  1801. WS_CHILD | WS_VISIBLE | BS_GROUPBOX,
  1802. 5,
  1803. 5,
  1804. WIDTH - 10,
  1805. y,
  1806. " Users ");
  1807. y += HEIGHT;
  1808. add_control(&p,
  1809. dia,
  1810. 0x82,
  1811. ID_STATIC,
  1812. WS_CHILD | WS_VISIBLE | WS_DISABLED,
  1813. 5,
  1814. y,
  1815. 100,
  1816. 12,
  1817. g_server_base_name);
  1818. assert(((intptr_t)p - (intptr_t)mem) < (intptr_t)sizeof(mem));
  1819. dia->cy = y + 20;
  1820. } while ((IDOK == DialogBoxIndirectParam(
  1821. NULL, dia, NULL, PasswordDlgProc, (LPARAM)path))
  1822. && (!g_exit_flag));
  1823. guard--;
  1824. #undef HEIGHT
  1825. #undef WIDTH
  1826. #undef LABEL_WIDTH
  1827. }
  1828. static int
  1829. manage_service(int action)
  1830. {
  1831. const char *service_name = g_server_name;
  1832. SC_HANDLE hSCM = NULL, hService = NULL;
  1833. SERVICE_DESCRIPTION descr;
  1834. char path[PATH_MAX + 20] = ""; /* Path to executable plus magic argument */
  1835. int success = 1;
  1836. descr.lpDescription = g_server_name;
  1837. if ((hSCM = OpenSCManager(NULL,
  1838. NULL,
  1839. action == ID_INSTALL_SERVICE ? GENERIC_WRITE
  1840. : GENERIC_READ))
  1841. == NULL) {
  1842. success = 0;
  1843. show_error();
  1844. } else if (action == ID_INSTALL_SERVICE) {
  1845. path[sizeof(path) - 1] = 0;
  1846. GetModuleFileName(NULL, path, sizeof(path) - 1);
  1847. strncat(path, " ", sizeof(path) - 1);
  1848. strncat(path, service_magic_argument, sizeof(path) - 1);
  1849. hService = CreateService(hSCM,
  1850. service_name,
  1851. service_name,
  1852. SERVICE_ALL_ACCESS,
  1853. SERVICE_WIN32_OWN_PROCESS,
  1854. SERVICE_AUTO_START,
  1855. SERVICE_ERROR_NORMAL,
  1856. path,
  1857. NULL,
  1858. NULL,
  1859. NULL,
  1860. NULL,
  1861. NULL);
  1862. if (hService) {
  1863. ChangeServiceConfig2(hService, SERVICE_CONFIG_DESCRIPTION, &descr);
  1864. } else {
  1865. show_error();
  1866. }
  1867. } else if (action == ID_REMOVE_SERVICE) {
  1868. if ((hService = OpenService(hSCM, service_name, DELETE)) == NULL
  1869. || !DeleteService(hService)) {
  1870. show_error();
  1871. }
  1872. } else if ((hService =
  1873. OpenService(hSCM, service_name, SERVICE_QUERY_STATUS))
  1874. == NULL) {
  1875. success = 0;
  1876. }
  1877. if (hService)
  1878. CloseServiceHandle(hService);
  1879. if (hSCM)
  1880. CloseServiceHandle(hSCM);
  1881. return success;
  1882. }
  1883. static LRESULT CALLBACK
  1884. WindowProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
  1885. {
  1886. static SERVICE_TABLE_ENTRY service_table[2];
  1887. int service_installed;
  1888. char buf[200], *service_argv[2];
  1889. POINT pt;
  1890. HMENU hMenu;
  1891. static UINT s_uTaskbarRestart; /* for taskbar creation */
  1892. service_argv[0] = __argv[0];
  1893. service_argv[1] = NULL;
  1894. memset(service_table, 0, sizeof(service_table));
  1895. service_table[0].lpServiceName = g_server_name;
  1896. service_table[0].lpServiceProc = (LPSERVICE_MAIN_FUNCTION)ServiceMain;
  1897. switch (msg) {
  1898. case WM_CREATE:
  1899. if (__argv[1] != NULL && !strcmp(__argv[1], service_magic_argument)) {
  1900. start_civetweb(1, service_argv);
  1901. StartServiceCtrlDispatcher(service_table);
  1902. exit(EXIT_SUCCESS);
  1903. } else {
  1904. start_civetweb(__argc, __argv);
  1905. s_uTaskbarRestart = RegisterWindowMessage(TEXT("TaskbarCreated"));
  1906. }
  1907. break;
  1908. case WM_COMMAND:
  1909. switch (LOWORD(wParam)) {
  1910. case ID_QUIT:
  1911. stop_civetweb();
  1912. Shell_NotifyIcon(NIM_DELETE, &TrayIcon);
  1913. g_exit_flag = 1;
  1914. PostQuitMessage(0);
  1915. return 0;
  1916. case ID_SETTINGS:
  1917. show_settings_dialog();
  1918. break;
  1919. case ID_PASSWORD:
  1920. change_password_file();
  1921. break;
  1922. case ID_INSTALL_SERVICE:
  1923. case ID_REMOVE_SERVICE:
  1924. manage_service(LOWORD(wParam));
  1925. break;
  1926. case ID_CONNECT:
  1927. fprintf(stdout, "[%s]\n", get_url_to_first_open_port(g_ctx));
  1928. ShellExecute(NULL,
  1929. "open",
  1930. get_url_to_first_open_port(g_ctx),
  1931. NULL,
  1932. NULL,
  1933. SW_SHOW);
  1934. break;
  1935. }
  1936. break;
  1937. case WM_USER:
  1938. switch (lParam) {
  1939. case WM_RBUTTONUP:
  1940. case WM_LBUTTONUP:
  1941. case WM_LBUTTONDBLCLK:
  1942. hMenu = CreatePopupMenu();
  1943. AppendMenu(hMenu,
  1944. MF_STRING | MF_GRAYED,
  1945. ID_SEPARATOR,
  1946. g_server_name);
  1947. AppendMenu(hMenu, MF_SEPARATOR, ID_SEPARATOR, "");
  1948. service_installed = manage_service(0);
  1949. snprintf(buf,
  1950. sizeof(buf) - 1,
  1951. "NT service: %s installed",
  1952. service_installed ? "" : "not");
  1953. buf[sizeof(buf) - 1] = 0;
  1954. AppendMenu(hMenu, MF_STRING | MF_GRAYED, ID_SEPARATOR, buf);
  1955. AppendMenu(hMenu,
  1956. MF_STRING | (service_installed ? MF_GRAYED : 0),
  1957. ID_INSTALL_SERVICE,
  1958. "Install service");
  1959. AppendMenu(hMenu,
  1960. MF_STRING | (!service_installed ? MF_GRAYED : 0),
  1961. ID_REMOVE_SERVICE,
  1962. "Deinstall service");
  1963. AppendMenu(hMenu, MF_SEPARATOR, ID_SEPARATOR, "");
  1964. AppendMenu(hMenu, MF_STRING, ID_CONNECT, "Start browser");
  1965. AppendMenu(hMenu, MF_STRING, ID_SETTINGS, "Edit settings");
  1966. AppendMenu(hMenu, MF_STRING, ID_PASSWORD, "Modify password file");
  1967. AppendMenu(hMenu, MF_SEPARATOR, ID_SEPARATOR, "");
  1968. AppendMenu(hMenu, MF_STRING, ID_QUIT, "Exit");
  1969. GetCursorPos(&pt);
  1970. SetForegroundWindow(hWnd);
  1971. TrackPopupMenu(hMenu, 0, pt.x, pt.y, 0, hWnd, NULL);
  1972. PostMessage(hWnd, WM_NULL, 0, 0);
  1973. DestroyMenu(hMenu);
  1974. break;
  1975. }
  1976. break;
  1977. case WM_CLOSE:
  1978. stop_civetweb();
  1979. Shell_NotifyIcon(NIM_DELETE, &TrayIcon);
  1980. g_exit_flag = 1;
  1981. PostQuitMessage(0);
  1982. return 0; /* We've just sent our own quit message, with proper hwnd. */
  1983. default:
  1984. if (msg == s_uTaskbarRestart)
  1985. Shell_NotifyIcon(NIM_ADD, &TrayIcon);
  1986. }
  1987. return DefWindowProc(hWnd, msg, wParam, lParam);
  1988. }
  1989. static int
  1990. MakeConsole(void)
  1991. {
  1992. DWORD err;
  1993. int ok = (GetConsoleWindow() != NULL);
  1994. if (!ok) {
  1995. if (!AttachConsole(ATTACH_PARENT_PROCESS)) {
  1996. FreeConsole();
  1997. if (!AllocConsole()) {
  1998. err = GetLastError();
  1999. if (err == ERROR_ACCESS_DENIED) {
  2000. MessageBox(NULL,
  2001. "Insufficient rights to create a console window",
  2002. "Error",
  2003. MB_ICONERROR);
  2004. }
  2005. }
  2006. AttachConsole(GetCurrentProcessId());
  2007. }
  2008. ok = (GetConsoleWindow() != NULL);
  2009. if (ok) {
  2010. freopen("CONIN$", "r", stdin);
  2011. freopen("CONOUT$", "w", stdout);
  2012. freopen("CONOUT$", "w", stderr);
  2013. }
  2014. }
  2015. if (ok) {
  2016. SetConsoleTitle(g_server_name);
  2017. }
  2018. return ok;
  2019. }
  2020. int WINAPI
  2021. WinMain(HINSTANCE hInst, HINSTANCE hPrev, LPSTR cmdline, int show)
  2022. {
  2023. WNDCLASS cls;
  2024. HWND hWnd;
  2025. MSG msg;
  2026. int i;
  2027. int dataLen = 4;
  2028. char data[256] = {0};
  2029. char masked_data[256] = {0};
  2030. uint32_t masking_key = 0x01020304;
  2031. for (i = 0; i < dataLen - 3; i += 4) {
  2032. *(uint32_t *)(void *)(masked_data + i) =
  2033. *(uint32_t *)(void *)(data + i) ^ masking_key;
  2034. }
  2035. if (i != dataLen) {
  2036. /* convert 1-3 remaining bytes */
  2037. i -= 4;
  2038. while (i < dataLen) {
  2039. *(uint8_t *)(void *)(masked_data + i) =
  2040. *(uint8_t *)(void *)(data + i)
  2041. ^ *(((uint8_t *)&masking_key) + (i % 4));
  2042. i++;
  2043. }
  2044. }
  2045. #if 0
  2046. /* http://lomont.org/Math/Papers/2008/Lomont_PRNG_2008.pdf */
  2047. /* initialize state to random bits
  2048. */
  2049. static unsigned long state[16];
  2050. /* init should also reset this to 0 */
  2051. static unsigned int index = 0;
  2052. /* return 32 bit random number
  2053. */
  2054. unsigned long WELLRN G512(void)
  2055. {
  2056. unsigned long a, b, c, d;
  2057. a = state[index];
  2058. c = state[(index + 13) & 15];
  2059. b = a ^ c ^ (a << 16) ^ (c << 15);
  2060. c = state[(index + 9) & 15];
  2061. c ^= (c >> 11);
  2062. a = state[index] = b ^ c;
  2063. d = a ^ ((a << 5) & 0xDA442D24 UL);
  2064. index = (index + 15) & 15;
  2065. a = state[index];
  2066. state[index] = a ^ b ^ d ^ (a << 2) ^ (b << 18) ^ (c << 28);
  2067. return state[index];
  2068. }
  2069. uint32_t x, y, z, w;
  2070. uint32_t xorshift128(void)
  2071. {
  2072. uint32_t t = x ^ (x << 11);
  2073. x = y;
  2074. y = z;
  2075. z = w;
  2076. return w = w ^ (w >> 19) ^ t ^ (t >> 8);
  2077. }
  2078. static uint64_t lfsr = 1;
  2079. static uint64_t lcg = 0;
  2080. uint64_t r = 0;
  2081. do {
  2082. lfsr = (lfsr >> 1)
  2083. | ((((lfsr >> 0) ^ (lfsr >> 1) ^ (lfsr >> 3) ^ (lfsr >> 4)) & 1)
  2084. << 63);
  2085. lcg = lcg * 6364136223846793005 + 1442695040888963407;
  2086. ++r;
  2087. } while (lcg != 0);
  2088. fprintf(stdout, "lfsr = %I64u, lcg = %i64u, r = %i64u\n", lfsr, lcg, r);
  2089. #endif
  2090. (void)hInst;
  2091. (void)hPrev;
  2092. (void)cmdline;
  2093. (void)show;
  2094. init_server_name((int)__argc, (const char **)__argv);
  2095. memset(&cls, 0, sizeof(cls));
  2096. cls.lpfnWndProc = (WNDPROC)WindowProc;
  2097. cls.hIcon = LoadIcon(NULL, IDI_APPLICATION);
  2098. cls.lpszClassName = g_server_base_name;
  2099. RegisterClass(&cls);
  2100. hWnd = CreateWindow(cls.lpszClassName,
  2101. g_server_name,
  2102. WS_OVERLAPPEDWINDOW,
  2103. 0,
  2104. 0,
  2105. 0,
  2106. 0,
  2107. NULL,
  2108. NULL,
  2109. NULL,
  2110. NULL);
  2111. ShowWindow(hWnd, SW_HIDE);
  2112. if (g_icon_name) {
  2113. hIcon =
  2114. LoadImage(NULL, g_icon_name, IMAGE_ICON, 16, 16, LR_LOADFROMFILE);
  2115. } else {
  2116. hIcon = LoadImage(GetModuleHandle(NULL),
  2117. MAKEINTRESOURCE(ID_ICON),
  2118. IMAGE_ICON,
  2119. 16,
  2120. 16,
  2121. 0);
  2122. }
  2123. TrayIcon.cbSize = sizeof(TrayIcon);
  2124. TrayIcon.uID = ID_ICON;
  2125. TrayIcon.uFlags = NIF_ICON | NIF_MESSAGE | NIF_TIP;
  2126. TrayIcon.hIcon = hIcon;
  2127. TrayIcon.hWnd = hWnd;
  2128. snprintf(TrayIcon.szTip, sizeof(TrayIcon.szTip), "%s", g_server_name);
  2129. TrayIcon.uCallbackMessage = WM_USER;
  2130. Shell_NotifyIcon(NIM_ADD, &TrayIcon);
  2131. while (GetMessage(&msg, hWnd, 0, 0) > 0) {
  2132. TranslateMessage(&msg);
  2133. DispatchMessage(&msg);
  2134. }
  2135. /* Return the WM_QUIT value. */
  2136. return (int)msg.wParam;
  2137. }
  2138. int
  2139. main(int argc, char *argv[])
  2140. {
  2141. (void)argc;
  2142. (void)argv;
  2143. return WinMain(0, 0, 0, 0);
  2144. }
  2145. #elif defined(USE_COCOA)
  2146. #import <Cocoa/Cocoa.h>
  2147. @interface Civetweb : NSObject <NSApplicationDelegate>
  2148. - (void)openBrowser;
  2149. - (void)shutDown;
  2150. @end
  2151. @implementation Civetweb
  2152. - (void)openBrowser
  2153. {
  2154. [[NSWorkspace sharedWorkspace]
  2155. openURL:[NSURL URLWithString:[NSString stringWithUTF8String:
  2156. get_url_to_first_open_port(
  2157. g_ctx)]]];
  2158. }
  2159. - (void)editConfig
  2160. {
  2161. create_config_file(g_ctx, g_config_file);
  2162. [[NSWorkspace sharedWorkspace]
  2163. openFile:[NSString stringWithUTF8String:g_config_file]
  2164. withApplication:@"TextEdit"];
  2165. }
  2166. - (void)shutDown
  2167. {
  2168. [NSApp terminate:nil];
  2169. }
  2170. @end
  2171. int
  2172. main(int argc, char *argv[])
  2173. {
  2174. init_server_name(argc, (const char **)argv);
  2175. start_civetweb(argc, argv);
  2176. [NSAutoreleasePool new];
  2177. [NSApplication sharedApplication];
  2178. /* Add delegate to process menu item actions */
  2179. Civetweb *myDelegate = [[Civetweb alloc] autorelease];
  2180. [NSApp setDelegate:myDelegate];
  2181. /* Run this app as agent */
  2182. ProcessSerialNumber psn = {0, kCurrentProcess};
  2183. TransformProcessType(&psn, kProcessTransformToBackgroundApplication);
  2184. SetFrontProcess(&psn);
  2185. /* Add status bar menu */
  2186. id menu = [[NSMenu new] autorelease];
  2187. /* Add version menu item */
  2188. [menu
  2189. addItem:
  2190. [[[NSMenuItem alloc]
  2191. /*initWithTitle:[NSString stringWithFormat:@"%s", server_name]*/
  2192. initWithTitle:[NSString stringWithUTF8String:g_server_name]
  2193. action:@selector(noexist)
  2194. keyEquivalent:@""] autorelease]];
  2195. /* Add configuration menu item */
  2196. [menu addItem:[[[NSMenuItem alloc] initWithTitle:@"Edit configuration"
  2197. action:@selector(editConfig)
  2198. keyEquivalent:@""] autorelease]];
  2199. /* Add connect menu item */
  2200. [menu
  2201. addItem:[[[NSMenuItem alloc] initWithTitle:@"Open web root in a browser"
  2202. action:@selector(openBrowser)
  2203. keyEquivalent:@""] autorelease]];
  2204. /* Separator */
  2205. [menu addItem:[NSMenuItem separatorItem]];
  2206. /* Add quit menu item */
  2207. [menu addItem:[[[NSMenuItem alloc] initWithTitle:@"Quit"
  2208. action:@selector(shutDown)
  2209. keyEquivalent:@"q"] autorelease]];
  2210. /* Attach menu to the status bar */
  2211. id item = [[[NSStatusBar systemStatusBar]
  2212. statusItemWithLength:NSVariableStatusItemLength] retain];
  2213. [item setHighlightMode:YES];
  2214. [item setImage:[NSImage imageNamed:@"civetweb_22x22.png"]];
  2215. [item setMenu:menu];
  2216. /* Run the app */
  2217. [NSApp activateIgnoringOtherApps:YES];
  2218. [NSApp run];
  2219. stop_civetweb(g_ctx);
  2220. return EXIT_SUCCESS;
  2221. }
  2222. #else
  2223. int
  2224. main(int argc, char *argv[])
  2225. {
  2226. init_server_name(argc, (const char **)argv);
  2227. start_civetweb(argc, argv);
  2228. fprintf(stdout,
  2229. "%s started on port(s) %s with web root [%s]\n",
  2230. g_server_name,
  2231. mg_get_option(g_ctx, "listening_ports"),
  2232. mg_get_option(g_ctx, "document_root"));
  2233. while (g_exit_flag == 0) {
  2234. sleep(1);
  2235. }
  2236. fprintf(stdout,
  2237. "Exiting on signal %d, waiting for all threads to finish...",
  2238. g_exit_flag);
  2239. fflush(stdout);
  2240. stop_civetweb();
  2241. fprintf(stdout, "%s", " done.\n");
  2242. return EXIT_SUCCESS;
  2243. }
  2244. #endif /* _WIN32 */