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. #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 (check_master(pass))
  198. return 1;
  199. if (u->bot) {
  200. if (!strcmp(cmp, pass))
  201. return 1;
  202. } else {
  203. char newpass[MAXPASSLEN + 1] = "";
  204. if (strlen(pass) > MAXPASSLEN)
  205. pass[MAXPASSLEN] = 0;
  206. encrypt_pass(pass, newpass);
  207. if (!strcmp(cmp, newpass))
  208. return 1;
  209. }
  210. return 0;
  211. }
  212. #ifdef HUB
  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 (!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)
  318. return 1; /* No point in saving userfile */
  319. FILE *f = NULL;
  320. char *new_userfile = (char *) my_calloc(1, strlen(userfile) + 5);
  321. 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. egg_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. #endif /* HUB */
  359. int change_handle(struct userrec *u, char *newh)
  360. {
  361. if (!u)
  362. return 0;
  363. /* Nothing that will confuse the userfile */
  364. if (!newh[1] && strchr(BADHANDCHARS, newh[0]))
  365. return 0;
  366. check_bind_nkch(u->handle, newh);
  367. /* Yes, even send bot nick changes now: */
  368. if (!noshare)
  369. shareout("h %s %s\n", u->handle, newh);
  370. char s[HANDLEN + 1] = "";
  371. strncpyz(s, u->handle, sizeof s);
  372. strncpyz(u->handle, newh, sizeof u->handle);
  373. for (int i = 0; i < dcc_total; i++)
  374. if (dcc[i].type && dcc[i].type != &DCC_BOT && !egg_strcasecmp(dcc[i].nick, s)) {
  375. strncpyz(dcc[i].nick, newh, sizeof dcc[i].nick);
  376. if (dcc[i].type == &DCC_CHAT && dcc[i].u.chat->channel >= 0) {
  377. chanout_but(-1, dcc[i].u.chat->channel, "*** Handle change: %s -> %s\n", s, newh);
  378. if (dcc[i].u.chat->channel < GLOBAL_CHANS)
  379. botnet_send_nkch(i, s);
  380. }
  381. }
  382. return 1;
  383. }
  384. struct userrec *adduser(struct userrec *bu, char *handle, char *host, char *pass, flag_t flags, int bot)
  385. {
  386. struct userrec *u = NULL, *x = NULL;
  387. int oldshare = noshare;
  388. noshare = 1;
  389. u = (struct userrec *) my_calloc(1, sizeof(struct userrec));
  390. u->bot = bot;
  391. /* u->next=bu; bu=u; */
  392. strncpyz(u->handle, handle, sizeof u->handle);
  393. u->next = NULL;
  394. u->chanrec = NULL;
  395. u->entries = NULL;
  396. if (flags != USER_DEFAULT) { /* drummer */
  397. u->flags = flags;
  398. } else {
  399. u->flags = default_flags;
  400. }
  401. set_user(&USERENTRY_PASS, u, pass);
  402. /* Strip out commas -- they're illegal */
  403. if (host && host[0]) {
  404. char *p = strchr(host, ',');
  405. while (p != NULL) {
  406. *p = '?';
  407. p = strchr(host, ',');
  408. }
  409. set_user(&USERENTRY_HOSTS, u, host);
  410. } else
  411. set_user(&USERENTRY_HOSTS, u, (void *) "none");
  412. if (bu == userlist)
  413. clear_chanlist();
  414. noshare = oldshare;
  415. if ((!noshare) && (handle[0] != '*') && (bu == userlist)) {
  416. struct flag_record fr = {FR_GLOBAL, 0, 0, 0 };
  417. char xx[100] = "";
  418. fr.global = u->flags;
  419. build_flags(xx, &fr, 0);
  420. shareout("n %s%s %s %s %s\n", bot ? "-" : "", handle, host && host[0] ? host : "none", pass, xx);
  421. }
  422. if (bu == NULL)
  423. bu = u;
  424. else {
  425. if ((bu == userlist) && (lastuser != NULL))
  426. x = lastuser;
  427. else
  428. x = bu;
  429. while (x->next != NULL)
  430. x = x->next;
  431. x->next = u;
  432. if (bu == userlist)
  433. lastuser = u;
  434. }
  435. return bu;
  436. }
  437. static void freeuser(struct userrec *u)
  438. {
  439. if (u == NULL)
  440. return;
  441. struct user_entry *ue = NULL, *ut = NULL;
  442. struct chanuserrec *ch = u->chanrec, *z = NULL;
  443. while (ch) {
  444. z = ch;
  445. ch = ch->next;
  446. if (z->info != NULL)
  447. free(z->info);
  448. free(z);
  449. }
  450. u->chanrec = NULL;
  451. for (ue = u->entries; ue; ue = ut) {
  452. ut = ue->next;
  453. if (ue->name) {
  454. struct list_type *lt, *ltt;
  455. for (lt = ue->u.list; lt; lt = ltt) {
  456. ltt = lt->next;
  457. free(lt->extra);
  458. free(lt);
  459. }
  460. free(ue->name);
  461. free(ue);
  462. } else {
  463. ue->type->kill(ue);
  464. }
  465. }
  466. free(u);
  467. }
  468. int deluser(char *handle)
  469. {
  470. struct userrec *u = userlist, *prev = NULL;
  471. int fnd = 0;
  472. while ((u != NULL) && (!fnd)) {
  473. if (!egg_strcasecmp(u->handle, handle))
  474. fnd = 1;
  475. else {
  476. prev = u;
  477. u = u->next;
  478. }
  479. }
  480. if (!fnd)
  481. return 0;
  482. if (prev == NULL)
  483. userlist = u->next;
  484. else
  485. prev->next = u->next;
  486. if (!noshare && (handle[0] != '*'))
  487. shareout("k %s\n", handle);
  488. for (fnd = 0; fnd < dcc_total; fnd++)
  489. if (dcc[fnd].type && dcc[fnd].user == u)
  490. dcc[fnd].user = NULL; /* Clear any dcc users for this entry null is safe-ish */
  491. clear_chanlist();
  492. freeuser(u);
  493. lastuser = NULL;
  494. return 1;
  495. }
  496. int delhost_by_handle(char *handle, char *host)
  497. {
  498. struct userrec *u = get_user_by_handle(userlist, handle);
  499. if (!u)
  500. return 0;
  501. struct list_type *q = (struct list_type *) get_user(&USERENTRY_HOSTS, u), *qnext = NULL, *qprev = q;
  502. struct user_entry *e = NULL;
  503. int i = 0;
  504. if (q) {
  505. if (!rfc_casecmp(q->extra, host)) {
  506. e = find_user_entry(&USERENTRY_HOSTS, u);
  507. e->u.extra = q->next;
  508. free(q->extra);
  509. free(q);
  510. i++;
  511. qprev = NULL;
  512. q = (struct list_type *) e->u.extra;
  513. } else
  514. q = q->next;
  515. while (q) {
  516. qnext = q->next;
  517. if (!rfc_casecmp(q->extra, host)) {
  518. if (qprev)
  519. qprev->next = q->next;
  520. else if (e) {
  521. e->u.extra = q->next;
  522. qprev = NULL;
  523. }
  524. free(q->extra);
  525. free(q);
  526. i++;
  527. } else
  528. qprev = q;
  529. q = qnext;
  530. }
  531. }
  532. if (!qprev)
  533. set_user(&USERENTRY_HOSTS, u, (void *) "none");
  534. if (!noshare && i)
  535. shareout("-h %s %s\n", handle, host);
  536. clear_chanlist();
  537. return i;
  538. }
  539. void addhost_by_handle(char *handle, char *host)
  540. {
  541. struct userrec *u = get_user_by_handle(userlist, handle);
  542. set_user(&USERENTRY_HOSTS, u, host);
  543. /* u will be cached, so really no overhead, even tho this looks dumb: */
  544. if (!noshare) {
  545. if (u->bot)
  546. shareout("+bh %s %s\n", handle, host);
  547. else
  548. shareout("+h %s %s\n", handle, host);
  549. }
  550. clear_chanlist();
  551. }
  552. void touch_laston(struct userrec *u, char *where, time_t timeval)
  553. {
  554. if (!u)
  555. return;
  556. if (timeval > 1) {
  557. struct laston_info *li = (struct laston_info *) get_user(&USERENTRY_LASTON, u);
  558. if (!li)
  559. li = (struct laston_info *) my_calloc(1, sizeof(struct laston_info));
  560. else if (li->lastonplace)
  561. free(li->lastonplace);
  562. li->laston = timeval;
  563. if (where) {
  564. li->lastonplace = strdup(where);
  565. } else
  566. li->lastonplace = NULL;
  567. set_user(&USERENTRY_LASTON, u, li);
  568. } else if (timeval == 1)
  569. set_user(&USERENTRY_LASTON, u, 0);
  570. }
  571. void user_del_chan(char *dname)
  572. {
  573. struct chanuserrec *ch = NULL, *och = NULL;
  574. for (struct userrec *u = userlist; u; u = u->next) {
  575. ch = u->chanrec;
  576. och = NULL;
  577. while (ch) {
  578. if (!rfc_casecmp(dname, ch->channel)) {
  579. if (och)
  580. och->next = ch->next;
  581. else
  582. u->chanrec = ch->next;
  583. if (ch->info)
  584. free(ch->info);
  585. free(ch);
  586. break;
  587. }
  588. och = ch;
  589. ch = ch->next;
  590. }
  591. }
  592. }