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