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 "$FreeBSD: stable/9/release/picobsd/tinyware/passwd/passwd.c 238481 2012-07-15 11:39:35Z des $";
46 #endif /* not lint */
47
48 #include <sys/types.h>
49
50 #include <err.h>
51 #include <errno.h>
52 #include <libutil.h>
53 #include <stdio.h>
54 #include <stdlib.h>
55 #include <string.h>
56 #include <unistd.h>
57
58 #ifdef YP
59 #include <pwd.h>
60 #include <pw_yp.h>
61 #include <rpcsvc/yp.h>
62 int __use_yp = 0;
63 int yp_errno = YP_TRUE;
64 extern int yp_passwd( char * );
65 #endif
66
67 #include "extern.h"
68
69 static void usage(void);
70
71 int use_local_passwd = 0;
72
73 int
main(argc,argv)74 main(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
182 static void
usage()183 usage()
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