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