xref: /minix3/usr.bin/passwd/pam_passwd.c (revision 5c00743626989a611dd2966296c00a8cc4abca6f)
1*5c007436SBen Gras /*	$NetBSD: pam_passwd.c,v 1.6 2010/03/09 16:14:08 joerg Exp $	*/
2*5c007436SBen Gras 
3*5c007436SBen Gras /*-
4*5c007436SBen Gras  * Copyright (c) 2002 Networks Associates Technologies, Inc.
5*5c007436SBen Gras  * All rights reserved.
6*5c007436SBen Gras  *
7*5c007436SBen Gras  * This software was developed for the FreeBSD Project by ThinkSec AS and
8*5c007436SBen Gras  * NAI Labs, the Security Research Division of Network Associates, Inc.
9*5c007436SBen Gras  * under DARPA/SPAWAR contract N66001-01-C-8035 ("CBOSS"), as part of the
10*5c007436SBen Gras  * DARPA CHATS research program.
11*5c007436SBen Gras  *
12*5c007436SBen Gras  * Redistribution and use in source and binary forms, with or without
13*5c007436SBen Gras  * modification, are permitted provided that the following conditions
14*5c007436SBen Gras  * are met:
15*5c007436SBen Gras  * 1. Redistributions of source code must retain the above copyright
16*5c007436SBen Gras  *    notice, this list of conditions and the following disclaimer.
17*5c007436SBen Gras  * 2. Redistributions in binary form must reproduce the above copyright
18*5c007436SBen Gras  *    notice, this list of conditions and the following disclaimer in the
19*5c007436SBen Gras  *    documentation and/or other materials provided with the distribution.
20*5c007436SBen Gras  * 3. The name of the author may not be used to endorse or promote
21*5c007436SBen Gras  *    products derived from this software without specific prior written
22*5c007436SBen Gras  *    permission.
23*5c007436SBen Gras  *
24*5c007436SBen Gras  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
25*5c007436SBen Gras  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26*5c007436SBen Gras  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27*5c007436SBen Gras  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
28*5c007436SBen Gras  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29*5c007436SBen Gras  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30*5c007436SBen Gras  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31*5c007436SBen Gras  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32*5c007436SBen Gras  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33*5c007436SBen Gras  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34*5c007436SBen Gras  * SUCH DAMAGE.
35*5c007436SBen Gras  */
36*5c007436SBen Gras 
37*5c007436SBen Gras #include <sys/cdefs.h>
38*5c007436SBen Gras #ifdef __FreeBSD__
39*5c007436SBen Gras __FBSDID("$FreeBSD: src/usr.bin/passwd/passwd.c,v 1.23 2003/04/18 21:27:09 nectar Exp $");
40*5c007436SBen Gras #else
41*5c007436SBen Gras __RCSID("$NetBSD: pam_passwd.c,v 1.6 2010/03/09 16:14:08 joerg Exp $");
42*5c007436SBen Gras #endif
43*5c007436SBen Gras 
44*5c007436SBen Gras #include <sys/param.h>
45*5c007436SBen Gras 
46*5c007436SBen Gras #include <err.h>
47*5c007436SBen Gras #include <pwd.h>
48*5c007436SBen Gras #include <stdio.h>
49*5c007436SBen Gras #include <stdlib.h>
50*5c007436SBen Gras #include <syslog.h>
51*5c007436SBen Gras #include <unistd.h>
52*5c007436SBen Gras 
53*5c007436SBen Gras #include "extern.h"
54*5c007436SBen Gras 
55*5c007436SBen Gras #include <security/pam_appl.h>
56*5c007436SBen Gras #include <security/openpam.h>
57*5c007436SBen Gras 
58*5c007436SBen Gras static pam_handle_t *pamh;
59*5c007436SBen Gras static struct pam_conv pamc = {
60*5c007436SBen Gras 	openpam_ttyconv,
61*5c007436SBen Gras 	NULL
62*5c007436SBen Gras };
63*5c007436SBen Gras 
64*5c007436SBen Gras #define	pam_check(msg)							\
65*5c007436SBen Gras do {									\
66*5c007436SBen Gras 	if (pam_err != PAM_SUCCESS) {					\
67*5c007436SBen Gras 		warnx("%s: %s", (msg), pam_strerror(pamh, pam_err));	\
68*5c007436SBen Gras 		goto end;						\
69*5c007436SBen Gras 	}								\
70*5c007436SBen Gras } while (/*CONSTCOND*/0)
71*5c007436SBen Gras 
72*5c007436SBen Gras void
pwpam_process(const char * username,int argc,char ** argv)73*5c007436SBen Gras pwpam_process(const char *username, int argc, char **argv)
74*5c007436SBen Gras {
75*5c007436SBen Gras 	int ch, pam_err;
76*5c007436SBen Gras 	char hostname[MAXHOSTNAMELEN + 1];
77*5c007436SBen Gras 
78*5c007436SBen Gras 	/* details about the invoking user for logging */
79*5c007436SBen Gras 	const uid_t i_uid = getuid();
80*5c007436SBen Gras 	const struct passwd *const i_pwd = getpwuid(i_uid);
81*5c007436SBen Gras 	const char *const i_username = (i_pwd && i_pwd->pw_name)
82*5c007436SBen Gras 		? i_pwd->pw_name : "(null)";
83*5c007436SBen Gras 
84*5c007436SBen Gras 	while ((ch = getopt(argc, argv, "")) != -1) {
85*5c007436SBen Gras 		switch (ch) {
86*5c007436SBen Gras 		default:
87*5c007436SBen Gras 			usage();
88*5c007436SBen Gras 			/* NOTREACHED */
89*5c007436SBen Gras 		}
90*5c007436SBen Gras 	}
91*5c007436SBen Gras 
92*5c007436SBen Gras 	argc -= optind;
93*5c007436SBen Gras 	argv += optind;
94*5c007436SBen Gras 
95*5c007436SBen Gras 	switch (argc) {
96*5c007436SBen Gras 	case 0:
97*5c007436SBen Gras 		/* username already provided */
98*5c007436SBen Gras 		break;
99*5c007436SBen Gras 	case 1:
100*5c007436SBen Gras 		username = argv[0];
101*5c007436SBen Gras 		break;
102*5c007436SBen Gras 	default:
103*5c007436SBen Gras 		usage();
104*5c007436SBen Gras 		/* NOTREACHED */
105*5c007436SBen Gras 	}
106*5c007436SBen Gras 
107*5c007436SBen Gras 	(void)printf("Changing password for %s.\n", username);
108*5c007436SBen Gras 
109*5c007436SBen Gras 	/* initialize PAM -- always use the program name "passwd" */
110*5c007436SBen Gras 	pam_err = pam_start("passwd", username, &pamc, &pamh);
111*5c007436SBen Gras 	if (pam_err != PAM_SUCCESS)
112*5c007436SBen Gras 		errx(1, "unable to start PAM session: %s",
113*5c007436SBen Gras 		    pam_strerror(NULL, pam_err));
114*5c007436SBen Gras 
115*5c007436SBen Gras 	pam_err = pam_set_item(pamh, PAM_TTY, ttyname(STDERR_FILENO));
116*5c007436SBen Gras 	pam_check("unable to set TTY");
117*5c007436SBen Gras 
118*5c007436SBen Gras 	(void)gethostname(hostname, sizeof hostname);
119*5c007436SBen Gras 	pam_err = pam_set_item(pamh, PAM_RHOST, hostname);
120*5c007436SBen Gras 	pam_check("unable to set RHOST");
121*5c007436SBen Gras 
122*5c007436SBen Gras 	pam_err = pam_set_item(pamh, PAM_RUSER, getlogin());
123*5c007436SBen Gras 	pam_check("unable to set RUSER");
124*5c007436SBen Gras 
125*5c007436SBen Gras 	/* set new password */
126*5c007436SBen Gras 	pam_err = pam_chauthtok(pamh, 0);
127*5c007436SBen Gras 	if (pam_err != PAM_SUCCESS) {
128*5c007436SBen Gras 		if (pam_err == PAM_PERM_DENIED) {
129*5c007436SBen Gras 			syslog(LOG_AUTH | LOG_NOTICE,
130*5c007436SBen Gras 			       "user %s (UID %lu) failed to change the "
131*5c007436SBen Gras 			       "PAM authentication token of user %s: %s",
132*5c007436SBen Gras 			       i_username, (unsigned long)i_uid, username,
133*5c007436SBen Gras 			       pam_strerror(pamh, pam_err));
134*5c007436SBen Gras 		}
135*5c007436SBen Gras 		printf("Unable to change auth token: %s\n",
136*5c007436SBen Gras 		    pam_strerror(pamh, pam_err));
137*5c007436SBen Gras 	} else {
138*5c007436SBen Gras 		syslog(LOG_AUTH | LOG_INFO,
139*5c007436SBen Gras 		       "user %s (UID %lu) successfully changed the "
140*5c007436SBen Gras 		       "PAM authentication token of user %s",
141*5c007436SBen Gras 		       i_username, (unsigned long)i_uid, username);
142*5c007436SBen Gras 	}
143*5c007436SBen Gras 
144*5c007436SBen Gras  end:
145*5c007436SBen Gras 	pam_end(pamh, pam_err);
146*5c007436SBen Gras 	if (pam_err == PAM_SUCCESS)
147*5c007436SBen Gras 		return;
148*5c007436SBen Gras 	exit(1);
149*5c007436SBen Gras }
150