userrec.c 15 KB

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