userrec.c 16 KB

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