1/*-
2 * SPDX-License-Identifier: BSD-3-Clause
3 *
4 * Copyright (c) 2002 Networks Associates Technologies, Inc.
5 * All rights reserved.
6 *
7 * This software was developed for the FreeBSD Project by ThinkSec AS and
8 * NAI Labs, the Security Research Division of Network Associates, Inc.
9 * under DARPA/SPAWAR contract N66001-01-C-8035 ("CBOSS"), as part of the
10 * DARPA CHATS research program.
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
15 * 1. Redistributions of source code must retain the above copyright
16 *    notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 *    notice, this list of conditions and the following disclaimer in the
19 *    documentation and/or other materials provided with the distribution.
20 * 3. The name of the author may not be used to endorse or promote
21 *    products derived from this software without specific prior written
22 *    permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * SUCH DAMAGE.
35 */
36
37#include <sys/cdefs.h>
38__FBSDID("$FreeBSD: stable/11/usr.bin/passwd/passwd.c 330449 2018-03-05 07:26:05Z eadler $");
39
40#include <sys/param.h>
41
42#include <err.h>
43#include <pwd.h>
44#include <stdio.h>
45#include <stdlib.h>
46#include <syslog.h>
47#include <unistd.h>
48
49#include <security/pam_appl.h>
50#include <security/openpam.h>
51
52static pam_handle_t *pamh;
53static struct pam_conv pamc = {
54	openpam_ttyconv,
55	NULL
56};
57
58static char	*yp_domain;
59static char	*yp_host;
60
61static void
62usage(void)
63{
64	fprintf(stderr, "usage: passwd [-ly] [-d domain] [-h host] [user]\n");
65	exit(1);
66}
67
68int
69main(int argc, char *argv[])
70{
71	char hostname[MAXHOSTNAMELEN];
72	struct passwd *pwd = NULL; /* Keep compiler happy. */
73	int o, pam_err;
74	uid_t uid;
75
76	while ((o = getopt(argc, argv, "d:h:loy")) != -1)
77		switch (o) {
78		case 'd':
79			yp_domain = optarg;
80			break;
81		case 'h':
82			yp_host = optarg;
83			break;
84		case 'l':
85		case 'o':
86		case 'y':
87			/* compatibility */
88			break;
89		default:
90			usage();
91		}
92
93	argc -= optind;
94	argv += optind;
95
96	uid = getuid();
97
98	switch (argc) {
99	case 0:
100		if ((pwd = getpwuid(uid)) == NULL)
101			errx(1, "who are you?");
102		break;
103	case 1:
104		if ((pwd = getpwnam(*argv)) == NULL)
105			errx(1, "%s: no such user", *argv);
106		break;
107	default:
108		usage();
109	}
110
111	if (uid != 0 && uid != pwd->pw_uid)
112		errx(1, "permission denied");
113
114	/* check where the user's from */
115	switch (pwd->pw_fields & _PWF_SOURCE) {
116	case _PWF_FILES:
117		fprintf(stderr, "Changing local password for %s\n",
118		    pwd->pw_name);
119		break;
120	case _PWF_NIS:
121		fprintf(stderr, "Changing NIS password for %s\n",
122		    pwd->pw_name);
123		break;
124	default:
125		/* XXX: Green men ought to be supported via PAM. */
126		errx(1,
127	  "Sorry, `passwd' can only change passwords for local or NIS users.");
128	}
129
130#define pam_check(func) do { \
131	if (pam_err != PAM_SUCCESS) { \
132		if (pam_err == PAM_AUTH_ERR || pam_err == PAM_PERM_DENIED || \
133		    pam_err == PAM_AUTHTOK_RECOVERY_ERR) \
134			warnx("sorry"); \
135		else \
136			warnx("%s(): %s", func, pam_strerror(pamh, pam_err)); \
137		goto end; \
138	} \
139} while (0)
140
141	/* initialize PAM */
142	pam_err = pam_start("passwd", pwd->pw_name, &pamc, &pamh);
143	pam_check("pam_start");
144
145	pam_err = pam_set_item(pamh, PAM_TTY, ttyname(STDERR_FILENO));
146	pam_check("pam_set_item");
147	gethostname(hostname, sizeof hostname);
148	pam_err = pam_set_item(pamh, PAM_RHOST, hostname);
149	pam_check("pam_set_item");
150	pam_err = pam_set_item(pamh, PAM_RUSER, getlogin());
151	pam_check("pam_set_item");
152
153	/* set YP domain and host */
154	pam_err = pam_set_data(pamh, "yp_domain", yp_domain, NULL);
155	pam_check("pam_set_data");
156	pam_err = pam_set_data(pamh, "yp_server", yp_host, NULL);
157	pam_check("pam_set_data");
158
159	/* set new password */
160	pam_err = pam_chauthtok(pamh, 0);
161	pam_check("pam_chauthtok");
162
163 end:
164	pam_end(pamh, pam_err);
165	exit(pam_err == PAM_SUCCESS ? 0 : 1);
166}
167