1
0

AuthorizedPasswords.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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/AuthorizedPasswords.h"
  16. #include "benc/Int.h"
  17. #include "benc/List.h"
  18. #include "benc/String.h"
  19. #include "util/AddrTools.h"
  20. struct Context
  21. {
  22. struct Admin* admin;
  23. struct CryptoAuth* ca;
  24. struct Allocator* allocator;
  25. Identity
  26. };
  27. static void sendResponse(String* msg, struct Admin* admin, String* txid, struct Allocator* alloc)
  28. {
  29. Dict* output = Dict_new(alloc);
  30. Dict_putStringC(output, "error", msg, alloc);
  31. Admin_sendMessage(output, txid, admin);
  32. }
  33. static void add(Dict* args, void* vcontext, String* txid, struct Allocator* alloc)
  34. {
  35. struct Context* context = Identity_check((struct Context*) vcontext);
  36. String* passwd = Dict_getStringC(args, "password");
  37. String* user = Dict_getStringC(args, "user");
  38. String* ipv6 = Dict_getStringC(args, "ipv6");
  39. uint8_t ipv6Bytes[16];
  40. uint8_t* ipv6Arg;
  41. if (!ipv6) {
  42. ipv6Arg = NULL;
  43. } else if (AddrTools_parseIp(ipv6Bytes, ipv6->bytes)) {
  44. sendResponse(String_CONST("Invalid IPv6 Address"), context->admin, txid, alloc);
  45. return;
  46. } else {
  47. ipv6Arg = ipv6Bytes;
  48. }
  49. int32_t ret = CryptoAuth_addUser_ipv6(passwd, user, ipv6Arg, context->ca);
  50. switch (ret) {
  51. case 0:
  52. sendResponse(String_CONST("none"), context->admin, txid, alloc);
  53. break;
  54. case CryptoAuth_addUser_DUPLICATE:
  55. sendResponse(String_CONST("Password already added."), context->admin, txid, alloc);
  56. break;
  57. default:
  58. sendResponse(String_CONST("Unknown error."), context->admin, txid, alloc);
  59. }
  60. }
  61. static void remove(Dict* args, void* vcontext, String* txid, struct Allocator* requestAlloc)
  62. {
  63. struct Context* context = Identity_check((struct Context*) vcontext);
  64. String* user = Dict_getStringC(args, "user");
  65. int32_t ret = CryptoAuth_removeUsers(context->ca, user);
  66. if (ret) {
  67. sendResponse(String_CONST("none"), context->admin, txid, requestAlloc);
  68. } else {
  69. sendResponse(String_CONST("Unknown error."), context->admin, txid, requestAlloc);
  70. }
  71. }
  72. static void list(Dict* args, void* vcontext, String* txid, struct Allocator* requestAlloc)
  73. {
  74. struct Context* context = Identity_check((struct Context*) vcontext);
  75. struct Allocator* child = Allocator_child(context->allocator);
  76. List* users = CryptoAuth_getUsers(context->ca, child);
  77. uint32_t count = List_size(users);
  78. Dict response = Dict_CONST(
  79. String_CONST("total"), Int_OBJ(count), Dict_CONST(
  80. String_CONST("users"), List_OBJ(users), NULL
  81. ));
  82. Admin_sendMessage(&response, txid, context->admin);
  83. Allocator_free(child);
  84. }
  85. void AuthorizedPasswords_init(struct Admin* admin,
  86. struct CryptoAuth* ca,
  87. struct Allocator* allocator)
  88. {
  89. struct Context* context = Allocator_malloc(allocator, sizeof(struct Context));
  90. context->admin = admin;
  91. context->allocator = allocator;
  92. context->ca = ca;
  93. Identity_set(context);
  94. Admin_registerFunction("AuthorizedPasswords_add", add, context, true,
  95. ((struct Admin_FunctionArg[]){
  96. { .name = "password", .required = 1, .type = "String" },
  97. { .name = "ipv6", .required = 0, .type = "String" },
  98. { .name = "user", .required = 0, .type = "String" }
  99. }), admin);
  100. Admin_registerFunction("AuthorizedPasswords_remove", remove, context, true,
  101. ((struct Admin_FunctionArg[]){
  102. { .name = "user", .required = 1, .type = "String" }
  103. }), admin);
  104. Admin_registerFunction("AuthorizedPasswords_list", list, context, true, NULL, admin);
  105. }