main.c 65 KB

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