xref: /openbsd-src/libexec/login_token/init.c (revision a7b56afa0e2c1bb64f89dcd77df11cba33516425)
1 /*	$OpenBSD: init.c,v 1.6 2020/09/06 17:08:29 mortimer 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 struct token_types *tt;
66 
67 static struct {
68 	char	*name;
69 	u_int	value;
70 } modes[] = {
71 	{ "hexadecimal",	TOKEN_HEXMODE },
72 	{ "hex",		TOKEN_HEXMODE },
73 	{ "decimal",		TOKEN_DECMODE },
74 	{ "dec",		TOKEN_DECMODE },
75 	{ "phonebook",		TOKEN_PHONEMODE },
76 	{ "phone",		TOKEN_PHONEMODE },
77 	{ "reduced-input",	TOKEN_RIM },
78 	{ "rim",		TOKEN_RIM }
79 };
80 
81 int
token_init(char * path)82 token_init(char *path)
83 {
84 	char *p;
85 	int i;
86 
87 	if ((p = strrchr(path, '/')) && p[1] != '\0')
88 		path = p + 1;
89 
90 	for (i = 0; i < sizeof(types)/sizeof(types[0]); ++i)
91 		if (strstr(path, types[i].name) != NULL) {
92 			tt = &types[i];
93 			return (0);
94 		}
95 	if ((p = strstr(path, "token")) != NULL) {
96 		fprintf(stderr, "Please invoke as one of:");
97 		for (i = 0; i < sizeof(types)/sizeof(types[0]); ++i)
98 			fprintf(stderr, " %.*s%s%s",
99 			    (int)(p - path), path, types[i].name, p + 5);
100 		fprintf(stderr, "\n");
101 		exit(1);
102 
103 	}
104 	return (-1);
105 }
106 
107 u_int
token_mode(char * mode)108 token_mode(char *mode)
109 {
110 	int i;
111 
112 	for (i = 0; i < sizeof(modes)/sizeof(modes[0]); ++i)
113 		if (strstr(mode, modes[i].name) != NULL)
114 			return (tt->modes & modes[i].value);
115 	return (0);
116 }
117 
118 char *
token_getmode(u_int mode)119 token_getmode(u_int mode)
120 {
121 	int i;
122 	static char buf[32];
123 
124 	for (i = 0; i < sizeof(modes)/sizeof(modes[0]); ++i)
125 		if (mode == modes[i].value)
126 			return(modes[i].name);
127 	snprintf(buf, sizeof(buf), "0x%x", mode);
128 	return(buf);
129 }
130