xref: /openbsd-src/libexec/login_reject/login_reject.c (revision a39bec83d1872d464994ac68ff3e5a7b6e617607)
1*a39bec83Smestre /*	$OpenBSD: login_reject.c,v 1.18 2021/10/23 19:08:48 mestre Exp $	*/
2b8a38d8eSmillert 
3b8a38d8eSmillert /*-
4b8a38d8eSmillert  * Copyright (c) 1995 Berkeley Software Design, Inc. All rights reserved.
5b8a38d8eSmillert  *
6b8a38d8eSmillert  * Redistribution and use in source and binary forms, with or without
7b8a38d8eSmillert  * modification, are permitted provided that the following conditions
8b8a38d8eSmillert  * are met:
9b8a38d8eSmillert  * 1. Redistributions of source code must retain the above copyright
10b8a38d8eSmillert  *    notice, this list of conditions and the following disclaimer.
11b8a38d8eSmillert  * 2. Redistributions in binary form must reproduce the above copyright
12b8a38d8eSmillert  *    notice, this list of conditions and the following disclaimer in the
13b8a38d8eSmillert  *    documentation and/or other materials provided with the distribution.
14b8a38d8eSmillert  * 3. All advertising materials mentioning features or use of this software
15b8a38d8eSmillert  *    must display the following acknowledgement:
16b8a38d8eSmillert  *      This product includes software developed by Berkeley Software Design,
17b8a38d8eSmillert  *      Inc.
18b8a38d8eSmillert  * 4. The name of Berkeley Software Design, Inc.  may not be used to endorse
19b8a38d8eSmillert  *    or promote products derived from this software without specific prior
20b8a38d8eSmillert  *    written permission.
21b8a38d8eSmillert  *
22b8a38d8eSmillert  * THIS SOFTWARE IS PROVIDED BY BERKELEY SOFTWARE DESIGN, INC. ``AS IS'' AND
23b8a38d8eSmillert  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24b8a38d8eSmillert  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25b8a38d8eSmillert  * ARE DISCLAIMED.  IN NO EVENT SHALL BERKELEY SOFTWARE DESIGN, INC. BE LIABLE
26b8a38d8eSmillert  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27b8a38d8eSmillert  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28b8a38d8eSmillert  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29b8a38d8eSmillert  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30b8a38d8eSmillert  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31b8a38d8eSmillert  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32b8a38d8eSmillert  * SUCH DAMAGE.
33b8a38d8eSmillert  *
34b8a38d8eSmillert  *	BSDI $From: login_reject.c,v 1.5 1996/08/22 20:43:11 prb Exp $
35b8a38d8eSmillert  */
36b8a38d8eSmillert 
37f4147939Sguenther #include <sys/resource.h>
38f4147939Sguenther 
39b8a38d8eSmillert #include <login_cap.h>
4077386583Sgsoares #include <readpassphrase.h>
41b8a38d8eSmillert #include <stdio.h>
42b8a38d8eSmillert #include <stdlib.h>
43b8a38d8eSmillert #include <string.h>
44b8a38d8eSmillert #include <syslog.h>
45b8a38d8eSmillert #include <unistd.h>
46b8a38d8eSmillert 
47b8a38d8eSmillert int
main(int argc,char * argv[])489f80dbcfSderaadt main(int argc, char *argv[])
49b8a38d8eSmillert {
50b8a38d8eSmillert 	struct rlimit rl;
5190afb38dSmillert 	FILE *back;
529d8a360cStedu 	char passbuf[1];
539f80dbcfSderaadt 	int mode = 0, c;
54b8a38d8eSmillert 
55b8a38d8eSmillert 	rl.rlim_cur = 0;
56b8a38d8eSmillert 	rl.rlim_max = 0;
57b8a38d8eSmillert 	(void)setrlimit(RLIMIT_CORE, &rl);
58b8a38d8eSmillert 
59b8a38d8eSmillert 	(void)setpriority(PRIO_PROCESS, 0, 0);
60b8a38d8eSmillert 
6119b364eaSmestre 	if (pledge("stdio rpath tty", NULL) == -1) {
62bd917b9eSmillert 		syslog(LOG_AUTH|LOG_ERR, "pledge: %m");
63bd917b9eSmillert 		exit(1);
64bd917b9eSmillert 	}
6556d66987Sderaadt 
66b8a38d8eSmillert 	openlog("login", LOG_ODELAY, LOG_AUTH);
67b8a38d8eSmillert 
68d52e83b8Smpech 	while ((c = getopt(argc, argv, "v:s:")) != -1)
69b8a38d8eSmillert 		switch (c) {
70b8a38d8eSmillert 		case 'v':
71b8a38d8eSmillert 			break;
72b8a38d8eSmillert 		case 's':	/* service */
73b8a38d8eSmillert 			if (strcmp(optarg, "login") == 0)
74b8a38d8eSmillert 				mode = 0;
75b8a38d8eSmillert 			else if (strcmp(optarg, "challenge") == 0)
76b8a38d8eSmillert 				mode = 1;
77b8a38d8eSmillert 			else if (strcmp(optarg, "response") == 0)
78b8a38d8eSmillert 				mode = 2;
79b8a38d8eSmillert 			else {
80b8a38d8eSmillert 				syslog(LOG_ERR, "%s: invalid service", optarg);
81b8a38d8eSmillert 				exit(1);
82b8a38d8eSmillert 			}
83b8a38d8eSmillert 			break;
84b8a38d8eSmillert 		default:
85b8a38d8eSmillert 			syslog(LOG_ERR, "usage error");
86b8a38d8eSmillert 			exit(1);
87b8a38d8eSmillert 		}
88b8a38d8eSmillert 
89b8a38d8eSmillert 	switch (argc - optind) {
90b8a38d8eSmillert 	case 2:
91b8a38d8eSmillert 	case 1:
92b8a38d8eSmillert 		break;
93b8a38d8eSmillert 	default:
94b8a38d8eSmillert 		syslog(LOG_ERR, "usage error");
95b8a38d8eSmillert 		exit(1);
96b8a38d8eSmillert 	}
97b8a38d8eSmillert 
98b8a38d8eSmillert 	if (!(back = fdopen(3, "r+")))  {
99b8a38d8eSmillert 		syslog(LOG_ERR, "reopening back channel: %m");
100b8a38d8eSmillert 		exit(1);
101b8a38d8eSmillert 	}
102b8a38d8eSmillert 	if (mode == 1) {
103b8a38d8eSmillert 		fprintf(back, BI_SILENT "\n");
104b8a38d8eSmillert 		exit(0);
105b8a38d8eSmillert 	}
106b8a38d8eSmillert 
107b8a38d8eSmillert 	if (mode == 2) {
108b8a38d8eSmillert 		mode = 0;
109b8a38d8eSmillert 		c = -1;
11023ae9128Stedu 		while (read(3, passbuf, 1) == 1) {
111b8a38d8eSmillert 			if (passbuf[0] == '\0' && ++mode == 2)
112b8a38d8eSmillert 				break;
113b8a38d8eSmillert 		}
114b8a38d8eSmillert 		if (mode < 2) {
115b8a38d8eSmillert 			syslog(LOG_ERR, "protocol error on back channel");
116b8a38d8eSmillert 			exit(1);
117b8a38d8eSmillert 		}
118b8a38d8eSmillert 	} else
11977386583Sgsoares 		readpassphrase("Password:", passbuf, sizeof(passbuf), 0);
120b8a38d8eSmillert 
1219d8a360cStedu 	crypt_checkpass("password", NULL);
122*a39bec83Smestre 	explicit_bzero(passbuf, sizeof(passbuf));
12390afb38dSmillert 
124b8a38d8eSmillert 	fprintf(back, BI_REJECT "\n");
125b8a38d8eSmillert 	exit(1);
126b8a38d8eSmillert }
127