1 /* 2 * Sun RPC is a product of Sun Microsystems, Inc. and is provided for 3 * unrestricted use provided that this legend is included on all tape 4 * media and as a part of the software program in whole or part. Users 5 * may copy or modify Sun RPC without charge, but are not authorized 6 * to license or distribute it to anyone else except as part of a product or 7 * program developed by the user or with the express written consent of 8 * Sun Microsystems, Inc. 9 * 10 * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE 11 * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR 12 * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE. 13 * 14 * Sun RPC is provided with no support and without any obligation on the 15 * part of Sun Microsystems, Inc. to assist in its use, correction, 16 * modification or enhancement. 17 * 18 * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE 19 * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC 20 * OR ANY PART THEREOF. 21 * 22 * In no event will Sun Microsystems, Inc. be liable for any lost revenue 23 * or profits or other special, indirect and consequential damages, even if 24 * Sun has been advised of the possibility of such damages. 25 * 26 * Sun Microsystems, Inc. 27 * 2550 Garcia Avenue 28 * Mountain View, California 94043 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)() = 0; 62 63 /* 64 * Get somebody's public key 65 */ 66 static int 67 __getpublickey_real(netname, publickey) 68 const char *netname; 69 char *publickey; 70 { 71 char lookup[3 * HEXKEYBYTES]; 72 char *p; 73 74 if (publickey == NULL) 75 return (0); 76 if (!getpublicandprivatekey(netname, lookup)) 77 return (0); 78 p = strchr(lookup, ':'); 79 if (p == NULL) { 80 return (0); 81 } 82 *p = '\0'; 83 (void) strncpy(publickey, lookup, HEXKEYBYTES); 84 publickey[HEXKEYBYTES] = '\0'; 85 return (1); 86 } 87 88 /* 89 * reads the file /etc/publickey looking for a + to optionally go to the 90 * yellow pages 91 */ 92 93 int 94 getpublicandprivatekey(key, ret) 95 const char *key; 96 char *ret; 97 { 98 char buf[1024]; /* big enough */ 99 char *res; 100 FILE *fd; 101 char *mkey; 102 char *mval; 103 104 fd = fopen(PKFILE, "r"); 105 if (fd == NULL) 106 return (0); 107 for (;;) { 108 res = fgets(buf, sizeof(buf), fd); 109 if (res == NULL) { 110 fclose(fd); 111 return (0); 112 } 113 if (res[0] == '#') 114 continue; 115 else if (res[0] == '+') { 116 #ifdef YP 117 char *PKMAP = "publickey.byname"; 118 char *lookup; 119 char *domain; 120 int err; 121 int len; 122 123 err = yp_get_default_domain(&domain); 124 if (err) { 125 continue; 126 } 127 lookup = NULL; 128 err = yp_match(domain, PKMAP, key, strlen(key), &lookup, &len); 129 if (err) { 130 #ifdef DEBUG 131 fprintf(stderr, "match failed error %d\n", err); 132 #endif 133 continue; 134 } 135 lookup[len] = 0; 136 strcpy(ret, lookup); 137 fclose(fd); 138 free(lookup); 139 return (2); 140 #else /* YP */ 141 #ifdef DEBUG 142 fprintf(stderr, 143 "Bad record in %s '+' -- NIS not supported in this library copy\n", PKFILE); 144 #endif /* DEBUG */ 145 continue; 146 #endif /* YP */ 147 } else { 148 mkey = strsep(&res, "\t "); 149 if (mkey == NULL) { 150 fprintf(stderr, 151 "Bad record in %s -- %s", PKFILE, buf); 152 continue; 153 } 154 do { 155 mval = strsep(&res, " \t#\n"); 156 } while (mval != NULL && !*mval); 157 if (mval == NULL) { 158 fprintf(stderr, 159 "Bad record in %s val problem - %s", PKFILE, buf); 160 continue; 161 } 162 if (strcmp(mkey, key) == 0) { 163 strcpy(ret, mval); 164 fclose(fd); 165 return (1); 166 } 167 } 168 } 169 } 170 171 int getpublickey(netname, publickey) 172 const char *netname; 173 char *publickey; 174 { 175 if (__getpublickey_LOCAL != NULL) 176 return(__getpublickey_LOCAL(netname, publickey)); 177 else 178 return(__getpublickey_real(netname, publickey)); 179 } 180