/*-1* SPDX-License-Identifier: BSD-3-Clause2*3* Copyright (c) 2009, Sun Microsystems, Inc.4* All rights reserved.5*6* Redistribution and use in source and binary forms, with or without7* modification, are permitted provided that the following conditions are met:8* - Redistributions of source code must retain the above copyright notice,9* this list of conditions and the following disclaimer.10* - Redistributions in binary form must reproduce the above copyright notice,11* this list of conditions and the following disclaimer in the documentation12* and/or other materials provided with the distribution.13* - Neither the name of Sun Microsystems, Inc. nor the names of its14* contributors may be used to endorse or promote products derived15* from this software without specific prior written permission.16*17* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"18* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE19* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE20* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE21* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR22* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF23* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS24* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN25* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)26* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE27* POSSIBILITY OF SUCH DAMAGE.28*/2930/*31* publickey.c32* Copyright (C) 1986, Sun Microsystems, Inc.33*/3435/*36* Public key lookup routines37*/38#include "namespace.h"39#include <stdio.h>40#include <pwd.h>41#include <rpc/rpc.h>42#include <rpc/key_prot.h>43#include <rpcsvc/yp_prot.h>44#include <rpcsvc/ypclnt.h>45#include <string.h>46#include <stdlib.h>47#include "un-namespace.h"4849#define PKFILE "/etc/publickey"5051/*52* Hack to let ypserv/rpc.nisd use AUTH_DES.53*/54int (*__getpublickey_LOCAL)(const char *, char *) = 0;5556/*57* Get somebody's public key58*/59static int60__getpublickey_real(const char *netname, char *publickey)61{62char lookup[3 * HEXKEYBYTES];63char *p;6465if (publickey == NULL)66return (0);67if (!getpublicandprivatekey(netname, lookup))68return (0);69p = strchr(lookup, ':');70if (p == NULL) {71return (0);72}73*p = '\0';74(void) strncpy(publickey, lookup, HEXKEYBYTES);75publickey[HEXKEYBYTES] = '\0';76return (1);77}7879/*80* reads the file /etc/publickey looking for a + to optionally go to the81* yellow pages82*/8384int85getpublicandprivatekey(const char *key, char *ret)86{87char buf[1024]; /* big enough */88char *res;89FILE *fd;90char *mkey;91char *mval;9293fd = fopen(PKFILE, "r");94if (fd == NULL)95return (0);96for (;;) {97res = fgets(buf, sizeof(buf), fd);98if (res == NULL) {99fclose(fd);100return (0);101}102if (res[0] == '#')103continue;104else if (res[0] == '+') {105#ifdef YP106char *PKMAP = "publickey.byname";107char *lookup;108char *domain;109int err;110int len;111112err = yp_get_default_domain(&domain);113if (err) {114continue;115}116lookup = NULL;117err = yp_match(domain, PKMAP, key, strlen(key), &lookup, &len);118if (err) {119#ifdef DEBUG120fprintf(stderr, "match failed error %d\n", err);121#endif122continue;123}124lookup[len] = 0;125strcpy(ret, lookup);126fclose(fd);127free(lookup);128return (2);129#else /* YP */130#ifdef DEBUG131fprintf(stderr,132"Bad record in %s '+' -- NIS not supported in this library copy\n", PKFILE);133#endif /* DEBUG */134continue;135#endif /* YP */136} else {137mkey = strsep(&res, "\t ");138if (mkey == NULL) {139fprintf(stderr,140"Bad record in %s -- %s", PKFILE, buf);141continue;142}143do {144mval = strsep(&res, " \t#\n");145} while (mval != NULL && !*mval);146if (mval == NULL) {147fprintf(stderr,148"Bad record in %s val problem - %s", PKFILE, buf);149continue;150}151if (strcmp(mkey, key) == 0) {152strcpy(ret, mval);153fclose(fd);154return (1);155}156}157}158}159160int getpublickey(const char *netname, char *publickey)161{162if (__getpublickey_LOCAL != NULL)163return(__getpublickey_LOCAL(netname, publickey));164else165return(__getpublickey_real(netname, publickey));166}167168169