xref: /netbsd-src/usr.bin/passwd/passwd.c (revision cda4f8f6ee55684e8d311b86c99ea59191e6b74f)
1 /*
2  * Copyright (c) 1988 The Regents of the University of California.
3  * 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 char copyright[] =
36 "@(#) Copyright (c) 1988 The Regents of the University of California.\n\
37  All rights reserved.\n";
38 #endif /* not lint */
39 
40 #ifndef lint
41 static char sccsid[] = "@(#)passwd.c	5.5 (Berkeley) 7/6/91";
42 #endif /* not lint */
43 
44 #include <stdio.h>
45 #include <unistd.h>
46 
47 /*
48  * Note on configuration:
49  *      Generally one would not use both Kerberos and YP
50  *      to maintain passwords.
51  *
52  */
53 #ifdef KERBEROS
54 int use_kerberos = 1;
55 #else
56 int use_kerberos = 0;
57 #endif
58 #ifdef YP
59 int force_yp = 0;
60 int use_yp;
61 #else
62 int use_yp = 0;
63 #endif
64 
65 main(argc, argv)
66 	int argc;
67 	char **argv;
68 {
69 	extern int optind;
70 	register int ch;
71 	char *uname;
72         int status;
73 
74 #ifdef	YP
75         use_yp = _yp_check(NULL);
76 #endif
77 
78 	while ((ch = getopt(argc, argv, "lky")) != EOF)
79 		switch (ch) {
80 		case 'l':		/* change local password file */
81 			use_kerberos = 0;
82 			use_yp = 0;
83 			break;
84 		case 'k':		/* change Kerberos password */
85 #ifdef	KERBEROS
86 			use_kerberos = 1;
87 			use_yp = 0;
88 			break;
89 #else
90                         usage();
91                         exit(1);
92 #endif
93 		case 'y':		/* change YP password */
94 #ifdef	YP
95                         if (!use_yp) {
96                                 fprintf(stderr, "passwd: YP not in use.\n");
97                                 usage();
98                                 exit(1);
99                         }
100 
101                         /* XXX Maybe just exec yppasswd ?? */
102 			use_kerberos = 0;
103 			use_yp = 1;
104                         force_yp = 1;
105 			break;
106 #else
107                         usage();
108                         exit(1);
109 #endif
110 		default:
111 		case '?':
112 			usage();
113 			exit(1);
114 		}
115 
116 	argc -= optind;
117 	argv += optind;
118 
119 	uname = getlogin();
120         if (uname == NULL) {
121                 fprintf(stderr, "passwd: who are you ??\n");
122                 exit(1);
123         }
124 
125 	switch(argc) {
126 	case 0:
127 		break;
128 	case 1:
129 #ifdef	KERBEROS
130 		if (use_kerberos && strcmp(argv[1], uname)) {
131 			(void)fprintf(stderr, "passwd: %s\n\t%s\n%s\n",
132 "to change another user's Kerberos password, do",
133 "\"kinit user; passwd; kdestroy\";",
134 "to change a user's local passwd, use \"passwd -l user\"");
135 			exit(1);
136 		}
137 #endif
138 		uname = argv[0];
139 		break;
140 	default:
141 		usage();
142 		exit(1);
143 	}
144 
145 #ifdef	KERBEROS
146 	if (use_kerberos)
147 		exit(krb_passwd());
148 #endif
149 #ifdef	YP
150         if (force_yp || ((status = local_passwd(uname)) && use_yp))
151 		exit(yp_passwd(uname));
152         exit(status);
153 #endif
154 	exit(local_passwd(uname));
155 }
156 
157 usage()
158 {
159 #if	defined(KERBEROS) && defined(YP)
160 	(void)fprintf(stderr, "usage: passwd [-l] [-k] [-y] user\n");
161 #else	/* !(KERBEROS && YP) */
162 #if	defined(KERBEROS) || defined(YP)
163 	(void)fprintf(stderr, "usage: passwd [-l] user\n");
164 #else
165 	(void)fprintf(stderr, "usage: passwd user\n");
166 #endif
167 #endif	/* KERBEROS && YP */
168 }
169