Merge branch 'vendor/FILE'
[dragonfly.git] / lib / libc / rpc / getpublickey.c
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.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 $
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 "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"
54
55 #define PKFILE "/etc/publickey"
56
57 /*
58  * Hack to let ypserv/rpc.nisd use AUTH_DES.
59  */
60 int (*__getpublickey_LOCAL)() = 0;
61
62 /*
63  * Get somebody's public key
64  */
65 static int
66 __getpublickey_real(const char *netname, 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         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(const char *key, 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
166 int
167 getpublickey(const char *netname, char *publickey)
168 {
169         if (__getpublickey_LOCAL != NULL)
170                 return(__getpublickey_LOCAL(netname, publickey));
171         else
172                 return(__getpublickey_real(netname, publickey));
173 }