xref: /freebsd/usr.bin/passwd/passwd.c (revision 7e19b1ec)
1 /*
2  * Copyright (c) 1988, 1993, 1994
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. All advertising materials mentioning features or use of this software
14  *    must display the following acknowledgement:
15  *	This product includes software developed by the University of
16  *	California, Berkeley and its contributors.
17  * 4. Neither the name of the University nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  */
33 
34 #ifndef lint
35 static const char copyright[] =
36 "@(#) Copyright (c) 1988, 1993, 1994\n\
37 	The Regents of the University of California.  All rights reserved.\n";
38 #endif /* not lint */
39 
40 #ifndef lint
41 #if 0
42 static char sccsid[] = "@(#)passwd.c	8.3 (Berkeley) 4/2/94";
43 #endif
44 static const char rcsid[] =
45 	"$Id: passwd.c,v 1.12 1997/03/29 04:31:27 imp Exp $";
46 #endif /* not lint */
47 
48 #include <err.h>
49 #include <errno.h>
50 #include <stdio.h>
51 #include <stdlib.h>
52 #include <string.h>
53 #include <unistd.h>
54 
55 #ifdef YP
56 #include <pwd.h>
57 #include <pw_yp.h>
58 #include <rpcsvc/yp.h>
59 int __use_yp = 0;
60 int yp_errno = YP_TRUE;
61 extern int yp_passwd	__P(( char * ));
62 #endif
63 
64 #ifdef KERBEROS
65 #include "krb.h"
66 #endif
67 
68 #include "extern.h"
69 
70 static void usage __P((void));
71 
72 int use_local_passwd = 0;
73 
74 int
75 main(argc, argv)
76 	int argc;
77 	char **argv;
78 {
79 	int ch;
80 	char *uname;
81 	char *iflag = 0, *rflag = 0, *uflag = 0;
82 
83 #ifdef YP
84 #ifdef KERBEROS
85 	char realm[REALM_SZ];
86 #define OPTIONS "d:h:lysfoi:r:u:"
87 #else
88 #define OPTIONS "d:h:lysfo"
89 #endif
90 #else
91 #ifdef KERBEROS
92 	char realm[REALM_SZ];
93 #define OPTIONS "li:r:u:"
94 #else
95 #define OPTIONS "l"
96 #endif
97 #endif
98 
99 #ifdef YP
100 	int res = 0;
101 
102 	if (strstr(argv[0], "yppasswd")) __use_yp = 1;
103 #endif
104 
105 	while ((ch = getopt(argc, argv, OPTIONS)) != -1) {
106 		switch (ch) {
107 		case 'l':		/* change local password file */
108 			use_local_passwd = 1;
109 			break;
110 #ifdef KERBEROS
111 		case 'i':
112 			iflag = optarg;
113 			break;
114 		case 'r':
115 			rflag = optarg;
116 			break;
117 		case 'u':
118 			uflag = optarg;
119 			break;
120 #endif /* KERBEROS */
121 #ifdef	YP
122 		case 'y':			/* Change NIS password */
123 			__use_yp = 1;
124 			break;
125 		case 'd':			/* Specify NIS domain. */
126 #ifdef PARANOID
127 			if (!getuid()) {
128 #endif
129 				yp_domain = optarg;
130 				if (yp_server == NULL)
131 					yp_server = "localhost";
132 #ifdef PARANOID
133 			} else {
134 				warnx("only the super-user may use the -d flag");
135 			}
136 #endif
137 			break;
138 		case 'h':			/* Specify NIS server. */
139 #ifdef PARANOID
140 			if (!getuid()) {
141 #endif
142 				yp_server = optarg;
143 #ifdef PARANOID
144 			} else {
145 				warnx("only the super-user may use the -h flag");
146 			}
147 #endif
148 			break;
149 		case 'o':
150 			force_old++;
151 			break;
152 #endif
153 		default:
154 		case '?':
155 			usage();
156 		}
157 	}
158 
159 	argc -= optind;
160 	argv += optind;
161 
162 	if ((uname = getlogin()) == NULL)
163 		err(1, "getlogin");
164 
165 	switch(argc) {
166 	case 0:
167 		break;
168 	case 1:
169 		uname = argv[0];
170 		break;
171 	default:
172 		usage();
173 	}
174 
175 #ifdef YP
176 	/*
177 	 * If NIS is turned on in the password database, use it, else punt.
178 	 */
179 #ifdef KERBEROS
180 	if (__use_yp || (iflag == NULL && rflag == NULL && uflag == NULL)) {
181 #endif
182 		res = use_yp(uname, 0, 0);
183 		if (res == USER_YP_ONLY) {
184 			if (!use_local_passwd) {
185 				exit(yp_passwd(uname));
186 			} else {
187 			/*
188 			 * Reject -l flag if NIS is turned on and the user
189 			 * doesn't exist in the local password database.
190 			 */
191 				errx(1, "unknown local user: %s", uname);
192 			}
193 		} else if (res == USER_LOCAL_ONLY) {
194 			/*
195 			 * Reject -y flag if user only exists locally.
196 			 */
197 			if (__use_yp)
198 				errx(1, "unknown NIS user: %s", uname);
199 		} else if (res == USER_YP_AND_LOCAL) {
200 			if (!use_local_passwd && (yp_in_pw_file || __use_yp))
201 				exit(yp_passwd(uname));
202 		}
203 #ifdef KERBEROS
204 	}
205 #endif
206 #endif
207 
208 	if (!use_local_passwd) {
209 #ifdef	KERBEROS
210 		if(krb_get_lrealm(realm, 0) == KSUCCESS) {
211 			fprintf(stderr, "realm %s\n", realm);
212 			exit(krb_passwd(argv[0], iflag, rflag, uflag));
213 		}
214 #endif
215 	}
216 	exit(local_passwd(uname));
217 }
218 
219 static void
220 usage()
221 {
222 
223 #ifdef	YP
224 #ifdef	KERBEROS
225 	fprintf(stderr, "%s\n%s\n",
226 		"usage: passwd [-l] [-i instance] [-r realm] [-u fullname]",
227 		"       passwd [-l] [-y] [-o] [-d domain [-h host]] [user]");
228 #else
229 	(void)fprintf(stderr,
230 		"usage: passwd [-l] [-y] [-o] [-d domain [-h host]] [user]\n");
231 #endif
232 #else
233 #ifdef	KERBEROS
234 	fprintf(stderr,
235 		"usage: passwd [-l] [-i instance] [-r realm] [-u fullname] [user]\n");
236 #else
237 	(void)fprintf(stderr, "usage: passwd user\n");
238 #endif
239 #endif
240 	exit(1);
241 }
242