AuthorizedPasswords.c 4.8 KB

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