passwd.c revision 1.11 1 /* $NetBSD: passwd.c,v 1.11 1997/01/07 04:08:18 tls Exp $ */
2
3 /*
4 * Copyright (c) 1988, 1993, 1994
5 * The Regents of the University of California. All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by the University of
18 * California, Berkeley and its contributors.
19 * 4. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
34 */
35
36 #ifndef lint
37 static char copyright[] =
38 "@(#) Copyright (c) 1988, 1993, 1994\n\
39 The Regents of the University of California. All rights reserved.\n";
40 #endif /* not lint */
41
42 #ifndef lint
43 #if 0
44 static char sccsid[] = "from: @(#)passwd.c 8.3 (Berkeley) 4/2/94";
45 #else
46 static char rcsid[] = "$NetBSD: passwd.c,v 1.11 1997/01/07 04:08:18 tls Exp $";
47 #endif
48 #endif /* not lint */
49
50 #include <err.h>
51 #include <stdio.h>
52 #include <string.h>
53 #include <unistd.h>
54
55 #include "extern.h"
56
57 void usage __P((void));
58
59 /*
60 * Note on configuration:
61 * Generally one would not use both Kerberos and YP
62 * to maintain passwords.
63 *
64 */
65
66 int use_kerberos;
67 int use_yp;
68 int yppwd;
69 int yflag;
70
71 extern char *__progname; /* from crt0.o */
72
73 #ifdef YP
74 extern int _yp_check __P((char **)); /* buried deep inside libc */
75 #endif
76
77 main(argc, argv)
78 int argc;
79 char **argv;
80 {
81 extern int optind;
82 int ch;
83 char *username;
84 char *iflag = 0, *rflag = 0, *uflag = 0;
85
86 #if defined(KERBEROS) || defined(KERBEROS5)
87 if (strcmp(__progname, "kpasswd") == 0)
88 use_kerberos = 1;
89 else
90 use_kerberos = krb_check();
91 #endif
92 #ifdef YP
93 use_yp = _yp_check(NULL);
94 #endif
95
96 if (strcmp(__progname, "yppasswd") == 0) {
97 #ifdef YP
98 if (!use_yp)
99 errx(1, "YP not in use.");
100 use_kerberos = 0;
101 yppwd = 1;
102 #else
103 errx(1, "YP support not compiled in.");
104 #endif
105 }
106
107
108 while ((ch = getopt(argc, argv, "lkyi:r:u:")) != -1)
109 switch (ch) {
110 case 'l': /* change local password file */
111 if (yppwd)
112 usage();
113 use_kerberos = 0;
114 use_yp = 0;
115 break;
116 #ifdef KERBEROS
117 case 'i':
118 iflag = optarg;
119 break;
120 case 'r':
121 rflag = optarg;
122 break;
123 case 'u':
124 uflag = optarg;
125 break;
126 #endif
127 case 'k': /* change Kerberos password */
128 #if defined(KERBEROS) || defined(KERBEROS5)
129 if (yppwd)
130 usage();
131 use_kerberos = 1;
132 use_yp = 0;
133 break;
134 #else
135 errx(1, "Kerberos support not compiled in.");
136 #endif
137 case 'y': /* change YP password */
138 #ifdef YP
139 if (yppwd)
140 usage();
141 if (!use_yp)
142 errx(1, "YP not in use.");
143 use_kerberos = 0;
144 yflag = 1;
145 break;
146 #else
147 errx(1, "YP support not compiled in.");
148 #endif
149 default:
150 usage();
151 }
152
153 argc -= optind;
154 argv += optind;
155
156 username = getlogin();
157 if (username == NULL)
158 errx(1, "who are you ??");
159
160 switch(argc) {
161 case 0:
162 break;
163 case 1:
164 #ifdef KERBEROS5
165 if (use_kerberos && strcmp(argv[0], username)) {
166 errx(1, "%s\n\t%s\n%s\n",
167 "to change another user's Kerberos password, do",
168 "\"kinit <user>; passwd; kdestroy\";",
169 "to change a user's local passwd, use\
170 \"passwd -l <user>\"");
171 }
172 #endif
173 username = argv[0];
174 break;
175 default:
176 usage();
177 exit(1);
178 }
179
180 #if defined(KERBEROS) || defined(KERBEROS5)
181 if (use_kerberos)
182 exit(kadm_passwd(username, iflag, rflag, uflag));
183 #else
184 #ifdef KERBEROS5
185 if (use_kerberos)
186 exit(krb_passwd());
187 #endif
188 #endif
189 #ifdef YP
190 if (use_yp)
191 exit(yp_passwd(username));
192 #endif
193 exit(local_passwd(username));
194 }
195
196 void
197 usage()
198 {
199
200 if (yppwd)
201 fprintf(stderr, "usage: %s user\n", __progname);
202 else
203 fprintf(stderr, "usage: %s [-l] [-k] [-y] [-i instance] [-r realm] [-u fullname] user\n", __progname);
204 exit(1);
205 }
206