passwd.c revision 14212
1294113Sbapt/* 2241675Suqs * Copyright (c) 1988, 1993, 1994 3275432Sbapt * The Regents of the University of California. All rights reserved. 4279527Sbapt * 5241675Suqs * Redistribution and use in source and binary forms, with or without 6241675Suqs * modification, are permitted provided that the following conditions 7241675Suqs * are met: 8241675Suqs * 1. Redistributions of source code must retain the above copyright 9241675Suqs * notice, this list of conditions and the following disclaimer. 10241675Suqs * 2. Redistributions in binary form must reproduce the above copyright 11241675Suqs * notice, this list of conditions and the following disclaimer in the 12241675Suqs * documentation and/or other materials provided with the distribution. 13241675Suqs * 3. All advertising materials mentioning features or use of this software 14241675Suqs * must display the following acknowledgement: 15241675Suqs * This product includes software developed by the University of 16241675Suqs * California, Berkeley and its contributors. 17241675Suqs * 4. Neither the name of the University nor the names of its contributors 18241675Suqs * may be used to endorse or promote products derived from this software 19241675Suqs * without specific prior written permission. 20275432Sbapt * 21275432Sbapt * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 22241675Suqs * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23241675Suqs * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24241675Suqs * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 25241675Suqs * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26241675Suqs * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27241675Suqs * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28241675Suqs * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29241675Suqs * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30274880Sbapt * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31241675Suqs * SUCH DAMAGE. 32241675Suqs * 33241675Suqs * $id$ 34241675Suqs * 35275432Sbapt */ 36275432Sbapt 37241675Suqs#ifndef lint 38275432Sbaptstatic const char copyright[] = 39275432Sbapt"@(#) Copyright (c) 1988, 1993, 1994\n\ 40275432Sbapt The Regents of the University of California. All rights reserved.\n"; 41275432Sbapt#endif /* not lint */ 42275432Sbapt 43275432Sbapt#ifndef lint 44275432Sbaptstatic const char sccsid[] = "From: @(#)passwd.c 8.3 (Berkeley) 4/2/94"; 45275432Sbaptstatic const char rcsid[] = 46275432Sbapt "$Id: passwd.c,v 1.7 1995/12/16 09:45:15 markm Exp $"; 47275432Sbapt#endif /* not lint */ 48275432Sbapt 49275432Sbapt#include <err.h> 50275432Sbapt#include <errno.h> 51275432Sbapt#include <stdio.h> 52275432Sbapt#include <stdlib.h> 53275432Sbapt#include <unistd.h> 54275432Sbapt#include <string.h> 55275432Sbapt 56275432Sbapt#ifdef YP 57275432Sbapt#include <pwd.h> 58275432Sbapt#include <pw_yp.h> 59275432Sbapt#include <rpcsvc/yp.h> 60275432Sbaptint __use_yp = 0; 61275432Sbaptint yp_errno = YP_TRUE; 62275432Sbaptextern int yp_passwd __P(( char * )); 63275432Sbapt#endif 64275432Sbapt 65275432Sbapt#ifdef KERBEROS 66275432Sbapt#include "krb.h" 67275432Sbapt#endif 68275432Sbapt 69275432Sbapt#include "extern.h" 70275432Sbapt 71275432Sbaptvoid usage __P((void)); 72275432Sbapt 73275432Sbaptint use_local_passwd = 0; 74275432Sbapt 75275432Sbaptint 76275432Sbaptmain(argc, argv) 77275432Sbapt int argc; 78275432Sbapt char **argv; 79275432Sbapt{ 80275432Sbapt int ch; 81275432Sbapt char *uname; 82275432Sbapt char *iflag = 0, *rflag = 0, *uflag = 0; 83275432Sbapt 84275432Sbapt#ifdef YP 85275432Sbapt#ifdef KERBEROS 86241675Suqs char realm[REALM_SZ]; 87241675Suqs#define OPTIONS "d:h:lysfoi:r:u:" 88275432Sbapt#else 89275432Sbapt#define OPTIONS "d:h:lysfo" 90275432Sbapt#endif 91275432Sbapt#else 92275432Sbapt#ifdef KERBEROS 93275432Sbapt char realm[REALM_SZ]; 94275432Sbapt#define OPTIONS "li:r:u:" 95275432Sbapt#else 96275432Sbapt#define OPTIONS "l" 97275432Sbapt#endif 98275432Sbapt#endif 99275432Sbapt 100275432Sbapt#ifdef YP 101275432Sbapt int res = 0; 102275432Sbapt 103275432Sbapt if (strstr(argv[0], "yppasswd")) __use_yp = 1; 104275432Sbapt#endif 105275432Sbapt 106275432Sbapt while ((ch = getopt(argc, argv, OPTIONS)) != EOF) { 107275432Sbapt switch (ch) { 108275432Sbapt case 'l': /* change local password file */ 109275432Sbapt use_local_passwd = 1; 110275432Sbapt break; 111275432Sbapt#ifdef KERBEROS 112275432Sbapt case 'i': 113275432Sbapt iflag = optarg; 114275432Sbapt break; 115275432Sbapt case 'r': 116275432Sbapt rflag = optarg; 117275432Sbapt break; 118275432Sbapt case 'u': 119275432Sbapt uflag = optarg; 120275432Sbapt break; 121275432Sbapt#endif /* KERBEROS */ 122275432Sbapt#ifdef YP 123275432Sbapt case 'y': /* Change NIS password */ 124275432Sbapt __use_yp = 1; 125275432Sbapt break; 126275432Sbapt case 'd': /* Specify NIS domain. */ 127275432Sbapt#ifdef PARANOID 128275432Sbapt if (!getuid()) { 129275432Sbapt#endif 130275432Sbapt yp_domain = optarg; 131275432Sbapt if (yp_server == NULL) 132275432Sbapt yp_server = "localhost"; 133275432Sbapt#ifdef PARANOID 134275432Sbapt } else { 135275432Sbapt warnx("Only the super-user may use the -d flag."); 136275432Sbapt } 137241675Suqs#endif 138241675Suqs break; 139241675Suqs case 'h': /* Specify NIS server. */ 140241675Suqs#ifdef PARANOID 141241675Suqs if (!getuid()) { 142241675Suqs#endif 143241675Suqs yp_server = optarg; 144241675Suqs#ifdef PARANOID 145241675Suqs } else { 146241675Suqs warnx("Only the super-user may use the -h flag."); 147241675Suqs } 148241675Suqs#endif 149241675Suqs break; 150241675Suqs case 'o': 151241675Suqs force_old++; 152241675Suqs break; 153241675Suqs#endif 154241675Suqs default: 155241675Suqs case '?': 156241675Suqs usage(); 157241675Suqs } 158241675Suqs } 159241675Suqs 160241675Suqs argc -= optind; 161241675Suqs argv += optind; 162241675Suqs 163241675Suqs if ((uname = getlogin()) == NULL) 164241675Suqs err(1, "getlogin"); 165241675Suqs 166241675Suqs switch(argc) { 167241675Suqs case 0: 168241675Suqs break; 169241675Suqs case 1: 170241675Suqs uname = argv[0]; 171241675Suqs break; 172241675Suqs default: 173241675Suqs usage(); 174241675Suqs } 175241675Suqs 176241675Suqs#ifdef YP 177241675Suqs /* 178241675Suqs * If NIS is turned on in the password database, use it, else punt. 179241675Suqs */ 180241675Suqs#ifdef KERBEROS 181241675Suqs if (__use_yp || (iflag == NULL && rflag == NULL && uflag == NULL)) { 182241675Suqs#endif 183241675Suqs res = use_yp(uname, 0, 0); 184241675Suqs if (res == USER_YP_ONLY || __use_yp) { 185241675Suqs if (!use_local_passwd) { 186241675Suqs exit(yp_passwd(uname)); 187241675Suqs } else { 188241675Suqs /* 189241675Suqs * Reject -l flag if NIS is turned on and the user 190241675Suqs * doesn't exist in the local password database. 191241675Suqs */ 192241675Suqs errx(1, "unknown local user: %s.", uname); 193241675Suqs } 194241675Suqs } else if (res == USER_LOCAL_ONLY) { 195241675Suqs /* 196241675Suqs * Reject -y flag if user only exists locally. 197280025Sbapt */ 198241675Suqs if (__use_yp) 199241675Suqs errx(1, "unknown NIS user: %s.", uname); 200241675Suqs } else if (res == USER_YP_AND_LOCAL) { 201241675Suqs if (!use_local_passwd) 202275432Sbapt exit(yp_passwd(uname)); 203241675Suqs } 204241675Suqs#ifdef KERBEROS 205241675Suqs } 206241675Suqs#endif 207275432Sbapt#endif 208275432Sbapt 209275432Sbapt if (!use_local_passwd) { 210275432Sbapt#ifdef KERBEROS 211275432Sbapt if(krb_get_lrealm(realm, 0) == KSUCCESS) { 212275432Sbapt fprintf(stderr, "realm %s\n", realm); 213275432Sbapt exit(krb_passwd(argv[0], iflag, rflag, uflag)); 214275432Sbapt } 215275432Sbapt#endif 216275432Sbapt } 217275432Sbapt exit(local_passwd(uname)); 218275432Sbapt} 219275432Sbapt 220275432Sbaptvoid 221275432Sbaptusage() 222275432Sbapt{ 223275432Sbapt 224275432Sbapt#ifdef YP 225275432Sbapt#ifdef KERBEROS 226275432Sbapt fprintf(stderr, 227275432Sbapt "usage: passwd [-l] [-i instance] [-r realm] [-u fullname]\n"); 228275432Sbapt fprintf(stderr, 229275432Sbapt " [-l] [-y] [-o] [-d domain [-h host]] [user]\n"); 230275432Sbapt#else 231275432Sbapt (void)fprintf(stderr, "usage: passwd [-l] [-y] [-o] [-d domain \ 232275432Sbapt[-h host]] [user] \n"); 233275432Sbapt#endif 234275432Sbapt#else 235275432Sbapt#ifdef KERBEROS 236275432Sbapt fprintf(stderr, 237275432Sbapt "usage: passwd [-l] [-i instance] [-r realm] [-u fullname] [user]\n"); 238275432Sbapt#else 239275432Sbapt (void)fprintf(stderr, "usage: passwd user\n"); 240275432Sbapt#endif 241275432Sbapt#endif 242275432Sbapt exit(1); 243275432Sbapt} 244275432Sbapt