xref: /freebsd/usr.bin/passwd/passwd.c (revision a8445737e740901f5f2c8d24c12ef7fc8b00134e)
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.13 1997/07/31 06:57:47 charnier 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 #ifdef KERBEROS
82 	char *iflag = 0, *rflag = 0, *uflag = 0;
83 #endif
84 
85 #ifdef YP
86 #ifdef KERBEROS
87 	char realm[REALM_SZ];
88 #define OPTIONS "d:h:lysfoi:r:u:"
89 #else
90 #define OPTIONS "d:h:lysfo"
91 #endif
92 #else
93 #ifdef KERBEROS
94 	char realm[REALM_SZ];
95 #define OPTIONS "li:r:u:"
96 #else
97 #define OPTIONS "l"
98 #endif
99 #endif
100 
101 #ifdef YP
102 	int res = 0;
103 
104 	if (strstr(argv[0], "yppasswd")) __use_yp = 1;
105 #endif
106 
107 	while ((ch = getopt(argc, argv, OPTIONS)) != -1) {
108 		switch (ch) {
109 		case 'l':		/* change local password file */
110 			use_local_passwd = 1;
111 			break;
112 #ifdef KERBEROS
113 		case 'i':
114 			iflag = optarg;
115 			break;
116 		case 'r':
117 			rflag = optarg;
118 			break;
119 		case 'u':
120 			uflag = optarg;
121 			break;
122 #endif /* KERBEROS */
123 #ifdef	YP
124 		case 'y':			/* Change NIS password */
125 			__use_yp = 1;
126 			break;
127 		case 'd':			/* Specify NIS domain. */
128 #ifdef PARANOID
129 			if (!getuid()) {
130 #endif
131 				yp_domain = optarg;
132 				if (yp_server == NULL)
133 					yp_server = "localhost";
134 #ifdef PARANOID
135 			} else {
136 				warnx("only the super-user may use the -d flag");
137 			}
138 #endif
139 			break;
140 		case 'h':			/* Specify NIS server. */
141 #ifdef PARANOID
142 			if (!getuid()) {
143 #endif
144 				yp_server = optarg;
145 #ifdef PARANOID
146 			} else {
147 				warnx("only the super-user may use the -h flag");
148 			}
149 #endif
150 			break;
151 		case 'o':
152 			force_old++;
153 			break;
154 #endif
155 		default:
156 		case '?':
157 			usage();
158 		}
159 	}
160 
161 	argc -= optind;
162 	argv += optind;
163 
164 	if ((uname = getlogin()) == NULL)
165 		err(1, "getlogin");
166 
167 	switch(argc) {
168 	case 0:
169 		break;
170 	case 1:
171 		uname = argv[0];
172 		break;
173 	default:
174 		usage();
175 	}
176 
177 #ifdef YP
178 	/*
179 	 * If NIS is turned on in the password database, use it, else punt.
180 	 */
181 #ifdef KERBEROS
182 	if (__use_yp || (iflag == NULL && rflag == NULL && uflag == NULL)) {
183 #endif
184 		res = use_yp(uname, 0, 0);
185 		if (res == USER_YP_ONLY) {
186 			if (!use_local_passwd) {
187 				exit(yp_passwd(uname));
188 			} else {
189 			/*
190 			 * Reject -l flag if NIS is turned on and the user
191 			 * doesn't exist in the local password database.
192 			 */
193 				errx(1, "unknown local user: %s", uname);
194 			}
195 		} else if (res == USER_LOCAL_ONLY) {
196 			/*
197 			 * Reject -y flag if user only exists locally.
198 			 */
199 			if (__use_yp)
200 				errx(1, "unknown NIS user: %s", uname);
201 		} else if (res == USER_YP_AND_LOCAL) {
202 			if (!use_local_passwd && (yp_in_pw_file || __use_yp))
203 				exit(yp_passwd(uname));
204 		}
205 #ifdef KERBEROS
206 	}
207 #endif
208 #endif
209 
210 	if (!use_local_passwd) {
211 #ifdef	KERBEROS
212 		if(krb_get_lrealm(realm, 0) == KSUCCESS) {
213 			fprintf(stderr, "realm %s\n", realm);
214 			exit(krb_passwd(argv[0], iflag, rflag, uflag));
215 		}
216 #endif
217 	}
218 	exit(local_passwd(uname));
219 }
220 
221 static void
222 usage()
223 {
224 
225 #ifdef	YP
226 #ifdef	KERBEROS
227 	fprintf(stderr, "%s\n%s\n",
228 		"usage: passwd [-l] [-i instance] [-r realm] [-u fullname]",
229 		"       passwd [-l] [-y] [-o] [-d domain [-h host]] [user]");
230 #else
231 	(void)fprintf(stderr,
232 		"usage: passwd [-l] [-y] [-o] [-d domain [-h host]] [user]\n");
233 #endif
234 #else
235 #ifdef	KERBEROS
236 	fprintf(stderr,
237 		"usage: passwd [-l] [-i instance] [-r realm] [-u fullname] [user]\n");
238 #else
239 	(void)fprintf(stderr, "usage: passwd user\n");
240 #endif
241 #endif
242 	exit(1);
243 }
244