-
Notifications
You must be signed in to change notification settings - Fork 26
/
server.c
1250 lines (1052 loc) · 31.2 KB
/
server.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
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Sigma Control API DUT (server)
* Copyright (c) 2014, Qualcomm Atheros, Inc.
* Copyright (c) 2018-2021, The Linux Foundation
* All Rights Reserved.
* Licensed under the Clear BSD license. See README for more details.
*/
#include "sigma_dut.h"
#include <sqlite3.h>
#ifndef ROOT_DIR
#define ROOT_DIR "/home/user/hs20-server"
#endif /* ROOT_DIR */
#ifndef SERVER_DB
#define SERVER_DB ROOT_DIR "/AS/DB/eap_user.db"
#endif /* SERVER_DB */
#ifndef CERT_DIR
#define CERT_DIR ROOT_DIR "/certs"
#endif /* CERT_DIR */
static enum sigma_cmd_result cmd_server_ca_get_version(struct sigma_dut *dut,
struct sigma_conn *conn,
struct sigma_cmd *cmd)
{
send_resp(dut, conn, SIGMA_COMPLETE, "version," SIGMA_DUT_VER);
return STATUS_SENT;
}
static enum sigma_cmd_result cmd_server_get_info(struct sigma_dut *dut,
struct sigma_conn *conn,
struct sigma_cmd *cmd)
{
char ver[128], resp[256];
get_ver(ROOT_DIR "/spp/hs20_spp_server -v", ver, sizeof(ver));
snprintf(resp, sizeof(resp), "vendor,OSU,model,OS,version,%s", ver);
send_resp(dut, conn, SIGMA_COMPLETE, resp);
return STATUS_SENT;
}
static int server_reset_user(struct sigma_dut *dut, const char *user)
{
sqlite3 *db;
int res = -1;
char *sql = NULL;
const char *realm = "wi-fi.org";
const char *methods = "TTLS-MSCHAPV2";
const char *password = "ChangeMe";
int phase2 = 1;
int machine_managed = 1;
const char *remediation = "";
int fetch_pps = 0;
const char *osu_user = NULL;
const char *osu_password = NULL;
const char *policy = NULL;
sigma_dut_print(dut, DUT_MSG_DEBUG, "Reset user %s", user);
if (sqlite3_open(SERVER_DB, &db)) {
sigma_dut_print(dut, DUT_MSG_ERROR,
"Failed to open SQLite database %s",
SERVER_DB);
return -1;
}
if (strcmp(user, "test01") == 0) {
remediation = "machine";
} else if (strcmp(user, "test02") == 0) {
remediation = "user";
machine_managed = 0;
} else if (strcmp(user, "test03") == 0) {
/* UpdateInterval-based client trigger for policy update */
policy = "ruckus130";
} else if (strcmp(user, "test04") == 0) {
} else if (strcmp(user, "test05") == 0) {
} else if (strcmp(user, "test06") == 0) {
realm = "example.com";
} else if (strcmp(user, "test07") == 0) {
} else if (strcmp(user, "test08") == 0) {
remediation = "machine";
osu_user = "testdmacc08";
osu_password = "P@ssw0rd";
} else if (strcmp(user, "test09") == 0) {
/* UpdateInterval-based client trigger for policy update */
policy = "ruckus130";
osu_user = "testdmacc09";
osu_password = "P@ssw0rd";
} else if (strcmp(user, "test10") == 0) {
remediation = "machine";
methods = "TLS";
} else if (strcmp(user, "test11") == 0) {
} else if (strcmp(user, "test12") == 0) {
remediation = "user";
methods = "TLS";
} else if (strcmp(user, "test20") == 0) {
} else if (strcmp(user, "test26") == 0) {
/* TODO: Cred01 with username/password? */
user = "1310026000000001";
methods = "SIM";
} else if (strcmp(user, "test30") == 0) {
osu_user = "testdmacc30";
osu_password = "P@ssw0rd";
} else if (strcmp(user, "test31") == 0) {
osu_user = "testdmacc31";
osu_password = "P@ssw0rd";
} else if (strcmp(user, "test32") == 0) {
osu_user = "testdmacc32";
osu_password = "P@ssw0rd";
} else if (strcmp(user, "test33") == 0) {
osu_user = "testdmacc33";
osu_password = "P@ssw0rd";
} else if (strcmp(user, "test34") == 0) {
osu_user = "testdmacc34";
osu_password = "P@ssw0rd";
} else if (strcmp(user, "test35") == 0) {
osu_user = "testdmacc35";
osu_password = "P@ssw0rd";
} else if (strcmp(user, "test36") == 0) {
} else if (strcmp(user, "test37") == 0) {
osu_user = "testdmacc37";
osu_password = "P@ssw0rd";
} else if (strcmp(user, "testdmacc08") == 0 ||
strcmp(user, "testdmacc09") == 0) {
/* No need to set anything separate for testdmacc* users */
sqlite3_close(db);
return 0;
} else {
sigma_dut_print(dut, DUT_MSG_INFO, "Unsupported username '%s'",
user);
goto fail;
}
sql = sqlite3_mprintf("INSERT OR REPLACE INTO users(identity,realm,methods,password,phase2,machine_managed,remediation,fetch_pps,osu_user,osu_password,policy) VALUES (%Q,%Q,%Q,%Q,%d,%d,%Q,%d,%Q,%Q,%Q)",
user, realm, methods, password,
phase2, machine_managed, remediation, fetch_pps,
osu_user, osu_password, policy);
if (!sql)
goto fail;
sigma_dut_print(dut, DUT_MSG_DEBUG, "SQL: %s", sql);
if (sqlite3_exec(db, sql, NULL, NULL, NULL) != SQLITE_OK) {
sigma_dut_print(dut, DUT_MSG_ERROR, "SQL operation failed: %s",
sqlite3_errmsg(db));
} else {
res = 0;
}
sqlite3_free(sql);
fail:
sqlite3_close(db);
return res;
}
static int server_reset_serial(struct sigma_dut *dut, const char *serial)
{
sqlite3 *db;
int res = -1;
char *sql = NULL;
const char *realm = "wi-fi.org";
const char *methods = "TLS";
int phase2 = 0;
int machine_managed = 1;
const char *remediation = "";
int fetch_pps = 0;
const char *osu_user = NULL;
const char *osu_password = NULL;
const char *policy = NULL;
char user[128];
const char *cert = "";
const char *subrem = "";
snprintf(user, sizeof(user), "cert-%s", serial);
sigma_dut_print(dut, DUT_MSG_DEBUG, "Reset user %s (serial number: %s)",
user, serial);
if (sqlite3_open(SERVER_DB, &db)) {
sigma_dut_print(dut, DUT_MSG_ERROR,
"Failed to open SQLite database %s",
SERVER_DB);
return -1;
}
if (strcmp(serial, "1046") == 0) {
remediation = "machine";
cert = "3786eb9ef44778fe8048f9fa6f8c3e611f2dbdd15f239fa93edcc417debefa5a";
subrem = "homeoi";
} else if (strcmp(serial, "1047") == 0) {
remediation = "user";
cert = "55cd0af162f2fb6de5b9481e37a0b0887f42e477ab09586b0c10f24b269b893f";
} else {
sigma_dut_print(dut, DUT_MSG_INFO,
"Unsupported serial number '%s'", serial);
goto fail;
}
sql = sqlite3_mprintf("INSERT OR REPLACE INTO users(identity,realm,methods,phase2,machine_managed,remediation,fetch_pps,osu_user,osu_password,policy,cert,subrem) VALUES (%Q,%Q,%Q,%d,%d,%Q,%d,%Q,%Q,%Q,%Q,%Q)",
user, realm, methods,
phase2, machine_managed, remediation, fetch_pps,
osu_user, osu_password, policy, cert, subrem);
if (!sql)
goto fail;
sigma_dut_print(dut, DUT_MSG_DEBUG, "SQL: %s", sql);
if (sqlite3_exec(db, sql, NULL, NULL, NULL) != SQLITE_OK) {
sigma_dut_print(dut, DUT_MSG_ERROR, "SQL operation failed: %s",
sqlite3_errmsg(db));
} else {
res = 0;
}
sqlite3_free(sql);
fail:
sqlite3_close(db);
return res;
}
static int server_reset_cert_enroll(struct sigma_dut *dut, const char *addr)
{
sqlite3 *db;
char *sql;
sigma_dut_print(dut, DUT_MSG_DEBUG,
"Reset certificate enrollment status for %s", addr);
if (sqlite3_open(SERVER_DB, &db)) {
sigma_dut_print(dut, DUT_MSG_ERROR,
"Failed to open SQLite database %s",
SERVER_DB);
return -1;
}
if (strcasecmp(addr, "any") == 0)
sql = sqlite3_mprintf("DELETE FROM cert_enroll");
else
sql = sqlite3_mprintf("DELETE FROM cert_enroll WHERE mac_addr=%Q",
addr);
if (!sql) {
sqlite3_close(db);
return -1;
}
sigma_dut_print(dut, DUT_MSG_DEBUG, "SQL: %s", sql);
if (sqlite3_exec(db, sql, NULL, NULL, NULL) != SQLITE_OK) {
sigma_dut_print(dut, DUT_MSG_ERROR,
"SQL operation failed: %s",
sqlite3_errmsg(db));
sqlite3_free(sql);
sqlite3_close(db);
return -1;
}
sqlite3_free(sql);
sqlite3_close(db);
return 0;
}
static int server_reset_imsi(struct sigma_dut *dut, const char *imsi)
{
sqlite3 *db;
char *sql;
sigma_dut_print(dut, DUT_MSG_DEBUG, "Reset policy provisioning for %s",
imsi);
if (sqlite3_open(SERVER_DB, &db)) {
sigma_dut_print(dut, DUT_MSG_ERROR,
"Failed to open SQLite database %s",
SERVER_DB);
return -1;
}
sql = sqlite3_mprintf("DELETE FROM users WHERE identity=%Q", imsi);
if (!sql) {
sqlite3_close(db);
return -1;
}
sigma_dut_print(dut, DUT_MSG_DEBUG, "SQL: %s", sql);
if (sqlite3_exec(db, sql, NULL, NULL, NULL) != SQLITE_OK) {
sigma_dut_print(dut, DUT_MSG_ERROR,
"SQL operation failed: %s",
sqlite3_errmsg(db));
sqlite3_free(sql);
sqlite3_close(db);
return -1;
}
sqlite3_free(sql);
sqlite3_close(db);
return 0;
}
static enum sigma_cmd_result cmd_server_reset_default(struct sigma_dut *dut,
struct sigma_conn *conn,
struct sigma_cmd *cmd)
{
const char *var;
enum sigma_program prog;
var = get_param(cmd, "Program");
if (!var) {
send_resp(dut, conn, SIGMA_ERROR,
"errorCode,Missing program parameter");
return STATUS_SENT;
}
prog = sigma_program_to_enum(var);
if (!is_passpoint_r2_or_newer(prog)) {
send_resp(dut, conn, SIGMA_ERROR,
"errorCode,Unsupported program");
return STATUS_SENT;
}
var = get_param(cmd, "UserName");
if (var && server_reset_user(dut, var) < 0) {
send_resp(dut, conn, SIGMA_ERROR,
"errorCode,Failed to reset user account to defaults");
return STATUS_SENT;
}
var = get_param(cmd, "SerialNo");
if (var && server_reset_serial(dut, var)) {
send_resp(dut, conn, SIGMA_ERROR,
"errorCode,Failed to reset user account to defaults");
return STATUS_SENT;
}
var = get_param(cmd, "ClientMACAddr");
if (var && server_reset_cert_enroll(dut, var) < 0) {
send_resp(dut, conn, SIGMA_ERROR,
"errorCode,Failed to reset cert enroll to defaults");
return STATUS_SENT;
}
var = get_param(cmd, "imsi_val");
if (var && server_reset_imsi(dut, var) < 0) {
send_resp(dut, conn, SIGMA_ERROR,
"errorCode,Failed to reset IMSI/SIM user");
return STATUS_SENT;
}
return SUCCESS_SEND_STATUS;
}
static int get_last_msk_cb(void *ctx, int argc, char *argv[], char *col[])
{
char **last_msk = ctx;
if (argc < 1 || !argv[0])
return 0;
free(*last_msk);
*last_msk = strdup(argv[0]);
return 0;
}
static char * get_last_msk(struct sigma_dut *dut, sqlite3 *db,
const char *username)
{
char *sql, *last_msk = NULL;
sql = sqlite3_mprintf("SELECT last_msk FROM users WHERE identity=%Q",
username);
if (!sql)
return NULL;
if (sqlite3_exec(db, sql, get_last_msk_cb, &last_msk, NULL) !=
SQLITE_OK) {
sigma_dut_print(dut, DUT_MSG_ERROR,
"SQL operation to fetch last_msk failed: %s",
sqlite3_errmsg(db));
sqlite3_free(sql);
return NULL;
}
sqlite3_free(sql);
return last_msk;
}
static enum sigma_cmd_result
aaa_auth_status(struct sigma_dut *dut, struct sigma_conn *conn,
struct sigma_cmd *cmd, const char *username, int timeout)
{
sqlite3 *db;
char *sql = NULL;
int i;
char resp[500];
if (sqlite3_open(SERVER_DB, &db)) {
sigma_dut_print(dut, DUT_MSG_ERROR,
"Failed to open SQLite database %s",
SERVER_DB);
return INVALID_SEND_STATUS;
}
sql = sqlite3_mprintf("UPDATE users SET last_msk=NULL WHERE identity=%Q",
username);
if (!sql) {
sqlite3_close(db);
return ERROR_SEND_STATUS;
}
if (sqlite3_exec(db, sql, NULL, NULL, NULL) != SQLITE_OK) {
sigma_dut_print(dut, DUT_MSG_ERROR,
"SQL operation to clear last_msk failed: %s",
sqlite3_errmsg(db));
sqlite3_free(sql);
sqlite3_close(db);
return ERROR_SEND_STATUS;
}
sqlite3_free(sql);
if (sqlite3_changes(db) < 1) {
sigma_dut_print(dut, DUT_MSG_ERROR,
"No DB rows modified (specified user not found)");
sqlite3_close(db);
return ERROR_SEND_STATUS;
}
snprintf(resp, sizeof(resp), "AuthStatus,TIMEOUT,MSK,NULL");
for (i = 0; i < timeout; i++) {
char *last_msk;
last_msk = get_last_msk(dut, db, username);
if (last_msk) {
if (strcmp(last_msk, "FAIL") == 0) {
snprintf(resp, sizeof(resp),
"AuthStatus,FAIL,MSK,NULL");
} else {
snprintf(resp, sizeof(resp),
"AuthStatus,SUCCESS,MSK,%s", last_msk);
}
free(last_msk);
break;
}
sleep(1);
}
sqlite3_close(db);
send_resp(dut, conn, SIGMA_COMPLETE, resp);
return STATUS_SENT;
}
static int get_last_serial_cb(void *ctx, int argc, char *argv[], char *col[])
{
char **last_serial = ctx;
if (argc < 1 || !argv[0])
return 0;
free(*last_serial);
*last_serial = strdup(argv[0]);
return 0;
}
static char * get_last_serial(struct sigma_dut *dut, sqlite3 *db,
const char *addr)
{
char *sql, *last_serial = NULL;
if (!addr || strcasecmp(addr, "any") == 0)
sql = sqlite3_mprintf("SELECT serialnum FROM cert_enroll");
else
sql = sqlite3_mprintf("SELECT serialnum FROM cert_enroll WHERE mac_addr=%Q",
addr);
if (!sql)
return NULL;
sigma_dut_print(dut, DUT_MSG_DEBUG, "SQL: %s", sql);
if (sqlite3_exec(db, sql, get_last_serial_cb, &last_serial, NULL) !=
SQLITE_OK) {
sigma_dut_print(dut, DUT_MSG_ERROR,
"SQL operation to fetch last_serial failed: %s",
sqlite3_errmsg(db));
sqlite3_free(sql);
return NULL;
}
sqlite3_free(sql);
return last_serial;
}
static enum sigma_cmd_result
osu_cert_enroll_status(struct sigma_dut *dut, struct sigma_conn *conn,
struct sigma_cmd *cmd, const char *addr, int timeout)
{
sqlite3 *db;
int i;
char resp[500];
if (sqlite3_open(SERVER_DB, &db)) {
sigma_dut_print(dut, DUT_MSG_ERROR,
"Failed to open SQLite database %s",
SERVER_DB);
return INVALID_SEND_STATUS;
}
snprintf(resp, sizeof(resp), "OSUStatus,TIMEOUT");
for (i = 0; i < timeout; i++) {
char *last_serial;
last_serial = get_last_serial(dut, db, addr);
if (last_serial) {
if (strcmp(last_serial, "FAIL") == 0) {
snprintf(resp, sizeof(resp),
"OSUStatus,FAIL");
} else if (strlen(last_serial) > 0) {
snprintf(resp, sizeof(resp),
"OSUStatus,SUCCESS,SerialNo,%s",
last_serial);
}
free(last_serial);
break;
}
sleep(1);
}
sqlite3_close(db);
send_resp(dut, conn, SIGMA_COMPLETE, resp);
return STATUS_SENT;
}
static int get_user_field_cb(void *ctx, int argc, char *argv[], char *col[])
{
char **val = ctx;
if (argc < 1 || !argv[0])
return 0;
free(*val);
*val = strdup(argv[0]);
return 0;
}
static char * get_user_field_helper(struct sigma_dut *dut, sqlite3 *db,
const char *id_field,
const char *identity, const char *field)
{
char *sql, *val = NULL;
sql = sqlite3_mprintf("SELECT %s FROM users WHERE %s=%Q",
field, id_field, identity);
if (!sql)
return NULL;
sigma_dut_print(dut, DUT_MSG_DEBUG, "SQL: %s", sql);
if (sqlite3_exec(db, sql, get_user_field_cb, &val, NULL) != SQLITE_OK) {
sigma_dut_print(dut, DUT_MSG_ERROR,
"SQL operation to fetch user field failed: %s",
sqlite3_errmsg(db));
sqlite3_free(sql);
return NULL;
}
sqlite3_free(sql);
return val;
}
static char * get_user_field(struct sigma_dut *dut, sqlite3 *db,
const char *identity, const char *field)
{
return get_user_field_helper(dut, db, "identity", identity, field);
}
static char * get_user_dmacc_field(struct sigma_dut *dut, sqlite3 *db,
const char *identity, const char *field)
{
return get_user_field_helper(dut, db, "osu_user", identity, field);
}
static int get_eventlog_new_serialno_cb(void *ctx, int argc, char *argv[],
char *col[])
{
char **serialno = ctx;
char *val;
if (argc < 1 || !argv[0])
return 0;
val = argv[0];
if (strncmp(val, "renamed user to: cert-", 22) != 0)
return 0;
val += 22;
free(*serialno);
*serialno = strdup(val);
return 0;
}
static char * get_eventlog_new_serialno(struct sigma_dut *dut, sqlite3 *db,
const char *username)
{
char *sql, *serial = NULL;
sql = sqlite3_mprintf("SELECT notes FROM eventlog WHERE user=%Q AND notes LIKE %Q",
username, "renamed user to:%");
if (!sql)
return NULL;
if (sqlite3_exec(db, sql, get_eventlog_new_serialno_cb, &serial,
NULL) != SQLITE_OK) {
sigma_dut_print(dut, DUT_MSG_ERROR,
"SQL operation to fetch new serialno failed: %s",
sqlite3_errmsg(db));
sqlite3_free(sql);
return NULL;
}
sqlite3_free(sql);
return serial;
}
static enum sigma_cmd_result
osu_remediation_status(struct sigma_dut *dut, struct sigma_conn *conn,
int timeout, const char *username, const char *serialno)
{
sqlite3 *db;
int i;
char resp[500];
char name[100];
char *remediation = NULL;
int dmacc = 0;
if (!username && !serialno)
return INVALID_SEND_STATUS;
if (!username) {
snprintf(name, sizeof(name), "cert-%s", serialno);
username = name;
}
if (sqlite3_open(SERVER_DB, &db)) {
sigma_dut_print(dut, DUT_MSG_ERROR,
"Failed to open SQLite database %s",
SERVER_DB);
return ERROR_SEND_STATUS;
}
remediation = get_user_field(dut, db, username, "remediation");
if (!remediation) {
remediation = get_user_dmacc_field(dut, db, username,
"remediation");
dmacc = 1;
}
if (!remediation) {
snprintf(resp, sizeof(resp),
"RemediationStatus,User entry not found");
goto done;
}
if (remediation[0] == '\0') {
snprintf(resp, sizeof(resp),
"RemediationStatus,User was not configured to need remediation");
goto done;
}
snprintf(resp, sizeof(resp), "RemediationStatus,TIMEOUT");
for (i = 0; i < timeout; i++) {
sleep(1);
free(remediation);
if (dmacc)
remediation = get_user_dmacc_field(dut, db, username,
"remediation");
else
remediation = get_user_field(dut, db, username,
"remediation");
if (!remediation && serialno) {
char *new_serial;
/* Certificate reenrollment through subscription
* remediation - fetch the new serial number */
new_serial = get_eventlog_new_serialno(dut, db,
username);
if (!new_serial) {
/* New SerialNo not known?! */
snprintf(resp, sizeof(resp),
"RemediationStatus,Remediation Complete,SerialNo,Unknown");
break;
}
snprintf(resp, sizeof(resp),
"RemediationStatus,Remediation Complete,SerialNo,%s",
new_serial);
free(new_serial);
break;
} else if (remediation && remediation[0] == '\0') {
snprintf(resp, sizeof(resp),
"RemediationStatus,Remediation Complete");
break;
}
}
done:
free(remediation);
sqlite3_close(db);
send_resp(dut, conn, SIGMA_COMPLETE, resp);
return STATUS_SENT;
}
static enum sigma_cmd_result
osu_polupd_status(struct sigma_dut *dut, struct sigma_conn *conn, int timeout,
const char *username, const char *serialno)
{
sqlite3 *db;
char *sql;
int i;
char resp[500];
char name[100];
char *policy = NULL;
int dmacc = 0;
if (!username && !serialno)
return INVALID_SEND_STATUS;
if (!username) {
snprintf(name, sizeof(name), "cert-%s", serialno);
username = name;
}
if (sqlite3_open(SERVER_DB, &db)) {
sigma_dut_print(dut, DUT_MSG_ERROR,
"Failed to open SQLite database %s",
SERVER_DB);
return ERROR_SEND_STATUS;
}
policy = get_user_field(dut, db, username, "policy");
if (!policy) {
policy = get_user_dmacc_field(dut, db, username, "policy");
dmacc = 1;
}
if (!policy) {
snprintf(resp, sizeof(resp),
"PolicyUpdateStatus,User entry not found");
goto done;
}
if (policy[0] == '\0') {
snprintf(resp, sizeof(resp),
"PolicyUpdateStatus,User was not configured to need policy update");
goto done;
}
sql = sqlite3_mprintf("UPDATE users SET polupd_done=0 WHERE %s=%Q",
(dmacc ? "osu_user" : "identity"),
username);
if (!sql) {
snprintf(resp, sizeof(resp),
"PolicyUpdateStatus,Internal error");
goto done;
}
sigma_dut_print(dut, DUT_MSG_DEBUG, "SQL: %s", sql);
if (sqlite3_exec(db, sql, NULL, NULL, NULL) != SQLITE_OK) {
sigma_dut_print(dut, DUT_MSG_ERROR,
"SQL operation to fetch user field failed: %s",
sqlite3_errmsg(db));
sqlite3_free(sql);
goto done;
}
sqlite3_free(sql);
snprintf(resp, sizeof(resp), "PolicyUpdateStatus,TIMEOUT");
for (i = 0; i < timeout; i++) {
sleep(1);
free(policy);
if (dmacc)
policy = get_user_dmacc_field(dut, db, username,
"polupd_done");
else
policy = get_user_field(dut, db, username,
"polupd_done");
if (policy && atoi(policy)) {
snprintf(resp, sizeof(resp),
"PolicyUpdateStatus,UpdateComplete");
break;
}
}
done:
free(policy);
sqlite3_close(db);
send_resp(dut, conn, SIGMA_COMPLETE, resp);
return STATUS_SENT;
}
static enum sigma_cmd_result
osu_sim_policy_provisioning_status(struct sigma_dut *dut,
struct sigma_conn *conn,
const char *imsi, int timeout)
{
sqlite3 *db;
int i;
char resp[500];
char *id = NULL;
if (sqlite3_open(SERVER_DB, &db)) {
sigma_dut_print(dut, DUT_MSG_ERROR,
"Failed to open SQLite database %s",
SERVER_DB);
return INVALID_SEND_STATUS;
}
snprintf(resp, sizeof(resp), "PolicyProvisioning,TIMEOUT");
for (i = 0; i < timeout; i++) {
free(id);
id = get_user_field(dut, db, imsi, "identity");
if (id) {
snprintf(resp, sizeof(resp),
"PolicyProvisioning,Provisioning Complete");
break;
}
sleep(1);
}
free(id);
sqlite3_close(db);
send_resp(dut, conn, SIGMA_COMPLETE, resp);
return STATUS_SENT;
}
static enum sigma_cmd_result cmd_server_request_status(struct sigma_dut *dut,
struct sigma_conn *conn,
struct sigma_cmd *cmd)
{
const char *var, *username, *serialno, *imsi, *addr, *status;
int osu, timeout;
char resp[500];
enum sigma_program prog;
var = get_param(cmd, "Program");
if (!var) {
send_resp(dut, conn, SIGMA_ERROR,
"errorCode,Missing program parameter");
return STATUS_SENT;
}
prog = sigma_program_to_enum(var);
if (!is_passpoint_r2_or_newer(prog)) {
send_resp(dut, conn, SIGMA_ERROR,
"errorCode,Unsupported program");
return STATUS_SENT;
}
var = get_param(cmd, "Device");
if (!var ||
(strcasecmp(var, "AAAServer") != 0 &&
strcasecmp(var, "OSUServer") != 0)) {
send_resp(dut, conn, SIGMA_ERROR,
"errorCode,Unsupported device type");
return STATUS_SENT;
}
osu = strcasecmp(var, "OSUServer") == 0;
var = get_param(cmd, "Timeout");
if (!var) {
send_resp(dut, conn, SIGMA_ERROR,
"errorCode,Missing timeout");
return STATUS_SENT;
}
timeout = atoi(var);
sigma_dut_print(dut, DUT_MSG_DEBUG, "timeout: %d", timeout);
username = get_param(cmd, "UserName");
if (username)
sigma_dut_print(dut, DUT_MSG_DEBUG, "UserName: %s", username);
serialno = get_param(cmd, "SerialNo");
if (serialno)
sigma_dut_print(dut, DUT_MSG_DEBUG, "SerialNo: %s", serialno);
imsi = get_param(cmd, "imsi_val");
if (imsi)
sigma_dut_print(dut, DUT_MSG_DEBUG, "imsi_val: %s", imsi);
addr = get_param(cmd, "ClientMACAddr");
if (addr)
sigma_dut_print(dut, DUT_MSG_DEBUG, "ClientMACAddr: %s", addr);
status = get_param(cmd, "Status");
if (status)
sigma_dut_print(dut, DUT_MSG_DEBUG, "Status: %s", status);
if (osu && status && strcasecmp(status, "Remediation") == 0)
return osu_remediation_status(dut, conn, timeout, username,
serialno);
if (osu && status && strcasecmp(status, "PolicyUpdate") == 0)
return osu_polupd_status(dut, conn, timeout, username,
serialno);
if (!osu && status && strcasecmp(status, "Authentication") == 0 &&
username)
return aaa_auth_status(dut, conn, cmd, username, timeout);
if (!osu && status && strcasecmp(status, "Authentication") == 0 &&
serialno) {
snprintf(resp, sizeof(resp), "cert-%s", serialno);
return aaa_auth_status(dut, conn, cmd, resp, timeout);
}
if (osu && status && strcasecmp(status, "OSU") == 0)
return osu_cert_enroll_status(dut, conn, cmd, addr, timeout);
if (osu && status && strcasecmp(status, "PolicyProvisioning") == 0 &&
imsi)
return osu_sim_policy_provisioning_status(dut, conn, imsi,
timeout);
return SUCCESS_SEND_STATUS;
}
static int osu_set_cert_reenroll(struct sigma_dut *dut, const char *serial,
int enable)
{
sqlite3 *db;
char *sql;
char id[100];
int ret = -1;
if (sqlite3_open(SERVER_DB, &db)) {
sigma_dut_print(dut, DUT_MSG_ERROR,
"Failed to open SQLite database %s",
SERVER_DB);
return -1;
}
snprintf(id, sizeof(id), "cert-%s", serial);
sql = sqlite3_mprintf("UPDATE users SET remediation=%Q WHERE lower(identity)=lower(%Q)",
enable ? "reenroll" : "", id);
if (!sql)
goto fail;
sigma_dut_print(dut, DUT_MSG_DEBUG, "SQL: %s", sql);
if (sqlite3_exec(db, sql, NULL, NULL, NULL) != SQLITE_OK) {
sigma_dut_print(dut, DUT_MSG_ERROR, "SQL operation failed: %s",
sqlite3_errmsg(db));
goto fail;
}
if (sqlite3_changes(db) < 1) {
sigma_dut_print(dut, DUT_MSG_ERROR, "No DB rows modified (specified serial number not found)");
goto fail;
}
ret = 0;
fail:
sqlite3_close(db);
return ret;
}
static enum sigma_cmd_result cmd_server_set_parameter(struct sigma_dut *dut,
struct sigma_conn *conn,
struct sigma_cmd *cmd)