-
Notifications
You must be signed in to change notification settings - Fork 0
/
pam_otp.c
453 lines (353 loc) · 13.3 KB
/
pam_otp.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
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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
/* Copyright (C) 2018 Harry Kodden
*/
#define PAM_SM_ACCOUNT
#define PAM_SM_AUTH
#define PAM_SM_PASSWORD
#define PAM_SM_SESSION
#include <syslog.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <security/pam_appl.h>
#include <security/pam_modules.h>
#include <security/pam_ext.h>
#include <security/pam_modutil.h>
#include <ldap.h>
#include "otp.h"
#define LASTLOGON "Lastlogin: %d"
typedef struct {
int debug;
char *ldap_uri;
char *ldap_basedn;
char *ldap_binddn;
char *ldap_passwd;
char *ldap_filter;
char *uid;
char *ttl;
} module_config;
void free_config(module_config *cfg) {
if (cfg) {
free(&cfg->ldap_uri);
free(&cfg->ldap_basedn);
free(&cfg->ldap_binddn);
free(&cfg->ldap_passwd);
free(&cfg->ldap_filter);
free(&cfg->uid);
free(&cfg->ttl);
free(cfg);
}
}
void debug(const pam_handle_t *pamh, module_config *cfg, const char *fmt, ...) {
if (cfg->debug) {
va_list l;
va_start(l, fmt);
pam_vsyslog(pamh, LOG_DEBUG, fmt, l);
va_end(l);
}
}
int ldap(pam_handle_t * pamh, module_config * cfg, const char *user, const char *token) {
LDAP *ld = NULL;
LDAPMessage *result = NULL;
if (! user) {
pam_syslog(pamh, LOG_ERR, "Module error: called without an user");
return PAM_AUTH_ERR;
}
if (! token) {
pam_syslog(pamh, LOG_ERR, "Module error: called without an token");
return PAM_AUTH_ERR;
}
int status = ldap_initialize(&ld, cfg->ldap_uri);
if (status != LDAP_SUCCESS) {
pam_syslog(pamh, LOG_ERR, "Unable to connect to LDAP server");
return PAM_AUTH_ERR;
}
BerValue *servercred = NULL;
BerValue cred = { .bv_len = strlen(cfg->ldap_passwd) , .bv_val = cfg->ldap_passwd };
int protocol = LDAP_VERSION3;
ldap_set_option(ld, LDAP_OPT_PROTOCOL_VERSION, &protocol);
status = ldap_sasl_bind_s(ld, cfg->ldap_binddn, LDAP_SASL_SIMPLE, &cred, NULL, NULL, &servercred);
debug(pamh, cfg, "Binding...\n");
if (status != LDAP_SUCCESS) {
pam_syslog(pamh, LOG_ERR, "Could not bind to LDAP server: %s", ldap_err2string(status));
// cleanup ldap structure
ldap_unbind_ext(ld, NULL, NULL);
return PAM_AUTH_ERR;
}
char *base;
if (asprintf(&base, "ou=people,%s", cfg->ldap_basedn) < 0) {
ldap_unbind_ext(ld, NULL, NULL);
return PAM_AUTH_ERR;
}
debug(pamh, cfg, "Searching: %s\n", base);
char *filter;
if (cfg->ldap_filter != NULL && strlen(cfg->ldap_filter) > 0) {
asprintf(&filter, "(&%s(%s=%s))", cfg->ldap_filter, cfg->uid, user);
} else {
asprintf(&filter, "(%s=%s)", cfg->uid, user);
}
status = ldap_search_ext_s(ld, base, LDAP_SCOPE_SUBTREE, filter,
0, 0, NULL,
NULL, LDAP_NO_LIMIT, LDAP_NO_LIMIT, &result);
free(base);
free(filter);
if (status != LDAP_SUCCESS) {
pam_syslog(pamh, LOG_ERR, "Could not search in LDAP server: %s", ldap_err2string(status));
// cleanup ldap structure
ldap_unbind_ext(ld, NULL, NULL);
return PAM_AUTH_ERR;
}
int retval = PAM_AUTH_ERR;
LDAPMessage *e = NULL;
for (e = ldap_first_entry(ld, result); e != NULL; e = ldap_next_entry(ld, e)) {
char *dn = NULL;
if ( (dn = ldap_get_dn( ld, e )) != NULL ) {
debug(pamh, cfg, "DN: %s\n", dn);
}
BerElement *ber = NULL;
char *a = NULL;
int match = 0;
char *secret = NULL;
int lastlogon = 0;
for (a = ldap_first_attribute(ld, e, &ber); a != NULL;
a = ldap_next_attribute(ld, e, ber)) {
BerValue **val;
BerValue **vals = ldap_get_values_len(ld, e, a);
for (val = vals; *val; ++val) {
char *v = (*val)->bv_val;
debug(pamh, cfg, "%s -> %s\n", a, v);
if (!strcmp(a, cfg->uid) && !strcmp(v, user)) {
debug(pamh, cfg, "MATCH !!!\n");
match = 1;
}
if (!strcmp(a, "userPassword")) {
debug(pamh, cfg, "SECRET FOUND !!!\n");
secret = strdup(v);
}
if (!strcmp(a, "description") && (sscanf(v, LASTLOGON, &lastlogon) == 1)) {
debug(pamh, cfg, "LAST LOGON FOUND: %d\n", lastlogon);
}
}
ldap_value_free_len(vals);
ldap_memfree(a);
}
ber_free(ber, 0);
if (match && (secret != NULL)) {
debug(pamh, cfg, "EVALUATING TOKEN: %s...\n", token);
debug(pamh, cfg, "Lastlogon = %d\n", lastlogon);
debug(pamh, cfg, "valid_token = %d\n", valid_token(secret, lastlogon, atoi(token)));
debug(pamh, cfg, "valid_token now = %d\n", valid_token(secret, time(NULL), atoi(token)));
if ((lastlogon > 0) && valid_token(secret, lastlogon, atoi(token))) {
int ttl = 0;
if (cfg->ttl != NULL) {
ttl = atoi(cfg->ttl);
}
debug(pamh, cfg, "LAST TOKEN IS VALID ! (exists: %d seconds)\n", time(NULL) - lastlogon);
if (!ttl || ((time(NULL) - lastlogon) <= ttl)) {
retval = PAM_SUCCESS;
}
}
if ((retval != PAM_SUCCESS) && (lastlogon > 0)) {
LDAPMod lastLogon;
LDAPMod *mods[2];
debug(pamh, cfg, "Removing LastLogon...\n");
char *t;
asprintf(&t, LASTLOGON, lastlogon);
char *lastLogon_values[] = {t, NULL};
lastLogon.mod_op = LDAP_MOD_DELETE;
lastLogon.mod_type = "description";
lastLogon.mod_values = lastLogon_values;
mods[0] = &lastLogon;
mods[1] = NULL;
if ((status = ldap_modify_ext_s(ld, dn, mods, NULL, NULL)) != LDAP_SUCCESS) {
pam_syslog(pamh, LOG_ERR, "Could not delete attribute: \"%s\"", ldap_err2string(status));
}
free(t);
}
if ((retval != PAM_SUCCESS) && valid_token(secret, time(NULL), atoi(token))) {
debug(pamh, cfg, "TOKEN OK !\n");
retval = PAM_SUCCESS;
char *t;
debug(pamh, cfg, "Updating LastLogon...\n");
if (asprintf(&t, LASTLOGON, time(NULL)) >= 0) {
debug(pamh, cfg, "Modify LastLogon...\n");
LDAPMod objectClass, lastLogon;
LDAPMod *mods[2];
char *lastLogon_values[] = {t, NULL};
lastLogon.mod_op = LDAP_MOD_REPLACE;
lastLogon.mod_type = "description";
lastLogon.mod_values = lastLogon_values;
mods[0] = &lastLogon;
mods[1] = NULL;
if ((status = ldap_modify_ext_s(ld, dn, mods, NULL, NULL)) != LDAP_SUCCESS) {
pam_syslog(pamh, LOG_ERR, "Could not update attribute: \"%s\"", ldap_err2string(status));
}
free(t);
}
}
if (retval != PAM_SUCCESS) {
debug(pamh, cfg, "TOKEN NOT OK !\n", token);
}
ldap_memfree( dn );
}
free(secret);
if (retval == PAM_SUCCESS)
break;
}
// cleanup
ldap_msgfree(result);
ldap_unbind_ext(ld, NULL, NULL);
return retval;
}
int pam_sm_open_session(pam_handle_t *pamh, int flags, int argc, const char **argv) {
return (PAM_SUCCESS);
}
int pam_sm_close_session(pam_handle_t *pamh, int flags, int argc, const char **argv) {
return (PAM_SUCCESS);
}
int pam_sm_acct_mgmt(pam_handle_t *pamh, int flags, int argc, const char **argv) {
return (PAM_SUCCESS);
}
/**
* Handles the basic parsing of a given option.
* @arg buf is the buffer to be parsed
* @arg opt_name_with_eq is the option name we are looking for (including equal sign)
* Note that dst has to be freed by the caller in case of 0 return code
* returns 0 if the option was not found
* returns -1 if an error occured (duplicate option)
* returns the position of the start of the value in the buffer otherwise
*/
int raw_parse_option(pam_handle_t *pamh, const char* buf, const char* opt_name_with_eq, char** dst) {
size_t opt_len = strlen(opt_name_with_eq);
if (0 == strncmp(buf, opt_name_with_eq, opt_len)) {
if (dst && *dst) {
pam_syslog(pamh, LOG_ERR,
"Duplicated option : %s. Only first one is taken into account",
opt_name_with_eq);
return -1;
} else {
return (int)opt_len;
}
}
return 0;
}
/// calls strdup and returns whether we had a memory error
int strdup_or_die(char** dst, const char* src) {
*dst = strdup(src);
return *dst ? 0 : -1;
}
/**
* Handles the parsing of a given option.
* @arg buf is the buffer to be parsed
* @arg opt_name_with_eq is the option name we are looking for (including equal sign)
* @arg dst is the destination buffer for the value found if any.
* Note that dst has to be freed by the caller in case of 0 return code
* returns 0 if the option was not found in the buffer
* returns 1 if the option was found in buffer and parsed properly
* returns -1 in case of error
*/
int parse_str_option(pam_handle_t *pamh, const char* buf, const char* opt_name_with_eq, char** dst) {
int value_pos = raw_parse_option(pamh, buf, opt_name_with_eq, dst);
if (value_pos > 0) {
if (strdup_or_die(dst, buf + value_pos)) {
return -1;
}
return 1;
} else if (value_pos == -1) {
// Don't crash on duplicate, ignore 2nd value
return 1;
}
return value_pos;
}
void parse_config(pam_handle_t *pamh, int argc, const char **argv, module_config **ncfg) {
module_config *cfg = NULL;
int mem_error = 0;
int i;
cfg = (module_config *) calloc(1, sizeof(module_config));
if (!cfg) {
pam_syslog(pamh, LOG_CRIT, "Out of memory");
return;
}
cfg->debug = 0;
cfg->ldap_uri = NULL;
cfg->ldap_basedn = NULL;
cfg->ldap_binddn = NULL;
cfg->ldap_passwd = NULL;
cfg->ldap_filter = NULL;
cfg->uid = NULL;
cfg->ttl = NULL;
for (i = 0; i < argc; ++i) {
int retval = !strcmp(argv[i], "debug");
if (retval) cfg->debug = 1;
if (retval == 0) retval = parse_str_option(pamh, argv[i], "ldap=", &cfg->ldap_uri);
if (retval == 0) retval = parse_str_option(pamh, argv[i], "basedn=", &cfg->ldap_basedn);
if (retval == 0) retval = parse_str_option(pamh, argv[i], "binddn=", &cfg->ldap_binddn);
if (retval == 0) retval = parse_str_option(pamh, argv[i], "passwd=", &cfg->ldap_passwd);
if (retval == 0) retval = parse_str_option(pamh, argv[i], "filter=", &cfg->ldap_filter);
if (retval == 0) retval = parse_str_option(pamh, argv[i], "uid=", &cfg->uid);
if (retval == 0) retval = parse_str_option(pamh, argv[i], "ttl=", &cfg->ttl);
if (0 == retval) {
pam_syslog(pamh, LOG_ERR, "Invalid option: %s", argv[i]);
free_config(cfg);
return;
} else if (retval < 0) {
mem_error = retval;
break;
}
}
// in case we got a memory error in the previous code, give up immediately
if (mem_error) {
pam_syslog(pamh, LOG_CRIT, "Out of memory");
free_config(cfg);
return;
}
if (! cfg->uid) {
debug(pamh, cfg, "Setting default value for 'uid' (=uid)");
cfg->uid = strdup("uid");
}
if (! cfg->ttl) {
debug(pamh, cfg, "Setting default value for 'ttl' (=0)");
cfg->ttl = strdup("0");
}
if (! cfg->ldap_filter) {
debug(pamh, cfg, "Setting default value for 'filter' (=\"\")");
cfg->ldap_filter = strdup("");
}
debug(pamh, cfg, "debug => %d", cfg->debug);
debug(pamh, cfg, "ldap_uri => %s", cfg->ldap_uri);
debug(pamh, cfg, "ldap_basedn => %s", cfg->ldap_basedn);
debug(pamh, cfg, "ldap_binddn => %s", cfg->ldap_binddn);
debug(pamh, cfg, "ldap_passwd => %s", cfg->ldap_passwd);
debug(pamh, cfg, "ldap_filter => %s", cfg->ldap_filter);
debug(pamh, cfg, "uid => %s", cfg->uid);
debug(pamh, cfg, "ttl => %s", cfg->ttl);
*ncfg = cfg;
}
int pam_sm_authenticate(pam_handle_t *pamh, int flags, int argc, const char **argv) {
char *user = NULL;
char *token = NULL;
module_config *cfg = NULL;
parse_config(pamh, argc, argv, &cfg);
if (!cfg) {
pam_syslog(pamh, LOG_ERR, "configuration invalid");
return PAM_AUTH_ERR;
}
(void) pam_get_user(pamh, (const char **) &user, NULL);
if (!user) {
return PAM_USER_UNKNOWN;
}
if (pam_prompt(pamh, PAM_PROMPT_ECHO_ON, &token, "%s", "TOKEN: ") != PAM_SUCCESS) {
pam_syslog(pamh, LOG_INFO, "Unable to get user input");
return PAM_AUTH_ERR;
}
int result = ldap(pamh, cfg, user, token);
free(token);
return (result);
}
int pam_sm_setcred(pam_handle_t *pamh, int flags, int argc, const char **argv) {
return (PAM_SUCCESS);
}
int pam_sm_chauthtok(pam_handle_t *pamh, int flags, int argc, const char **argv) {
return (PAM_SUCCESS);
}