xref: /openbsd/libexec/login_token/init.c (revision db3296cf)
1 /*	$OpenBSD: init.c,v 1.2 2003/07/10 00:04:28 david 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 static struct token_types types[] = {
46 	{ "activ", "ActivCard", "/etc/activ.db", "012345",
47 	    TOKEN_HEXINIT,
48 	    TOKEN_DECMODE | TOKEN_HEXMODE,			/* avail */
49 	    TOKEN_HEXMODE },					/* default */
50 	{ "crypto", "CRYPTOCard", "/etc/crypto.db", "012345",
51 	    TOKEN_HEXINIT | TOKEN_PHONE,
52 	    TOKEN_DECMODE | TOKEN_HEXMODE | TOKEN_PHONEMODE | TOKEN_RIM,
53 	    TOKEN_HEXMODE },					/* default */
54 	{ "snk", "SNK 004", "/etc/snk.db", "222333",
55 	    0,
56 	    TOKEN_DECMODE | TOKEN_HEXMODE,			/* avail */
57 	    TOKEN_DECMODE },					/* default */
58 	{ "token", "X9.9 Token", "/etc/x99token.db", "012345",
59 	    TOKEN_HEXINIT,
60 	    TOKEN_DECMODE | TOKEN_HEXMODE | TOKEN_RIM,		/* avail */
61 	    TOKEN_HEXMODE },					/* default */
62 };
63 
64 static struct {
65 	char	*name;
66 	u_int	value;
67 } modes[] = {
68 	{ "hexadecimal",	TOKEN_HEXMODE },
69 	{ "hex",		TOKEN_HEXMODE },
70 	{ "decimal",		TOKEN_DECMODE },
71 	{ "dec",		TOKEN_DECMODE },
72 	{ "phonebook",		TOKEN_PHONEMODE },
73 	{ "phone",		TOKEN_PHONEMODE },
74 	{ "reduced-input",	TOKEN_RIM },
75 	{ "rim",		TOKEN_RIM }
76 };
77 
78 int
79 token_init(char *path)
80 {
81 	char *p;
82 	int i;
83 
84 	if ((p = strrchr(path, '/')) && p[1] != '\0')
85 		path = p + 1;
86 
87 	for (i = 0; i < sizeof(types)/sizeof(types[0]); ++i)
88 		if (strstr(path, types[i].name) != NULL) {
89 			tt = &types[i];
90 			return (0);
91 		}
92 	if ((p = strstr(path, "token")) != NULL) {
93 		fprintf(stderr, "Please invoke as one of:");
94 		for (i = 0; i < sizeof(types)/sizeof(types[0]); ++i)
95 			fprintf(stderr, " %.*s%s%s",
96 			    p - path, path, types[i].name, p + 5);
97 		fprintf(stderr, "\n");
98 		exit(1);
99 
100 	}
101 	return (-1);
102 }
103 
104 u_int
105 token_mode(char *mode)
106 {
107 	int i;
108 
109 	for (i = 0; i < sizeof(modes)/sizeof(modes[0]); ++i)
110 		if (strstr(mode, modes[i].name) != NULL)
111 			return (tt->modes & modes[i].value);
112 				return (0);
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 }
128