1
0

AuthorizedPasswords.c 4.4 KB

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