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