passwd.c revision 1.13 1 /* $NetBSD: passwd.c,v 1.13 1998/07/11 15:55:48 mrg 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.13 1998/07/11 15:55:48 mrg 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) || defined(KERBEROS5)
87 char *iflag = 0, *rflag = 0, *uflag = 0;
88
89 if (strcmp(__progname, "kpasswd") == 0)
90 use_kerberos = 1;
91 else
92 use_kerberos = krb_check();
93 #endif
94 #ifdef YP
95 use_yp = _yp_check(NULL);
96 #endif
97
98 if (strcmp(__progname, "yppasswd") == 0) {
99 #ifdef YP
100 if (!use_yp)
101 errx(1, "YP not in use.");
102 use_kerberos = 0;
103 yppwd = 1;
104 #else
105 errx(1, "YP support not compiled in.");
106 #endif
107 }
108
109 while ((ch = getopt(argc, argv, "lkyi:r:u:")) != -1)
110 switch (ch) {
111 case 'l': /* change local password file */
112 if (yppwd)
113 usage();
114 use_kerberos = 0;
115 use_yp = 0;
116 break;
117 #ifdef KERBEROS
118 case 'i':
119 iflag = optarg;
120 break;
121 case 'r':
122 rflag = optarg;
123 break;
124 case 'u':
125 uflag = optarg;
126 break;
127 #endif
128 case 'k': /* change Kerberos password */
129 #if defined(KERBEROS) || defined(KERBEROS5)
130 if (yppwd)
131 usage();
132 use_kerberos = 1;
133 use_yp = 0;
134 break;
135 #endif
136 #ifndef KERBEROS
137 case 'i':
138 case 'r':
139 case 'u':
140 errx(1, "Kerberos4 support not compiled in.");
141 #endif
142 case 'y': /* change YP password */
143 #ifdef YP
144 if (yppwd)
145 usage();
146 if (!use_yp)
147 errx(1, "YP not in use.");
148 use_kerberos = 0;
149 yflag = 1;
150 break;
151 #else
152 errx(1, "YP support not compiled in.");
153 #endif
154 default:
155 usage();
156 }
157
158 argc -= optind;
159 argv += optind;
160
161 username = getlogin();
162 if (username == NULL)
163 errx(1, "who are you ??");
164
165 switch(argc) {
166 case 0:
167 break;
168 case 1:
169 #ifdef KERBEROS5
170 if (use_kerberos && strcmp(argv[0], username)) {
171 errx(1, "%s\n\t%s\n%s\n",
172 "to change another user's Kerberos password, do",
173 "\"kinit <user>; passwd; kdestroy\";",
174 "to change a user's local passwd, use\
175 \"passwd -l <user>\"");
176 }
177 #endif
178 username = argv[0];
179 break;
180 default:
181 usage();
182 exit(1);
183 }
184
185 #if defined(KERBEROS) || defined(KERBEROS5)
186 if (use_kerberos)
187 exit(kadm_passwd(username, iflag, rflag, uflag));
188 #else
189 #ifdef KERBEROS5
190 if (use_kerberos)
191 exit(krb_passwd());
192 #endif
193 #endif
194 #ifdef YP
195 if (use_yp)
196 exit(yp_passwd(username));
197 #endif
198 exit(local_passwd(username));
199 }
200
201 void
202 usage()
203 {
204
205 if (yppwd)
206 fprintf(stderr, "usage: %s user\n", __progname);
207 else
208 fprintf(stderr, "usage: %s [-l] [-k] [-y] [-i instance] [-r realm] [-u fullname] user\n", __progname);
209 exit(1);
210 }
211