1 /*	$OpenBSD: init.c,v 1.3 2005/02/17 13:49:38 aaron 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 <stdio.h>
39 #include <stdlib.h>
40 #include <string.h>
41 
42 #include "token.h"
43 #include "tokendb.h"
44 
45 __RCSID("$MirOS: src/libexec/login_token/init.c,v 1.2 2006/09/21 02:10:53 tg Exp $");
46 
47 static struct token_types types[] = {
48 	{ "activ", "ActivCard", "/etc/activ.db", "012345",
49 	    TOKEN_HEXINIT,
50 	    TOKEN_DECMODE | TOKEN_HEXMODE,			/* avail */
51 	    TOKEN_HEXMODE },					/* default */
52 	{ "crypto", "CRYPTOCard", "/etc/crypto.db", "012345",
53 	    TOKEN_HEXINIT | TOKEN_PHONE,
54 	    TOKEN_DECMODE | TOKEN_HEXMODE | TOKEN_PHONEMODE | TOKEN_RIM,
55 	    TOKEN_HEXMODE },					/* default */
56 	{ "snk", "SNK 004", "/etc/snk.db", "222333",
57 	    0,
58 	    TOKEN_DECMODE | TOKEN_HEXMODE,			/* avail */
59 	    TOKEN_DECMODE },					/* default */
60 	{ "token", "X9.9 Token", "/etc/x99token.db", "012345",
61 	    TOKEN_HEXINIT,
62 	    TOKEN_DECMODE | TOKEN_HEXMODE | TOKEN_RIM,		/* avail */
63 	    TOKEN_HEXMODE },					/* default */
64 };
65 
66 static struct {
67 	char	*name;
68 	u_int	value;
69 } modes[] = {
70 	{ "hexadecimal",	TOKEN_HEXMODE },
71 	{ "hex",		TOKEN_HEXMODE },
72 	{ "decimal",		TOKEN_DECMODE },
73 	{ "dec",		TOKEN_DECMODE },
74 	{ "phonebook",		TOKEN_PHONEMODE },
75 	{ "phone",		TOKEN_PHONEMODE },
76 	{ "reduced-input",	TOKEN_RIM },
77 	{ "rim",		TOKEN_RIM }
78 };
79 
80 int
token_init(char * path)81 token_init(char *path)
82 {
83 	char *p;
84 	int i;
85 
86 	if ((p = strrchr(path, '/')) && p[1] != '\0')
87 		path = p + 1;
88 
89 	for (i = 0; i < sizeof(types)/sizeof(types[0]); ++i)
90 		if (strstr(path, types[i].name) != NULL) {
91 			tt = &types[i];
92 			return (0);
93 		}
94 	if ((p = strstr(path, "token")) != NULL) {
95 		fprintf(stderr, "Please invoke as one of:");
96 		for (i = 0; i < sizeof(types)/sizeof(types[0]); ++i)
97 			fprintf(stderr, " %.*s%s%s",
98 			    (int)(p - path), path, types[i].name, p + 5);
99 		fprintf(stderr, "\n");
100 		exit(1);
101 
102 	}
103 	return (-1);
104 }
105 
106 u_int
token_mode(char * mode)107 token_mode(char *mode)
108 {
109 	int i;
110 
111 	for (i = 0; i < sizeof(modes)/sizeof(modes[0]); ++i)
112 		if (strstr(mode, modes[i].name) != NULL)
113 			return (tt->modes & modes[i].value);
114 	return (0);
115 }
116 
117 char *
token_getmode(u_int mode)118 token_getmode(u_int mode)
119 {
120 	int i;
121 	static char buf[32];
122 
123 	for (i = 0; i < sizeof(modes)/sizeof(modes[0]); ++i)
124 		if (mode == modes[i].value)
125 			return(modes[i].name);
126 	snprintf(buf, sizeof(buf), "0x%x", mode);
127 	return(buf);
128 }
129