Home | History | Annotate | Line # | Download | only in passwd
passwd.c revision 1.9
      1 /*	$NetBSD: passwd.c,v 1.9 1996/11/26 23:35:38 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.9 1996/11/26 23:35:38 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 int yflag;
     66 
     67 extern	char *__progname;		/* from crt0.o */
     68 
     69 #ifdef YP
     70 extern int _yp_check __P((char **));	/* buried deep inside libc */
     71 #endif
     72 
     73 main(argc, argv)
     74 	int argc;
     75 	char **argv;
     76 {
     77 	extern int optind;
     78 	register int ch;
     79 	char *username;
     80 
     81 #if defined(KERBEROS) || defined(KERBEROS5)
     82 	use_kerberos = 1;
     83 #endif
     84 #ifdef	YP
     85 	use_yp = _yp_check(NULL);
     86 #endif
     87 
     88 	if (strcmp(__progname, "yppasswd") == 0) {
     89 #ifdef YP
     90 		if (!use_yp)
     91 			errx(1, "YP not in use.");
     92 		use_kerberos = 0;
     93 		yppwd = 1;
     94 #else
     95 		errx(1, "YP support not compiled in.");
     96 #endif
     97 	}
     98 
     99 
    100 	while ((ch = getopt(argc, argv, "lky")) != -1)
    101 		switch (ch) {
    102 		case 'l':		/* change local password file */
    103 			if (yppwd)
    104 				usage();
    105 			use_kerberos = 0;
    106 			use_yp = 0;
    107 			break;
    108 		case 'k':		/* change Kerberos password */
    109 #if defined(KERBEROS) || defined(KERBEROS5)
    110 			if (yppwd)
    111 				usage();
    112 			use_kerberos = 1;
    113 			use_yp = 0;
    114 			break;
    115 #else
    116 			errx(1, "Kerberos support not compiled in.");
    117 #endif
    118 		case 'y':		/* change YP password */
    119 #ifdef	YP
    120 			if (yppwd)
    121 				usage();
    122 			if (!use_yp)
    123 				errx(1, "YP not in use.");
    124 			use_kerberos = 0;
    125 			yflag = 1;
    126 			break;
    127 #else
    128 			errx(1, "YP support not compiled in.");
    129 #endif
    130 		default:
    131 			usage();
    132 		}
    133 
    134 	argc -= optind;
    135 	argv += optind;
    136 
    137 	username = getlogin();
    138 	if (username == NULL)
    139 		errx(1, "who are you ??");
    140 
    141 	switch(argc) {
    142 	case 0:
    143 		break;
    144 	case 1:
    145 #if defined(KERBEROS) || defined(KERBEROS5)
    146 		if (use_kerberos && strcmp(argv[0], username)) {
    147 			errx(1, "%s\n\t%s\n%s\n",
    148 "to change another user's Kerberos password, do",
    149 "\"kinit <user>; passwd; kdestroy\";",
    150 "to change a user's local passwd, use \"passwd -l <user>\"");
    151 		}
    152 #endif
    153 		username = argv[0];
    154 		break;
    155 	default:
    156 		usage();
    157 		exit(1);
    158 	}
    159 
    160 #if defined(KERBEROS) || defined(KERBEROS5)
    161 	if (use_kerberos)
    162 		exit(krb_passwd());
    163 #endif
    164 #ifdef	YP
    165 	if (use_yp)
    166 		exit(yp_passwd(username));
    167 #endif
    168 	exit(local_passwd(username));
    169 }
    170 
    171 usage()
    172 {
    173 
    174 	if (yppwd)
    175 		fprintf(stderr, "usage: %s user\n", __progname);
    176 	else
    177 		fprintf(stderr, "usage: %s [-l] [-k] [-y] user\n", __progname);
    178 	exit(1);
    179 }
    180