You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

73 lines
2.0 KiB

  1. /*
  2. Copyright (c) 2016, Antonio SJ Musumeci <trapexit@spawn.link>
  3. Permission to use, copy, modify, and/or distribute this software for any
  4. purpose with or without fee is hereby granted, provided that the above
  5. copyright notice and this permission notice appear in all copies.
  6. THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  7. WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  8. MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  9. ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  10. WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  11. ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  12. OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  13. */
  14. #pragma once
  15. #include <sys/types.h>
  16. #include <unistd.h>
  17. #include <array>
  18. #define MAXGIDS 32
  19. #define MAXRECS 256
  20. // GIDCache is a global, per thread cache of uid to gid + supplemental
  21. // groups mapping for use when threads change credentials. This is
  22. // needed due to the high cost of querying such information. The cache
  23. // instance should always be thread local and live the lifetime of the
  24. // app. The constructor will register the instance so they can each be
  25. // told to invalidate the cache on demand. A second instance on the
  26. // same thread will cause an assert to be triggered.
  27. struct GIDRecord
  28. {
  29. uid_t uid;
  30. int size;
  31. gid_t gids[MAXGIDS];
  32. bool
  33. operator<(const struct GIDRecord &b) const;
  34. };
  35. struct GIDCache
  36. {
  37. public:
  38. GIDCache();
  39. public:
  40. bool invalidate;
  41. size_t size;
  42. std::array<GIDRecord,MAXRECS> recs;
  43. private:
  44. GIDRecord *begin(void);
  45. GIDRecord *end(void);
  46. GIDRecord *allocrec(void);
  47. GIDRecord *lower_bound(GIDRecord *begin,
  48. GIDRecord *end,
  49. const uid_t uid);
  50. GIDRecord *cache(const uid_t uid,
  51. const gid_t gid);
  52. public:
  53. int
  54. initgroups(const uid_t uid,
  55. const gid_t gid);
  56. public:
  57. static void invalidate_all_caches();
  58. };