userrec.c 15 KB

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