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