xref: /dragonfly/lib/libc/rpc/getpublickey.c (revision c03f08f3)
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.3 2005/11/13 12:27:04 swildner 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(char *netname, 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 	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(char *key, char *ret)
90 {
91 	char buf[1024];	/* big enough */
92 	char *res;
93 	FILE *fd;
94 	char *mkey;
95 	char *mval;
96 
97 	fd = fopen(PKFILE, "r");
98 	if (fd == NULL)
99 		return (0);
100 	for (;;) {
101 		res = fgets(buf, sizeof(buf), fd);
102 		if (res == NULL) {
103 			fclose(fd);
104 			return (0);
105 		}
106 		if (res[0] == '#')
107 			continue;
108 		else if (res[0] == '+') {
109 #ifdef YP
110 			char *PKMAP = "publickey.byname";
111 			char *lookup;
112 			char *domain;
113 			int err;
114 			int len;
115 
116 			err = yp_get_default_domain(&domain);
117 			if (err) {
118 				continue;
119 			}
120 			lookup = NULL;
121 			err = yp_match(domain, PKMAP, key, strlen(key), &lookup, &len);
122 			if (err) {
123 #ifdef DEBUG
124 				fprintf(stderr, "match failed error %d\n", err);
125 #endif
126 				continue;
127 			}
128 			lookup[len] = 0;
129 			strcpy(ret, lookup);
130 			fclose(fd);
131 			free(lookup);
132 			return (2);
133 #else /* YP */
134 #ifdef DEBUG
135 			fprintf(stderr,
136 "Bad record in %s '+' -- NIS not supported in this library copy\n", PKFILE);
137 #endif /* DEBUG */
138 			continue;
139 #endif /* YP */
140 		} else {
141 			mkey = strsep(&res, "\t ");
142 			if (mkey == NULL) {
143 				fprintf(stderr,
144 				"Bad record in %s -- %s", PKFILE, buf);
145 				continue;
146 			}
147 			do {
148 				mval = strsep(&res, " \t#\n");
149 			} while (mval != NULL && !*mval);
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
165 getpublickey(char *netname, char *publickey)
166 {
167 	if (__getpublickey_LOCAL != NULL)
168 		return(__getpublickey_LOCAL(netname, publickey));
169 	else
170 		return(__getpublickey_real(netname, publickey));
171 }
172