Bring in a transport-independent RPC (TI-RPC).
[dragonfly.git] / lib / libc / rpc / getpublickey.c
blob81eedf8a4474e35a155d7ef2fa7db960c4daf97e
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.
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.
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.
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.
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.
26 * Sun Microsystems, Inc.
27 * 2550 Garcia Avenue
28 * Mountain View, California 94043
30 * $FreeBSD: src/lib/libc/rpc/getpublickey.c,v 1.9 2006/02/28 16:02:26 deischen Exp $
31 * $DragonFly: src/lib/libc/rpc/getpublickey.c,v 1.3 2005/11/13 12:27:04 swildner Exp $
33 * @(#)publickey.c 1.10 91/03/11 Copyr 1986 Sun Micro
37 * publickey.c
38 * Copyright (C) 1986, Sun Microsystems, Inc.
42 * Public key lookup routines
44 #include "namespace.h"
45 #include <stdio.h>
46 #include <pwd.h>
47 #include <rpc/rpc.h>
48 #include <rpc/key_prot.h>
49 #include <rpcsvc/yp_prot.h>
50 #include <rpcsvc/ypclnt.h>
51 #include <string.h>
52 #include <stdlib.h>
53 #include "un-namespace.h"
55 #define PKFILE "/etc/publickey"
58 * Hack to let ypserv/rpc.nisd use AUTH_DES.
60 int (*__getpublickey_LOCAL)() = 0;
63 * Get somebody's public key
65 static int
66 __getpublickey_real(const char *netname, char *publickey)
68 char lookup[3 * HEXKEYBYTES];
69 char *p;
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);
79 *p = '\0';
80 strncpy(publickey, lookup, HEXKEYBYTES);
81 publickey[HEXKEYBYTES] = '\0';
82 return (1);
86 * reads the file /etc/publickey looking for a + to optionally go to the
87 * yellow pages
90 int
91 getpublicandprivatekey(const char *key, char *ret)
93 char buf[1024]; /* big enough */
94 char *res;
95 FILE *fd;
96 char *mkey;
97 char *mval;
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);
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;
118 err = yp_get_default_domain(&domain);
119 if (err) {
120 continue;
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;
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;
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;
157 if (strcmp(mkey, key) == 0) {
158 strcpy(ret, mval);
159 fclose(fd);
160 return (1);
167 getpublickey(const char *netname, char *publickey)
169 if (__getpublickey_LOCAL != NULL)
170 return(__getpublickey_LOCAL(netname, publickey));
171 else
172 return(__getpublickey_real(netname, publickey));