1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
|
/*
Copyright (c) 2011 Samuel Lidén Borell <samuel@slbdata.se>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
#define _BSD_SOURCE
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
#include "backend_private.h"
typedef struct palt_reglist {
struct palt_reglist *next;
palt_tokenlist_t *tokenlist;
} palt_reglist_t;
static int use_count = 0;
#define BACKEND_COUNT (sizeof(backends) / sizeof(palt_backend_t *))
static palt_backend_t *const backends[] = {
&palt_softtoken_backend,
};
static palt_reglist_t *registrations = NULL;
static palt_token_t *tokens = NULL;
char *palt_get_token_name(const palt_token_t *token) {
return strdup(token->name);
}
void palt_register_tokenlist(palt_tokenlist_t *tokenlist) {
if (use_count++ == 0) {
/* Initialize everything if this is the first call */
size_t i;
for (i = 0; i < BACKEND_COUNT; i++) {
backends[i]->initialize();
}
}
/* Register for notifications */
palt_reglist_t *entry = malloc(sizeof(palt_reglist_t));
entry->next = registrations;
entry->tokenlist = tokenlist;
registrations = entry;
}
void palt_unregister_tokenlist(palt_tokenlist_t *tokenlist) {
/* Unregister for notifications */
palt_reglist_t *entry = registrations;
if (entry->tokenlist == tokenlist) {
/* Delete first entry */
registrations = entry->next;
free(entry);
} else {
while (entry->next->tokenlist != tokenlist) entry = entry->next;
palt_reglist_t *after = entry->next->next;
free(entry->next);
entry->next = after;
}
/* Shutdown the system if this was the last remaining list */
if (--use_count == 0) {
size_t i;
for (i = 0; i < BACKEND_COUNT; i++) {
backends[i]->shutdown();
}
}
}
void palt_scan_tokens() {
if (use_count != 0) {
size_t i;
for (i = 0; i < BACKEND_COUNT; i++) {
if (backends[i]->start_scan) backends[i]->start_scan();
}
}
}
palt_token_t *palt_add_token(const palt_backend_t *backend, char *name, void *backend_specific) {
palt_reglist_t *reg;
/* Add to list */
palt_token_t *token = malloc(sizeof(palt_token_t));
token->next = tokens;
token->backend = backend;
token->name = name;
token->backend_specific = backend_specific;
tokens = token;
/* Notify */
for (reg = registrations; reg; reg = reg->next) {
reg->tokenlist->added(reg->tokenlist, token);
}
return token;
}
#define REQ_BACKEND(req) ((palt_backend_t *)req->token->backend)
void palt_generate(palt_request_t *req) {
REQ_BACKEND(req)->generate(req);
}
void palt_auth(palt_request_t *req) {
REQ_BACKEND(req)->auth(req);
}
|