view utils/sws-card-lookup.c @ 58:871281cb0555

doc/Low-level-commands: apdu-checksw documented
author Mychaela Falconia <falcon@freecalypso.org>
date Tue, 23 Mar 2021 00:21:02 +0000
parents fa81221ac9b6
children
line wrap: on
line source

/*
 * This program is a simple command line utility for looking up a card
 * in sws-card-db (database generated with sws-email2db) by either
 * ICCID or IMSI and retrieving other associated database fields.
 */

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

static char sws_card_db_file[] = "/opt/freecalypso/sim-data/sws-card-db";

static char *match_key, match_value[20];
static struct dbread_state dbs;

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, match_value);
}

static void
preen_imsi(arg)
	char *arg;
{
	u_char nibbles[15];

	if (parse_decimal_shorthand(arg, nibbles, 15) < 0)
		exit(1);	/* error msg already printed */
	nibbles_to_ascii(nibbles, 15, match_value);
}

static void
lookup_one_key(key)
	char *key;
{
	char *val;

	val = dbread_find_key_req(&dbs, key);
	if (!val)
		exit(1);	/* error msg already printed */
	puts(val);
}

static void
lookup_mult_keys(keys)
	char **keys;
{
	char *key, *val;

	for (; *keys; keys++) {
		key = *keys;
		val = dbread_find_key(&dbs, key);
		if (val)
			printf("%s=%s\n", key, val);
	}
}

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

	if (argc < 4) {
		fprintf(stderr,
			"usage: %s {iccid|imsi} card-number retrieve-key\n",
			argv[0]);
		exit(1);
	}
	match_key = argv[1];
	if (!strcmp(match_key, "iccid"))
		preen_iccid(argv[2]);
	else if (!strcmp(match_key, "imsi"))
		preen_imsi(argv[2]);
	else {
		fprintf(stderr, "error: look-up key must be iccid or imsi\n");
		exit(1);
	}
	rc = dbread_find_record(sws_card_db_file, &dbs, match_key, match_value);
	if (rc < 0)
		exit(1);	/* error msg already printed */
	if (argc == 4)
		lookup_one_key(argv[3]);
	else
		lookup_mult_keys(argv + 3);
	exit(0);
}