1 /*- 2 * SPDX-License-Identifier: BSD-3-Clause 3 * 4 * Copyright (c) 2009, Sun Microsystems, Inc. 5 * All rights reserved. 6 * 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. 17 * 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. 29 */ 30 31 #if defined(LIBC_SCCS) && !defined(lint) 32 static char sccsid[] = "@(#)publickey.c 1.10 91/03/11 Copyr 1986 Sun Micro"; 33 #endif 34 #include <sys/cdefs.h> 35 __FBSDID("$FreeBSD$"); 36 37 /* 38 * publickey.c 39 * Copyright (C) 1986, Sun Microsystems, Inc. 40 */ 41 42 /* 43 * Public key lookup routines 44 */ 45 #include "namespace.h" 46 #include <stdio.h> 47 #include <pwd.h> 48 #include <rpc/rpc.h> 49 #include <rpc/key_prot.h> 50 #include <rpcsvc/yp_prot.h> 51 #include <rpcsvc/ypclnt.h> 52 #include <string.h> 53 #include <stdlib.h> 54 #include "un-namespace.h" 55 56 #define PKFILE "/etc/publickey" 57 58 /* 59 * Hack to let ypserv/rpc.nisd use AUTH_DES. 60 */ 61 int (*__getpublickey_LOCAL)(const char *, char *) = 0; 62 63 /* 64 * Get somebody's public key 65 */ 66 static int 67 __getpublickey_real(const char *netname, char *publickey) 68 { 69 char lookup[3 * HEXKEYBYTES]; 70 char *p; 71 72 if (publickey == NULL) 73 return (0); 74 if (!getpublicandprivatekey(netname, lookup)) 75 return (0); 76 p = strchr(lookup, ':'); 77 if (p == NULL) { 78 return (0); 79 } 80 *p = '\0'; 81 (void) strncpy(publickey, lookup, HEXKEYBYTES); 82 publickey[HEXKEYBYTES] = '\0'; 83 return (1); 84 } 85 86 /* 87 * reads the file /etc/publickey looking for a + to optionally go to the 88 * yellow pages 89 */ 90 91 int 92 getpublicandprivatekey(const char *key, char *ret) 93 { 94 char buf[1024]; /* big enough */ 95 char *res; 96 FILE *fd; 97 char *mkey; 98 char *mval; 99 100 fd = fopen(PKFILE, "r"); 101 if (fd == NULL) 102 return (0); 103 for (;;) { 104 res = fgets(buf, sizeof(buf), fd); 105 if (res == NULL) { 106 fclose(fd); 107 return (0); 108 } 109 if (res[0] == '#') 110 continue; 111 else if (res[0] == '+') { 112 #ifdef YP 113 char *PKMAP = "publickey.byname"; 114 char *lookup; 115 char *domain; 116 int err; 117 int len; 118 119 err = yp_get_default_domain(&domain); 120 if (err) { 121 continue; 122 } 123 lookup = NULL; 124 err = yp_match(domain, PKMAP, key, strlen(key), &lookup, &len); 125 if (err) { 126 #ifdef DEBUG 127 fprintf(stderr, "match failed error %d\n", err); 128 #endif 129 continue; 130 } 131 lookup[len] = 0; 132 strcpy(ret, lookup); 133 fclose(fd); 134 free(lookup); 135 return (2); 136 #else /* YP */ 137 #ifdef DEBUG 138 fprintf(stderr, 139 "Bad record in %s '+' -- NIS not supported in this library copy\n", PKFILE); 140 #endif /* DEBUG */ 141 continue; 142 #endif /* YP */ 143 } else { 144 mkey = strsep(&res, "\t "); 145 if (mkey == NULL) { 146 fprintf(stderr, 147 "Bad record in %s -- %s", PKFILE, buf); 148 continue; 149 } 150 do { 151 mval = strsep(&res, " \t#\n"); 152 } while (mval != NULL && !*mval); 153 if (mval == NULL) { 154 fprintf(stderr, 155 "Bad record in %s val problem - %s", PKFILE, buf); 156 continue; 157 } 158 if (strcmp(mkey, key) == 0) { 159 strcpy(ret, mval); 160 fclose(fd); 161 return (1); 162 } 163 } 164 } 165 } 166 167 int getpublickey(const char *netname, char *publickey) 168 { 169 if (__getpublickey_LOCAL != NULL) 170 return(__getpublickey_LOCAL(netname, publickey)); 171 else 172 return(__getpublickey_real(netname, publickey)); 173 } 174