xref: /freebsd/usr.bin/passwd/passwd.c (revision 17ee9d00bc1ae1e598c38f25826f861e4bc6c3ce)
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 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 static char sccsid[] = "From: @(#)passwd.c	8.3 (Berkeley) 4/2/94";
42 static const char rcsid[] =
43 	"$Id: passwd.c,v 1.2 1995/01/20 22:03:36 wollman Exp $";
44 #endif /* not lint */
45 
46 #include <err.h>
47 #include <errno.h>
48 #include <stdio.h>
49 #include <stdlib.h>
50 #include <unistd.h>
51 
52 #ifdef YP
53 #include <pwd.h>
54 #include <limits.h>
55 #include <db.h>
56 #include <fcntl.h>
57 #include <utmp.h>
58 #include <sys/types.h>
59 #include <sys/stat.h>
60 #include <sys/param.h>
61 #endif
62 
63 #ifdef KERBEROS
64 #include "krb.h"
65 #endif
66 
67 #include "extern.h"
68 
69 void	usage __P((void));
70 
71 int use_local_passwd = 0;
72 
73 #ifdef YP
74 #define PERM_SECURE (S_IRUSR|S_IWUSR)
75 int use_yp_passwd = 0, opt_shell = 0, opt_fullname = 0;
76 char *prog_name;
77 HASHINFO openinfo = {
78         4096,           /* bsize */
79         32,             /* ffactor */
80         256,            /* nelem */
81         2048 * 1024,    /* cachesize */
82         NULL,           /* hash */
83         0,              /* lorder */
84 };
85 #endif
86 
87 int
88 main(argc, argv)
89 	int argc;
90 	char **argv;
91 {
92 	int ch;
93 	char *uname;
94 	char *iflag = 0, *rflag = 0, *uflag = 0;
95 
96 #ifdef YP
97 #ifdef KERBEROS
98 	char realm[REALM_SZ];
99 #define OPTIONS "lysfi:r:u:"
100 #else
101 #define OPTIONS "lysf"
102 #endif
103 #else
104 #ifdef KERBEROS
105 	char realm[REALM_SZ];
106 #define OPTIONS "li:r:u:"
107 #else
108 #define OPTIONS "l"
109 #endif
110 #endif
111 
112 #ifdef YP
113 	DB *dbp;
114 	DBT key,data;
115 	char bf[UT_NAMESIZE + 2];
116 
117 	if (strstr(argv[0], (prog_name = "ypchpass")))
118 		use_yp_passwd = opt_shell = opt_fullname = 1;
119 	if (strstr(argv[0], (prog_name = "ypchsh"))) opt_shell = 1;
120 	if (strstr(argv[0], (prog_name = "ypchfn"))) opt_fullname = 1;
121 	if (strstr(argv[0], (prog_name = "yppasswd"))) use_yp_passwd = 1;
122 #endif
123 
124 	while ((ch = getopt(argc, argv, OPTIONS)) != EOF) {
125 		switch (ch) {
126 		case 'l':		/* change local password file */
127 			use_local_passwd = 1;
128 			break;
129 #ifdef KERBEROS
130 		case 'i':
131 			iflag = optarg;
132 			break;
133 		case 'r':
134 			rflag = optarg;
135 			break;
136 		case 'u':
137 			uflag = optarg;
138 			break;
139 #endif /* KERBEROS */
140 #ifdef	YP
141 		case 'y':			/* Change NIS password */
142 			use_yp_passwd = 1;
143 			break;
144 		case 's':			/* Change NIS shell field */
145 			opt_shell = 1;
146 			break;
147 		case 'f':			/* Change NIS GECOS field */
148 			opt_fullname = 1;
149 			break;
150 #endif
151 		default:
152 		case '?':
153 			usage();
154 		}
155 	}
156 
157 	argc -= optind;
158 	argv += optind;
159 
160 	if ((uname = getlogin()) == NULL)
161 		err(1, "getlogin");
162 
163 	switch(argc) {
164 	case 0:
165 		break;
166 	case 1:
167 		uname = argv[0];
168 		break;
169 	default:
170 		usage();
171 	}
172 
173 #ifdef YP
174 	/*
175 	 * If the user isn't in the local database file, he must
176 	 * be in the NIS database.
177 	 */
178 #ifdef KERBEROS
179 	if (!use_yp_passwd && !opt_shell && !opt_fullname &&
180 		iflag == NULL && rflag == NULL && uflag == NULL) {
181 #else
182 	if (!use_yp_passwd && !opt_shell && !opt_fullname) {
183 #endif
184 		if ((dbp = dbopen(_PATH_MP_DB, O_RDONLY, PERM_SECURE,
185 				DB_HASH, &openinfo)) == NULL)
186 			errx(1, "error opening database: %s.", _PATH_MP_DB);
187 
188 		bf[0] = _PW_KEYBYNAME;
189 		bcopy(uname, bf + 1, MIN(strlen(uname), UT_NAMESIZE));
190 		key.data = (u_char *)bf;
191 		key.size = strlen(uname) + 1;
192 		if ((dbp->get)(dbp,&key,&data,0))
193 			use_yp_passwd = 1;
194 		(dbp->close)(dbp);
195 	}
196 
197 	if (!use_local_passwd && (use_yp_passwd || opt_shell || opt_fullname))
198 		exit(yp_passwd(uname));
199 #endif
200 
201 	if (!use_local_passwd) {
202 #ifdef	KERBEROS
203 		if(krb_get_lrealm(realm, 0) == KSUCCESS) {
204 			fprintf(stderr, "realm %s\n", realm);
205 			exit(krb_passwd(argv[0], iflag, rflag, uflag));
206 		}
207 #endif
208 	}
209 #ifdef YP
210 	if (use_local_passwd && use_yp_passwd)
211 		errx(1,"unknown local user: %s.",uname);
212 #endif
213 	exit(local_passwd(uname));
214 }
215 
216 void
217 usage()
218 {
219 
220 #ifdef	YP
221 #ifdef	KERBEROS
222 	fprintf(stderr,
223 	 "usage: passwd [-l] [-i instance] [-r realm] [-u fullname]\n");
224 	fprintf(stderr,
225 	"        [-l] [-y] [-f] [-s] [user]\n");
226 #else
227 	(void)fprintf(stderr, "usage: passwd [-y] [-f] [-s] [user] \n");
228 #endif
229 #else
230 #ifdef	KERBEROS
231 	fprintf(stderr,
232 	 "usage: passwd [-l] [-i instance] [-r realm] [-u fullname] [user]\n");
233 #else
234 	(void)fprintf(stderr, "usage: passwd user\n");
235 #endif
236 #endif
237 	exit(1);
238 }
239