Home | History | Annotate | Line # | Download | only in passwd
passwd.c revision 1.10
      1 /*	$NetBSD: passwd.c,v 1.10 1996/12/28 04:30:07 tls Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1988, 1993, 1994
      5  *	The Regents of the University of California.  All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  * 3. All advertising materials mentioning features or use of this software
     16  *    must display the following acknowledgement:
     17  *	This product includes software developed by the University of
     18  *	California, Berkeley and its contributors.
     19  * 4. Neither the name of the University nor the names of its contributors
     20  *    may be used to endorse or promote products derived from this software
     21  *    without specific prior written permission.
     22  *
     23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     33  * SUCH DAMAGE.
     34  */
     35 
     36 #ifndef lint
     37 static char copyright[] =
     38 "@(#) Copyright (c) 1988, 1993, 1994\n\
     39 	The Regents of the University of California.  All rights reserved.\n";
     40 #endif /* not lint */
     41 
     42 #ifndef lint
     43 #if 0
     44 static char sccsid[] = "from: @(#)passwd.c    8.3 (Berkeley) 4/2/94";
     45 #else
     46 static char rcsid[] = "$NetBSD: passwd.c,v 1.10 1996/12/28 04:30:07 tls Exp $";
     47 #endif
     48 #endif /* not lint */
     49 
     50 #include <err.h>
     51 #include <stdio.h>
     52 #include <string.h>
     53 #include <unistd.h>
     54 
     55 #include "extern.h"
     56 
     57 void	usage __P((void));
     58 
     59 /*
     60  * Note on configuration:
     61  *      Generally one would not use both Kerberos and YP
     62  *      to maintain passwords.
     63  *
     64  */
     65 
     66 int use_kerberos;
     67 int use_yp;
     68 int yppwd;
     69 int yflag;
     70 
     71 extern	char *__progname;		/* from crt0.o */
     72 
     73 #ifdef YP
     74 extern int _yp_check __P((char **));	/* buried deep inside libc */
     75 #endif
     76 
     77 main(argc, argv)
     78 	int argc;
     79 	char **argv;
     80 {
     81 	extern int optind;
     82 	int ch;
     83 	char *username;
     84 
     85 #if defined(KERBEROS) || defined(KERBEROS5)
     86 	use_kerberos = 1;
     87 #endif
     88 #ifdef	YP
     89 	use_yp = _yp_check(NULL);
     90 #endif
     91 
     92 	if (strcmp(__progname, "yppasswd") == 0) {
     93 #ifdef YP
     94 		if (!use_yp)
     95 			errx(1, "YP not in use.");
     96 		use_kerberos = 0;
     97 		yppwd = 1;
     98 #else
     99 		errx(1, "YP support not compiled in.");
    100 #endif
    101 	}
    102 
    103 
    104 	while ((ch = getopt(argc, argv, "lky")) != -1)
    105 		switch (ch) {
    106 		case 'l':		/* change local password file */
    107 			if (yppwd)
    108 				usage();
    109 			use_kerberos = 0;
    110 			use_yp = 0;
    111 			break;
    112 		case 'k':		/* change Kerberos password */
    113 #if defined(KERBEROS) || defined(KERBEROS5)
    114 			if (yppwd)
    115 				usage();
    116 			use_kerberos = 1;
    117 			use_yp = 0;
    118 			break;
    119 #else
    120 			errx(1, "Kerberos support not compiled in.");
    121 #endif
    122 		case 'y':		/* change YP password */
    123 #ifdef	YP
    124 			if (yppwd)
    125 				usage();
    126 			if (!use_yp)
    127 				errx(1, "YP not in use.");
    128 			use_kerberos = 0;
    129 			yflag = 1;
    130 			break;
    131 #else
    132 			errx(1, "YP support not compiled in.");
    133 #endif
    134 		default:
    135 			usage();
    136 		}
    137 
    138 	argc -= optind;
    139 	argv += optind;
    140 
    141 	username = getlogin();
    142 	if (username == NULL)
    143 		errx(1, "who are you ??");
    144 
    145 	switch(argc) {
    146 	case 0:
    147 		break;
    148 	case 1:
    149 #if defined(KERBEROS) || defined(KERBEROS5)
    150 		if (use_kerberos && strcmp(argv[0], username)) {
    151 			errx(1, "%s\n\t%s\n%s\n",
    152 			     "to change another user's Kerberos password, do",
    153 			     "\"kinit <user>; passwd; kdestroy\";",
    154 			     "to change a user's local passwd, use\
    155 			     \"passwd -l <user>\"");
    156 		}
    157 #endif
    158 		username = argv[0];
    159 		break;
    160 	default:
    161 		usage();
    162 		exit(1);
    163 	}
    164 
    165 #if defined(KERBEROS) || defined(KERBEROS5)
    166 	if (use_kerberos)
    167 		exit(krb_passwd());
    168 #endif
    169 #ifdef	YP
    170 	if (use_yp)
    171 		exit(yp_passwd(username));
    172 #endif
    173 	exit(local_passwd(username));
    174 }
    175 
    176 void
    177 usage()
    178 {
    179 
    180 	if (yppwd)
    181 		fprintf(stderr, "usage: %s user\n", __progname);
    182 	else
    183 		fprintf(stderr, "usage: %s [-l] [-k] [-y] user\n", __progname);
    184 	exit(1);
    185 }
    186