conf.c 52 KB

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