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.

175 lines
3.5 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 <grp.h>
  15. #include <pwd.h>
  16. #include <stdlib.h>
  17. #include <sys/types.h>
  18. #include <unistd.h>
  19. #if defined __linux__ and UGID_USE_RWLOCK == 0
  20. # include <sys/syscall.h>
  21. #elif __APPLE__
  22. # include <sys/param.h>
  23. #endif
  24. #include <cstdlib>
  25. #include <algorithm>
  26. #include "gidcache.hpp"
  27. inline
  28. bool
  29. gid_t_rec::operator<(const struct gid_t_rec &b) const
  30. {
  31. return uid < b.uid;
  32. }
  33. inline
  34. gid_t_rec *
  35. gid_t_cache::begin(void)
  36. {
  37. return recs;
  38. }
  39. inline
  40. gid_t_rec *
  41. gid_t_cache::end(void)
  42. {
  43. return recs + size;
  44. }
  45. inline
  46. gid_t_rec *
  47. gid_t_cache::allocrec(void)
  48. {
  49. if(size == MAXRECS)
  50. return &recs[rand() % MAXRECS];
  51. else
  52. return &recs[size++];
  53. }
  54. inline
  55. gid_t_rec *
  56. gid_t_cache::lower_bound(gid_t_rec *begin,
  57. gid_t_rec *end,
  58. const uid_t uid)
  59. {
  60. int step;
  61. int count;
  62. gid_t_rec *iter;
  63. count = std::distance(begin,end);
  64. while(count > 0)
  65. {
  66. iter = begin;
  67. step = count / 2;
  68. std::advance(iter,step);
  69. if(iter->uid < uid)
  70. {
  71. begin = ++iter;
  72. count -= step + 1;
  73. }
  74. else
  75. {
  76. count = step;
  77. }
  78. }
  79. return begin;
  80. }
  81. static
  82. int
  83. _getgrouplist(const char *user,
  84. const gid_t group,
  85. gid_t *groups,
  86. int *ngroups)
  87. {
  88. #if __APPLE__
  89. return ::getgrouplist(user,group,(int*)groups,ngroups);
  90. #else
  91. return ::getgrouplist(user,group,groups,ngroups);
  92. #endif
  93. }
  94. gid_t_rec *
  95. gid_t_cache::cache(const uid_t uid,
  96. const gid_t gid)
  97. {
  98. int rv;
  99. char buf[4096];
  100. struct passwd pwd;
  101. struct passwd *pwdrv;
  102. gid_t_rec *rec;
  103. rec = allocrec();
  104. rec->uid = uid;
  105. rv = ::getpwuid_r(uid,&pwd,buf,sizeof(buf),&pwdrv);
  106. if(pwdrv != NULL && rv == 0)
  107. {
  108. rec->size = 0;
  109. ::_getgrouplist(pwd.pw_name,gid,NULL,&rec->size);
  110. rec->size = std::min(MAXGIDS,rec->size);
  111. rv = ::_getgrouplist(pwd.pw_name,gid,rec->gids,&rec->size);
  112. if(rv == -1)
  113. {
  114. rec->gids[0] = gid;
  115. rec->size = 1;
  116. }
  117. }
  118. return rec;
  119. }
  120. static
  121. inline
  122. int
  123. setgroups(const gid_t_rec *rec)
  124. {
  125. #if defined __linux__ and UGID_USE_RWLOCK == 0
  126. # if defined SYS_setgroups32
  127. return ::syscall(SYS_setgroups32,rec->size,rec->gids);
  128. # else
  129. return ::syscall(SYS_setgroups,rec->size,rec->gids);
  130. # endif
  131. #else
  132. return ::setgroups(rec->size,rec->gids);
  133. #endif
  134. }
  135. int
  136. gid_t_cache::initgroups(const uid_t uid,
  137. const gid_t gid)
  138. {
  139. int rv;
  140. gid_t_rec *rec;
  141. rec = lower_bound(begin(),end(),uid);
  142. if(rec == end() || rec->uid != uid)
  143. {
  144. rec = cache(uid,gid);
  145. rv = ::setgroups(rec);
  146. std::sort(begin(),end());
  147. }
  148. else
  149. {
  150. rv = ::setgroups(rec);
  151. }
  152. return rv;
  153. }