Security_admin.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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. };
  27. static void sendError(char* errorMessage, String* txid, struct Admin* admin)
  28. {
  29. Dict error = Dict_CONST(String_CONST("error"), String_OBJ(String_CONST(errorMessage)), NULL);
  30. Admin_sendMessage(&error, txid, admin);
  31. }
  32. static void setUser(Dict* args, void* vcontext, String* txid, struct Allocator* requestAlloc)
  33. {
  34. struct Context* const ctx = (struct Context*) vcontext;
  35. struct Jmp jmp;
  36. Jmp_try(jmp) {
  37. String* user = Dict_getString(args, String_CONST("user"));
  38. Security_setUser(user->bytes, ctx->logger, &jmp.handler);
  39. } Jmp_catch {
  40. sendError(jmp.message, txid, ctx->admin);
  41. return;
  42. }
  43. sendError("none", txid, ctx->admin);
  44. }
  45. static void dropPermissions(Dict* args, void* vctx, String* txid, struct Allocator* requestAlloc)
  46. {
  47. struct Context* const ctx = (struct Context*) vctx;
  48. struct Jmp jmp;
  49. Jmp_try(jmp) {
  50. Security_dropPermissions(requestAlloc, ctx->logger, &jmp.handler);
  51. } Jmp_catch {
  52. sendError(jmp.message, txid, ctx->admin);
  53. return;
  54. }
  55. sendError("none", txid, ctx->admin);
  56. }
  57. static void checkPermissionsB(struct Except* eh,
  58. String* txid,
  59. struct Admin* admin,
  60. struct Allocator* requestAlloc)
  61. {
  62. struct Security_Permissions* sp = Security_checkPermissions(requestAlloc, eh);
  63. Dict* out = Dict_new(requestAlloc);
  64. Dict_putInt(out, String_CONST("noOpenFiles"), sp->noOpenFiles, requestAlloc);
  65. Dict_putInt(out, String_CONST("seccompExists"), sp->seccompExists, requestAlloc);
  66. Dict_putInt(out, String_CONST("seccompEnforcing"), sp->seccompEnforcing, requestAlloc);
  67. Dict_putInt(out, String_CONST("memoryLimitBytes"), sp->memoryLimitBytes, requestAlloc);
  68. Dict_putString(out, String_CONST("error"), String_CONST("none"), requestAlloc);
  69. Admin_sendMessage(out, txid, admin);
  70. }
  71. static void checkPermissions(Dict* args, void* vctx, String* txid, struct Allocator* requestAlloc)
  72. {
  73. struct Context* const ctx = (struct Context*) vctx;
  74. struct Jmp jmp;
  75. Jmp_try(jmp) {
  76. checkPermissionsB(&jmp.handler, txid, ctx->admin, requestAlloc);
  77. } Jmp_catch {
  78. sendError(jmp.message, txid, ctx->admin);
  79. return;
  80. }
  81. }
  82. void Security_admin_register(struct Allocator* alloc, struct Log* logger, struct Admin* admin)
  83. {
  84. struct Context* ctx = Allocator_clone(alloc, (&(struct Context) {
  85. .logger = logger,
  86. .admin = admin
  87. }));
  88. struct Admin_FunctionArg setUserArgs[] = {
  89. { .name = "user", .required = 1, .type = "String" }
  90. };
  91. Admin_registerFunction("Security_setUser", setUser, ctx, true, setUserArgs, admin);
  92. Admin_registerFunction("Security_dropPermissions", dropPermissions, ctx, true, NULL, admin);
  93. Admin_registerFunction("Security_checkPermissions", checkPermissions, ctx, true, NULL, admin);
  94. }