remote pool_handle_t
[tlspool] / tool / lidsel.c
1 /* tlspool/lidsel.c -- Simple demo of the localid selection API
2  *
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.
7  *
8  * From: Rick van Rein <rick@openfortress.nl>
9  */
10
11
12 #include <stdlib.h>
13 #include <stdio.h>
14 #include <string.h>
15 #include <stdint.h>
16
17 #include <syslog.h>
18 #include <errno.h>
19
20 #include <tlspool/starttls.h>
21 #include <tlspool/commands.h>
22
23
24 #ifndef MAXNUM_DB_LIDS
25 #   define MAXNUM_DB_LIDS 100
26 #endif
27
28
29 struct data {
30         char dblids [MAXNUM_DB_LIDS] [128];
31         int dblidctr;
32 };
33
34
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.
38  */
39 char *lidcb (lidentry_t *entry, void *data) {
40         //
41         // Declare & initialise
42         struct data *d = (struct data *) data;
43         int error = 0;
44         char input [128+1];
45         char *inpastnum;
46         long entryindex;
47 printf ("DEBUG: lidsel.c lidcb() called with localid %s\n", entry->localid);
48
49         //
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],
55                                 entry->localid, 128);
56                         printf ("[%d] %s\n", d->dblidctr,
57                                 entry->localid);
58                 }
59                 d->dblidctr++;
60 printf ("DEBUG: lidsel.c lidcb() returns after processing database entry\n");
61                 return NULL;
62         }
63
64         //
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;
70         }
71         entry->remoteid [127] = '\0';
72         printf ("Remote identity: %s\n", entry->remoteid);
73         do {
74                 error = 0;
75                 printf ("Please enter a local identity as a string, or by index:\n> ");
76                 fflush (stdout);
77                 fgets (input, 128, stdin);
78                 input [127] = '\0';
79                 if (input [0] == '\0') {
80                         ; // Accept empty string as empty line
81                 } else if (input [strlen (input) -1] != '\n') {
82                         error = 1;
83                 } else {
84                         input [strlen (input) -1] = '\0';
85                 }
86                 if (input [0] == '\0') {
87                         memset (entry->localid, 0, 128);
88                         continue;       /* to loop end, return no entry */
89                 }
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);
95                         if (!error) {
96                                 memcpy (entry->localid,
97                                         d->dblids [entryindex], 128);
98                         }
99                 } else {
100                         memcpy (entry->localid,
101                                 input, 128);
102                 }
103         } while (error);
104         d->dblidctr = 0;
105 printf ("DEBUG: lidsel.c lidcb() returns after setting localid to %s and flags to 0x%08x\n", entry->localid, entry->flags);
106         return NULL;
107 }
108
109
110 int main (int argc, char *argv []) {
111         //
112         // Declare & initialise
113         struct data data;
114         data.dblidctr = 0;
115         uint32_t regflags = PIOF_LIDENTRY_WANT_DBENTRY;
116         int responsetime_sec = 300;
117         int exitval;
118
119         //
120         // Parse cmdline args
121         if (argc > 1) {
122                 fprintf (stderr, "Ignoring program arguments\n");
123         }
124
125         //
126         // Open log, and also dump on stderr
127         openlog ("lidsel", LOG_PID | LOG_PERROR | LOG_NDELAY, LOG_USER);
128
129         //
130         // Service the TLS Pool
131         exitval = -tlspool_localid_service (NULL, regflags, responsetime_sec, lidcb, &data);
132         if (exitval == 1) {
133                 perror ("localid entry service terminated");
134         }
135
136         //
137         // Close the log facility
138         closelog ();
139
140         //
141         // Return exitval
142         return exitval;
143 }