2 * Copyright (c) 1992/3 Theo de Raadt <deraadt@fsa.ca>
3 * Copyright (c) 1994 Olaf Kirch <okir@monad.swb.de>
4 * Copyright (c) 1995 Bill Paul <wpaul@ctr.columbia.edu>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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. The name of the author may not be used to endorse or promote
16 * products derived from this software without specific prior written
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
20 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
23 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * $FreeBSD: src/usr.bin/passwd/yp_passwd.c,v 1.15.6.1 2002/02/15 00:46:56 des Exp $
32 * $DragonFly: src/usr.bin/passwd/yp_passwd.c,v 1.2 2003/06/17 04:29:30 dillon Exp $
41 #include <sys/types.h>
47 #include <rpcsvc/yp_prot.h>
48 #include <rpcsvc/ypclnt.h>
49 #include <rpcsvc/yppasswd.h>
52 #include "yppasswd_private.h"
54 extern char *getnewpasswd(struct passwd
*, int);
59 struct yppasswd yppasswd
;
60 struct master_yppasswd master_yppasswd
;
67 char *sockname
= YP_SOCKNAME
;
73 if ((master
= get_yp_master(1)) == NULL
) {
74 warnx("failed to find NIS master server");
79 * It is presumed that by the time we get here, use_yp()
80 * has been called and that we have verified that the user
81 * actually exists. This being the case, the yp_password
82 * stucture has already been filled in for us.
85 /* Use the correct password */
86 pw
= (struct passwd
*)&yp_password
;
88 if (pw
->pw_uid
!= uid
&& uid
!= 0) {
89 warnx("only the super-user may change account information \
96 /* Initialize password information */
98 master_yppasswd
.newpw
.pw_passwd
= strdup(pw
->pw_passwd
);
99 master_yppasswd
.newpw
.pw_name
= strdup(pw
->pw_name
);
100 master_yppasswd
.newpw
.pw_uid
= pw
->pw_uid
;
101 master_yppasswd
.newpw
.pw_gid
= pw
->pw_gid
;
102 master_yppasswd
.newpw
.pw_expire
= pw
->pw_expire
;
103 master_yppasswd
.newpw
.pw_change
= pw
->pw_change
;
104 master_yppasswd
.newpw
.pw_fields
= pw
->pw_fields
;
105 master_yppasswd
.newpw
.pw_gecos
= strdup(pw
->pw_gecos
);
106 master_yppasswd
.newpw
.pw_dir
= strdup(pw
->pw_dir
);
107 master_yppasswd
.newpw
.pw_shell
= strdup(pw
->pw_shell
);
108 master_yppasswd
.newpw
.pw_class
= pw
->pw_class
!= NULL
?
109 strdup(pw
->pw_class
) : "";
110 master_yppasswd
.oldpass
= "";
111 master_yppasswd
.domain
= yp_domain
;
113 yppasswd
.newpw
.pw_passwd
= strdup(pw
->pw_passwd
);
114 yppasswd
.newpw
.pw_name
= strdup(pw
->pw_name
);
115 yppasswd
.newpw
.pw_uid
= pw
->pw_uid
;
116 yppasswd
.newpw
.pw_gid
= pw
->pw_gid
;
117 yppasswd
.newpw
.pw_gecos
= strdup(pw
->pw_gecos
);
118 yppasswd
.newpw
.pw_dir
= strdup(pw
->pw_dir
);
119 yppasswd
.newpw
.pw_shell
= strdup(pw
->pw_shell
);
120 yppasswd
.oldpass
= "";
124 printf("Changing NIS password for %s on %s in domain %s.\n",
125 pw
->pw_name
, master
, yp_domain
);
127 printf("Changing NIS password for %s on %s.\n",
128 pw
->pw_name
, master
);
130 /* Get old password */
132 if (pw
->pw_passwd
[0] && !suser_override
) {
133 yppasswd
.oldpass
= strdup(getpass("Old Password: "));
134 if (strcmp(crypt(yppasswd
.oldpass
, pw
->pw_passwd
),
141 if (suser_override
) {
142 if ((master_yppasswd
.newpw
.pw_passwd
= getnewpasswd(pw
, 1)) == NULL
)
145 if ((yppasswd
.newpw
.pw_passwd
= getnewpasswd(pw
, 1)) == NULL
)
149 if (suser_override
) {
150 if ((clnt
= clnt_create(sockname
, MASTER_YPPASSWDPROG
,
151 MASTER_YPPASSWDVERS
, "unix")) == NULL
) {
152 warnx("failed to contact rpc.yppasswdd on host %s: %s",
153 master
, clnt_spcreateerror(""));
157 if ((clnt
= clnt_create(master
, YPPASSWDPROG
,
158 YPPASSWDVERS
, "udp")) == NULL
) {
159 warnx("failed to contact rpc.yppasswdd on host %s: %s",
160 master
, clnt_spcreateerror(""));
165 * The yppasswd.x file said `unix authentication required',
166 * so I added it. This is the only reason it is in here.
167 * My yppasswdd doesn't use it, but maybe some others out there
170 clnt
->cl_auth
= authunix_create_default();
173 status
= yppasswdproc_update_master_1(&master_yppasswd
, clnt
);
175 status
= yppasswdproc_update_1(&yppasswd
, clnt
);
177 clnt_geterr(clnt
, &err
);
179 auth_destroy(clnt
->cl_auth
);
182 if (err
.re_status
!= RPC_SUCCESS
|| status
== NULL
|| *status
) {
183 errx(1, "failed to change NIS password: %s",
184 clnt_sperrno(err
.re_status
));
187 printf("\nNIS password has%s been changed on %s.\n",
188 (err
.re_status
!= RPC_SUCCESS
|| status
== NULL
|| *status
) ?
189 " not" : "", master
);
191 return ((err
.re_status
|| status
== NULL
|| *status
));