view 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
line wrap: on
line source

/*
 * This utility takes an ICCID number on the command line (needs to be
 * read from the plastic with human eyeballs and entered by the operator),
 * reads card provisioning data from fcsim1-prov-db, and generates
 * an fc-simtool command script for programming one FCSIM1 card.  The
 * intent is that the output of this program will be piped directly
 * into fc-simtool.
 */

#include <sys/types.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include "../libutil/dbread.h"

static char prov_db_file[] = "/opt/freecalypso/sim-data/fcsim1-prov-db";

static char *adm_key = "88888888";
static int adm_hex;
static char *common_script;
static char iccid_fullstr[20];
static struct dbread_state dbs;
static char *prov_imsi, *prov_acc, *prov_ki, *prov_msisdn;

static void
preen_iccid(arg)
	char *arg;
{
	u_char nibbles[19];

	if (parse_decimal_shorthand(arg, nibbles, 19) < 0)
		exit(1);	/* error msg already printed */
	if (nibbles[18] != compute_iccid_luhn(nibbles)) {
		fprintf(stderr, "error: Luhn check digit mismatch\n");
		exit(1);
	}
	nibbles_to_ascii(nibbles, 19, iccid_fullstr);
}

static void
parse_cmdline(argc, argv)
	char **argv;
{
	extern int optind;
	extern char *optarg;
	int c;

	while ((c = getopt(argc, argv, "a:A:c:")) != EOF) {
		switch (c) {
		case 'a':
			adm_key = optarg;
			continue;
		case 'A':
			adm_key = optarg;
			adm_hex = 1;
			continue;
		case 'c':
			common_script = optarg;
			continue;
		case '?':
		default:
usage:			fprintf(stderr, "usage: %s [options] iccid\n", argv[0]);
			exit(1);
		}
	}
	if (argc - optind != 1)
		goto usage;
	preen_iccid(argv[optind]);
}

main(argc, argv)
	char **argv;
{
	int rc;

	parse_cmdline(argc, argv);
	rc = dbread_find_record(prov_db_file, &dbs, "ICCID", iccid_fullstr);
	if (rc < 0)
		exit(1);	/* error msg already printed */
	prov_imsi = dbread_find_key_req(&dbs, "IMSI");
	if (!prov_imsi)
		exit(1);	/* error msg already printed */
	prov_acc = dbread_find_key_req(&dbs, "ACC");
	if (!prov_acc)
		exit(1);	/* error msg already printed */
	prov_ki = dbread_find_key_req(&dbs, "Ki");
	if (!prov_ki)
		exit(1);	/* error msg already printed */
	prov_msisdn = dbread_find_key(&dbs, "MSISDN");

	/* emit the script! */
	printf("verify-%s 11 %s\n", adm_hex ? "hex" : "ext", adm_key);
	if (common_script)
		printf("exec %s\n", common_script);
	printf("write-iccid %s\n", iccid_fullstr);
	printf("write-imsi %s\n", prov_imsi);
	printf("write-acc %s\n", prov_acc);
	printf("grcard2-set-ki %s\n", prov_ki);
	if (prov_msisdn)
		printf("pb-update-imm msisdn 1 %s\n", prov_msisdn);
	exit(0);
}