main.c 65 KB

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