userent.cc 26 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120
  1. /*
  2. * Copyright (C) 1997 Robey Pointer
  3. * Copyright (C) 1999 - 2002 Eggheads Development Team
  4. * Copyright (C) 2002 - 2014 Bryan Drewery
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License
  8. * as published by the Free Software Foundation; either version 2
  9. * of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  19. */
  20. /*
  21. * userent.c -- handles:
  22. * user-entry handling, new stylem more versatile.
  23. *
  24. */
  25. #include "common.h"
  26. #include "users.h"
  27. #include "src/mod/share.mod/share.h"
  28. #include "misc.h"
  29. #include "chanprog.h"
  30. #include "main.h"
  31. #include "debug.h"
  32. #include "userrec.h"
  33. #include "match.h"
  34. #include "dccutil.h"
  35. #include "crypt.h"
  36. #include "botmsg.h"
  37. #include "botnet.h"
  38. #include <bdlib/src/Stream.h>
  39. #include <bdlib/src/String.h>
  40. static struct user_entry_type *entry_type_list = NULL;
  41. void init_userent()
  42. {
  43. add_entry_type(&USERENTRY_COMMENT);
  44. add_entry_type(&USERENTRY_INFO);
  45. add_entry_type(&USERENTRY_LASTON);
  46. add_entry_type(&USERENTRY_BOTADDR);
  47. add_entry_type(&USERENTRY_OS);
  48. add_entry_type(&USERENTRY_NODENAME);
  49. add_entry_type(&USERENTRY_USERNAME);
  50. add_entry_type(&USERENTRY_ARCH);
  51. add_entry_type(&USERENTRY_OSVER);
  52. add_entry_type(&USERENTRY_PASS);
  53. add_entry_type(&USERENTRY_PASS1);
  54. add_entry_type(&USERENTRY_SECPASS);
  55. add_entry_type(&USERENTRY_HOSTS);
  56. add_entry_type(&USERENTRY_STATS);
  57. add_entry_type(&USERENTRY_ADDED);
  58. add_entry_type(&USERENTRY_MODIFIED);
  59. add_entry_type(&USERENTRY_SET);
  60. }
  61. void list_type_kill(struct list_type *t)
  62. {
  63. struct list_type *u = NULL;
  64. while (t) {
  65. u = t->next;
  66. free(t->extra);
  67. free(t);
  68. t = u;
  69. }
  70. }
  71. bool def_unpack(struct userrec *u, struct user_entry *e)
  72. {
  73. char *tmp = e->u.list->extra;
  74. e->u.list->extra = NULL;
  75. list_type_kill(e->u.list);
  76. e->u.string = tmp;
  77. return 1;
  78. }
  79. bool def_kill(struct user_entry *e)
  80. {
  81. free(e->u.string);
  82. free(e);
  83. return 1;
  84. }
  85. void write_userfile_protected(bd::Stream& stream, const struct userrec *u, const struct user_entry *e, int idx)
  86. {
  87. /* only write if saving local, or if sending to hub, or if sending to same user as entry */
  88. if (idx == -1 || dcc[idx].hub || dcc[idx].user == u) {
  89. stream << bd::String::printf("--%s %s\n", e->type->name, e->u.string);
  90. }
  91. }
  92. void def_write_userfile(bd::Stream& stream, const struct userrec *u, const struct user_entry *e, int idx)
  93. {
  94. stream << bd::String::printf("--%s %s\n", e->type->name, e->u.string);
  95. }
  96. void *def_get(struct userrec *u, struct user_entry *e)
  97. {
  98. return e->u.string;
  99. }
  100. bool def_set_real(struct userrec *u, struct user_entry *e, void *buf, bool protect)
  101. {
  102. char *string = (char *) buf;
  103. if (string && !string[0])
  104. string = NULL;
  105. if (!string && !e->u.string)
  106. return 1;
  107. if (string) {
  108. size_t l = strlen (string);
  109. char *i = NULL;
  110. if (l > 160)
  111. l = 160;
  112. e->u.string = (char *) realloc (e->u.string, l + 1);
  113. strlcpy (e->u.string, string, l + 1);
  114. for (i = e->u.string; *i; i++)
  115. /* Allow bold, inverse, underline, color text here...
  116. * But never add cr or lf!! --rtc
  117. */
  118. if ((unsigned int) *i < 32 && !strchr ("\002\003\026\037", *i))
  119. *i = '?';
  120. } else { /* string == NULL && e->u.string != NULL */
  121. free(e->u.string);
  122. e->u.string = NULL;
  123. }
  124. if (!noshare) {
  125. if (protect)
  126. shareout_prot(u, "c %s %s %s\n", e->type->name, u->handle, e->u.string ? e->u.string : "");
  127. else
  128. shareout("c %s %s %s\n", e->type->name, u->handle, e->u.string ? e->u.string : "");
  129. }
  130. return 1;
  131. }
  132. bool def_set(struct userrec *u, struct user_entry *e, void *buf)
  133. {
  134. return (def_set_real(u, e, buf, 0));
  135. }
  136. bool set_protected(struct userrec *u, struct user_entry *e, void *buf)
  137. {
  138. return (def_set_real(u, e, buf, 1));
  139. }
  140. bool def_gotshare(struct userrec *u, struct user_entry *e, char *data, int idx)
  141. {
  142. if (conf.bot->hub)
  143. putlog(LOG_DEBUG, "@", "%s: change %s %s", dcc[idx].nick, e->type->name, u->handle);
  144. return e->type->set(u, e, data);
  145. }
  146. void def_display(int idx, struct user_entry *e, struct userrec *u)
  147. {
  148. dprintf(idx, " %s: %s\n", e->type->name, e->u.string);
  149. }
  150. static void comment_display(int idx, struct user_entry *e, struct userrec *u)
  151. {
  152. if (dcc[idx].user && (dcc[idx].user->flags & USER_MASTER))
  153. dprintf(idx, " COMMENT: %s\n", e->u.string);
  154. }
  155. struct user_entry_type USERENTRY_COMMENT =
  156. {
  157. 0, /* always 0 ;) */
  158. def_gotshare,
  159. def_unpack,
  160. def_write_userfile,
  161. def_kill,
  162. def_get,
  163. def_set,
  164. comment_display,
  165. "COMMENT"
  166. };
  167. struct user_entry_type USERENTRY_INFO =
  168. {
  169. 0, /* always 0 ;) */
  170. def_gotshare,
  171. def_unpack,
  172. def_write_userfile,
  173. def_kill,
  174. def_get,
  175. def_set,
  176. def_display,
  177. "INFO"
  178. };
  179. static void added_display(int idx, struct user_entry *e, struct userrec *u)
  180. {
  181. /* format: unixtime handle */
  182. if (dcc[idx].user && (dcc[idx].user->flags & USER_OWNER)) {
  183. char tmp[30] = "", tmp2[70] = "", *hnd = NULL;
  184. time_t tm;
  185. strlcpy(tmp, e->u.string, sizeof(tmp));
  186. hnd = strchr(tmp, ' ');
  187. if (hnd)
  188. *hnd++ = 0;
  189. tm = atoi(tmp);
  190. strftime(tmp2, sizeof(tmp2), "%a, %d %b %Y %H:%M:%S %Z", gmtime(&tm));
  191. if (hnd)
  192. dprintf(idx, " -- Added %s by %s\n", tmp2, hnd);
  193. else
  194. dprintf(idx, " -- Added %s\n", tmp2);
  195. }
  196. }
  197. struct user_entry_type USERENTRY_ADDED = {
  198. 0, /* always 0 ;) */
  199. def_gotshare,
  200. def_unpack,
  201. def_write_userfile,
  202. def_kill,
  203. def_get,
  204. def_set,
  205. added_display,
  206. "ADDED"
  207. };
  208. static bool set_set(struct userrec *u, struct user_entry *e, void *buf)
  209. {
  210. struct xtra_key *curr = (struct xtra_key *) e->u.extra,
  211. *newxk = (struct xtra_key *) buf, *old = NULL;
  212. /* find the curr key if it exists */
  213. for (; curr; curr = curr->next) {
  214. if (curr->key && !strcasecmp(curr->key, newxk->key)) {
  215. old = curr;
  216. break;
  217. }
  218. }
  219. /* we will possibly free new below, so let's send the information to the botnet now */
  220. if (!noshare && !set_noshare) {
  221. /* Always share groups to all bots. */
  222. if (!strcmp(newxk->key, "groups")) {
  223. shareout("c %s %s %s %s\n", e->type->name, u->handle, newxk->key, newxk->data ? newxk->data : "");
  224. } else {
  225. /* only share to pertinent bots */
  226. shareout_prot(u, "c %s %s %s %s\n", e->type->name, u->handle, newxk->key, newxk->data ? newxk->data : "");
  227. }
  228. }
  229. /* unset and bail out if the new data is empty and the old doesn't exist, why'd we even get this change? */
  230. if (!old && (!newxk->data || !newxk->data[0])) {
  231. /* or simply ... delete non-existant entry */
  232. free(newxk->key);
  233. free(newxk->data);
  234. free(newxk);
  235. return 1;
  236. }
  237. /* if we have a new entry and an old entry.. or our new entry is empty -> clear out the old entry */
  238. if ((old && old != newxk) || !newxk->data || !newxk->data[0]) {
  239. list_delete((struct list_type **) (&e->u.extra), (struct list_type *) old);
  240. free(old->key);
  241. free(old->data);
  242. free(old);
  243. }
  244. /* add the new entry if it's not empty */
  245. if (old != newxk && newxk->data) {
  246. if (newxk->data[0]) {
  247. list_insert((struct xtra_key **) (&e->u.extra), newxk);
  248. } else {
  249. free(newxk->data);
  250. free(newxk->key);
  251. free(newxk);
  252. }
  253. }
  254. return 1;
  255. }
  256. static bool set_unpack(struct userrec *u, struct user_entry *e)
  257. {
  258. struct list_type *curr = NULL, *head = NULL;
  259. head = curr = e->u.list;
  260. e->u.extra = NULL;
  261. struct xtra_key *t = NULL;
  262. char *key = NULL, *data = NULL;
  263. while (curr) {
  264. t = (struct xtra_key *) calloc(1, sizeof(struct xtra_key));
  265. data = curr->extra;
  266. key = newsplit(&data);
  267. if (data[0]) {
  268. t->key = strdup(key);
  269. t->data = strdup(data);
  270. list_insert((struct xtra_key **) (&e->u.extra), t);
  271. } else
  272. free(t);
  273. curr = curr->next;
  274. }
  275. list_type_kill(head);
  276. return 1;
  277. }
  278. static void set_display(int idx, struct user_entry *e, struct userrec *u)
  279. {
  280. if (conf.bot->hub) {
  281. struct xtra_key *xk = (struct xtra_key *) e->u.extra;
  282. struct flag_record fr = {FR_GLOBAL, 0, 0, 0 };
  283. dprintf(idx, " BOTSET:\n");
  284. get_user_flagrec(dcc[idx].user, &fr, NULL);
  285. /* scan thru xtra field, searching for matches */
  286. for (; xk; xk = xk->next) {
  287. /* ok, it's a valid xtra field entry */
  288. if (glob_owner(fr))
  289. dprintf(idx, " %s: %s\n", xk->key, xk->data ? xk->data : "");
  290. }
  291. }
  292. }
  293. static bool set_gotshare(struct userrec *u, struct user_entry *e, char *buf, int idx)
  294. {
  295. char *name = newsplit(&buf);
  296. if (!name || !name[0])
  297. return 1;
  298. if (!strcasecmp(u->handle, conf.bot->nick)) {
  299. set_noshare = 1;
  300. /* This will also call set_user(). */
  301. var_set_by_name(conf.bot->nick, name, buf[0] ? buf : NULL);
  302. set_noshare = 0;
  303. } else if (conf.bot->hub || conf.bot->localhub || !strcmp(name, "groups")) {
  304. var_set_userentry(u->handle, name, buf);
  305. }
  306. return 1;
  307. }
  308. static void set_write_userfile(bd::Stream& stream, const struct userrec *u, const struct user_entry *e, int idx)
  309. {
  310. int localhub = nextbot(u->handle);
  311. struct xtra_key *x = (struct xtra_key *) e->u.extra;
  312. for (; x; x = x->next) {
  313. /*
  314. * only write if saving local, or if sending to hub, or if sending to
  315. * same user as entry, or they are not connected,
  316. * or the localhub in the chain, or sending 'groups'.
  317. * SA shareout_prot
  318. */
  319. if (idx == -1 || dcc[idx].hub || dcc[idx].user == u ||
  320. (localhub == -1 || idx == localhub) || !strcmp(x->key, "groups")) {
  321. stream << bd::String::printf("--%s %s %s\n", e->type->name, x->key, x->data ? x->data : "");
  322. }
  323. }
  324. }
  325. static bool set_kill(struct user_entry *e)
  326. {
  327. struct xtra_key *x = (struct xtra_key *) e->u.extra, *y = NULL;
  328. for (; x; x = y) {
  329. y = x->next;
  330. free(x->key);
  331. free(x->data);
  332. free(x);
  333. }
  334. free(e);
  335. return 1;
  336. }
  337. struct user_entry_type USERENTRY_SET = {
  338. 0,
  339. set_gotshare,
  340. set_unpack,
  341. set_write_userfile,
  342. set_kill,
  343. def_get,
  344. set_set,
  345. set_display,
  346. "SET"
  347. };
  348. static void botmisc_display(int idx, struct user_entry *e, struct userrec *u)
  349. {
  350. if (conf.bot->hub) {
  351. struct flag_record fr = {FR_GLOBAL, 0, 0, 0 };
  352. get_user_flagrec(dcc[idx].user, &fr, NULL);
  353. if (glob_admin(fr))
  354. dprintf(idx, " %s: %s\n", e->type->name, e->u.string ? e->u.string : "");
  355. }
  356. }
  357. struct user_entry_type USERENTRY_USERNAME = {
  358. 0,
  359. def_gotshare,
  360. def_unpack,
  361. write_userfile_protected,
  362. def_kill,
  363. def_get,
  364. set_protected,
  365. botmisc_display,
  366. "USERNAME"
  367. };
  368. struct user_entry_type USERENTRY_NODENAME = {
  369. 0,
  370. def_gotshare,
  371. def_unpack,
  372. write_userfile_protected,
  373. def_kill,
  374. def_get,
  375. set_protected,
  376. botmisc_display,
  377. "NODENAME"
  378. };
  379. struct user_entry_type USERENTRY_OS = {
  380. 0,
  381. def_gotshare,
  382. def_unpack,
  383. write_userfile_protected,
  384. def_kill,
  385. def_get,
  386. set_protected,
  387. botmisc_display,
  388. "OS"
  389. };
  390. struct user_entry_type USERENTRY_OSVER = {
  391. 0,
  392. def_gotshare,
  393. def_unpack,
  394. write_userfile_protected,
  395. def_kill,
  396. def_get,
  397. set_protected,
  398. botmisc_display,
  399. "OSVER"
  400. };
  401. struct user_entry_type USERENTRY_ARCH = {
  402. 0,
  403. def_gotshare,
  404. def_unpack,
  405. write_userfile_protected,
  406. def_kill,
  407. def_get,
  408. set_protected,
  409. botmisc_display,
  410. "ARCH"
  411. };
  412. void stats_add(struct userrec *u, int islogin, int op)
  413. {
  414. if (!u || u->bot)
  415. return;
  416. char *s = (char *) get_user(&USERENTRY_STATS, u), s2[50] = "";
  417. int sl, so;
  418. if (s) {
  419. strlcpy(s2, s, sizeof(s2));
  420. } else
  421. strlcpy(s2, "0 0", sizeof(s2));
  422. s = strchr(s2, ' ');
  423. if (s) {
  424. s++;
  425. so = atoi(s);
  426. } else
  427. so = 0;
  428. sl = atoi(s2);
  429. if (islogin)
  430. sl++;
  431. if (op)
  432. so++;
  433. simple_snprintf(s2, sizeof(s2), "%i %i", sl, so);
  434. set_user(&USERENTRY_STATS, u, s2);
  435. }
  436. char s2_8[3] = "",s1_14[3] = "",s2_1[3] = "";
  437. static void stats_display(int idx, struct user_entry *e, struct userrec *u)
  438. {
  439. /* format: logincount opcount */
  440. if (!u->bot && dcc[idx].user && (dcc[idx].user->flags & USER_OWNER)) {
  441. char *p = strchr(e->u.string, ' ');
  442. if (p)
  443. dprintf(idx, " -- %i logins, %i ops\n", atoi(e->u.string), atoi(p));
  444. }
  445. }
  446. struct user_entry_type USERENTRY_STATS = {
  447. 0, /* always 0 ;) */
  448. def_gotshare,
  449. def_unpack,
  450. def_write_userfile,
  451. def_kill,
  452. def_get,
  453. def_set,
  454. stats_display,
  455. "STATS"
  456. };
  457. void update_mod(char *handle, char *nick, char *cmd, char *par)
  458. {
  459. char tmp[100] = "";
  460. simple_snprintf(tmp, sizeof tmp, "%li, %s (%s %s)", (long) now, nick, cmd, (par && par[0]) ? par : "");
  461. set_user(&USERENTRY_MODIFIED, get_user_by_handle(userlist, handle), tmp);
  462. }
  463. static void modified_display(int idx, struct user_entry *e, struct userrec *u)
  464. {
  465. if (e && dcc[idx].user && (dcc[idx].user->flags & USER_MASTER)) {
  466. char tmp[1024] = "", tmp2[1024] = "", *hnd = NULL;
  467. time_t tm;
  468. strlcpy(tmp, e->u.string, sizeof(tmp));
  469. hnd = strchr(tmp, ' ');
  470. if (hnd)
  471. *hnd++ = 0;
  472. tm = atoi(tmp);
  473. strftime(tmp2, sizeof(tmp2), "%a, %d %b %Y %H:%M:%S %Z", gmtime(&tm));
  474. if (hnd)
  475. dprintf(idx, " -- Modified %s by %s\n", tmp2, hnd);
  476. else
  477. dprintf(idx, " -- Modified %s\n", tmp2);
  478. }
  479. }
  480. struct user_entry_type USERENTRY_MODIFIED =
  481. {
  482. 0,
  483. def_gotshare,
  484. def_unpack,
  485. def_write_userfile,
  486. def_kill,
  487. def_get,
  488. def_set,
  489. modified_display,
  490. "MODIFIED"
  491. };
  492. static bool pass_set(struct userrec *u, struct user_entry *e, void *buf)
  493. {
  494. char *newpass = NULL;
  495. char *pass = (char *) buf;
  496. free(e->u.extra);
  497. if (!pass || !pass[0] || (pass[0] == '-'))
  498. e->u.extra = NULL;
  499. else {
  500. unsigned char *p = (unsigned char *) pass;
  501. if (u->bot || (pass[0] == '+'))
  502. newpass = strdup(pass);
  503. else {
  504. while (*p) {
  505. if ((*p <= 32) || (*p == 127))
  506. *p = '?';
  507. p++;
  508. }
  509. newpass = salted_sha1(pass);
  510. }
  511. e->u.extra = strdup(newpass);
  512. }
  513. if (!noshare)
  514. shareout("c %s %s %s\n", e->type->name, u->handle, newpass ? newpass : "");
  515. free(newpass);
  516. /* clear old records */
  517. noshare = 1;
  518. set_user(&USERENTRY_PASS1, u, NULL);
  519. noshare = 0;
  520. return 1;
  521. }
  522. struct user_entry_type USERENTRY_PASS =
  523. {
  524. 0,
  525. def_gotshare,
  526. def_unpack,
  527. def_write_userfile,
  528. def_kill,
  529. def_get,
  530. pass_set,
  531. NULL,
  532. "PASS2"
  533. };
  534. static bool pass1_set(struct userrec *u, struct user_entry *e, void *buf)
  535. {
  536. char *pass = (char *) buf;
  537. free(e->u.extra);
  538. if (!pass || !pass[0] || (pass[0] == '-'))
  539. e->u.extra = NULL;
  540. else {
  541. unsigned char *p = (unsigned char *) pass;
  542. while (*p) {
  543. if ((*p <= 32) || (*p == 127))
  544. *p = '?';
  545. p++;
  546. }
  547. if (u->bot || (pass[0] == '+'))
  548. e->u.extra = strdup(pass);
  549. else
  550. e->u.extra = encrypt_string(u->handle, pass);
  551. }
  552. if (!noshare)
  553. shareout("c %s %s %s\n", e->type->name, u->handle, pass ? pass : "");
  554. return 1;
  555. }
  556. struct user_entry_type USERENTRY_PASS1 =
  557. {
  558. 0,
  559. def_gotshare,
  560. def_unpack,
  561. def_write_userfile,
  562. def_kill,
  563. def_get,
  564. pass1_set,
  565. NULL,
  566. "PASS1"
  567. };
  568. static void secpass_display(int idx, struct user_entry *e, struct userrec *u)
  569. {
  570. struct flag_record fr = {FR_GLOBAL, 0, 0, 0 };
  571. get_user_flagrec(dcc[idx].user, &fr, NULL);
  572. if (!strcmp(u->handle, dcc[idx].nick) || (glob_admin(fr) && isowner(dcc[idx].nick))) {
  573. if (conf.bot->hub)
  574. dprintf(idx, " %s: %s\n", e->type->name, e->u.string);
  575. else {
  576. dprintf(idx, " %s: Hidden on leaf bots.", e->type->name);
  577. if (dcc[idx].u.chat->su_nick)
  578. dprintf(idx, " Nice try, %s.", dcc[idx].u.chat->su_nick);
  579. dprintf(idx, "\n");
  580. }
  581. }
  582. }
  583. struct user_entry_type USERENTRY_SECPASS =
  584. {
  585. 0,
  586. def_gotshare,
  587. def_unpack,
  588. def_write_userfile,
  589. def_kill,
  590. def_get,
  591. def_set,
  592. secpass_display,
  593. "SECPASS"
  594. };
  595. static bool laston_unpack(struct userrec *u, struct user_entry *e)
  596. {
  597. char *par = e->u.list->extra, *arg = newsplit(&par);
  598. struct laston_info *li = (struct laston_info *) calloc(1, sizeof(struct laston_info));
  599. if (!par[0])
  600. par = "???";
  601. li->laston = atoi(arg);
  602. li->lastonplace = strdup(par);
  603. list_type_kill(e->u.list);
  604. e->u.extra = li;
  605. return 1;
  606. }
  607. static void laston_write_userfile(bd::Stream& stream, const struct userrec *u, const struct user_entry *e, int idx)
  608. {
  609. struct laston_info *li = (struct laston_info *) e->u.extra;
  610. stream << bd::String::printf("--LASTON %li %s\n", (long) li->laston, li->lastonplace ? li->lastonplace : "");
  611. }
  612. static bool laston_kill(struct user_entry *e)
  613. {
  614. free(((struct laston_info *) (e->u.extra))->lastonplace);
  615. free(e->u.extra);
  616. free(e);
  617. return 1;
  618. }
  619. static bool laston_set(struct userrec *u, struct user_entry *e, void *buf)
  620. {
  621. struct laston_info *li = (struct laston_info *) e->u.extra;
  622. if (li != buf) {
  623. if (li) {
  624. free(li->lastonplace);
  625. free(li);
  626. }
  627. li = (struct laston_info *) buf;
  628. e->u.extra = (struct laston_info *) buf;
  629. }
  630. /* FIXME: laston sharing is disabled until a better solution is found
  631. if (!noshare)
  632. shareout("c LASTON %s %s %li\n", u->handle, li->lastonplace ? li->lastonplace : "-", (long) li->laston);
  633. */
  634. return 1;
  635. }
  636. static bool laston_gotshare(struct userrec *u, struct user_entry *e, char *par, int idx)
  637. {
  638. char *where = NULL;
  639. time_t timeval = 0;
  640. if (par[0])
  641. where = newsplit(&par);
  642. if (where && !strcmp(where, "-"))
  643. where = NULL;
  644. if (par[0])
  645. timeval = atol(newsplit(&par));
  646. touch_laston(u, where, timeval);
  647. return 1;
  648. }
  649. struct user_entry_type USERENTRY_LASTON =
  650. {
  651. 0, /* always 0 ;) */
  652. laston_gotshare,
  653. laston_unpack,
  654. laston_write_userfile,
  655. laston_kill,
  656. def_get,
  657. laston_set,
  658. 0,
  659. "LASTON"
  660. };
  661. static bool botaddr_unpack(struct userrec *u, struct user_entry *e)
  662. {
  663. char p[1024] = "", *q1 = NULL, *q2 = NULL;
  664. struct bot_addr *bi = (struct bot_addr *) calloc(1, sizeof(struct bot_addr));
  665. /* address:port/port:hublevel:uplink */
  666. strlcpy(p, e->u.list->extra, sizeof(p));
  667. q1 = strchr(p, ':');
  668. if (q1)
  669. *q1++ = 0;
  670. bi->address = strdup(p);
  671. if (q1) {
  672. q2 = strchr(q1, ':');
  673. if (q2)
  674. *q2++ = 0;
  675. bi->telnet_port = atoi(q1);
  676. q1 = strchr(q1, '/');
  677. if (q1) {
  678. q1++;
  679. bi->relay_port = atoi(q1);
  680. }
  681. if (q2) {
  682. q1 = strchr(q2, ':');
  683. if (q1) {
  684. *q1++ = 0;
  685. bi->uplink = strdup(q1);
  686. }
  687. bi->hublevel = atoi(q2);
  688. }
  689. }
  690. if (!bi->telnet_port)
  691. bi->telnet_port = 3333;
  692. if (!bi->relay_port)
  693. bi->relay_port = bi->telnet_port;
  694. if (!bi->uplink) {
  695. bi->uplink = (char *) calloc(1, 1);
  696. }
  697. list_type_kill(e->u.list);
  698. e->u.extra = bi;
  699. return 1;
  700. }
  701. static bool botaddr_kill(struct user_entry *e)
  702. {
  703. free(((struct bot_addr *) (e->u.extra))->address);
  704. free(((struct bot_addr *) (e->u.extra))->uplink);
  705. free(e->u.extra);
  706. free(e);
  707. return 1;
  708. }
  709. static void botaddr_write_userfile(bd::Stream& stream, const struct userrec *u, const struct user_entry *e, int idx)
  710. {
  711. struct bot_addr *bi = (struct bot_addr *) e->u.extra;
  712. stream << bd::String::printf("--%s %s:%u/%u:%u:%s\n", e->type->name, bi->address, bi->telnet_port, bi->relay_port, bi->hublevel, bi->uplink);
  713. }
  714. static bool botaddr_set(struct userrec *u, struct user_entry *e, void *buf)
  715. {
  716. struct bot_addr *bi = (struct bot_addr *) e->u.extra;
  717. if (!bi && !buf)
  718. return 1;
  719. if (bi != buf) {
  720. if (bi) {
  721. free(bi->address);
  722. free(bi->uplink);
  723. free(bi);
  724. }
  725. ContextNote("botaddr_set", "(sharebug) occurred in botaddr_set");
  726. bi = (struct bot_addr *) buf;
  727. e->u.extra = (struct bot_addr *) buf;
  728. }
  729. if (bi && !noshare) {
  730. shareout("c BOTADDR %s %s %d %d %d %s\n",u->handle,
  731. (bi->address && bi->address[0]) ? bi->address : "127.0.0.1",
  732. bi->telnet_port, bi->relay_port, bi->hublevel, bi->uplink);
  733. }
  734. return 1;
  735. }
  736. static void botaddr_display(int idx, struct user_entry *e, struct userrec *u)
  737. {
  738. if (conf.bot->hub) {
  739. struct flag_record fr = {FR_GLOBAL, 0, 0, 0 };
  740. get_user_flagrec(dcc[idx].user, &fr, NULL);
  741. if (glob_admin(fr)) {
  742. struct bot_addr *bi = (struct bot_addr *) e->u.extra;
  743. if (bi->hublevel && bi->hublevel != 999) {
  744. if (bi->address) {
  745. dprintf(idx, " ADDRESS: %.70s\n", bi->address);
  746. dprintf(idx, " port: %d\n", bi->telnet_port);
  747. }
  748. dprintf(idx, " HUBLEVEL: %d\n", bi->hublevel);
  749. }
  750. if (bi->uplink && bi->uplink[0])
  751. dprintf(idx, " UPLINK: %s\n", bi->uplink);
  752. }
  753. }
  754. }
  755. static bool botaddr_gotshare(struct userrec *u, struct user_entry *e, char *buf, int idx)
  756. {
  757. struct bot_addr *bi = (struct bot_addr *) calloc(1, sizeof(struct bot_addr));
  758. char *arg = newsplit(&buf);
  759. bi->address = strdup(arg);
  760. arg = newsplit(&buf);
  761. bi->telnet_port = atoi(arg);
  762. arg = newsplit(&buf);
  763. bi->relay_port = atoi(arg);
  764. arg = newsplit(&buf);
  765. bi->hublevel = atoi(arg);
  766. bi->uplink = strdup(buf);
  767. if (!bi->telnet_port)
  768. bi->telnet_port = 3333;
  769. if (!bi->relay_port)
  770. bi->relay_port = bi->telnet_port;
  771. return botaddr_set(u, e, bi);
  772. }
  773. struct user_entry_type USERENTRY_BOTADDR =
  774. {
  775. 0, /* always 0 ;) */
  776. botaddr_gotshare,
  777. botaddr_unpack,
  778. botaddr_write_userfile,
  779. botaddr_kill,
  780. def_get,
  781. botaddr_set,
  782. botaddr_display,
  783. "BOTADDR"
  784. };
  785. static void hosts_write_userfile(bd::Stream& stream, const struct userrec *u, const struct user_entry *e, int idx)
  786. {
  787. struct list_type *h = NULL;
  788. for (h = (struct list_type *) e->u.extra; h; h = h->next)
  789. stream << bd::String::printf("--HOSTS %s\n", h->extra);
  790. }
  791. static bool hosts_null(struct userrec *u, struct user_entry *e)
  792. {
  793. return 1;
  794. }
  795. static bool hosts_kill(struct user_entry *e)
  796. {
  797. list_type_kill(e->u.list);
  798. free(e);
  799. return 1;
  800. }
  801. static void hosts_display(int idx, struct user_entry *e, struct userrec *u)
  802. {
  803. /* if this is a su, dont show hosts
  804. * otherwise, let users see their own hosts */
  805. if (conf.bot->hub ||
  806. (!conf.bot->hub && (dcc[idx].simul || (!strcmp(u->handle,dcc[idx].nick) && !dcc[idx].u.chat->su_nick)))) {
  807. char s[1024] = "";
  808. struct list_type *q = NULL;
  809. strlcpy(s, " HOSTS: ", sizeof(s));
  810. for (q = e->u.list; q; q = q->next) {
  811. if (s[0] && !s[9])
  812. strlcat(s, q->extra, sizeof(s));
  813. else if (!s[0])
  814. simple_snprintf(s, sizeof(s), " %s", q->extra);
  815. else {
  816. if (strlen(s) + strlen(q->extra) + 2 > 65) {
  817. dprintf(idx, "%s\n", s);
  818. simple_snprintf(s, sizeof(s), " %s", q->extra);
  819. } else {
  820. strlcat(s, ", ", sizeof(s));
  821. strlcat(s, q->extra, sizeof(s));
  822. }
  823. }
  824. }
  825. if (s[0])
  826. dprintf(idx, "%s\n", s);
  827. } else if (!conf.bot->hub) {
  828. dprintf(idx, " HOSTS: Hidden on leaf bots.");
  829. if (dcc[idx].u.chat->su_nick)
  830. dprintf(idx, " Nice try, %s.", dcc[idx].u.chat->su_nick);
  831. dprintf(idx, "\n");
  832. }
  833. }
  834. static bool hosts_set(struct userrec *u, struct user_entry *e, void *buf)
  835. {
  836. if (!buf || !strcasecmp((const char *) buf, "none")) {
  837. /* When the bot crashes, it's in this part, not in the 'else' part */
  838. list_type_kill(e->u.list);
  839. e->u.list = NULL;
  840. } else {
  841. char *host = (char *) buf, *p = strchr(host, ',');
  842. struct list_type **t;
  843. /* Can't have ,'s in hostmasks */
  844. while (p) {
  845. *p = '?';
  846. p = strchr(host, ',');
  847. }
  848. /* fred1: check for redundant hostmasks with
  849. * controversial "superpenis" algorithm ;) */
  850. /* I'm surprised Raistlin hasn't gotten involved in this controversy */
  851. t = &(e->u.list);
  852. while (*t) {
  853. if (wild_match(host, (*t)->extra)) {
  854. struct list_type *listu;
  855. listu = *t;
  856. *t = (*t)->next;
  857. free(listu->extra);
  858. free(listu);
  859. } else
  860. t = &((*t)->next);
  861. }
  862. *t = (struct list_type *) calloc(1, sizeof(struct list_type));
  863. (*t)->next = NULL;
  864. (*t)->extra = strdup(host);
  865. }
  866. return 1;
  867. }
  868. static bool hosts_gotshare(struct userrec *u, struct user_entry *e, char *buf, int idx)
  869. {
  870. /* doh, try to be too clever and it bites your butt */
  871. return 0;
  872. }
  873. struct user_entry_type USERENTRY_HOSTS =
  874. {
  875. 0,
  876. hosts_gotshare,
  877. hosts_null,
  878. hosts_write_userfile,
  879. hosts_kill,
  880. def_get,
  881. hosts_set,
  882. hosts_display,
  883. "HOSTS"
  884. };
  885. bool list_append(struct list_type **h, struct list_type *i)
  886. {
  887. for (; *h; h = &((*h)->next))
  888. ;
  889. *h = i;
  890. return 1;
  891. }
  892. bool list_delete(struct list_type **h, struct list_type *i)
  893. {
  894. for (; *h; h = &((*h)->next))
  895. if (*h == i) {
  896. *h = i->next;
  897. return 1;
  898. }
  899. return 0;
  900. }
  901. bool list_contains(struct list_type *h, struct list_type *i)
  902. {
  903. for (; h; h = h->next)
  904. if (h == i) {
  905. return 1;
  906. }
  907. return 0;
  908. }
  909. bool add_entry_type(struct user_entry_type *type)
  910. {
  911. struct userrec *u = NULL;
  912. list_insert((&entry_type_list), type);
  913. for (u = userlist; u; u = u->next) {
  914. struct user_entry *e = find_user_entry(type, u);
  915. if (e && e->name) {
  916. e->type = type;
  917. e->type->unpack(u, e);
  918. free(e->name);
  919. e->name = NULL;
  920. }
  921. }
  922. return 1;
  923. }
  924. struct user_entry_type *find_entry_type(char *name)
  925. {
  926. struct user_entry_type *p = NULL;
  927. for (p = entry_type_list; p; p = p->next) {
  928. if (!strcasecmp(name, p->name))
  929. return p;
  930. }
  931. return NULL;
  932. }
  933. struct user_entry *find_user_entry(struct user_entry_type *et, struct userrec *u)
  934. {
  935. struct user_entry **e = NULL, *t = NULL;
  936. for (e = &(u->entries); *e; e = &((*e)->next)) {
  937. if (((*e)->type == et) || ((*e)->name && !strcasecmp((*e)->name, et->name))) {
  938. t = *e;
  939. *e = t->next;
  940. t->next = u->entries;
  941. u->entries = t;
  942. return t;
  943. }
  944. }
  945. return NULL;
  946. }
  947. void *get_user(struct user_entry_type *et, struct userrec *u)
  948. {
  949. struct user_entry *e = NULL;
  950. if (u && (e = find_user_entry(et, u)))
  951. return et->get(u, e);
  952. return NULL;
  953. }
  954. bool set_user(struct user_entry_type *et, struct userrec *u, void *d)
  955. {
  956. if (!u || !et)
  957. return 0;
  958. struct user_entry *e = NULL;
  959. bool r;
  960. if (!(e = find_user_entry(et, u))) {
  961. e = (struct user_entry *) calloc(1, sizeof(struct user_entry));
  962. e->type = et;
  963. e->name = NULL;
  964. e->u.list = NULL;
  965. list_insert((&(u->entries)), e);
  966. }
  967. r = et->set(u, e, d);
  968. if (!e->u.list) {
  969. list_delete((struct list_type **) &(u->entries), (struct list_type *) e);
  970. free(e);
  971. }
  972. return r;
  973. }
  974. /* vim: set sts=2 sw=2 ts=8 et: */