1261057Smav/*-
2261057Smav * Copyright (c) 2009, Sun Microsystems, Inc.
3261057Smav * All rights reserved.
428877Sjdp *
5261057Smav * Redistribution and use in source and binary forms, with or without
6261057Smav * modification, are permitted provided that the following conditions are met:
7261057Smav * - Redistributions of source code must retain the above copyright notice,
8261057Smav *   this list of conditions and the following disclaimer.
9261057Smav * - Redistributions in binary form must reproduce the above copyright notice,
10261057Smav *   this list of conditions and the following disclaimer in the documentation
11261057Smav *   and/or other materials provided with the distribution.
12261057Smav * - Neither the name of Sun Microsystems, Inc. nor the names of its
13261057Smav *   contributors may be used to endorse or promote products derived
14261057Smav *   from this software without specific prior written permission.
15261057Smav *
16261057Smav * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17261057Smav * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18261057Smav * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19261057Smav * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20261057Smav * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21261057Smav * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22261057Smav * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23261057Smav * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24261057Smav * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25261057Smav * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26261057Smav * POSSIBILITY OF SUCH DAMAGE.
2728877Sjdp */
28136581Sobrien
29136581Sobrien#if defined(LIBC_SCCS) && !defined(lint)
3036165Swpaulstatic char sccsid[] = "@(#)publickey.c 1.10 91/03/11 Copyr 1986 Sun Micro";
3136165Swpaul#endif
3292990Sobrien#include <sys/cdefs.h>
3392990Sobrien__FBSDID("$FreeBSD$");
3428877Sjdp
3528877Sjdp/*
3636165Swpaul * publickey.c
3736165Swpaul * Copyright (C) 1986, Sun Microsystems, Inc.
3828877Sjdp */
3928877Sjdp
4036165Swpaul/*
4136165Swpaul * Public key lookup routines
4236165Swpaul */
4374462Salfred#include "namespace.h"
4436165Swpaul#include <stdio.h>
4536165Swpaul#include <pwd.h>
4636165Swpaul#include <rpc/rpc.h>
4736165Swpaul#include <rpc/key_prot.h>
4836165Swpaul#include <rpcsvc/yp_prot.h>
4936165Swpaul#include <rpcsvc/ypclnt.h>
5036165Swpaul#include <string.h>
5136165Swpaul#include <stdlib.h>
5274462Salfred#include "un-namespace.h"
5328877Sjdp
5436165Swpaul#define PKFILE "/etc/publickey"
5536165Swpaul
5636165Swpaul/*
5736165Swpaul * Hack to let ypserv/rpc.nisd use AUTH_DES.
5836165Swpaul */
5936165Swpaulint (*__getpublickey_LOCAL)() = 0;
6036165Swpaul
6136165Swpaul/*
6236165Swpaul * Get somebody's public key
6336165Swpaul */
64156090Sdeischenstatic int
6536165Swpaul__getpublickey_real(netname, publickey)
66156109Sdeischen	const char *netname;
6736165Swpaul	char *publickey;
6828877Sjdp{
6936165Swpaul	char lookup[3 * HEXKEYBYTES];
7036165Swpaul	char *p;
7136165Swpaul
7236165Swpaul	if (publickey == NULL)
7336165Swpaul		return (0);
7436165Swpaul	if (!getpublicandprivatekey(netname, lookup))
7536165Swpaul		return (0);
7636165Swpaul	p = strchr(lookup, ':');
7736165Swpaul	if (p == NULL) {
7836165Swpaul		return (0);
7936165Swpaul	}
8036165Swpaul	*p = '\0';
8136165Swpaul	(void) strncpy(publickey, lookup, HEXKEYBYTES);
8236165Swpaul	publickey[HEXKEYBYTES] = '\0';
8336165Swpaul	return (1);
8428877Sjdp}
8536165Swpaul
8636165Swpaul/*
8736165Swpaul * reads the file /etc/publickey looking for a + to optionally go to the
8836165Swpaul * yellow pages
8936165Swpaul */
9036165Swpaul
9136165Swpaulint
9236165Swpaulgetpublicandprivatekey(key, ret)
93156109Sdeischen	const char *key;
9436165Swpaul	char *ret;
9536165Swpaul{
9636165Swpaul	char buf[1024];	/* big enough */
9736165Swpaul	char *res;
9836165Swpaul	FILE *fd;
9936165Swpaul	char *mkey;
10036165Swpaul	char *mval;
10136165Swpaul
10236165Swpaul	fd = fopen(PKFILE, "r");
10365220Sache	if (fd == NULL)
10436165Swpaul		return (0);
10536165Swpaul	for (;;) {
10665220Sache		res = fgets(buf, sizeof(buf), fd);
10765220Sache		if (res == NULL) {
10836165Swpaul			fclose(fd);
10936165Swpaul			return (0);
11036165Swpaul		}
11136165Swpaul		if (res[0] == '#')
11236165Swpaul			continue;
11336165Swpaul		else if (res[0] == '+') {
11436165Swpaul#ifdef YP
11536165Swpaul			char *PKMAP = "publickey.byname";
11636165Swpaul			char *lookup;
11736165Swpaul			char *domain;
11836165Swpaul			int err;
11936165Swpaul			int len;
12036165Swpaul
12136165Swpaul			err = yp_get_default_domain(&domain);
12236165Swpaul			if (err) {
12336165Swpaul				continue;
12436165Swpaul			}
12536165Swpaul			lookup = NULL;
12636165Swpaul			err = yp_match(domain, PKMAP, key, strlen(key), &lookup, &len);
12736165Swpaul			if (err) {
12836165Swpaul#ifdef DEBUG
12936165Swpaul				fprintf(stderr, "match failed error %d\n", err);
13036165Swpaul#endif
13136165Swpaul				continue;
13236165Swpaul			}
13336165Swpaul			lookup[len] = 0;
13436165Swpaul			strcpy(ret, lookup);
13536165Swpaul			fclose(fd);
13636165Swpaul			free(lookup);
13736165Swpaul			return (2);
13836165Swpaul#else /* YP */
13936165Swpaul#ifdef DEBUG
14036165Swpaul			fprintf(stderr,
14136165Swpaul"Bad record in %s '+' -- NIS not supported in this library copy\n", PKFILE);
14236165Swpaul#endif /* DEBUG */
14336165Swpaul			continue;
14436165Swpaul#endif /* YP */
14536165Swpaul		} else {
14665220Sache			mkey = strsep(&res, "\t ");
14736165Swpaul			if (mkey == NULL) {
14836165Swpaul				fprintf(stderr,
14936165Swpaul				"Bad record in %s -- %s", PKFILE, buf);
15036165Swpaul				continue;
15136165Swpaul			}
15265220Sache			do {
15365220Sache				mval = strsep(&res, " \t#\n");
15465220Sache			} while (mval != NULL && !*mval);
15536165Swpaul			if (mval == NULL) {
15636165Swpaul				fprintf(stderr,
15736165Swpaul			"Bad record in %s val problem - %s", PKFILE, buf);
15836165Swpaul				continue;
15936165Swpaul			}
16036165Swpaul			if (strcmp(mkey, key) == 0) {
16136165Swpaul				strcpy(ret, mval);
16236165Swpaul				fclose(fd);
16336165Swpaul				return (1);
16436165Swpaul			}
16536165Swpaul		}
16636165Swpaul	}
16736165Swpaul}
16836165Swpaul
16936165Swpaulint getpublickey(netname, publickey)
17090271Salfred	const char *netname;
17136165Swpaul	char *publickey;
17236165Swpaul{
17336165Swpaul	if (__getpublickey_LOCAL != NULL)
17436165Swpaul		return(__getpublickey_LOCAL(netname, publickey));
17536165Swpaul	else
17636165Swpaul		return(__getpublickey_real(netname, publickey));
17736165Swpaul}
178