xref: /freebsd/lib/libc/rpc/getpublickey.c (revision 4cf49a43559ed9fdad601bdcccd2c55963008675)
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 #if !defined(lint) && defined(SCCSIDS)
31 static char sccsid[] = "@(#)publickey.c 1.10 91/03/11 Copyr 1986 Sun Micro";
32 #endif
33 
34 /*
35  * publickey.c
36  * Copyright (C) 1986, Sun Microsystems, Inc.
37  */
38 
39 /*
40  * Public key lookup routines
41  */
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
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
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 == (FILE *) 0)
101 		return (0);
102 	for (;;) {
103 		res = fgets(buf, 1024, fd);
104 		if (res == 0) {
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 = strtok(buf, "\t ");
144 			if (mkey == NULL) {
145 				fprintf(stderr,
146 				"Bad record in %s -- %s", PKFILE, buf);
147 				continue;
148 			}
149 			mval = strtok((char *)NULL, " \t#\n");
150 			if (mval == NULL) {
151 				fprintf(stderr,
152 			"Bad record in %s val problem - %s", PKFILE, buf);
153 				continue;
154 			}
155 			if (strcmp(mkey, key) == 0) {
156 				strcpy(ret, mval);
157 				fclose(fd);
158 				return (1);
159 			}
160 		}
161 	}
162 }
163 
164 int getpublickey(netname, publickey)
165 	char *netname;
166 	char *publickey;
167 {
168 	if (__getpublickey_LOCAL != NULL)
169 		return(__getpublickey_LOCAL(netname, publickey));
170 	else
171 		return(__getpublickey_real(netname, publickey));
172 }
173