xref: /freebsd/usr.bin/passwd/passwd.c (revision e075ffc9)
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  * $id$
34  *
35  */
36 
37 #ifndef lint
38 static const char copyright[] =
39 "@(#) Copyright (c) 1988, 1993, 1994\n\
40 	The Regents of the University of California.  All rights reserved.\n";
41 #endif /* not lint */
42 
43 #ifndef lint
44 static const char sccsid[] = "From: @(#)passwd.c	8.3 (Berkeley) 4/2/94";
45 static const char rcsid[] =
46 	"$Id$";
47 #endif /* not lint */
48 
49 #include <err.h>
50 #include <errno.h>
51 #include <stdio.h>
52 #include <stdlib.h>
53 #include <unistd.h>
54 #include <string.h>
55 
56 #ifdef YP
57 #include <pwd.h>
58 #include <pw_yp.h>
59 char *prog_name;
60 int __use_yp = 0;
61 int yp_passwd(char *user);
62 #endif
63 
64 #ifdef KERBEROS
65 #include "krb.h"
66 #endif
67 
68 #include "extern.h"
69 
70 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 "lysfi:r:u:"
87 #else
88 #define OPTIONS "lysf"
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], (prog_name = "yppasswd"))) __use_yp = 1;
103 #endif
104 
105 	while ((ch = getopt(argc, argv, OPTIONS)) != EOF) {
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 #endif
126 		default:
127 		case '?':
128 			usage();
129 		}
130 	}
131 
132 	argc -= optind;
133 	argv += optind;
134 
135 	if ((uname = getlogin()) == NULL)
136 		err(1, "getlogin");
137 
138 	switch(argc) {
139 	case 0:
140 		break;
141 	case 1:
142 		uname = argv[0];
143 		break;
144 	default:
145 		usage();
146 	}
147 
148 #ifdef YP
149 	/*
150 	 * If NIS is turned on in the password database, use it, else punt.
151 	 */
152 #ifdef KERBEROS
153 	if (__use_yp || (iflag == NULL && rflag == NULL && uflag == NULL)) {
154 #endif
155 		res = use_yp(uname);
156 		if (res == USER_YP_ONLY) {
157 			if (!use_local_passwd) {
158 				exit(yp_passwd(uname));
159 			} else {
160 			/*
161 			 * Reject -l flag if NIS is turned on and the user
162 			 * doesn't exist in the local password database.
163 			 */
164 				errx(1, "unknown local user: %s.", uname);
165 			}
166 		} else if (res == USER_LOCAL_ONLY) {
167 			/*
168 			 * Reject -y flag if user only exists locally.
169 			 */
170 			if (__use_yp)
171 				errx(1, "unknown NIS user: %s.", uname);
172 		} else if (res == USER_YP_AND_LOCAL) {
173 			if (!use_local_passwd)
174 				exit(yp_passwd(uname));
175 		}
176 #ifdef KERBEROS
177 	}
178 #endif
179 #endif
180 
181 	if (!use_local_passwd) {
182 #ifdef	KERBEROS
183 		if(krb_get_lrealm(realm, 0) == KSUCCESS) {
184 			fprintf(stderr, "realm %s\n", realm);
185 			exit(krb_passwd(argv[0], iflag, rflag, uflag));
186 		}
187 #endif
188 	}
189 	exit(local_passwd(uname));
190 }
191 
192 void
193 usage()
194 {
195 
196 #ifdef	YP
197 #ifdef	KERBEROS
198 	fprintf(stderr,
199 	 "usage: passwd [-l] [-i instance] [-r realm] [-u fullname]\n");
200 	fprintf(stderr,
201 	"        [-l] [-y] [user]\n");
202 #else
203 	(void)fprintf(stderr, "usage: passwd [-l] [-y] [user] \n");
204 #endif
205 #else
206 #ifdef	KERBEROS
207 	fprintf(stderr,
208 	 "usage: passwd [-l] [-i instance] [-r realm] [-u fullname] [user]\n");
209 #else
210 	(void)fprintf(stderr, "usage: passwd user\n");
211 #endif
212 #endif
213 	exit(1);
214 }
215