Security_admin.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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 <https://www.gnu.org/licenses/>.
  14. */
  15. #include "admin/Admin.h"
  16. #include "benc/String.h"
  17. #include "benc/Dict.h"
  18. #include "exception/Er.h"
  19. #include "util/log/Log.h"
  20. #include "util/Security.h"
  21. #include "util/Security_admin.h"
  22. struct Context
  23. {
  24. struct Log* logger;
  25. struct Admin* admin;
  26. struct Security* sec;
  27. Identity
  28. };
  29. static void sendError(const char* errorMessage, String* txid, struct Admin* admin)
  30. {
  31. Dict error = Dict_CONST(String_CONST("error"),
  32. String_OBJ(String_CONST((char*)errorMessage)), NULL);
  33. Admin_sendMessage(&error, txid, admin);
  34. }
  35. static void setUser(Dict* args, void* vctx, String* txid, struct Allocator* requestAlloc)
  36. {
  37. struct Context* const ctx = Identity_check((struct Context*) vctx);
  38. int64_t* user = Dict_getIntC(args, "uid");
  39. int64_t* group = Dict_getIntC(args, "gid");
  40. int gid = group ? (int)*group : 0;
  41. int64_t* keepNetAdmin = Dict_getIntC(args, "keepNetAdmin");
  42. struct Er_Ret* er = NULL;
  43. Er_check(&er, Security_setUser(*user, gid, *keepNetAdmin, ctx->logger, requestAlloc));
  44. if (er) {
  45. sendError(er->message, txid, ctx->admin);
  46. return;
  47. }
  48. sendError("none", txid, ctx->admin);
  49. }
  50. static void checkPermissions(Dict* args, void* vctx, String* txid, struct Allocator* requestAlloc)
  51. {
  52. struct Context* const ctx = Identity_check((struct Context*) vctx);
  53. struct Er_Ret* er = NULL;
  54. struct Security_Permissions* sp = Er_check(&er, Security_checkPermissions(requestAlloc));
  55. if (er) {
  56. sendError(er->message, txid, ctx->admin);
  57. return;
  58. }
  59. Dict* out = Dict_new(requestAlloc);
  60. Dict_putIntC(out, "noOpenFiles", sp->noOpenFiles, requestAlloc);
  61. Dict_putIntC(out, "seccompExists", sp->seccompExists, requestAlloc);
  62. Dict_putIntC(out, "seccompEnforcing", sp->seccompEnforcing, requestAlloc);
  63. Dict_putIntC(out, "userId", sp->uid, requestAlloc);
  64. Dict_putStringCC(out, "error", "none", requestAlloc);
  65. Admin_sendMessage(out, txid, ctx->admin);
  66. }
  67. #define NOARG_CALL(vctx, txid, func, requestAlloc) \
  68. do { \
  69. struct Context* const ctx = Identity_check((struct Context*) vctx); \
  70. struct Er_Ret* er = NULL; \
  71. Er_check(&er, func(requestAlloc)); \
  72. if (er) { \
  73. sendError(er->message, txid, ctx->admin); \
  74. return; \
  75. } \
  76. sendError("none", txid, ctx->admin); \
  77. } while (0)
  78. // CHECKFILES_IGNORE expecting { bracket
  79. static void nofiles(Dict* args, void* vctx, String* txid, struct Allocator* requestAlloc)
  80. {
  81. NOARG_CALL(vctx, txid, Security_nofiles, requestAlloc);
  82. }
  83. static void noforks(Dict* args, void* vctx, String* txid, struct Allocator* requestAlloc)
  84. {
  85. NOARG_CALL(vctx, txid, Security_noforks, requestAlloc);
  86. }
  87. static void chroot(Dict* args, void* vctx, String* txid, struct Allocator* requestAlloc)
  88. {
  89. struct Context* const ctx = Identity_check((struct Context*) vctx);
  90. struct Er_Ret* er = NULL;
  91. String* root = Dict_getStringC(args, "root");
  92. Er_check(&er, Security_chroot(root->bytes, requestAlloc));
  93. if (er) {
  94. sendError(er->message, txid, ctx->admin);
  95. return;
  96. }
  97. sendError("none", txid, ctx->admin);
  98. }
  99. static void seccomp(Dict* args, void* vctx, String* txid, struct Allocator* requestAlloc)
  100. {
  101. struct Context* const ctx = Identity_check((struct Context*) vctx);
  102. struct Er_Ret* er = NULL;
  103. struct Security_Permissions* sp = Er_check(&er, Security_checkPermissions(requestAlloc));
  104. if (er) {
  105. sendError(er->message, txid, ctx->admin);
  106. return;
  107. }
  108. if (sp->seccompEnforcing) {
  109. sendError("seccomp is already enabled", txid, ctx->admin);
  110. return;
  111. }
  112. Er_check(&er, Security_seccomp(requestAlloc, ctx->logger));
  113. if (er) {
  114. sendError(er->message, txid, ctx->admin);
  115. return;
  116. }
  117. sendError("none", txid, ctx->admin);
  118. }
  119. static void setupComplete(Dict* args, void* vctx, String* txid, struct Allocator* requestAlloc)
  120. {
  121. struct Context* const ctx = Identity_check((struct Context*) vctx);
  122. Security_setupComplete(ctx->sec);
  123. sendError("none", txid, ctx->admin);
  124. }
  125. static void getUser(Dict* args, void* vctx, String* txid, struct Allocator* requestAlloc)
  126. {
  127. struct Context* const ctx = Identity_check((struct Context*) vctx);
  128. String* user = Dict_getStringC(args, "user");
  129. Dict* ret = Security_getUser((user) ? user->bytes : NULL, requestAlloc);
  130. Admin_sendMessage(ret, txid, ctx->admin);
  131. }
  132. void Security_admin_register(struct Allocator* alloc,
  133. struct Log* logger,
  134. struct Security* sec,
  135. struct Admin* admin)
  136. {
  137. struct Context* ctx = Allocator_clone(alloc, (&(struct Context) {
  138. .logger = logger,
  139. .admin = admin
  140. }));
  141. Identity_set(ctx);
  142. ctx->sec = sec;
  143. Admin_registerFunction("Security_nofiles", nofiles, ctx, true, NULL, admin);
  144. Admin_registerFunction("Security_noforks", noforks, ctx, true, NULL, admin);
  145. Admin_registerFunction("Security_chroot", chroot, ctx, true, ((struct Admin_FunctionArg[]) {
  146. { .name = "root", .required = 1, .type = "String" }
  147. }), admin);
  148. Admin_registerFunction("Security_setUser", setUser, ctx, true, ((struct Admin_FunctionArg[]) {
  149. { .name = "uid", .required = 1, .type = "Int" },
  150. { .name = "gid", .required = 0, .type = "Int" },
  151. { .name = "keepNetAdmin", .required = 1, .type = "Int" },
  152. }), admin);
  153. Admin_registerFunction("Security_getUser", getUser, ctx, true, ((struct Admin_FunctionArg[]) {
  154. { .name = "user", .required = 0, .type = "String" }
  155. }), admin);
  156. Admin_registerFunction("Security_seccomp", seccomp, ctx, true, NULL, admin);
  157. Admin_registerFunction("Security_setupComplete", setupComplete, ctx, true, NULL, admin);
  158. Admin_registerFunction("Security_checkPermissions", checkPermissions, ctx, true, NULL, admin);
  159. }