passwd.c revision 1.6 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[] = "from: @(#)passwd.c 5.5 (Berkeley) 7/6/91";*/
42 static char rcsid[] = "$Id: passwd.c,v 1.6 1994/07/27 03:28:23 brezak Exp $";
43 #endif /* not lint */
44
45 #include <stdio.h>
46 #include <unistd.h>
47
48 /*
49 * Note on configuration:
50 * Generally one would not use both Kerberos and YP
51 * to maintain passwords.
52 *
53 */
54
55 int use_kerberos;
56 int use_yp;
57
58 #ifdef YP
59 int force_yp;
60 #endif
61
62 main(argc, argv)
63 int argc;
64 char **argv;
65 {
66 extern int optind;
67 register int ch;
68 char *username;
69 int status = 0;
70
71 #if defined(KERBEROS) || defined(KERBEROS5)
72 use_kerberos = 1;
73 #endif
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 #if defined(KERBEROS) || defined(KERBEROS5)
86 use_kerberos = 1;
87 use_yp = 0;
88 break;
89 #else
90 fprintf(stderr, "passwd: Kerberos not compiled in\n");
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 exit(1);
98 }
99 use_kerberos = 0;
100 use_yp = 1;
101 force_yp = 1;
102 break;
103 #else
104 fprintf(stderr, "passwd: YP not compiled in\n");
105 exit(1);
106 #endif
107 default:
108 usage();
109 exit(1);
110 }
111
112 argc -= optind;
113 argv += optind;
114
115 username = getlogin();
116 if (username == NULL) {
117 fprintf(stderr, "passwd: who are you ??\n");
118 exit(1);
119 }
120
121 switch(argc) {
122 case 0:
123 break;
124 case 1:
125 #if defined(KERBEROS) || defined(KERBEROS5)
126 if (use_kerberos && strcmp(argv[0], username)) {
127 (void)fprintf(stderr, "passwd: %s\n\t%s\n%s\n",
128 "to change another user's Kerberos password, do",
129 "\"kinit <user>; passwd; kdestroy\";",
130 "to change a user's local passwd, use \"passwd -l <user>\"");
131 exit(1);
132 }
133 #endif
134 username = argv[0];
135 break;
136 default:
137 usage();
138 exit(1);
139 }
140
141 #if defined(KERBEROS) || defined(KERBEROS5)
142 if (use_kerberos)
143 exit(krb_passwd());
144 #endif
145 #ifdef YP
146 if (force_yp || ((status = local_passwd(username)) && use_yp))
147 exit(yp_passwd(username));
148 exit(status);
149 #endif
150 exit(local_passwd(username));
151 }
152
153 usage()
154 {
155 fprintf(stderr, "usage: passwd [-l] [-k] [-y] user\n");
156 }
157