Home | History | Annotate | Line # | Download | only in passwd
passwd.c revision 1.8
      1 /*	$NetBSD: passwd.c,v 1.8 1996/08/09 09:19:41 thorpej Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1988 The Regents of the University of California.
      5  * 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 char copyright[] =
     38 "@(#) Copyright (c) 1988 The Regents of the University of California.\n\
     39  All rights reserved.\n";
     40 #endif /* not lint */
     41 
     42 #ifndef lint
     43 #if 0
     44 static char sccsid[] = "from: @(#)passwd.c	5.5 (Berkeley) 7/6/91";
     45 #else
     46 static char rcsid[] = "$NetBSD: passwd.c,v 1.8 1996/08/09 09:19:41 thorpej 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 /*
     56  * Note on configuration:
     57  *      Generally one would not use both Kerberos and YP
     58  *      to maintain passwords.
     59  *
     60  */
     61 
     62 int use_kerberos;
     63 int use_yp;
     64 int yppwd;
     65 
     66 extern	char *__progname;		/* from crt0.o */
     67 
     68 #ifdef YP
     69 extern int _yp_check __P((char **));	/* buried deep inside libc */
     70 #endif
     71 
     72 main(argc, argv)
     73 	int argc;
     74 	char **argv;
     75 {
     76 	extern int optind;
     77 	register int ch;
     78 	char *username;
     79 
     80 #if defined(KERBEROS) || defined(KERBEROS5)
     81 	use_kerberos = 1;
     82 #endif
     83 #ifdef	YP
     84 	use_yp = _yp_check(NULL);
     85 #endif
     86 
     87 	if (strcmp(__progname, "yppasswd") == 0) {
     88 #ifdef YP
     89 		if (!use_yp)
     90 			errx(1, "YP not in use.");
     91 		use_kerberos = 0;
     92 		yppwd = 1;
     93 #else
     94 		errx(1, "YP support not compiled in.");
     95 #endif
     96 	}
     97 
     98 
     99 	while ((ch = getopt(argc, argv, "lky")) != -1)
    100 		switch (ch) {
    101 		case 'l':		/* change local password file */
    102 			if (yppwd)
    103 				usage();
    104 			use_kerberos = 0;
    105 			use_yp = 0;
    106 			break;
    107 		case 'k':		/* change Kerberos password */
    108 #if defined(KERBEROS) || defined(KERBEROS5)
    109 			if (yppwd)
    110 				usage();
    111 			use_kerberos = 1;
    112 			use_yp = 0;
    113 			break;
    114 #else
    115 			errx(1, "Kerberos support not compiled in.");
    116 #endif
    117 		case 'y':		/* change YP password */
    118 #ifdef	YP
    119 			if (yppwd)
    120 				usage();
    121 			if (!use_yp)
    122 				errx(1, "YP not in use.");
    123 			use_kerberos = 0;
    124 			break;
    125 #else
    126 			errx(1, "YP support not compiled in.");
    127 #endif
    128 		default:
    129 			usage();
    130 		}
    131 
    132 	argc -= optind;
    133 	argv += optind;
    134 
    135 	username = getlogin();
    136 	if (username == NULL)
    137 		errx(1, "who are you ??");
    138 
    139 	switch(argc) {
    140 	case 0:
    141 		break;
    142 	case 1:
    143 #if defined(KERBEROS) || defined(KERBEROS5)
    144 		if (use_kerberos && strcmp(argv[0], username)) {
    145 			errx(1, "%s\n\t%s\n%s\n",
    146 "to change another user's Kerberos password, do",
    147 "\"kinit <user>; passwd; kdestroy\";",
    148 "to change a user's local passwd, use \"passwd -l <user>\"");
    149 		}
    150 #endif
    151 		username = argv[0];
    152 		break;
    153 	default:
    154 		usage();
    155 		exit(1);
    156 	}
    157 
    158 #if defined(KERBEROS) || defined(KERBEROS5)
    159 	if (use_kerberos)
    160 		exit(krb_passwd());
    161 #endif
    162 #ifdef	YP
    163 	if (use_yp)
    164 		exit(yp_passwd(username));
    165 #endif
    166 	exit(local_passwd(username));
    167 }
    168 
    169 usage()
    170 {
    171 
    172 	if (yppwd)
    173 		fprintf(stderr, "usage: %s user\n", __progname);
    174 	else
    175 		fprintf(stderr, "usage: %s [-l] [-k] [-y] user\n", __progname);
    176 	exit(1);
    177 }
    178