161d9df3e62aaa0e87ad05452fcb95142159a17b6Dmitry Shmidt/*
261d9df3e62aaa0e87ad05452fcb95142159a17b6Dmitry Shmidt * External password backend
361d9df3e62aaa0e87ad05452fcb95142159a17b6Dmitry Shmidt * Copyright (c) 2012, Jouni Malinen <j@w1.fi>
461d9df3e62aaa0e87ad05452fcb95142159a17b6Dmitry Shmidt *
561d9df3e62aaa0e87ad05452fcb95142159a17b6Dmitry Shmidt * This software may be distributed under the terms of the BSD license.
661d9df3e62aaa0e87ad05452fcb95142159a17b6Dmitry Shmidt * See README for more details.
761d9df3e62aaa0e87ad05452fcb95142159a17b6Dmitry Shmidt */
861d9df3e62aaa0e87ad05452fcb95142159a17b6Dmitry Shmidt
961d9df3e62aaa0e87ad05452fcb95142159a17b6Dmitry Shmidt#include "includes.h"
1061d9df3e62aaa0e87ad05452fcb95142159a17b6Dmitry Shmidt
1161d9df3e62aaa0e87ad05452fcb95142159a17b6Dmitry Shmidt#ifdef __linux__
1261d9df3e62aaa0e87ad05452fcb95142159a17b6Dmitry Shmidt#include <sys/mman.h>
1361d9df3e62aaa0e87ad05452fcb95142159a17b6Dmitry Shmidt#endif /* __linux__ */
1461d9df3e62aaa0e87ad05452fcb95142159a17b6Dmitry Shmidt
1561d9df3e62aaa0e87ad05452fcb95142159a17b6Dmitry Shmidt#include "common.h"
1661d9df3e62aaa0e87ad05452fcb95142159a17b6Dmitry Shmidt#include "ext_password_i.h"
1761d9df3e62aaa0e87ad05452fcb95142159a17b6Dmitry Shmidt
1861d9df3e62aaa0e87ad05452fcb95142159a17b6Dmitry Shmidt
1961d9df3e62aaa0e87ad05452fcb95142159a17b6Dmitry Shmidt#ifdef CONFIG_EXT_PASSWORD_TEST
2061d9df3e62aaa0e87ad05452fcb95142159a17b6Dmitry Shmidtextern struct ext_password_backend ext_password_test;
2161d9df3e62aaa0e87ad05452fcb95142159a17b6Dmitry Shmidt#endif /* CONFIG_EXT_PASSWORD_TEST */
2261d9df3e62aaa0e87ad05452fcb95142159a17b6Dmitry Shmidt
2361d9df3e62aaa0e87ad05452fcb95142159a17b6Dmitry Shmidtstatic const struct ext_password_backend *backends[] = {
2461d9df3e62aaa0e87ad05452fcb95142159a17b6Dmitry Shmidt#ifdef CONFIG_EXT_PASSWORD_TEST
2561d9df3e62aaa0e87ad05452fcb95142159a17b6Dmitry Shmidt	&ext_password_test,
2661d9df3e62aaa0e87ad05452fcb95142159a17b6Dmitry Shmidt#endif /* CONFIG_EXT_PASSWORD_TEST */
2761d9df3e62aaa0e87ad05452fcb95142159a17b6Dmitry Shmidt	NULL
2861d9df3e62aaa0e87ad05452fcb95142159a17b6Dmitry Shmidt};
2961d9df3e62aaa0e87ad05452fcb95142159a17b6Dmitry Shmidt
3061d9df3e62aaa0e87ad05452fcb95142159a17b6Dmitry Shmidtstruct ext_password_data {
3161d9df3e62aaa0e87ad05452fcb95142159a17b6Dmitry Shmidt	const struct ext_password_backend *backend;
3261d9df3e62aaa0e87ad05452fcb95142159a17b6Dmitry Shmidt	void *priv;
3361d9df3e62aaa0e87ad05452fcb95142159a17b6Dmitry Shmidt};
3461d9df3e62aaa0e87ad05452fcb95142159a17b6Dmitry Shmidt
3561d9df3e62aaa0e87ad05452fcb95142159a17b6Dmitry Shmidt
3661d9df3e62aaa0e87ad05452fcb95142159a17b6Dmitry Shmidtstruct ext_password_data * ext_password_init(const char *backend,
3761d9df3e62aaa0e87ad05452fcb95142159a17b6Dmitry Shmidt					     const char *params)
3861d9df3e62aaa0e87ad05452fcb95142159a17b6Dmitry Shmidt{
3961d9df3e62aaa0e87ad05452fcb95142159a17b6Dmitry Shmidt	struct ext_password_data *data;
4061d9df3e62aaa0e87ad05452fcb95142159a17b6Dmitry Shmidt	int i;
4161d9df3e62aaa0e87ad05452fcb95142159a17b6Dmitry Shmidt
4261d9df3e62aaa0e87ad05452fcb95142159a17b6Dmitry Shmidt	data = os_zalloc(sizeof(*data));
4361d9df3e62aaa0e87ad05452fcb95142159a17b6Dmitry Shmidt	if (data == NULL)
4461d9df3e62aaa0e87ad05452fcb95142159a17b6Dmitry Shmidt		return NULL;
4561d9df3e62aaa0e87ad05452fcb95142159a17b6Dmitry Shmidt
4661d9df3e62aaa0e87ad05452fcb95142159a17b6Dmitry Shmidt	for (i = 0; backends[i]; i++) {
4761d9df3e62aaa0e87ad05452fcb95142159a17b6Dmitry Shmidt		if (os_strcmp(backends[i]->name, backend) == 0) {
4861d9df3e62aaa0e87ad05452fcb95142159a17b6Dmitry Shmidt			data->backend = backends[i];
4961d9df3e62aaa0e87ad05452fcb95142159a17b6Dmitry Shmidt			break;
5061d9df3e62aaa0e87ad05452fcb95142159a17b6Dmitry Shmidt		}
5161d9df3e62aaa0e87ad05452fcb95142159a17b6Dmitry Shmidt	}
5261d9df3e62aaa0e87ad05452fcb95142159a17b6Dmitry Shmidt
5361d9df3e62aaa0e87ad05452fcb95142159a17b6Dmitry Shmidt	if (!data->backend) {
5461d9df3e62aaa0e87ad05452fcb95142159a17b6Dmitry Shmidt		os_free(data);
5561d9df3e62aaa0e87ad05452fcb95142159a17b6Dmitry Shmidt		return NULL;
5661d9df3e62aaa0e87ad05452fcb95142159a17b6Dmitry Shmidt	}
5761d9df3e62aaa0e87ad05452fcb95142159a17b6Dmitry Shmidt
5861d9df3e62aaa0e87ad05452fcb95142159a17b6Dmitry Shmidt	data->priv = data->backend->init(params);
5961d9df3e62aaa0e87ad05452fcb95142159a17b6Dmitry Shmidt	if (data->priv == NULL) {
6061d9df3e62aaa0e87ad05452fcb95142159a17b6Dmitry Shmidt		os_free(data);
6161d9df3e62aaa0e87ad05452fcb95142159a17b6Dmitry Shmidt		return NULL;
6261d9df3e62aaa0e87ad05452fcb95142159a17b6Dmitry Shmidt	}
6361d9df3e62aaa0e87ad05452fcb95142159a17b6Dmitry Shmidt
6461d9df3e62aaa0e87ad05452fcb95142159a17b6Dmitry Shmidt	return data;
6561d9df3e62aaa0e87ad05452fcb95142159a17b6Dmitry Shmidt}
6661d9df3e62aaa0e87ad05452fcb95142159a17b6Dmitry Shmidt
6761d9df3e62aaa0e87ad05452fcb95142159a17b6Dmitry Shmidt
6861d9df3e62aaa0e87ad05452fcb95142159a17b6Dmitry Shmidtvoid ext_password_deinit(struct ext_password_data *data)
6961d9df3e62aaa0e87ad05452fcb95142159a17b6Dmitry Shmidt{
7061d9df3e62aaa0e87ad05452fcb95142159a17b6Dmitry Shmidt	if (data && data->backend && data->priv)
7161d9df3e62aaa0e87ad05452fcb95142159a17b6Dmitry Shmidt		data->backend->deinit(data->priv);
7261d9df3e62aaa0e87ad05452fcb95142159a17b6Dmitry Shmidt	os_free(data);
7361d9df3e62aaa0e87ad05452fcb95142159a17b6Dmitry Shmidt}
7461d9df3e62aaa0e87ad05452fcb95142159a17b6Dmitry Shmidt
7561d9df3e62aaa0e87ad05452fcb95142159a17b6Dmitry Shmidt
7661d9df3e62aaa0e87ad05452fcb95142159a17b6Dmitry Shmidtstruct wpabuf * ext_password_get(struct ext_password_data *data,
7761d9df3e62aaa0e87ad05452fcb95142159a17b6Dmitry Shmidt				 const char *name)
7861d9df3e62aaa0e87ad05452fcb95142159a17b6Dmitry Shmidt{
7961d9df3e62aaa0e87ad05452fcb95142159a17b6Dmitry Shmidt	if (data == NULL)
8061d9df3e62aaa0e87ad05452fcb95142159a17b6Dmitry Shmidt		return NULL;
8161d9df3e62aaa0e87ad05452fcb95142159a17b6Dmitry Shmidt	return data->backend->get(data->priv, name);
8261d9df3e62aaa0e87ad05452fcb95142159a17b6Dmitry Shmidt}
8361d9df3e62aaa0e87ad05452fcb95142159a17b6Dmitry Shmidt
8461d9df3e62aaa0e87ad05452fcb95142159a17b6Dmitry Shmidt
8561d9df3e62aaa0e87ad05452fcb95142159a17b6Dmitry Shmidtstruct wpabuf * ext_password_alloc(size_t len)
8661d9df3e62aaa0e87ad05452fcb95142159a17b6Dmitry Shmidt{
8761d9df3e62aaa0e87ad05452fcb95142159a17b6Dmitry Shmidt	struct wpabuf *buf;
8861d9df3e62aaa0e87ad05452fcb95142159a17b6Dmitry Shmidt
8961d9df3e62aaa0e87ad05452fcb95142159a17b6Dmitry Shmidt	buf = wpabuf_alloc(len);
9061d9df3e62aaa0e87ad05452fcb95142159a17b6Dmitry Shmidt	if (buf == NULL)
9161d9df3e62aaa0e87ad05452fcb95142159a17b6Dmitry Shmidt		return NULL;
9261d9df3e62aaa0e87ad05452fcb95142159a17b6Dmitry Shmidt
9361d9df3e62aaa0e87ad05452fcb95142159a17b6Dmitry Shmidt#ifdef __linux__
9461d9df3e62aaa0e87ad05452fcb95142159a17b6Dmitry Shmidt	if (mlock(wpabuf_head(buf), wpabuf_len(buf)) < 0) {
9561d9df3e62aaa0e87ad05452fcb95142159a17b6Dmitry Shmidt		wpa_printf(MSG_ERROR, "EXT PW: mlock failed: %s",
9661d9df3e62aaa0e87ad05452fcb95142159a17b6Dmitry Shmidt			   strerror(errno));
9761d9df3e62aaa0e87ad05452fcb95142159a17b6Dmitry Shmidt	}
9861d9df3e62aaa0e87ad05452fcb95142159a17b6Dmitry Shmidt#endif /* __linux__ */
9961d9df3e62aaa0e87ad05452fcb95142159a17b6Dmitry Shmidt
10061d9df3e62aaa0e87ad05452fcb95142159a17b6Dmitry Shmidt	return buf;
10161d9df3e62aaa0e87ad05452fcb95142159a17b6Dmitry Shmidt}
10261d9df3e62aaa0e87ad05452fcb95142159a17b6Dmitry Shmidt
10361d9df3e62aaa0e87ad05452fcb95142159a17b6Dmitry Shmidt
10461d9df3e62aaa0e87ad05452fcb95142159a17b6Dmitry Shmidtvoid ext_password_free(struct wpabuf *pw)
10561d9df3e62aaa0e87ad05452fcb95142159a17b6Dmitry Shmidt{
10661d9df3e62aaa0e87ad05452fcb95142159a17b6Dmitry Shmidt	if (pw == NULL)
10761d9df3e62aaa0e87ad05452fcb95142159a17b6Dmitry Shmidt		return;
10861d9df3e62aaa0e87ad05452fcb95142159a17b6Dmitry Shmidt	os_memset(wpabuf_mhead(pw), 0, wpabuf_len(pw));
10961d9df3e62aaa0e87ad05452fcb95142159a17b6Dmitry Shmidt#ifdef __linux__
11061d9df3e62aaa0e87ad05452fcb95142159a17b6Dmitry Shmidt	if (munlock(wpabuf_head(pw), wpabuf_len(pw)) < 0) {
11161d9df3e62aaa0e87ad05452fcb95142159a17b6Dmitry Shmidt		wpa_printf(MSG_ERROR, "EXT PW: munlock failed: %s",
11261d9df3e62aaa0e87ad05452fcb95142159a17b6Dmitry Shmidt			   strerror(errno));
11361d9df3e62aaa0e87ad05452fcb95142159a17b6Dmitry Shmidt	}
11461d9df3e62aaa0e87ad05452fcb95142159a17b6Dmitry Shmidt#endif /* __linux__ */
11561d9df3e62aaa0e87ad05452fcb95142159a17b6Dmitry Shmidt	wpabuf_free(pw);
11661d9df3e62aaa0e87ad05452fcb95142159a17b6Dmitry Shmidt}
117