unix-caps.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. /*
  2. * libwebsockets - small server side websockets and web server implementation
  3. *
  4. * Copyright (C) 2010 - 2019 Andy Green <[email protected]>
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a copy
  7. * of this software and associated documentation files (the "Software"), to
  8. * deal in the Software without restriction, including without limitation the
  9. * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  10. * sell copies of the Software, and to permit persons to whom the Software is
  11. * furnished to do so, subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be included in
  14. * all copies or substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  19. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  21. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  22. * IN THE SOFTWARE.
  23. */
  24. #if !defined(_GNU_SOURCE)
  25. #define _GNU_SOURCE
  26. #endif
  27. #include "private-lib-core.h"
  28. #include <pwd.h>
  29. #include <grp.h>
  30. #if defined(LWS_HAVE_SYS_CAPABILITY_H) && defined(LWS_HAVE_LIBCAP)
  31. static void
  32. _lws_plat_apply_caps(int mode, const cap_value_t *cv, int count)
  33. {
  34. cap_t caps;
  35. if (!count)
  36. return;
  37. caps = cap_get_proc();
  38. cap_set_flag(caps, mode, count, cv, CAP_SET);
  39. cap_set_proc(caps);
  40. prctl(PR_SET_KEEPCAPS, 1, 0, 0, 0);
  41. cap_free(caps);
  42. }
  43. #endif
  44. int
  45. lws_plat_user_colon_group_to_ids(const char *u_colon_g, uid_t *puid, gid_t *pgid)
  46. {
  47. char *colon = strchr(u_colon_g, ':'), u[33];
  48. struct passwd *p;
  49. struct group *g;
  50. int ulen;
  51. if (!colon)
  52. return 1;
  53. ulen = lws_ptr_diff(colon, u_colon_g);
  54. if (ulen < 2 || ulen > (int)sizeof(u) - 1)
  55. return 1;
  56. memcpy(u, u_colon_g, ulen);
  57. u[ulen] = '\0';
  58. colon++;
  59. g = getgrnam(colon);
  60. if (!g) {
  61. lwsl_err("%s: unknown group '%s'\n", __func__, colon);
  62. return 1;
  63. }
  64. *pgid = g->gr_gid;
  65. p = getpwnam(u);
  66. if (!p) {
  67. lwsl_err("%s: unknown group '%s'\n", __func__, u);
  68. return 1;
  69. }
  70. *puid = p->pw_uid;
  71. return 0;
  72. }
  73. int
  74. lws_plat_drop_app_privileges(struct lws_context *context, int actually_drop)
  75. {
  76. struct passwd *p;
  77. struct group *g;
  78. /* if he gave us the groupname, align gid to match it */
  79. if (context->groupname) {
  80. g = getgrnam(context->groupname);
  81. if (g) {
  82. lwsl_info("%s: group %s -> gid %u\n", __func__,
  83. context->groupname, g->gr_gid);
  84. context->gid = g->gr_gid;
  85. } else {
  86. lwsl_err("%s: unknown groupname '%s'\n", __func__,
  87. context->groupname);
  88. return 1;
  89. }
  90. }
  91. /* if he gave us the username, align uid to match it */
  92. if (context->username) {
  93. p = getpwnam(context->username);
  94. if (p) {
  95. context->uid = p->pw_uid;
  96. lwsl_info("%s: username %s -> uid %u\n", __func__,
  97. context->username, (unsigned int)p->pw_uid);
  98. } else {
  99. lwsl_err("%s: unknown username %s\n", __func__,
  100. context->username);
  101. return 1;
  102. }
  103. }
  104. if (!actually_drop)
  105. return 0;
  106. /* if he gave us the gid or we have it from the groupname, set it */
  107. if (context->gid && context->gid != -1) {
  108. g = getgrgid(context->gid);
  109. if (!g) {
  110. lwsl_err("%s: cannot find name for gid %d\n",
  111. __func__, context->gid);
  112. return 1;
  113. }
  114. if (setgid(context->gid)) {
  115. lwsl_err("%s: setgid: %s failed\n", __func__,
  116. strerror(LWS_ERRNO));
  117. return 1;
  118. }
  119. lwsl_notice("%s: effective group '%s'\n", __func__,
  120. g->gr_name);
  121. } else
  122. lwsl_info("%s: not changing group\n", __func__);
  123. /* if he gave us the uid or we have it from the username, set it */
  124. if (context->uid && context->uid != -1) {
  125. p = getpwuid(context->uid);
  126. if (!p) {
  127. lwsl_err("%s: getpwuid: unable to find uid %d\n",
  128. __func__, context->uid);
  129. return 1;
  130. }
  131. #if defined(LWS_HAVE_SYS_CAPABILITY_H) && defined(LWS_HAVE_LIBCAP)
  132. _lws_plat_apply_caps(CAP_PERMITTED, context->caps,
  133. context->count_caps);
  134. #endif
  135. if (initgroups(p->pw_name, context->gid))
  136. return 1;
  137. if (setuid(context->uid)) {
  138. lwsl_err("%s: setuid: %s failed\n", __func__,
  139. strerror(LWS_ERRNO));
  140. return 1;
  141. } else
  142. lwsl_notice("%s: effective user '%s'\n",
  143. __func__, p->pw_name);
  144. #if defined(LWS_HAVE_SYS_CAPABILITY_H) && defined(LWS_HAVE_LIBCAP)
  145. _lws_plat_apply_caps(CAP_EFFECTIVE, context->caps,
  146. context->count_caps);
  147. if (context->count_caps) {
  148. int n;
  149. for (n = 0; n < context->count_caps; n++)
  150. lwsl_notice(" RETAINING CAP %d\n",
  151. (int)context->caps[n]);
  152. }
  153. #endif
  154. } else
  155. lwsl_info("%s: not changing user\n", __func__);
  156. return 0;
  157. }