passwd.c revision 1591
1331722Seadler/*
2156136Sdavidxu * Copyright (c) 1988, 1993, 1994
3156136Sdavidxu *	The Regents of the University of California.  All rights reserved.
4156136Sdavidxu *
5156136Sdavidxu * Redistribution and use in source and binary forms, with or without
6156136Sdavidxu * modification, are permitted provided that the following conditions
7156136Sdavidxu * are met:
8156136Sdavidxu * 1. Redistributions of source code must retain the above copyright
9156136Sdavidxu *    notice, this list of conditions and the following disclaimer.
10156136Sdavidxu * 2. Redistributions in binary form must reproduce the above copyright
11156136Sdavidxu *    notice, this list of conditions and the following disclaimer in the
12156136Sdavidxu *    documentation and/or other materials provided with the distribution.
13156136Sdavidxu * 3. All advertising materials mentioning features or use of this software
14156136Sdavidxu *    must display the following acknowledgement:
15156136Sdavidxu *	This product includes software developed by the University of
16156136Sdavidxu *	California, Berkeley and its contributors.
17156136Sdavidxu * 4. Neither the name of the University nor the names of its contributors
18156136Sdavidxu *    may be used to endorse or promote products derived from this software
19156136Sdavidxu *    without specific prior written permission.
20156136Sdavidxu *
21156136Sdavidxu * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22156136Sdavidxu * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23156136Sdavidxu * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24156136Sdavidxu * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25156136Sdavidxu * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26156136Sdavidxu * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27156136Sdavidxu * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28156136Sdavidxu * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29156136Sdavidxu * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30156136Sdavidxu * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31156136Sdavidxu * SUCH DAMAGE.
32156136Sdavidxu */
33156136Sdavidxu
34156136Sdavidxu#ifndef lint
35156136Sdavidxustatic char copyright[] =
36156136Sdavidxu"@(#) Copyright (c) 1988, 1993, 1994\n\
37156136Sdavidxu	The Regents of the University of California.  All rights reserved.\n";
38156136Sdavidxu#endif /* not lint */
39156136Sdavidxu
40156136Sdavidxu#ifndef lint
41156136Sdavidxustatic char sccsid[] = "@(#)passwd.c	8.3 (Berkeley) 4/2/94";
42156136Sdavidxu#endif /* not lint */
43156136Sdavidxu
44156136Sdavidxu#include <err.h>
45156136Sdavidxu#include <errno.h>
46156136Sdavidxu#include <stdio.h>
47156136Sdavidxu#include <stdlib.h>
48156136Sdavidxu#include <unistd.h>
49156136Sdavidxu
50156136Sdavidxu#include "extern.h"
51156136Sdavidxu
52156136Sdavidxuvoid	usage __P((void));
53156136Sdavidxu
54156136Sdavidxu#ifdef KERBEROS
55156136Sdavidxuint use_kerberos = 1;
56156136Sdavidxu#endif
57156136Sdavidxu
58156136Sdavidxuint
59156136Sdavidxumain(argc, argv)
60156136Sdavidxu	int argc;
61156136Sdavidxu	char **argv;
62156136Sdavidxu{
63156136Sdavidxu	int ch;
64156136Sdavidxu	char *uname;
65156136Sdavidxu
66156136Sdavidxu	while ((ch = getopt(argc, argv, "l")) != EOF)
67156267Sdavidxu		switch (ch) {
68156136Sdavidxu#ifdef KERBEROS
69156136Sdavidxu		case 'l':		/* change local password file */
70156267Sdavidxu			use_kerberos = 0;
71156136Sdavidxu			break;
72156136Sdavidxu#endif
73156136Sdavidxu		default:
74156136Sdavidxu		case '?':
75156267Sdavidxu			usage();
76156267Sdavidxu		}
77156136Sdavidxu
78156136Sdavidxu	argc -= optind;
79156136Sdavidxu	argv += optind;
80156136Sdavidxu
81156136Sdavidxu	if ((uname = getlogin()) == NULL)
82156136Sdavidxu		err(1, "getlogin");
83156136Sdavidxu
84156136Sdavidxu	switch(argc) {
85156136Sdavidxu	case 0:
86156136Sdavidxu		break;
87156136Sdavidxu	case 1:
88156136Sdavidxu#ifdef	KERBEROS
89156136Sdavidxu		if (use_kerberos && strcmp(argv[0], uname))
90156136Sdavidxu			errx(1,"%s\n\t%s\n%s\n",
91156136Sdavidxu		"to change another user's Kerberos password, do",
92156136Sdavidxu		"\"kinit user; passwd; kdestroy\";",
93156136Sdavidxu		"to change a user's local passwd, use \"passwd -l user\"");
94156136Sdavidxu#endif
95156136Sdavidxu		uname = argv[0];
96156136Sdavidxu		break;
97156136Sdavidxu	default:
98156136Sdavidxu		usage();
99156136Sdavidxu	}
100156136Sdavidxu
101156136Sdavidxu#ifdef	KERBEROS
102156136Sdavidxu	if (use_kerberos)
103156136Sdavidxu		exit(krb_passwd());
104156136Sdavidxu#endif
105227661Skib	exit(local_passwd(uname));
106156136Sdavidxu}
107156136Sdavidxu
108156136Sdavidxuvoid
109156136Sdavidxuusage()
110156267Sdavidxu{
111156136Sdavidxu
112227661Skib#ifdef	KERBEROS
113156136Sdavidxu	(void)fprintf(stderr, "usage: passwd [-l] user\n");
114156136Sdavidxu#else
115156136Sdavidxu	(void)fprintf(stderr, "usage: passwd user\n");
116156136Sdavidxu#endif
117156136Sdavidxu	exit(1);
118156136Sdavidxu}
119156136Sdavidxu