userent.c 26 KB

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