ypclnt_passwd.c revision 94575
194575Sdes/*-
294575Sdes * Copyright (c) 2002 Networks Associates Technology, Inc.
394575Sdes * All rights reserved.
494575Sdes *
594575Sdes * This software was developed for the FreeBSD Project by ThinkSec AS and
694575Sdes * NAI Labs, the Security Research Division of Network Associates, Inc.
794575Sdes * under DARPA/SPAWAR contract N66001-01-C-8035 ("CBOSS"), as part of the
894575Sdes * DARPA CHATS research program.
994575Sdes *
1094575Sdes * Redistribution and use in source and binary forms, with or without
1194575Sdes * modification, are permitted provided that the following conditions
1294575Sdes * are met:
1394575Sdes * 1. Redistributions of source code must retain the above copyright
1494575Sdes *    notice, this list of conditions and the following disclaimer.
1594575Sdes * 2. Redistributions in binary form must reproduce the above copyright
1694575Sdes *    notice, this list of conditions and the following disclaimer in the
1794575Sdes *    documentation and/or other materials provided with the distribution.
1894575Sdes * 3. The name of the author may not be used to endorse or promote
1994575Sdes *    products derived from this software without specific prior written
2094575Sdes *    permission.
2194575Sdes *
2294575Sdes * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
2394575Sdes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2494575Sdes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2594575Sdes * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
2694575Sdes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2794575Sdes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2894575Sdes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2994575Sdes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
3094575Sdes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3194575Sdes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3294575Sdes * SUCH DAMAGE.
3394575Sdes *
3494575Sdes * $FreeBSD: head/lib/libypclnt/ypclnt_passwd.c 94575 2002-04-13 06:20:02Z des $
3594575Sdes */
3694575Sdes
3794575Sdes#include <err.h>
3894575Sdes#include <errno.h>
3994575Sdes#include <pwd.h>
4094575Sdes#include <stdlib.h>
4194575Sdes#include <string.h>
4294575Sdes
4394575Sdes#include <rpcsvc/ypclnt.h>
4494575Sdes#include <rpcsvc/yppasswd.h>
4594575Sdes
4694575Sdes#include "ypclnt.h"
4794575Sdes
4894575Sdesint
4994575Sdesypclnt_passwd(ypclnt_t *ypclnt, const struct passwd *pwd, const char *passwd)
5094575Sdes{
5194575Sdes	struct yppasswd yppwd;
5294575Sdes	struct rpc_err rpcerr;
5394575Sdes	CLIENT *clnt = NULL;
5494575Sdes	int ret, *result;
5594575Sdes
5694575Sdes	/* check that rpc.yppasswdd is running */
5794575Sdes	if (getrpcport(ypclnt->server, YPPASSWDPROG,
5894575Sdes		YPPASSWDPROC_UPDATE, IPPROTO_UDP) == 0) {
5994575Sdes		ypclnt_error(ypclnt, __func__, "no rpc.yppasswdd on server");
6094575Sdes		return (-1);
6194575Sdes	}
6294575Sdes
6394575Sdes	/* fill the yppasswd structure */
6494575Sdes	memset(&yppwd, 0, sizeof yppwd);
6594575Sdes	yppwd.newpw.pw_uid = pwd->pw_uid;
6694575Sdes	yppwd.newpw.pw_gid = pwd->pw_gid;
6794575Sdes	if ((yppwd.newpw.pw_name = strdup(pwd->pw_name)) == NULL ||
6894575Sdes	    (yppwd.newpw.pw_passwd = strdup(pwd->pw_passwd)) == NULL ||
6994575Sdes	    (yppwd.newpw.pw_gecos = strdup(pwd->pw_gecos)) == NULL ||
7094575Sdes	    (yppwd.newpw.pw_dir = strdup(pwd->pw_dir)) == NULL ||
7194575Sdes	    (yppwd.newpw.pw_shell = strdup(pwd->pw_shell)) == NULL ||
7294575Sdes	    (yppwd.oldpass = strdup(passwd)) == NULL) {
7394575Sdes		ypclnt_error(ypclnt, __func__, strerror(errno));
7494575Sdes		ret = -1;
7594575Sdes		goto done;
7694575Sdes	}
7794575Sdes
7894575Sdes	/* connect to rpc.yppasswdd */
7994575Sdes	clnt = clnt_create(ypclnt->server, YPPASSWDPROG, YPPASSWDVERS, "udp");
8094575Sdes	if (clnt == NULL) {
8194575Sdes		ypclnt_error(ypclnt, __func__,
8294575Sdes		    "failed to connect to rpc.yppasswdd: %s",
8394575Sdes		    clnt_spcreateerror(ypclnt->server));
8494575Sdes		ret = -1;
8594575Sdes		goto done;
8694575Sdes	}
8794575Sdes	clnt->cl_auth = authunix_create_default();
8894575Sdes
8994575Sdes	/* request the update */
9094575Sdes	result = yppasswdproc_update_1(&yppwd, clnt);
9194575Sdes
9294575Sdes	/* check for RPC errors */
9394575Sdes	clnt_geterr(clnt, &rpcerr);
9494575Sdes	if (rpcerr.re_status != RPC_SUCCESS) {
9594575Sdes		ypclnt_error(ypclnt, __func__,
9694575Sdes		    "NIS password update failed: %s",
9794575Sdes		    clnt_sperror(clnt, ypclnt->server));
9894575Sdes		ret = -1;
9994575Sdes		goto done;
10094575Sdes	}
10194575Sdes
10294575Sdes	/* check the result of the update */
10394575Sdes	if (result == NULL || *result != 0) {
10494575Sdes		ypclnt_error(ypclnt, __func__,
10594575Sdes		    "NIS password update failed");
10694575Sdes		/* XXX how do we get more details? */
10794575Sdes		ret = -1;
10894575Sdes		goto done;
10994575Sdes	}
11094575Sdes
11194575Sdes	ypclnt_error(ypclnt, NULL, NULL);
11294575Sdes	ret = 0;
11394575Sdes
11494575Sdes done:
11594575Sdes	if (clnt != NULL) {
11694575Sdes		auth_destroy(clnt->cl_auth);
11794575Sdes		clnt_destroy(clnt);
11894575Sdes	}
11994575Sdes	free(yppwd.newpw.pw_name);
12094575Sdes	free(yppwd.newpw.pw_passwd);
12194575Sdes	free(yppwd.newpw.pw_gecos);
12294575Sdes	free(yppwd.newpw.pw_dir);
12394575Sdes	free(yppwd.newpw.pw_shell);
12494575Sdes	if (yppwd.oldpass != NULL) {
12594575Sdes		memset(yppwd.oldpass, 0, strlen(yppwd.oldpass));
12694575Sdes		free(yppwd.oldpass);
12794575Sdes	}
12894575Sdes	return (ret);
12994575Sdes}
130