1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
|
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* or http://www.opensolaris.org/os/licensing.
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright 2009 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
/*
* This contains miscellaneous functions moved from commands to the library.
*/
#include "mt.h"
#include <stdlib.h>
#include <stdio.h>
#include <syslog.h>
#include <string.h>
#include <unistd.h>
#include <gssapi/gssapi.h>
#include <rpc/rpc.h>
#include <rpcsvc/nis.h>
#include <rpcsvc/nis_dhext.h>
#include <rpc/auth.h>
#include <rpc/auth_sys.h>
#include <rpc/auth_des.h>
#include <rpc/key_prot.h>
#include <netdir.h>
#include <netconfig.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <dlfcn.h>
#include <gssapi/gssapi.h>
extern int bin2hex(int len, unsigned char *binnum, char *hexnum);
extern int hex2bin(int len, char *hexnum, char *binnum);
#define MECH_LIB_PREFIX1 "/usr/lib/"
#ifdef _LP64
#define MECH_LIB_PREFIX2 "64/"
#else /* _LP64 */
#define MECH_LIB_PREFIX2 ""
#endif /* _LP64 */
#define MECH_LIB_DIR "gss/"
#define MECH_LIB_PREFIX MECH_LIB_PREFIX1 MECH_LIB_PREFIX2
#define MECHDH MECH_LIB_PREFIX MECH_LIB_DIR "mech_dh.so.1"
#define LIBGSS MECH_LIB_PREFIX "libgss.so.1"
static gss_OID_desc __dh_gss_c_nt_netname = {
9, "\053\006\004\001\052\002\032\001\001"
};
mutex_t gss_load_lock = DEFAULTMUTEX;
static gss_OID GSS_EXPORT_NAME = 0;
static gss_OID DH_NETNAME = &__dh_gss_c_nt_netname;
typedef OM_uint32 (*gss_fptr)();
OM_uint32 (*g_import_name)();
OM_uint32 (*g_display_name)();
OM_uint32 (*g_release_name)();
OM_uint32 (*g_release_buffer)();
OM_uint32 (*g_release_oid)();
/*
* gss_OID_load()
*
* This routine is called by __nis_gssprin2netname to define values for
* the gss-api-export-name OID, the Diffie-Hellman netname OID, and
* the gss support routines that it needs.
* The reason for this support routine is that libnsl cannot have an
* explicit dependency on libgss. Callers of __nisgssprin2netname are
* expected to have loaded libgss through the rpcsec layer. The work around
* is to dlopen the needed shared objects and grab the symbols with dlsym.
* This routine opens libgss RTLD_NOLOAD. If this fails then libgss.so.1
* is not loaded and we return error. Otherwise it uses dlsym to
* defines GSS_EXPORT_NAME to have the value of GSS_C_NT_EXPORT_NAME and
* to assign the above fuction pointers.
* If this succeeds then the routine will attempt to load mech_dh.so.1
* and over ride DH_NETNAME with the value of __DH_GSS_C_NT_NETNAME from
* that shared object. We don't consider it an error if this fails because
* its conceivable that another mechanism backend will support the netname
* name type and mech_dh.so.1 not be available.
*
* Return 0 on failer, 1 on success.
*/
static int
gss_OID_load()
{
void *dh;
gss_OID *OIDptr;
int stat = 0;
(void) mutex_lock(&gss_load_lock);
if (GSS_EXPORT_NAME) {
(void) mutex_unlock(&gss_load_lock);
return (0);
}
/* if LIBGSS is not loaded return an error */
if ((dh = dlopen(LIBGSS, RTLD_NOLOAD)) == NULL) {
(void) mutex_unlock(&gss_load_lock);
return (0);
}
OIDptr = (gss_OID *)dlsym(dh, "GSS_C_NT_EXPORT_NAME");
if (OIDptr)
GSS_EXPORT_NAME = *OIDptr;
else
goto Done;
g_import_name = (gss_fptr)dlsym(dh, "gss_import_name");
if (g_import_name == 0)
goto Done;
g_display_name = (gss_fptr)dlsym(dh, "gss_display_name");
if (g_display_name == 0)
goto Done;
g_release_name = (gss_fptr)dlsym(dh, "gss_release_name");
if (g_release_name == 0)
goto Done;
g_release_buffer = (gss_fptr)dlsym(dh, "gss_release_buffer");
if (g_release_buffer == 0)
goto Done;
g_release_oid = (gss_fptr)dlsym(dh, "gss_release_oid");
if (g_release_oid == 0)
goto Done;
stat = 1;
/*
* Try and get the official netname oid from mech_dh.so.
* If this fails will just keep our default from above.
*/
if ((dh = dlopen(MECHDH, RTLD_LAZY)) != NULL) {
OIDptr = (gss_OID *)dlsym(dh, "__DH_GSS_C_NT_NETNAME");
if (OIDptr)
DH_NETNAME = *OIDptr;
}
Done:
(void) mutex_unlock(&gss_load_lock);
if (stat == 0)
GSS_EXPORT_NAME = 0;
return (stat);
}
/*
* int
* __nis_gssprin2netname(rpc_gss_principal_t prin,
* char netname[MAXNETNAMELEN+1])
*
* This routine attempts to extract the netname from an rpc_gss_principal_t
* which is in { gss-api-exorted-name } format. Return 0 if a netname was
* found, else return -1.
*/
/*
* This routine has a dependency on libgss.so. So we will pragma weak
* the interfaces that we need. When this routine is called libgss
* should have been loaded by the rpcsec layer. We will call gss_OID_load
* to get the value for GSS_EXPORT_NAME. If gss_OID_load failes return -1.
*/
#define OID_IS_EQUAL(o1, o2) ((o1) && (o2) && \
((o1)->length == (o2)->length) && \
(memcmp((o1)->elements, (o2)->elements, (o1)->length) == 0))
int
__nis_gssprin2netname(rpc_gss_principal_t prin, char netname[])
{
gss_buffer_desc display_name;
gss_name_t name;
gss_OID name_type;
gss_buffer_desc expName;
int stat = -1;
OM_uint32 major, minor;
/* See if we already got the OID */
if (GSS_EXPORT_NAME == 0) {
/* Nope. See if GSS is loaded and get the OIDs */
if (!gss_OID_load())
return (-1); /* if libgss.so.1 isn't loaded */
}
expName.length = prin->len;
expName.value = prin->name;
major = (*g_import_name)(&minor, &expName,
(gss_OID) GSS_EXPORT_NAME, &name);
if (major == GSS_S_COMPLETE) {
major = (*g_display_name)(&minor, name,
&display_name, &name_type);
/* We're done with the gss_internal name */
(void) (*g_release_name)(&minor, &name);
if (major == GSS_S_COMPLETE) {
/*
* Check if we've got a netname. If we do we copy it
* and make sure that its null terminated.
*/
if (OID_IS_EQUAL(DH_NETNAME, name_type)) {
(void) strncpy(netname,
(char *)display_name.value,
MAXNETNAMELEN);
netname[MAXNETNAMELEN] = '\0';
stat = 0;
}
/*
* If there are other display formats that can
* be converted to netnames easily, insert here.
*
* else if (OID_IS_EQUAL(OTHER_NT_OID, name_type)) {
* convert2netname(display_name.value, netname);
* } ...
*/
/* Release temporty storage */
(void) (*g_release_buffer)(&minor, &display_name);
(void) (*g_release_oid)(&minor, &name_type);
}
}
if (stat == 0)
return (stat);
return (stat);
}
/*
* Extract a public key given a key length and alg. type from a packed
* netobj containing extended Diffie-Hellman keys.
*/
char *
__nis_dhext_extract_pkey(netobj *no, keylen_t keylen, algtype_t algtype)
{
char *hexkey;
/* LINTED pointer cast */
extdhkey_t *keyent = (extdhkey_t *)no->n_bytes;
/* LINTED pointer cast */
while (keyent < (extdhkey_t *)(no->n_bytes + no->n_len)) {
char *keyoffset;
size_t binlen = (ntohs(keyent->keylen) + 7) / 8;
size_t binpadlen = ((binlen + 3) / 4) * 4;
size_t hexkeylen = binlen * 2 + 1;
if (keylen == ntohs(keyent->keylen) &&
algtype == ntohs(keyent->algtype)) {
if (!(hexkey = malloc(hexkeylen)))
return (NULL);
(void) bin2hex(binlen, keyent->key, hexkey);
return (hexkey);
}
keyoffset = (char *)keyent + (sizeof (ushort_t) * 2) +
binpadlen;
/* LINTED pointer cast */
keyent = (extdhkey_t *)keyoffset;
}
return (NULL);
}
|