AuthorizedPasswords.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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. int64_t* page_p = Dict_getIntC(args, "page");
  76. int page = (page_p) ? *page_p : 0;
  77. struct StringList* users = CryptoAuth_getUsers(context->ca, requestAlloc);
  78. List* out = List_new(requestAlloc);
  79. for (int i = page * 16; i < users->length && i < (page + 1) * 16; i++) {
  80. List_addString(out, StringList_get(users, i), requestAlloc);
  81. }
  82. Dict* response = Dict_new(requestAlloc);
  83. Dict_putIntC(response, "total", users->length, requestAlloc);
  84. Dict_putListC(response, "users", out, requestAlloc);
  85. Admin_sendMessage(response, txid, context->admin);
  86. }
  87. void AuthorizedPasswords_init(struct Admin* admin,
  88. struct CryptoAuth* ca,
  89. struct Allocator* allocator)
  90. {
  91. struct Context* context = Allocator_malloc(allocator, sizeof(struct Context));
  92. context->admin = admin;
  93. context->allocator = allocator;
  94. context->ca = ca;
  95. Identity_set(context);
  96. Admin_registerFunction("AuthorizedPasswords_add", add, context, true,
  97. ((struct Admin_FunctionArg[]){
  98. { .name = "password", .required = 1, .type = "String" },
  99. { .name = "ipv6", .required = 0, .type = "String" },
  100. { .name = "user", .required = 0, .type = "String" }
  101. }), admin);
  102. Admin_registerFunction("AuthorizedPasswords_remove", remove, context, true,
  103. ((struct Admin_FunctionArg[]){
  104. { .name = "user", .required = 1, .type = "String" }
  105. }), admin);
  106. Admin_registerFunction("AuthorizedPasswords_list", list, context, true,
  107. ((struct Admin_FunctionArg[]){
  108. { .name = "page", .required = 0, .type = "Int" }
  109. }), admin);
  110. }