flags.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534
  1. /*
  2. * flags.c -- handles:
  3. * all the flag matching/conversion functions in one neat package :)
  4. *
  5. */
  6. #include "common.h"
  7. #include "crypt.h"
  8. #include "src/mod/share.mod/share.h"
  9. #include "rfc1459.h"
  10. #include "userrec.h"
  11. #include "misc.h"
  12. #include "dccutil.h"
  13. #include "userent.h"
  14. #include "users.h"
  15. flag_t FLAG[128];
  16. void
  17. init_flags()
  18. {
  19. unsigned char i;
  20. for (i = 0; i < 'A'; i++)
  21. FLAG[i] = 0;
  22. for (; i <= 'Z'; i++)
  23. FLAG[i] = (flag_t) 1 << (26 + (i - 'A'));
  24. for (; i < 'a'; i++)
  25. FLAG[i] = 0;
  26. for (; i <= 'z'; i++)
  27. FLAG[i] = (flag_t) 1 << (i - 'a');
  28. for (; i < 128; i++)
  29. FLAG[i] = 0;
  30. }
  31. /* Some flags are mutually exclusive -- this roots them out
  32. */
  33. flag_t
  34. sanity_check(flag_t atr, int bot)
  35. {
  36. if (bot && (atr & (USER_PARTY | USER_MASTER | USER_OWNER | USER_ADMIN | USER_HUBA | USER_CHUBA)))
  37. atr &= ~(USER_PARTY | USER_MASTER | USER_OWNER | USER_ADMIN | USER_HUBA | USER_CHUBA);
  38. /* only bots should be there: */
  39. if (!bot && (atr & (USER_DOLIMIT | USER_DOVOICE | USER_UPDATEHUB | USER_CHANHUB)))
  40. atr &= ~(USER_DOLIMIT | USER_DOVOICE | USER_UPDATEHUB | USER_CHANHUB);
  41. if ((atr & USER_OP) && (atr & USER_DEOP))
  42. atr &= ~(USER_OP | USER_DEOP);
  43. if ((atr & USER_AUTOOP) && (atr & USER_DEOP))
  44. atr &= ~(USER_AUTOOP | USER_DEOP);
  45. if ((atr & USER_VOICE) && (atr & USER_QUIET))
  46. atr &= ~(USER_VOICE | USER_QUIET);
  47. /* Can't be admin without also being owner and having hub access */
  48. if (atr & USER_ADMIN)
  49. atr |= USER_OWNER | USER_HUBA | USER_PARTY;
  50. /* Hub access gets chanhub access */
  51. if (atr & USER_HUBA)
  52. atr |= USER_CHUBA;
  53. if (atr & USER_OWNER) {
  54. atr |= USER_MASTER;
  55. }
  56. /* Master implies botmaster, op */
  57. if (atr & USER_MASTER)
  58. atr |= USER_OP | USER_CHUBA;
  59. /* Can't be botnet master without party-line access */
  60. /* if (atr & USER_BOTMAST)
  61. atr |= USER_PARTY;
  62. */
  63. return atr;
  64. }
  65. /* Sanity check on channel attributes
  66. */
  67. flag_t
  68. chan_sanity_check(flag_t chatr)
  69. {
  70. /* admin for chan does shit.. */
  71. if (chatr & USER_ADMIN)
  72. chatr &= ~(USER_ADMIN);
  73. if ((chatr & USER_OP) && (chatr & USER_DEOP))
  74. chatr &= ~(USER_OP | USER_DEOP);
  75. if ((chatr & USER_AUTOOP) && (chatr & USER_DEOP))
  76. chatr &= ~(USER_AUTOOP | USER_DEOP);
  77. if ((chatr & USER_VOICE) && (chatr & USER_QUIET))
  78. chatr &= ~(USER_VOICE | USER_QUIET);
  79. /* Can't be channel owner without also being channel master */
  80. if (chatr & USER_OWNER)
  81. chatr |= USER_MASTER;
  82. /* Master implies op */
  83. if (chatr & USER_MASTER)
  84. chatr |= USER_OP;
  85. return chatr;
  86. }
  87. /* Get icon symbol for a user (depending on access level)
  88. *
  89. * (*) owner on any channel
  90. * (+) master on any channel
  91. * (%) botnet master
  92. * (@) op on any channel
  93. * (-) other
  94. */
  95. char
  96. geticon(int idx)
  97. {
  98. struct flag_record fr = { FR_GLOBAL | FR_CHAN | FR_ANYWH, 0, 0, 0 };
  99. if (!dcc[idx].user)
  100. return '-';
  101. get_user_flagrec(dcc[idx].user, &fr, 0);
  102. if (glob_admin(fr))
  103. return '^';
  104. if (chan_owner(fr))
  105. return '*';
  106. if (chan_master(fr))
  107. return '+';
  108. if (chan_op(fr))
  109. return '@';
  110. return '-';
  111. }
  112. void
  113. break_down_flags(const char *string, struct flag_record *plus, struct flag_record *minus)
  114. {
  115. struct flag_record *which = plus;
  116. int chan = 0; /* 0 = glob, 1 = chan */
  117. int flags = plus->match;
  118. if (!(flags & FR_GLOBAL)) {
  119. if (flags & FR_CHAN)
  120. chan = 1;
  121. else
  122. return; /* We dont actually want any..huh? */
  123. }
  124. egg_bzero(plus, sizeof(struct flag_record));
  125. if (minus)
  126. egg_bzero(minus, sizeof(struct flag_record));
  127. plus->match = FR_OR; /* Default binding type OR */
  128. while (*string) {
  129. switch (*string) {
  130. case '+':
  131. which = plus;
  132. break;
  133. case '-':
  134. which = minus ? minus : plus;
  135. break;
  136. case '|':
  137. case '&':
  138. if (!chan) {
  139. if (*string == '|')
  140. plus->match = FR_OR;
  141. else
  142. plus->match = FR_AND;
  143. }
  144. which = plus;
  145. chan++;
  146. if (chan == 2)
  147. string = "";
  148. else if (chan == 3)
  149. chan = 1;
  150. break; /* switch() */
  151. default:
  152. {
  153. flag_t flagbit = FLAG[(unsigned char) *string];
  154. if (flagbit) {
  155. switch (chan) {
  156. case 0:
  157. /* which->global |= (flag_t) 1 << (*string - 'a'); */
  158. which->global |=flagbit;
  159. break;
  160. case 1:
  161. /* which->chan |= (flag_t) 1 << (*string - 'a'); */
  162. which->chan |= flagbit;
  163. break;
  164. }
  165. }
  166. }
  167. }
  168. string++;
  169. }
  170. /*
  171. * for (which = plus; which; which = (which == plus ? minus : 0)) {
  172. * which->global &= USER_VALID;
  173. * which->chan &= CHAN_VALID;
  174. * }
  175. */
  176. plus->match |= flags;
  177. if (minus) {
  178. minus->match |= flags;
  179. if (!(plus->match & (FR_AND | FR_OR)))
  180. plus->match |= FR_OR;
  181. }
  182. }
  183. static int
  184. flag2str(char *string, flag_t flag)
  185. {
  186. unsigned char c;
  187. char *old = string;
  188. for (c = 0; c < 128; c++)
  189. if (flag & FLAG[c])
  190. *string++ = c;
  191. if (string == old)
  192. *string++ = '-';
  193. return string - old;
  194. }
  195. int
  196. build_flags(char *string, struct flag_record *plus, struct flag_record *minus)
  197. {
  198. char *old = string;
  199. if (plus->match & FR_GLOBAL) {
  200. if (minus && plus->global)
  201. *string++ = '+';
  202. string += flag2str(string, plus->global);
  203. if (minus && minus->global) {
  204. *string++ = '-';
  205. string += flag2str(string, minus->global);
  206. }
  207. }
  208. if (plus->match & FR_CHAN) {
  209. /* FIXME: BLAH */
  210. /* if (plus->match & (FR_GLOBAL | FR_BOT))
  211. *string++ = (plus->match & FR_AND) ? '&' : '|';
  212. */
  213. if (plus->match & FR_GLOBAL)
  214. *string++ = (plus->match & FR_AND) ? '&' : '|';
  215. if (minus && plus->chan)
  216. *string++ = '+';
  217. string += flag2str(string, plus->chan);
  218. if (minus && minus->chan) {
  219. *string++ = '-';
  220. string += flag2str(string, minus->global);
  221. }
  222. }
  223. if (string == old) {
  224. *string++ = '-';
  225. *string = 0;
  226. return 0;
  227. }
  228. *string = 0;
  229. return string - old;
  230. }
  231. /* Returns 1 if flags match, 0 if they don't. */
  232. int
  233. flagrec_ok(struct flag_record *req, struct flag_record *have)
  234. {
  235. if (req->match & FR_AND) {
  236. return flagrec_eq(req, have);
  237. } else if (req->match & FR_OR) {
  238. int hav = have->global;
  239. /* no flags, is a match */
  240. if (!req->chan && !req->global)
  241. return 1;
  242. if (hav & req->global)
  243. return 1;
  244. if (have->chan & req->chan)
  245. return 1;
  246. return 0;
  247. }
  248. return 0;
  249. }
  250. /* Returns 1 if flags match, 0 if they don't. */
  251. int
  252. flagrec_eq(struct flag_record *req, struct flag_record *have)
  253. {
  254. if (req->match & FR_AND) {
  255. if (req->match & FR_GLOBAL) {
  256. if ((req->global &have->global) !=req->global)
  257. return 0;
  258. }
  259. if (req->match & FR_CHAN) {
  260. if ((req->chan & have->chan) != req->chan)
  261. return 0;
  262. }
  263. return 1;
  264. } else if (req->match & FR_OR) {
  265. if (!req->chan && !req->global)
  266. return 1;
  267. if (req->match & FR_GLOBAL) {
  268. if (have->global &req->global)
  269. return 1;
  270. }
  271. if (req->match & FR_CHAN) {
  272. if (have->chan & req->chan)
  273. return 1;
  274. }
  275. return 0;
  276. }
  277. return 0; /* fr0k3 binding, dont pass it */
  278. }
  279. void
  280. set_user_flagrec(struct userrec *u, struct flag_record *fr, const char *chname)
  281. {
  282. struct chanuserrec *cr = NULL;
  283. int oldflags = fr->match;
  284. char buffer[100] = "";
  285. struct chanset_t *ch;
  286. if (!u)
  287. return;
  288. if (oldflags & FR_GLOBAL) {
  289. u->flags = fr->global;
  290. if (!noshare) {
  291. fr->match = FR_GLOBAL;
  292. build_flags(buffer, fr, NULL);
  293. shareout("a %s %s\n", u->handle, buffer);
  294. }
  295. }
  296. if ((oldflags & FR_CHAN) && chname) {
  297. for (cr = u->chanrec; cr; cr = cr->next)
  298. if (!rfc_casecmp(chname, cr->channel))
  299. break;
  300. ch = findchan_by_dname(chname);
  301. if (!cr && ch) {
  302. cr = calloc(1, sizeof(struct chanuserrec));
  303. cr->next = u->chanrec;
  304. u->chanrec = cr;
  305. strncpyz(cr->channel, chname, sizeof cr->channel);
  306. }
  307. if (cr && ch) {
  308. cr->flags = fr->chan;
  309. if (!noshare) {
  310. fr->match = FR_CHAN;
  311. build_flags(buffer, fr, NULL);
  312. shareout("a %s %s %s\n", u->handle, buffer, chname);
  313. }
  314. }
  315. }
  316. fr->match = oldflags;
  317. }
  318. /* Always pass the dname (display name) to this function for chname <cybah>
  319. */
  320. void
  321. get_user_flagrec(struct userrec *u, struct flag_record *fr, const char *chname)
  322. {
  323. struct chanuserrec *cr = NULL;
  324. fr->bot = 0;
  325. if (!u) {
  326. fr->global = fr->chan = 0;
  327. return;
  328. }
  329. if (u->bot)
  330. fr->bot = 1;
  331. if (fr->match & FR_GLOBAL) {
  332. fr->global = u->flags;
  333. } else {
  334. fr->global = 0;
  335. }
  336. if (fr->match & FR_CHAN) {
  337. if (fr->match & FR_ANYWH) {
  338. fr->chan = u->flags;
  339. for (cr = u->chanrec; cr; cr = cr->next)
  340. if (findchan_by_dname(cr->channel)) {
  341. fr->chan |= cr->flags;
  342. }
  343. } else {
  344. if (chname) {
  345. for (cr = u->chanrec; cr; cr = cr->next) {
  346. if (!rfc_casecmp(chname, cr->channel))
  347. break;
  348. }
  349. }
  350. if (cr) {
  351. fr->chan = cr->flags;
  352. } else {
  353. fr->chan = 0;
  354. }
  355. }
  356. }
  357. }
  358. /* private returns 0 if user has access, and 1 if they dont because of +private
  359. * This function does not check if the user has "op" access, it only checks if the user is
  360. * restricted by +private for the channel
  361. */
  362. inline int
  363. private(struct flag_record fr, struct chanset_t *chan, int type)
  364. {
  365. if (!chan || !channel_private(chan) || glob_bot(fr) || glob_owner(fr))
  366. return 0; /* user is implicitly not restricted by +private, they may however be lacking other flags */
  367. if (type == PRIV_OP) {
  368. /* |o implies all flags above. n| has access to all +private. Bots are exempt. */
  369. if (chan_op(fr))
  370. return 0;
  371. } else if (type == PRIV_VOICE) {
  372. if (chan_voice(fr))
  373. return 0;
  374. }
  375. return 1; /* user is restricted by +private */
  376. }
  377. inline int
  378. chk_op(struct flag_record fr, struct chanset_t *chan)
  379. {
  380. if (!chan || (!private(fr, chan, PRIV_OP) && !chk_deop(fr))) {
  381. if (chan_op(fr) || (glob_op(fr) && !chan_deop(fr)))
  382. return 1;
  383. }
  384. return 0;
  385. }
  386. inline int
  387. chk_autoop(struct flag_record fr, struct chanset_t *chan)
  388. {
  389. if (glob_bot(fr))
  390. return 0;
  391. if (!chan || (!channel_take(chan) && !private(fr, chan, PRIV_OP) && chk_op(fr, chan) && !chk_deop(fr))) {
  392. if (channel_autoop(chan) || chan_autoop(fr) || glob_autoop(fr))
  393. return 1;
  394. }
  395. return 0;
  396. }
  397. inline int
  398. chk_deop(struct flag_record fr)
  399. {
  400. if (chan_deop(fr) || (glob_deop(fr) && !chan_op(fr)))
  401. return 1;
  402. else
  403. return 0;
  404. }
  405. inline int
  406. chk_voice(struct flag_record fr, struct chanset_t *chan)
  407. {
  408. if (!chan || (!private(fr, chan, PRIV_VOICE) && !chk_devoice(fr))) {
  409. if (chan_voice(fr) || (glob_voice(fr) && !chan_quiet(fr)))
  410. return 1;
  411. }
  412. return 0;
  413. }
  414. inline int
  415. chk_devoice(struct flag_record fr)
  416. {
  417. if (chan_quiet(fr) || (glob_quiet(fr) && !chan_voice(fr)))
  418. return 1;
  419. else
  420. return 0;
  421. }
  422. inline int
  423. chk_noflood(struct flag_record fr)
  424. {
  425. return (chan_noflood(fr) || glob_noflood(fr));
  426. }
  427. inline int
  428. isupdatehub()
  429. {
  430. #ifdef HUB
  431. if (conf.bot->u && (conf.bot->u->flags & USER_UPDATEHUB))
  432. return 1;
  433. else
  434. #endif /* HUB */
  435. return 0;
  436. }
  437. inline int
  438. ischanhub()
  439. {
  440. if (conf.bot->u && (conf.bot->u->flags & USER_CHANHUB))
  441. return 1;
  442. else
  443. return 0;
  444. }
  445. int
  446. dovoice(struct chanset_t *chan)
  447. {
  448. struct flag_record fr = { FR_GLOBAL | FR_CHAN, 0, 0, 0 };
  449. if (!chan)
  450. return 0;
  451. get_user_flagrec(conf.bot->u, &fr, chan->dname);
  452. if (glob_dovoice(fr) || chan_dovoice(fr))
  453. return 1;
  454. return 0;
  455. }
  456. int
  457. dolimit(struct chanset_t *chan)
  458. {
  459. struct flag_record fr = { FR_GLOBAL | FR_CHAN, 0, 0, 0 };
  460. if (!chan)
  461. return 0;
  462. get_user_flagrec(conf.bot->u, &fr, chan->dname);
  463. if (glob_dolimit(fr) || chan_dolimit(fr))
  464. return 1;
  465. return 0;
  466. }
  467. int
  468. whois_access(struct userrec *user, struct userrec *whois_user)
  469. {
  470. struct flag_record fr = { FR_GLOBAL | FR_CHAN, 0, 0, 0 }, whois = {
  471. FR_GLOBAL | FR_CHAN, 0, 0, 0};
  472. get_user_flagrec(user, &fr, NULL);
  473. get_user_flagrec(whois_user, &whois, NULL);
  474. if ((glob_master(whois) && !glob_master(fr)) ||
  475. (glob_owner(whois) && !glob_owner(fr)) ||
  476. (glob_admin(whois) && !glob_admin(fr)) || (glob_bot(whois) && !glob_master(fr)))
  477. return 0;
  478. return 1;
  479. }