login_token.c revision 1.3
1/*	$OpenBSD: login_token.c,v 1.3 2001/10/24 13:06:36 mpech 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 <stdio.h>
45#include <syslog.h>
46#include <stdlib.h>
47#include <string.h>
48#include <unistd.h>
49#include <login_cap.h>
50#include <bsd_auth.h>
51
52#include "token.h"
53
54int
55main(argc, argv)
56	int argc;
57	char **argv;
58{
59	FILE *back = NULL;
60	char *class = 0;
61	char *username = 0;
62	char *instance;
63	char challenge[1024];
64	char response[1024];
65	char *pp = 0;
66	int c;
67	int mode = 0;
68	struct rlimit cds;
69
70	(void)signal(SIGQUIT, SIG_IGN);
71	(void)signal(SIGINT, SIG_IGN);
72	(void)setpriority(PRIO_PROCESS, 0, 0);
73
74	openlog(NULL, LOG_ODELAY, LOG_AUTH);
75
76	cds.rlim_cur = 0;
77	cds.rlim_max = 0;
78	if (setrlimit(RLIMIT_CORE, &cds) < 0)
79		syslog(LOG_ERR, "couldn't set core dump size to 0: %m");
80
81	if (token_init(argv[0]) < 0) {
82		syslog(LOG_ERR, "unknown token type");
83		errx(1, "unknown token type");
84	}
85
86	while ((c = getopt(argc, argv, "ds:v:")) != -1)
87		switch(c) {
88		case 'd':		/* to remain undocumented */
89			back = stdout;
90			break;
91		case 'v':
92			break;
93		case 's':	/* service */
94			if (strcmp(optarg, "login") == 0)
95				mode = 0;
96			else if (strcmp(optarg, "challenge") == 0)
97				mode = 1;
98			else if (strcmp(optarg, "response") == 0)
99				mode = 2;
100			else {
101				syslog(LOG_ERR, "%s: invalid service", optarg);
102				exit(1);
103			}
104			break;
105		default:
106			syslog(LOG_ERR, "usage error");
107			exit(1);
108		}
109
110	switch(argc - optind) {
111	case 2:
112		class = argv[optind + 1];
113	case 1:
114		username = argv[optind];
115		break;
116	default:
117		syslog(LOG_ERR, "usage error");
118		exit(1);
119	}
120
121
122	if (back == NULL && (back = fdopen(3, "r+")) == NULL)  {
123		syslog(LOG_ERR, "reopening back channel");
124		exit(1);
125	}
126	if (mode == 2) {
127		mode = 0;
128		c = -1;
129		while (++c < sizeof(challenge) &&
130		    read(3, &challenge[c], 1) == 1) {
131			if (challenge[c] == '\0' && ++mode == 2)
132				break;
133			if (challenge[c] == '\0' && mode == 1)
134				pp = challenge + c + 1;
135		}
136		if (mode < 2) {
137			syslog(LOG_ERR, "protocol error on back channel");
138			exit(1);
139		}
140	} else {
141		tokenchallenge(username, challenge, sizeof(challenge),
142		    tt->proper);
143		if (mode == 1) {
144			fprintf(back, BI_VALUE " challenge %s\n",
145			    auth_mkvalue(challenge));
146			fprintf(back, BI_CHALLENGE "\n");
147			exit(0);
148		}
149
150		pp = readpassphrase(challenge, response, sizeof(response), 0);
151		if (!pp || *pp == '\0') {
152			char buf[64];
153			snprintf(buf, sizeof(buf), "%s Response [echo on]: ",
154			    tt->proper);
155			pp = readpassphrase(buf, response, sizeof(response),
156			    RPP_ECHO_ON);
157		}
158	}
159
160	if (tokenverify(username, challenge, pp) == 0) {
161		fprintf(back, BI_AUTH "\n");
162
163		if ((instance = strchr(username, '.'))) {
164			*instance++ = 0;
165			if (strcmp(instance, "root") == 0)
166				fprintf(back, BI_ROOTOKAY "\n");
167		}
168		fprintf(back, BI_SECURE "\n");
169		exit(0);
170	}
171
172	fprintf(back, BI_REJECT "\n");
173	exit(1);
174}
175