126213Swpaul/*
226213Swpaul * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
326213Swpaul * unrestricted use provided that this legend is included on all tape
426213Swpaul * media and as a part of the software program in whole or part.  Users
526213Swpaul * may copy or modify Sun RPC without charge, but are not authorized
626213Swpaul * to license or distribute it to anyone else except as part of a product or
726213Swpaul * program developed by the user or with the express written consent of
826213Swpaul * Sun Microsystems, Inc.
926213Swpaul *
1026213Swpaul * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
1126213Swpaul * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
1226213Swpaul * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
1326213Swpaul *
1426213Swpaul * Sun RPC is provided with no support and without any obligation on the
1526213Swpaul * part of Sun Microsystems, Inc. to assist in its use, correction,
1626213Swpaul * modification or enhancement.
1726213Swpaul *
1826213Swpaul * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
1926213Swpaul * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
2026213Swpaul * OR ANY PART THEREOF.
2126213Swpaul *
2226213Swpaul * In no event will Sun Microsystems, Inc. be liable for any lost revenue
2326213Swpaul * or profits or other special, indirect and consequential damages, even if
2426213Swpaul * Sun has been advised of the possibility of such damages.
2526213Swpaul *
2626213Swpaul * Sun Microsystems, Inc.
2726213Swpaul * 2550 Garcia Avenue
2826213Swpaul * Mountain View, California  94043
2926213Swpaul */
3084220Sdillon
3184220Sdillon#include <sys/cdefs.h>
3284220Sdillon__FBSDID("$FreeBSD: releng/10.3/lib/librpcsvc/secretkey.c 189087 2009-02-26 20:32:11Z ed $");
3384220Sdillon
3426213Swpaul#if !defined(lint) && defined(SCCSIDS)
3526213Swpaulstatic char sccsid[] = "@(#)secretkey.c 1.8 91/03/11 Copyr 1986 Sun Micro";
3626213Swpaul#endif
3726213Swpaul
3826213Swpaul/*
3926213Swpaul * secretkey.c
4026213Swpaul * Copyright (C) 1986, Sun Microsystems, Inc.
4126213Swpaul */
4226213Swpaul
4326213Swpaul/*
4426213Swpaul * Secret key lookup routines
4526213Swpaul */
4626213Swpaul#include <stdio.h>
4726213Swpaul#include <pwd.h>
4826213Swpaul#include <rpc/rpc.h>
4926213Swpaul#include <rpc/key_prot.h>
5026213Swpaul#include <rpcsvc/yp_prot.h>
5126213Swpaul#include <rpcsvc/ypclnt.h>
5226213Swpaul#include <string.h>
5326213Swpaul
5492917Sobrienextern int xdecrypt( char *, char * );
5526213Swpaul
5626213Swpaul/*
5726213Swpaul * Get somebody's encrypted secret key from the database, using the given
5826213Swpaul * passwd to decrypt it.
5926213Swpaul */
6026213Swpaulint
61189087Sedgetsecretkey(char *netname, char *secretkey, char *passwd)
6226213Swpaul{
6326213Swpaul	char lookup[3 * HEXKEYBYTES];
6426213Swpaul	char *p;
6526213Swpaul
6626213Swpaul	if (secretkey == NULL)
6726213Swpaul		return (0);
6826213Swpaul	if (!getpublicandprivatekey(netname, lookup))
6926213Swpaul		return (0);
7026213Swpaul	p = strchr(lookup, ':');
7126213Swpaul	if (p == NULL) {
7226213Swpaul		return (0);
7326213Swpaul	}
7426213Swpaul	p++;
7526213Swpaul	if (!xdecrypt(p, passwd)) {
7626213Swpaul		return (0);
7726213Swpaul	}
7826213Swpaul	if (memcmp(p, p + HEXKEYBYTES, KEYCHECKSUMSIZE) != 0) {
7926213Swpaul		secretkey[0] = '\0';
8026213Swpaul		return (1);
8126213Swpaul	}
8226213Swpaul	p[HEXKEYBYTES] = '\0';
8326213Swpaul	(void) strncpy(secretkey, p, HEXKEYBYTES);
8426213Swpaul	secretkey[HEXKEYBYTES] = '\0';
8526213Swpaul	return (1);
8626213Swpaul}
87