conf.c 53 KB

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