userent.c 26 KB

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