GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: libexec/login_lchpass/login_lchpass.c Lines: 0 42 0.0 %
Date: 2017-11-07 Branches: 0 21 0.0 %

Line Branch Exec Source
1
/*	$OpenBSD: login_lchpass.c,v 1.20 2016/09/03 10:48:15 gsoares 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_lchpass.c,v 1.4 1997/08/08 18:58:23 prb Exp $
35
 */
36
#include <sys/stat.h>
37
#include <sys/time.h>
38
#include <sys/resource.h>
39
#include <sys/file.h>
40
#include <sys/uio.h>
41
#include <sys/wait.h>
42
43
#include <err.h>
44
#include <errno.h>
45
#include <pwd.h>
46
#include <signal.h>
47
#include <stdio.h>
48
#include <stdlib.h>
49
#include <string.h>
50
#include <syslog.h>
51
#include <unistd.h>
52
#include <stdarg.h>
53
#include <login_cap.h>
54
#include <readpassphrase.h>
55
56
#define BACK_CHANNEL	3
57
58
int local_passwd(char *, int);
59
60
int
61
main(int argc, char *argv[])
62
{
63
	struct iovec iov[2];
64
	struct passwd *pwd;
65
	char *username = NULL, *hash = NULL;
66
	char oldpass[1024];
67
	struct rlimit rl;
68
	int c;
69
70
	iov[0].iov_base = BI_SILENT;
71
	iov[0].iov_len = sizeof(BI_SILENT) - 1;
72
	iov[1].iov_base = "\n";
73
	iov[1].iov_len = 1;
74
75
	rl.rlim_cur = 0;
76
	rl.rlim_max = 0;
77
	(void)setrlimit(RLIMIT_CORE, &rl);
78
79
	(void)setpriority(PRIO_PROCESS, 0, 0);
80
81
	openlog("login", LOG_ODELAY, LOG_AUTH);
82
83
	while ((c = getopt(argc, argv, "v:s:")) != -1)
84
		switch (c) {
85
		case 'v':
86
			break;
87
		case 's':	/* service */
88
			if (strcmp(optarg, "login") != 0) {
89
				syslog(LOG_ERR, "%s: invalid service", optarg);
90
				exit(1);
91
			}
92
			break;
93
		default:
94
			syslog(LOG_ERR, "usage error");
95
			exit(1);
96
		}
97
98
	switch (argc - optind) {
99
	case 2:
100
		/* class is not used */
101
	case 1:
102
		username = argv[optind];
103
		break;
104
	default:
105
		syslog(LOG_ERR, "usage error");
106
		exit(1);
107
	}
108
109
	pwd = getpwnam_shadow(username);
110
	if (pwd) {
111
		if (pwd->pw_uid == 0) {
112
			syslog(LOG_ERR, "attempted root password change");
113
			pwd = NULL;
114
		} else if (*pwd->pw_passwd == '\0') {
115
			syslog(LOG_ERR, "%s attempting to add password",
116
			    username);
117
			pwd = NULL;
118
		}
119
	}
120
121
	if (pwd)
122
		hash = pwd->pw_passwd;
123
124
	(void)setpriority(PRIO_PROCESS, 0, -4);
125
126
	(void)printf("Changing local password for %s.\n", username);
127
	if ((readpassphrase("Old Password:", oldpass, sizeof(oldpass),
128
		    RPP_ECHO_OFF)) == NULL)
129
		exit(1);
130
131
	if (crypt_checkpass(oldpass, hash) != 0) {
132
		explicit_bzero(oldpass, strlen(oldpass));
133
		exit(1);
134
	}
135
	explicit_bzero(oldpass, strlen(oldpass));
136
137
	/*
138
	 * We rely on local_passwd() to block signals during the
139
	 * critical section.
140
	 */
141
	local_passwd(pwd->pw_name, 1);
142
	(void)writev(BACK_CHANNEL, iov, 2);
143
	exit(0);
144
}