Imported Upstream version 0.2.5
[platform/upstream/libtirpc.git] / src / 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 #include <sys/cdefs.h>
30 */
31
32 /*
33  * publickey.c
34  * Copyright (C) 1986, Sun Microsystems, Inc.
35  */
36
37 /*
38  * Public key lookup routines
39  */
40 #include <stdio.h>
41 #include <pwd.h>
42 #include <rpc/rpc.h>
43 #include <rpc/key_prot.h>
44 #include <rpcsvc/yp_prot.h>
45 #include <rpcsvc/ypclnt.h>
46 #include <string.h>
47 #include <stdlib.h>
48
49 #include "debug.h"
50
51 #define PKFILE "/etc/publickey"
52
53 /*
54  * Hack to let ypserv/rpc.nisd use AUTH_DES.
55  */
56 int (*__getpublickey_LOCAL)() = 0;
57
58 /*
59  * Get somebody's public key
60  */
61 int
62 __getpublickey_real(netname, publickey)
63         char *netname;
64         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         (void) 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(key, ret)
90         char *key;
91         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                                 LIBTIRPC_DEBUG(1, 
126                                         ("getpublicandprivatekey: match failed error %d\n", err));
127                                 continue;
128                         }
129                         lookup[len] = 0;
130                         strcpy(ret, lookup);
131                         fclose(fd);
132                         free(lookup);
133                         return (2);
134 #else /* YP */
135                         LIBTIRPC_DEBUG(1, 
136 ("Bad record in %s '+' -- NIS not supported in this library copy\n", PKFILE));
137                         continue;
138 #endif /* YP */
139                 } else {
140                         mkey = strsep(&res, "\t ");
141                         if (mkey == NULL) {
142                                 fprintf(stderr,
143                                 "Bad record in %s -- %s", PKFILE, buf);
144                                 continue;
145                         }
146                         do {
147                                 mval = strsep(&res, " \t#\n");
148                         } while (mval != NULL && !*mval);
149                         if (mval == NULL) {
150                                 fprintf(stderr,
151                         "Bad record in %s val problem - %s", PKFILE, buf);
152                                 continue;
153                         }
154                         if (strcmp(mkey, key) == 0) {
155                                 strcpy(ret, mval);
156                                 fclose(fd);
157                                 return (1);
158                         }
159                 }
160         }
161 }
162
163 int getpublickey(netname, publickey)
164         const char *netname;
165         char *publickey;
166 {
167         if (__getpublickey_LOCAL != NULL)
168                 return(__getpublickey_LOCAL(netname, publickey));
169         else
170                 return(__getpublickey_real(netname, publickey));
171 }