FreeCalypso > hg > fc-sim-tools
diff utils/sws-card-lookup.c @ 27:ca8a6f95826a
implemented sws-card-lookup and underlying libutil functions
author | Mychaela Falconia <falcon@freecalypso.org> |
---|---|
date | Tue, 16 Mar 2021 23:22:37 +0000 |
parents | |
children | fa81221ac9b6 |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/utils/sws-card-lookup.c Tue Mar 16 23:22:37 2021 +0000 @@ -0,0 +1,101 @@ +/* + * 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(&dbs, key); + if (!val) { + fprintf(stderr, "error: card record has no %s field\n", key); + exit(1); + } + 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); +}