conf.c 55 KB

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