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