Add the DragonFly cvs id and perform general cleanups on cvs/rcs/sccs ids. Most
[dragonfly.git] / usr.bin / passwd / passwd.c
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  * @(#) Copyright (c) 1988, 1993, 1994 The Regents of the University of California.  All rights reserved.
34  * @(#)passwd.c 8.3 (Berkeley) 4/2/94
35  * $FreeBSD: src/usr.bin/passwd/passwd.c,v 1.16.2.1 2001/03/12 10:48:08 assar Exp $
36  * $DragonFly: src/usr.bin/passwd/passwd.c,v 1.2 2003/06/17 04:29:30 dillon Exp $
37  */
38
39 #include <sys/types.h>
40
41 #include <err.h>
42 #include <errno.h>
43 #include <libutil.h>
44 #include <stdio.h>
45 #include <stdlib.h>
46 #include <string.h>
47 #include <unistd.h>
48
49 #ifdef YP
50 #include <pwd.h>
51 #include <pw_yp.h>
52 #include <rpcsvc/yp.h>
53 int __use_yp = 0;
54 int yp_errno = YP_TRUE;
55 extern int yp_passwd    __P(( char * ));
56 #endif
57
58 #ifdef KERBEROS
59 #include "krb.h"
60 #endif
61
62 #include "extern.h"
63
64 static void usage __P((void));
65
66 int use_local_passwd = 0;
67
68 int
69 main(argc, argv)
70         int argc;
71         char **argv;
72 {
73         int ch;
74         char *uname;
75 #ifdef KERBEROS
76         char *iflag = 0, *rflag = 0, *uflag = 0;
77         char *k;
78 #endif
79
80 #ifdef YP
81 #ifdef KERBEROS
82         char realm[REALM_SZ];
83 #define OPTIONS "d:h:lysfoi:r:u:"
84 #else
85 #define OPTIONS "d:h:lysfo"
86 #endif
87 #else
88 #ifdef KERBEROS
89         char realm[REALM_SZ];
90 #define OPTIONS "li:r:u:"
91 #else
92 #define OPTIONS "l"
93 #endif
94 #endif
95
96 #ifdef YP
97         int res = 0;
98
99         if (strstr(argv[0], "yppasswd")) __use_yp = 1;
100 #endif
101
102         while ((ch = getopt(argc, argv, OPTIONS)) != -1) {
103                 switch (ch) {
104                 case 'l':               /* change local password file */
105                         use_local_passwd = 1;
106                         break;
107 #ifdef KERBEROS
108                 case 'i':
109                         iflag = optarg;
110                         break;
111                 case 'r':
112                         rflag = optarg;
113                         break;
114                 case 'u':
115                         uflag = optarg;
116                         break;
117 #endif /* KERBEROS */
118 #ifdef  YP
119                 case 'y':                       /* Change NIS password */
120                         __use_yp = 1;
121                         break;
122                 case 'd':                       /* Specify NIS domain. */
123 #ifdef PARANOID
124                         if (!getuid()) {
125 #endif
126                                 yp_domain = optarg;
127                                 if (yp_server == NULL)
128                                         yp_server = "localhost";
129 #ifdef PARANOID
130                         } else {
131                                 warnx("only the super-user may use the -d flag");
132                         }
133 #endif
134                         break;
135                 case 'h':                       /* Specify NIS server. */
136 #ifdef PARANOID
137                         if (!getuid()) {
138 #endif
139                                 yp_server = optarg;
140 #ifdef PARANOID
141                         } else {
142                                 warnx("only the super-user may use the -h flag");
143                         }
144 #endif
145                         break;
146                 case 'o':
147                         force_old++;
148                         break;
149 #endif
150                 default:
151                 case '?':
152                         usage();
153                 }
154         }
155
156         argc -= optind;
157         argv += optind;
158
159         if ((uname = getlogin()) == NULL)
160                 err(1, "getlogin");
161
162         switch(argc) {
163         case 0:
164                 break;
165         case 1:
166                 uname = argv[0];
167                 break;
168         default:
169                 usage();
170         }
171
172 #ifdef YP
173         /*
174          * If NIS is turned on in the password database, use it, else punt.
175          */
176 #ifdef KERBEROS
177         if (__use_yp || (iflag == NULL && rflag == NULL && uflag == NULL)) {
178 #endif
179                 res = use_yp(uname, 0, 0);
180                 if (res == USER_YP_ONLY) {
181                         if (!use_local_passwd) {
182                                 exit(yp_passwd(uname));
183                         } else {
184                         /*
185                          * Reject -l flag if NIS is turned on and the user
186                          * doesn't exist in the local password database.
187                          */
188                                 errx(1, "unknown local user: %s", uname);
189                         }
190                 } else if (res == USER_LOCAL_ONLY) {
191                         /*
192                          * Reject -y flag if user only exists locally.
193                          */
194                         if (__use_yp)
195                                 errx(1, "unknown NIS user: %s", uname);
196                 } else if (res == USER_YP_AND_LOCAL) {
197                         if (!use_local_passwd && (yp_in_pw_file || __use_yp))
198                                 exit(yp_passwd(uname));
199                 }
200 #ifdef KERBEROS
201         }
202 #endif
203 #endif
204
205         if (!use_local_passwd) {
206 #ifdef  KERBEROS
207                 k = auth_getval("auth_list");
208                 if (k && strstr(k, "kerberos"))
209                 if(krb_get_lrealm(realm, 0) == KSUCCESS) {
210                         setuid(getuid());
211                         fprintf(stderr, "realm %s\n", realm);
212                         exit(krb_passwd(argv[0], iflag, rflag, uflag));
213                 }
214 #endif
215         }
216         exit(local_passwd(uname));
217 }
218
219 static void
220 usage()
221 {
222
223 #ifdef  YP
224 #ifdef  KERBEROS
225         fprintf(stderr, "%s\n%s\n",
226                 "usage: passwd [-l] [-i instance] [-r realm] [-u fullname]",
227                 "       passwd [-l] [-y] [-o] [-d domain [-h host]] [user]");
228 #else
229         (void)fprintf(stderr,
230                 "usage: passwd [-l] [-y] [-o] [-d domain [-h host]] [user]\n");
231 #endif
232 #else
233 #ifdef  KERBEROS
234         fprintf(stderr,
235                 "usage: passwd [-l] [-i instance] [-r realm] [-u fullname] [user]\n");
236 #else
237         (void)fprintf(stderr, "usage: passwd user\n");
238 #endif
239 #endif
240         exit(1);
241 }