1 /* tlspool/lidsel.c -- Simple demo of the localid selection API
3 * This API demo connects to the LID entry interface to the TLS Pool, and
4 * guides the selection of local identities. To that end, it prints any
5 * database entries that are proposed, and it requests the entry of either
6 * one of these by number, or of a complete local identity string.
8 * From: Rick van Rein <rick@openfortress.nl>
20 #include <tlspool/starttls.h>
21 #include <tlspool/commands.h>
24 #ifndef MAXNUM_DB_LIDS
25 # define MAXNUM_DB_LIDS 100
30 char dblids [MAXNUM_DB_LIDS] [128];
35 /* The workhorse is the callback function. It prints out database entries,
36 * and stores them for future reference. It also requests entry of the
37 * local identity or the index number of the database entries printed.
39 char *lidcb (lidentry_t *entry, void *data) {
41 // Declare & initialise
42 struct data *d = (struct data *) data;
47 printf ("DEBUG: lidsel.c lidcb() called with localid %s\n", entry->localid);
50 // Handle database entries
51 if (entry->flags & PIOF_LIDENTRY_DBENTRY) {
52 entry->localid [127] = '\0';
53 if (d->dblidctr < MAXNUM_DB_LIDS) {
54 memcpy (d->dblids [d->dblidctr],
56 printf ("[%d] %s\n", d->dblidctr,
60 printf ("DEBUG: lidsel.c lidcb() returns after processing database entry\n");
65 // Handle requests for localid
66 if (d->dblidctr >= MAXNUM_DB_LIDS) {
67 fprintf (stderr, "Overwhelmed by %d > %d entries\n",
68 d->dblidctr, MAXNUM_DB_LIDS);
69 d->dblidctr = MAXNUM_DB_LIDS;
71 entry->remoteid [127] = '\0';
72 printf ("Remote identity: %s\n", entry->remoteid);
75 printf ("Please enter a local identity as a string, or by index:\n> ");
77 fgets (input, 128, stdin);
79 if (input [0] == '\0') {
80 ; // Accept empty string as empty line
81 } else if (input [strlen (input) -1] != '\n') {
84 input [strlen (input) -1] = '\0';
86 if (input [0] == '\0') {
87 memset (entry->localid, 0, 128);
88 continue; /* to loop end, return no entry */
90 entryindex = strtol (input, &inpastnum, 10);
91 if (*inpastnum == '\0') {
92 error = error || (entryindex < 0);
93 error = error || (entryindex >= d->dblidctr);
94 error = error || (entryindex >= MAXNUM_DB_LIDS);
96 memcpy (entry->localid,
97 d->dblids [entryindex], 128);
100 memcpy (entry->localid,
105 printf ("DEBUG: lidsel.c lidcb() returns after setting localid to %s and flags to 0x%08x\n", entry->localid, entry->flags);
110 int main (int argc, char *argv []) {
112 // Declare & initialise
115 uint32_t regflags = PIOF_LIDENTRY_WANT_DBENTRY;
116 int responsetime_sec = 300;
120 // Parse cmdline args
122 fprintf (stderr, "Ignoring program arguments\n");
126 // Open log, and also dump on stderr
127 openlog ("lidsel", LOG_PID | LOG_PERROR | LOG_NDELAY, LOG_USER);
130 // Service the TLS Pool
131 exitval = -tlspool_localid_service (NULL, regflags, responsetime_sec, lidcb, &data);
133 perror ("localid entry service terminated");
137 // Close the log facility