ext_password.c 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. /*
  2. * External password backend
  3. * Copyright (c) 2012, Jouni Malinen <j@w1.fi>
  4. *
  5. * This software may be distributed under the terms of the BSD license.
  6. * See README for more details.
  7. */
  8. #include "includes.h"
  9. #ifdef __linux__
  10. #include <sys/mman.h>
  11. #endif /* __linux__ */
  12. #include "common.h"
  13. #include "ext_password_i.h"
  14. #ifdef CONFIG_EXT_PASSWORD_TEST
  15. extern struct ext_password_backend ext_password_test;
  16. #endif /* CONFIG_EXT_PASSWORD_TEST */
  17. static const struct ext_password_backend *backends[] = {
  18. #ifdef CONFIG_EXT_PASSWORD_TEST
  19. &ext_password_test,
  20. #endif /* CONFIG_EXT_PASSWORD_TEST */
  21. NULL
  22. };
  23. struct ext_password_data {
  24. const struct ext_password_backend *backend;
  25. void *priv;
  26. };
  27. struct ext_password_data * ext_password_init(const char *backend,
  28. const char *params)
  29. {
  30. struct ext_password_data *data;
  31. int i;
  32. data = os_zalloc(sizeof(*data));
  33. if (data == NULL)
  34. return NULL;
  35. for (i = 0; backends[i]; i++) {
  36. if (os_strcmp(backends[i]->name, backend) == 0) {
  37. data->backend = backends[i];
  38. break;
  39. }
  40. }
  41. if (!data->backend) {
  42. os_free(data);
  43. return NULL;
  44. }
  45. data->priv = data->backend->init(params);
  46. if (data->priv == NULL) {
  47. os_free(data);
  48. return NULL;
  49. }
  50. return data;
  51. }
  52. void ext_password_deinit(struct ext_password_data *data)
  53. {
  54. if (data && data->backend && data->priv)
  55. data->backend->deinit(data->priv);
  56. os_free(data);
  57. }
  58. struct wpabuf * ext_password_get(struct ext_password_data *data,
  59. const char *name)
  60. {
  61. if (data == NULL)
  62. return NULL;
  63. return data->backend->get(data->priv, name);
  64. }
  65. struct wpabuf * ext_password_alloc(size_t len)
  66. {
  67. struct wpabuf *buf;
  68. buf = wpabuf_alloc(len);
  69. if (buf == NULL)
  70. return NULL;
  71. #ifdef __linux__
  72. if (mlock(wpabuf_head(buf), wpabuf_len(buf)) < 0) {
  73. wpa_printf(MSG_ERROR, "EXT PW: mlock failed: %s",
  74. strerror(errno));
  75. }
  76. #endif /* __linux__ */
  77. return buf;
  78. }
  79. void ext_password_free(struct wpabuf *pw)
  80. {
  81. if (pw == NULL)
  82. return;
  83. os_memset(wpabuf_mhead(pw), 0, wpabuf_len(pw));
  84. #ifdef __linux__
  85. if (munlock(wpabuf_head(pw), wpabuf_len(pw)) < 0) {
  86. wpa_printf(MSG_ERROR, "EXT PW: munlock failed: %s",
  87. strerror(errno));
  88. }
  89. #endif /* __linux__ */
  90. wpabuf_free(pw);
  91. }