userrec.c 16 KB

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