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.

215 lines
4.2 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. #include "rnd.hpp"
  15. #include <grp.h>
  16. #include <pwd.h>
  17. #include <stdlib.h>
  18. #include <sys/types.h>
  19. #include <unistd.h>
  20. #if defined __linux__ and UGID_USE_RWLOCK == 0
  21. # include <sys/syscall.h>
  22. #elif __APPLE__
  23. # include <sys/param.h>
  24. #endif
  25. #include <cassert>
  26. #include <cstdlib>
  27. #include <algorithm>
  28. #include <unordered_map>
  29. #include <mutex>
  30. #include "gidcache.hpp"
  31. std::mutex g_REGISTERED_CACHES_MUTEX;
  32. std::unordered_map<pthread_t,GIDCache*> g_REGISTERED_CACHES;
  33. inline
  34. bool
  35. GIDRecord::operator<(const struct GIDRecord &b) const
  36. {
  37. return uid < b.uid;
  38. }
  39. GIDCache::GIDCache()
  40. : invalidate(false),
  41. size(0),
  42. recs()
  43. {
  44. std::lock_guard<std::mutex> guard(g_REGISTERED_CACHES_MUTEX);
  45. bool inserted;
  46. pthread_t pthid;
  47. pthid = pthread_self();
  48. inserted = g_REGISTERED_CACHES.emplace(pthid,this).second;
  49. assert(inserted == true);
  50. }
  51. inline
  52. GIDRecord *
  53. GIDCache::begin(void)
  54. {
  55. return &recs[0];
  56. }
  57. inline
  58. GIDRecord *
  59. GIDCache::end(void)
  60. {
  61. return &recs[size];
  62. }
  63. inline
  64. GIDRecord *
  65. GIDCache::allocrec(void)
  66. {
  67. if(size == MAXRECS)
  68. return &recs[RND::rand64(MAXRECS)];
  69. else
  70. return &recs[size++];
  71. }
  72. inline
  73. GIDRecord *
  74. GIDCache::lower_bound(GIDRecord *begin,
  75. GIDRecord *end,
  76. const uid_t uid)
  77. {
  78. int step;
  79. int count;
  80. GIDRecord *iter;
  81. count = std::distance(begin,end);
  82. while(count > 0)
  83. {
  84. iter = begin;
  85. step = count / 2;
  86. std::advance(iter,step);
  87. if(iter->uid < uid)
  88. {
  89. begin = ++iter;
  90. count -= step + 1;
  91. }
  92. else
  93. {
  94. count = step;
  95. }
  96. }
  97. return begin;
  98. }
  99. static
  100. int
  101. _getgrouplist(const char *user,
  102. const gid_t group,
  103. gid_t *groups,
  104. int *ngroups)
  105. {
  106. #if __APPLE__
  107. return ::getgrouplist(user,group,(int*)groups,ngroups);
  108. #else
  109. return ::getgrouplist(user,group,groups,ngroups);
  110. #endif
  111. }
  112. GIDRecord *
  113. GIDCache::cache(const uid_t uid,
  114. const gid_t gid)
  115. {
  116. int rv;
  117. char buf[4096];
  118. struct passwd pwd;
  119. struct passwd *pwdrv;
  120. GIDRecord *rec;
  121. rec = allocrec();
  122. rec->uid = uid;
  123. rv = ::getpwuid_r(uid,&pwd,buf,sizeof(buf),&pwdrv);
  124. if(pwdrv != NULL && rv == 0)
  125. {
  126. rec->size = 0;
  127. ::_getgrouplist(pwd.pw_name,gid,NULL,&rec->size);
  128. rec->size = std::min(MAXGIDS,rec->size);
  129. rv = ::_getgrouplist(pwd.pw_name,gid,rec->gids,&rec->size);
  130. if(rv == -1)
  131. {
  132. rec->gids[0] = gid;
  133. rec->size = 1;
  134. }
  135. }
  136. return rec;
  137. }
  138. static
  139. inline
  140. int
  141. setgroups(const GIDRecord *rec)
  142. {
  143. #if defined __linux__ and UGID_USE_RWLOCK == 0
  144. # if defined SYS_setgroups32
  145. return ::syscall(SYS_setgroups32,rec->size,rec->gids);
  146. # else
  147. return ::syscall(SYS_setgroups,rec->size,rec->gids);
  148. # endif
  149. #else
  150. return ::setgroups(rec->size,rec->gids);
  151. #endif
  152. }
  153. int
  154. GIDCache::initgroups(const uid_t uid,
  155. const gid_t gid)
  156. {
  157. int rv;
  158. GIDRecord *rec;
  159. if(invalidate)
  160. {
  161. size = 0;
  162. invalidate = false;
  163. }
  164. rec = lower_bound(begin(),end(),uid);
  165. if(rec == end() || rec->uid != uid)
  166. {
  167. rec = cache(uid,gid);
  168. rv = ::setgroups(rec);
  169. std::sort(begin(),end());
  170. }
  171. else
  172. {
  173. rv = ::setgroups(rec);
  174. }
  175. return rv;
  176. }
  177. void
  178. GIDCache::invalidate_all_caches()
  179. {
  180. std::lock_guard<std::mutex> guard(g_REGISTERED_CACHES_MUTEX);
  181. for(auto &p : g_REGISTERED_CACHES)
  182. p.second->invalidate = true;
  183. }