/*- * Copyright (c) 2006 Fredrik Lindberg. * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * * $Id: bbdm.c 57 2006-02-23 14:15:13Z fli $ */ #include #include #include #include #include #include #include #include #include #include #include #include #define VERSION "1.0" #define DEFCONFPATH "/usr/local/etc/birdb.conf" static void usage(char *pname) { printf("BioAPI BIR Database Management %s\n", VERSION); printf("Copyright 2006, Fredrik Lindberg \n"); printf("Usage %s [options]\n", pname); printf( " -l bsp|birdb \t List avaiable BSPs or BIR DB backends\n" " -c username \t Capture and create BIR record\n" " -v username \t Verify an existing BIR record\n" " -i \t\t Identify a user (returns username)\n" " -r username \t List all records for a user\n" " -d username \t Clear records for user\n" " -b uuid \t Specify which BSP to use\n" " -m backend \t Specify BIRDB backend module\n" " -f path \t Path to BIRDB configuration\n" ); } static int check_username(char *str) { while (*str != '\0') { if (isalnum(*str++) == 0) return (1); } return (0); } enum { NOACTION, CAPTURE, /* Enroll/capture and create a new record */ VERIFY, /* Verify an existing record */ IDENTIFY, /* Attempt to identify a user */ DELETE, /* Delete records for user */ LISTBSPS, /* List avaiable BSPs */ LISTBIRDB, /* List avaiable BIRDB backends */ LISTUREC /* List records for a user */ }; int main(int argc, char *argv[]) { char *username = NULL, *bsp_id = NULL, *bm_name = NULL; char *conf = NULL, bsp_path[FILENAME_MAX]; int ch, error, action = NOACTION; int i, bsp_count, mods; struct birdb_mod *bm, **bmlist; struct birdb_rec *rec, **recs, key; struct bsp_list *bsps; BioAPI_HANDLE *handle; birdb *bdb; void *bmh; if (argc < 0) { usage(argv[0]); exit(EXIT_FAILURE); } while ((ch = getopt(argc, argv, "hl:c:v:b:im:r:d:f:")) != -1) { switch (ch) { case 'h': usage(argv[0]); exit(EXIT_SUCCESS); break; case 'b': bsp_id = argv[optind - 1]; break; case 'c': username = argv[optind - 1]; action = CAPTURE; break; case 'v': username = argv[optind - 1]; action = VERIFY; break; case 'i': action = IDENTIFY; break; case 'l': if (strcasecmp(argv[optind - 1], "bsp") == 0) action = LISTBSPS; else if (strcasecmp(argv[optind - 1], "birdb") == 0) action = LISTBIRDB; else errx(EXIT_FAILURE, "Invalid argument"); break; case 'm': bm_name = argv[optind - 1]; break; case 'r': username = argv[optind - 1]; action = LISTUREC; break; case 'd': username = argv[optind - 1]; action = DELETE; break; case 'f': conf = argv[optind - 1]; break; default: usage(argv[0]); exit(EXIT_FAILURE); } } if (getuid() != 0) { if (geteuid() == 0) setuid(0); else errx(EXIT_FAILURE, "You do not have sufficient privileges to use this tool"); } error = bioapi_init(); if (error) errx(EXIT_FAILURE, "Failed to initate BioAPI"); if (conf == NULL) conf = DEFCONFPATH; switch (action) { case CAPTURE: if (bsp_id == NULL) errx(EXIT_FAILURE, "You must specify a BSP UUID"); if (bm_name == NULL) errx(EXIT_FAILURE, "You must specify a BIRDB backend"); if (username == NULL) errx(EXIT_FAILURE, "You must specify a user"); if (check_username(username)) errx(EXIT_FAILURE, "Illegal username"); /* Initiate birdb backend system */ bdb = birdb_init(); if (bdb == NULL) errx(EXIT_FAILURE, "Failed to initiate BIR DB"); /* Parse cfg file (load configured backend modules) */ error = birdb_cfgparse(bdb, conf); if (error < 0) errx(EXIT_FAILURE, "Failed to parse %s", conf); /* Find the module the user asked for */ bm = birdb_findmod(bdb, bm_name); if (bm == NULL) errx(EXIT_FAILURE, "Backend ``%s'' not loaded", bm_name); /* Open the birdb backend, use arguments from cfg file */ bmh = birdb_backend_open(bm, bsp_id, bm->bm_argc, bm->bm_argv); if (bmh == NULL) errx(EXIT_FAILURE, "Failed to open backend"); /* Attach BSP */ handle = bioapi_attach_bsp(bsp_id); if (handle == NULL) errx(EXIT_FAILURE, "Failed to initate BSP %s\n", bsp_id); /* Enroll user */ rec = bioapi_enroll(handle, bm, bmh, username); if (rec != NULL) { printf("Please verify record\n"); error = bioapi_verify(handle, rec); if (error == 0) printf("Record for ``%s'' created successfully\n", username); else { birdb_backend_del(bm, bmh, rec); printf("Failed to verify, records do not match\n"); } } else warnx("Failed to create BIR record\n"); birdb_freerec(rec); /* close backend, close birdb system, detach bsp */ birdb_backend_close(bm, bmh); birdb_close(bdb); bioapi_detach_bsp(handle, bsp_id); break; case VERIFY: if (bsp_id == NULL) errx(EXIT_FAILURE, "You must specify a BSP UUID"); if (bm_name == NULL) errx(EXIT_FAILURE, "You must specify a BIRDB backend"); if (username == NULL) errx(EXIT_FAILURE, "You must specify a user"); if (check_username(username)) errx(EXIT_FAILURE, "Illegal username"); /* Initiate birdb backend system */ bdb = birdb_init(); if (bdb == NULL) errx(EXIT_FAILURE, "Failed to initiate BIR DB"); /* Parse cfg file (load configured backend modules) */ error = birdb_cfgparse(bdb, conf); if (error < 0) errx(EXIT_FAILURE, "Failed to parse %s", conf); /* Find the module the user asked for */ bm = birdb_findmod(bdb, bm_name); if (bm == NULL) errx(EXIT_FAILURE, "Backend ``%s'' not loaded", bm_name); /* Open the birdb backend, use arguments from cfg file */ bmh = birdb_backend_open(bm, bsp_id, bm->bm_argc, bm->bm_argv); if (bmh == NULL) errx(EXIT_FAILURE, "Failed to open backend"); key.br_key = username; /* Get the results from backend, might be more than one */ recs = birdb_backend_get(bm, bmh, &key); if (recs != NULL) { /* Attach BSP */ handle = bioapi_attach_bsp(bsp_id); if (handle == NULL) errx(EXIT_FAILURE, "Failed to initate BSP %s\n", bsp_id); error = bioapi_verify_many(handle, recs); if (error >= 0) { printf("User record verified (creation time %.24s)\n", ctime(&recs[error]->br_ctime)); } else { printf("Verification failed\n"); } bioapi_detach_bsp(handle, bsp_id); } else { printf("No records avaiable\n"); } birdb_backend_freegetres(bm, bmh, recs); /* close backend, close birdb system */ birdb_backend_close(bm, bmh); birdb_close(bdb); break; case IDENTIFY: if (bsp_id == NULL) errx(EXIT_FAILURE, "You must specify a BSP UUID"); if (bm_name == NULL) errx(EXIT_FAILURE, "You must specify a BIRDB backend"); /* Initiate birdb backend system */ bdb = birdb_init(); if (bdb == NULL) errx(EXIT_FAILURE, "Failed to initiate BIR DB"); /* Parse cfg file (load configured backend modules) */ error = birdb_cfgparse(bdb, conf); if (error < 0) errx(EXIT_FAILURE, "Failed to parse %s", conf); /* Find the module the user asked for */ bm = birdb_findmod(bdb, bm_name); if (bm == NULL) errx(EXIT_FAILURE, "Backend ``%s'' not loaded", bm_name); /* Open the birdb backend, use arguments from cfg file */ bmh = birdb_backend_open(bm, bsp_id, bm->bm_argc, bm->bm_argv); if (bmh == NULL) errx(EXIT_FAILURE, "Failed to open backend"); /* Attach BSP */ handle = bioapi_attach_bsp(bsp_id); if (handle == NULL) errx(EXIT_FAILURE, "Failed to initate BSP %s\n", bsp_id); error = bioapi_identify(handle, bm, bmh, &username); if (error == 0) printf("User identified as ``%s''\n", username); else printf("Unable to identify user\n"); /* close backend, close birdb system, detach bsp */ birdb_backend_close(bm, bmh); birdb_close(bdb); bioapi_detach_bsp(handle, bsp_id); break; case LISTBSPS: bsp_count = bioapi_get_bsp_list(&bsps); for (i = 0; i < bsp_count; i++) { printf("UUID %s\n", bsps[i].bsp_uuid); printf("\t %s %s (%s)\n", bsps[i].bsp_vend, bsps[i].bsp_name, bsps[i].bsp_desc); } bioapi_destroy_bsp_list(bsps, bsp_count); break; case LISTBIRDB: /* Initiate birdb backend system */ bdb = birdb_init(); if (bdb == NULL) errx(EXIT_FAILURE, "Failed to initiate BIR DB"); /* Parse cfg file (load configured backend modules) */ error = birdb_cfgparse(bdb, conf); if (error < 0) errx(EXIT_FAILURE, "Failed to parse %s", conf); printf("Installed BIRDB modules\n"); bmlist = birdb_getmodlist(bdb, &mods); for (i = 0; i < mods; i++) { printf("%s \t %s\n", birdb_backend_getname(bmlist[i]), birdb_backend_getdesc(bmlist[i])); } birdb_freemodlist(bmlist); birdb_close(bdb); break; case LISTUREC: if (bsp_id == NULL) errx(EXIT_FAILURE, "You must specify a BSP UUID"); if (bm_name == NULL) errx(EXIT_FAILURE, "You must specify a BIRDB backend"); if (username == NULL) errx(EXIT_FAILURE, "You must specify a user"); if (check_username(username)) errx(EXIT_FAILURE, "Illegal username"); /* Initiate birdb backend system */ bdb = birdb_init(); if (bdb == NULL) errx(EXIT_FAILURE, "Failed to initiate BIR DB"); /* Parse cfg file (load configured backend modules) */ error = birdb_cfgparse(bdb, conf); if (error < 0) errx(EXIT_FAILURE, "Failed to parse %s", conf); /* Find the module the user asked for */ bm = birdb_findmod(bdb, bm_name); if (bm == NULL) errx(EXIT_FAILURE, "Backend ``%s'' not loaded", bm_name); /* Open the birdb backend, use arguments from cfg file */ bmh = birdb_backend_open(bm, bsp_id, bm->bm_argc, bm->bm_argv); if (bmh == NULL) errx(EXIT_FAILURE, "Failed to open backend"); key.br_key = username; /* Get the results from backend, might be more than one */ recs = birdb_backend_get(bm, bmh, &key); printf("Records for user ``%s''\n", username); if (recs != NULL) { for (i = 0; recs[i] != NULL; i++) { printf("%d \t %.24s\n", i + 1, ctime(&recs[i]->br_ctime)); } } birdb_backend_freegetres(bm, bmh, recs); /* close backend, close birdb system, detach bsp */ birdb_backend_close(bm, bmh); birdb_close(bdb); break; case DELETE: if (bsp_id == NULL) errx(EXIT_FAILURE, "You must specify a BSP UUID"); if (bm_name == NULL) errx(EXIT_FAILURE, "You must specify a BIRDB backend"); if (username == NULL) errx(EXIT_FAILURE, "You must specify a user"); if (check_username(username)) errx(EXIT_FAILURE, "Illegal username"); /* Initiate birdb backend system */ bdb = birdb_init(); if (bdb == NULL) errx(EXIT_FAILURE, "Failed to initiate BIR DB"); /* Parse cfg file (load configured backend modules) */ error = birdb_cfgparse(bdb, conf); if (error < 0) errx(EXIT_FAILURE, "Failed to parse %s", conf); /* Find the module the user asked for */ bm = birdb_findmod(bdb, bm_name); if (bm == NULL) errx(EXIT_FAILURE, "Backend ``%s'' not loaded", bm_name); /* Open the birdb backend, use arguments from cfg file */ bmh = birdb_backend_open(bm, bsp_id, bm->bm_argc, bm->bm_argv); if (bmh == NULL) errx(EXIT_FAILURE, "Failed to open backend"); key.br_key = username; /* Get the results from backend, might be more than one */ recs = birdb_backend_get(bm, bmh, &key); if (recs != NULL) { for (i = 0; recs[i] != NULL; i++) { birdb_backend_del(bm, bmh, recs[i]); } printf("Records for user ``%s'' deleted\n", username); } else { printf("No records for that user\n"); } birdb_backend_freegetres(bm, bmh, recs); /* close backend, close birdb system */ birdb_backend_close(bm, bmh); birdb_close(bdb); break; case NOACTION: /* FALLTHROUGH */ default: usage(argv[0]); break; } bioapi_destroy(); return (0); }