userrec.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705
  1. /*
  2. * userrec.c -- handles:
  3. * add_q() del_q() str2flags() flags2str() str2chflags() chflags2str()
  4. * a bunch of functions to find and change user records
  5. * change and check user (and channel-specific) flags
  6. *
  7. */
  8. #include <sys/stat.h>
  9. #include "common.h"
  10. #include "userrec.h"
  11. #include "misc.h"
  12. #include "misc_file.h"
  13. #include "rfc1459.h"
  14. #include "dcc.h"
  15. #include "src/mod/share.mod/share.h"
  16. #include "src/mod/channels.mod/channels.h"
  17. #include "main.h"
  18. #include "users.h"
  19. #include "chan.h"
  20. #include "match.h"
  21. #include "dccutil.h"
  22. #include "tandem.h"
  23. #include "chanprog.h"
  24. #include "crypt.h"
  25. #include "core_binds.h"
  26. int noshare = 1; /* don't send out to sharebots */
  27. struct userrec *userlist = NULL; /* user records are stored here */
  28. struct userrec *lastuser = NULL; /* last accessed user record */
  29. maskrec *global_bans = NULL,
  30. *global_exempts = NULL,
  31. *global_invites = NULL;
  32. struct igrec *global_ign = NULL;
  33. int cache_hit = 0,
  34. cache_miss = 0; /* temporary cache accounting */
  35. int strict_host = 0;
  36. int userfile_perm = 0600; /* Userfile permissions,
  37. default rw------- */
  38. #ifdef HUB
  39. static int sort_users = 1; /* sort the userlist when saving */
  40. #endif /* HUB */
  41. int count_users(struct userrec *bu)
  42. {
  43. int tot = 0;
  44. struct userrec *u = NULL;
  45. for (u = bu; u; u = u->next)
  46. tot++;
  47. return tot;
  48. }
  49. /* Removes a username prefix (~+-^=) from a userhost.
  50. * e.g, "nick!~user@host" -> "nick!user@host"
  51. */
  52. char *fixfrom(char *s)
  53. {
  54. char *p = NULL;
  55. if (!s || !*s || strict_host)
  56. return s;
  57. if ((p = strchr(s, '!'))) {
  58. if (!*(++p))
  59. return s; /* There's nothing following "!". */
  60. } else
  61. p = s; /* There's no nick. */
  62. if (strchr("~+-^=", *p) && *(p + 1) != '@')
  63. memmove(p, p + 1, strlen(p)); /* NUL is included without +1. */
  64. return s;
  65. }
  66. static struct userrec *check_dcclist_hand(char *handle)
  67. {
  68. int i;
  69. for (i = 0; i < dcc_total; i++)
  70. if (!egg_strcasecmp(dcc[i].nick, handle))
  71. return dcc[i].user;
  72. return NULL;
  73. }
  74. struct userrec *get_user_by_handle(struct userrec *bu, char *handle)
  75. {
  76. struct userrec *u = NULL, *ret = NULL;
  77. if (!handle)
  78. return NULL;
  79. /* FIXME: This should be done outside of this function. */
  80. rmspace(handle);
  81. if (!handle[0] || (handle[0] == '*'))
  82. return NULL;
  83. if (bu == userlist) {
  84. if (lastuser && !egg_strcasecmp(lastuser->handle, handle)) {
  85. cache_hit++;
  86. return lastuser;
  87. }
  88. ret = check_dcclist_hand(handle);
  89. if (ret) {
  90. cache_hit++;
  91. return ret;
  92. }
  93. ret = check_chanlist_hand(handle);
  94. if (ret) {
  95. cache_hit++;
  96. return ret;
  97. }
  98. cache_miss++;
  99. }
  100. for (u = bu; u; u = u->next)
  101. if (!egg_strcasecmp(u->handle, handle)) {
  102. if (bu == userlist)
  103. lastuser = u;
  104. return u;
  105. }
  106. return NULL;
  107. }
  108. /* Fix capitalization, etc
  109. */
  110. void correct_handle(char *handle)
  111. {
  112. struct userrec *u = NULL;
  113. u = get_user_by_handle(userlist, handle);
  114. if (u == NULL || handle == u->handle)
  115. return;
  116. strcpy(handle, u->handle);
  117. }
  118. /* This will be usefull in a lot of places, much more code re-use so we
  119. * endup with a smaller executable bot. <cybah>
  120. */
  121. void clear_masks(maskrec *m)
  122. {
  123. maskrec *temp = NULL;
  124. for (; m; m = temp) {
  125. temp = m->next;
  126. if (m->mask)
  127. free(m->mask);
  128. if (m->user)
  129. free(m->user);
  130. if (m->desc)
  131. free(m->desc);
  132. free(m);
  133. }
  134. }
  135. static void freeuser(struct userrec *);
  136. void clear_userlist(struct userrec *bu)
  137. {
  138. struct userrec *u = NULL, *v = NULL;
  139. int i;
  140. for (u = bu; u; u = v) {
  141. v = u->next;
  142. freeuser(u);
  143. }
  144. if (userlist == bu) {
  145. struct chanset_t *cst = NULL;
  146. for (i = 0; i < dcc_total; i++)
  147. dcc[i].user = NULL;
  148. conf.bot->u = NULL;
  149. clear_chanlist();
  150. lastuser = NULL;
  151. while (global_ign)
  152. delignore(global_ign->igmask);
  153. clear_masks(global_bans);
  154. clear_masks(global_exempts);
  155. clear_masks(global_invites);
  156. global_exempts = global_invites = global_bans = NULL;
  157. for (cst = chanset; cst; cst = cst->next) {
  158. clear_masks(cst->bans);
  159. clear_masks(cst->exempts);
  160. clear_masks(cst->invites);
  161. cst->bans = cst->exempts = cst->invites = NULL;
  162. }
  163. }
  164. /* Remember to set your userlist to NULL after calling this */
  165. }
  166. /* Find CLOSEST host match
  167. * (if "*!*@*" and "*!*@*clemson.edu" both match, use the latter!)
  168. *
  169. * Checks the chanlist first, to possibly avoid needless search.
  170. */
  171. struct userrec *get_user_by_host(char *host)
  172. {
  173. struct userrec *u = NULL, *ret = NULL;
  174. struct list_type *q = NULL;
  175. int cnt, i;
  176. char host2[UHOSTLEN] = "";
  177. if (host == NULL)
  178. return NULL;
  179. rmspace(host);
  180. if (!host[0])
  181. return NULL;
  182. ret = check_chanlist(host);
  183. cnt = 0;
  184. if (ret != NULL) {
  185. cache_hit++;
  186. return ret;
  187. }
  188. cache_miss++;
  189. strncpyz(host2, host, sizeof host2);
  190. host = fixfrom(host);
  191. for (u = userlist; u; u = u->next) {
  192. q = (struct list_type *) get_user(&USERENTRY_HOSTS, u);
  193. for (; q; q = q->next) {
  194. i = wild_match(q->extra, host);
  195. if (i > cnt) {
  196. ret = u;
  197. cnt = i;
  198. }
  199. }
  200. }
  201. if (ret != NULL) {
  202. lastuser = ret;
  203. set_chanlist(host2, ret);
  204. }
  205. return ret;
  206. }
  207. /* Try: pass_match_by_host("-",host)
  208. * will return 1 if no password is set for that host
  209. */
  210. int u_pass_match(struct userrec *u, char *in)
  211. {
  212. char *cmp = NULL, newpass[32] = "", pass[MAXPASSLEN + 1] = "";
  213. if (!u)
  214. return 0;
  215. egg_snprintf(pass, sizeof pass, "%s", in);
  216. cmp = (char *) get_user(&USERENTRY_PASS, u);
  217. if (!cmp && (!pass[0] || (pass[0] == '-')))
  218. return 1;
  219. if (!cmp || !pass || !pass[0] || (pass[0] == '-'))
  220. return 0;
  221. if (u->bot) {
  222. if (!strcmp(cmp, pass))
  223. return 1;
  224. } else {
  225. if (strlen(pass) > MAXPASSLEN)
  226. pass[MAXPASSLEN] = 0;
  227. encrypt_pass(pass, newpass);
  228. if (!strcmp(cmp, newpass))
  229. return 1;
  230. }
  231. return 0;
  232. }
  233. #ifdef HUB
  234. int write_user(struct userrec *u, FILE * f, int idx)
  235. {
  236. char s[181] = "";
  237. struct chanuserrec *ch = NULL;
  238. struct chanset_t *cst = NULL;
  239. struct user_entry *ue = NULL;
  240. struct flag_record fr = {FR_GLOBAL, 0, 0, 0 };
  241. fr.global = u->flags;
  242. build_flags(s, &fr, NULL);
  243. if (lfprintf(f, "%s%-10s - %-24s\n", u->bot ? "-" : "", u->handle, s) == EOF)
  244. return 0;
  245. for (ch = u->chanrec; ch; ch = ch->next) {
  246. cst = findchan_by_dname(ch->channel);
  247. if (cst) {
  248. if (idx >= 0) {
  249. fr.match = FR_CHAN;
  250. get_user_flagrec(dcc[idx].user, &fr, ch->channel);
  251. }
  252. fr.match = FR_CHAN;
  253. fr.chan = ch->flags;
  254. build_flags(s, &fr, NULL);
  255. if (lfprintf(f, "! %-20s %lu %-10s %s\n", ch->channel, ch->laston, s, ch->info ? ch->info : "") == EOF)
  256. return 0;
  257. }
  258. }
  259. for (ue = u->entries; ue; ue = ue->next) {
  260. if (ue->name) {
  261. struct list_type *lt = NULL;
  262. for (lt = ue->u.list; lt; lt = lt->next)
  263. if (lfprintf(f, "--%s %s\n", ue->name, lt->extra) == EOF)
  264. return 0;
  265. } else {
  266. if (!ue->type->write_userfile(f, u, ue))
  267. return 0;
  268. }
  269. }
  270. return 1;
  271. }
  272. static int sort_compare(struct userrec *a, struct userrec *b)
  273. {
  274. /* Order by flags, then alphabetically
  275. * first bots: +h / +a / +l / other bots
  276. * then users: +n / +m / +o / other users
  277. * return true if (a > b)
  278. */
  279. if (a->bot && b->bot) {
  280. if (bot_hublevel(a) > bot_hublevel(b))
  281. return 1;
  282. else if (bot_hublevel(a) < bot_hublevel(b))
  283. return 0;
  284. else if (bot_hublevel(a) == bot_hublevel(b) && bot_hublevel(a) == 999)
  285. return 0;
  286. } else {
  287. if (a->bot && !b->bot)
  288. return 1;
  289. if (!a->bot && b->bot)
  290. return 0;
  291. if (~a->flags & b->flags & USER_ADMIN)
  292. return 1;
  293. if (a->flags & ~b->flags & USER_ADMIN)
  294. return 0;
  295. if (~a->flags & b->flags & USER_OWNER)
  296. return 1;
  297. if (a->flags & ~b->flags & USER_OWNER)
  298. return 0;
  299. if (~a->flags & b->flags & USER_MASTER)
  300. return 1;
  301. if (a->flags & ~b->flags & USER_MASTER)
  302. return 0;
  303. if (~a->flags & b->flags & USER_OP)
  304. return 1;
  305. if (a->flags & ~b->flags & USER_OP)
  306. return 0;
  307. }
  308. return (egg_strcasecmp(a->handle, b->handle) > 0);
  309. }
  310. static void sort_userlist()
  311. {
  312. int again = 1;
  313. struct userrec *last = NULL, *p = NULL, *c = NULL, *n = NULL;
  314. while ((userlist != last) && (again)) {
  315. p = NULL;
  316. c = userlist;
  317. n = c->next;
  318. again = 0;
  319. while (n != last) {
  320. if (sort_compare(c, n)) {
  321. again = 1;
  322. c->next = n->next;
  323. n->next = c;
  324. if (p == NULL)
  325. userlist = n;
  326. else
  327. p->next = n;
  328. }
  329. p = c;
  330. c = n;
  331. n = n->next;
  332. }
  333. last = c;
  334. }
  335. }
  336. /* Rewrite the entire user file. Call USERFILE hook as well, probably
  337. * causing the channel file to be rewritten as well.
  338. */
  339. int write_userfile(int idx)
  340. {
  341. FILE *f = NULL;
  342. char *new_userfile = NULL, s1[81] = "", backup[DIRMAX] = "";
  343. time_t tt;
  344. struct userrec *u = NULL;
  345. int ok;
  346. if (userlist == NULL)
  347. return 1; /* No point in saving userfile */
  348. new_userfile = (char *) calloc(1, strlen(userfile) + 5);
  349. sprintf(new_userfile, "%s~new", userfile);
  350. f = fopen(new_userfile, "w");
  351. fixmod(new_userfile);
  352. if (f == NULL) {
  353. putlog(LOG_MISC, "*", USERF_ERRWRITE);
  354. free(new_userfile);
  355. return 2;
  356. }
  357. if (idx >= 0)
  358. dprintf(idx, "Saving userfile...\n");
  359. if (sort_users)
  360. sort_userlist();
  361. tt = now;
  362. strcpy(s1, ctime(&tt));
  363. lfprintf(f, "#4v: %s -- %s -- written %s", ver, conf.bot->nick, s1);
  364. ok = 1;
  365. fclose(f);
  366. channels_writeuserfile();
  367. f = fopen(new_userfile, "a");
  368. putlog(LOG_DEBUG, "@", "Writing user entries.");
  369. for (u = userlist; u && ok; u = u->next)
  370. ok = write_user(u, f, idx);
  371. if (!ok || fflush(f)) {
  372. putlog(LOG_MISC, "*", "%s (%s)", USERF_ERRWRITE, strerror(ferror(f)));
  373. fclose(f);
  374. free(new_userfile);
  375. return 3;
  376. }
  377. fclose(f);
  378. putlog(LOG_DEBUG, "@", "Done writing userfile.");
  379. egg_snprintf(backup, sizeof backup, "%s%s~", tempdir, userfile);
  380. copyfile(userfile, backup);
  381. movefile(new_userfile, userfile);
  382. free(new_userfile);
  383. return 0;
  384. }
  385. #endif /* HUB */
  386. int change_handle(struct userrec *u, char *newh)
  387. {
  388. int i;
  389. char s[HANDLEN + 1] = "";
  390. if (!u)
  391. return 0;
  392. /* Nothing that will confuse the userfile */
  393. if (!newh[1] && strchr(BADHANDCHARS, newh[0]))
  394. return 0;
  395. check_bind_nkch(u->handle, newh);
  396. /* Yes, even send bot nick changes now: */
  397. if (!noshare)
  398. shareout("h %s %s\n", u->handle, newh);
  399. strncpyz(s, u->handle, sizeof s);
  400. strncpyz(u->handle, newh, sizeof u->handle);
  401. for (i = 0; i < dcc_total; i++)
  402. if (dcc[i].type != &DCC_BOT && !egg_strcasecmp(dcc[i].nick, s)) {
  403. strncpyz(dcc[i].nick, newh, sizeof dcc[i].nick);
  404. if (dcc[i].type == &DCC_CHAT && dcc[i].u.chat->channel >= 0) {
  405. chanout_but(-1, dcc[i].u.chat->channel,
  406. "*** Handle change: %s -> %s\n", s, newh);
  407. if (dcc[i].u.chat->channel < GLOBAL_CHANS)
  408. botnet_send_nkch(i, s);
  409. }
  410. }
  411. return 1;
  412. }
  413. struct userrec *adduser(struct userrec *bu, char *handle, char *host, char *pass, flag_t flags, int bot)
  414. {
  415. struct userrec *u = NULL, *x = NULL;
  416. int oldshare = noshare;
  417. noshare = 1;
  418. u = (struct userrec *) calloc(1, sizeof(struct userrec));
  419. u->bot = bot;
  420. /* u->next=bu; bu=u; */
  421. strncpyz(u->handle, handle, sizeof u->handle);
  422. u->next = NULL;
  423. u->chanrec = NULL;
  424. u->entries = NULL;
  425. if (flags != USER_DEFAULT) { /* drummer */
  426. u->flags = flags;
  427. } else {
  428. u->flags = default_flags;
  429. }
  430. set_user(&USERENTRY_PASS, u, pass);
  431. /* Strip out commas -- they're illegal */
  432. if (host && host[0]) {
  433. char *p = NULL;
  434. /* About this fixfrom():
  435. * We should use this fixfrom before every call of adduser()
  436. * but its much easier to use here... (drummer)
  437. * Only use it if we have a host :) (dw)
  438. */
  439. host = fixfrom(host);
  440. p = strchr(host, ',');
  441. while (p != NULL) {
  442. *p = '?';
  443. p = strchr(host, ',');
  444. }
  445. set_user(&USERENTRY_HOSTS, u, host);
  446. } else
  447. set_user(&USERENTRY_HOSTS, u, (void *) "none");
  448. if (bu == userlist)
  449. clear_chanlist();
  450. noshare = oldshare;
  451. if ((!noshare) && (handle[0] != '*') && (bu == userlist)) {
  452. struct flag_record fr = {FR_GLOBAL, 0, 0, 0 };
  453. char xx[100] = "";
  454. fr.global = u->flags;
  455. build_flags(xx, &fr, 0);
  456. shareout("n %s%s %s %s %s\n", bot ? "-" : "", handle, host && host[0] ? host : "none", pass, xx);
  457. }
  458. if (bu == NULL)
  459. bu = u;
  460. else {
  461. if ((bu == userlist) && (lastuser != NULL))
  462. x = lastuser;
  463. else
  464. x = bu;
  465. while (x->next != NULL)
  466. x = x->next;
  467. x->next = u;
  468. if (bu == userlist)
  469. lastuser = u;
  470. }
  471. return bu;
  472. }
  473. static void freeuser(struct userrec *u)
  474. {
  475. struct user_entry *ue = NULL, *ut = NULL;
  476. struct chanuserrec *ch = NULL, *z = NULL;
  477. if (u == NULL)
  478. return;
  479. ch = u->chanrec;
  480. while (ch) {
  481. z = ch;
  482. ch = ch->next;
  483. if (z->info != NULL)
  484. free(z->info);
  485. free(z);
  486. }
  487. u->chanrec = NULL;
  488. for (ue = u->entries; ue; ue = ut) {
  489. ut = ue->next;
  490. if (ue->name) {
  491. struct list_type *lt, *ltt;
  492. for (lt = ue->u.list; lt; lt = ltt) {
  493. ltt = lt->next;
  494. free(lt->extra);
  495. free(lt);
  496. }
  497. free(ue->name);
  498. free(ue);
  499. } else {
  500. ue->type->kill(ue);
  501. }
  502. }
  503. free(u);
  504. }
  505. int deluser(char *handle)
  506. {
  507. struct userrec *u = userlist, *prev = NULL;
  508. int fnd = 0;
  509. while ((u != NULL) && (!fnd)) {
  510. if (!egg_strcasecmp(u->handle, handle))
  511. fnd = 1;
  512. else {
  513. prev = u;
  514. u = u->next;
  515. }
  516. }
  517. if (!fnd)
  518. return 0;
  519. if (prev == NULL)
  520. userlist = u->next;
  521. else
  522. prev->next = u->next;
  523. if (!noshare && (handle[0] != '*'))
  524. shareout("k %s\n", handle);
  525. for (fnd = 0; fnd < dcc_total; fnd++)
  526. if (dcc[fnd].user == u)
  527. dcc[fnd].user = 0; /* Clear any dcc users for this entry,
  528. * null is safe-ish */
  529. clear_chanlist();
  530. freeuser(u);
  531. lastuser = NULL;
  532. return 1;
  533. }
  534. int delhost_by_handle(char *handle, char *host)
  535. {
  536. struct userrec *u = NULL;
  537. struct list_type *q = NULL, *qnext = NULL, *qprev = NULL;
  538. struct user_entry *e = NULL;
  539. int i = 0;
  540. u = get_user_by_handle(userlist, handle);
  541. if (!u)
  542. return 0;
  543. q = (struct list_type *) get_user(&USERENTRY_HOSTS, u);
  544. qprev = q;
  545. if (q) {
  546. if (!rfc_casecmp(q->extra, host)) {
  547. e = find_user_entry(&USERENTRY_HOSTS, u);
  548. e->u.extra = q->next;
  549. free(q->extra);
  550. free(q);
  551. i++;
  552. qprev = NULL;
  553. q = (struct list_type *) e->u.extra;
  554. } else
  555. q = q->next;
  556. while (q) {
  557. qnext = q->next;
  558. if (!rfc_casecmp(q->extra, host)) {
  559. if (qprev)
  560. qprev->next = q->next;
  561. else if (e) {
  562. e->u.extra = q->next;
  563. qprev = NULL;
  564. }
  565. free(q->extra);
  566. free(q);
  567. i++;
  568. } else
  569. qprev = q;
  570. q = qnext;
  571. }
  572. }
  573. if (!qprev)
  574. set_user(&USERENTRY_HOSTS, u, (void *) "none");
  575. if (!noshare && i)
  576. shareout("-h %s %s\n", handle, host);
  577. clear_chanlist();
  578. return i;
  579. }
  580. void addhost_by_handle(char *handle, char *host)
  581. {
  582. struct userrec *u = get_user_by_handle(userlist, handle);
  583. set_user(&USERENTRY_HOSTS, u, host);
  584. /* u will be cached, so really no overhead, even tho this looks dumb: */
  585. if (!noshare) {
  586. if (u->bot)
  587. shareout("+bh %s %s\n", handle, host);
  588. else
  589. shareout("+h %s %s\n", handle, host);
  590. }
  591. clear_chanlist();
  592. }
  593. void touch_laston(struct userrec *u, char *where, time_t timeval)
  594. {
  595. if (!u)
  596. return;
  597. if (timeval > 1) {
  598. struct laston_info *li = (struct laston_info *) get_user(&USERENTRY_LASTON, u);
  599. if (!li)
  600. li = (struct laston_info *) calloc(1, sizeof(struct laston_info));
  601. else if (li->lastonplace)
  602. free(li->lastonplace);
  603. li->laston = timeval;
  604. if (where) {
  605. li->lastonplace = strdup(where);
  606. } else
  607. li->lastonplace = NULL;
  608. set_user(&USERENTRY_LASTON, u, li);
  609. } else if (timeval == 1)
  610. set_user(&USERENTRY_LASTON, u, 0);
  611. }
  612. void user_del_chan(char *dname)
  613. {
  614. struct chanuserrec *ch = NULL, *och = NULL;
  615. struct userrec *u = NULL;
  616. for (u = userlist; u; u = u->next) {
  617. ch = u->chanrec;
  618. och = NULL;
  619. while (ch) {
  620. if (!rfc_casecmp(dname, ch->channel)) {
  621. if (och)
  622. och->next = ch->next;
  623. else
  624. u->chanrec = ch->next;
  625. if (ch->info)
  626. free(ch->info);
  627. free(ch);
  628. break;
  629. }
  630. och = ch;
  631. ch = ch->next;
  632. }
  633. }
  634. }