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