passwd.c revision 1.15 1 /* $NetBSD: passwd.c,v 1.15 2000/01/26 01:18:48 aidan 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 #include <sys/cdefs.h>
37 #ifndef lint
38 __COPYRIGHT("@(#) 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 __RCSID("$NetBSD: passwd.c,v 1.15 2000/01/26 01:18:48 aidan 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 int use_kerberos;
66 int use_yp;
67 int yppwd;
68 int yflag;
69
70 extern char *__progname; /* from crt0.o */
71
72 int main __P((int, char **));
73
74 #ifdef YP
75 extern int _yp_check __P((char **)); /* buried deep inside libc */
76 #endif
77
78 int
79 main(argc, argv)
80 int argc;
81 char **argv;
82 {
83 extern int optind;
84 int ch;
85 char *username;
86 #if defined(KERBEROS)
87 char *iflag = 0, *rflag = 0;
88 #endif
89 #if defined(KERBEROS) || defined(KERBEROS5)
90 char *uflag = 0;
91 #endif
92
93 #if defined(KERBEROS) || defined(KERBEROS5)
94 if (strcmp(__progname, "kpasswd") == 0)
95 use_kerberos = 1;
96 else
97 use_kerberos = krb_check();
98 #endif
99 #ifdef YP
100 use_yp = _yp_check(NULL);
101 #endif
102
103 if (strcmp(__progname, "yppasswd") == 0) {
104 #ifdef YP
105 if (!use_yp)
106 errx(1, "YP not in use.");
107 use_kerberos = 0;
108 yppwd = 1;
109 #else
110 errx(1, "YP support not compiled in.");
111 #endif
112 }
113
114 while ((ch = getopt(argc, argv, "lkyi:r:u:")) != -1)
115 switch (ch) {
116 case 'l': /* change local password file */
117 if (yppwd)
118 usage();
119 use_kerberos = 0;
120 use_yp = 0;
121 break;
122 #ifdef KERBEROS
123 case 'i':
124 iflag = optarg;
125 break;
126 case 'r':
127 rflag = optarg;
128 break;
129 #endif
130 #if defined(KERBEROS) || defined(KERBEROS5)
131 case 'u':
132 uflag = optarg;
133 break;
134 #endif
135 case 'k': /* change Kerberos password */
136 #if defined(KERBEROS) || defined(KERBEROS5)
137 if (yppwd)
138 usage();
139 use_kerberos = 1;
140 use_yp = 0;
141 break;
142 #endif
143 #ifndef KERBEROS
144 case 'i':
145 case 'r':
146 errx(1, "Kerberos4 support not compiled in.");
147 #endif
148 #if !defined(KERBEROS) && !defined(KERBEROS5)
149 case 'u':
150 errx(1, "Kerberos support not compiled in.");
151 #endif
152 case 'y': /* change YP password */
153 #ifdef YP
154 if (yppwd)
155 usage();
156 if (!use_yp)
157 errx(1, "YP not in use.");
158 use_kerberos = 0;
159 yflag = 1;
160 break;
161 #else
162 errx(1, "YP support not compiled in.");
163 #endif
164 default:
165 usage();
166 }
167
168 argc -= optind;
169 argv += optind;
170
171 username = getlogin();
172 if (username == NULL)
173 errx(1, "who are you ??");
174
175 switch(argc) {
176 case 0:
177 break;
178 case 1:
179 #ifdef KERBEROS5
180 if (use_kerberos && strcmp(argv[0], username)) {
181 errx(1, "%s\n\t%s\n%s\n",
182 "to change another user's Kerberos password, do",
183 "\"kinit <user>; passwd; kdestroy\";",
184 "to change a user's local passwd, use\
185 \"passwd -l <user>\"");
186 }
187 #endif
188 username = argv[0];
189 break;
190 default:
191 usage();
192 exit(1);
193 }
194
195 #if defined(KERBEROS5)
196 if (use_kerberos)
197 exit(kadm5_passwd(username));
198 #elif defined(KERBEROS)
199 if (uflag && (iflag || rflag))
200 errx(1, "-u cannot be used with -r or -i");
201
202 if (use_kerberos)
203 exit(kadm_passwd(username, iflag, rflag, uflag));
204 #endif
205 #ifdef YP
206 if (use_yp)
207 exit(yp_passwd(username));
208 #endif
209 exit(local_passwd(username));
210 }
211
212 void
213 usage()
214 {
215
216 if (yppwd)
217 fprintf(stderr, "usage: %s user\n", __progname);
218 else
219 fprintf(stderr, "usage: %s [-l] [-k] [-y] [-i instance] [-r realm] [-u fullname] user\n", __progname);
220 exit(1);
221 }
222