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