Security_admin.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. /* vim: set expandtab ts=4 sw=4: */
  2. /*
  3. * You may redistribute this program and/or modify it under the terms of
  4. * the GNU General Public License as published by the Free Software Foundation,
  5. * either version 3 of the License, or (at your option) any later version.
  6. *
  7. * This program is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU General Public License for more details.
  11. *
  12. * You should have received a copy of the GNU General Public License
  13. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. */
  15. #include "admin/Admin.h"
  16. #include "benc/String.h"
  17. #include "benc/Dict.h"
  18. #include "exception/Except.h"
  19. #include "exception/Jmp.h"
  20. #include "util/log/Log.h"
  21. #include "util/Security.h"
  22. struct Context
  23. {
  24. struct Log* logger;
  25. struct Admin* admin;
  26. struct Security* sec;
  27. Identity
  28. };
  29. static void sendError(char* errorMessage, String* txid, struct Admin* admin)
  30. {
  31. Dict error = Dict_CONST(String_CONST("error"), String_OBJ(String_CONST(errorMessage)), NULL);
  32. Admin_sendMessage(&error, txid, admin);
  33. }
  34. static void setUser(Dict* args, void* vctx, String* txid, struct Allocator* requestAlloc)
  35. {
  36. struct Context* const ctx = Identity_check((struct Context*) vctx);
  37. struct Jmp jmp;
  38. Jmp_try(jmp) {
  39. int64_t* user = Dict_getInt(args, String_CONST("uid"));
  40. int64_t* group = Dict_getInt(args, String_CONST("gid"));
  41. int gid = group ? (int)*group : 0;
  42. int64_t* keepNetAdmin = Dict_getInt(args, String_CONST("keepNetAdmin"));
  43. Security_setUser(*user, gid, *keepNetAdmin, ctx->logger, &jmp.handler, requestAlloc);
  44. } Jmp_catch {
  45. sendError(jmp.message, txid, ctx->admin);
  46. return;
  47. }
  48. sendError("none", txid, ctx->admin);
  49. }
  50. static void checkPermissionsB(struct Except* eh,
  51. String* txid,
  52. struct Admin* admin,
  53. struct Allocator* requestAlloc)
  54. {
  55. struct Security_Permissions* sp = Security_checkPermissions(requestAlloc, eh);
  56. Dict* out = Dict_new(requestAlloc);
  57. Dict_putInt(out, String_CONST("noOpenFiles"), sp->noOpenFiles, requestAlloc);
  58. Dict_putInt(out, String_CONST("seccompExists"), sp->seccompExists, requestAlloc);
  59. Dict_putInt(out, String_CONST("seccompEnforcing"), sp->seccompEnforcing, requestAlloc);
  60. Dict_putInt(out, String_CONST("userId"), sp->uid, requestAlloc);
  61. Dict_putString(out, String_CONST("error"), String_CONST("none"), requestAlloc);
  62. Admin_sendMessage(out, txid, admin);
  63. }
  64. static void checkPermissions(Dict* args, void* vctx, String* txid, struct Allocator* requestAlloc)
  65. {
  66. struct Context* const ctx = Identity_check((struct Context*) vctx);
  67. struct Jmp jmp;
  68. Jmp_try(jmp) {
  69. checkPermissionsB(&jmp.handler, txid, ctx->admin, requestAlloc);
  70. } Jmp_catch {
  71. sendError(jmp.message, txid, ctx->admin);
  72. return;
  73. }
  74. }
  75. #define NOARG_CALL(vctx, txid, func) \
  76. do { \
  77. struct Context* const ctx = Identity_check((struct Context*) vctx); \
  78. struct Jmp jmp; \
  79. Jmp_try(jmp) { \
  80. func(&jmp.handler); \
  81. } Jmp_catch { \
  82. sendError(jmp.message, txid, ctx->admin); \
  83. return; \
  84. } \
  85. sendError("none", txid, ctx->admin); \
  86. } while (0)
  87. // CHECKFILES_IGNORE expecting { bracket
  88. static void nofiles(Dict* args, void* vctx, String* txid, struct Allocator* requestAlloc)
  89. {
  90. NOARG_CALL(vctx, txid, Security_nofiles);
  91. }
  92. static void noforks(Dict* args, void* vctx, String* txid, struct Allocator* requestAlloc)
  93. {
  94. NOARG_CALL(vctx, txid, Security_noforks);
  95. }
  96. static void chroot(Dict* args, void* vctx, String* txid, struct Allocator* requestAlloc)
  97. {
  98. struct Context* const ctx = Identity_check((struct Context*) vctx);
  99. struct Jmp jmp;
  100. Jmp_try(jmp) {
  101. String* root = Dict_getString(args, String_CONST("root"));
  102. Security_chroot(root->bytes, &jmp.handler);
  103. } Jmp_catch {
  104. sendError(jmp.message, txid, ctx->admin);
  105. return;
  106. }
  107. sendError("none", txid, ctx->admin);
  108. }
  109. static void seccomp(Dict* args, void* vctx, String* txid, struct Allocator* requestAlloc)
  110. {
  111. struct Context* const ctx = Identity_check((struct Context*) vctx);
  112. struct Jmp jmp;
  113. struct Except* eh = &jmp.handler;
  114. Jmp_try(jmp) {
  115. struct Security_Permissions* sp = Security_checkPermissions(requestAlloc, eh);
  116. if (!sp->seccompEnforcing) {
  117. Security_seccomp(requestAlloc, ctx->logger, eh);
  118. } else {
  119. sendError("seccomp is already enabled", txid, ctx->admin);
  120. }
  121. } Jmp_catch {
  122. sendError(jmp.message, txid, ctx->admin);
  123. return;
  124. }
  125. sendError("none", txid, ctx->admin);
  126. }
  127. static void setupComplete(Dict* args, void* vctx, String* txid, struct Allocator* requestAlloc)
  128. {
  129. struct Context* const ctx = Identity_check((struct Context*) vctx);
  130. Security_setupComplete(ctx->sec);
  131. sendError("none", txid, ctx->admin);
  132. }
  133. static void getUser(Dict* args, void* vctx, String* txid, struct Allocator* requestAlloc)
  134. {
  135. struct Context* const ctx = Identity_check((struct Context*) vctx);
  136. String* user = Dict_getString(args, String_CONST("user"));
  137. Dict* ret = Security_getUser((user) ? user->bytes : NULL, requestAlloc);
  138. Admin_sendMessage(ret, txid, ctx->admin);
  139. }
  140. void Security_admin_register(struct Allocator* alloc,
  141. struct Log* logger,
  142. struct Security* sec,
  143. struct Admin* admin)
  144. {
  145. struct Context* ctx = Allocator_clone(alloc, (&(struct Context) {
  146. .logger = logger,
  147. .admin = admin
  148. }));
  149. Identity_set(ctx);
  150. ctx->sec = sec;
  151. Admin_registerFunction("Security_nofiles", nofiles, ctx, true, NULL, admin);
  152. Admin_registerFunction("Security_noforks", noforks, ctx, true, NULL, admin);
  153. Admin_registerFunction("Security_chroot", chroot, ctx, true, ((struct Admin_FunctionArg[]) {
  154. { .name = "root", .required = 1, .type = "String" }
  155. }), admin);
  156. Admin_registerFunction("Security_setUser", setUser, ctx, true, ((struct Admin_FunctionArg[]) {
  157. { .name = "uid", .required = 1, .type = "Int" },
  158. { .name = "gid", .required = 0, .type = "Int" },
  159. { .name = "keepNetAdmin", .required = 1, .type = "Int" },
  160. }), admin);
  161. Admin_registerFunction("Security_getUser", getUser, ctx, true, ((struct Admin_FunctionArg[]) {
  162. { .name = "user", .required = 0, .type = "String" }
  163. }), admin);
  164. Admin_registerFunction("Security_seccomp", seccomp, ctx, true, NULL, admin);
  165. Admin_registerFunction("Security_setupComplete", setupComplete, ctx, true, NULL, admin);
  166. Admin_registerFunction("Security_checkPermissions", checkPermissions, ctx, true, NULL, admin);
  167. }