userent.cc 26 KB

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