userrec.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702
  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 = 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, new[32] = "", pass[16] = "";
  213. if (!u)
  214. return 0;
  215. egg_snprintf(pass, sizeof pass, "%s", in);
  216. cmp = 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->flags & USER_BOT) {
  222. if (!strcmp(cmp, pass))
  223. return 1;
  224. } else {
  225. if (strlen(pass) > 15)
  226. pass[15] = 0;
  227. encrypt_pass(pass, new);
  228. if (!strcmp(cmp, new))
  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};
  241. fr.global = u->flags;
  242. build_flags(s, &fr, NULL);
  243. if (lfprintf(f, "%-10s - %-24s\n", 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->flags & b->flags & USER_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->flags & b->flags & USER_BOT)
  288. return 1;
  289. if (a->flags & ~b->flags & USER_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 = 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(NULL, "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)
  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->next=bu; bu=u; */
  420. strncpyz(u->handle, handle, sizeof u->handle);
  421. u->next = NULL;
  422. u->chanrec = NULL;
  423. u->entries = NULL;
  424. if (flags != USER_DEFAULT) { /* drummer */
  425. u->flags = flags;
  426. } else {
  427. u->flags = default_flags;
  428. }
  429. set_user(&USERENTRY_PASS, u, pass);
  430. /* Strip out commas -- they're illegal */
  431. if (host && host[0]) {
  432. char *p = NULL;
  433. /* About this fixfrom():
  434. * We should use this fixfrom before every call of adduser()
  435. * but its much easier to use here... (drummer)
  436. * Only use it if we have a host :) (dw)
  437. */
  438. host = fixfrom(host);
  439. p = strchr(host, ',');
  440. while (p != NULL) {
  441. *p = '?';
  442. p = strchr(host, ',');
  443. }
  444. set_user(&USERENTRY_HOSTS, u, host);
  445. } else
  446. set_user(&USERENTRY_HOSTS, u, "none");
  447. if (bu == userlist)
  448. clear_chanlist();
  449. noshare = oldshare;
  450. if ((!noshare) && (handle[0] != '*') && (bu == userlist)) {
  451. struct flag_record fr = {FR_GLOBAL, 0, 0};
  452. char xx[100] = "";
  453. fr.global = u->flags;
  454. build_flags(xx, &fr, 0);
  455. shareout(NULL, "n %s %s %s %s\n", handle, host && host[0] ? host : "none", pass, xx);
  456. }
  457. if (bu == NULL)
  458. bu = u;
  459. else {
  460. if ((bu == userlist) && (lastuser != NULL))
  461. x = lastuser;
  462. else
  463. x = bu;
  464. while (x->next != NULL)
  465. x = x->next;
  466. x->next = u;
  467. if (bu == userlist)
  468. lastuser = u;
  469. }
  470. return bu;
  471. }
  472. static void freeuser(struct userrec *u)
  473. {
  474. struct user_entry *ue = NULL, *ut = NULL;
  475. struct chanuserrec *ch = NULL, *z = NULL;
  476. if (u == NULL)
  477. return;
  478. ch = u->chanrec;
  479. while (ch) {
  480. z = ch;
  481. ch = ch->next;
  482. if (z->info != NULL)
  483. free(z->info);
  484. free(z);
  485. }
  486. u->chanrec = NULL;
  487. for (ue = u->entries; ue; ue = ut) {
  488. ut = ue->next;
  489. if (ue->name) {
  490. struct list_type *lt, *ltt;
  491. for (lt = ue->u.list; lt; lt = ltt) {
  492. ltt = lt->next;
  493. free(lt->extra);
  494. free(lt);
  495. }
  496. free(ue->name);
  497. free(ue);
  498. } else {
  499. ue->type->kill(ue);
  500. }
  501. }
  502. free(u);
  503. }
  504. int deluser(char *handle)
  505. {
  506. struct userrec *u = userlist, *prev = NULL;
  507. int fnd = 0;
  508. while ((u != NULL) && (!fnd)) {
  509. if (!egg_strcasecmp(u->handle, handle))
  510. fnd = 1;
  511. else {
  512. prev = u;
  513. u = u->next;
  514. }
  515. }
  516. if (!fnd)
  517. return 0;
  518. if (prev == NULL)
  519. userlist = u->next;
  520. else
  521. prev->next = u->next;
  522. if (!noshare && (handle[0] != '*'))
  523. shareout(NULL, "k %s\n", handle);
  524. for (fnd = 0; fnd < dcc_total; fnd++)
  525. if (dcc[fnd].user == u)
  526. dcc[fnd].user = 0; /* Clear any dcc users for this entry,
  527. * null is safe-ish */
  528. clear_chanlist();
  529. freeuser(u);
  530. lastuser = NULL;
  531. return 1;
  532. }
  533. int delhost_by_handle(char *handle, char *host)
  534. {
  535. struct userrec *u = NULL;
  536. struct list_type *q = NULL, *qnext = NULL, *qprev = NULL;
  537. struct user_entry *e = NULL;
  538. int i = 0;
  539. u = get_user_by_handle(userlist, handle);
  540. if (!u)
  541. return 0;
  542. q = get_user(&USERENTRY_HOSTS, u);
  543. qprev = q;
  544. if (q) {
  545. if (!rfc_casecmp(q->extra, host)) {
  546. e = find_user_entry(&USERENTRY_HOSTS, u);
  547. e->u.extra = q->next;
  548. free(q->extra);
  549. free(q);
  550. i++;
  551. qprev = NULL;
  552. q = e->u.extra;
  553. } else
  554. q = q->next;
  555. while (q) {
  556. qnext = q->next;
  557. if (!rfc_casecmp(q->extra, host)) {
  558. if (qprev)
  559. qprev->next = q->next;
  560. else if (e) {
  561. e->u.extra = q->next;
  562. qprev = NULL;
  563. }
  564. free(q->extra);
  565. free(q);
  566. i++;
  567. } else
  568. qprev = q;
  569. q = qnext;
  570. }
  571. }
  572. if (!qprev)
  573. set_user(&USERENTRY_HOSTS, u, "none");
  574. if (!noshare && i)
  575. shareout(NULL, "-h %s %s\n", handle, host);
  576. clear_chanlist();
  577. return i;
  578. }
  579. void addhost_by_handle(char *handle, char *host)
  580. {
  581. struct userrec *u = get_user_by_handle(userlist, handle);
  582. set_user(&USERENTRY_HOSTS, u, host);
  583. /* u will be cached, so really no overhead, even tho this looks dumb: */
  584. if (!noshare) {
  585. if (u->flags & USER_BOT)
  586. shareout(NULL, "+bh %s %s\n", handle, host);
  587. else
  588. shareout(NULL, "+h %s %s\n", handle, host);
  589. }
  590. clear_chanlist();
  591. }
  592. void touch_laston(struct userrec *u, char *where, time_t timeval)
  593. {
  594. if (!u)
  595. return;
  596. if (timeval > 1) {
  597. struct laston_info *li = (struct laston_info *) get_user(&USERENTRY_LASTON, u);
  598. if (!li)
  599. li = calloc(1, sizeof(struct laston_info));
  600. else if (li->lastonplace)
  601. free(li->lastonplace);
  602. li->laston = timeval;
  603. if (where) {
  604. li->lastonplace = strdup(where);
  605. } else
  606. li->lastonplace = NULL;
  607. set_user(&USERENTRY_LASTON, u, li);
  608. } else if (timeval == 1)
  609. set_user(&USERENTRY_LASTON, u, 0);
  610. }
  611. void user_del_chan(char *dname)
  612. {
  613. struct chanuserrec *ch = NULL, *och = NULL;
  614. struct userrec *u = NULL;
  615. for (u = userlist; u; u = u->next) {
  616. ch = u->chanrec;
  617. och = NULL;
  618. while (ch) {
  619. if (!rfc_casecmp(dname, ch->channel)) {
  620. if (och)
  621. och->next = ch->next;
  622. else
  623. u->chanrec = ch->next;
  624. if (ch->info)
  625. free(ch->info);
  626. free(ch);
  627. break;
  628. }
  629. och = ch;
  630. ch = ch->next;
  631. }
  632. }
  633. }