conf.c 64 KB

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