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