]> CyberLeo.Net >> Repos - FreeBSD/releng/10.2.git/blob - lib/libc/rpc/getpublickey.c
- Copy stable/10@285827 to releng/10.2 in preparation for 10.2-RC1
[FreeBSD/releng/10.2.git] / lib / libc / rpc / getpublickey.c
1 /*-
2  * Copyright (c) 2009, Sun Microsystems, Inc.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without 
6  * modification, are permitted provided that the following conditions are met:
7  * - Redistributions of source code must retain the above copyright notice, 
8  *   this list of conditions and the following disclaimer.
9  * - Redistributions in binary form must reproduce the above copyright notice, 
10  *   this list of conditions and the following disclaimer in the documentation 
11  *   and/or other materials provided with the distribution.
12  * - Neither the name of Sun Microsystems, Inc. nor the names of its 
13  *   contributors may be used to endorse or promote products derived 
14  *   from this software without specific prior written permission.
15  * 
16  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 
17  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 
19  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 
20  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 
21  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 
22  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 
23  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
24  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
25  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
26  * POSSIBILITY OF SUCH DAMAGE.
27  */
28
29 #if defined(LIBC_SCCS) && !defined(lint)
30 static char sccsid[] = "@(#)publickey.c 1.10 91/03/11 Copyr 1986 Sun Micro";
31 #endif
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
34
35 /*
36  * publickey.c
37  * Copyright (C) 1986, Sun Microsystems, Inc.
38  */
39
40 /*
41  * Public key lookup routines
42  */
43 #include "namespace.h"
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 #include "un-namespace.h"
53
54 #define PKFILE "/etc/publickey"
55
56 /*
57  * Hack to let ypserv/rpc.nisd use AUTH_DES.
58  */
59 int (*__getpublickey_LOCAL)() = 0;
60
61 /*
62  * Get somebody's public key
63  */
64 static int
65 __getpublickey_real(netname, publickey)
66         const char *netname;
67         char *publickey;
68 {
69         char lookup[3 * HEXKEYBYTES];
70         char *p;
71
72         if (publickey == NULL)
73                 return (0);
74         if (!getpublicandprivatekey(netname, lookup))
75                 return (0);
76         p = strchr(lookup, ':');
77         if (p == NULL) {
78                 return (0);
79         }
80         *p = '\0';
81         (void) strncpy(publickey, lookup, HEXKEYBYTES);
82         publickey[HEXKEYBYTES] = '\0';
83         return (1);
84 }
85
86 /*
87  * reads the file /etc/publickey looking for a + to optionally go to the
88  * yellow pages
89  */
90
91 int
92 getpublicandprivatekey(key, ret)
93         const char *key;
94         char *ret;
95 {
96         char buf[1024]; /* big enough */
97         char *res;
98         FILE *fd;
99         char *mkey;
100         char *mval;
101
102         fd = fopen(PKFILE, "r");
103         if (fd == NULL)
104                 return (0);
105         for (;;) {
106                 res = fgets(buf, sizeof(buf), fd);
107                 if (res == NULL) {
108                         fclose(fd);
109                         return (0);
110                 }
111                 if (res[0] == '#')
112                         continue;
113                 else if (res[0] == '+') {
114 #ifdef YP
115                         char *PKMAP = "publickey.byname";
116                         char *lookup;
117                         char *domain;
118                         int err;
119                         int len;
120
121                         err = yp_get_default_domain(&domain);
122                         if (err) {
123                                 continue;
124                         }
125                         lookup = NULL;
126                         err = yp_match(domain, PKMAP, key, strlen(key), &lookup, &len);
127                         if (err) {
128 #ifdef DEBUG
129                                 fprintf(stderr, "match failed error %d\n", err);
130 #endif
131                                 continue;
132                         }
133                         lookup[len] = 0;
134                         strcpy(ret, lookup);
135                         fclose(fd);
136                         free(lookup);
137                         return (2);
138 #else /* YP */
139 #ifdef DEBUG
140                         fprintf(stderr,
141 "Bad record in %s '+' -- NIS not supported in this library copy\n", PKFILE);
142 #endif /* DEBUG */
143                         continue;
144 #endif /* YP */
145                 } else {
146                         mkey = strsep(&res, "\t ");
147                         if (mkey == NULL) {
148                                 fprintf(stderr,
149                                 "Bad record in %s -- %s", PKFILE, buf);
150                                 continue;
151                         }
152                         do {
153                                 mval = strsep(&res, " \t#\n");
154                         } while (mval != NULL && !*mval);
155                         if (mval == NULL) {
156                                 fprintf(stderr,
157                         "Bad record in %s val problem - %s", PKFILE, buf);
158                                 continue;
159                         }
160                         if (strcmp(mkey, key) == 0) {
161                                 strcpy(ret, mval);
162                                 fclose(fd);
163                                 return (1);
164                         }
165                 }
166         }
167 }
168
169 int getpublickey(netname, publickey)
170         const char *netname;
171         char *publickey;
172 {
173         if (__getpublickey_LOCAL != NULL)
174                 return(__getpublickey_LOCAL(netname, publickey));
175         else
176                 return(__getpublickey_real(netname, publickey));
177 }