bsd-setres_id.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /*
  2. * Copyright (c) 2012 Darren Tucker (dtucker at zip com au).
  3. *
  4. * Permission to use, copy, modify, and distribute this software for any
  5. * purpose with or without fee is hereby granted, provided that the above
  6. * copyright notice and this permission notice appear in all copies.
  7. *
  8. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  9. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  10. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  11. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  12. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  13. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  14. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  15. */
  16. #include "includes.h"
  17. #include <sys/types.h>
  18. #include <stdarg.h>
  19. #include <unistd.h>
  20. #include <string.h>
  21. #include "log.h"
  22. #if !defined(HAVE_SETRESGID) || defined(BROKEN_SETRESGID)
  23. int
  24. setresgid(gid_t rgid, gid_t egid, gid_t sgid)
  25. {
  26. int ret = 0, saved_errno;
  27. if (rgid != sgid) {
  28. errno = ENOSYS;
  29. return -1;
  30. }
  31. #if defined(HAVE_SETREGID) && !defined(BROKEN_SETREGID)
  32. if (setregid(rgid, egid) < 0) {
  33. saved_errno = errno;
  34. error("setregid %lu: %.100s", (u_long)rgid, strerror(errno));
  35. errno = saved_errno;
  36. ret = -1;
  37. }
  38. #else
  39. if (setegid(egid) < 0) {
  40. saved_errno = errno;
  41. error("setegid %lu: %.100s", (u_long)egid, strerror(errno));
  42. errno = saved_errno;
  43. ret = -1;
  44. }
  45. if (setgid(rgid) < 0) {
  46. saved_errno = errno;
  47. error("setgid %lu: %.100s", (u_long)rgid, strerror(errno));
  48. errno = saved_errno;
  49. ret = -1;
  50. }
  51. #endif
  52. return ret;
  53. }
  54. #endif
  55. #if !defined(HAVE_SETRESUID) || defined(BROKEN_SETRESUID)
  56. int
  57. setresuid(uid_t ruid, uid_t euid, uid_t suid)
  58. {
  59. int ret = 0, saved_errno;
  60. if (ruid != suid) {
  61. errno = ENOSYS;
  62. return -1;
  63. }
  64. #if defined(HAVE_SETREUID) && !defined(BROKEN_SETREUID)
  65. if (setreuid(ruid, euid) < 0) {
  66. saved_errno = errno;
  67. error("setreuid %lu: %.100s", (u_long)ruid, strerror(errno));
  68. errno = saved_errno;
  69. ret = -1;
  70. }
  71. #else
  72. # ifndef SETEUID_BREAKS_SETUID
  73. if (seteuid(euid) < 0) {
  74. saved_errno = errno;
  75. error("seteuid %lu: %.100s", (u_long)euid, strerror(errno));
  76. errno = saved_errno;
  77. ret = -1;
  78. }
  79. # endif
  80. if (setuid(ruid) < 0) {
  81. saved_errno = errno;
  82. error("setuid %lu: %.100s", (u_long)ruid, strerror(errno));
  83. errno = saved_errno;
  84. ret = -1;
  85. }
  86. #endif
  87. return ret;
  88. }
  89. #endif