comparison utils/fcsim1-program.c @ 79:2a24e94400e8

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