| 1 |  |  | /*	$OpenBSD: init.c,v 1.5 2013/12/03 01:29:00 deraadt Exp $	*/ | 
    
    | 2 |  |  |  | 
    
    | 3 |  |  | /*- | 
    
    | 4 |  |  |  * Copyright (c) 1996 Berkeley Software Design, Inc. All rights reserved. | 
    
    | 5 |  |  |  * | 
    
    | 6 |  |  |  * Redistribution and use in source and binary forms, with or without | 
    
    | 7 |  |  |  * modification, are permitted provided that the following conditions | 
    
    | 8 |  |  |  * are met: | 
    
    | 9 |  |  |  * 1. Redistributions of source code must retain the above copyright | 
    
    | 10 |  |  |  *    notice, this list of conditions and the following disclaimer. | 
    
    | 11 |  |  |  * 2. Redistributions in binary form must reproduce the above copyright | 
    
    | 12 |  |  |  *    notice, this list of conditions and the following disclaimer in the | 
    
    | 13 |  |  |  *    documentation and/or other materials provided with the distribution. | 
    
    | 14 |  |  |  * 3. All advertising materials mentioning features or use of this software | 
    
    | 15 |  |  |  *    must display the following acknowledgement: | 
    
    | 16 |  |  |  *      This product includes software developed by Berkeley Software Design, | 
    
    | 17 |  |  |  *      Inc. | 
    
    | 18 |  |  |  * 4. The name of Berkeley Software Design, Inc.  may not be used to endorse | 
    
    | 19 |  |  |  *    or promote products derived from this software without specific prior | 
    
    | 20 |  |  |  *    written permission. | 
    
    | 21 |  |  |  * | 
    
    | 22 |  |  |  * THIS SOFTWARE IS PROVIDED BY BERKELEY SOFTWARE DESIGN, INC. ``AS IS'' AND | 
    
    | 23 |  |  |  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | 
    
    | 24 |  |  |  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | 
    
    | 25 |  |  |  * ARE DISCLAIMED.  IN NO EVENT SHALL BERKELEY SOFTWARE DESIGN, INC. BE LIABLE | 
    
    | 26 |  |  |  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | 
    
    | 27 |  |  |  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | 
    
    | 28 |  |  |  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | 
    
    | 29 |  |  |  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | 
    
    | 30 |  |  |  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | 
    
    | 31 |  |  |  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | 
    
    | 32 |  |  |  * SUCH DAMAGE. | 
    
    | 33 |  |  |  * | 
    
    | 34 |  |  |  *	BSDI $From: init.c,v 1.2 1996/09/05 23:17:06 prb Exp $ | 
    
    | 35 |  |  |  */ | 
    
    | 36 |  |  |  | 
    
    | 37 |  |  | #include <sys/types.h> | 
    
    | 38 |  |  | #include <limits.h> | 
    
    | 39 |  |  | #include <stdio.h> | 
    
    | 40 |  |  | #include <stdlib.h> | 
    
    | 41 |  |  | #include <string.h> | 
    
    | 42 |  |  |  | 
    
    | 43 |  |  | #include "token.h" | 
    
    | 44 |  |  | #include "tokendb.h" | 
    
    | 45 |  |  |  | 
    
    | 46 |  |  | static struct token_types types[] = { | 
    
    | 47 |  |  | 	{ "activ", "ActivCard", "/etc/activ.db", "012345", | 
    
    | 48 |  |  | 	    TOKEN_HEXINIT, | 
    
    | 49 |  |  | 	    TOKEN_DECMODE | TOKEN_HEXMODE,			/* avail */ | 
    
    | 50 |  |  | 	    TOKEN_HEXMODE },					/* default */ | 
    
    | 51 |  |  | 	{ "crypto", "CRYPTOCard", "/etc/crypto.db", "012345", | 
    
    | 52 |  |  | 	    TOKEN_HEXINIT | TOKEN_PHONE, | 
    
    | 53 |  |  | 	    TOKEN_DECMODE | TOKEN_HEXMODE | TOKEN_PHONEMODE | TOKEN_RIM, | 
    
    | 54 |  |  | 	    TOKEN_HEXMODE },					/* default */ | 
    
    | 55 |  |  | 	{ "snk", "SNK 004", "/etc/snk.db", "222333", | 
    
    | 56 |  |  | 	    0, | 
    
    | 57 |  |  | 	    TOKEN_DECMODE | TOKEN_HEXMODE,			/* avail */ | 
    
    | 58 |  |  | 	    TOKEN_DECMODE },					/* default */ | 
    
    | 59 |  |  | 	{ "token", "X9.9 Token", "/etc/x99token.db", "012345", | 
    
    | 60 |  |  | 	    TOKEN_HEXINIT, | 
    
    | 61 |  |  | 	    TOKEN_DECMODE | TOKEN_HEXMODE | TOKEN_RIM,		/* avail */ | 
    
    | 62 |  |  | 	    TOKEN_HEXMODE },					/* default */ | 
    
    | 63 |  |  | }; | 
    
    | 64 |  |  |  | 
    
    | 65 |  |  | static struct { | 
    
    | 66 |  |  | 	char	*name; | 
    
    | 67 |  |  | 	u_int	value; | 
    
    | 68 |  |  | } modes[] = { | 
    
    | 69 |  |  | 	{ "hexadecimal",	TOKEN_HEXMODE }, | 
    
    | 70 |  |  | 	{ "hex",		TOKEN_HEXMODE }, | 
    
    | 71 |  |  | 	{ "decimal",		TOKEN_DECMODE }, | 
    
    | 72 |  |  | 	{ "dec",		TOKEN_DECMODE }, | 
    
    | 73 |  |  | 	{ "phonebook",		TOKEN_PHONEMODE }, | 
    
    | 74 |  |  | 	{ "phone",		TOKEN_PHONEMODE }, | 
    
    | 75 |  |  | 	{ "reduced-input",	TOKEN_RIM }, | 
    
    | 76 |  |  | 	{ "rim",		TOKEN_RIM } | 
    
    | 77 |  |  | }; | 
    
    | 78 |  |  |  | 
    
    | 79 |  |  | int | 
    
    | 80 |  |  | token_init(char *path) | 
    
    | 81 |  |  | { | 
    
    | 82 |  |  | 	char *p; | 
    
    | 83 |  |  | 	int i; | 
    
    | 84 |  |  |  | 
    
    | 85 |  |  | 	if ((p = strrchr(path, '/')) && p[1] != '\0') | 
    
    | 86 |  |  | 		path = p + 1; | 
    
    | 87 |  |  |  | 
    
    | 88 |  |  | 	for (i = 0; i < sizeof(types)/sizeof(types[0]); ++i) | 
    
    | 89 |  |  | 		if (strstr(path, types[i].name) != NULL) { | 
    
    | 90 |  |  | 			tt = &types[i]; | 
    
    | 91 |  |  | 			return (0); | 
    
    | 92 |  |  | 		} | 
    
    | 93 |  |  | 	if ((p = strstr(path, "token")) != NULL) { | 
    
    | 94 |  |  | 		fprintf(stderr, "Please invoke as one of:"); | 
    
    | 95 |  |  | 		for (i = 0; i < sizeof(types)/sizeof(types[0]); ++i) | 
    
    | 96 |  |  | 			fprintf(stderr, " %.*s%s%s", | 
    
    | 97 |  |  | 			    (int)(p - path), path, types[i].name, p + 5); | 
    
    | 98 |  |  | 		fprintf(stderr, "\n"); | 
    
    | 99 |  |  | 		exit(1); | 
    
    | 100 |  |  |  | 
    
    | 101 |  |  | 	} | 
    
    | 102 |  |  | 	return (-1); | 
    
    | 103 |  |  | } | 
    
    | 104 |  |  |  | 
    
    | 105 |  |  | u_int | 
    
    | 106 |  |  | token_mode(char *mode) | 
    
    | 107 |  |  | { | 
    
    | 108 |  |  | 	int i; | 
    
    | 109 |  |  |  | 
    
    | 110 |  |  | 	for (i = 0; i < sizeof(modes)/sizeof(modes[0]); ++i) | 
    
    | 111 |  |  | 		if (strstr(mode, modes[i].name) != NULL) | 
    
    | 112 |  |  | 			return (tt->modes & modes[i].value); | 
    
    | 113 |  |  | 	return (0); | 
    
    | 114 |  |  | } | 
    
    | 115 |  |  |  | 
    
    | 116 |  |  | char * | 
    
    | 117 |  |  | token_getmode(u_int mode) | 
    
    | 118 |  |  | { | 
    
    | 119 |  |  | 	int i; | 
    
    | 120 |  |  | 	static char buf[32]; | 
    
    | 121 |  |  |  | 
    
    | 122 |  |  | 	for (i = 0; i < sizeof(modes)/sizeof(modes[0]); ++i) | 
    
    | 123 |  |  | 		if (mode == modes[i].value) | 
    
    | 124 |  |  | 			return(modes[i].name); | 
    
    | 125 |  |  | 	snprintf(buf, sizeof(buf), "0x%x", mode); | 
    
    | 126 |  |  | 	return(buf); | 
    
    | 127 |  |  | } |