1331722Seadler/*
226219Swpaul * Copyright (c) 1996
326219Swpaul *	Bill Paul <wpaul@ctr.columbia.edu>.  All rights reserved.
426219Swpaul *
526219Swpaul * Redistribution and use in source and binary forms, with or without
626219Swpaul * modification, are permitted provided that the following conditions
726219Swpaul * are met:
826219Swpaul * 1. Redistributions of source code must retain the above copyright
926219Swpaul *    notice, this list of conditions and the following disclaimer.
1026219Swpaul * 2. Redistributions in binary form must reproduce the above copyright
1126219Swpaul *    notice, this list of conditions and the following disclaimer in the
1226219Swpaul *    documentation and/or other materials provided with the distribution.
1326219Swpaul * 3. All advertising materials mentioning features or use of this software
1426219Swpaul *    must display the following acknowledgement:
1526219Swpaul *	This product includes software developed by Bill Paul.
1626219Swpaul * 4. Neither the name of the author nor the names of any co-contributors
1726219Swpaul *    may be used to endorse or promote products derived from this software
1826219Swpaul *    without specific prior written permission.
1926219Swpaul *
2026219Swpaul * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND
2126219Swpaul * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2226219Swpaul * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2326219Swpaul * ARE DISCLAIMED.  IN NO EVENT SHALL Bill Paul OR CONTRIBUTORS BE LIABLE
2426219Swpaul * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2526219Swpaul * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2626219Swpaul * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2726219Swpaul * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2826219Swpaul * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2926219Swpaul * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3026219Swpaul * SUCH DAMAGE.
3126219Swpaul */
3226219Swpaul
3392990Sobrien#include <sys/cdefs.h>
3492990Sobrien__FBSDID("$FreeBSD$");
3592990Sobrien
3674462Salfred#include "namespace.h"
37111010Snectar#include <err.h>
3826219Swpaul#include <sys/types.h>
3926219Swpaul#include <rpc/des_crypt.h>
4026219Swpaul#include <rpc/des.h>
4126219Swpaul#include <string.h>
4226219Swpaul#include <rpcsvc/crypt.h>
4374462Salfred#include "un-namespace.h"
4426219Swpaul
4526219Swpaulint
46287341Srodrigc_des_crypt_call(char *buf, int len, struct desparams *dparms)
4726219Swpaul{
4826219Swpaul	CLIENT *clnt;
4926219Swpaul	desresp  *result_1;
5026219Swpaul	desargs  des_crypt_1_arg;
5190256Salfred	struct netconfig *nconf;
5290256Salfred	void *localhandle;
5390256Salfred	int stat;
5426219Swpaul
5590256Salfred	nconf = NULL;
5690256Salfred	localhandle = setnetconfig();
5790256Salfred	while ((nconf = getnetconfig(localhandle)) != NULL) {
5890256Salfred		if (nconf->nc_protofmly != NULL &&
5990256Salfred		     strcmp(nconf->nc_protofmly, NC_LOOPBACK) == 0)
6090256Salfred			break;
6190256Salfred	}
6290256Salfred	if (nconf == NULL) {
6390256Salfred		warnx("getnetconfig: %s", nc_sperror());
64278039Spfg		endnetconfig(localhandle);
6590256Salfred		return(DESERR_HWERROR);
6690256Salfred	}
6790256Salfred	clnt = clnt_tp_create(NULL, CRYPT_PROG, CRYPT_VERS, nconf);
6826219Swpaul	if (clnt == (CLIENT *) NULL) {
6990256Salfred		endnetconfig(localhandle);
7026219Swpaul		return(DESERR_HWERROR);
7126219Swpaul	}
7290256Salfred	endnetconfig(localhandle);
7326219Swpaul
7426219Swpaul	des_crypt_1_arg.desbuf.desbuf_len = len;
7526219Swpaul	des_crypt_1_arg.desbuf.desbuf_val = buf;
76228538Sdim	des_crypt_1_arg.des_dir = (dparms->des_dir == ENCRYPT) ? ENCRYPT_DES : DECRYPT_DES;
77228538Sdim	des_crypt_1_arg.des_mode = (dparms->des_mode == CBC) ? CBC_DES : ECB_DES;
7826219Swpaul	bcopy(dparms->des_ivec, des_crypt_1_arg.des_ivec, 8);
7926219Swpaul	bcopy(dparms->des_key, des_crypt_1_arg.des_key, 8);
8026219Swpaul
8126219Swpaul	result_1 = des_crypt_1(&des_crypt_1_arg, clnt);
8226219Swpaul	if (result_1 == (desresp *) NULL) {
8336816Swpaul		clnt_destroy(clnt);
8426219Swpaul		return(DESERR_HWERROR);
8526219Swpaul	}
8626219Swpaul
8726219Swpaul	stat = result_1->stat;
8826219Swpaul
8926219Swpaul	if (result_1->stat == DESERR_NONE ||
9026219Swpaul	    result_1->stat == DESERR_NOHWDEVICE) {
9126219Swpaul		bcopy(result_1->desbuf.desbuf_val, buf, len);
9226219Swpaul		bcopy(result_1->des_ivec, dparms->des_ivec, 8);
9326219Swpaul	}
9426219Swpaul
9595658Sdes	clnt_freeres(clnt, (xdrproc_t)xdr_desresp, result_1);
9626219Swpaul	clnt_destroy(clnt);
9726219Swpaul
9826219Swpaul	return(stat);
9926219Swpaul}
100