1
0

userrec.c 16 KB

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