1 /*
2  * Copyright (c) 2009, Sun Microsystems, Inc.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  * - Redistributions of source code must retain the above copyright notice,
8  *   this list of conditions and the following disclaimer.
9  * - Redistributions in binary form must reproduce the above copyright notice,
10  *   this list of conditions and the following disclaimer in the documentation
11  *   and/or other materials provided with the distribution.
12  * - Neither the name of Sun Microsystems, Inc. nor the names of its
13  *   contributors may be used to endorse or promote products derived
14  *   from this software without specific prior written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26  * POSSIBILITY OF SUCH DAMAGE.
27  */
28 /*
29 #include <sys/cdefs.h>
30 */
31 
32 /*
33  * publickey.c
34  * Copyright (C) 1986, Sun Microsystems, Inc.
35  */
36 
37 /*
38  * Public key lookup routines
39  */
40 #include <wintirpc.h>
41 #ifndef _WIN32
42 #include <stdio.h>
43 //#include <pwd.h>
44 #include <rpc/rpc.h>
45 #include <rpc/key_prot.h>
46 #include <rpcsvc/yp_prot.h>
47 #include <rpcsvc/ypclnt.h>
48 #include <string.h>
49 #include <stdlib.h>
50 
51 #define PKFILE "/etc/publickey"
52 
53 /*
54  * Hack to let ypserv/rpc.nisd use AUTH_DES.
55  */
56 int (*__getpublickey_LOCAL)() = 0;
57 
58 /*
59  * Get somebody's public key
60  */
61 int
__getpublickey_real(netname,publickey)62 __getpublickey_real(netname, publickey)
63 	char *netname;
64 	char *publickey;
65 {
66 	char lookup[3 * HEXKEYBYTES];
67 	char *p;
68 
69 	if (publickey == NULL)
70 		return (0);
71 	if (!getpublicandprivatekey(netname, lookup))
72 		return (0);
73 	p = strchr(lookup, ':');
74 	if (p == NULL) {
75 		return (0);
76 	}
77 	*p = '\0';
78 	(void) strncpy(publickey, lookup, HEXKEYBYTES);
79 	publickey[HEXKEYBYTES] = '\0';
80 	return (1);
81 }
82 
83 /*
84  * reads the file /etc/publickey looking for a + to optionally go to the
85  * yellow pages
86  */
87 
88 int
getpublicandprivatekey(key,ret)89 getpublicandprivatekey(key, ret)
90 	char *key;
91 	char *ret;
92 {
93 	char buf[1024];	/* big enough */
94 	char *res;
95 	FILE *fd;
96 	char *mkey;
97 	char *mval;
98 
99 	fd = fopen(PKFILE, "r");
100 	if (fd == NULL)
101 		return (0);
102 	for (;;) {
103 		res = fgets(buf, sizeof(buf), fd);
104 		if (res == NULL) {
105 			fclose(fd);
106 			return (0);
107 		}
108 		if (res[0] == '#')
109 			continue;
110 		else if (res[0] == '+') {
111 #ifdef YP
112 			char *PKMAP = "publickey.byname";
113 			char *lookup;
114 			char *domain;
115 			int err;
116 			int len;
117 
118 			err = yp_get_default_domain(&domain);
119 			if (err) {
120 				continue;
121 			}
122 			lookup = NULL;
123 			err = yp_match(domain, PKMAP, key, strlen(key), &lookup, &len);
124 			if (err) {
125 #ifdef DEBUG
126 				fprintf(stderr, "match failed error %d\n", err);
127 #endif
128 				continue;
129 			}
130 			lookup[len] = 0;
131 			strcpy(ret, lookup);
132 			fclose(fd);
133 			free(lookup);
134 			return (2);
135 #else /* YP */
136 #ifdef DEBUG
137 			fprintf(stderr,
138 "Bad record in %s '+' -- NIS not supported in this library copy\n", PKFILE);
139 #endif /* DEBUG */
140 			continue;
141 #endif /* YP */
142 		} else {
143 			mkey = strsep(&res, "\t ");
144 			if (mkey == NULL) {
145 				fprintf(stderr,
146 				"Bad record in %s -- %s", PKFILE, buf);
147 				continue;
148 			}
149 			do {
150 				mval = strsep(&res, " \t#\n");
151 			} while (mval != NULL && !*mval);
152 			if (mval == NULL) {
153 				fprintf(stderr,
154 			"Bad record in %s val problem - %s", PKFILE, buf);
155 				continue;
156 			}
157 			if (strcmp(mkey, key) == 0) {
158 				strcpy(ret, mval);
159 				fclose(fd);
160 				return (1);
161 			}
162 		}
163 	}
164 }
165 
getpublickey(netname,publickey)166 int getpublickey(netname, publickey)
167 	const char *netname;
168 	char *publickey;
169 {
170 	if (__getpublickey_LOCAL != NULL)
171 		return(__getpublickey_LOCAL(netname, publickey));
172 	else
173 		return(__getpublickey_real(netname, publickey));
174 }
175 #endif	/* ! _WIN32 */
176