GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: libexec/login_passwd/login.c Lines: 0 61 0.0 %
Date: 2017-11-07 Branches: 0 51 0.0 %

Line Branch Exec Source
1
/*	$OpenBSD: login.c,v 1.16 2016/09/03 11:24:40 tedu Exp $	*/
2
3
/*-
4
 * Copyright (c) 1995 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_passwd.c,v 1.11 1997/08/08 18:58:24 prb Exp $
35
 */
36
37
#include "common.h"
38
39
FILE *back = NULL;
40
41
int
42
main(int argc, char **argv)
43
{
44
	int opt, mode = 0, ret, lastchance = 0;
45
	char *username, *password = NULL;
46
	char pbuf[1024];
47
	char response[1024];
48
	int arg_login = 0, arg_notickets = 0;
49
	char invokinguser[LOGIN_NAME_MAX];
50
	char *wheel = NULL, *class = NULL;
51
52
	invokinguser[0] = '\0';
53
54
	setpriority(PRIO_PROCESS, 0, 0);
55
56
	openlog(NULL, LOG_ODELAY, LOG_AUTH);
57
58
	while ((opt = getopt(argc, argv, "ds:v:")) != -1) {
59
		switch (opt) {
60
		case 'd':
61
			back = stdout;
62
			break;
63
		case 's':	/* service */
64
			if (strcmp(optarg, "login") == 0)
65
				mode = MODE_LOGIN;
66
			else if (strcmp(optarg, "challenge") == 0)
67
				mode = MODE_CHALLENGE;
68
			else if (strcmp(optarg, "response") == 0)
69
				mode = MODE_RESPONSE;
70
			else {
71
				syslog(LOG_ERR, "%s: invalid service", optarg);
72
				exit(1);
73
			}
74
			break;
75
		case 'v':
76
			if (strncmp(optarg, "wheel=", 6) == 0)
77
				wheel = optarg + 6;
78
			else if (strncmp(optarg, "lastchance=", 11) == 0)
79
				lastchance = (strcmp(optarg + 11, "yes") == 0);
80
			else if (strcmp(optarg, "login=yes") == 0)
81
				arg_login = 1;
82
			else if (strcmp(optarg, "notickets=yes") == 0)
83
				arg_notickets = 1;
84
			else if (strncmp(optarg, "invokinguser=", 13) == 0)
85
				snprintf(invokinguser, sizeof(invokinguser),
86
				    "%s", &optarg[13]);
87
			/* Silently ignore unsupported variables */
88
			break;
89
		default:
90
			syslog(LOG_ERR, "usage error1");
91
			exit(1);
92
		}
93
	}
94
95
	switch (argc - optind) {
96
	case 2:
97
		class = argv[optind + 1];
98
		/*FALLTHROUGH*/
99
	case 1:
100
		username = argv[optind];
101
		break;
102
	default:
103
		syslog(LOG_ERR, "usage error2");
104
		exit(1);
105
	}
106
107
	if (back == NULL && (back = fdopen(3, "r+")) == NULL) {
108
		syslog(LOG_ERR, "reopening back channel: %m");
109
		exit(1);
110
	}
111
112
	/*
113
	 * Read password, either as from the terminal or if the
114
	 * response mode is active from the caller program.
115
	 *
116
	 * XXX  This is completely ungrokkable, and should be rewritten.
117
	 */
118
	switch (mode) {
119
	case MODE_RESPONSE: {
120
		int count;
121
		mode = 0;
122
		count = -1;
123
		while (++count < sizeof(response) &&
124
		    read(3, &response[count], 1) == 1) {
125
			if (response[count] == '\0' && ++mode == 2)
126
				break;
127
			if (response[count] == '\0' && mode == 1) {
128
				password = response + count + 1;
129
			}
130
		}
131
		if (mode < 2) {
132
			syslog(LOG_ERR, "protocol error on back channel");
133
			exit(1);
134
		}
135
		break;
136
	}
137
138
	case MODE_LOGIN:
139
		password = readpassphrase("Password:", pbuf, sizeof(pbuf), RPP_ECHO_OFF);
140
		break;
141
	case MODE_CHALLENGE:
142
		fprintf(back, BI_AUTH "\n");
143
		exit(0);
144
		break;
145
	default:
146
		syslog(LOG_ERR, "%d: unknown mode", mode);
147
		exit(1);
148
		break;
149
	}
150
151
	ret = AUTH_FAILED;
152
#ifdef PASSWD
153
	if (ret != AUTH_OK)
154
		ret = pwd_login(username, password, wheel, lastchance, class);
155
#endif
156
157
	if (password != NULL)
158
		explicit_bzero(password, strlen(password));
159
	if (ret != AUTH_OK)
160
		fprintf(back, BI_REJECT "\n");
161
162
	closelog();
163
164
	exit(0);
165
}