cfg.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913
  1. /*
  2. * config.c -- handles:
  3. * botnet config
  4. * cmdpass functions
  5. */
  6. #include "common.h"
  7. #include "cfg.h"
  8. #include "cmds.h"
  9. #include "userrec.h"
  10. #include "auth.h"
  11. #include "misc.h"
  12. #include "users.h"
  13. #include "dccutil.h"
  14. #include "botmsg.h"
  15. #include <sys/stat.h>
  16. #include <sys/types.h>
  17. #include <unistd.h>
  18. #include <fcntl.h>
  19. #include "chan.h"
  20. #include "tandem.h"
  21. #include "src/mod/channels.mod/channels.h"
  22. #include "src/mod/ctcp.mod/ctcp.h"
  23. #ifdef LEAF
  24. #include "src/chanprog.h"
  25. #include "src/mod/server.mod/server.h"
  26. #endif /* LEAF */
  27. #include "botnet.h"
  28. #include <net/if.h>
  29. #include "stat.h"
  30. int cfg_count = 0, cfg_noshare = 0;
  31. struct cfg_entry **cfg = NULL;
  32. char cmdprefix = '+'; /* This is the prefix for msg/channel cmds */
  33. struct cmd_pass *cmdpass = NULL;
  34. #ifdef HUB
  35. static void chanset_describe(struct cfg_entry * entry, int idx) {
  36. dprintf(idx, STR("chanset is a list of default options for when a channel is added. Same format as .chanset.\n"));
  37. }
  38. #endif /* HUB */
  39. #ifdef LEAF
  40. static void chanset_changed(struct cfg_entry *entry, int *valid) {
  41. if (entry->ldata)
  42. strncpyz(cfg_glob_chanset, (char *) entry->ldata, 512);
  43. else if (entry->gdata)
  44. strncpyz(cfg_glob_chanset, (char *) entry->gdata, 512);
  45. }
  46. #endif /* LEAF */
  47. struct cfg_entry CFG_CHANSET = {
  48. "chanset", CFGF_LOCAL | CFGF_GLOBAL, NULL, NULL,
  49. #ifdef LEAF
  50. chanset_changed, chanset_changed
  51. #else
  52. NULL, NULL, chanset_describe
  53. #endif /* LEAF */
  54. };
  55. #ifdef HUB
  56. static void servport_describe(struct cfg_entry * entry, int idx) {
  57. dprintf(idx, STR("servport is the default port to use for server connections.\n"));
  58. }
  59. #endif /* HUB */
  60. #ifdef LEAF
  61. static void servport_changed(struct cfg_entry *entry, int *valid) {
  62. if (entry->ldata)
  63. default_port = atoi(entry->ldata);
  64. else if (entry->gdata)
  65. default_port = atoi(entry->gdata);
  66. }
  67. #endif /* LEAF */
  68. struct cfg_entry CFG_SERVPORT = {
  69. "servport", CFGF_LOCAL | CFGF_GLOBAL, NULL, NULL,
  70. #ifdef LEAF
  71. servport_changed, servport_changed
  72. #else
  73. NULL, NULL, servport_describe
  74. #endif /* LEAF */
  75. };
  76. #ifdef HUB
  77. static void authkey_describe(struct cfg_entry *entry, int idx) {
  78. dprintf(idx,
  79. STR("authkey is used for authing, give to your users if they are to use DCC chat or IRC cmds. (can be bot specific)\n"));
  80. }
  81. #endif /* HUB */
  82. struct cfg_entry CFG_AUTHKEY = {
  83. "authkey", CFGF_LOCAL | CFGF_GLOBAL, NULL, NULL,
  84. #ifdef LEAF
  85. NULL, NULL
  86. #else
  87. NULL, NULL, authkey_describe
  88. #endif /* LEAF */
  89. };
  90. #ifdef HUB
  91. static void msgcmds_describe(struct cfg_entry *entry, int idx) {
  92. if (entry == &CFG_MSGOP)
  93. dprintf(idx, STR("msgop defines the cmd for opping via msging the bot (leave blank to disable)\n"));
  94. else if (entry == &CFG_MSGPASS)
  95. dprintf(idx, STR("msgpass defines the cmd for setting a pass via msging the bot (leave blank to disable)\n"));
  96. else if (entry == &CFG_MSGINVITE)
  97. dprintf(idx, STR("msginvite defines the cmd for requesting invite via msging the bot (leave blank to disable)\n"));
  98. else if (entry == &CFG_MSGIDENT)
  99. dprintf(idx, STR("msgident defines the cmd for identing via msging the bot (leave blank to disable)\n"));
  100. }
  101. #endif /* HUB */
  102. struct cfg_entry CFG_MSGOP = {
  103. "msgop", CFGF_LOCAL | CFGF_GLOBAL, NULL, NULL,
  104. NULL, NULL
  105. #ifdef HUB
  106. , msgcmds_describe
  107. #endif /* HUB */
  108. };
  109. struct cfg_entry CFG_MSGPASS = {
  110. "msgpass", CFGF_LOCAL | CFGF_GLOBAL, NULL, NULL,
  111. NULL, NULL
  112. #ifdef HUB
  113. , msgcmds_describe
  114. #endif /* HUB */
  115. };
  116. struct cfg_entry CFG_MSGINVITE = {
  117. "msginvite", CFGF_LOCAL | CFGF_GLOBAL, NULL, NULL,
  118. NULL, NULL
  119. #ifdef HUB
  120. , msgcmds_describe
  121. #endif /* HUB */
  122. };
  123. struct cfg_entry CFG_MSGIDENT = {
  124. "msgident", CFGF_LOCAL | CFGF_GLOBAL, NULL, NULL,
  125. NULL, NULL
  126. #ifdef HUB
  127. , msgcmds_describe
  128. #endif /* HUB */
  129. };
  130. #ifdef HUB
  131. static void cmdprefix_describe(struct cfg_entry *entry, int idx) {
  132. dprintf(idx, STR("cmdprefix is the prefix character used for msg cmds, ie: !op or .op\n"));
  133. }
  134. #endif /* HUB */
  135. #ifdef LEAF
  136. static void cmdprefix_changed(struct cfg_entry *entry, int *valid) {
  137. if (entry->ldata)
  138. cmdprefix = entry->ldata[0];
  139. else if (entry->gdata)
  140. cmdprefix = entry->gdata[0];
  141. }
  142. #endif /* LEAF */
  143. struct cfg_entry CFG_CMDPREFIX = {
  144. "cmdprefix", CFGF_LOCAL | CFGF_GLOBAL, NULL, NULL,
  145. #ifdef LEAF
  146. cmdprefix_changed, cmdprefix_changed
  147. #else
  148. NULL, NULL, cmdprefix_describe
  149. #endif /* LEAF */
  150. };
  151. void deflag_user(struct userrec *u, int why, char *msg, struct chanset_t *chan)
  152. {
  153. char tmp[256] = "", tmp2[1024] = "";
  154. struct flag_record fr = {FR_GLOBAL | FR_CHAN, 0, 0, 0 };
  155. int which = 0;
  156. if (!u)
  157. return;
  158. switch (why) {
  159. case DEFLAG_BADCOOKIE:
  160. strcpy(tmp, "Bad op cookie");
  161. which = chan->bad_cookie;
  162. break;
  163. case DEFLAG_MANUALOP:
  164. strcpy(tmp, STR("Manual op in -manop channel"));
  165. which = chan->manop;
  166. break;
  167. #ifdef G_MEAN
  168. case DEFLAG_MEAN_DEOP:
  169. strcpy(tmp, STR("Deopped bot in +mean channel"));
  170. ent = &CFG_MEANDEOP;
  171. break;
  172. case DEFLAG_MEAN_KICK:
  173. strcpy(tmp, STR("Kicked bot in +mean channel"));
  174. ent = &CFG_MEANDEOP;
  175. break;
  176. case DEFLAG_MEAN_BAN:
  177. strcpy(tmp, STR("Banned bot in +mean channel"));
  178. ent = &CFG_MEANDEOP;
  179. break;
  180. #endif /* G_MEAN */
  181. case DEFLAG_MDOP:
  182. strcpy(tmp, "Mass deop");
  183. which = chan->mdop;
  184. break;
  185. case DEFLAG_MOP:
  186. strcpy(tmp, "Mass op");
  187. which = chan->mop;
  188. break;
  189. default:
  190. sprintf(tmp, "Reason #%i", why);
  191. }
  192. if (which == P_DEOP) {
  193. putlog(LOG_WARN, "*", "Setting %s +d (%s): %s", u->handle, tmp, msg);
  194. sprintf(tmp2, "+d: %s (%s)", tmp, msg);
  195. set_user(&USERENTRY_COMMENT, u, tmp2);
  196. get_user_flagrec(u, &fr, chan->dname);
  197. fr.global = USER_DEOP;
  198. fr.chan = USER_DEOP;
  199. set_user_flagrec(u, &fr, chan->dname);
  200. } else if (which == P_KICK) {
  201. putlog(LOG_WARN, "*", "Setting %s +dk (%s): %s", u->handle, tmp, msg);
  202. sprintf(tmp2, "+dk: %s (%s)", tmp, msg);
  203. set_user(&USERENTRY_COMMENT, u, tmp2);
  204. get_user_flagrec(u, &fr, chan->dname);
  205. fr.global = USER_DEOP | USER_KICK;
  206. fr.chan = USER_DEOP | USER_KICK;
  207. set_user_flagrec(u, &fr, chan->dname);
  208. } else if (which == P_DELETE) {
  209. putlog(LOG_WARN, "*", "Deleting %s (%s): %s", u->handle, tmp, msg);
  210. deluser(u->handle);
  211. } else {
  212. putlog(LOG_WARN, "*", "No user flag effects for %s (%s): %s", u->handle, tmp, msg);
  213. sprintf(tmp2, "Warning: %s (%s)", tmp, msg);
  214. set_user(&USERENTRY_COMMENT, u, tmp2);
  215. }
  216. }
  217. #ifdef HUB
  218. static void misc_describe(struct cfg_entry *cfgent, int idx)
  219. {
  220. int i = 0;
  221. if (!strcmp(cfgent->name, "fork-interval")) {
  222. dprintf(idx, STR("fork-interval is number of seconds in between each fork() call made by the bot, to change process ID and reset cpu usage counters.\n"));
  223. i = 1;
  224. } else if (!strcmp(cfgent->name, "login")) {
  225. dprintf(idx, STR("login sets how to handle someone logging in to the shell\n"));
  226. } else if (!strcmp(cfgent->name, "trace")) {
  227. dprintf(idx, STR("trace sets how to handle someone tracing/debugging the bot\n"));
  228. } else if (!strcmp(cfgent->name, "promisc")) {
  229. dprintf(idx, STR("promisc sets how to handle when a interface is set to promiscuous mode\n"));
  230. } else if (!strcmp(cfgent->name, "bad-process")) {
  231. dprintf(idx, STR("bad-process sets how to handle when a running process not listed in process-list is detected\n"));
  232. } else if (!strcmp(cfgent->name, "process-list")) {
  233. dprintf(idx, STR("process-list is a comma-separated list of \"expected processes\" running on the bots uid\n"));
  234. i = 1;
  235. } else if (!strcmp(cfgent->name, "hijack")) {
  236. dprintf(idx, STR("hijack sets how to handle when a commonly used hijack method attempt is detected. (recommended: die)\n"));
  237. }
  238. if (!i)
  239. dprintf(idx, "Valid settings are: ignore, warn, die, reject, suicide\n");
  240. }
  241. #endif /* HUB */
  242. static void fork_lchanged(struct cfg_entry * cfgent, int * valid) {
  243. if (!cfgent->ldata)
  244. return;
  245. if (atoi(cfgent->ldata) <= 0)
  246. *valid = 0;
  247. }
  248. static void fork_gchanged(struct cfg_entry * cfgent, int * valid) {
  249. if (!cfgent->gdata)
  250. return;
  251. if (atoi(cfgent->gdata) <= 0)
  252. *valid = 0;
  253. }
  254. #ifdef HUB
  255. static void fork_describe(struct cfg_entry * cfgent, int idx) {
  256. dprintf(idx, STR("fork-interval is number of seconds in between each fork() call made by the bot, to change process ID and reset cpu usage counters.\n"));
  257. }
  258. #endif /* HUB */
  259. struct cfg_entry CFG_FORKINTERVAL = {
  260. "fork-interval", CFGF_GLOBAL | CFGF_LOCAL, NULL, NULL,
  261. fork_gchanged, fork_lchanged
  262. #ifdef HUB
  263. , fork_describe
  264. #endif /* HUB */
  265. };
  266. static void detect_lchanged(struct cfg_entry * cfgent, int * valid) {
  267. char *p = NULL;
  268. if (!(p = (char *) cfgent->ldata))
  269. *valid = 1;
  270. else if (strcmp(p, "ignore") && strcmp(p, "die") && strcmp(p, "reject") && strcmp(p, "suicide") && strcmp(p, "warn"))
  271. *valid = 0;
  272. }
  273. static void detect_gchanged(struct cfg_entry * cfgent, int * valid) {
  274. char *p = (char *) cfgent->ldata;
  275. if (!p)
  276. *valid=1;
  277. else if (strcmp(p, "ignore") && strcmp(p, "die") && strcmp(p, "reject") && strcmp(p, "suicide") && strcmp(p, "warn"))
  278. *valid=0;
  279. }
  280. struct cfg_entry CFG_LOGIN = {
  281. "login", CFGF_GLOBAL | CFGF_LOCAL, NULL, NULL,
  282. detect_gchanged, detect_lchanged
  283. #ifdef HUB
  284. , misc_describe
  285. #endif /* HUB */
  286. };
  287. struct cfg_entry CFG_HIJACK = {
  288. "hijack", CFGF_GLOBAL | CFGF_LOCAL, NULL, NULL,
  289. detect_gchanged, detect_lchanged
  290. #ifdef HUB
  291. , misc_describe
  292. #endif /* HUB */
  293. };
  294. struct cfg_entry CFG_TRACE = {
  295. "trace", CFGF_GLOBAL | CFGF_LOCAL, NULL, NULL,
  296. detect_gchanged, detect_lchanged
  297. #ifdef HUB
  298. , misc_describe
  299. #endif /* HUB */
  300. };
  301. struct cfg_entry CFG_PROMISC = {
  302. "promisc", CFGF_GLOBAL | CFGF_LOCAL, NULL, NULL,
  303. detect_gchanged, detect_lchanged
  304. #ifdef HUB
  305. , misc_describe
  306. #endif /* HUB */
  307. };
  308. struct cfg_entry CFG_BADPROCESS = {
  309. "bad-process", CFGF_GLOBAL | CFGF_LOCAL, NULL, NULL,
  310. detect_gchanged, detect_lchanged
  311. #ifdef HUB
  312. , misc_describe
  313. #endif /* HUB */
  314. };
  315. struct cfg_entry CFG_PROCESSLIST = {
  316. "process-list", CFGF_GLOBAL | CFGF_LOCAL, NULL, NULL,
  317. NULL, NULL
  318. #ifdef HUB
  319. , misc_describe
  320. #endif /* HUB */
  321. };
  322. #ifdef LEAF
  323. static void servers_changed(struct cfg_entry * entry, int * valid) {
  324. char *slist = NULL, *p = NULL;
  325. if (!strcmp(entry->name, "servers")) {
  326. if (conf.bot->host6 || conf.bot->ip6) /* we want to use the servers6 entry. */
  327. return;
  328. } else if (!strcmp(entry->name, "servers6")) {
  329. if (!conf.bot->host6 && !conf.bot->ip6) /* we probably want to use the normal server list.. */
  330. return;
  331. }
  332. slist = (char *) (entry->ldata ? entry->ldata : (entry->gdata ? entry->gdata : ""));
  333. if (serverlist) {
  334. clearq(serverlist);
  335. serverlist = NULL;
  336. }
  337. shuffle(slist, ",");
  338. p = strdup(slist);
  339. add_server(p);
  340. free(p);
  341. }
  342. #endif /* LEAF */
  343. #ifdef HUB
  344. static void servers_describe(struct cfg_entry * entry, int idx) {
  345. if (!strcmp(entry->name, "servers"))
  346. dprintf(idx, STR("servers is a comma-separated list of servers the bot will use\n"));
  347. else
  348. dprintf(idx, STR("servers6 is a comma-separated list of servers the bot will use (FOR IPv6)\n"));
  349. }
  350. #endif /* HUB */
  351. struct cfg_entry CFG_SERVERS = {
  352. "servers", CFGF_LOCAL | CFGF_GLOBAL, NULL, NULL,
  353. #ifdef LEAF
  354. servers_changed, servers_changed
  355. #else
  356. NULL, NULL, servers_describe
  357. #endif /* LEAF */
  358. };
  359. struct cfg_entry CFG_SERVERS6 = {
  360. "servers6", CFGF_LOCAL | CFGF_GLOBAL, NULL, NULL,
  361. #ifdef LEAF
  362. servers_changed, servers_changed
  363. #else
  364. NULL, NULL, servers_describe
  365. #endif /* LEAF */
  366. };
  367. #ifdef LEAF
  368. static void nick_changed(struct cfg_entry * entry, int * valid) {
  369. char *p = NULL;
  370. if (entry->ldata)
  371. p = entry->ldata;
  372. else if (entry->gdata)
  373. p = entry->gdata;
  374. else
  375. p = NULL;
  376. if (p && p[0])
  377. strncpyz(origbotname, p, NICKLEN + 1);
  378. else
  379. strncpyz(origbotname, conf.bot->nick, NICKLEN + 1);
  380. if (server_online)
  381. dprintf(DP_SERVER, "NICK %s\n", origbotname);
  382. }
  383. #endif /* LEAF */
  384. #ifdef HUB
  385. static void nick_describe(struct cfg_entry * entry, int idx) {
  386. dprintf(idx, STR("nick is the bots preferred nick when connecting/using .resetnick\n"));
  387. }
  388. #endif /* HUB */
  389. struct cfg_entry CFG_NICK = {
  390. "nick", CFGF_LOCAL | CFGF_GLOBAL, NULL, NULL,
  391. #ifdef LEAF
  392. nick_changed, nick_changed
  393. #else
  394. NULL, NULL, nick_describe
  395. #endif /* LEAF */
  396. };
  397. #ifdef HUB
  398. static void realname_describe(struct cfg_entry * entry, int idx) {
  399. dprintf(idx, STR("realname is the bots \"real name\" when connecting\n"));
  400. }
  401. #endif /* HUB */
  402. #ifdef LEAF
  403. static void realname_changed(struct cfg_entry * entry, int * valid) {
  404. if (entry->ldata)
  405. strncpyz(botrealname, (char *) entry->ldata, 121);
  406. else if (entry->gdata)
  407. strncpyz(botrealname, (char *) entry->gdata, 121);
  408. }
  409. #endif /* LEAF */
  410. struct cfg_entry CFG_REALNAME = {
  411. "realname", CFGF_LOCAL | CFGF_GLOBAL, NULL, NULL,
  412. #ifdef LEAF
  413. realname_changed, realname_changed
  414. #else
  415. NULL, NULL, realname_describe
  416. #endif /* LEAF */
  417. };
  418. #ifdef HUB
  419. static void dccauth_describe(struct cfg_entry *cfgent, int idx)
  420. {
  421. dprintf(idx, STR("dccauth is boolean (0 or 1). Set to use auth checking on dcc/telnet login.\n"));
  422. }
  423. #endif /* HUB */
  424. struct cfg_entry CFG_DCCAUTH = {
  425. "dccauth", CFGF_GLOBAL | CFGF_LOCAL, NULL, NULL,
  426. NULL, NULL
  427. #ifdef HUB
  428. , dccauth_describe
  429. #endif /* HUB */
  430. };
  431. #ifdef HUB
  432. static void getin_describe(struct cfg_entry *cfgent, int idx)
  433. {
  434. if (!strcmp(cfgent->name, "op-bots"))
  435. dprintf(idx, STR("op-bots is number of bots to ask every time a oprequest is to be made\n"));
  436. else if (!strcmp(cfgent->name, "in-bots"))
  437. dprintf(idx, STR("in-bots is number of bots to ask every time a inrequest is to be made\n"));
  438. else if (!strcmp(cfgent->name, "op-requests"))
  439. dprintf(idx, STR("op-requests (requests:seconds) limits how often the bot will ask for ops\n"));
  440. else if (!strcmp(cfgent->name, "lag-threshold"))
  441. dprintf(idx, STR("lag-threshold is maximum acceptable server lag for the bot to send/honor requests\n"));
  442. else if (!strcmp(cfgent->name, "op-time-slack"))
  443. dprintf(idx, STR("op-time-slack is number of seconds a opcookies encoded time value can be off from the bots current time\n"));
  444. else if (!strcmp(cfgent->name, "close-threshold"))
  445. dprintf(idx, STR("Format H:L. When at least H hubs but L or less leafs are linked, close all channels\n"));
  446. else if (!strcmp(cfgent->name, "kill-threshold"))
  447. dprintf(idx, STR("When more than kill-threshold bots have been killed/k-lined the last minute, channels are locked\n"));
  448. else if (!strcmp(cfgent->name, "fight-threshold"))
  449. dprintf(idx, STR("When more than fight-threshold ops/deops/kicks/bans/unbans altogether have happened on a channel in one minute, the channel is locked\n"));
  450. else {
  451. dprintf(idx, STR("No description for %s ???\n"), cfgent->name);
  452. putlog(LOG_ERRORS, "*", STR("getin_describe() called with unknown config entry %s"), cfgent->name);
  453. }
  454. }
  455. #endif /* HUB */
  456. static void getin_changed(struct cfg_entry *cfgent, int *valid)
  457. {
  458. int i;
  459. if (!cfgent->gdata)
  460. return;
  461. *valid = 0;
  462. if (!strcmp(cfgent->name, "op-requests")) {
  463. int L,
  464. R;
  465. char *value = cfgent->gdata;
  466. L = atoi(value);
  467. value = strchr(value, ':');
  468. if (!value)
  469. return;
  470. value++;
  471. R = atoi(value);
  472. if ((R >= 60) || (R <= 3) || (L < 1) || (L > R))
  473. return;
  474. *valid = 1;
  475. return;
  476. }
  477. if (!strcmp(cfgent->name, "close-threshold")) {
  478. int L, R;
  479. char *value = NULL;
  480. value = cfgent->gdata;
  481. L = atoi(value);
  482. value = strchr(value, ':');
  483. if (!value)
  484. return;
  485. value++;
  486. R = atoi(value);
  487. if ((R >= 1000) || (R < 0) || (L < 0) || (L > 100))
  488. return;
  489. *valid = 1;
  490. return;
  491. }
  492. i = atoi(cfgent->gdata);
  493. if (!strcmp(cfgent->name, "op-bots")) {
  494. if ((i < 1) || (i > 10))
  495. return;
  496. } else if (!strcmp(cfgent->name, "invite-bots")) {
  497. if ((i < 1) || (i > 10))
  498. return;
  499. } else if (!strcmp(cfgent->name, "key-bots")) {
  500. if ((i < 1) || (i > 10))
  501. return;
  502. } else if (!strcmp(cfgent->name, "limit-bots")) {
  503. if ((i < 1) || (i > 10))
  504. return;
  505. } else if (!strcmp(cfgent->name, "unban-bots")) {
  506. if ((i < 1) || (i > 10))
  507. return;
  508. } else if (!strcmp(cfgent->name, "lag-threshold")) {
  509. if ((i < 3) || (i > 60))
  510. return;
  511. } else if (!strcmp(cfgent->name, "fight-threshold")) {
  512. if (i && ((i < 50) || (i > 1000)))
  513. return;
  514. } else if (!strcmp(cfgent->name, "kill-threshold")) {
  515. if ((i < 0) || (i >= 200))
  516. return;
  517. } else if (!strcmp(cfgent->name, "op-time-slack")) {
  518. if ((i < 30) || (i > 1200))
  519. return;
  520. }
  521. *valid = 1;
  522. return;
  523. }
  524. struct cfg_entry CFG_OPBOTS = {
  525. "op-bots", CFGF_GLOBAL, NULL, NULL,
  526. getin_changed, NULL
  527. #ifdef HUB
  528. , getin_describe
  529. #endif /* HUB */
  530. };
  531. struct cfg_entry CFG_FIGHTTHRESHOLD = {
  532. "fight-threshold", CFGF_GLOBAL, NULL, NULL,
  533. getin_changed, NULL
  534. #ifdef HUB
  535. , getin_describe
  536. #endif /* HUB */
  537. };
  538. struct cfg_entry CFG_CLOSETHRESHOLD = {
  539. "close-threshold", CFGF_GLOBAL, NULL, NULL,
  540. getin_changed, NULL
  541. #ifdef HUB
  542. , getin_describe
  543. #endif /* HUB */
  544. };
  545. struct cfg_entry CFG_KILLTHRESHOLD = {
  546. "kill-threshold", CFGF_GLOBAL, NULL, NULL,
  547. getin_changed, NULL
  548. #ifdef HUB
  549. , getin_describe
  550. #endif /* HUB */
  551. };
  552. struct cfg_entry CFG_INBOTS = {
  553. "in-bots", CFGF_GLOBAL, NULL, NULL,
  554. getin_changed, NULL
  555. #ifdef HUB
  556. , getin_describe
  557. #endif /* HUB */
  558. };
  559. struct cfg_entry CFG_LAGTHRESHOLD = {
  560. "lag-threshold", CFGF_GLOBAL, NULL, NULL,
  561. getin_changed, NULL
  562. #ifdef HUB
  563. , getin_describe
  564. #endif /* HUB */
  565. };
  566. struct cfg_entry CFG_OPREQUESTS = {
  567. "op-requests", CFGF_GLOBAL, NULL, NULL,
  568. getin_changed, NULL
  569. #ifdef HUB
  570. , getin_describe
  571. #endif /* HUB */
  572. };
  573. void add_cfg(struct cfg_entry *entry)
  574. {
  575. cfg = (struct cfg_entry **) realloc(cfg, sizeof(void *) * (cfg_count + 1));
  576. cfg[cfg_count] = entry;
  577. cfg_count++;
  578. entry->ldata = NULL;
  579. entry->gdata = NULL;
  580. }
  581. static struct cfg_entry *check_can_set_cfg(char *target, char *entryname)
  582. {
  583. int i;
  584. struct userrec *u = NULL;
  585. struct cfg_entry *entry = NULL;
  586. for (i = 0; i < cfg_count; i++)
  587. if (!strcmp(cfg[i]->name, entryname)) {
  588. entry = cfg[i];
  589. break;
  590. }
  591. if (!entry)
  592. return 0;
  593. if (target) {
  594. if (!(entry->flags & CFGF_LOCAL))
  595. return 0;
  596. if (!(u = get_user_by_handle(userlist, target)))
  597. return 0;
  598. if (!u->bot)
  599. return 0;
  600. } else {
  601. if (!(entry->flags & CFGF_GLOBAL))
  602. return 0;
  603. }
  604. return entry;
  605. }
  606. void set_cfg_str(char *target, char *entryname, char *data)
  607. {
  608. struct cfg_entry *entry = NULL;
  609. if (!(entry = check_can_set_cfg(target, entryname)))
  610. return;
  611. if (data && !strcmp(data, "-"))
  612. data = NULL;
  613. if (data && (strlen(data) >= 1024))
  614. data[1023] = 0;
  615. if (target) {
  616. struct userrec *u = get_user_by_handle(userlist, target);
  617. struct xtra_key *xk = NULL;
  618. char *olddata = entry->ldata;
  619. if (u && !strcmp(conf.bot->nick, u->handle)) {
  620. if (data) {
  621. entry->ldata = strdup(data);
  622. } else
  623. entry->ldata = NULL;
  624. if (entry->localchanged) {
  625. int valid = 1;
  626. /* entry->localchanged(entry, olddata, &valid); */
  627. entry->localchanged(entry, &valid);
  628. if (!valid) {
  629. if (entry->ldata)
  630. free(entry->ldata);
  631. entry->ldata = olddata;
  632. data = olddata;
  633. olddata = NULL;
  634. }
  635. }
  636. }
  637. xk = (struct xtra_key *) calloc(1, sizeof(struct xtra_key));
  638. xk->key = strdup(entry->name);
  639. if (data) {
  640. xk->data = strdup(data);
  641. }
  642. set_user(&USERENTRY_CONFIG, u, xk);
  643. if (olddata)
  644. free(olddata);
  645. } else {
  646. char *olddata = entry->gdata;
  647. if (data) {
  648. entry->gdata = strdup(data);
  649. } else
  650. entry->gdata = NULL;
  651. if (entry->globalchanged) {
  652. int valid = 1;
  653. /* entry->globalchanged(entry, olddata, &valid); */
  654. entry->globalchanged(entry, &valid);
  655. if (!valid) {
  656. if (entry->gdata)
  657. free(entry->gdata);
  658. entry->gdata = olddata;
  659. olddata = NULL;
  660. }
  661. }
  662. if (!cfg_noshare)
  663. botnet_send_cfg_broad(-1, entry);
  664. if (olddata)
  665. free(olddata);
  666. }
  667. }
  668. struct cfg_entry CFG_MOTD = {
  669. "motd", CFGF_GLOBAL, NULL, NULL,
  670. NULL, NULL
  671. #ifdef HUB
  672. , NULL
  673. #endif /* HUB */
  674. };
  675. void init_config()
  676. {
  677. add_cfg(&CFG_AUTHKEY);
  678. add_cfg(&CFG_BADPROCESS);
  679. add_cfg(&CFG_DCCAUTH);
  680. add_cfg(&CFG_CHANSET);
  681. add_cfg(&CFG_CLOAK_SCRIPT);
  682. add_cfg(&CFG_CLOSETHRESHOLD);
  683. add_cfg(&CFG_CMDPREFIX);
  684. add_cfg(&CFG_FIGHTTHRESHOLD);
  685. add_cfg(&CFG_FORKINTERVAL);
  686. add_cfg(&CFG_HIJACK);
  687. add_cfg(&CFG_INBOTS);
  688. add_cfg(&CFG_KILLTHRESHOLD);
  689. add_cfg(&CFG_LAGTHRESHOLD);
  690. add_cfg(&CFG_LOGIN);
  691. add_cfg(&CFG_MOTD);
  692. #ifdef G_MEAN
  693. add_cfg(&CFG_MEANBAN);
  694. add_cfg(&CFG_MEANDEOP);
  695. add_cfg(&CFG_MEANKICK);
  696. #endif /* G_MEAN */
  697. add_cfg(&CFG_MSGIDENT);
  698. add_cfg(&CFG_MSGINVITE);
  699. add_cfg(&CFG_MSGOP);
  700. add_cfg(&CFG_MSGPASS);
  701. add_cfg(&CFG_NICK);
  702. add_cfg(&CFG_OPBOTS);
  703. add_cfg(&CFG_OPREQUESTS);
  704. add_cfg(&CFG_PROCESSLIST);
  705. add_cfg(&CFG_PROMISC);
  706. add_cfg(&CFG_REALNAME);
  707. add_cfg(&CFG_SERVERS);
  708. add_cfg(&CFG_SERVERS6);
  709. add_cfg(&CFG_SERVPORT);
  710. add_cfg(&CFG_TRACE);
  711. }
  712. int check_cmd_pass(const char *cmd, char *pass)
  713. {
  714. struct cmd_pass *cp = NULL;
  715. for (cp = cmdpass; cp; cp = cp->next)
  716. if (!egg_strcasecmp(cmd, cp->name)) {
  717. char tmp[32] = "";
  718. encrypt_pass(pass, tmp);
  719. if (!strcmp(tmp, cp->pass))
  720. return 1;
  721. return 0;
  722. }
  723. return 0;
  724. }
  725. int has_cmd_pass(const char *cmd)
  726. {
  727. struct cmd_pass *cp = NULL;
  728. for (cp = cmdpass; cp; cp = cp->next)
  729. if (!egg_strcasecmp(cmd, cp->name))
  730. return 1;
  731. return 0;
  732. }
  733. void set_cmd_pass(char *ln, int shareit)
  734. {
  735. struct cmd_pass *cp = NULL;
  736. char *cmd = NULL;
  737. cmd = newsplit(&ln);
  738. for (cp = cmdpass; cp; cp = cp->next)
  739. if (!strcmp(cmd, cp->name))
  740. break;
  741. if (cp)
  742. if (ln[0]) {
  743. /* change */
  744. strcpy(cp->pass, ln);
  745. if (shareit)
  746. botnet_send_cmdpass(-1, cp->name, cp->pass);
  747. } else {
  748. if (cp == cmdpass)
  749. cmdpass = cp->next;
  750. else {
  751. struct cmd_pass *cp2;
  752. cp2 = cmdpass;
  753. while (cp2->next != cp)
  754. cp2 = cp2->next;
  755. cp2->next = cp->next;
  756. }
  757. if (shareit)
  758. botnet_send_cmdpass(-1, cp->name, "");
  759. free(cp->name);
  760. free(cp);
  761. } else if (ln[0]) {
  762. /* create */
  763. cp = (struct cmd_pass *) calloc(1, sizeof(struct cmd_pass));
  764. cp->next = cmdpass;
  765. cmdpass = cp;
  766. cp->name = strdup(cmd);
  767. strcpy(cp->pass, ln);
  768. if (shareit)
  769. botnet_send_cmdpass(-1, cp->name, cp->pass);
  770. }
  771. }
  772. void userfile_cfg_line(char *ln)
  773. {
  774. char *name = NULL;
  775. int i;
  776. struct cfg_entry *cfgent = NULL;
  777. name = newsplit(&ln);
  778. for (i = 0; !cfgent && (i < cfg_count); i++)
  779. if (!strcmp(cfg[i]->name, name))
  780. cfgent = cfg[i];
  781. if (cfgent) {
  782. /* if we are a leaf, dont share the cfg line. */
  783. if (bot_hublevel(conf.bot->u) == 999)
  784. cfg_noshare = 1;
  785. set_cfg_str(NULL, cfgent->name, (ln && ln[0]) ? ln : NULL);
  786. /* regardless of leaf/hub it is safe to set this to 0 */
  787. cfg_noshare = 0;
  788. } else
  789. putlog(LOG_ERRORS, "*", "Unrecognized config entry %s in userfile", name);
  790. }
  791. void got_config_share(int idx, char *ln, int broad)
  792. {
  793. char *name = NULL;
  794. int i;
  795. struct cfg_entry *cfgent = NULL;
  796. cfg_noshare = 1;
  797. name = newsplit(&ln);
  798. for (i = 0; !cfgent && (i < cfg_count); i++)
  799. if (!strcmp(cfg[i]->name, name))
  800. cfgent = cfg[i];
  801. if (cfgent) {
  802. set_cfg_str(NULL, cfgent->name, (ln && ln[0]) ? ln : NULL);
  803. if (broad)
  804. botnet_send_cfg_broad(idx, cfgent);
  805. } else
  806. putlog(LOG_ERRORS, "*", "Unrecognized config entry %s in userfile", name);
  807. cfg_noshare = 0;
  808. }
  809. void trigger_cfg_changed()
  810. {
  811. int i;
  812. struct xtra_key *xk = NULL;
  813. for (i = 0; i < cfg_count; i++) {
  814. if (cfg[i]->flags & CFGF_LOCAL) {
  815. xk = get_user(&USERENTRY_CONFIG, conf.bot->u);
  816. while (xk && strcmp(xk->key, cfg[i]->name))
  817. xk = xk->next;
  818. if (xk) {
  819. putlog(LOG_DEBUG, "*", "trigger_cfg_changed for %s", cfg[i]->name ? cfg[i]->name : "(null)");
  820. if (!strcmp(cfg[i]->name, xk->key ? xk->key : "")) {
  821. set_cfg_str(conf.bot->nick, cfg[i]->name, xk->data);
  822. }
  823. }
  824. }
  825. }
  826. }