userent.cc 26 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132
  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. 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. 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(char *handle, char *nick, char *cmd, char *par)
  493. {
  494. char tmp[100] = "";
  495. simple_snprintf(tmp, sizeof tmp, "%li, %s (%s %s)", (long) now, nick, cmd, (par && par[0]) ? par : "");
  496. set_user(&USERENTRY_MODIFIED, get_user_by_handle(userlist, handle), tmp);
  497. }
  498. static void modified_display(int idx, struct user_entry *e, struct userrec *u)
  499. {
  500. if (e && dcc[idx].user && (dcc[idx].user->flags & USER_MASTER)) {
  501. char tmp[1024] = "", tmp2[1024] = "", *hnd = NULL;
  502. time_t tm;
  503. strlcpy(tmp, e->u.string, sizeof(tmp));
  504. hnd = strchr(tmp, ' ');
  505. if (hnd)
  506. *hnd++ = 0;
  507. tm = atoi(tmp);
  508. strftime(tmp2, sizeof(tmp2), "%a, %d %b %Y %H:%M:%S %Z", gmtime(&tm));
  509. if (hnd)
  510. dprintf(idx, " -- Modified %s by %s\n", tmp2, hnd);
  511. else
  512. dprintf(idx, " -- Modified %s\n", tmp2);
  513. }
  514. }
  515. struct user_entry_type USERENTRY_MODIFIED =
  516. {
  517. 0,
  518. def_gotshare,
  519. def_unpack,
  520. def_write_userfile,
  521. def_kill,
  522. def_get,
  523. def_set,
  524. modified_display,
  525. "MODIFIED"
  526. };
  527. static bool pass_set(struct userrec *u, struct user_entry *e, void *buf)
  528. {
  529. char *newpass = NULL;
  530. char *pass = (char *) buf;
  531. free(e->u.extra);
  532. if (!pass || !pass[0] || (pass[0] == '-'))
  533. e->u.extra = NULL;
  534. else {
  535. unsigned char *p = (unsigned char *) pass;
  536. if (u->bot || (pass[0] == '+'))
  537. newpass = strdup(pass);
  538. else {
  539. while (*p) {
  540. if ((*p <= 32) || (*p == 127))
  541. *p = '?';
  542. p++;
  543. }
  544. newpass = salted_sha1(pass);
  545. }
  546. e->u.extra = strdup(newpass);
  547. }
  548. if (!noshare)
  549. shareout("c %s %s %s\n", e->type->name, u->handle, newpass ? newpass : "");
  550. free(newpass);
  551. /* clear old records */
  552. noshare = 1;
  553. set_user(&USERENTRY_PASS1, u, NULL);
  554. noshare = 0;
  555. return 1;
  556. }
  557. struct user_entry_type USERENTRY_PASS =
  558. {
  559. 0,
  560. def_gotshare,
  561. def_unpack,
  562. def_write_userfile,
  563. def_kill,
  564. def_get,
  565. pass_set,
  566. NULL,
  567. "PASS2"
  568. };
  569. static bool pass1_set(struct userrec *u, struct user_entry *e, void *buf)
  570. {
  571. char *pass = (char *) buf;
  572. free(e->u.extra);
  573. if (!pass || !pass[0] || (pass[0] == '-'))
  574. e->u.extra = NULL;
  575. else {
  576. unsigned char *p = (unsigned char *) pass;
  577. while (*p) {
  578. if ((*p <= 32) || (*p == 127))
  579. *p = '?';
  580. p++;
  581. }
  582. if (u->bot || (pass[0] == '+'))
  583. e->u.extra = strdup(pass);
  584. else
  585. e->u.extra = encrypt_string(u->handle, pass);
  586. }
  587. if (!noshare)
  588. shareout("c %s %s %s\n", e->type->name, u->handle, pass ? pass : "");
  589. return 1;
  590. }
  591. struct user_entry_type USERENTRY_PASS1 =
  592. {
  593. 0,
  594. def_gotshare,
  595. def_unpack,
  596. def_write_userfile,
  597. def_kill,
  598. def_get,
  599. pass1_set,
  600. NULL,
  601. "PASS1"
  602. };
  603. static void secpass_display(int idx, struct user_entry *e, struct userrec *u)
  604. {
  605. struct flag_record fr = {FR_GLOBAL, 0, 0, 0 };
  606. get_user_flagrec(dcc[idx].user, &fr, NULL);
  607. if (!strcmp(u->handle, dcc[idx].nick) || (glob_admin(fr) && isowner(dcc[idx].nick))) {
  608. if (conf.bot->hub)
  609. dprintf(idx, " %s: %s\n", e->type->name, e->u.string);
  610. else {
  611. dprintf(idx, " %s: Hidden on leaf bots.", e->type->name);
  612. if (dcc[idx].u.chat->su_nick)
  613. dprintf(idx, " Nice try, %s.", dcc[idx].u.chat->su_nick);
  614. dprintf(idx, "\n");
  615. }
  616. }
  617. }
  618. struct user_entry_type USERENTRY_SECPASS =
  619. {
  620. 0,
  621. def_gotshare,
  622. def_unpack,
  623. def_write_userfile,
  624. def_kill,
  625. def_get,
  626. def_set,
  627. secpass_display,
  628. "SECPASS"
  629. };
  630. static bool laston_unpack(struct userrec *u, struct user_entry *e)
  631. {
  632. char *par = e->u.list->extra, *arg = newsplit(&par);
  633. struct laston_info *li = (struct laston_info *) calloc(1, sizeof(struct laston_info));
  634. if (!par[0])
  635. par = "???";
  636. li->laston = atoi(arg);
  637. li->lastonplace = strdup(par);
  638. list_type_kill(e->u.list);
  639. e->u.extra = li;
  640. return 1;
  641. }
  642. static void laston_write_userfile(bd::Stream& stream, const struct userrec *u, const struct user_entry *e, int idx)
  643. {
  644. struct laston_info *li = (struct laston_info *) e->u.extra;
  645. stream << bd::String::printf("--LASTON %li %s\n", (long) li->laston, li->lastonplace ? li->lastonplace : "");
  646. }
  647. static bool laston_kill(struct user_entry *e)
  648. {
  649. free(((struct laston_info *) (e->u.extra))->lastonplace);
  650. free(e->u.extra);
  651. free(e);
  652. return 1;
  653. }
  654. static bool laston_set(struct userrec *u, struct user_entry *e, void *buf)
  655. {
  656. struct laston_info *li = (struct laston_info *) e->u.extra;
  657. if (li != buf) {
  658. if (li) {
  659. free(li->lastonplace);
  660. free(li);
  661. }
  662. li = (struct laston_info *) buf;
  663. e->u.extra = (struct laston_info *) buf;
  664. }
  665. if (!noshare)
  666. shareout_hub("c LASTON %s %s %li\n", u->handle, li->lastonplace ? li->lastonplace : "-", (long) li->laston);
  667. return 1;
  668. }
  669. static bool laston_gotshare(struct userrec *u, struct user_entry *e, char *par, int idx)
  670. {
  671. char *where = NULL;
  672. time_t timeval = 0;
  673. if (par[0])
  674. where = newsplit(&par);
  675. if (where && !strcmp(where, "-"))
  676. where = NULL;
  677. if (par[0])
  678. timeval = atol(newsplit(&par));
  679. touch_laston(u, where, timeval);
  680. return 1;
  681. }
  682. struct user_entry_type USERENTRY_LASTON =
  683. {
  684. 0, /* always 0 ;) */
  685. laston_gotshare,
  686. laston_unpack,
  687. laston_write_userfile,
  688. laston_kill,
  689. def_get,
  690. laston_set,
  691. 0,
  692. "LASTON"
  693. };
  694. static bool botaddr_unpack(struct userrec *u, struct user_entry *e)
  695. {
  696. char p[1024] = "", *q1 = NULL, *q2 = NULL;
  697. struct bot_addr *bi = (struct bot_addr *) calloc(1, sizeof(struct bot_addr));
  698. /* address:port/port:hublevel:uplink */
  699. strlcpy(p, e->u.list->extra, sizeof(p));
  700. q1 = strchr(p, ':');
  701. if (q1)
  702. *q1++ = 0;
  703. bi->address = strdup(p);
  704. if (q1) {
  705. q2 = strchr(q1, ':');
  706. if (q2)
  707. *q2++ = 0;
  708. bi->telnet_port = atoi(q1);
  709. q1 = strchr(q1, '/');
  710. if (q1) {
  711. q1++;
  712. bi->relay_port = atoi(q1);
  713. }
  714. if (q2) {
  715. q1 = strchr(q2, ':');
  716. if (q1) {
  717. *q1++ = 0;
  718. bi->uplink = strdup(q1);
  719. }
  720. bi->hublevel = atoi(q2);
  721. }
  722. }
  723. if (!bi->telnet_port)
  724. bi->telnet_port = 3333;
  725. if (!bi->relay_port)
  726. bi->relay_port = bi->telnet_port;
  727. if (!bi->uplink) {
  728. bi->uplink = (char *) calloc(1, 1);
  729. }
  730. list_type_kill(e->u.list);
  731. e->u.extra = bi;
  732. return 1;
  733. }
  734. static bool botaddr_kill(struct user_entry *e)
  735. {
  736. free(((struct bot_addr *) (e->u.extra))->address);
  737. free(((struct bot_addr *) (e->u.extra))->uplink);
  738. free(e->u.extra);
  739. free(e);
  740. return 1;
  741. }
  742. static void botaddr_write_userfile(bd::Stream& stream, const struct userrec *u, const struct user_entry *e, int idx)
  743. {
  744. struct bot_addr *bi = (struct bot_addr *) e->u.extra;
  745. 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);
  746. }
  747. static bool botaddr_set(struct userrec *u, struct user_entry *e, void *buf)
  748. {
  749. struct bot_addr *bi = (struct bot_addr *) e->u.extra;
  750. if (!bi && !buf)
  751. return 1;
  752. if (bi != buf) {
  753. if (bi) {
  754. free(bi->address);
  755. free(bi->uplink);
  756. free(bi);
  757. }
  758. ContextNote("botaddr_set", "(sharebug) occurred in botaddr_set");
  759. bi = (struct bot_addr *) buf;
  760. e->u.extra = (struct bot_addr *) buf;
  761. }
  762. if (bi && !noshare) {
  763. shareout("c BOTADDR %s %s %d %d %d %s\n",u->handle,
  764. (bi->address && bi->address[0]) ? bi->address : "127.0.0.1",
  765. bi->telnet_port, bi->relay_port, bi->hublevel, bi->uplink);
  766. }
  767. return 1;
  768. }
  769. static void botaddr_display(int idx, struct user_entry *e, struct userrec *u)
  770. {
  771. if (conf.bot->hub) {
  772. struct flag_record fr = {FR_GLOBAL, 0, 0, 0 };
  773. get_user_flagrec(dcc[idx].user, &fr, NULL);
  774. if (glob_admin(fr)) {
  775. struct bot_addr *bi = (struct bot_addr *) e->u.extra;
  776. if (bi->hublevel && bi->hublevel != 999) {
  777. if (bi->address) {
  778. dprintf(idx, " ADDRESS: %.70s\n", bi->address);
  779. dprintf(idx, " port: %d\n", bi->telnet_port);
  780. }
  781. dprintf(idx, " HUBLEVEL: %d\n", bi->hublevel);
  782. }
  783. if (bi->uplink && bi->uplink[0])
  784. dprintf(idx, " UPLINK: %s\n", bi->uplink);
  785. }
  786. }
  787. }
  788. static bool botaddr_gotshare(struct userrec *u, struct user_entry *e, char *buf, int idx)
  789. {
  790. struct bot_addr *bi = (struct bot_addr *) calloc(1, sizeof(struct bot_addr));
  791. char *arg = newsplit(&buf);
  792. bi->address = strdup(arg);
  793. arg = newsplit(&buf);
  794. bi->telnet_port = atoi(arg);
  795. arg = newsplit(&buf);
  796. bi->relay_port = atoi(arg);
  797. arg = newsplit(&buf);
  798. bi->hublevel = atoi(arg);
  799. bi->uplink = strdup(buf);
  800. if (!bi->telnet_port)
  801. bi->telnet_port = 3333;
  802. if (!bi->relay_port)
  803. bi->relay_port = bi->telnet_port;
  804. return botaddr_set(u, e, bi);
  805. }
  806. struct user_entry_type USERENTRY_BOTADDR =
  807. {
  808. 0, /* always 0 ;) */
  809. botaddr_gotshare,
  810. botaddr_unpack,
  811. botaddr_write_userfile,
  812. botaddr_kill,
  813. def_get,
  814. botaddr_set,
  815. botaddr_display,
  816. "BOTADDR"
  817. };
  818. static void hosts_write_userfile(bd::Stream& stream, const struct userrec *u, const struct user_entry *e, int idx)
  819. {
  820. struct list_type *h = NULL;
  821. for (h = (struct list_type *) e->u.extra; h; h = h->next)
  822. stream << bd::String::printf("--HOSTS %s\n", h->extra);
  823. }
  824. static bool hosts_null(struct userrec *u, struct user_entry *e)
  825. {
  826. return 1;
  827. }
  828. static bool hosts_kill(struct user_entry *e)
  829. {
  830. list_type_kill(e->u.list);
  831. free(e);
  832. return 1;
  833. }
  834. static void hosts_display(int idx, struct user_entry *e, struct userrec *u)
  835. {
  836. /* if this is a su, dont show hosts
  837. * otherwise, let users see their own hosts */
  838. if (conf.bot->hub ||
  839. (!conf.bot->hub && (dcc[idx].simul || (!strcmp(u->handle,dcc[idx].nick) && !dcc[idx].u.chat->su_nick)))) {
  840. char s[1024] = "";
  841. struct list_type *q = NULL;
  842. strlcpy(s, " HOSTS: ", sizeof(s));
  843. for (q = e->u.list; q; q = q->next) {
  844. if (s[0] && !s[9])
  845. strlcat(s, q->extra, sizeof(s));
  846. else if (!s[0])
  847. simple_snprintf(s, sizeof(s), " %s", q->extra);
  848. else {
  849. if (strlen(s) + strlen(q->extra) + 2 > 65) {
  850. dprintf(idx, "%s\n", s);
  851. simple_snprintf(s, sizeof(s), " %s", q->extra);
  852. } else {
  853. strlcat(s, ", ", sizeof(s));
  854. strlcat(s, q->extra, sizeof(s));
  855. }
  856. }
  857. }
  858. if (s[0])
  859. dprintf(idx, "%s\n", s);
  860. } else if (!conf.bot->hub) {
  861. dprintf(idx, " HOSTS: Hidden on leaf bots.");
  862. if (dcc[idx].u.chat->su_nick)
  863. dprintf(idx, " Nice try, %s.", dcc[idx].u.chat->su_nick);
  864. dprintf(idx, "\n");
  865. }
  866. }
  867. static bool hosts_set(struct userrec *u, struct user_entry *e, void *buf)
  868. {
  869. if (!buf || !strcasecmp((const char *) buf, "none")) {
  870. /* When the bot crashes, it's in this part, not in the 'else' part */
  871. list_type_kill(e->u.list);
  872. e->u.list = NULL;
  873. } else {
  874. char *host = (char *) buf, *p = strchr(host, ',');
  875. struct list_type **t;
  876. /* Can't have ,'s in hostmasks */
  877. while (p) {
  878. *p = '?';
  879. p = strchr(host, ',');
  880. }
  881. /* fred1: check for redundant hostmasks with
  882. * controversial "superpenis" algorithm ;) */
  883. /* I'm surprised Raistlin hasn't gotten involved in this controversy */
  884. t = &(e->u.list);
  885. while (*t) {
  886. if (wild_match(host, (*t)->extra)) {
  887. struct list_type *listu;
  888. listu = *t;
  889. *t = (*t)->next;
  890. free(listu->extra);
  891. free(listu);
  892. } else
  893. t = &((*t)->next);
  894. }
  895. *t = (struct list_type *) calloc(1, sizeof(struct list_type));
  896. (*t)->next = NULL;
  897. (*t)->extra = strdup(host);
  898. }
  899. return 1;
  900. }
  901. static bool hosts_gotshare(struct userrec *u, struct user_entry *e, char *buf, int idx)
  902. {
  903. /* doh, try to be too clever and it bites your butt */
  904. return 0;
  905. }
  906. struct user_entry_type USERENTRY_HOSTS =
  907. {
  908. 0,
  909. hosts_gotshare,
  910. hosts_null,
  911. hosts_write_userfile,
  912. hosts_kill,
  913. def_get,
  914. hosts_set,
  915. hosts_display,
  916. "HOSTS"
  917. };
  918. bool add_entry_type(struct user_entry_type *type)
  919. {
  920. struct userrec *u = NULL;
  921. list_insert((&entry_type_list), type);
  922. for (u = userlist; u; u = u->next) {
  923. struct user_entry *e = find_user_entry(type, u);
  924. if (e && e->name) {
  925. e->type = type;
  926. e->type->unpack(u, e);
  927. free(e->name);
  928. e->name = NULL;
  929. }
  930. }
  931. return 1;
  932. }
  933. struct user_entry_type *find_entry_type(const char *name)
  934. {
  935. struct user_entry_type *p = NULL;
  936. for (p = entry_type_list; p; p = p->next) {
  937. if (!strcasecmp(name, p->name))
  938. return p;
  939. }
  940. return NULL;
  941. }
  942. struct user_entry *find_user_entry(struct user_entry_type *et, struct userrec *u)
  943. {
  944. struct user_entry **e = NULL, *t = NULL;
  945. for (e = &(u->entries); *e; e = &((*e)->next)) {
  946. if (((*e)->type == et) || ((*e)->name && !strcasecmp((*e)->name, et->name))) {
  947. t = *e;
  948. *e = t->next;
  949. t->next = u->entries;
  950. u->entries = t;
  951. return t;
  952. }
  953. }
  954. return NULL;
  955. }
  956. void *get_user(struct user_entry_type *et, struct userrec *u)
  957. {
  958. struct user_entry *e = NULL;
  959. if (u && (e = find_user_entry(et, u)))
  960. return et->get(u, e);
  961. return NULL;
  962. }
  963. bool set_user(struct user_entry_type *et, struct userrec *u, void *d)
  964. {
  965. if (!u || !et)
  966. return 0;
  967. struct user_entry *e = NULL;
  968. bool r;
  969. if (!(e = find_user_entry(et, u))) {
  970. e = (struct user_entry *) calloc(1, sizeof(struct user_entry));
  971. e->type = et;
  972. e->name = NULL;
  973. e->u.list = NULL;
  974. list_insert((&(u->entries)), e);
  975. }
  976. r = et->set(u, e, d);
  977. if (!e->u.list) {
  978. list_delete((struct list_type **) &(u->entries), (struct list_type *) e);
  979. free(e);
  980. }
  981. return r;
  982. }
  983. /* vim: set sts=2 sw=2 ts=8 et: */