2 * Copyright (c) 2002 Networks Associates Technologies, Inc.
5 * This software was developed for the FreeBSD Project by ThinkSec AS and
6 * NAI Labs, the Security Research Division of Network Associates, Inc.
7 * under DARPA/SPAWAR contract N66001-01-C-8035 ("CBOSS"), as part of the
8 * DARPA CHATS research program.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. The name of the author may not be used to endorse or promote
19 * products derived from this software without specific prior written
22 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * $FreeBSD: src/usr.bin/passwd/passwd.c,v 1.23 2003/04/18 21:27:09 nectar Exp $
37 #include <sys/param.h>
46 #include <security/pam_appl.h>
47 #include <security/openpam.h>
49 static pam_handle_t
*pamh
;
50 static struct pam_conv pamc
= {
55 static char *yp_domain
;
61 fprintf(stderr
, "usage: passwd [-ly] [-d domain] [-h host] [user]\n");
66 main(int argc
, char **argv
)
68 char hostname
[MAXHOSTNAMELEN
];
69 struct passwd
*pwd
= NULL
;
73 while ((o
= getopt(argc
, argv
, "d:h:loy")) != -1)
97 if ((pwd
= getpwuid(uid
)) == NULL
)
98 errx(1, "who are you?");
101 if ((pwd
= getpwnam(*argv
)) == NULL
)
102 errx(1, "%s: no such user", *argv
);
108 if (uid
!= 0 && uid
!= pwd
->pw_uid
)
109 errx(1, "permission denied");
111 /* check where the user's from */
112 switch (pwd
->pw_fields
& _PWF_SOURCE
) {
114 fprintf(stderr
, "Changing local password for %s\n",
118 fprintf(stderr
, "Changing NIS password for %s\n",
122 fprintf(stderr
, "Changing password for %s\n",
126 #define pam_check(func) do { \
127 if (pam_err != PAM_SUCCESS) { \
128 if (pam_err == PAM_AUTH_ERR || pam_err == PAM_PERM_DENIED || \
129 pam_err == PAM_AUTHTOK_RECOVERY_ERR) \
132 warnx("%s(): %s", func, pam_strerror(pamh, pam_err)); \
138 pam_err
= pam_start("passwd", pwd
->pw_name
, &pamc
, &pamh
);
139 pam_check("pam_start");
141 pam_err
= pam_set_item(pamh
, PAM_TTY
, ttyname(STDERR_FILENO
));
142 pam_check("pam_set_item");
143 gethostname(hostname
, sizeof hostname
);
144 pam_err
= pam_set_item(pamh
, PAM_RHOST
, hostname
);
145 pam_check("pam_set_item");
146 pam_err
= pam_set_item(pamh
, PAM_RUSER
, getlogin());
147 pam_check("pam_set_item");
149 /* set YP domain and host */
150 pam_err
= pam_set_data(pamh
, "yp_domain", yp_domain
, NULL
);
151 pam_check("pam_set_data");
152 pam_err
= pam_set_data(pamh
, "yp_server", yp_host
, NULL
);
153 pam_check("pam_set_data");
155 /* set new password */
156 pam_err
= pam_chauthtok(pamh
, 0);
157 pam_check("pam_chauthtok");
160 pam_end(pamh
, pam_err
);
161 exit(pam_err
== PAM_SUCCESS
? 0 : 1);