FreeCalypso > hg > fc-sim-tools
annotate utils/fcsim1-program.c @ 96:a5cfe5135701
simtool: grcard2.c split into grcard2ki.c and grcard2pins.c
author | Mychaela Falconia <falcon@freecalypso.org> |
---|---|
date | Wed, 05 May 2021 03:55:23 +0000 |
parents | 2a24e94400e8 |
children |
rev | line source |
---|---|
79
2a24e94400e8
fcsim1-program utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
1 /* |
2a24e94400e8
fcsim1-program utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
2 * This utility takes an ICCID number on the command line (needs to be |
2a24e94400e8
fcsim1-program utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
3 * read from the plastic with human eyeballs and entered by the operator), |
2a24e94400e8
fcsim1-program utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
4 * reads card provisioning data from fcsim1-prov-db, and generates |
2a24e94400e8
fcsim1-program utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
5 * an fc-simtool command script for programming one FCSIM1 card. The |
2a24e94400e8
fcsim1-program utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
6 * intent is that the output of this program will be piped directly |
2a24e94400e8
fcsim1-program utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
7 * into fc-simtool. |
2a24e94400e8
fcsim1-program utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
8 */ |
2a24e94400e8
fcsim1-program utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
9 |
2a24e94400e8
fcsim1-program utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
10 #include <sys/types.h> |
2a24e94400e8
fcsim1-program utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
11 #include <stdio.h> |
2a24e94400e8
fcsim1-program utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
12 #include <stdlib.h> |
2a24e94400e8
fcsim1-program utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
13 #include <unistd.h> |
2a24e94400e8
fcsim1-program utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
14 #include "../libutil/dbread.h" |
2a24e94400e8
fcsim1-program utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
15 |
2a24e94400e8
fcsim1-program utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
16 static char prov_db_file[] = "/opt/freecalypso/sim-data/fcsim1-prov-db"; |
2a24e94400e8
fcsim1-program utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
17 |
2a24e94400e8
fcsim1-program utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
18 static char *adm_key = "88888888"; |
2a24e94400e8
fcsim1-program utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
19 static int adm_hex; |
2a24e94400e8
fcsim1-program utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
20 static char *common_script; |
2a24e94400e8
fcsim1-program utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
21 static char iccid_fullstr[20]; |
2a24e94400e8
fcsim1-program utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
22 static struct dbread_state dbs; |
2a24e94400e8
fcsim1-program utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
23 static char *prov_imsi, *prov_acc, *prov_ki, *prov_msisdn; |
2a24e94400e8
fcsim1-program utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
24 |
2a24e94400e8
fcsim1-program utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
25 static void |
2a24e94400e8
fcsim1-program utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
26 preen_iccid(arg) |
2a24e94400e8
fcsim1-program utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
27 char *arg; |
2a24e94400e8
fcsim1-program utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
28 { |
2a24e94400e8
fcsim1-program utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
29 u_char nibbles[19]; |
2a24e94400e8
fcsim1-program utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
30 |
2a24e94400e8
fcsim1-program utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
31 if (parse_decimal_shorthand(arg, nibbles, 19) < 0) |
2a24e94400e8
fcsim1-program utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
32 exit(1); /* error msg already printed */ |
2a24e94400e8
fcsim1-program utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
33 if (nibbles[18] != compute_iccid_luhn(nibbles)) { |
2a24e94400e8
fcsim1-program utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
34 fprintf(stderr, "error: Luhn check digit mismatch\n"); |
2a24e94400e8
fcsim1-program utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
35 exit(1); |
2a24e94400e8
fcsim1-program utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
36 } |
2a24e94400e8
fcsim1-program utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
37 nibbles_to_ascii(nibbles, 19, iccid_fullstr); |
2a24e94400e8
fcsim1-program utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
38 } |
2a24e94400e8
fcsim1-program utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
39 |
2a24e94400e8
fcsim1-program utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
40 static void |
2a24e94400e8
fcsim1-program utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
41 parse_cmdline(argc, argv) |
2a24e94400e8
fcsim1-program utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
42 char **argv; |
2a24e94400e8
fcsim1-program utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
43 { |
2a24e94400e8
fcsim1-program utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
44 extern int optind; |
2a24e94400e8
fcsim1-program utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
45 extern char *optarg; |
2a24e94400e8
fcsim1-program utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
46 int c; |
2a24e94400e8
fcsim1-program utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
47 |
2a24e94400e8
fcsim1-program utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
48 while ((c = getopt(argc, argv, "a:A:c:")) != EOF) { |
2a24e94400e8
fcsim1-program utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
49 switch (c) { |
2a24e94400e8
fcsim1-program utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
50 case 'a': |
2a24e94400e8
fcsim1-program utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
51 adm_key = optarg; |
2a24e94400e8
fcsim1-program utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
52 continue; |
2a24e94400e8
fcsim1-program utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
53 case 'A': |
2a24e94400e8
fcsim1-program utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
54 adm_key = optarg; |
2a24e94400e8
fcsim1-program utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
55 adm_hex = 1; |
2a24e94400e8
fcsim1-program utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
56 continue; |
2a24e94400e8
fcsim1-program utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
57 case 'c': |
2a24e94400e8
fcsim1-program utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
58 common_script = optarg; |
2a24e94400e8
fcsim1-program utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
59 continue; |
2a24e94400e8
fcsim1-program utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
60 case '?': |
2a24e94400e8
fcsim1-program utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
61 default: |
2a24e94400e8
fcsim1-program utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
62 usage: fprintf(stderr, "usage: %s [options] iccid\n", argv[0]); |
2a24e94400e8
fcsim1-program utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
63 exit(1); |
2a24e94400e8
fcsim1-program utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
64 } |
2a24e94400e8
fcsim1-program utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
65 } |
2a24e94400e8
fcsim1-program utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
66 if (argc - optind != 1) |
2a24e94400e8
fcsim1-program utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
67 goto usage; |
2a24e94400e8
fcsim1-program utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
68 preen_iccid(argv[optind]); |
2a24e94400e8
fcsim1-program utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
69 } |
2a24e94400e8
fcsim1-program utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
70 |
2a24e94400e8
fcsim1-program utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
71 main(argc, argv) |
2a24e94400e8
fcsim1-program utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
72 char **argv; |
2a24e94400e8
fcsim1-program utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
73 { |
2a24e94400e8
fcsim1-program utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
74 int rc; |
2a24e94400e8
fcsim1-program utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
75 |
2a24e94400e8
fcsim1-program utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
76 parse_cmdline(argc, argv); |
2a24e94400e8
fcsim1-program utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
77 rc = dbread_find_record(prov_db_file, &dbs, "ICCID", iccid_fullstr); |
2a24e94400e8
fcsim1-program utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
78 if (rc < 0) |
2a24e94400e8
fcsim1-program utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
79 exit(1); /* error msg already printed */ |
2a24e94400e8
fcsim1-program utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
80 prov_imsi = dbread_find_key_req(&dbs, "IMSI"); |
2a24e94400e8
fcsim1-program utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
81 if (!prov_imsi) |
2a24e94400e8
fcsim1-program utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
82 exit(1); /* error msg already printed */ |
2a24e94400e8
fcsim1-program utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
83 prov_acc = dbread_find_key_req(&dbs, "ACC"); |
2a24e94400e8
fcsim1-program utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
84 if (!prov_acc) |
2a24e94400e8
fcsim1-program utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
85 exit(1); /* error msg already printed */ |
2a24e94400e8
fcsim1-program utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
86 prov_ki = dbread_find_key_req(&dbs, "Ki"); |
2a24e94400e8
fcsim1-program utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
87 if (!prov_ki) |
2a24e94400e8
fcsim1-program utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
88 exit(1); /* error msg already printed */ |
2a24e94400e8
fcsim1-program utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
89 prov_msisdn = dbread_find_key(&dbs, "MSISDN"); |
2a24e94400e8
fcsim1-program utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
90 |
2a24e94400e8
fcsim1-program utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
91 /* emit the script! */ |
2a24e94400e8
fcsim1-program utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
92 printf("verify-%s 11 %s\n", adm_hex ? "hex" : "ext", adm_key); |
2a24e94400e8
fcsim1-program utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
93 if (common_script) |
2a24e94400e8
fcsim1-program utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
94 printf("exec %s\n", common_script); |
2a24e94400e8
fcsim1-program utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
95 printf("write-iccid %s\n", iccid_fullstr); |
2a24e94400e8
fcsim1-program utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
96 printf("write-imsi %s\n", prov_imsi); |
2a24e94400e8
fcsim1-program utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
97 printf("write-acc %s\n", prov_acc); |
2a24e94400e8
fcsim1-program utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
98 printf("grcard2-set-ki %s\n", prov_ki); |
2a24e94400e8
fcsim1-program utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
99 if (prov_msisdn) |
2a24e94400e8
fcsim1-program utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
100 printf("pb-update-imm msisdn 1 %s\n", prov_msisdn); |
2a24e94400e8
fcsim1-program utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
101 exit(0); |
2a24e94400e8
fcsim1-program utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
102 } |