1 /*	$OpenBSD: login_token.c,v 1.8 2004/03/10 21:30:27 millert Exp $	*/
2 
3 /*-
4  * Copyright (c) 1995, 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: login_token.c,v 1.2 1996/09/04 05:33:05 prb Exp $
35  */
36 
37 #include <sys/types.h>
38 #include <sys/param.h>
39 #include <sys/time.h>
40 #include <sys/resource.h>
41 
42 #include <err.h>
43 #include <readpassphrase.h>
44 #include <signal.h>
45 #include <stdio.h>
46 #include <syslog.h>
47 #include <stdlib.h>
48 #include <string.h>
49 #include <unistd.h>
50 #include <login_cap.h>
51 #include <bsd_auth.h>
52 
53 #include "token.h"
54 
55 int
main(int argc,char * argv[])56 main(int argc, char *argv[])
57 {
58 	FILE *back = NULL;
59 	char *class = 0;
60 	char *username = 0;
61 	char *instance;
62 	char challenge[1024];
63 	char response[1024];
64 	char *pp = 0;
65 	int c;
66 	int mode = 0;
67 	struct rlimit cds;
68 	sigset_t blockset;
69 
70 	(void)setpriority(PRIO_PROCESS, 0, 0);
71 
72 	/* We block keyboard-generated signals during database accesses. */
73 	sigemptyset(&blockset);
74 	sigaddset(&blockset, SIGINT);
75 	sigaddset(&blockset, SIGQUIT);
76 	sigaddset(&blockset, SIGTSTP);
77 
78 	openlog(NULL, LOG_ODELAY, LOG_AUTH);
79 
80 	cds.rlim_cur = 0;
81 	cds.rlim_max = 0;
82 	if (setrlimit(RLIMIT_CORE, &cds) < 0)
83 		syslog(LOG_ERR, "couldn't set core dump size to 0: %m");
84 
85 	(void)sigprocmask(SIG_BLOCK, &blockset, NULL);
86 	if (token_init(argv[0]) < 0) {
87 		syslog(LOG_ERR, "unknown token type");
88 		errx(1, "unknown token type");
89 	}
90 	(void)sigprocmask(SIG_UNBLOCK, &blockset, NULL);
91 
92 	while ((c = getopt(argc, argv, "ds:v:")) != -1)
93 		switch (c) {
94 		case 'd':		/* to remain undocumented */
95 			back = stdout;
96 			break;
97 		case 'v':
98 			break;
99 		case 's':	/* service */
100 			if (strcmp(optarg, "login") == 0)
101 				mode = 0;
102 			else if (strcmp(optarg, "challenge") == 0)
103 				mode = 1;
104 			else if (strcmp(optarg, "response") == 0)
105 				mode = 2;
106 			else {
107 				syslog(LOG_ERR, "%s: invalid service", optarg);
108 				exit(1);
109 			}
110 			break;
111 		default:
112 			syslog(LOG_ERR, "usage error");
113 			exit(1);
114 		}
115 
116 	switch (argc - optind) {
117 	case 2:
118 		class = argv[optind + 1];
119 	case 1:
120 		username = argv[optind];
121 		break;
122 	default:
123 		syslog(LOG_ERR, "usage error");
124 		exit(1);
125 	}
126 
127 
128 	if (back == NULL && (back = fdopen(3, "r+")) == NULL)  {
129 		syslog(LOG_ERR, "reopening back channel");
130 		exit(1);
131 	}
132 	if (mode == 2) {
133 		mode = 0;
134 		c = -1;
135 		while (++c < sizeof(challenge) &&
136 		    read(3, &challenge[c], 1) == 1) {
137 			if (challenge[c] == '\0' && ++mode == 2)
138 				break;
139 			if (challenge[c] == '\0' && mode == 1)
140 				pp = challenge + c + 1;
141 		}
142 		if (mode < 2) {
143 			syslog(LOG_ERR, "protocol error on back channel");
144 			exit(1);
145 		}
146 	} else {
147 		(void)sigprocmask(SIG_BLOCK, &blockset, NULL);
148 		tokenchallenge(username, challenge, sizeof(challenge),
149 		    tt->proper);
150 		(void)sigprocmask(SIG_UNBLOCK, &blockset, NULL);
151 		if (mode == 1) {
152 			fprintf(back, BI_VALUE " challenge %s\n",
153 			    auth_mkvalue(challenge));
154 			fprintf(back, BI_CHALLENGE "\n");
155 			exit(0);
156 		}
157 
158 		pp = readpassphrase(challenge, response, sizeof(response), 0);
159 		if (pp == NULL)
160 			exit(1);
161 		if (*pp == '\0') {
162 			char buf[64];
163 			snprintf(buf, sizeof(buf), "%s Response [echo on]: ",
164 			    tt->proper);
165 			pp = readpassphrase(buf, response, sizeof(response),
166 			    RPP_ECHO_ON);
167 			if (pp == NULL)
168 				exit(1);
169 		}
170 	}
171 
172 	(void)sigprocmask(SIG_BLOCK, &blockset, NULL);
173 	if (tokenverify(username, challenge, pp) == 0) {
174 		fprintf(back, BI_AUTH "\n");
175 
176 		if ((instance = strchr(username, '.'))) {
177 			*instance++ = 0;
178 			if (strcmp(instance, "root") == 0)
179 				fprintf(back, BI_ROOTOKAY "\n");
180 		}
181 		fprintf(back, BI_SECURE "\n");
182 		exit(0);
183 	}
184 
185 	fprintf(back, BI_REJECT "\n");
186 	exit(1);
187 }
188