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