Debian Codegraph @

extra/tirpc/libtirpc-1.3.5/src/getpublickey.c

raw symbols SCIP
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 /*
30 * publickey.c
31 * Copyright (C) 1986, Sun Microsystems, Inc.
32 */
33
34 /*
35 * Public key lookup routines
36 */
37 #include <stdio.h>
38 #include <pwd.h>
39 #include <rpc/rpc.h>
40 #include <rpc/key_prot.h>
41 #ifdef YP
42 #include <rpcsvc/yp_prot.h>
43 #include <rpcsvc/ypclnt.h>
44 #endif
45 #include <string.h>
46 #include <stdlib.h>
47
48 #include "debug.h"
49
50 #define PKFILE "/etc/publickey"
51
52 /*
53 * Hack to let ypserv/rpc.nisd use AUTH_DES.
54 */
55 int (*__getpublickey_LOCAL)() = 0;
56
57 /*
58 * Get somebody's public key
59 */
60 int
61 __getpublickey_real(netname, publickey)
62 char *netname;
63 char *publickey;
64 {
65 char lookup[3 * HEXKEYBYTES];
66 char *p;
67
68 if (publickey == NULL)
69 return (0);
70 if (!getpublicandprivatekey(netname, lookup))
71 return (0);
72 p = strchr(lookup, ':');
73 if (p == NULL) {
74 return (0);
75 }
76 *p = '\0';
77 memcpy(publickey, lookup, HEXKEYBYTES);
78 publickey[HEXKEYBYTES] = '\0';
79 return (1);
80 }
81
82 /*
83 * reads the file /etc/publickey looking for a + to optionally go to the
84 * yellow pages
85 */
86
87 int
88 getpublicandprivatekey(key, ret)
89 char *key;
90 char *ret;
91 {
92 char buf[1024]; /* big enough */
93 char *res;
94 FILE *fd;
95 char *mkey;
96 char *mval;
97
98 fd = fopen(PKFILE, "r");
99 if (fd == NULL)
100 return (0);
101 for (;;) {
102 res = fgets(buf, sizeof(buf), fd);
103 if (res == NULL) {
104 fclose(fd);
105 return (0);
106 }
107 if (res[0] == '#')
108 continue;
109 else if (res[0] == '+') {
110 #ifdef YP
111 char *PKMAP = "publickey.byname";
112 char *lookup;
113 char *domain;
114 int err;
115 int len;
116
117 err = yp_get_default_domain(&domain);
118 if (err) {
119 continue;
120 }
121 lookup = NULL;
122 err = yp_match(domain, PKMAP, key, strlen(key), &lookup, &len);
123 if (err) {
124 LIBTIRPC_DEBUG(1,
125 ("getpublicandprivatekey: match failed error %d\n", err));
126 continue;
127 }
128 lookup[len] = 0;
129 strcpy(ret, lookup);
130 fclose(fd);
131 free(lookup);
132 return (2);
133 #else /* YP */
134 LIBTIRPC_DEBUG(1,
135 ("Bad record in %s '+' -- NIS not supported in this library copy\n", PKFILE));
136 continue;
137 #endif /* YP */
138 } else {
139 mkey = strsep(&res, "\t ");
140 if (mkey == NULL) {
141 fprintf(stderr,
142 "Bad record in %s -- %s", PKFILE, buf);
143 continue;
144 }
145 do {
146 mval = strsep(&res, " \t#\n");
147 } while (mval != NULL && !*mval);
148 if (mval == NULL) {
149 fprintf(stderr,
150 "Bad record in %s val problem - %s", PKFILE, buf);
151 continue;
152 }
153 if (strcmp(mkey, key) == 0) {
154 strcpy(ret, mval);
155 fclose(fd);
156 return (1);
157 }
158 }
159 }
160 }
161
162 int getpublickey(netname, publickey)
163 const char *netname;
164 char *publickey;
165 {
166 if (__getpublickey_LOCAL != NULL)
167 return(__getpublickey_LOCAL(netname, publickey));
168 else
169 return(__getpublickey_real(netname, publickey));
170 }