| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
#include <stdio.h> |
| 38 |
#include <pwd.h> |
| 39 |
#include <rpc/rpc.h> |
| 40 |
#include <rpc/key_prot.h> |
| 41 |
#ifdef YP |
| 42 |
#include <rpcsvc/yp_prot.h> |
| 43 |
#include <rpcsvc/ypclnt.h> |
| 44 |
#endif |
| 45 |
#include <string.h> |
| 46 |
#include <stdlib.h> |
| 47 |
|
| 48 |
#include "debug.h" |
| 49 |
|
| 50 |
#define PKFILE "/etc/publickey" |
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
int (*__getpublickey_LOCAL)() = 0; |
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
int |
| 61 |
__getpublickey_real(netname, publickey) |
| 62 |
char *netname; |
| 63 |
char *publickey; |
| 64 |
{ |
| 65 |
char lookup[3 * HEXKEYBYTES]; |
| 66 |
char *p; |
| 67 |
|
| 68 |
if (publickey == NULL) |
| 69 |
return (0); |
| 70 |
if (!getpublicandprivatekey(netname, lookup)) |
| 71 |
return (0); |
| 72 |
p = strchr(lookup, ':'); |
| 73 |
if (p == NULL) { |
| 74 |
return (0); |
| 75 |
} |
| 76 |
*p = '\0'; |
| 77 |
memcpy(publickey, lookup, HEXKEYBYTES); |
| 78 |
publickey[HEXKEYBYTES] = '\0'; |
| 79 |
return (1); |
| 80 |
} |
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
int |
| 88 |
getpublicandprivatekey(key, ret) |
| 89 |
char *key; |
| 90 |
char *ret; |
| 91 |
{ |
| 92 |
char buf[1024]; |
| 93 |
char *res; |
| 94 |
FILE *fd; |
| 95 |
char *mkey; |
| 96 |
char *mval; |
| 97 |
|
| 98 |
fd = fopen(PKFILE, "r"); |
| 99 |
if (fd == NULL) |
| 100 |
return (0); |
| 101 |
for (;;) { |
| 102 |
res = fgets(buf, sizeof(buf), fd); |
| 103 |
if (res == NULL) { |
| 104 |
fclose(fd); |
| 105 |
return (0); |
| 106 |
} |
| 107 |
if (res[0] == '#') |
| 108 |
continue; |
| 109 |
else if (res[0] == '+') { |
| 110 |
#ifdef YP |
| 111 |
char *PKMAP = "publickey.byname"; |
| 112 |
char *lookup; |
| 113 |
char *domain; |
| 114 |
int err; |
| 115 |
int len; |
| 116 |
|
| 117 |
err = yp_get_default_domain(&domain); |
| 118 |
if (err) { |
| 119 |
continue; |
| 120 |
} |
| 121 |
lookup = NULL; |
| 122 |
err = yp_match(domain, PKMAP, key, strlen(key), &lookup, &len); |
| 123 |
if (err) { |
| 124 |
LIBTIRPC_DEBUG(1, |
| 125 |
("getpublicandprivatekey: match failed error %d\n", err)); |
| 126 |
continue; |
| 127 |
} |
| 128 |
lookup[len] = 0; |
| 129 |
strcpy(ret, lookup); |
| 130 |
fclose(fd); |
| 131 |
free(lookup); |
| 132 |
return (2); |
| 133 |
#else |
| 134 |
LIBTIRPC_DEBUG(1, |
| 135 |
("Bad record in %s '+' -- NIS not supported in this library copy\n", PKFILE)); |
| 136 |
continue; |
| 137 |
#endif |
| 138 |
} else { |
| 139 |
mkey = strsep(&res, "\t "); |
| 140 |
if (mkey == NULL) { |
| 141 |
fprintf(stderr, |
| 142 |
"Bad record in %s -- %s", PKFILE, buf); |
| 143 |
continue; |
| 144 |
} |
| 145 |
do { |
| 146 |
mval = strsep(&res, " \t#\n"); |
| 147 |
} while (mval != NULL && !*mval); |
| 148 |
if (mval == NULL) { |
| 149 |
fprintf(stderr, |
| 150 |
"Bad record in %s val problem - %s", PKFILE, buf); |
| 151 |
continue; |
| 152 |
} |
| 153 |
if (strcmp(mkey, key) == 0) { |
| 154 |
strcpy(ret, mval); |
| 155 |
fclose(fd); |
| 156 |
return (1); |
| 157 |
} |
| 158 |
} |
| 159 |
} |
| 160 |
} |
| 161 |
|
| 162 |
int getpublickey(netname, publickey) |
| 163 |
const char *netname; |
| 164 |
char *publickey; |
| 165 |
{ |
| 166 |
if (__getpublickey_LOCAL != NULL) |
| 167 |
return(__getpublickey_LOCAL(netname, publickey)); |
| 168 |
else |
| 169 |
return(__getpublickey_real(netname, publickey)); |
| 170 |
} |