-
Notifications
You must be signed in to change notification settings - Fork 74
/
otp.c
784 lines (650 loc) · 21 KB
/
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
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
#include <config.h>
#include <stdio.h>
#include <stdint.h>
#ifdef HAVE_ENDIAN_H
#include <endian.h>
#elif HAVE_SYS_ENDIAN_H
#include <sys/endian.h>
#endif
#include <string.h>
#include <ctype.h>
#include <inttypes.h>
#ifndef htobe64
#include <netinet/in.h>
#endif
#include <openssl/evp.h>
#include <openssl/hmac.h>
#include <openssl/sha.h>
#ifdef HAVE_OPENVPN_OPENVPN_PLUGIN_H
#include "openvpn/openvpn-plugin.h"
#elif HAVE_OPENVPN_PLUGIN_H
#include "openvpn-plugin.h"
#endif
#include "base32.h"
#include "hex.h"
#define MAXWORDLEN 256
#include "openvpn-cr.h"
static char *otp_secrets = "/etc/ppp/otp-secrets";
static char *hotp_counters = "/var/spool/openvpn/hotp-counters/";
static int otp_slop = 180;
static int totp_t0 = 0;
static int totp_step = 30;
static int totp_digits = 6;
static int motp_step = 10;
static int hotp_syncwindow = 2;
static int debug = 0;
static int password_is_cr = 0;
typedef struct user_entry {
char name[MAXWORDLEN];
char server[MAXWORDLEN];
char secret[MAXWORDLEN];
char addr[MAXWORDLEN];
} user_entry_t;
typedef struct otp_params {
const char *method;
const char *hash;
const char *encoding;
const char *key;
const char *pin;
const char *udid;
} otp_params_t;
#define LOG(format, ...) logmessage(format, ## __VA_ARGS__)
#define DEBUG(format, ...) logdebug(format, ## __VA_ARGS__)
static void logmessage(const char *format, ...)
{
va_list va;
va_start(va, format);
vfprintf(stderr, format, va);
va_end(va);
}
static void logdebug(const char *format, ...)
{
if (debug > 0) {
va_list va;
va_start(va, format);
vfprintf(stderr, format, va);
va_end(va);
}
}
#ifndef htobe64
static uint64_t
htobe64(uint64_t value)
{
#if __BYTE_ORDER == __LITTLE_ENDIAN
uint32_t low = htonl(value);
uint32_t high = htonl(value >> 32);
return (((uint64_t)low) << 32) | high;
#elif __BYTE_ORDER == __BIG_ENDIAN
return value;
#else
#error "Unknown BYTE_ORDER"
#endif
}
#endif
static void
seek_eoln(FILE *secrets_file)
{
while (!feof(secrets_file) && '\n' != fgetc(secrets_file)) {
// Do nothing
}
}
static int
read_word(FILE *secrets_file, char word[MAXWORDLEN])
{
char ch = 0;
char *p = word;
char *q = word + MAXWORDLEN - 1;
char quote = 0;
while (!feof(secrets_file) && isspace((ch = fgetc(secrets_file)))) {
// Do nothing
}
while (!feof(secrets_file)) {
if (quote) {
if (ch == quote) {
quote = 0;
}
else {
*p++ = ch;
}
}
else if (isspace(ch) || '#' == ch) {
*p = *q = 0;
return ch;
}
else if ('\'' == ch || '"' == ch) {
quote = ch;
}
else if ('\\' == ch) {
*p = fgetc(secrets_file);
if ('\n' != *p) {
++p;
}
}
else {
*p++ = ch;
}
if (p > q) {
return -1;
}
ch = fgetc(secrets_file);
}
return -1;
}
static int
read_user_entry(FILE *secrets_file, user_entry_t *user_entry)
{
int rc;
retry:
if (feof(secrets_file)) {
return -1;
}
rc = read_word(secrets_file, user_entry->name);
if ('#' == rc || -1 == rc) {
seek_eoln(secrets_file);
goto retry;
}
if ('\n' == rc) {
goto retry;
}
rc = read_word(secrets_file, user_entry->server);
if ('#' == rc || -1 == rc) {
seek_eoln(secrets_file);
goto retry;
}
if ('\n' == rc) {
goto retry;
}
rc = read_word(secrets_file, user_entry->secret);
if ('#' == rc || -1 == rc) {
seek_eoln(secrets_file);
goto retry;
}
if ('\n' == rc) {
goto retry;
}
rc = read_word(secrets_file, user_entry->addr);
if (-1 == rc) {
seek_eoln(secrets_file);
goto retry;
}
if ('\n' != rc) {
seek_eoln(secrets_file);
}
return 0;
}
static int
split_secret(char *secret, otp_params_t *otp_params)
{
char *p = secret;
otp_params->method = p;
if (NULL == (p = strchr(p, ':'))) {
return -1;
}
*p++ = 0;
otp_params->hash = p;
if (NULL == (p = strchr(p, ':'))) {
return -1;
}
*p++ = 0;
otp_params->encoding = p;
if (NULL == (p = strchr(p, ':'))) {
return -1;
}
*p++ = 0;
otp_params->key = p;
if (NULL == (p = strchr(p, ':'))) {
return -1;
}
*p++ = 0;
otp_params->pin = p;
if (NULL != (p = strchr(p, ':'))) {
*p++ = 0;
}
otp_params->udid = p;
if (p && strchr(p, ':')) {
return -1;
}
return 0;
}
static int
hotp_read_counter(const void * otp_key)
{
/* Compute SHA1 for the otp_key */
unsigned char hash[SHA_DIGEST_LENGTH];
char hexdigest[SHA_DIGEST_LENGTH*2];
char line[256];
char path[256];
FILE *counter_file;
int i;
SHA1(otp_key, strlen(otp_key), hash);
for (i = 0; i < 20; i++) {
sprintf(&hexdigest[i*2], "%02x", hash[i]);
}
snprintf(path, sizeof(path), "%s%s", hotp_counters, hexdigest);
/* Find matching SHA1*/
DEBUG("OTP-AUTH: opening HOTP counter file '%s'\n", path);
counter_file = fopen(path, "r");
if (counter_file != NULL) {
if (fgets(line, sizeof(line), counter_file)) {
fclose(counter_file);
int ret = atoi(line);
DEBUG("OTP-AUTH: current HOTP value is %i\n", ret);
return atoi(line);
}
fclose(counter_file);
}
LOG("OTP-AUTH: failed to read HOTP counter file '%s'\n", path);
return -1;
}
static int
hotp_set_counter(const void * otp_key, int counter)
{
/* Compute SHA1 for the otp_key */
unsigned char hash[SHA_DIGEST_LENGTH];
char hexdigest[SHA_DIGEST_LENGTH*2];
char line[256];
char path[256];
FILE *counter_file;
int i;
SHA1(otp_key, strlen(otp_key), hash);
for (i = 0; i < 20; i++) {
sprintf(&hexdigest[i*2], "%02x", hash[i]);
}
snprintf(path, sizeof(path), "%s%s", hotp_counters, hexdigest);
/* Find matching SHA1*/
DEBUG("OTP-AUTH: opening HOTP counter file '%s' for writing\n", path);
counter_file = fopen(path, "w");
if (counter_file != NULL) {
DEBUG("OTP-AUTH: setting HOTP counter value to %i\n", counter);
if (fprintf(counter_file, "%d", counter)) {
fclose(counter_file);
DEBUG("OTP-AUTH: HOTP counter update successful\n", counter);
return 0;
}
fclose(counter_file);
}
LOG("OTP-AUTH: failed to write HOTP counter file '%s'\n", path);
return -1;
}
/**
* Verify user name and password
*/
static int otp_verify(const char *vpn_username, const char *vpn_secret)
{
FILE *secrets_file;
user_entry_t user_entry;
otp_params_t otp_params;
const EVP_MD *otp_digest;
char secret[256];
uint8_t decoded_secret[256];
int i;
int ok = 0;
secrets_file = fopen(otp_secrets, "r");
if (NULL == secrets_file) {
LOG("OTP-AUTH: failed to open %s\n", otp_secrets);
return ok;
}
DEBUG("OTP-AUTH: trying to authenticate username '%s'\n", vpn_username);
while (!feof(secrets_file)) {
if (read_user_entry(secrets_file, &user_entry)) {
continue;
}
if (strcmp(vpn_username, user_entry.name)) {
continue;
}
DEBUG("OTP-AUTH: username '%s' exists in '%s'\n", vpn_username, otp_secrets);
/* Handle non-otp passwords before trying to parse out otp fields */
if (!strncasecmp(user_entry.secret, "plain:", sizeof("plain:") - 1)) {
const char *password = user_entry.secret + sizeof("plain:") - 1;
if (!strcmp (vpn_username, user_entry.name)
&& password && !strcmp (vpn_secret, password)) {
ok = 1;
}
goto done;
}
if (split_secret(user_entry.secret, &otp_params)) {
goto done;
}
otp_digest = EVP_get_digestbyname(otp_params.hash);
if (!otp_digest) {
LOG("OTP-AUTH: unknown digest '%s'\n", otp_params.hash);
goto done;
}
unsigned int key_len;
const void * otp_key;
if (!strcasecmp(otp_params.encoding, "base32")) {
key_len = base32_decode((uint8_t *) otp_params.key, decoded_secret, sizeof(decoded_secret));
otp_key = decoded_secret;
} else
if (!strcasecmp(otp_params.encoding, "hex")) {
key_len = hex_decode(otp_params.key, decoded_secret, sizeof(decoded_secret));
otp_key = decoded_secret;
} else
if (!strcasecmp(otp_params.encoding, "text")) {
otp_key = otp_params.key;
key_len = strlen(otp_params.key);
} else {
LOG("OTP-AUTH: unknown encoding '%s'\n", otp_params.encoding);
goto done;
}
uint64_t T, Tn, Ti;
uint8_t mac[EVP_MAX_MD_SIZE];
unsigned maclen;
if (!strncasecmp("totp", otp_params.method, 4)) {
#if OPENSSL_VERSION_NUMBER >= 0x10100000L
HMAC_CTX* hmac = HMAC_CTX_new();
#else
HMAC_CTX hmac;
#endif
const uint8_t *otp_bytes;
uint32_t otp, divisor = 1;
int tstep = totp_step;
int tdigits = totp_digits;
if (!strcasecmp("totp-60-6", otp_params.method)) {
tstep = 60;
tdigits = 6;
}
int range = otp_slop / tstep;
T = (time(NULL) - totp_t0) / tstep;
for (i = 0; i < tdigits; ++i) {
divisor *= 10;
}
for (i = -range; !ok && i <= range; ++i) {
Tn = htobe64(T + i);
#if OPENSSL_VERSION_NUMBER >= 0x10100000L
HMAC_CTX_reset(hmac);
HMAC_Init_ex(hmac, otp_key, key_len, otp_digest, NULL);
HMAC_Update(hmac, (uint8_t *)&Tn, sizeof(Tn));
HMAC_Final(hmac, mac, &maclen);
#else
HMAC_CTX_init(&hmac);
HMAC_Init(&hmac, otp_key, key_len, otp_digest);
HMAC_Update(&hmac, (uint8_t *)&Tn, sizeof(Tn));
HMAC_Final(&hmac, mac, &maclen);
#endif
otp_bytes = mac + (mac[maclen - 1] & 0x0f);
otp = ((otp_bytes[0] & 0x7f) << 24) | (otp_bytes[1] << 16) |
(otp_bytes[2] << 8) | otp_bytes[3];
otp %= divisor;
snprintf(secret, sizeof(secret), "%s%0*u", otp_params.pin, tdigits, otp);
DEBUG("OTP-AUTH: trying method='%s', client_username='%s', client_secret='%s', server_username='%s', server_secret='%s'\n", otp_params.method, vpn_username, vpn_secret, user_entry.name, secret);
if (vpn_username && !strcmp (vpn_username, user_entry.name)
&& vpn_secret && !strcmp (vpn_secret, secret)) {
ok = 1;
DEBUG("OTP-AUTH: auth ok for method='%s', client_username='%s', client_secret='%s'\n", otp_params.method, vpn_username, vpn_secret);
}
}
#if OPENSSL_VERSION_NUMBER >= 0x10100000L
HMAC_CTX_free(hmac);
#endif
}
else if (!strncasecmp("hotp", otp_params.method, 4)) {
#if OPENSSL_VERSION_NUMBER >= 0x10100000L
HMAC_CTX* hmac = HMAC_CTX_new();
#else
HMAC_CTX hmac;
#endif
const uint8_t *otp_bytes;
uint32_t otp, divisor = 1;
int tdigits = totp_digits;
int i = 0;
i = hotp_read_counter(otp_params.key);
if (i >= 0) {
T = i;
for (i = 0; i < tdigits; ++i) {
divisor *= 10;
}
for (i = 0; !ok && i <= hotp_syncwindow; i++) {
Ti = T+i;
Tn = htobe64(Ti);
#if OPENSSL_VERSION_NUMBER >= 0x10100000L
HMAC_CTX_reset(hmac);
HMAC_Init_ex(hmac, otp_key, key_len, otp_digest, NULL);
HMAC_Update(hmac, (uint8_t *)&Tn, sizeof(Tn));
HMAC_Final(hmac, mac, &maclen);
#else
HMAC_CTX_init(&hmac);
HMAC_Init(&hmac, otp_key, key_len, otp_digest);
HMAC_Update(&hmac, (uint8_t *)&Tn, sizeof(Tn));
HMAC_Final(&hmac, mac, &maclen);
#endif
otp_bytes = mac + (mac[maclen - 1] & 0x0f);
otp = ((otp_bytes[0] & 0x7f) << 24) | (otp_bytes[1] << 16) |
(otp_bytes[2] << 8) | otp_bytes[3];
otp %= divisor;
snprintf(secret, sizeof(secret), "%s%0*u", otp_params.pin, tdigits, otp);
DEBUG("OTP-AUTH: trying method='%s', client_username='%s', client_secret='%s', server_username='%s', server_secret='%s', hotp=%"PRIu64"\n", otp_params.method, vpn_username, vpn_secret, user_entry.name, secret, Ti);
if (vpn_username && !strcmp (vpn_username, user_entry.name)
&& vpn_secret && !strcmp (vpn_secret, secret)) {
ok = 1;
DEBUG("OTP-AUTH: auth ok for method='%s', client_username='%s', client_secret='%s', hotp=%"PRIu64"\n", otp_params.method, vpn_username, vpn_secret, Ti);
hotp_set_counter(otp_params.key, Ti+1);
}
}
#if OPENSSL_VERSION_NUMBER >= 0x10100000L
HMAC_CTX_free(hmac);
#endif
}
}
else if (!strcasecmp("motp", otp_params.method)) {
#if OPENSSL_VERSION_NUMBER >= 0x10100000L
EVP_MD_CTX* ctx = EVP_MD_CTX_new();
#else
EVP_MD_CTX ctx;
#endif
char buf[64];
int n;
int range = otp_slop / motp_step;
T = time(NULL) / motp_step;
for (i = -range; !ok && i <= range; ++i) {
#if OPENSSL_VERSION_NUMBER >= 0x10100000L
EVP_MD_CTX_reset(ctx);
EVP_DigestInit_ex(ctx, otp_digest, NULL);
n = sprintf(buf, "%" PRIu64, T + i);
EVP_DigestUpdate(ctx, buf, n);
EVP_DigestUpdate(ctx, otp_key, key_len);
EVP_DigestUpdate(ctx, otp_params.pin, strlen(otp_params.pin));
if (otp_params.udid) {
int udid_len = strlen(otp_params.udid);
EVP_DigestUpdate(ctx, otp_params.udid, udid_len);
}
EVP_DigestFinal_ex(ctx, mac, &maclen);
#else
EVP_MD_CTX_init(&ctx);
EVP_DigestInit_ex(&ctx, otp_digest, NULL);
n = sprintf(buf, "%" PRIu64, T + i);
EVP_DigestUpdate(&ctx, buf, n);
EVP_DigestUpdate(&ctx, otp_key, key_len);
EVP_DigestUpdate(&ctx, otp_params.pin, strlen(otp_params.pin));
if (otp_params.udid) {
int udid_len = strlen(otp_params.udid);
EVP_DigestUpdate(&ctx, otp_params.udid, udid_len);
}
EVP_DigestFinal_ex(&ctx, mac, &maclen);
EVP_MD_CTX_cleanup(&ctx);
#endif
snprintf(secret, sizeof(secret),
"%02x%02x%02x", mac[0], mac[1], mac[2]);
DEBUG("OTP-AUTH: trying method='%s', client_username='%s', client_secret='%s', server_username='%s', server_secret='%s'\n", otp_params.method, vpn_username, vpn_secret, user_entry.name, secret);
if (vpn_username && !strcmp (vpn_username, user_entry.name)
&& vpn_secret && !strcmp (vpn_secret, secret)) {
ok = 1;
DEBUG("OTP-AUTH: auth ok for method='%s', client_username='%s', client_secret='%s'\n", otp_params.method, vpn_username, vpn_secret);
}
}
#if OPENSSL_VERSION_NUMBER >= 0x10100000L
EVP_MD_CTX_free(ctx);
#endif
}
else {
LOG("OTP-AUTH: unknown OTP method %s\n", otp_params.method);
}
done:
memset(secret, 0, sizeof(secret));
}
if (NULL != secrets_file) {
fclose(secrets_file);
}
return ok;
}
/*
* Given an environmental variable name, search
* the envp array for its value, returning it
* if found or NULL otherwise.
*/
static const char * get_env (const char *name, const char *envp[])
{
if (envp)
{
int i;
const int namelen = strlen (name);
for (i = 0; envp[i]; ++i)
{
if (!strncmp (envp[i], name, namelen))
{
const char *cp = envp[i] + namelen;
if (*cp == '=')
return cp + 1;
}
}
}
return NULL;
}
/**
* Plugin open (init)
*/
OPENVPN_EXPORT openvpn_plugin_handle_t
openvpn_plugin_open_v1 (unsigned int *type_mask, const char *argv[], const char *envp[])
{
OpenSSL_add_all_digests();
/*
* We are only interested in intercepting the
* --auth-user-pass-verify callback.
*/
*type_mask = OPENVPN_PLUGIN_MASK (OPENVPN_PLUGIN_AUTH_USER_PASS_VERIFY);
/*
* Set up configuration variables
*
*/
const char * cfg_otp_secrets = get_env("otp_secrets", argv);
if (cfg_otp_secrets != NULL) {
otp_secrets = strdup(cfg_otp_secrets);
}
LOG("OTP-AUTH: otp_secrets=%s\n", otp_secrets);
const char * cfg_hotp_counter_file = get_env("hotp_counters", argv);
if (cfg_hotp_counter_file != NULL) {
hotp_counters = strdup(cfg_hotp_counter_file);
}
LOG("OTP-AUTH: hotp_counters=%s\n", hotp_counters);
const char * cfg_otp_slop = get_env("otp_slop", argv);
if (cfg_otp_slop != NULL) {
otp_slop = atoi(cfg_otp_slop);
}
LOG("OTP-AUTH: otp_slop=%i\n", otp_slop);
const char * cfg_totp_t0 = get_env("totp_t0", argv);
if (cfg_totp_t0 != NULL) {
totp_t0 = atoi(cfg_totp_t0);
}
LOG("OTP-AUTH: totp_t0=%i\n", totp_t0);
const char * cfg_totp_step= get_env("totp_step", argv);
if (cfg_totp_step != NULL) {
totp_step = atoi(cfg_totp_step);
}
LOG("OTP-AUTH: totp_step=%i\n", totp_step);
const char * cfg_totp_digits = get_env("totp_digits", argv);
if (cfg_totp_digits != NULL) {
totp_digits = atoi(cfg_totp_digits);
}
LOG("OTP-AUTH: totp_digits=%i\n", totp_digits);
const char * cfg_motp_step = get_env("motp_step", argv);
if (cfg_motp_step != NULL) {
motp_step = atoi(cfg_motp_step);
}
LOG("OTP-AUTH: motp_step=%i\n", motp_step);
const char * cfg_hotp_syncwindow = get_env("hotp_syncwindow", argv);
if (cfg_hotp_syncwindow != NULL) {
hotp_syncwindow = atoi(cfg_hotp_syncwindow);
}
LOG("OTP-AUTH: hotp_syncwindow=%i\n", hotp_syncwindow);
const char * cfg_password_cr = get_env("password_is_cr", argv);
if (cfg_password_cr != NULL) {
password_is_cr = atoi(cfg_password_cr);
}
LOG("OTP-AUTH: password_is_cr=%i\n", password_is_cr);
const char * cfg_debug = get_env("debug", argv);
if (cfg_debug != NULL) {
debug = atoi(cfg_debug);
}
LOG("OTP-AUTH: debug=%i\n", debug);
DEBUG("OTP_AUTH: debug mode has been enabled\n");
return (openvpn_plugin_handle_t) otp_secrets;
}
/**
* Check credentials
*/
OPENVPN_EXPORT int
openvpn_plugin_func_v1 (openvpn_plugin_handle_t handle, const int type, const char *argv[], const char *envp[])
{
/* get username/password from envp string array */
const char *username = get_env ("username", envp);
const char *password = get_env ("password", envp);
const char *ip = get_env ("untrusted_ip", envp);
const char *ip6 = get_env ("untrusted_ip6", envp);
const char *port = get_env ("untrusted_port", envp);
if (username == NULL) {
LOG("OTP_AUTH: Username is missing\n");
return OPENVPN_PLUGIN_FUNC_ERROR;
}
if (password == NULL) {
LOG("OTP_AUTH: Password is missing\n");
return OPENVPN_PLUGIN_FUNC_ERROR;
}
if ((ip == NULL && ip6 == NULL) || port == NULL) {
LOG("OTP_AUTH: IP or Port number is missing\n");
return OPENVPN_PLUGIN_FUNC_ERROR;
}
if (ip == NULL) {
ip = ip6;
}
const int ulen = strlen(username);
const int pwlen = strlen(password);
if ( ulen > MAXWORDLEN || ulen == 0 || pwlen > MAXWORDLEN || pwlen == 0) {
LOG("OTP_AUTH: Username or password is too long or empty\n");
return OPENVPN_PLUGIN_FUNC_ERROR;
}
const char *otp_password;
openvpn_response resp;
if (password_is_cr) {
char *parse_error;
if (!extract_openvpn_cr(password, &resp, &parse_error)) {
LOG("OTP-AUTH: Error extracting challenge/response. Parse error = '%s'\n", parse_error);
return OPENVPN_PLUGIN_FUNC_ERROR;
}
/*Take the response part, 'password' is for other authenticators*/
otp_password = (const char *)resp.response;
}
else {
otp_password = password;
}
if (otp_password == NULL) {
LOG("OTP_AUTH: OTP Password is missing\n");
return OPENVPN_PLUGIN_FUNC_ERROR;
}
/* check entered username/password against what we require */
int ok = otp_verify(username, otp_password);
if (ok == 1) {
LOG("OTP-AUTH: authentication succeeded for username '%s', remote %s:%s\n", username, ip, port);
return OPENVPN_PLUGIN_FUNC_SUCCESS;
}
else {
LOG("OTP-AUTH: authentication failed for username '%s', remote %s:%s\n", username, ip, port);
return OPENVPN_PLUGIN_FUNC_ERROR;
}
}
/**
* Plugin close
*/
OPENVPN_EXPORT void
openvpn_plugin_close_v1 (openvpn_plugin_handle_t handle)
{
}