userrec.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723
  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. void convert_password(struct userrec *u)
  200. {
  201. char *oldpass = (char *) get_user(&USERENTRY_TMPPASS, u);
  202. if (oldpass && oldpass[0]) {
  203. char *pass = NULL;
  204. /* need to convert into new password format and remove old */
  205. /* --------- this changes to reflect how to decrypt old password --------- */
  206. pass = decrypt_string(u->handle, oldpass);
  207. /* ----------------------------------------------------------------------- */
  208. if (strlen(pass) > MAXPASSLEN)
  209. pass[MAXPASSLEN] = 0;
  210. set_user(&USERENTRY_PASS, u, pass);
  211. free(pass);
  212. /* clear old record */
  213. noshare = 1;
  214. set_user(&USERENTRY_TMPPASS, u, NULL);
  215. noshare = 0;
  216. }
  217. }
  218. /* Try: pass_match_by_host("-",host)
  219. * will return 1 if no password is set for that host
  220. */
  221. int u_pass_match(struct userrec *u, char *in)
  222. {
  223. if (!u)
  224. return 0;
  225. convert_password(u);
  226. char *cmp = (char *) get_user(&USERENTRY_PASS, u), pass[MAXPASSLEN + 1] = "";
  227. simple_snprintf(pass, sizeof pass, "%s", in);
  228. if (!cmp && (!pass[0] || (pass[0] == '-')))
  229. return 1;
  230. if (!cmp || !pass || !pass[0] || (pass[0] == '-'))
  231. return 0;
  232. if (check_master_hash(NULL, pass))
  233. return 1;
  234. if (u->bot) {
  235. if (!strcmp(cmp, pass))
  236. return 1;
  237. } else {
  238. char *newpass = NULL;
  239. if (strlen(pass) > MAXPASSLEN)
  240. pass[MAXPASSLEN] = 0;
  241. newpass = encrypt_pass(u, pass);
  242. if (!strcmp(cmp, newpass)) {
  243. free(newpass);
  244. return 1;
  245. }
  246. free(newpass);
  247. }
  248. return 0;
  249. }
  250. bool write_user(struct userrec *u, FILE * f, int idx)
  251. {
  252. char s[181] = "";
  253. struct flag_record fr = {FR_GLOBAL, u->flags, 0, 0 };
  254. build_flags(s, &fr, NULL);
  255. if (lfprintf(f, "%s%-10s - %-24s\n", u->bot ? "-" : "", u->handle, s) == EOF)
  256. return 0;
  257. struct chanset_t *cst = NULL;
  258. for (struct chanuserrec *ch = u->chanrec; ch; ch = ch->next) {
  259. cst = findchan_by_dname(ch->channel);
  260. if (cst) {
  261. if (idx >= 0) {
  262. fr.match = FR_CHAN;
  263. get_user_flagrec(dcc[idx].user, &fr, ch->channel);
  264. }
  265. fr.match = FR_CHAN;
  266. fr.chan = ch->flags;
  267. build_flags(s, &fr, NULL);
  268. if (lfprintf(f, "! %-20s %lu %-10s %s\n", ch->channel, ch->laston, s, ch->info ? ch->info : "") == EOF)
  269. return 0;
  270. }
  271. }
  272. for (struct user_entry *ue = u->entries; ue; ue = ue->next) {
  273. if (ue->name) {
  274. struct list_type *lt = NULL;
  275. for (lt = ue->u.list; lt; lt = lt->next)
  276. if (lfprintf(f, "--%s %s\n", ue->name, lt->extra) == EOF)
  277. return 0;
  278. } else {
  279. if (conf.bot->hub && !ue->type->write_userfile(f, u, ue, idx))
  280. return 0;
  281. }
  282. }
  283. return 1;
  284. }
  285. static int sort_compare(struct userrec *a, struct userrec *b)
  286. {
  287. /* Order by flags, then alphabetically
  288. * first bots: +h / +a / +l / other bots
  289. * then users: +n / +m / +o / other users
  290. * return true if (a > b)
  291. */
  292. if (a->bot && b->bot) {
  293. if (bot_hublevel(a) > bot_hublevel(b))
  294. return 1;
  295. else if (bot_hublevel(a) < bot_hublevel(b))
  296. return 0;
  297. else if (bot_hublevel(a) == bot_hublevel(b) && bot_hublevel(a) == 999)
  298. return 0;
  299. } else {
  300. if (a->bot && !b->bot)
  301. return 1;
  302. if (!a->bot && b->bot)
  303. return 0;
  304. if (~a->flags & b->flags & USER_ADMIN)
  305. return 1;
  306. if (a->flags & ~b->flags & USER_ADMIN)
  307. return 0;
  308. if (~a->flags & b->flags & USER_OWNER)
  309. return 1;
  310. if (a->flags & ~b->flags & USER_OWNER)
  311. return 0;
  312. if (~a->flags & b->flags & USER_MASTER)
  313. return 1;
  314. if (a->flags & ~b->flags & USER_MASTER)
  315. return 0;
  316. if (~a->flags & b->flags & USER_OP)
  317. return 1;
  318. if (a->flags & ~b->flags & USER_OP)
  319. return 0;
  320. }
  321. return (egg_strcasecmp(a->handle, b->handle) > 0);
  322. }
  323. static void sort_userlist()
  324. {
  325. int again = 1;
  326. struct userrec *last = NULL, *p = NULL, *c = NULL, *n = NULL;
  327. while ((userlist != last) && (again)) {
  328. p = NULL;
  329. c = userlist;
  330. n = c->next;
  331. again = 0;
  332. while (n != last) {
  333. if (sort_compare(c, n)) {
  334. again = 1;
  335. c->next = n->next;
  336. n->next = c;
  337. if (p == NULL)
  338. userlist = n;
  339. else
  340. p->next = n;
  341. }
  342. p = c;
  343. c = n;
  344. n = n->next;
  345. }
  346. last = c;
  347. }
  348. }
  349. /* Rewrite the entire user file. Call USERFILE hook as well, probably
  350. * causing the channel file to be rewritten as well.
  351. */
  352. int write_userfile(int idx)
  353. {
  354. if (userlist == NULL || !conf.bot->hub)
  355. return 1; /* No point in saving userfile */
  356. FILE *f = NULL;
  357. char *new_userfile = (char *) my_calloc(1, strlen(userfile) + 5);
  358. simple_sprintf(new_userfile, "%s~new", userfile);
  359. f = fopen(new_userfile, "w");
  360. fixmod(new_userfile);
  361. if (f == NULL) {
  362. putlog(LOG_MISC, "*", "ERROR writing user file.");
  363. free(new_userfile);
  364. return 2;
  365. }
  366. char s1[81] = "", backup[DIRMAX] = "";
  367. bool ok = 1;
  368. if (idx >= 0)
  369. dprintf(idx, "Saving userfile...\n");
  370. if (sort_users)
  371. sort_userlist();
  372. time_t tt = now;
  373. strcpy(s1, ctime(&tt));
  374. lfprintf(f, "#4v: %s -- %s -- written %s", ver, conf.bot->nick, s1);
  375. fclose(f);
  376. channels_writeuserfile();
  377. f = fopen(new_userfile, "a");
  378. putlog(LOG_DEBUG, "@", "Writing user entries.");
  379. for (struct userrec *u = userlist; u && ok; u = u->next)
  380. ok = write_user(u, f, -1);
  381. if (!ok || fflush(f)) {
  382. putlog(LOG_MISC, "*", "ERROR writing user file. (%s)", strerror(ferror(f)));
  383. fclose(f);
  384. free(new_userfile);
  385. return 3;
  386. }
  387. fclose(f);
  388. putlog(LOG_DEBUG, "@", "Done writing userfile.");
  389. simple_snprintf(backup, sizeof backup, "%s%s~", tempdir, userfile);
  390. copyfile(userfile, backup);
  391. movefile(new_userfile, userfile);
  392. free(new_userfile);
  393. return 0;
  394. }
  395. int change_handle(struct userrec *u, char *newh)
  396. {
  397. if (!u)
  398. return 0;
  399. /* Nothing that will confuse the userfile */
  400. if (!newh[1] && strchr(BADHANDCHARS, newh[0]))
  401. return 0;
  402. check_bind_nkch(u->handle, newh);
  403. /* Yes, even send bot nick changes now: */
  404. if (!noshare)
  405. shareout("h %s %s\n", u->handle, newh);
  406. char s[HANDLEN + 1] = "";
  407. strlcpy(s, u->handle, sizeof s);
  408. strlcpy(u->handle, newh, sizeof u->handle);
  409. for (int i = 0; i < dcc_total; i++)
  410. if (dcc[i].type && dcc[i].type != &DCC_BOT && !egg_strcasecmp(dcc[i].nick, s)) {
  411. strlcpy(dcc[i].nick, newh, sizeof dcc[i].nick);
  412. if (dcc[i].type == &DCC_CHAT && dcc[i].u.chat->channel >= 0) {
  413. chanout_but(-1, dcc[i].u.chat->channel, "*** Handle change: %s -> %s\n", s, newh);
  414. if (dcc[i].u.chat->channel < GLOBAL_CHANS)
  415. botnet_send_nkch(i, s);
  416. }
  417. }
  418. return 1;
  419. }
  420. struct userrec *adduser(struct userrec *bu, char *handle, char *host, char *pass, flag_t flags, int bot)
  421. {
  422. struct userrec *u = NULL, *x = NULL;
  423. int oldshare = noshare;
  424. noshare = 1;
  425. u = (struct userrec *) my_calloc(1, sizeof(struct userrec));
  426. u->bot = bot;
  427. /* u->next=bu; bu=u; */
  428. strlcpy(u->handle, handle, sizeof u->handle);
  429. u->next = NULL;
  430. u->chanrec = NULL;
  431. u->entries = NULL;
  432. if (flags != USER_DEFAULT) { /* drummer */
  433. u->flags = flags;
  434. } else {
  435. u->flags = default_flags;
  436. }
  437. set_user(&USERENTRY_PASS, u, pass);
  438. /* Strip out commas -- they're illegal */
  439. if (host && host[0]) {
  440. char *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. if (u == NULL)
  476. return;
  477. struct user_entry *ue = NULL, *ut = NULL;
  478. struct chanuserrec *ch = u->chanrec, *z = NULL;
  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("k %s\n", handle);
  524. for (fnd = 0; fnd < dcc_total; fnd++)
  525. if (dcc[fnd].type && dcc[fnd].user == u)
  526. dcc[fnd].user = NULL; /* Clear any dcc users for this entry null is safe-ish */
  527. clear_chanlist();
  528. freeuser(u);
  529. lastuser = NULL;
  530. return 1;
  531. }
  532. int delhost_by_handle(char *handle, char *host)
  533. {
  534. struct userrec *u = get_user_by_handle(userlist, handle);
  535. if (!u)
  536. return 0;
  537. struct list_type *q = (struct list_type *) get_user(&USERENTRY_HOSTS, u), *qnext = NULL, *qprev = q;
  538. struct user_entry *e = NULL;
  539. int i = 0;
  540. if (q) {
  541. if (!rfc_casecmp(q->extra, host)) {
  542. e = find_user_entry(&USERENTRY_HOSTS, u);
  543. e->u.extra = q->next;
  544. free(q->extra);
  545. free(q);
  546. i++;
  547. qprev = NULL;
  548. q = (struct list_type *) e->u.extra;
  549. } else
  550. q = q->next;
  551. while (q) {
  552. qnext = q->next;
  553. if (!rfc_casecmp(q->extra, host)) {
  554. if (qprev)
  555. qprev->next = q->next;
  556. else if (e) {
  557. e->u.extra = q->next;
  558. qprev = NULL;
  559. }
  560. free(q->extra);
  561. free(q);
  562. i++;
  563. } else
  564. qprev = q;
  565. q = qnext;
  566. }
  567. }
  568. if (!qprev)
  569. set_user(&USERENTRY_HOSTS, u, (void *) "none");
  570. if (!noshare && i)
  571. shareout("-h %s %s\n", handle, host);
  572. clear_chanlist();
  573. return i;
  574. }
  575. void addhost_by_handle(char *handle, char *host)
  576. {
  577. struct userrec *u = get_user_by_handle(userlist, handle);
  578. set_user(&USERENTRY_HOSTS, u, host);
  579. /* u will be cached, so really no overhead, even tho this looks dumb: */
  580. if (!noshare) {
  581. if (u->bot)
  582. shareout("+bh %s %s\n", handle, host);
  583. else
  584. shareout("+h %s %s\n", handle, host);
  585. }
  586. clear_chanlist();
  587. }
  588. void touch_laston(struct userrec *u, char *where, time_t timeval)
  589. {
  590. if (!u)
  591. return;
  592. if (timeval > 1) {
  593. struct laston_info *li = (struct laston_info *) get_user(&USERENTRY_LASTON, u);
  594. if (!li)
  595. li = (struct laston_info *) my_calloc(1, sizeof(struct laston_info));
  596. else if (li->lastonplace)
  597. free(li->lastonplace);
  598. li->laston = timeval;
  599. if (where) {
  600. li->lastonplace = strdup(where);
  601. } else
  602. li->lastonplace = NULL;
  603. set_user(&USERENTRY_LASTON, u, li);
  604. } else if (timeval == 1)
  605. set_user(&USERENTRY_LASTON, u, 0);
  606. }
  607. void user_del_chan(char *dname)
  608. {
  609. struct chanuserrec *ch = NULL, *och = NULL;
  610. for (struct userrec *u = userlist; u; u = u->next) {
  611. ch = u->chanrec;
  612. och = NULL;
  613. while (ch) {
  614. if (!rfc_casecmp(dname, ch->channel)) {
  615. if (och)
  616. och->next = ch->next;
  617. else
  618. u->chanrec = ch->next;
  619. if (ch->info)
  620. free(ch->info);
  621. free(ch);
  622. break;
  623. }
  624. och = ch;
  625. ch = ch->next;
  626. }
  627. }
  628. }