1
0

Security_admin.c 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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. #define string_strlen
  23. #include "util/platform/libc/string.h"
  24. struct Context
  25. {
  26. struct Log* logger;
  27. struct Admin* admin;
  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* vcontext, String* txid, struct Allocator* requestAlloc)
  35. {
  36. struct Context* const ctx = (struct Context*) vcontext;
  37. struct Jmp jmp;
  38. Jmp_try(jmp) {
  39. String* user = Dict_getString(args, String_CONST("user"));
  40. Security_setUser(user->bytes, ctx->logger, &jmp.handler);
  41. } Jmp_catch {
  42. sendError(jmp.message, txid, ctx->admin);
  43. return;
  44. }
  45. sendError("none", txid, ctx->admin);
  46. }
  47. static void dropPermissions(Dict* args, void* vctx, String* txid, struct Allocator* requestAlloc)
  48. {
  49. struct Context* const ctx = (struct Context*) vctx;
  50. struct Jmp jmp;
  51. Jmp_try(jmp) {
  52. Security_dropPermissions(&jmp.handler);
  53. } Jmp_catch {
  54. sendError(jmp.message, txid, ctx->admin);
  55. return;
  56. }
  57. sendError("none", txid, ctx->admin);
  58. }
  59. void Security_admin_register(struct Allocator* alloc, struct Log* logger, struct Admin* admin)
  60. {
  61. struct Context* ctx = Allocator_clone(alloc, (&(struct Context) {
  62. .logger = logger,
  63. .admin = admin
  64. }));
  65. struct Admin_FunctionArg setUserArgs[] = {
  66. { .name = "user", .required = 1, .type = "String" }
  67. };
  68. Admin_registerFunction("Security_setUser", setUser, ctx, true, setUserArgs, admin);
  69. Admin_registerFunction("Security_dropPermissions", dropPermissions, ctx, true, NULL, admin);
  70. }