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 $
40 #include <sys/types.h>
46 #include <rpcsvc/yp_prot.h>
47 #include <rpcsvc/ypclnt.h>
48 #include <rpcsvc/yppasswd.h>
51 #include "yppasswd_private.h"
53 extern char *getnewpasswd(struct passwd *, int);
58 struct yppasswd yppasswd;
59 struct master_yppasswd master_yppasswd;
66 char *sockname = YP_SOCKNAME;
72 if ((master = get_yp_master(1)) == NULL) {
73 warnx("failed to find NIS master server");
78 * It is presumed that by the time we get here, use_yp()
79 * has been called and that we have verified that the user
80 * actually exists. This being the case, the yp_password
81 * stucture has already been filled in for us.
84 /* Use the correct password */
85 pw = (struct passwd *)&yp_password;
87 if (pw->pw_uid != uid && uid != 0) {
88 warnx("only the super-user may change account information \
95 /* Initialize password information */
97 master_yppasswd.newpw.pw_passwd = strdup(pw->pw_passwd);
98 master_yppasswd.newpw.pw_name = strdup(pw->pw_name);
99 master_yppasswd.newpw.pw_uid = pw->pw_uid;
100 master_yppasswd.newpw.pw_gid = pw->pw_gid;
101 master_yppasswd.newpw.pw_expire = pw->pw_expire;
102 master_yppasswd.newpw.pw_change = pw->pw_change;
103 master_yppasswd.newpw.pw_fields = pw->pw_fields;
104 master_yppasswd.newpw.pw_gecos = strdup(pw->pw_gecos);
105 master_yppasswd.newpw.pw_dir = strdup(pw->pw_dir);
106 master_yppasswd.newpw.pw_shell = strdup(pw->pw_shell);
107 master_yppasswd.newpw.pw_class = pw->pw_class != NULL ?
108 strdup(pw->pw_class) : "";
109 master_yppasswd.oldpass = "";
110 master_yppasswd.domain = yp_domain;
112 yppasswd.newpw.pw_passwd = strdup(pw->pw_passwd);
113 yppasswd.newpw.pw_name = strdup(pw->pw_name);
114 yppasswd.newpw.pw_uid = pw->pw_uid;
115 yppasswd.newpw.pw_gid = pw->pw_gid;
116 yppasswd.newpw.pw_gecos = strdup(pw->pw_gecos);
117 yppasswd.newpw.pw_dir = strdup(pw->pw_dir);
118 yppasswd.newpw.pw_shell = strdup(pw->pw_shell);
119 yppasswd.oldpass = "";
123 printf("Changing NIS password for %s on %s in domain %s.\n",
124 pw->pw_name, master, yp_domain);
126 printf("Changing NIS password for %s on %s.\n",
127 pw->pw_name, master);
129 /* Get old password */
131 if (pw->pw_passwd[0] && !suser_override) {
132 yppasswd.oldpass = strdup(getpass("Old Password: "));
133 if (strcmp(crypt(yppasswd.oldpass, pw->pw_passwd),
140 if (suser_override) {
141 if ((master_yppasswd.newpw.pw_passwd = getnewpasswd(pw, 1)) == NULL)
144 if ((yppasswd.newpw.pw_passwd = getnewpasswd(pw, 1)) == NULL)
148 if (suser_override) {
149 if ((clnt = clnt_create(sockname, MASTER_YPPASSWDPROG,
150 MASTER_YPPASSWDVERS, "unix")) == NULL) {
151 warnx("failed to contact rpc.yppasswdd on host %s: %s",
152 master, clnt_spcreateerror(""));
156 if ((clnt = clnt_create(master, YPPASSWDPROG,
157 YPPASSWDVERS, "udp")) == NULL) {
158 warnx("failed to contact rpc.yppasswdd on host %s: %s",
159 master, clnt_spcreateerror(""));
164 * The yppasswd.x file said `unix authentication required',
165 * so I added it. This is the only reason it is in here.
166 * My yppasswdd doesn't use it, but maybe some others out there
169 clnt->cl_auth = authunix_create_default();
172 status = yppasswdproc_update_master_1(&master_yppasswd, clnt);
174 status = yppasswdproc_update_1(&yppasswd, clnt);
176 clnt_geterr(clnt, &err);
178 auth_destroy(clnt->cl_auth);
181 if (err.re_status != RPC_SUCCESS || status == NULL || *status) {
182 errx(1, "failed to change NIS password: %s",
183 clnt_sperrno(err.re_status));
186 printf("\nNIS password has%s been changed on %s.\n",
187 (err.re_status != RPC_SUCCESS || status == NULL || *status) ?
188 " not" : "", master);
190 return ((err.re_status || status == NULL || *status));