-
Notifications
You must be signed in to change notification settings - Fork 122
/
patch-src_libcharon_plugins_xauth__generic_xauth__generic.c
194 lines (187 loc) · 4.9 KB
/
patch-src_libcharon_plugins_xauth__generic_xauth__generic.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
--- src/libcharon/plugins/xauth_generic/xauth_generic.c.orig 2015-04-24 19:32:06 UTC
+++ src/libcharon/plugins/xauth_generic/xauth_generic.c
@@ -13,10 +13,15 @@
* for more details.
*/
+#include <sys/types.h>
+#include <sys/wait.h>
+
#include "xauth_generic.h"
#include <daemon.h>
#include <library.h>
+#include <unistd.h>
+#include <errno.h>
typedef struct private_xauth_generic_t private_xauth_generic_t;
@@ -41,6 +46,103 @@ struct private_xauth_generic_t {
identification_t *peer;
};
+/**
+ * Add/Append to the environment pointer name=value pair
+ */
+static char *
+name_value_pair(const char *name, char *value)
+{
+ char *envitem;
+
+ envitem = malloc(strlen(name) + 1 + strlen(value) + 1);
+ if (envitem == NULL) {
+ DBG1(DBG_IKE,
+ "Cannot allocate memory.");
+ return NULL;
+ }
+ sprintf(envitem, "%s=%s", name, value);
+
+ return envitem;
+}
+
+/**
+ * Convert configuration attribute content to a null-terminated string
+ */
+static void attr2string(char *buf, size_t len, chunk_t chunk)
+{
+ if (chunk.len && chunk.len < len)
+ {
+ snprintf(buf, len, "%.*s", (int)chunk.len, chunk.ptr);
+ }
+}
+
+/**
+ * Authenticate a username/password using script
+ */
+static bool
+authenticate(char *service, chunk_t username, chunk_t password, char *authcfg)
+{
+ pid_t pid, rpid;
+ char *envp[4] = { NULL, NULL, NULL, NULL };
+ char *argv[3] = { NULL, NULL, NULL };
+ char user[128] = "", pass[128] = "";
+ int envc = 4;
+ int ret;
+
+ attr2string(user, sizeof(user), username);
+ attr2string(pass, sizeof(pass), password);
+
+ envp[0] = name_value_pair("username", user);
+ envp[1] = name_value_pair("password", pass);
+ envp[2] = name_value_pair("authcfg", authcfg);
+ envp[3] = NULL;
+
+ argv[0] = service;
+ argv[1] = service;
+ argv[2] = NULL;
+
+ pid = fork();
+ switch (pid) {
+ case 0:
+ execve(argv[0], argv, envp);
+ DBG1(DBG_IKE, "XAUTH-SCRIPT failed to execute script '%s'.", service);
+ _exit (127);
+ break;
+ case -1:
+ ret = -1;
+ break;
+ default:
+ do {
+ rpid = waitpid (pid, &ret, 0);
+ } while (rpid == -1 && errno == EINTR);
+ if (rpid != pid)
+ ret = -1;
+
+ break;
+ }
+
+ if (WIFEXITED(ret)) {
+ if (WEXITSTATUS(ret) == 0)
+ ret = 0;
+ else
+ ret = -1;
+ }
+ if (ret == 0)
+ DBG1(DBG_IKE, "XAuth-SCRIPT succeeded for user '%s'.", user);
+ else
+ DBG1(DBG_IKE, "XAuth-SCRIPT failed for user '%s' with return status: %d.",
+ user, ret);
+
+ if (envp[0] != NULL)
+ free(envp[0]);
+ if (envp[1] != NULL)
+ free(envp[1]);
+ if (envp[2] != NULL)
+ free(envp[2]);
+
+ return ret;
+}
+
METHOD(xauth_method_t, initiate_peer, status_t,
private_xauth_generic_t *this, cp_payload_t **out)
{
@@ -137,6 +239,7 @@ METHOD(xauth_method_t, process_server, s
chunk_t user = chunk_empty, pass = chunk_empty;
status_t status = FAILED;
int tried = 0;
+ char *service, *authcfg;
enumerator = in->create_attribute_enumerator(in);
while (enumerator->enumerate(enumerator, &attr))
@@ -176,29 +279,45 @@ METHOD(xauth_method_t, process_server, s
pass.len -= 1;
}
- enumerator = lib->credmgr->create_shared_enumerator(lib->credmgr,
- SHARED_EAP, this->server, this->peer);
- while (enumerator->enumerate(enumerator, &shared, NULL, NULL))
- {
- if (chunk_equals_const(shared->get_key(shared), pass))
- {
+ /* XXX: Maybe support even FCGI calling here? */
+ service = lib->settings->get_str(lib->settings,
+ "%s.plugins.xauth-generic.script", NULL, lib->ns);
+ if (service) {
+ authcfg = lib->settings->get_str(lib->settings,
+ "%s.plugins.xauth-generic.authcfg",
+ NULL, lib->ns);
+ if (!authenticate(service, user, pass, authcfg))
status = SUCCESS;
- break;
+ else {
+ DBG1(DBG_IKE, "Could not authenticate with XAuth secrets for '%Y' - '%Y' ",
+ this->server, this->peer);
}
- tried++;
- }
- enumerator->destroy(enumerator);
- if (status != SUCCESS)
- {
- if (!tried)
+ } else {
+
+ enumerator = lib->credmgr->create_shared_enumerator(lib->credmgr,
+ SHARED_EAP, this->server, this->peer);
+ while (enumerator->enumerate(enumerator, &shared, NULL, NULL))
{
- DBG1(DBG_IKE, "no XAuth secret found for '%Y' - '%Y'",
- this->server, this->peer);
+ if (chunk_equals_const(shared->get_key(shared), pass))
+ {
+ status = SUCCESS;
+ break;
+ }
+ tried++;
}
- else
+ enumerator->destroy(enumerator);
+ if (status != SUCCESS)
{
- DBG1(DBG_IKE, "none of %d found XAuth secrets for '%Y' - '%Y' "
- "matched", tried, this->server, this->peer);
+ if (!tried)
+ {
+ DBG1(DBG_IKE, "no XAuth secret found for '%Y' - '%Y'",
+ this->server, this->peer);
+ }
+ else
+ {
+ DBG1(DBG_IKE, "none of %d found XAuth secrets for '%Y' - '%Y' "
+ "matched", tried, this->server, this->peer);
+ }
}
}
return status;