conf.c 63 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433
  1. /*
  2. * ngIRCd -- The Next Generation IRC Daemon
  3. * Copyright (c)2001-2014 Alexander Barton (alex@barton.de) and Contributors.
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. * Please read the file COPYING, README and AUTHORS for more information.
  10. */
  11. #include "portab.h"
  12. /**
  13. * @file
  14. * Configuration management (reading, parsing & validation)
  15. */
  16. #include <assert.h>
  17. #include <errno.h>
  18. #ifdef PROTOTYPES
  19. # include <stdarg.h>
  20. #else
  21. # include <varargs.h>
  22. #endif
  23. #include <stdio.h>
  24. #include <stdlib.h>
  25. #include <string.h>
  26. #include <strings.h>
  27. #include <time.h>
  28. #include <unistd.h>
  29. #include <pwd.h>
  30. #include <grp.h>
  31. #include <sys/types.h>
  32. #include <dirent.h>
  33. #include "ngircd.h"
  34. #include "conn.h"
  35. #include "channel.h"
  36. #include "log.h"
  37. #include "match.h"
  38. #include "conf.h"
  39. static bool Use_Log = true, Using_MotdFile = true;
  40. static CONF_SERVER New_Server;
  41. static int New_Server_Idx;
  42. static char Conf_MotdFile[FNAME_LEN];
  43. static char Conf_HelpFile[FNAME_LEN];
  44. static char Conf_IncludeDir[FNAME_LEN];
  45. static void Set_Defaults PARAMS(( bool InitServers ));
  46. static bool Read_Config PARAMS(( bool TestOnly, bool IsStarting ));
  47. static void Read_Config_File PARAMS(( const char *File, FILE *fd ));
  48. static bool Validate_Config PARAMS(( bool TestOnly, bool Rehash ));
  49. static void Handle_GLOBAL PARAMS((const char *File, int Line,
  50. char *Var, char *Arg ));
  51. static void Handle_LIMITS PARAMS((const char *File, int Line,
  52. char *Var, char *Arg ));
  53. static void Handle_OPTIONS PARAMS((const char *File, int Line,
  54. char *Var, char *Arg ));
  55. static void Handle_OPERATOR PARAMS((const char *File, int Line,
  56. char *Var, char *Arg ));
  57. static void Handle_SERVER PARAMS((const char *File, int Line,
  58. char *Var, char *Arg ));
  59. static void Handle_CHANNEL PARAMS((const char *File, int Line,
  60. char *Var, char *Arg ));
  61. static void Config_Error PARAMS((const int Level, const char *Format, ...));
  62. static void Config_Error_NaN PARAMS((const char *File, const int LINE,
  63. const char *Value));
  64. static void Config_Error_Section PARAMS((const char *File, const int Line,
  65. const char *Item, const char *Section));
  66. static void Config_Error_TooLong PARAMS((const char *File, const int LINE,
  67. const char *Value));
  68. static void Init_Server_Struct PARAMS(( CONF_SERVER *Server ));
  69. #ifdef WANT_IPV6
  70. #define DEFAULT_LISTEN_ADDRSTR "::,0.0.0.0"
  71. #else
  72. #define DEFAULT_LISTEN_ADDRSTR "0.0.0.0"
  73. #endif
  74. #ifdef HAVE_LIBSSL
  75. #define DEFAULT_CIPHERS "HIGH:!aNULL:@STRENGTH:!SSLv3"
  76. #endif
  77. #ifdef HAVE_LIBGNUTLS
  78. #define DEFAULT_CIPHERS "SECURE128:-VERS-SSL3.0"
  79. #endif
  80. #ifdef SSL_SUPPORT
  81. static void Handle_SSL PARAMS((const char *File, int Line, char *Var, char *Ark));
  82. struct SSLOptions Conf_SSLOptions;
  83. /**
  84. * Initialize SSL configuration.
  85. */
  86. static void
  87. ConfSSL_Init(void)
  88. {
  89. free(Conf_SSLOptions.KeyFile);
  90. Conf_SSLOptions.KeyFile = NULL;
  91. free(Conf_SSLOptions.CertFile);
  92. Conf_SSLOptions.CertFile = NULL;
  93. free(Conf_SSLOptions.DHFile);
  94. Conf_SSLOptions.DHFile = NULL;
  95. array_free_wipe(&Conf_SSLOptions.KeyFilePassword);
  96. array_free(&Conf_SSLOptions.ListenPorts);
  97. free(Conf_SSLOptions.CipherList);
  98. Conf_SSLOptions.CipherList = NULL;
  99. }
  100. /**
  101. * Check if the current configuration uses/requires SSL.
  102. *
  103. * @returns true if SSL is used and should be initialized.
  104. */
  105. GLOBAL bool
  106. Conf_SSLInUse(void)
  107. {
  108. int i;
  109. /* SSL listen ports configured? */
  110. if (array_bytes(&Conf_SSLOptions.ListenPorts))
  111. return true;
  112. for (i = 0; i < MAX_SERVERS; i++) {
  113. if (Conf_Server[i].port > 0
  114. && Conf_Server[i].SSLConnect)
  115. return true;
  116. }
  117. return false;
  118. }
  119. /**
  120. * Make sure that a configured file is readable.
  121. *
  122. * Currently, this function is only used for SSL-related options ...
  123. *
  124. * @param Var Configuration variable
  125. * @param Filename Configured filename
  126. */
  127. static void
  128. CheckFileReadable(const char *Var, const char *Filename)
  129. {
  130. FILE *fp;
  131. if (!Filename)
  132. return;
  133. fp = fopen(Filename, "r");
  134. if (fp)
  135. fclose(fp);
  136. else
  137. Config_Error(LOG_ERR, "Can't read \"%s\" (\"%s\"): %s",
  138. Filename, Var, strerror(errno));
  139. }
  140. #endif
  141. /**
  142. * Duplicate string and warn on errors.
  143. *
  144. * @returns Pointer to string on success, NULL otherwise.
  145. */
  146. static char *
  147. strdup_warn(const char *str)
  148. {
  149. char *ptr = strdup(str);
  150. if (!ptr)
  151. Config_Error(LOG_ERR,
  152. "Could not allocate memory for string: %s", str);
  153. return ptr;
  154. }
  155. /**
  156. * Output a comma separated list of ports (integer values).
  157. */
  158. static void
  159. ports_puts(array *a)
  160. {
  161. size_t len;
  162. UINT16 *ports;
  163. len = array_length(a, sizeof(UINT16));
  164. if (len--) {
  165. ports = (UINT16*) array_start(a);
  166. printf("%u", (unsigned int) *ports);
  167. while (len--) {
  168. ports++;
  169. printf(", %u", (unsigned int) *ports);
  170. }
  171. }
  172. putc('\n', stdout);
  173. }
  174. /**
  175. * Parse a comma separated string into an array of port numbers (integers).
  176. */
  177. static void
  178. ports_parse(array *a, const char *File, int Line, char *Arg)
  179. {
  180. char *ptr;
  181. int port;
  182. UINT16 port16;
  183. array_trunc(a);
  184. ptr = strtok( Arg, "," );
  185. while (ptr) {
  186. ngt_TrimStr(ptr);
  187. port = atoi(ptr);
  188. if (port > 0 && port < 0xFFFF) {
  189. port16 = (UINT16) port;
  190. if (!array_catb(a, (char*)&port16, sizeof port16))
  191. Config_Error(LOG_ERR, "%s, line %d Could not add port number %ld: %s",
  192. File, Line, port, strerror(errno));
  193. } else {
  194. Config_Error( LOG_ERR, "%s, line %d (section \"Global\"): Illegal port number %ld!",
  195. File, Line, port );
  196. }
  197. ptr = strtok( NULL, "," );
  198. }
  199. }
  200. /**
  201. * Initialize configuration module.
  202. */
  203. GLOBAL void
  204. Conf_Init( void )
  205. {
  206. Read_Config(false, true);
  207. Validate_Config(false, false);
  208. }
  209. /**
  210. * "Rehash" (reload) server configuration.
  211. *
  212. * @returns true if configuration has been re-read, false on errors.
  213. */
  214. GLOBAL bool
  215. Conf_Rehash( void )
  216. {
  217. if (!Read_Config(false, false))
  218. return false;
  219. Validate_Config(false, true);
  220. /* Update CLIENT structure of local server */
  221. Client_SetInfo(Client_ThisServer(), Conf_ServerInfo);
  222. return true;
  223. }
  224. /**
  225. * Output a boolean value as "yes/no" string.
  226. */
  227. static const char*
  228. yesno_to_str(int boolean_value)
  229. {
  230. if (boolean_value)
  231. return "yes";
  232. return "no";
  233. }
  234. /**
  235. * Free all IRC operator configuration structures.
  236. */
  237. static void
  238. opers_free(void)
  239. {
  240. struct Conf_Oper *op;
  241. size_t len;
  242. len = array_length(&Conf_Opers, sizeof(*op));
  243. op = array_start(&Conf_Opers);
  244. while (len--) {
  245. free(op->mask);
  246. op++;
  247. }
  248. array_free(&Conf_Opers);
  249. }
  250. /**
  251. * Output all IRC operator configuration structures.
  252. */
  253. static void
  254. opers_puts(void)
  255. {
  256. struct Conf_Oper *op;
  257. size_t count, i;
  258. count = array_length(&Conf_Opers, sizeof(*op));
  259. op = array_start(&Conf_Opers);
  260. for (i = 0; i < count; i++, op++) {
  261. if (!op->name[0])
  262. continue;
  263. puts("[OPERATOR]");
  264. printf(" Name = %s\n", op->name);
  265. printf(" Password = %s\n", op->pwd);
  266. printf(" Mask = %s\n\n", op->mask ? op->mask : "");
  267. }
  268. }
  269. /**
  270. * Read configuration, validate and output it.
  271. *
  272. * This function waits for a keypress of the user when stdin/stdout are valid
  273. * tty's ("you can read our nice message and we can read in your keypress").
  274. *
  275. * @return 0 on success, 1 on failure(s); therefore the result code can
  276. * directly be used by exit() when running "ngircd --configtest".
  277. */
  278. GLOBAL int
  279. Conf_Test( void )
  280. {
  281. struct passwd *pwd;
  282. struct group *grp;
  283. unsigned int i;
  284. bool config_valid;
  285. size_t predef_channel_count;
  286. struct Conf_Channel *predef_chan;
  287. Use_Log = false;
  288. if (!Read_Config(true, true))
  289. return 1;
  290. config_valid = Validate_Config(true, false);
  291. /* Valid tty? */
  292. if(isatty(fileno(stdin)) && isatty(fileno(stdout))) {
  293. puts("OK, press enter to see a dump of your server configuration ...");
  294. getchar();
  295. } else
  296. puts("Ok, dump of your server configuration follows:\n");
  297. puts("[GLOBAL]");
  298. printf(" Name = %s\n", Conf_ServerName);
  299. printf(" AdminInfo1 = %s\n", Conf_ServerAdmin1);
  300. printf(" AdminInfo2 = %s\n", Conf_ServerAdmin2);
  301. printf(" AdminEMail = %s\n", Conf_ServerAdminMail);
  302. printf(" HelpFile = %s\n", Conf_HelpFile);
  303. printf(" Info = %s\n", Conf_ServerInfo);
  304. printf(" Listen = %s\n", Conf_ListenAddress);
  305. if (Using_MotdFile) {
  306. printf(" MotdFile = %s\n", Conf_MotdFile);
  307. printf(" MotdPhrase =\n");
  308. } else {
  309. printf(" MotdFile = \n");
  310. printf(" MotdPhrase = %s\n", array_bytes(&Conf_Motd)
  311. ? (const char*) array_start(&Conf_Motd) : "");
  312. }
  313. printf(" Network = %s\n", Conf_Network);
  314. if (!Conf_PAM)
  315. printf(" Password = %s\n", Conf_ServerPwd);
  316. printf(" PidFile = %s\n", Conf_PidFile);
  317. printf(" Ports = ");
  318. ports_puts(&Conf_ListenPorts);
  319. grp = getgrgid(Conf_GID);
  320. if (grp)
  321. printf(" ServerGID = %s\n", grp->gr_name);
  322. else
  323. printf(" ServerGID = %ld\n", (long)Conf_GID);
  324. pwd = getpwuid(Conf_UID);
  325. if (pwd)
  326. printf(" ServerUID = %s\n", pwd->pw_name);
  327. else
  328. printf(" ServerUID = %ld\n", (long)Conf_UID);
  329. puts("");
  330. puts("[LIMITS]");
  331. printf(" ConnectRetry = %d\n", Conf_ConnectRetry);
  332. printf(" IdleTimeout = %d\n", Conf_IdleTimeout);
  333. printf(" MaxConnections = %d\n", Conf_MaxConnections);
  334. printf(" MaxConnectionsIP = %d\n", Conf_MaxConnectionsIP);
  335. printf(" MaxJoins = %d\n", Conf_MaxJoins > 0 ? Conf_MaxJoins : -1);
  336. printf(" MaxNickLength = %u\n", Conf_MaxNickLength - 1);
  337. printf(" MaxListSize = %d\n", Conf_MaxListSize);
  338. printf(" PingTimeout = %d\n", Conf_PingTimeout);
  339. printf(" PongTimeout = %d\n", Conf_PongTimeout);
  340. puts("");
  341. puts("[OPTIONS]");
  342. printf(" AllowedChannelTypes = %s\n", Conf_AllowedChannelTypes);
  343. printf(" AllowRemoteOper = %s\n", yesno_to_str(Conf_AllowRemoteOper));
  344. printf(" ChrootDir = %s\n", Conf_Chroot);
  345. printf(" CloakHost = %s\n", Conf_CloakHost);
  346. printf(" CloakHostModeX = %s\n", Conf_CloakHostModeX);
  347. printf(" CloakHostSalt = %s\n", Conf_CloakHostSalt);
  348. printf(" CloakUserToNick = %s\n", yesno_to_str(Conf_CloakUserToNick));
  349. #ifdef WANT_IPV6
  350. printf(" ConnectIPv4 = %s\n", yesno_to_str(Conf_ConnectIPv6));
  351. printf(" ConnectIPv6 = %s\n", yesno_to_str(Conf_ConnectIPv4));
  352. #endif
  353. printf(" DefaultUserModes = %s\n", Conf_DefaultUserModes);
  354. printf(" DNS = %s\n", yesno_to_str(Conf_DNS));
  355. #ifdef IDENT
  356. printf(" Ident = %s\n", yesno_to_str(Conf_Ident));
  357. #endif
  358. printf(" IncludeDir = %s\n", Conf_IncludeDir);
  359. printf(" MorePrivacy = %s\n", yesno_to_str(Conf_MorePrivacy));
  360. printf(" NoticeBeforeRegistration = %s\n", yesno_to_str(Conf_NoticeBeforeRegistration));
  361. printf(" OperCanUseMode = %s\n", yesno_to_str(Conf_OperCanMode));
  362. printf(" OperChanPAutoOp = %s\n", yesno_to_str(Conf_OperChanPAutoOp));
  363. printf(" OperServerMode = %s\n", yesno_to_str(Conf_OperServerMode));
  364. #ifdef PAM
  365. printf(" PAM = %s\n", yesno_to_str(Conf_PAM));
  366. printf(" PAMIsOptional = %s\n", yesno_to_str(Conf_PAMIsOptional));
  367. printf(" PAMServiceName = %s\n", Conf_PAMServiceName);
  368. #endif
  369. #ifndef STRICT_RFC
  370. printf(" RequireAuthPing = %s\n", yesno_to_str(Conf_AuthPing));
  371. #endif
  372. printf(" ScrubCTCP = %s\n", yesno_to_str(Conf_ScrubCTCP));
  373. #ifdef SYSLOG
  374. printf(" SyslogFacility = %s\n",
  375. ngt_SyslogFacilityName(Conf_SyslogFacility));
  376. #endif
  377. printf(" WebircPassword = %s\n", Conf_WebircPwd);
  378. puts("");
  379. #ifdef SSL_SUPPORT
  380. puts("[SSL]");
  381. printf(" CertFile = %s\n", Conf_SSLOptions.CertFile
  382. ? Conf_SSLOptions.CertFile : "");
  383. printf(" CipherList = %s\n", Conf_SSLOptions.CipherList ?
  384. Conf_SSLOptions.CipherList : DEFAULT_CIPHERS);
  385. printf(" DHFile = %s\n", Conf_SSLOptions.DHFile
  386. ? Conf_SSLOptions.DHFile : "");
  387. printf(" KeyFile = %s\n", Conf_SSLOptions.KeyFile
  388. ? Conf_SSLOptions.KeyFile : "");
  389. if (array_bytes(&Conf_SSLOptions.KeyFilePassword))
  390. puts(" KeyFilePassword = <secret>");
  391. else
  392. puts(" KeyFilePassword = ");
  393. array_free_wipe(&Conf_SSLOptions.KeyFilePassword);
  394. printf(" Ports = ");
  395. ports_puts(&Conf_SSLOptions.ListenPorts);
  396. puts("");
  397. #endif
  398. opers_puts();
  399. for( i = 0; i < MAX_SERVERS; i++ ) {
  400. if( ! Conf_Server[i].name[0] ) continue;
  401. /* Valid "Server" section */
  402. puts( "[SERVER]" );
  403. printf( " Name = %s\n", Conf_Server[i].name );
  404. printf( " Host = %s\n", Conf_Server[i].host );
  405. printf( " Port = %u\n", (unsigned int)Conf_Server[i].port );
  406. #ifdef SSL_SUPPORT
  407. printf( " SSLConnect = %s\n", Conf_Server[i].SSLConnect?"yes":"no");
  408. #endif
  409. printf( " MyPassword = %s\n", Conf_Server[i].pwd_in );
  410. printf( " PeerPassword = %s\n", Conf_Server[i].pwd_out );
  411. printf( " ServiceMask = %s\n", Conf_Server[i].svs_mask);
  412. printf( " Group = %d\n", Conf_Server[i].group );
  413. printf( " Passive = %s\n\n", Conf_Server[i].flags & CONF_SFLAG_DISABLED ? "yes" : "no");
  414. }
  415. predef_channel_count = array_length(&Conf_Channels, sizeof(*predef_chan));
  416. predef_chan = array_start(&Conf_Channels);
  417. for (i = 0; i < predef_channel_count; i++, predef_chan++) {
  418. if (!predef_chan->name[0])
  419. continue;
  420. /* Valid "Channel" section */
  421. puts( "[CHANNEL]" );
  422. printf(" Name = %s\n", predef_chan->name);
  423. printf(" Modes = %s\n", predef_chan->modes);
  424. printf(" Key = %s\n", predef_chan->key);
  425. printf(" MaxUsers = %lu\n", predef_chan->maxusers);
  426. printf(" Topic = %s\n", predef_chan->topic);
  427. printf(" KeyFile = %s\n\n", predef_chan->keyfile);
  428. }
  429. return (config_valid ? 0 : 1);
  430. }
  431. /**
  432. * Remove connection information from configured server.
  433. *
  434. * If the server is set as "once", delete it from our configuration;
  435. * otherwise set the time for the next connection attempt.
  436. *
  437. * Non-server connections will be silently ignored.
  438. */
  439. GLOBAL void
  440. Conf_UnsetServer( CONN_ID Idx )
  441. {
  442. int i;
  443. time_t t;
  444. /* Check all our configured servers */
  445. for( i = 0; i < MAX_SERVERS; i++ ) {
  446. if( Conf_Server[i].conn_id != Idx ) continue;
  447. /* Gotcha! Mark server configuration as "unused": */
  448. Conf_Server[i].conn_id = NONE;
  449. if( Conf_Server[i].flags & CONF_SFLAG_ONCE ) {
  450. /* Delete configuration here */
  451. Init_Server_Struct( &Conf_Server[i] );
  452. } else {
  453. /* Set time for next connect attempt */
  454. t = time(NULL);
  455. if (Conf_Server[i].lasttry < t - Conf_ConnectRetry) {
  456. /* The connection has been "long", so we don't
  457. * require the next attempt to be delayed. */
  458. Conf_Server[i].lasttry =
  459. t - Conf_ConnectRetry + RECONNECT_DELAY;
  460. } else {
  461. /* "Short" connection, enforce "ConnectRetry"
  462. * but randomize it a little bit: 15 seconds. */
  463. Conf_Server[i].lasttry =
  464. #ifdef HAVE_ARC4RANDOM
  465. t + (arc4random() % 15);
  466. #else
  467. t + rand() / (RAND_MAX / 15);
  468. #endif
  469. }
  470. }
  471. }
  472. }
  473. /**
  474. * Set connection information for specified configured server.
  475. */
  476. GLOBAL bool
  477. Conf_SetServer( int ConfServer, CONN_ID Idx )
  478. {
  479. assert( ConfServer > NONE );
  480. assert( Idx > NONE );
  481. if (Conf_Server[ConfServer].conn_id > NONE &&
  482. Conf_Server[ConfServer].conn_id != Idx) {
  483. Log(LOG_ERR,
  484. "Connection %d: Server configuration of \"%s\" already in use by connection %d!",
  485. Idx, Conf_Server[ConfServer].name,
  486. Conf_Server[ConfServer].conn_id);
  487. Conn_Close(Idx, NULL, "Server configuration already in use", true);
  488. return false;
  489. }
  490. Conf_Server[ConfServer].conn_id = Idx;
  491. return true;
  492. }
  493. /**
  494. * Get index of server in configuration structure.
  495. */
  496. GLOBAL int
  497. Conf_GetServer( CONN_ID Idx )
  498. {
  499. int i = 0;
  500. assert( Idx > NONE );
  501. for( i = 0; i < MAX_SERVERS; i++ ) {
  502. if( Conf_Server[i].conn_id == Idx ) return i;
  503. }
  504. return NONE;
  505. }
  506. /**
  507. * Enable a server by name and adjust its port number.
  508. *
  509. * @returns true if a server has been enabled and now has a valid port
  510. * number and host name for outgoing connections.
  511. */
  512. GLOBAL bool
  513. Conf_EnableServer( const char *Name, UINT16 Port )
  514. {
  515. int i;
  516. assert( Name != NULL );
  517. for( i = 0; i < MAX_SERVERS; i++ ) {
  518. if( strcasecmp( Conf_Server[i].name, Name ) == 0 ) {
  519. /* Gotcha! Set port and enable server: */
  520. Conf_Server[i].port = Port;
  521. Conf_Server[i].flags &= ~CONF_SFLAG_DISABLED;
  522. return (Conf_Server[i].port && Conf_Server[i].host[0]);
  523. }
  524. }
  525. return false;
  526. }
  527. /**
  528. * Enable a server by name.
  529. *
  530. * The server is only usable as outgoing server, if it has set a valid port
  531. * number for outgoing connections!
  532. * If not, you have to use Conf_EnableServer() function to make it available.
  533. *
  534. * @returns true if a server has been enabled; false otherwise.
  535. */
  536. GLOBAL bool
  537. Conf_EnablePassiveServer(const char *Name)
  538. {
  539. int i;
  540. assert( Name != NULL );
  541. for (i = 0; i < MAX_SERVERS; i++) {
  542. if ((strcasecmp( Conf_Server[i].name, Name ) == 0)
  543. && (Conf_Server[i].port > 0)) {
  544. /* BINGO! Enable server */
  545. Conf_Server[i].flags &= ~CONF_SFLAG_DISABLED;
  546. Conf_Server[i].lasttry = 0;
  547. return true;
  548. }
  549. }
  550. return false;
  551. }
  552. /**
  553. * Disable a server by name.
  554. * An already established connection will be disconnected.
  555. *
  556. * @returns true if a server was found and has been disabled.
  557. */
  558. GLOBAL bool
  559. Conf_DisableServer( const char *Name )
  560. {
  561. int i;
  562. assert( Name != NULL );
  563. for( i = 0; i < MAX_SERVERS; i++ ) {
  564. if( strcasecmp( Conf_Server[i].name, Name ) == 0 ) {
  565. /* Gotcha! Disable and disconnect server: */
  566. Conf_Server[i].flags |= CONF_SFLAG_DISABLED;
  567. if( Conf_Server[i].conn_id > NONE )
  568. Conn_Close(Conf_Server[i].conn_id, NULL,
  569. "Server link terminated on operator request",
  570. true);
  571. return true;
  572. }
  573. }
  574. return false;
  575. }
  576. /**
  577. * Add a new remote server to our configuration.
  578. *
  579. * @param Name Name of the new server.
  580. * @param Port Port number to connect to or 0 for incoming connections.
  581. * @param Host Host name to connect to.
  582. * @param MyPwd Password that will be sent to the peer.
  583. * @param PeerPwd Password that must be received from the peer.
  584. * @returns true if the new server has been added; false otherwise.
  585. */
  586. GLOBAL bool
  587. Conf_AddServer(const char *Name, UINT16 Port, const char *Host,
  588. const char *MyPwd, const char *PeerPwd)
  589. {
  590. int i;
  591. assert( Name != NULL );
  592. assert( Host != NULL );
  593. assert( MyPwd != NULL );
  594. assert( PeerPwd != NULL );
  595. /* Search unused item in server configuration structure */
  596. for( i = 0; i < MAX_SERVERS; i++ ) {
  597. /* Is this item used? */
  598. if( ! Conf_Server[i].name[0] ) break;
  599. }
  600. if( i >= MAX_SERVERS ) return false;
  601. Init_Server_Struct( &Conf_Server[i] );
  602. strlcpy( Conf_Server[i].name, Name, sizeof( Conf_Server[i].name ));
  603. strlcpy( Conf_Server[i].host, Host, sizeof( Conf_Server[i].host ));
  604. strlcpy( Conf_Server[i].pwd_out, MyPwd, sizeof( Conf_Server[i].pwd_out ));
  605. strlcpy( Conf_Server[i].pwd_in, PeerPwd, sizeof( Conf_Server[i].pwd_in ));
  606. Conf_Server[i].port = Port;
  607. Conf_Server[i].flags = CONF_SFLAG_ONCE;
  608. return true;
  609. }
  610. /**
  611. * Check if the given nickname is reserved for services on a particular server.
  612. *
  613. * @param ConfServer The server index to check.
  614. * @param Nick The nickname to check.
  615. * @returns true if the given nickname belongs to an "IRC service".
  616. */
  617. GLOBAL bool
  618. Conf_NickIsService(int ConfServer, const char *Nick)
  619. {
  620. assert (ConfServer >= 0);
  621. assert (ConfServer < MAX_SERVERS);
  622. return MatchCaseInsensitiveList(Conf_Server[ConfServer].svs_mask,
  623. Nick, ",");
  624. }
  625. /**
  626. * Check if the given nickname is blocked for "normal client" use.
  627. *
  628. * @param ConfServer The server index or NONE to check all configured servers.
  629. * @param Nick The nickname to check.
  630. * @returns true if the given nickname belongs to an "IRC service".
  631. */
  632. GLOBAL bool
  633. Conf_NickIsBlocked(const char *Nick)
  634. {
  635. int i;
  636. for(i = 0; i < MAX_SERVERS; i++) {
  637. if (!Conf_Server[i].name[0])
  638. continue;
  639. if (Conf_NickIsService(i, Nick))
  640. return true;
  641. }
  642. return false;
  643. }
  644. /**
  645. * Initialize configuration settings with their default values.
  646. */
  647. static void
  648. Set_Defaults(bool InitServers)
  649. {
  650. int i;
  651. char random[RANDOM_SALT_LEN + 1];
  652. /* Global */
  653. strcpy(Conf_ServerName, "");
  654. strcpy(Conf_ServerAdmin1, "");
  655. strcpy(Conf_ServerAdmin2, "");
  656. strcpy(Conf_ServerAdminMail, "");
  657. snprintf(Conf_ServerInfo, sizeof Conf_ServerInfo, "%s %s",
  658. PACKAGE_NAME, PACKAGE_VERSION);
  659. strcpy(Conf_Network, "");
  660. free(Conf_ListenAddress);
  661. Conf_ListenAddress = NULL;
  662. array_free(&Conf_ListenPorts);
  663. array_free(&Conf_Motd);
  664. array_free(&Conf_Helptext);
  665. strlcpy(Conf_MotdFile, SYSCONFDIR, sizeof(Conf_MotdFile));
  666. strlcat(Conf_MotdFile, MOTD_FILE, sizeof(Conf_MotdFile));
  667. strlcpy(Conf_HelpFile, DOCDIR, sizeof(Conf_HelpFile));
  668. strlcat(Conf_HelpFile, HELP_FILE, sizeof(Conf_HelpFile));
  669. strcpy(Conf_ServerPwd, "");
  670. strlcpy(Conf_PidFile, PID_FILE, sizeof(Conf_PidFile));
  671. Conf_UID = Conf_GID = 0;
  672. /* Limits */
  673. Conf_ConnectRetry = 60;
  674. Conf_IdleTimeout = 0;
  675. Conf_MaxConnections = 0;
  676. Conf_MaxConnectionsIP = 5;
  677. Conf_MaxJoins = 10;
  678. Conf_MaxNickLength = CLIENT_NICK_LEN_DEFAULT;
  679. Conf_MaxListSize = 100;
  680. Conf_PingTimeout = 120;
  681. Conf_PongTimeout = 20;
  682. /* Options */
  683. strlcpy(Conf_AllowedChannelTypes, CHANTYPES,
  684. sizeof(Conf_AllowedChannelTypes));
  685. Conf_AllowRemoteOper = false;
  686. #ifndef STRICT_RFC
  687. Conf_AuthPing = false;
  688. #endif
  689. strlcpy(Conf_Chroot, CHROOT_DIR, sizeof(Conf_Chroot));
  690. strcpy(Conf_CloakHost, "");
  691. strcpy(Conf_CloakHostModeX, "");
  692. strlcpy(Conf_CloakHostSalt, ngt_RandomStr(random, RANDOM_SALT_LEN),
  693. sizeof(Conf_CloakHostSalt));
  694. Conf_CloakUserToNick = false;
  695. Conf_ConnectIPv4 = true;
  696. #ifdef WANT_IPV6
  697. Conf_ConnectIPv6 = true;
  698. #else
  699. Conf_ConnectIPv6 = false;
  700. #endif
  701. strcpy(Conf_DefaultUserModes, "");
  702. Conf_DNS = true;
  703. #ifdef IDENTAUTH
  704. Conf_Ident = true;
  705. #else
  706. Conf_Ident = false;
  707. #endif
  708. strcpy(Conf_IncludeDir, "");
  709. Conf_MorePrivacy = false;
  710. Conf_NoticeBeforeRegistration = false;
  711. Conf_OperCanMode = false;
  712. Conf_OperChanPAutoOp = true;
  713. Conf_OperServerMode = false;
  714. #ifdef PAM
  715. Conf_PAM = true;
  716. #else
  717. Conf_PAM = false;
  718. #endif
  719. Conf_PAMIsOptional = false;
  720. strcpy(Conf_PAMServiceName, "ngircd");
  721. Conf_ScrubCTCP = false;
  722. #ifdef SYSLOG
  723. #ifdef LOG_LOCAL5
  724. Conf_SyslogFacility = LOG_LOCAL5;
  725. #else
  726. Conf_SyslogFacility = 0;
  727. #endif
  728. #endif
  729. /* Initialize server configuration structures */
  730. if (InitServers) {
  731. for (i = 0; i < MAX_SERVERS;
  732. Init_Server_Struct(&Conf_Server[i++]));
  733. }
  734. }
  735. /**
  736. * Get number of configured listening ports.
  737. *
  738. * @returns The number of ports (IPv4+IPv6) on which the server should listen.
  739. */
  740. static bool
  741. no_listenports(void)
  742. {
  743. size_t cnt = array_bytes(&Conf_ListenPorts);
  744. #ifdef SSL_SUPPORT
  745. cnt += array_bytes(&Conf_SSLOptions.ListenPorts);
  746. #endif
  747. return cnt == 0;
  748. }
  749. /**
  750. * Read contents of a text file into an array.
  751. *
  752. * This function is used to read the MOTD and help text file, for example.
  753. *
  754. * @param filename Name of the file to read.
  755. * @return true, when the file has been read in.
  756. */
  757. static bool
  758. Read_TextFile(const char *Filename, const char *Name, array *Destination)
  759. {
  760. char line[127];
  761. FILE *fp;
  762. int line_no = 1;
  763. if (*Filename == '\0')
  764. return false;
  765. fp = fopen(Filename, "r");
  766. if (!fp) {
  767. Config_Error(LOG_ERR, "Can't read %s file \"%s\": %s",
  768. Name, Filename, strerror(errno));
  769. return false;
  770. }
  771. array_free(Destination);
  772. while (fgets(line, (int)sizeof line, fp)) {
  773. ngt_TrimLastChr(line, '\n');
  774. /* add text including \0 */
  775. if (!array_catb(Destination, line, strlen(line) + 1)) {
  776. Log(LOG_ERR, "Cannot read/add \"%s\", line %d: %s",
  777. Filename, line_no, strerror(errno));
  778. break;
  779. }
  780. line_no++;
  781. }
  782. fclose(fp);
  783. return true;
  784. }
  785. /**
  786. * Read ngIRCd configuration file.
  787. *
  788. * Please note that this function uses exit(1) on fatal errors and therefore
  789. * can result in ngIRCd terminating!
  790. *
  791. * @param ngircd_starting Flag indicating if ngIRCd is starting or not.
  792. * @returns true when the configuration file has been read
  793. * successfully; false otherwise.
  794. */
  795. static bool
  796. Read_Config(bool TestOnly, bool IsStarting)
  797. {
  798. const UINT16 defaultport = 6667;
  799. char *ptr, file[FNAME_LEN];
  800. struct dirent *entry;
  801. int i, n;
  802. FILE *fd;
  803. DIR *dh;
  804. /* Open configuration file */
  805. fd = fopen( NGIRCd_ConfFile, "r" );
  806. if( ! fd ) {
  807. /* No configuration file found! */
  808. Config_Error( LOG_ALERT, "Can't read configuration \"%s\": %s",
  809. NGIRCd_ConfFile, strerror( errno ));
  810. if (!IsStarting)
  811. return false;
  812. Config_Error( LOG_ALERT, "%s exiting due to fatal errors!", PACKAGE_NAME );
  813. exit( 1 );
  814. }
  815. opers_free();
  816. Set_Defaults(IsStarting);
  817. if (TestOnly)
  818. Config_Error(LOG_INFO,
  819. "Reading configuration from \"%s\" ...",
  820. NGIRCd_ConfFile );
  821. /* Clean up server configuration structure: mark all already
  822. * configured servers as "once" so that they are deleted
  823. * after the next disconnect and delete all unused servers.
  824. * And delete all servers which are "duplicates" of servers
  825. * that are already marked as "once" (such servers have been
  826. * created by the last rehash but are now useless). */
  827. for( i = 0; i < MAX_SERVERS; i++ ) {
  828. if( Conf_Server[i].conn_id == NONE ) Init_Server_Struct( &Conf_Server[i] );
  829. else {
  830. /* This structure is in use ... */
  831. if( Conf_Server[i].flags & CONF_SFLAG_ONCE ) {
  832. /* Check for duplicates */
  833. for( n = 0; n < MAX_SERVERS; n++ ) {
  834. if( n == i ) continue;
  835. if( Conf_Server[i].conn_id == Conf_Server[n].conn_id ) {
  836. Init_Server_Struct( &Conf_Server[n] );
  837. #ifdef DEBUG
  838. Log(LOG_DEBUG,"Deleted unused duplicate server %d (kept %d).",
  839. n, i );
  840. #endif
  841. }
  842. }
  843. } else {
  844. /* Mark server as "once" */
  845. Conf_Server[i].flags |= CONF_SFLAG_ONCE;
  846. Log( LOG_DEBUG, "Marked server %d as \"once\"", i );
  847. }
  848. }
  849. }
  850. /* Initialize variables */
  851. Init_Server_Struct( &New_Server );
  852. New_Server_Idx = NONE;
  853. #ifdef SSL_SUPPORT
  854. ConfSSL_Init();
  855. #endif
  856. Read_Config_File(NGIRCd_ConfFile, fd);
  857. fclose(fd);
  858. if (Conf_IncludeDir[0]) {
  859. dh = opendir(Conf_IncludeDir);
  860. if (!dh)
  861. Config_Error(LOG_ALERT,
  862. "Can't open include directory \"%s\": %s",
  863. Conf_IncludeDir, strerror(errno));
  864. } else {
  865. strlcpy(Conf_IncludeDir, SYSCONFDIR, sizeof(Conf_IncludeDir));
  866. strlcat(Conf_IncludeDir, CONFIG_DIR, sizeof(Conf_IncludeDir));
  867. dh = opendir(Conf_IncludeDir);
  868. }
  869. /* Include further configuration files, if IncludeDir is available */
  870. if (dh) {
  871. while ((entry = readdir(dh)) != NULL) {
  872. ptr = strrchr(entry->d_name, '.');
  873. if (!ptr || strcasecmp(ptr, ".conf") != 0)
  874. continue;
  875. snprintf(file, sizeof(file), "%s/%s",
  876. Conf_IncludeDir, entry->d_name);
  877. if (TestOnly)
  878. Config_Error(LOG_INFO,
  879. "Reading configuration from \"%s\" ...",
  880. file);
  881. fd = fopen(file, "r");
  882. if (fd) {
  883. Read_Config_File(file, fd);
  884. fclose(fd);
  885. } else
  886. Config_Error(LOG_ALERT,
  887. "Can't read configuration \"%s\": %s",
  888. file, strerror(errno));
  889. }
  890. closedir(dh);
  891. }
  892. /* Check if there is still a server to add */
  893. if( New_Server.name[0] ) {
  894. /* Copy data to "real" server structure */
  895. assert( New_Server_Idx > NONE );
  896. Conf_Server[New_Server_Idx] = New_Server;
  897. }
  898. /* not a single listening port? Add default. */
  899. if (no_listenports() &&
  900. !array_copyb(&Conf_ListenPorts, (char*) &defaultport, sizeof defaultport))
  901. {
  902. Config_Error(LOG_ALERT, "Could not add default listening Port %u: %s",
  903. (unsigned int) defaultport, strerror(errno));
  904. exit(1);
  905. }
  906. if (!Conf_ListenAddress)
  907. Conf_ListenAddress = strdup_warn(DEFAULT_LISTEN_ADDRSTR);
  908. if (!Conf_ListenAddress) {
  909. Config_Error(LOG_ALERT, "%s exiting due to fatal errors!", PACKAGE_NAME);
  910. exit(1);
  911. }
  912. /* No MOTD phrase configured? (re)try motd file. */
  913. if (array_bytes(&Conf_Motd) == 0) {
  914. if (Read_TextFile(Conf_MotdFile, "MOTD", &Conf_Motd))
  915. Using_MotdFile = true;
  916. }
  917. /* Try to read ngIRCd help text file. */
  918. (void)Read_TextFile(Conf_HelpFile, "help text", &Conf_Helptext);
  919. if (!array_bytes(&Conf_Helptext))
  920. Config_Error(LOG_WARNING,
  921. "No help text available, HELP command will be of limited use.");
  922. #ifdef SSL_SUPPORT
  923. /* Make sure that all SSL-related files are readable */
  924. CheckFileReadable("CertFile", Conf_SSLOptions.CertFile);
  925. CheckFileReadable("DHFile", Conf_SSLOptions.DHFile);
  926. CheckFileReadable("KeyFile", Conf_SSLOptions.KeyFile);
  927. /* Set the default ciphers if none were configured */
  928. if (!Conf_SSLOptions.CipherList)
  929. Conf_SSLOptions.CipherList = strdup_warn(DEFAULT_CIPHERS);
  930. #endif
  931. return true;
  932. }
  933. /**
  934. * Read in and handle a configuration file.
  935. *
  936. * @param File Name of the configuration file.
  937. * @param fd File descriptor already opened for reading.
  938. */
  939. static void
  940. Read_Config_File(const char *File, FILE *fd)
  941. {
  942. char section[LINE_LEN], str[LINE_LEN], *var, *arg, *ptr;
  943. int i, line = 0;
  944. size_t count;
  945. /* Read configuration file */
  946. section[0] = '\0';
  947. while (true) {
  948. if (!fgets(str, sizeof(str), fd))
  949. break;
  950. ngt_TrimStr(str);
  951. line++;
  952. /* Skip comments and empty lines */
  953. if (str[0] == ';' || str[0] == '#' || str[0] == '\0')
  954. continue;
  955. if (strlen(str) >= sizeof(str) - 1) {
  956. Config_Error(LOG_WARNING, "%s, line %d too long!",
  957. File, line);
  958. continue;
  959. }
  960. /* Is this the beginning of a new section? */
  961. if ((str[0] == '[') && (str[strlen(str) - 1] == ']')) {
  962. strlcpy(section, str, sizeof(section));
  963. if (strcasecmp(section, "[GLOBAL]") == 0
  964. || strcasecmp(section, "[LIMITS]") == 0
  965. || strcasecmp(section, "[OPTIONS]") == 0
  966. #ifdef SSL_SUPPORT
  967. || strcasecmp(section, "[SSL]") == 0
  968. #endif
  969. )
  970. continue;
  971. if (strcasecmp(section, "[SERVER]") == 0) {
  972. /* Check if there is already a server to add */
  973. if (New_Server.name[0]) {
  974. /* Copy data to "real" server structure */
  975. assert(New_Server_Idx > NONE);
  976. Conf_Server[New_Server_Idx] =
  977. New_Server;
  978. }
  979. /* Re-init structure for new server */
  980. Init_Server_Struct(&New_Server);
  981. /* Search unused item in server configuration structure */
  982. for (i = 0; i < MAX_SERVERS; i++) {
  983. /* Is this item used? */
  984. if (!Conf_Server[i].name[0])
  985. break;
  986. }
  987. if (i >= MAX_SERVERS) {
  988. /* Oops, no free item found! */
  989. Config_Error(LOG_ERR,
  990. "Too many servers configured.");
  991. New_Server_Idx = NONE;
  992. } else
  993. New_Server_Idx = i;
  994. continue;
  995. }
  996. if (strcasecmp(section, "[CHANNEL]") == 0) {
  997. count = array_length(&Conf_Channels,
  998. sizeof(struct
  999. Conf_Channel));
  1000. if (!array_alloc
  1001. (&Conf_Channels,
  1002. sizeof(struct Conf_Channel), count)) {
  1003. Config_Error(LOG_ERR,
  1004. "Could not allocate memory for new operator (line %d)",
  1005. line);
  1006. }
  1007. continue;
  1008. }
  1009. if (strcasecmp(section, "[OPERATOR]") == 0) {
  1010. count = array_length(&Conf_Opers,
  1011. sizeof(struct Conf_Oper));
  1012. if (!array_alloc(&Conf_Opers,
  1013. sizeof(struct Conf_Oper),
  1014. count)) {
  1015. Config_Error(LOG_ERR,
  1016. "Could not allocate memory for new channel (line &d)",
  1017. line);
  1018. }
  1019. continue;
  1020. }
  1021. Config_Error(LOG_ERR,
  1022. "%s, line %d: Unknown section \"%s\"!",
  1023. File, line, section);
  1024. section[0] = 0x1;
  1025. }
  1026. if (section[0] == 0x1)
  1027. continue;
  1028. /* Split line into variable name and parameters */
  1029. ptr = strchr(str, '=');
  1030. if (!ptr) {
  1031. Config_Error(LOG_ERR, "%s, line %d: Syntax error!",
  1032. File, line);
  1033. continue;
  1034. }
  1035. *ptr = '\0';
  1036. var = str;
  1037. ngt_TrimStr(var);
  1038. arg = ptr + 1;
  1039. ngt_TrimStr(arg);
  1040. if (strcasecmp(section, "[GLOBAL]") == 0)
  1041. Handle_GLOBAL(File, line, var, arg);
  1042. else if (strcasecmp(section, "[LIMITS]") == 0)
  1043. Handle_LIMITS(File, line, var, arg);
  1044. else if (strcasecmp(section, "[OPTIONS]") == 0)
  1045. Handle_OPTIONS(File, line, var, arg);
  1046. #ifdef SSL_SUPPORT
  1047. else if (strcasecmp(section, "[SSL]") == 0)
  1048. Handle_SSL(File, line, var, arg);
  1049. #endif
  1050. else if (strcasecmp(section, "[OPERATOR]") == 0)
  1051. Handle_OPERATOR(File, line, var, arg);
  1052. else if (strcasecmp(section, "[SERVER]") == 0)
  1053. Handle_SERVER(File, line, var, arg);
  1054. else if (strcasecmp(section, "[CHANNEL]") == 0)
  1055. Handle_CHANNEL(File, line, var, arg);
  1056. else
  1057. Config_Error(LOG_ERR,
  1058. "%s, line %d: Variable \"%s\" outside section!",
  1059. File, line, var);
  1060. }
  1061. }
  1062. /**
  1063. * Check whether a string argument is "true" or "false".
  1064. *
  1065. * @param Arg Input string.
  1066. * @returns true if the input string has been parsed as "yes", "true"
  1067. * (case insensitive) or a non-zero integer value.
  1068. */
  1069. static bool
  1070. Check_ArgIsTrue(const char *Arg)
  1071. {
  1072. if (strcasecmp(Arg, "yes") == 0)
  1073. return true;
  1074. if (strcasecmp(Arg, "true") == 0)
  1075. return true;
  1076. if (atoi(Arg) != 0)
  1077. return true;
  1078. return false;
  1079. }
  1080. /**
  1081. * Handle setting of "MaxNickLength".
  1082. *
  1083. * @param Line Line number in configuration file.
  1084. * @raram Arg Input string.
  1085. * @returns New configured maximum nickname length.
  1086. */
  1087. static unsigned int
  1088. Handle_MaxNickLength(const char *File, int Line, const char *Arg)
  1089. {
  1090. unsigned new;
  1091. new = (unsigned) atoi(Arg) + 1;
  1092. if (new > CLIENT_NICK_LEN) {
  1093. Config_Error(LOG_WARNING,
  1094. "%s, line %d: Value of \"MaxNickLength\" exceeds %u!",
  1095. File, Line, CLIENT_NICK_LEN - 1);
  1096. return CLIENT_NICK_LEN;
  1097. }
  1098. if (new < 2) {
  1099. Config_Error(LOG_WARNING,
  1100. "%s, line %d: Value of \"MaxNickLength\" must be at least 1!",
  1101. File, Line);
  1102. return 2;
  1103. }
  1104. return new;
  1105. }
  1106. /**
  1107. * Output a warning messages if IDENT is configured but not compiled in.
  1108. */
  1109. static void
  1110. WarnIdent(const char UNUSED *File, int UNUSED Line)
  1111. {
  1112. #ifndef IDENTAUTH
  1113. if (Conf_Ident) {
  1114. /* user has enabled ident lookups explicitly, but ... */
  1115. Config_Error(LOG_WARNING,
  1116. "%s: line %d: \"Ident = yes\", but ngircd was built without IDENT support!",
  1117. File, Line);
  1118. }
  1119. #endif
  1120. }
  1121. /**
  1122. * Output a warning messages if IPv6 is configured but not compiled in.
  1123. */
  1124. static void
  1125. WarnIPv6(const char UNUSED *File, int UNUSED Line)
  1126. {
  1127. #ifndef WANT_IPV6
  1128. if (Conf_ConnectIPv6) {
  1129. /* user has enabled IPv6 explicitly, but ... */
  1130. Config_Error(LOG_WARNING,
  1131. "%s: line %d: \"ConnectIPv6 = yes\", but ngircd was built without IPv6 support!",
  1132. File, Line);
  1133. }
  1134. #endif
  1135. }
  1136. /**
  1137. * Output a warning messages if PAM is configured but not compiled in.
  1138. */
  1139. static void
  1140. WarnPAM(const char UNUSED *File, int UNUSED Line)
  1141. {
  1142. #ifndef PAM
  1143. if (Conf_PAM) {
  1144. Config_Error(LOG_WARNING,
  1145. "%s: line %d: \"PAM = yes\", but ngircd was built without PAM support!",
  1146. File, Line);
  1147. }
  1148. #endif
  1149. }
  1150. /**
  1151. * Handle legacy "NoXXX" options in [GLOBAL] section.
  1152. *
  1153. * TODO: This function and support for "NoXXX" could be removed starting
  1154. * with ngIRCd release 19 (one release after marking it "deprecated").
  1155. *
  1156. * @param Var Variable name.
  1157. * @param Arg Argument string.
  1158. * @returns true if a NoXXX option has been processed; false otherwise.
  1159. */
  1160. static bool
  1161. CheckLegacyNoOption(const char *Var, const char *Arg)
  1162. {
  1163. if(strcasecmp(Var, "NoDNS") == 0) {
  1164. Conf_DNS = !Check_ArgIsTrue( Arg );
  1165. return true;
  1166. }
  1167. if (strcasecmp(Var, "NoIdent") == 0) {
  1168. Conf_Ident = !Check_ArgIsTrue(Arg);
  1169. return true;
  1170. }
  1171. if(strcasecmp(Var, "NoPAM") == 0) {
  1172. Conf_PAM = !Check_ArgIsTrue(Arg);
  1173. return true;
  1174. }
  1175. return false;
  1176. }
  1177. /**
  1178. * Handle deprecated legacy options in [GLOBAL] section.
  1179. *
  1180. * TODO: This function and support for these options in the [Global] section
  1181. * could be removed starting with ngIRCd release 19 (one release after
  1182. * marking it "deprecated").
  1183. *
  1184. * @param Var Variable name.
  1185. * @param Arg Argument string.
  1186. * @returns true if a legacy option has been processed; false otherwise.
  1187. */
  1188. static const char*
  1189. CheckLegacyGlobalOption(const char *File, int Line, char *Var, char *Arg)
  1190. {
  1191. if (strcasecmp(Var, "AllowRemoteOper") == 0
  1192. || strcasecmp(Var, "ChrootDir") == 0
  1193. || strcasecmp(Var, "ConnectIPv4") == 0
  1194. || strcasecmp(Var, "ConnectIPv6") == 0
  1195. || strcasecmp(Var, "OperCanUseMode") == 0
  1196. || strcasecmp(Var, "OperChanPAutoOp") == 0
  1197. || strcasecmp(Var, "OperServerMode") == 0
  1198. || strcasecmp(Var, "PredefChannelsOnly") == 0
  1199. || strcasecmp(Var, "SyslogFacility") == 0
  1200. || strcasecmp(Var, "WebircPassword") == 0) {
  1201. Handle_OPTIONS(File, Line, Var, Arg);
  1202. return "[Options]";
  1203. }
  1204. if (strcasecmp(Var, "ConnectRetry") == 0
  1205. || strcasecmp(Var, "IdleTimeout") == 0
  1206. || strcasecmp(Var, "MaxConnections") == 0
  1207. || strcasecmp(Var, "MaxConnectionsIP") == 0
  1208. || strcasecmp(Var, "MaxJoins") == 0
  1209. || strcasecmp(Var, "MaxNickLength") == 0
  1210. || strcasecmp(Var, "PingTimeout") == 0
  1211. || strcasecmp(Var, "PongTimeout") == 0) {
  1212. Handle_LIMITS(File, Line, Var, Arg);
  1213. return "[Limits]";
  1214. }
  1215. #ifdef SSL_SUPPORT
  1216. if (strcasecmp(Var, "SSLCertFile") == 0
  1217. || strcasecmp(Var, "SSLDHFile") == 0
  1218. || strcasecmp(Var, "SSLKeyFile") == 0
  1219. || strcasecmp(Var, "SSLKeyFilePassword") == 0
  1220. || strcasecmp(Var, "SSLPorts") == 0) {
  1221. Handle_SSL(File, Line, Var + 3, Arg);
  1222. return "[SSL]";
  1223. }
  1224. #endif
  1225. return NULL;
  1226. }
  1227. /**
  1228. * Strip "no" prefix of a string.
  1229. *
  1230. * TODO: This function and support for "NoXXX" should be removed starting
  1231. * with ngIRCd release 19! (One release after marking it "deprecated").
  1232. *
  1233. * @param str Pointer to input string starting with "no".
  1234. * @returns New pointer to string without "no" prefix.
  1235. */
  1236. static const char *
  1237. NoNo(const char *str)
  1238. {
  1239. assert(strncasecmp("no", str, 2) == 0 && str[2]);
  1240. return str + 2;
  1241. }
  1242. /**
  1243. * Invert "boolean" string.
  1244. *
  1245. * TODO: This function and support for "NoXXX" should be removed starting
  1246. * with ngIRCd release 19! (One release after marking it "deprecated").
  1247. *
  1248. * @param arg "Boolean" input string.
  1249. * @returns Pointer to inverted "boolean string".
  1250. */
  1251. static const char *
  1252. InvertArg(const char *arg)
  1253. {
  1254. return yesno_to_str(!Check_ArgIsTrue(arg));
  1255. }
  1256. /**
  1257. * Handle variable in [Global] configuration section.
  1258. *
  1259. * @param Line Line numer in configuration file.
  1260. * @param Var Variable name.
  1261. * @param Arg Variable argument.
  1262. */
  1263. static void
  1264. Handle_GLOBAL(const char *File, int Line, char *Var, char *Arg )
  1265. {
  1266. struct passwd *pwd;
  1267. struct group *grp;
  1268. size_t len;
  1269. const char *section;
  1270. char *ptr;
  1271. assert(File != NULL);
  1272. assert(Line > 0);
  1273. assert(Var != NULL);
  1274. assert(Arg != NULL);
  1275. if (strcasecmp(Var, "Name") == 0) {
  1276. len = strlcpy(Conf_ServerName, Arg, sizeof(Conf_ServerName));
  1277. if (len >= sizeof(Conf_ServerName))
  1278. Config_Error_TooLong(File, Line, Var);
  1279. return;
  1280. }
  1281. if (strcasecmp(Var, "AdminInfo1") == 0) {
  1282. len = strlcpy(Conf_ServerAdmin1, Arg, sizeof(Conf_ServerAdmin1));
  1283. if (len >= sizeof(Conf_ServerAdmin1))
  1284. Config_Error_TooLong(File, Line, Var);
  1285. return;
  1286. }
  1287. if (strcasecmp(Var, "AdminInfo2") == 0) {
  1288. len = strlcpy(Conf_ServerAdmin2, Arg, sizeof(Conf_ServerAdmin2));
  1289. if (len >= sizeof(Conf_ServerAdmin2))
  1290. Config_Error_TooLong(File, Line, Var);
  1291. return;
  1292. }
  1293. if (strcasecmp(Var, "AdminEMail") == 0) {
  1294. len = strlcpy(Conf_ServerAdminMail, Arg,
  1295. sizeof(Conf_ServerAdminMail));
  1296. if (len >= sizeof(Conf_ServerAdminMail))
  1297. Config_Error_TooLong(File, Line, Var);
  1298. return;
  1299. }
  1300. if (strcasecmp(Var, "Info") == 0) {
  1301. len = strlcpy(Conf_ServerInfo, Arg, sizeof(Conf_ServerInfo));
  1302. if (len >= sizeof(Conf_ServerInfo))
  1303. Config_Error_TooLong(File, Line, Var);
  1304. return;
  1305. }
  1306. if (strcasecmp(Var, "HelpFile") == 0) {
  1307. len = strlcpy(Conf_HelpFile, Arg, sizeof(Conf_HelpFile));
  1308. if (len >= sizeof(Conf_HelpFile))
  1309. Config_Error_TooLong(File, Line, Var);
  1310. return;
  1311. }
  1312. if (strcasecmp(Var, "Listen") == 0) {
  1313. if (Conf_ListenAddress) {
  1314. Config_Error(LOG_ERR,
  1315. "Multiple Listen= options, ignoring: %s",
  1316. Arg);
  1317. return;
  1318. }
  1319. Conf_ListenAddress = strdup_warn(Arg);
  1320. /* If allocation fails, we're in trouble: we cannot ignore the
  1321. * error -- otherwise ngircd would listen on all interfaces. */
  1322. if (!Conf_ListenAddress) {
  1323. Config_Error(LOG_ALERT,
  1324. "%s exiting due to fatal errors!",
  1325. PACKAGE_NAME);
  1326. exit(1);
  1327. }
  1328. return;
  1329. }
  1330. if (strcasecmp(Var, "MotdFile") == 0) {
  1331. len = strlcpy(Conf_MotdFile, Arg, sizeof(Conf_MotdFile));
  1332. if (len >= sizeof(Conf_MotdFile))
  1333. Config_Error_TooLong(File, Line, Var);
  1334. return;
  1335. }
  1336. if (strcasecmp(Var, "MotdPhrase") == 0) {
  1337. len = strlen(Arg);
  1338. if (len == 0)
  1339. return;
  1340. if (len >= 127) {
  1341. Config_Error_TooLong(File, Line, Var);
  1342. return;
  1343. }
  1344. if (!array_copyb(&Conf_Motd, Arg, len + 1))
  1345. Config_Error(LOG_WARNING,
  1346. "%s, line %d: Could not append MotdPhrase: %s",
  1347. File, Line, strerror(errno));
  1348. Using_MotdFile = false;
  1349. return;
  1350. }
  1351. if (strcasecmp(Var, "Network") == 0) {
  1352. len = strlcpy(Conf_Network, Arg, sizeof(Conf_Network));
  1353. if (len >= sizeof(Conf_Network))
  1354. Config_Error_TooLong(File, Line, Var);
  1355. ptr = strchr(Conf_Network, ' ');
  1356. if (ptr) {
  1357. Config_Error(LOG_WARNING,
  1358. "%s, line %d: \"Network\" can't contain spaces!",
  1359. File, Line);
  1360. *ptr = '\0';
  1361. }
  1362. return;
  1363. }
  1364. if(strcasecmp(Var, "Password") == 0) {
  1365. len = strlcpy(Conf_ServerPwd, Arg, sizeof(Conf_ServerPwd));
  1366. if (len >= sizeof(Conf_ServerPwd))
  1367. Config_Error_TooLong(File, Line, Var);
  1368. return;
  1369. }
  1370. if (strcasecmp(Var, "PidFile") == 0) {
  1371. len = strlcpy(Conf_PidFile, Arg, sizeof(Conf_PidFile));
  1372. if (len >= sizeof(Conf_PidFile))
  1373. Config_Error_TooLong(File, Line, Var);
  1374. return;
  1375. }
  1376. if (strcasecmp(Var, "Ports") == 0) {
  1377. ports_parse(&Conf_ListenPorts, File, Line, Arg);
  1378. return;
  1379. }
  1380. if (strcasecmp(Var, "ServerGID") == 0) {
  1381. grp = getgrnam(Arg);
  1382. if (grp)
  1383. Conf_GID = grp->gr_gid;
  1384. else {
  1385. Conf_GID = (unsigned int)atoi(Arg);
  1386. if (!Conf_GID && strcmp(Arg, "0"))
  1387. Config_Error(LOG_WARNING,
  1388. "%s, line %d: Value of \"%s\" is not a valid group name or ID!",
  1389. File, Line, Var);
  1390. }
  1391. return;
  1392. }
  1393. if (strcasecmp(Var, "ServerUID") == 0) {
  1394. pwd = getpwnam(Arg);
  1395. if (pwd)
  1396. Conf_UID = pwd->pw_uid;
  1397. else {
  1398. Conf_UID = (unsigned int)atoi(Arg);
  1399. if (!Conf_UID && strcmp(Arg, "0"))
  1400. Config_Error(LOG_WARNING,
  1401. "%s, line %d: Value of \"%s\" is not a valid user name or ID!",
  1402. File, Line, Var);
  1403. }
  1404. return;
  1405. }
  1406. if (CheckLegacyNoOption(Var, Arg)) {
  1407. /* TODO: This function and support for "NoXXX" could be
  1408. * be removed starting with ngIRCd release 19 (one release
  1409. * after marking it "deprecated"). */
  1410. Config_Error(LOG_WARNING,
  1411. "%s, line %d (section \"Global\"): \"No\"-Prefix is deprecated, use \"%s = %s\" in [Options] section!",
  1412. File, Line, NoNo(Var), InvertArg(Arg));
  1413. if (strcasecmp(Var, "NoIdent") == 0)
  1414. WarnIdent(File, Line);
  1415. else if (strcasecmp(Var, "NoPam") == 0)
  1416. WarnPAM(File, Line);
  1417. return;
  1418. }
  1419. if ((section = CheckLegacyGlobalOption(File, Line, Var, Arg))) {
  1420. /** TODO: This function and support for these options in the
  1421. * [Global] section could be removed starting with ngIRCd
  1422. * release 19 (one release after marking it "deprecated"). */
  1423. if (strncasecmp(Var, "SSL", 3) == 0) {
  1424. Config_Error(LOG_WARNING,
  1425. "%s, line %d (section \"Global\"): \"%s\" is deprecated here, move it to %s and rename to \"%s\"!",
  1426. File, Line, Var, section,
  1427. Var + 3);
  1428. } else {
  1429. Config_Error(LOG_WARNING,
  1430. "%s, line %d (section \"Global\"): \"%s\" is deprecated here, move it to %s!",
  1431. File, Line, Var, section);
  1432. }
  1433. return;
  1434. }
  1435. Config_Error_Section(File, Line, Var, "Global");
  1436. }
  1437. /**
  1438. * Handle variable in [Limits] configuration section.
  1439. *
  1440. * @param Line Line numer in configuration file.
  1441. * @param Var Variable name.
  1442. * @param Arg Variable argument.
  1443. */
  1444. static void
  1445. Handle_LIMITS(const char *File, int Line, char *Var, char *Arg)
  1446. {
  1447. assert(File != NULL);
  1448. assert(Line > 0);
  1449. assert(Var != NULL);
  1450. assert(Arg != NULL);
  1451. if (strcasecmp(Var, "ConnectRetry") == 0) {
  1452. Conf_ConnectRetry = atoi(Arg);
  1453. if (Conf_ConnectRetry < 5) {
  1454. Config_Error(LOG_WARNING,
  1455. "%s, line %d: Value of \"ConnectRetry\" too low!",
  1456. File, Line);
  1457. Conf_ConnectRetry = 5;
  1458. }
  1459. return;
  1460. }
  1461. if (strcasecmp(Var, "IdleTimeout") == 0) {
  1462. Conf_IdleTimeout = atoi(Arg);
  1463. if (!Conf_IdleTimeout && strcmp(Arg, "0"))
  1464. Config_Error_NaN(File, Line, Var);
  1465. return;
  1466. }
  1467. if (strcasecmp(Var, "MaxConnections") == 0) {
  1468. Conf_MaxConnections = atoi(Arg);
  1469. if (!Conf_MaxConnections && strcmp(Arg, "0"))
  1470. Config_Error_NaN(File, Line, Var);
  1471. return;
  1472. }
  1473. if (strcasecmp(Var, "MaxConnectionsIP") == 0) {
  1474. Conf_MaxConnectionsIP = atoi(Arg);
  1475. if (!Conf_MaxConnectionsIP && strcmp(Arg, "0"))
  1476. Config_Error_NaN(File, Line, Var);
  1477. return;
  1478. }
  1479. if (strcasecmp(Var, "MaxJoins") == 0) {
  1480. Conf_MaxJoins = atoi(Arg);
  1481. if (!Conf_MaxJoins && strcmp(Arg, "0"))
  1482. Config_Error_NaN(File, Line, Var);
  1483. return;
  1484. }
  1485. if (strcasecmp(Var, "MaxNickLength") == 0) {
  1486. Conf_MaxNickLength = Handle_MaxNickLength(File, Line, Arg);
  1487. return;
  1488. }
  1489. if (strcasecmp(Var, "MaxListSize") == 0) {
  1490. Conf_MaxListSize = atoi(Arg);
  1491. if (!Conf_MaxListSize && strcmp(Arg, "0"))
  1492. Config_Error_NaN(File, Line, Var);
  1493. return;
  1494. }
  1495. if (strcasecmp(Var, "PingTimeout") == 0) {
  1496. Conf_PingTimeout = atoi(Arg);
  1497. if (Conf_PingTimeout < 5) {
  1498. Config_Error(LOG_WARNING,
  1499. "%s, line %d: Value of \"PingTimeout\" too low!",
  1500. File, Line);
  1501. Conf_PingTimeout = 5;
  1502. }
  1503. return;
  1504. }
  1505. if (strcasecmp(Var, "PongTimeout") == 0) {
  1506. Conf_PongTimeout = atoi(Arg);
  1507. if (Conf_PongTimeout < 5) {
  1508. Config_Error(LOG_WARNING,
  1509. "%s, line %d: Value of \"PongTimeout\" too low!",
  1510. File, Line);
  1511. Conf_PongTimeout = 5;
  1512. }
  1513. return;
  1514. }
  1515. Config_Error_Section(File, Line, Var, "Limits");
  1516. }
  1517. /**
  1518. * Handle variable in [Options] configuration section.
  1519. *
  1520. * @param Line Line numer in configuration file.
  1521. * @param Var Variable name.
  1522. * @param Arg Variable argument.
  1523. */
  1524. static void
  1525. Handle_OPTIONS(const char *File, int Line, char *Var, char *Arg)
  1526. {
  1527. size_t len;
  1528. char *p;
  1529. assert(File != NULL);
  1530. assert(Line > 0);
  1531. assert(Var != NULL);
  1532. assert(Arg != NULL);
  1533. if (strcasecmp(Var, "AllowedChannelTypes") == 0) {
  1534. p = Arg;
  1535. Conf_AllowedChannelTypes[0] = '\0';
  1536. while (*p) {
  1537. if (strchr(Conf_AllowedChannelTypes, *p)) {
  1538. /* Prefix is already included; ignore it */
  1539. p++;
  1540. continue;
  1541. }
  1542. if (strchr(CHANTYPES, *p)) {
  1543. len = strlen(Conf_AllowedChannelTypes) + 1;
  1544. assert(len < sizeof(Conf_AllowedChannelTypes));
  1545. Conf_AllowedChannelTypes[len - 1] = *p;
  1546. Conf_AllowedChannelTypes[len] = '\0';
  1547. } else {
  1548. Config_Error(LOG_WARNING,
  1549. "%s, line %d: Unknown channel prefix \"%c\" in \"AllowedChannelTypes\"!",
  1550. File, Line, *p);
  1551. }
  1552. p++;
  1553. }
  1554. return;
  1555. }
  1556. if (strcasecmp(Var, "AllowRemoteOper") == 0) {
  1557. Conf_AllowRemoteOper = Check_ArgIsTrue(Arg);
  1558. return;
  1559. }
  1560. if (strcasecmp(Var, "ChrootDir") == 0) {
  1561. len = strlcpy(Conf_Chroot, Arg, sizeof(Conf_Chroot));
  1562. if (len >= sizeof(Conf_Chroot))
  1563. Config_Error_TooLong(File, Line, Var);
  1564. return;
  1565. }
  1566. if (strcasecmp(Var, "CloakHost") == 0) {
  1567. len = strlcpy(Conf_CloakHost, Arg, sizeof(Conf_CloakHost));
  1568. if (len >= sizeof(Conf_CloakHost))
  1569. Config_Error_TooLong(File, Line, Var);
  1570. return;
  1571. }
  1572. if (strcasecmp(Var, "CloakHostModeX") == 0) {
  1573. len = strlcpy(Conf_CloakHostModeX, Arg, sizeof(Conf_CloakHostModeX));
  1574. if (len >= sizeof(Conf_CloakHostModeX))
  1575. Config_Error_TooLong(File, Line, Var);
  1576. return;
  1577. }
  1578. if (strcasecmp(Var, "CloakHostSalt") == 0) {
  1579. len = strlcpy(Conf_CloakHostSalt, Arg, sizeof(Conf_CloakHostSalt));
  1580. if (len >= sizeof(Conf_CloakHostSalt))
  1581. Config_Error_TooLong(File, Line, Var);
  1582. return;
  1583. }
  1584. if (strcasecmp(Var, "CloakUserToNick") == 0) {
  1585. Conf_CloakUserToNick = Check_ArgIsTrue(Arg);
  1586. return;
  1587. }
  1588. if (strcasecmp(Var, "ConnectIPv6") == 0) {
  1589. Conf_ConnectIPv6 = Check_ArgIsTrue(Arg);
  1590. WarnIPv6(File, Line);
  1591. return;
  1592. }
  1593. if (strcasecmp(Var, "ConnectIPv4") == 0) {
  1594. Conf_ConnectIPv4 = Check_ArgIsTrue(Arg);
  1595. return;
  1596. }
  1597. if (strcasecmp(Var, "DefaultUserModes") == 0) {
  1598. p = Arg;
  1599. Conf_DefaultUserModes[0] = '\0';
  1600. while (*p) {
  1601. if (strchr(Conf_DefaultUserModes, *p)) {
  1602. /* Mode is already included; ignore it */
  1603. p++;
  1604. continue;
  1605. }
  1606. if (strchr(USERMODES, *p)) {
  1607. len = strlen(Conf_DefaultUserModes) + 1;
  1608. assert(len < sizeof(Conf_DefaultUserModes));
  1609. Conf_DefaultUserModes[len - 1] = *p;
  1610. Conf_DefaultUserModes[len] = '\0';
  1611. } else {
  1612. Config_Error(LOG_WARNING,
  1613. "%s, line %d: Unknown user mode \"%c\" in \"DefaultUserModes\"!",
  1614. File, Line, *p);
  1615. }
  1616. p++;
  1617. }
  1618. return;
  1619. }
  1620. if (strcasecmp(Var, "DNS") == 0) {
  1621. Conf_DNS = Check_ArgIsTrue(Arg);
  1622. return;
  1623. }
  1624. if (strcasecmp(Var, "Ident") == 0) {
  1625. Conf_Ident = Check_ArgIsTrue(Arg);
  1626. WarnIdent(File, Line);
  1627. return;
  1628. }
  1629. if (strcasecmp(Var, "IncludeDir") == 0) {
  1630. if (Conf_IncludeDir[0]) {
  1631. Config_Error(LOG_ERR,
  1632. "%s, line %d: Can't overwrite value of \"IncludeDir\" variable!",
  1633. File, Line);
  1634. return;
  1635. }
  1636. len = strlcpy(Conf_IncludeDir, Arg, sizeof(Conf_IncludeDir));
  1637. if (len >= sizeof(Conf_IncludeDir))
  1638. Config_Error_TooLong(File, Line, Var);
  1639. return;
  1640. }
  1641. if (strcasecmp(Var, "MorePrivacy") == 0) {
  1642. Conf_MorePrivacy = Check_ArgIsTrue(Arg);
  1643. return;
  1644. }
  1645. if (strcasecmp(Var, "NoticeAuth") == 0) {
  1646. /*
  1647. * TODO: This section and support for "NoticeAuth" variable
  1648. * could be removed starting with ngIRCd release 24 (one
  1649. * release after marking it "deprecated") ...
  1650. */
  1651. Config_Error(LOG_WARNING,
  1652. "%s, line %d (section \"Options\"): \"%s\" is deprecated, please use \"NoticeBeforeRegistration\"!",
  1653. File, Line, Var);
  1654. Conf_NoticeBeforeRegistration = Check_ArgIsTrue(Arg);
  1655. return;
  1656. }
  1657. if (strcasecmp(Var, "NoticeBeforeRegistration") == 0) {
  1658. Conf_NoticeBeforeRegistration = Check_ArgIsTrue(Arg);
  1659. return;
  1660. }
  1661. if (strcasecmp(Var, "OperCanUseMode") == 0) {
  1662. Conf_OperCanMode = Check_ArgIsTrue(Arg);
  1663. return;
  1664. }
  1665. if (strcasecmp(Var, "OperChanPAutoOp") == 0) {
  1666. Conf_OperChanPAutoOp = Check_ArgIsTrue(Arg);
  1667. return;
  1668. }
  1669. if (strcasecmp(Var, "OperServerMode") == 0) {
  1670. Conf_OperServerMode = Check_ArgIsTrue(Arg);
  1671. return;
  1672. }
  1673. if (strcasecmp(Var, "PAM") == 0) {
  1674. Conf_PAM = Check_ArgIsTrue(Arg);
  1675. WarnPAM(File, Line);
  1676. return;
  1677. }
  1678. if (strcasecmp(Var, "PAMIsOptional") == 0 ) {
  1679. Conf_PAMIsOptional = Check_ArgIsTrue(Arg);
  1680. return;
  1681. }
  1682. if (strcasecmp(Var, "PAMServiceName") == 0) {
  1683. len = strlcpy(Conf_PAMServiceName, Arg, sizeof(Conf_PAMServiceName));
  1684. if (len >= sizeof(Conf_PAMServiceName))
  1685. Config_Error_TooLong(File, Line, Var);
  1686. return;
  1687. }
  1688. if (strcasecmp(Var, "PredefChannelsOnly") == 0) {
  1689. /*
  1690. * TODO: This section and support for "PredefChannelsOnly"
  1691. * could be removed starting with ngIRCd release 22 (one
  1692. * release after marking it "deprecated") ...
  1693. */
  1694. Config_Error(LOG_WARNING,
  1695. "%s, line %d (section \"Options\"): \"%s\" is deprecated, please use \"AllowedChannelTypes\"!",
  1696. File, Line, Var);
  1697. if (Check_ArgIsTrue(Arg))
  1698. Conf_AllowedChannelTypes[0] = '\0';
  1699. else
  1700. strlcpy(Conf_AllowedChannelTypes, CHANTYPES,
  1701. sizeof(Conf_AllowedChannelTypes));
  1702. return;
  1703. }
  1704. #ifndef STRICT_RFC
  1705. if (strcasecmp(Var, "RequireAuthPing") == 0) {
  1706. Conf_AuthPing = Check_ArgIsTrue(Arg);
  1707. return;
  1708. }
  1709. #endif
  1710. if (strcasecmp(Var, "ScrubCTCP") == 0) {
  1711. Conf_ScrubCTCP = Check_ArgIsTrue(Arg);
  1712. return;
  1713. }
  1714. #ifdef SYSLOG
  1715. if (strcasecmp(Var, "SyslogFacility") == 0) {
  1716. Conf_SyslogFacility = ngt_SyslogFacilityID(Arg,
  1717. Conf_SyslogFacility);
  1718. return;
  1719. }
  1720. #endif
  1721. if (strcasecmp(Var, "WebircPassword") == 0) {
  1722. len = strlcpy(Conf_WebircPwd, Arg, sizeof(Conf_WebircPwd));
  1723. if (len >= sizeof(Conf_WebircPwd))
  1724. Config_Error_TooLong(File, Line, Var);
  1725. return;
  1726. }
  1727. Config_Error_Section(File, Line, Var, "Options");
  1728. }
  1729. #ifdef SSL_SUPPORT
  1730. /**
  1731. * Handle variable in [SSL] configuration section.
  1732. *
  1733. * @param Line Line numer in configuration file.
  1734. * @param Var Variable name.
  1735. * @param Arg Variable argument.
  1736. */
  1737. static void
  1738. Handle_SSL(const char *File, int Line, char *Var, char *Arg)
  1739. {
  1740. assert(File != NULL);
  1741. assert(Line > 0);
  1742. assert(Var != NULL);
  1743. assert(Arg != NULL);
  1744. if (strcasecmp(Var, "CertFile") == 0) {
  1745. assert(Conf_SSLOptions.CertFile == NULL);
  1746. Conf_SSLOptions.CertFile = strdup_warn(Arg);
  1747. return;
  1748. }
  1749. if (strcasecmp(Var, "DHFile") == 0) {
  1750. assert(Conf_SSLOptions.DHFile == NULL);
  1751. Conf_SSLOptions.DHFile = strdup_warn(Arg);
  1752. return;
  1753. }
  1754. if (strcasecmp(Var, "KeyFile") == 0) {
  1755. assert(Conf_SSLOptions.KeyFile == NULL);
  1756. Conf_SSLOptions.KeyFile = strdup_warn(Arg);
  1757. return;
  1758. }
  1759. if (strcasecmp(Var, "KeyFilePassword") == 0) {
  1760. assert(array_bytes(&Conf_SSLOptions.KeyFilePassword) == 0);
  1761. if (!array_copys(&Conf_SSLOptions.KeyFilePassword, Arg))
  1762. Config_Error(LOG_ERR,
  1763. "%s, line %d (section \"SSL\"): Could not copy %s: %s!",
  1764. File, Line, Var, strerror(errno));
  1765. return;
  1766. }
  1767. if (strcasecmp(Var, "Ports") == 0) {
  1768. ports_parse(&Conf_SSLOptions.ListenPorts, File, Line, Arg);
  1769. return;
  1770. }
  1771. if (strcasecmp(Var, "CipherList") == 0) {
  1772. assert(Conf_SSLOptions.CipherList == NULL);
  1773. Conf_SSLOptions.CipherList = strdup_warn(Arg);
  1774. return;
  1775. }
  1776. Config_Error_Section(File, Line, Var, "SSL");
  1777. }
  1778. #endif
  1779. /**
  1780. * Handle variable in [Operator] configuration section.
  1781. *
  1782. * @param Line Line numer in configuration file.
  1783. * @param Var Variable name.
  1784. * @param Arg Variable argument.
  1785. */
  1786. static void
  1787. Handle_OPERATOR(const char *File, int Line, char *Var, char *Arg )
  1788. {
  1789. size_t len;
  1790. struct Conf_Oper *op;
  1791. assert( File != NULL );
  1792. assert( Line > 0 );
  1793. assert( Var != NULL );
  1794. assert( Arg != NULL );
  1795. op = array_get(&Conf_Opers, sizeof(*op),
  1796. array_length(&Conf_Opers, sizeof(*op)) - 1);
  1797. if (!op)
  1798. return;
  1799. if (strcasecmp(Var, "Name") == 0) {
  1800. /* Name of IRC operator */
  1801. len = strlcpy(op->name, Arg, sizeof(op->name));
  1802. if (len >= sizeof(op->name))
  1803. Config_Error_TooLong(File, Line, Var);
  1804. return;
  1805. }
  1806. if (strcasecmp(Var, "Password") == 0) {
  1807. /* Password of IRC operator */
  1808. len = strlcpy(op->pwd, Arg, sizeof(op->pwd));
  1809. if (len >= sizeof(op->pwd))
  1810. Config_Error_TooLong(File, Line, Var);
  1811. return;
  1812. }
  1813. if (strcasecmp(Var, "Mask") == 0) {
  1814. if (op->mask)
  1815. return; /* Hostname already configured */
  1816. op->mask = strdup_warn( Arg );
  1817. return;
  1818. }
  1819. Config_Error_Section(File, Line, Var, "Operator");
  1820. }
  1821. /**
  1822. * Handle variable in [Server] configuration section.
  1823. *
  1824. * @param Line Line numer in configuration file.
  1825. * @param Var Variable name.
  1826. * @param Arg Variable argument.
  1827. */
  1828. static void
  1829. Handle_SERVER(const char *File, int Line, char *Var, char *Arg )
  1830. {
  1831. long port;
  1832. size_t len;
  1833. assert( File != NULL );
  1834. assert( Line > 0 );
  1835. assert( Var != NULL );
  1836. assert( Arg != NULL );
  1837. /* Ignore server block if no space is left in server configuration structure */
  1838. if( New_Server_Idx <= NONE ) return;
  1839. if( strcasecmp( Var, "Host" ) == 0 ) {
  1840. /* Hostname of the server */
  1841. len = strlcpy( New_Server.host, Arg, sizeof( New_Server.host ));
  1842. if (len >= sizeof( New_Server.host ))
  1843. Config_Error_TooLong(File, Line, Var);
  1844. return;
  1845. }
  1846. if( strcasecmp( Var, "Name" ) == 0 ) {
  1847. /* Name of the server ("Nick"/"ID") */
  1848. len = strlcpy( New_Server.name, Arg, sizeof( New_Server.name ));
  1849. if (len >= sizeof( New_Server.name ))
  1850. Config_Error_TooLong(File, Line, Var);
  1851. return;
  1852. }
  1853. if (strcasecmp(Var, "Bind") == 0) {
  1854. if (ng_ipaddr_init(&New_Server.bind_addr, Arg, 0))
  1855. return;
  1856. Config_Error(LOG_ERR, "%s, line %d (section \"Server\"): Can't parse IP address \"%s\"",
  1857. File, Line, Arg);
  1858. return;
  1859. }
  1860. if( strcasecmp( Var, "MyPassword" ) == 0 ) {
  1861. /* Password of this server which is sent to the peer */
  1862. if (*Arg == ':') {
  1863. Config_Error(LOG_ERR,
  1864. "%s, line %d (section \"Server\"): MyPassword must not start with ':'!",
  1865. File, Line);
  1866. }
  1867. len = strlcpy( New_Server.pwd_in, Arg, sizeof( New_Server.pwd_in ));
  1868. if (len >= sizeof( New_Server.pwd_in ))
  1869. Config_Error_TooLong(File, Line, Var);
  1870. return;
  1871. }
  1872. if( strcasecmp( Var, "PeerPassword" ) == 0 ) {
  1873. /* Passwort of the peer which must be received */
  1874. len = strlcpy( New_Server.pwd_out, Arg, sizeof( New_Server.pwd_out ));
  1875. if (len >= sizeof( New_Server.pwd_out ))
  1876. Config_Error_TooLong(File, Line, Var);
  1877. return;
  1878. }
  1879. if( strcasecmp( Var, "Port" ) == 0 ) {
  1880. /* Port to which this server should connect */
  1881. port = atol( Arg );
  1882. if (port >= 0 && port < 0xFFFF)
  1883. New_Server.port = (UINT16)port;
  1884. else
  1885. Config_Error(LOG_ERR,
  1886. "%s, line %d (section \"Server\"): Illegal port number %ld!",
  1887. File, Line, port );
  1888. return;
  1889. }
  1890. #ifdef SSL_SUPPORT
  1891. if( strcasecmp( Var, "SSLConnect" ) == 0 ) {
  1892. New_Server.SSLConnect = Check_ArgIsTrue(Arg);
  1893. return;
  1894. }
  1895. #endif
  1896. if( strcasecmp( Var, "Group" ) == 0 ) {
  1897. /* Server group */
  1898. New_Server.group = atoi( Arg );
  1899. if (!New_Server.group && strcmp(Arg, "0"))
  1900. Config_Error_NaN(File, Line, Var);
  1901. return;
  1902. }
  1903. if( strcasecmp( Var, "Passive" ) == 0 ) {
  1904. if (Check_ArgIsTrue(Arg))
  1905. New_Server.flags |= CONF_SFLAG_DISABLED;
  1906. return;
  1907. }
  1908. if (strcasecmp(Var, "ServiceMask") == 0) {
  1909. len = strlcpy(New_Server.svs_mask, ngt_LowerStr(Arg),
  1910. sizeof(New_Server.svs_mask));
  1911. if (len >= sizeof(New_Server.svs_mask))
  1912. Config_Error_TooLong(File, Line, Var);
  1913. return;
  1914. }
  1915. Config_Error_Section(File, Line, Var, "Server");
  1916. }
  1917. /**
  1918. * Copy channel name into channel structure.
  1919. *
  1920. * If the channel name is not valid because of a missing prefix ('#', '&'),
  1921. * a default prefix of '#' will be added.
  1922. *
  1923. * @param new_chan New already allocated channel structure.
  1924. * @param name Name of the new channel.
  1925. * @returns true on success, false otherwise.
  1926. */
  1927. static bool
  1928. Handle_Channelname(struct Conf_Channel *new_chan, const char *name)
  1929. {
  1930. size_t size = sizeof(new_chan->name);
  1931. char *dest = new_chan->name;
  1932. if (!Channel_IsValidName(name)) {
  1933. /*
  1934. * maybe user forgot to add a '#'.
  1935. * This is only here for user convenience.
  1936. */
  1937. *dest = '#';
  1938. --size;
  1939. ++dest;
  1940. }
  1941. return size > strlcpy(dest, name, size);
  1942. }
  1943. /**
  1944. * Handle variable in [Channel] configuration section.
  1945. *
  1946. * @param Line Line numer in configuration file.
  1947. * @param Var Variable name.
  1948. * @param Arg Variable argument.
  1949. */
  1950. static void
  1951. Handle_CHANNEL(const char *File, int Line, char *Var, char *Arg)
  1952. {
  1953. size_t len;
  1954. struct Conf_Channel *chan;
  1955. assert( File != NULL );
  1956. assert( Line > 0 );
  1957. assert( Var != NULL );
  1958. assert( Arg != NULL );
  1959. chan = array_get(&Conf_Channels, sizeof(*chan),
  1960. array_length(&Conf_Channels, sizeof(*chan)) - 1);
  1961. if (!chan)
  1962. return;
  1963. if (strcasecmp(Var, "Name") == 0) {
  1964. if (!Handle_Channelname(chan, Arg))
  1965. Config_Error_TooLong(File, Line, Var);
  1966. return;
  1967. }
  1968. if (strcasecmp(Var, "Modes") == 0) {
  1969. /* Initial modes */
  1970. len = strlcpy(chan->modes, Arg, sizeof(chan->modes));
  1971. if (len >= sizeof(chan->modes))
  1972. Config_Error_TooLong(File, Line, Var);
  1973. return;
  1974. }
  1975. if( strcasecmp( Var, "Topic" ) == 0 ) {
  1976. /* Initial topic */
  1977. len = strlcpy(chan->topic, Arg, sizeof(chan->topic));
  1978. if (len >= sizeof(chan->topic))
  1979. Config_Error_TooLong(File, Line, Var);
  1980. return;
  1981. }
  1982. if( strcasecmp( Var, "Key" ) == 0 ) {
  1983. /* Initial Channel Key (mode k) */
  1984. len = strlcpy(chan->key, Arg, sizeof(chan->key));
  1985. if (len >= sizeof(chan->key))
  1986. Config_Error_TooLong(File, Line, Var);
  1987. return;
  1988. }
  1989. if( strcasecmp( Var, "MaxUsers" ) == 0 ) {
  1990. /* maximum user limit, mode l */
  1991. chan->maxusers = (unsigned long) atol(Arg);
  1992. if (!chan->maxusers && strcmp(Arg, "0"))
  1993. Config_Error_NaN(File, Line, Var);
  1994. return;
  1995. }
  1996. if (strcasecmp(Var, "KeyFile") == 0) {
  1997. /* channel keys */
  1998. len = strlcpy(chan->keyfile, Arg, sizeof(chan->keyfile));
  1999. if (len >= sizeof(chan->keyfile))
  2000. Config_Error_TooLong(File, Line, Var);
  2001. return;
  2002. }
  2003. Config_Error_Section(File, Line, Var, "Channel");
  2004. }
  2005. /**
  2006. * Validate server configuration.
  2007. *
  2008. * Please note that this function uses exit(1) on fatal errors and therefore
  2009. * can result in ngIRCd terminating!
  2010. *
  2011. * @param Configtest true if the daemon has been called with "--configtest".
  2012. * @param Rehash true if re-reading configuration on runtime.
  2013. * @returns true if configuration is valid.
  2014. */
  2015. static bool
  2016. Validate_Config(bool Configtest, bool Rehash)
  2017. {
  2018. /* Validate configuration settings. */
  2019. #ifdef DEBUG
  2020. int i, servers, servers_once;
  2021. #endif
  2022. bool config_valid = true;
  2023. char *ptr;
  2024. /* Emit a warning when the config file is not a full path name */
  2025. if (NGIRCd_ConfFile[0] && NGIRCd_ConfFile[0] != '/') {
  2026. Config_Error(LOG_WARNING,
  2027. "Not specifying a full path name to \"%s\" can cause problems when rehashing the server!",
  2028. NGIRCd_ConfFile);
  2029. }
  2030. /* Validate configured server name, see RFC 2812 section 2.3.1 */
  2031. ptr = Conf_ServerName;
  2032. do {
  2033. if (*ptr >= 'a' && *ptr <= 'z') continue;
  2034. if (*ptr >= 'A' && *ptr <= 'Z') continue;
  2035. if (*ptr >= '0' && *ptr <= '9') continue;
  2036. if (ptr > Conf_ServerName) {
  2037. if (*ptr == '.' || *ptr == '-')
  2038. continue;
  2039. }
  2040. Conf_ServerName[0] = '\0';
  2041. break;
  2042. } while (*(++ptr));
  2043. if (!Conf_ServerName[0]) {
  2044. /* No server name configured! */
  2045. config_valid = false;
  2046. Config_Error(LOG_ALERT,
  2047. "No (valid) server name configured in \"%s\" (section 'Global': 'Name')!",
  2048. NGIRCd_ConfFile);
  2049. if (!Configtest && !Rehash) {
  2050. Config_Error(LOG_ALERT,
  2051. "%s exiting due to fatal errors!",
  2052. PACKAGE_NAME);
  2053. exit(1);
  2054. }
  2055. }
  2056. if (Conf_ServerName[0] && !strchr(Conf_ServerName, '.')) {
  2057. /* No dot in server name! */
  2058. config_valid = false;
  2059. Config_Error(LOG_ALERT,
  2060. "Invalid server name configured in \"%s\" (section 'Global': 'Name'): Dot missing!",
  2061. NGIRCd_ConfFile);
  2062. if (!Configtest) {
  2063. Config_Error(LOG_ALERT,
  2064. "%s exiting due to fatal errors!",
  2065. PACKAGE_NAME);
  2066. exit(1);
  2067. }
  2068. }
  2069. #ifdef STRICT_RFC
  2070. if (!Conf_ServerAdminMail[0]) {
  2071. /* No administrative contact configured! */
  2072. config_valid = false;
  2073. Config_Error(LOG_ALERT,
  2074. "No administrator email address configured in \"%s\" ('AdminEMail')!",
  2075. NGIRCd_ConfFile);
  2076. if (!Configtest) {
  2077. Config_Error(LOG_ALERT,
  2078. "%s exiting due to fatal errors!",
  2079. PACKAGE_NAME);
  2080. exit(1);
  2081. }
  2082. }
  2083. #endif
  2084. if (!Conf_ServerAdmin1[0] && !Conf_ServerAdmin2[0]
  2085. && !Conf_ServerAdminMail[0]) {
  2086. /* No administrative information configured! */
  2087. Config_Error(LOG_WARNING,
  2088. "No administrative information configured but required by RFC!");
  2089. }
  2090. #ifdef PAM
  2091. if (Conf_PAM && Conf_ServerPwd[0])
  2092. Config_Error(LOG_ERR,
  2093. "This server uses PAM, \"Password\" in [Global] section will be ignored!");
  2094. #endif
  2095. #ifdef DEBUG
  2096. servers = servers_once = 0;
  2097. for (i = 0; i < MAX_SERVERS; i++) {
  2098. if (Conf_Server[i].name[0]) {
  2099. servers++;
  2100. if (Conf_Server[i].flags & CONF_SFLAG_ONCE)
  2101. servers_once++;
  2102. }
  2103. }
  2104. Log(LOG_DEBUG,
  2105. "Configuration: Operators=%ld, Servers=%d[%d], Channels=%ld",
  2106. array_length(&Conf_Opers, sizeof(struct Conf_Oper)),
  2107. servers, servers_once,
  2108. array_length(&Conf_Channels, sizeof(struct Conf_Channel)));
  2109. #endif
  2110. return config_valid;
  2111. }
  2112. /**
  2113. * Output "line too long" warning.
  2114. *
  2115. * @param Line Line number in configuration file.
  2116. * @param Item Affected variable name.
  2117. */
  2118. static void
  2119. Config_Error_TooLong(const char *File, const int Line, const char *Item)
  2120. {
  2121. Config_Error(LOG_WARNING, "%s, line %d: Value of \"%s\" too long!",
  2122. File, Line, Item );
  2123. }
  2124. /**
  2125. * Output "unknown variable" warning.
  2126. *
  2127. * @param Line Line number in configuration file.
  2128. * @param Item Affected variable name.
  2129. * @param Section Section name.
  2130. */
  2131. static void
  2132. Config_Error_Section(const char *File, const int Line, const char *Item,
  2133. const char *Section)
  2134. {
  2135. Config_Error(LOG_ERR, "%s, line %d (section \"%s\"): Unknown variable \"%s\"!",
  2136. File, Line, Section, Item);
  2137. }
  2138. /**
  2139. * Output "not a number" warning.
  2140. *
  2141. * @param Line Line number in configuration file.
  2142. * @param Item Affected variable name.
  2143. */
  2144. static void
  2145. Config_Error_NaN(const char *File, const int Line, const char *Item )
  2146. {
  2147. Config_Error(LOG_WARNING, "%s, line %d: Value of \"%s\" is not a number!",
  2148. File, Line, Item );
  2149. }
  2150. /**
  2151. * Output configuration error to console and/or logfile.
  2152. *
  2153. * On runtime, the normal log functions of the daemon are used. But when
  2154. * testing the configuration ("--configtest"), all messages go directly
  2155. * to the console.
  2156. *
  2157. * @param Level Severity level of the message.
  2158. * @param Format Format string; see printf() function.
  2159. */
  2160. #ifdef PROTOTYPES
  2161. static void Config_Error( const int Level, const char *Format, ... )
  2162. #else
  2163. static void Config_Error( Level, Format, va_alist )
  2164. const int Level;
  2165. const char *Format;
  2166. va_dcl
  2167. #endif
  2168. {
  2169. char msg[MAX_LOG_MSG_LEN];
  2170. va_list ap;
  2171. assert( Format != NULL );
  2172. #ifdef PROTOTYPES
  2173. va_start( ap, Format );
  2174. #else
  2175. va_start( ap );
  2176. #endif
  2177. vsnprintf( msg, MAX_LOG_MSG_LEN, Format, ap );
  2178. va_end( ap );
  2179. if (!Use_Log) {
  2180. if (Level <= LOG_WARNING)
  2181. printf(" - %s\n", msg);
  2182. else
  2183. puts(msg);
  2184. } else
  2185. Log(Level, "%s", msg);
  2186. }
  2187. #ifdef DEBUG
  2188. /**
  2189. * Dump internal state of the "configuration module".
  2190. */
  2191. GLOBAL void
  2192. Conf_DebugDump(void)
  2193. {
  2194. int i;
  2195. Log(LOG_DEBUG, "Configured servers:");
  2196. for (i = 0; i < MAX_SERVERS; i++) {
  2197. if (! Conf_Server[i].name[0])
  2198. continue;
  2199. Log(LOG_DEBUG,
  2200. " - %s: %s:%d, last=%ld, group=%d, flags=%d, conn=%d",
  2201. Conf_Server[i].name, Conf_Server[i].host,
  2202. Conf_Server[i].port, Conf_Server[i].lasttry,
  2203. Conf_Server[i].group, Conf_Server[i].flags,
  2204. Conf_Server[i].conn_id);
  2205. }
  2206. }
  2207. #endif
  2208. /**
  2209. * Initialize server configuration structure to default values.
  2210. *
  2211. * @param Server Pointer to server structure to initialize.
  2212. */
  2213. static void
  2214. Init_Server_Struct( CONF_SERVER *Server )
  2215. {
  2216. assert( Server != NULL );
  2217. memset( Server, 0, sizeof (CONF_SERVER) );
  2218. Server->group = NONE;
  2219. Server->lasttry = time( NULL ) - Conf_ConnectRetry + STARTUP_DELAY;
  2220. if( NGIRCd_Passive ) Server->flags = CONF_SFLAG_DISABLED;
  2221. Proc_InitStruct(&Server->res_stat);
  2222. Server->conn_id = NONE;
  2223. memset(&Server->bind_addr, 0, sizeof(Server->bind_addr));
  2224. }
  2225. /* -eof- */