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.

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