1169691Skan/*
2169691Skan * Copyright (c) 1988, 1993, 1994
3169691Skan *	The Regents of the University of California.  All rights reserved.
4169691Skan *
5169691Skan * Redistribution and use in source and binary forms, with or without
6169691Skan * modification, are permitted provided that the following conditions
7169691Skan * are met:
8169691Skan * 1. Redistributions of source code must retain the above copyright
9169691Skan *    notice, this list of conditions and the following disclaimer.
10169691Skan * 2. Redistributions in binary form must reproduce the above copyright
11169691Skan *    notice, this list of conditions and the following disclaimer in the
12169691Skan *    documentation and/or other materials provided with the distribution.
13169691Skan * 3. All advertising materials mentioning features or use of this software
14169691Skan *    must display the following acknowledgement:
15169691Skan *	This product includes software developed by the University of
16169691Skan *	California, Berkeley and its contributors.
17169691Skan * 4. Neither the name of the University nor the names of its contributors
18169691Skan *    may be used to endorse or promote products derived from this software
19169691Skan *    without specific prior written permission.
20169691Skan *
21169691Skan * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22169691Skan * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23169691Skan * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24169691Skan * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25169691Skan * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26169691Skan * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27169691Skan * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28169691Skan * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29169691Skan * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30169691Skan * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31169691Skan * SUCH DAMAGE.
32169691Skan */
33169691Skan
34169691Skan#ifndef lint
35169691Skanstatic const char copyright[] =
36169691Skan"@(#) Copyright (c) 1988, 1993, 1994\n\
37169691Skan	The Regents of the University of California.  All rights reserved.\n";
38169691Skan#endif /* not lint */
39169691Skan
40169691Skan#ifndef lint
41169691Skan#if 0
42169691Skanstatic char sccsid[] = "@(#)passwd.c	8.3 (Berkeley) 4/2/94";
43169691Skan#endif
44169691Skanstatic const char rcsid[] =
45169691Skan  "$FreeBSD$";
46169691Skan#endif /* not lint */
47169691Skan
48169691Skan#include <sys/types.h>
49169691Skan
50169691Skan#include <err.h>
51169691Skan#include <errno.h>
52169691Skan#include <libutil.h>
53169691Skan#include <stdio.h>
54169691Skan#include <stdlib.h>
55169691Skan#include <string.h>
56169691Skan#include <unistd.h>
57169691Skan
58169691Skan#ifdef YP
59169691Skan#include <pwd.h>
60169691Skan#include <pw_yp.h>
61169691Skan#include <rpcsvc/yp.h>
62169691Skanint __use_yp = 0;
63169691Skanint yp_errno = YP_TRUE;
64169691Skanextern int yp_passwd( char * );
65169691Skan#endif
66169691Skan
67169691Skan#include "extern.h"
68169691Skan
69169691Skanstatic void usage(void);
70169691Skan
71int use_local_passwd = 0;
72
73int
74main(argc, argv)
75	int argc;
76	char **argv;
77{
78	int ch;
79	char *uname;
80
81#ifdef YP
82#define OPTIONS "d:h:lysfo"
83#else
84#define OPTIONS "l"
85#endif
86
87#ifdef YP
88	int res = 0;
89
90	if (strstr(argv[0], "yppasswd")) __use_yp = 1;
91#endif
92
93	while ((ch = getopt(argc, argv, OPTIONS)) != -1) {
94		switch (ch) {
95		case 'l':		/* change local password file */
96			use_local_passwd = 1;
97			break;
98#ifdef	YP
99		case 'y':			/* Change NIS password */
100			__use_yp = 1;
101			break;
102		case 'd':			/* Specify NIS domain. */
103#ifdef PARANOID
104			if (!getuid()) {
105#endif
106				yp_domain = optarg;
107				if (yp_server == NULL)
108					yp_server = "localhost";
109#ifdef PARANOID
110			} else {
111				warnx("only the super-user may use the -d flag");
112			}
113#endif
114			break;
115		case 'h':			/* Specify NIS server. */
116#ifdef PARANOID
117			if (!getuid()) {
118#endif
119				yp_server = optarg;
120#ifdef PARANOID
121			} else {
122				warnx("only the super-user may use the -h flag");
123			}
124#endif
125			break;
126		case 'o':
127			force_old++;
128			break;
129#endif
130		default:
131		case '?':
132			usage();
133		}
134	}
135
136	argc -= optind;
137	argv += optind;
138
139	if ((uname = getlogin()) == NULL)
140		err(1, "getlogin");
141
142	switch(argc) {
143	case 0:
144		break;
145	case 1:
146		uname = argv[0];
147		break;
148	default:
149		usage();
150	}
151
152#ifdef YP
153	/*
154	 * If NIS is turned on in the password database, use it, else punt.
155	 */
156	res = use_yp(uname, 0, 0);
157	if (res == USER_YP_ONLY) {
158		if (!use_local_passwd) {
159			exit(yp_passwd(uname));
160		} else {
161			/*
162			 * Reject -l flag if NIS is turned on and the user
163			 * doesn't exist in the local password database.
164			 */
165			errx(1, "unknown local user: %s", uname);
166		}
167	} else if (res == USER_LOCAL_ONLY) {
168		/*
169		 * Reject -y flag if user only exists locally.
170		 */
171		if (__use_yp)
172			errx(1, "unknown NIS user: %s", uname);
173	} else if (res == USER_YP_AND_LOCAL) {
174		if (!use_local_passwd && (yp_in_pw_file || __use_yp))
175			exit(yp_passwd(uname));
176	}
177#endif
178
179	exit(local_passwd(uname));
180}
181
182static void
183usage()
184{
185
186#ifdef	YP
187	(void)fprintf(stderr,
188		"usage: passwd [-l] [-y] [-o] [-d domain [-h host]] [user]\n");
189#else
190	(void)fprintf(stderr, "usage: passwd [-l] user\n");
191#endif
192	exit(1);
193}
194