AuthorizedPasswords.c 4.5 KB

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