/*- * 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. */ #include #include #include #include #include #include #include #include #include #include "bioapitool.h" #define VERSION "1.0" #define BIR_PATH "/var/db/bioapi/bir" static void usage(char *pname) { printf("bioapitool %s, Copyright 2006" " Fredrik Lindberg \n", VERSION); printf("Usage %s [options]\n", pname); printf( " -l \t\t List avaiable BSPs\n" " -b uuid \t Specify which BSP to use\n" " -p path \t Path to BIR database\n" " -c username \t Capture and create BIR record\n" " -v username \t Verify an existing BIR record\n" ); } static int check_username(char *str) { while (*str != '\0') { if (isalnum(*str++) == 0) return (1); } return (0); } enum { NOACTION, CAPTURE, ENROLL, VERIFY, LISTBSPS }; int main(int argc, char *argv[]) { char *username = NULL, *bsp_id = NULL, *path = NULL; char bsp_path[FILENAME_MAX]; int ch, error, action = NOACTION; int i, bsp_count, retval = 0; struct bsp_list *bsps; BioAPI_HANDLE *handle; if (argc < 2) { usage(argv[0]); exit(EXIT_FAILURE); } while ((ch = getopt(argc, argv, "hlc:v:b:p:")) != -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 'l': action = LISTBSPS; break; case 'p': path = 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 = init_bioapi(); if (error) errx(EXIT_FAILURE, "Failed to initate BioAPI"); if (path == NULL) path = BIR_PATH; switch (action) { case CAPTURE: if (bsp_id == NULL) errx(EXIT_FAILURE, "You must specify a BSP UUID"); if (username == NULL) errx(EXIT_FAILURE, "You must specify a user"); if (check_username(username)) errx(EXIT_FAILURE, "Illegal username"); handle = set_bsp(bsp_id); if (handle == NULL) errx(EXIT_FAILURE, "Failed to initate BSP %s\n", bsp_id); snprintf(bsp_path, FILENAME_MAX - 1, "%s/%s", path, bsp_id); /* Make sure the path exists */ error = mkdir(bsp_path, S_IRWXU); if (error != 0 && errno != EEXIST) { warn("Unable to access %s", bsp_path); break; } error = create_record(handle, username, bsp_path); if (error == 0) { printf("Please verify record\n"); error = verify_record(handle, username, bsp_path); if (error == 0) printf("Record for user created successfully\n"); else { remove_record(username, bsp_path); printf("Failed to verify, records do not match\n"); } } else warnx("Failed to create BIR record\n"); unload_bsp(handle, bsp_id); break; case VERIFY: if (bsp_id == NULL) errx(EXIT_FAILURE, "You must specify a BSP UUID"); if (username == NULL) errx(EXIT_FAILURE, "You must specify a user"); if (check_username(username)) errx(EXIT_FAILURE, "Illegal username"); handle = set_bsp(bsp_id); if (handle == NULL) errx(EXIT_FAILURE, "Failed to initate BSP %s\n", bsp_id); snprintf(bsp_path, FILENAME_MAX - 1, "%s/%s", path, bsp_id); retval = verify(handle, username, bsp_path); if (retval == 0) printf("Verification sucessful\n"); else printf("Verification failed\n"); unload_bsp(handle, bsp_id); break; case LISTBSPS: bsp_count = 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); } destroy_bsp_list(bsps, bsp_count); break; case NOACTION: /* FALLTHROUGH */ default: usage(argv[0]); break; } close_bioapi(handle); return (retval); }