This repository was archived by the owner on Apr 13, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplugin.c
More file actions
319 lines (228 loc) · 5.76 KB
/
Copy pathplugin.c
File metadata and controls
319 lines (228 loc) · 5.76 KB
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
#define _GNU_SOURCE
#include <string.h>
#include <ldap.h>
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <errno.h>
#include <ctype.h>
#include "api/plugin.h"
#define AuthResult(t) PLG_AUTH_##t
#if defined(NDEBUG)
#define debug(fmt,...)
#else
#define debug(fmt,...) do { fprintf(stderr, "DEBUG [%d] "fmt"\n", __LINE__,##__VA_ARGS__);} while(0)
#endif
#define error(fmt,...) do { fprintf(stderr, "ERROR [%d] "fmt"\n", __LINE__,##__VA_ARGS__);} while(0)
typedef struct {
const char *key;
char *value;
} Option;
enum {
OPTION_URL = 0,
OPTION_BASE_DN,
OPTION_BIND_DN,
OPTION_BIND_PASSWD,
OPTION_USER_SEARCH,
OPTION_USER_ATTR
};
static Option options[] = {
{.key = "url", .value = NULL },
{.key = "base.dn", .value = NULL },
{.key = "bind.dn", .value = NULL },
{.key = "bind.password", .value = NULL },
{.key = "user.search", .value = NULL },
{.key = "user.attr.username", .value = NULL },
{0}
};
#define OPTION(t) options[OPTION_##t].value
static int user_auth(const char*, const char*);
static int load_config(const char*);
static char* substitute(const char*, const char*, const char*);
static char* ldap_user_getdn(LDAP*, const char*, const char*, const char*, const char*);
static int bind_with_user(LDAP *, const char *, const char *);
static void parse_line(char *);
/*
* This is the function we are required to implement.
*/
int
plugin_init(Plugin *p, const char *cfg)
{
/* Since the plugin has a configuration file,
we need to parse and load the configuration first. */
if (load_config(cfg))
return -1;
plugin_register_auth(p, user_auth);
return 0;
}
static
int
user_auth(const char *username, const char *password)
{
assert(username);
assert(password);
LDAP *ldap = NULL;
int result = AuthResult(DENIED);
debug("Initializing LDAP ...");
int rc = ldap_initialize(&ldap, OPTION(URL));
if (rc){
error("LDAP error: %s.",ldap_err2string(rc));
return result;
}
rc = bind_with_user(ldap, OPTION(BIND_DN), OPTION(BIND_PASSWD));
if (rc){
ldap_unbind_ext_s(ldap,NULL,NULL);
goto exit_auth;
}
char *user_dn = ldap_user_getdn(ldap, username, OPTION(BASE_DN), OPTION(USER_SEARCH), OPTION(USER_ATTR));
if (NULL != user_dn){
rc = bind_with_user(ldap, user_dn, password);
result = rc ? AuthResult(DENIED) : AuthResult(GRANTED);
free(user_dn);
}
exit_auth:;
ldap_unbind_ext_s(ldap,NULL,NULL);
return result;
}
static int
bind_with_user(LDAP *ldap, const char *user_dn, const char *password)
{
assert(ldap);
assert(user_dn);
assert(password);
int p = LDAP_VERSION3;
int rc = ldap_set_option(ldap, LDAP_OPT_PROTOCOL_VERSION, &p);
if (LDAP_OPT_SUCCESS != rc){
error("LDAP error: %s.", ldap_err2string(rc));
return -1;
}
rc = ldap_simple_bind_s(ldap, user_dn, password);
if (rc){
error("LDAP error: %s.", ldap_err2string(rc));
return -1;
}
debug("User %s bound to LDAP successfully.", user_dn);
return 0;
}
static
char*
substitute(const char *string_s, const char *pattern_s,
const char *input_s)
{
assert(string_s);
size_t plen = strlen(pattern_s);
size_t slen = strlen(string_s);
size_t ilen = strlen(input_s);
size_t olen = slen - plen + ilen + 1;
char *begin = strstr(string_s, pattern_s);
if (!begin)
return NULL;
char *out = malloc(olen);
memcpy(out, string_s,begin - string_s);
int offset = begin - string_s;
memcpy(out + offset, input_s, ilen);
offset += ilen;
memcpy(out + offset,begin + plen,slen - (begin - string_s) - plen);
*(out + olen - 1) = '\0';
return out;
}
static char*
ldap_user_getdn(LDAP *ldap, const char *username_s, const char *base_dn_s,
const char *user_search_s, const char *user_attr_name_s)
{
LDAPMessage *result;
char *user_dn = NULL;
char *search_s = substitute(user_search_s,"$", username_s);
char *attrs[] = { OPTION(USER_ATTR), NULL};
debug("LDAP Searching LDAP with %s ...",search_s);
int rc = ldap_search_ext_s(ldap, base_dn_s, LDAP_SCOPE_SUBTREE, search_s,
attrs, 0, NULL, NULL, NULL, 0, &result);
if (rc){
error("LDAP search error: %s.", ldap_err2string(rc));
free(search_s);
return NULL;
}
char *dn;
for (LDAPMessage *e = ldap_first_entry(ldap, result); e; e = ldap_next_entry(ldap, e)){
if (NULL != user_dn)
break;
if (NULL == (dn = ldap_get_dn(ldap, e)))
continue;
BerElement *ber;
struct berval **vals;
for (char *a = ldap_first_attribute(ldap, e, &ber); a; a = ldap_next_attribute(ldap, e, ber)){
if (strcmp(user_attr_name_s, a)){
ldap_memfree(a);
continue;
}
if ((vals = ldap_get_values_len(ldap, e, a))){
for (size_t i = 0; vals[i]; i++ ) {
if (strcmp(username_s, vals[i]->bv_val))
continue;
user_dn = strdup(dn);
debug("LDAP Found %s user: %s", username_s, dn);
break;
}
ldap_value_free_len(vals);
}
ldap_memfree(a);
}
ber_free(ber, 0);
ldap_memfree(dn);
}
ldap_msgfree(result);
if (!user_dn)
debug("LDAP: No such user: %s",username_s);
free(search_s);
return user_dn;
}
static void
parse_line(char *line)
{
while (isspace(*line))
line++;
if ('#' == *line)
return;
char *val = strchr(line, '=');
if (NULL == val)
return;
*val = '\0';
char *k = line;
char *v = ++val;
char *n = strchr(v, '\n');
if (NULL != n){
*n = '\0';
}
for (Option *o = options; o->key; o++){
if (o->value || strcmp(o->key, k))
continue;
o->value = strdup(v);
}
}
static
int
load_config(const char *cfg)
{
char buff[512];
if (NULL == cfg){
error("No configuration file!");
return -1;
}
FILE *c = fopen(cfg, "r");
if (NULL == c){
error("Cannot read configuration file! %s", strerror(errno));
return -1;
}
while (fgets(buff, sizeof(buff), c)){
char *line = buff;
parse_line(line);
}
fclose(c);
for (Option *o = options; o->key; o++){
if (NULL == o->value){
error("Missing %s option!", o->key);
return -1;
}
}
return 0;
}