main.c 56 KB

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