Home | History | Annotate | Line # | Download | only in passwd
passwd.c revision 1.12
      1 /*	$NetBSD: passwd.c,v 1.12 1997/10/19 12:30:00 lukem 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 #include <sys/cdefs.h>
     37 #ifndef lint
     38 __COPYRIGHT("@(#) 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 __RCSID("$NetBSD: passwd.c,v 1.12 1997/10/19 12:30:00 lukem 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 int	main __P((int, char **));
     74 
     75 #ifdef YP
     76 extern int _yp_check __P((char **));	/* buried deep inside libc */
     77 #endif
     78 
     79 int
     80 main(argc, argv)
     81 	int argc;
     82 	char **argv;
     83 {
     84 	extern int optind;
     85 	int ch;
     86 	char *username;
     87 #if defined(KERBEROS) || defined(KERBEROS5)
     88 	char *iflag = 0, *rflag = 0, *uflag = 0;
     89 
     90 	if (strcmp(__progname, "kpasswd") == 0)
     91 		use_kerberos = 1;
     92 	else
     93 		use_kerberos = krb_check();
     94 #endif
     95 #ifdef	YP
     96 	use_yp = _yp_check(NULL);
     97 #endif
     98 
     99 	if (strcmp(__progname, "yppasswd") == 0) {
    100 #ifdef YP
    101 		if (!use_yp)
    102 			errx(1, "YP not in use.");
    103 		use_kerberos = 0;
    104 		yppwd = 1;
    105 #else
    106 		errx(1, "YP support not compiled in.");
    107 #endif
    108 	}
    109 
    110 
    111 	while ((ch = getopt(argc, argv, "lkyi:r:u:")) != -1)
    112 		switch (ch) {
    113 		case 'l':		/* change local password file */
    114 			if (yppwd)
    115 				usage();
    116 			use_kerberos = 0;
    117 			use_yp = 0;
    118 			break;
    119 #ifdef KERBEROS
    120 		case 'i':
    121 			iflag = optarg;
    122 			break;
    123 		case 'r':
    124 			rflag = optarg;
    125 			break;
    126 		case 'u':
    127 			uflag = optarg;
    128 			break;
    129 #endif
    130 		case 'k':		/* change Kerberos password */
    131 #if defined(KERBEROS) || defined(KERBEROS5)
    132 			if (yppwd)
    133 				usage();
    134 			use_kerberos = 1;
    135 			use_yp = 0;
    136 			break;
    137 #else
    138 			errx(1, "Kerberos support not compiled in.");
    139 #endif
    140 		case 'y':		/* change YP password */
    141 #ifdef	YP
    142 			if (yppwd)
    143 				usage();
    144 			if (!use_yp)
    145 				errx(1, "YP not in use.");
    146 			use_kerberos = 0;
    147 			yflag = 1;
    148 			break;
    149 #else
    150 			errx(1, "YP support not compiled in.");
    151 #endif
    152 		default:
    153 			usage();
    154 		}
    155 
    156 	argc -= optind;
    157 	argv += optind;
    158 
    159 	username = getlogin();
    160 	if (username == NULL)
    161 		errx(1, "who are you ??");
    162 
    163 	switch(argc) {
    164 	case 0:
    165 		break;
    166 	case 1:
    167 #ifdef KERBEROS5
    168 		if (use_kerberos && strcmp(argv[0], username)) {
    169 			errx(1, "%s\n\t%s\n%s\n",
    170 			     "to change another user's Kerberos password, do",
    171 			     "\"kinit <user>; passwd; kdestroy\";",
    172 			     "to change a user's local passwd, use\
    173 			     \"passwd -l <user>\"");
    174 		}
    175 #endif
    176 		username = argv[0];
    177 		break;
    178 	default:
    179 		usage();
    180 		exit(1);
    181 	}
    182 
    183 #if defined(KERBEROS) || defined(KERBEROS5)
    184 	if (use_kerberos)
    185 		exit(kadm_passwd(username, iflag, rflag, uflag));
    186 #else
    187 #ifdef KERBEROS5
    188 	if (use_kerberos)
    189 		exit(krb_passwd());
    190 #endif
    191 #endif
    192 #ifdef	YP
    193 	if (use_yp)
    194 		exit(yp_passwd(username));
    195 #endif
    196 	exit(local_passwd(username));
    197 }
    198 
    199 void
    200 usage()
    201 {
    202 
    203 	if (yppwd)
    204 		fprintf(stderr, "usage: %s user\n", __progname);
    205 	else
    206 		fprintf(stderr, "usage: %s [-l] [-k] [-y] [-i instance] [-r realm] [-u fullname] user\n", __progname);
    207 	exit(1);
    208 }
    209