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