2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 2009, Sun Microsystems, Inc.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions are met:
9 * - Redistributions of source code must retain the above copyright notice,
10 * this list of conditions and the following disclaimer.
11 * - Redistributions in binary form must reproduce the above copyright notice,
12 * this list of conditions and the following disclaimer in the documentation
13 * and/or other materials provided with the distribution.
14 * - Neither the name of Sun Microsystems, Inc. nor the names of its
15 * contributors may be used to endorse or promote products derived
16 * from this software without specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
22 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 * POSSIBILITY OF SUCH DAMAGE.
31 #if defined(LIBC_SCCS) && !defined(lint)
32 static char sccsid[] = "@(#)publickey.c 1.10 91/03/11 Copyr 1986 Sun Micro";
34 #include <sys/cdefs.h>
35 __FBSDID("$FreeBSD$");
39 * Copyright (C) 1986, Sun Microsystems, Inc.
43 * Public key lookup routines
45 #include "namespace.h"
49 #include <rpc/key_prot.h>
50 #include <rpcsvc/yp_prot.h>
51 #include <rpcsvc/ypclnt.h>
54 #include "un-namespace.h"
56 #define PKFILE "/etc/publickey"
59 * Hack to let ypserv/rpc.nisd use AUTH_DES.
61 int (*__getpublickey_LOCAL)(const char *, char *) = 0;
64 * Get somebody's public key
67 __getpublickey_real(const char *netname, char *publickey)
69 char lookup[3 * HEXKEYBYTES];
72 if (publickey == NULL)
74 if (!getpublicandprivatekey(netname, lookup))
76 p = strchr(lookup, ':');
81 (void) strncpy(publickey, lookup, HEXKEYBYTES);
82 publickey[HEXKEYBYTES] = '\0';
87 * reads the file /etc/publickey looking for a + to optionally go to the
92 getpublicandprivatekey(const char *key, char *ret)
94 char buf[1024]; /* big enough */
100 fd = fopen(PKFILE, "r");
104 res = fgets(buf, sizeof(buf), fd);
111 else if (res[0] == '+') {
113 char *PKMAP = "publickey.byname";
119 err = yp_get_default_domain(&domain);
124 err = yp_match(domain, PKMAP, key, strlen(key), &lookup, &len);
127 fprintf(stderr, "match failed error %d\n", err);
139 "Bad record in %s '+' -- NIS not supported in this library copy\n", PKFILE);
144 mkey = strsep(&res, "\t ");
147 "Bad record in %s -- %s", PKFILE, buf);
151 mval = strsep(&res, " \t#\n");
152 } while (mval != NULL && !*mval);
155 "Bad record in %s val problem - %s", PKFILE, buf);
158 if (strcmp(mkey, key) == 0) {
167 int getpublickey(const char *netname, char *publickey)
169 if (__getpublickey_LOCAL != NULL)
170 return(__getpublickey_LOCAL(netname, publickey));
172 return(__getpublickey_real(netname, publickey));