userrec.c 16 KB

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