1
0

AuthorizedPasswords.c 4.9 KB

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