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.

117 lines
2.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. #pragma once
  15. #include <sys/stat.h>
  16. #include <sys/syscall.h>
  17. #include <sys/types.h>
  18. #include <unistd.h>
  19. #include <grp.h>
  20. #include <pwd.h>
  21. #include <map>
  22. #include <vector>
  23. #if defined SYS_setreuid32
  24. #define SETREUID(R,E) (::syscall(SYS_setreuid32,(R),(E)))
  25. #else
  26. #define SETREUID(R,E) (::syscall(SYS_setreuid,(R),(E)))
  27. #endif
  28. #if defined SYS_setregid32
  29. #define SETREGID(R,E) (::syscall(SYS_setregid32,(R),(E)))
  30. #else
  31. #define SETREGID(R,E) (::syscall(SYS_setregid,(R),(E)))
  32. #endif
  33. #if defined SYS_geteuid32
  34. #define GETEUID() (::syscall(SYS_geteuid32))
  35. #else
  36. #define GETEUID() (::syscall(SYS_geteuid))
  37. #endif
  38. #if defined SYS_getegid32
  39. #define GETEGID() (::syscall(SYS_getegid32))
  40. #else
  41. #define GETEGID() (::syscall(SYS_getegid))
  42. #endif
  43. namespace ugid
  44. {
  45. extern thread_local uid_t currentuid;
  46. extern thread_local gid_t currentgid;
  47. extern thread_local bool initialized;
  48. struct Set
  49. {
  50. Set(const uid_t newuid_,
  51. const gid_t newgid_)
  52. {
  53. if(!initialized)
  54. {
  55. currentuid = GETEUID();
  56. currentgid = GETEGID();
  57. initialized = true;
  58. }
  59. if((newuid_ == currentuid) && (newgid_ == currentgid))
  60. return;
  61. if(currentuid != 0)
  62. {
  63. SETREUID(-1,0);
  64. SETREGID(-1,0);
  65. }
  66. if(newgid_)
  67. {
  68. SETREGID(-1,newgid_);
  69. initgroups(newuid_,newgid_);
  70. }
  71. if(newuid_)
  72. SETREUID(-1,newuid_);
  73. currentuid = newuid_;
  74. currentgid = newgid_;
  75. }
  76. };
  77. struct SetRootGuard
  78. {
  79. SetRootGuard() :
  80. prevuid(currentuid),
  81. prevgid(currentgid)
  82. {
  83. Set(0,0);
  84. }
  85. ~SetRootGuard()
  86. {
  87. Set(prevuid,prevgid);
  88. }
  89. const uid_t prevuid;
  90. const gid_t prevgid;
  91. };
  92. }
  93. #undef SETREUID
  94. #undef SETREGID
  95. #undef GETEUID
  96. #undef GETEGID