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