comparison 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
comparison
equal deleted inserted replaced
26:322f6fcdc36e 27:ca8a6f95826a
1 /*
2 * This program is a simple command line utility for looking up a card
3 * in sws-card-db (database generated with sws-email2db) by either
4 * ICCID or IMSI and retrieving other associated database fields.
5 */
6
7 #include <sys/types.h>
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <string.h>
11 #include <strings.h>
12 #include "../libutil/dbread.h"
13
14 static char sws_card_db_file[] = "/opt/freecalypso/sim-data/sws-card-db";
15
16 static char *match_key, match_value[20];
17 static struct dbread_state dbs;
18
19 static void
20 preen_iccid(arg)
21 char *arg;
22 {
23 u_char nibbles[19];
24
25 if (parse_decimal_shorthand(arg, nibbles, 19) < 0)
26 exit(1); /* error msg already printed */
27 if (nibbles[18] != compute_iccid_luhn(nibbles)) {
28 fprintf(stderr, "error: Luhn check digit mismatch\n");
29 exit(1);
30 }
31 nibbles_to_ascii(nibbles, 19, match_value);
32 }
33
34 static void
35 preen_imsi(arg)
36 char *arg;
37 {
38 u_char nibbles[15];
39
40 if (parse_decimal_shorthand(arg, nibbles, 15) < 0)
41 exit(1); /* error msg already printed */
42 nibbles_to_ascii(nibbles, 15, match_value);
43 }
44
45 static void
46 lookup_one_key(key)
47 char *key;
48 {
49 char *val;
50
51 val = dbread_find_key(&dbs, key);
52 if (!val) {
53 fprintf(stderr, "error: card record has no %s field\n", key);
54 exit(1);
55 }
56 puts(val);
57 }
58
59 static void
60 lookup_mult_keys(keys)
61 char **keys;
62 {
63 char *key, *val;
64
65 for (; *keys; keys++) {
66 key = *keys;
67 val = dbread_find_key(&dbs, key);
68 if (val)
69 printf("%s=%s\n", key, val);
70 }
71 }
72
73 main(argc, argv)
74 char **argv;
75 {
76 int rc;
77
78 if (argc < 4) {
79 fprintf(stderr,
80 "usage: %s {iccid|imsi} card-number retrieve-key\n",
81 argv[0]);
82 exit(1);
83 }
84 match_key = argv[1];
85 if (!strcmp(match_key, "iccid"))
86 preen_iccid(argv[2]);
87 else if (!strcmp(match_key, "imsi"))
88 preen_imsi(argv[2]);
89 else {
90 fprintf(stderr, "error: look-up key must be iccid or imsi\n");
91 exit(1);
92 }
93 rc = dbread_find_record(sws_card_db_file, &dbs, match_key, match_value);
94 if (rc < 0)
95 exit(1); /* error msg already printed */
96 if (argc == 4)
97 lookup_one_key(argv[3]);
98 else
99 lookup_mult_keys(argv + 3);
100 exit(0);
101 }