userrec.c 15 KB

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