userent.c 26 KB

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