/* Copyright (c) 2011 Samuel Lidén Borell 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 #include #include #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); }