1/*
2 * Copyright (c) 2005, 2012, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.  Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26#include <stdio.h>
27#include <stdlib.h>
28#include <dlfcn.h>
29#include "NativeFunc.h"
30
31/* standard GSS method names (ordering is from mapfile) */
32static const char RELEASE_NAME[]                = "gss_release_name";
33static const char IMPORT_NAME[]                 = "gss_import_name";
34static const char COMPARE_NAME[]                = "gss_compare_name";
35static const char CANONICALIZE_NAME[]           = "gss_canonicalize_name";
36static const char EXPORT_NAME[]                 = "gss_export_name";
37static const char DISPLAY_NAME[]                = "gss_display_name";
38static const char ACQUIRE_CRED[]                = "gss_acquire_cred";
39static const char RELEASE_CRED[]                = "gss_release_cred";
40static const char INQUIRE_CRED[]                = "gss_inquire_cred";
41static const char IMPORT_SEC_CONTEXT[]          = "gss_import_sec_context";
42static const char INIT_SEC_CONTEXT[]            = "gss_init_sec_context";
43static const char ACCEPT_SEC_CONTEXT[]          = "gss_accept_sec_context";
44static const char INQUIRE_CONTEXT[]             = "gss_inquire_context";
45static const char DELETE_SEC_CONTEXT[]          = "gss_delete_sec_context";
46static const char CONTEXT_TIME[]                = "gss_context_time";
47static const char WRAP_SIZE_LIMIT[]             = "gss_wrap_size_limit";
48static const char EXPORT_SEC_CONTEXT[]          = "gss_export_sec_context";
49static const char GET_MIC[]                     = "gss_get_mic";
50static const char VERIFY_MIC[]                  = "gss_verify_mic";
51static const char WRAP[]                        = "gss_wrap";
52static const char UNWRAP[]                      = "gss_unwrap";
53static const char INDICATE_MECHS[]              = "gss_indicate_mechs";
54static const char INQUIRE_NAMES_FOR_MECH[]      = "gss_inquire_names_for_mech";
55
56/* additional GSS methods not public thru mapfile */
57
58static const char ADD_OID_SET_MEMBER[]          = "gss_add_oid_set_member";
59static const char DISPLAY_STATUS[]              = "gss_display_status";
60static const char CREATE_EMPTY_OID_SET[]        = "gss_create_empty_oid_set";
61static const char RELEASE_OID_SET[]             = "gss_release_oid_set";
62static const char RELEASE_BUFFER[]              = "gss_release_buffer";
63
64/**
65 * Initialize native GSS function pointers
66 */
67char* loadNative(const char *libName) {
68
69    char *error;
70    void *gssLib;
71    int failed;
72    OM_uint32 minor, major;
73
74    ftab = NULL;
75    failed = FALSE;
76    error = NULL;
77
78    gssLib = dlopen(libName, RTLD_NOW);
79    if (gssLib == NULL) {
80        failed = TRUE;
81        goto out;
82    }
83
84    /* global function table instance */
85    ftab = (GSS_FUNCTION_TABLE_PTR)malloc(sizeof(GSS_FUNCTION_TABLE));
86    if (ftab == NULL) {
87        failed = TRUE;
88        goto out;
89    }
90
91    ftab->releaseName = (RELEASE_NAME_FN_PTR)dlsym(gssLib, RELEASE_NAME);
92    if (ftab->releaseName == NULL) {
93        failed = TRUE;
94        goto out;
95    }
96
97    ftab->importName = (IMPORT_NAME_FN_PTR)dlsym(gssLib, IMPORT_NAME);
98    if (ftab->importName == NULL) {
99        failed = TRUE;
100        goto out;
101    }
102
103    ftab->compareName = (COMPARE_NAME_FN_PTR)dlsym(gssLib, COMPARE_NAME);
104    if (ftab->compareName == NULL) {
105        failed = TRUE;
106        goto out;
107    }
108
109    ftab->canonicalizeName = (CANONICALIZE_NAME_FN_PTR)
110                                dlsym(gssLib, CANONICALIZE_NAME);
111    if (ftab->canonicalizeName == NULL) {
112        failed = TRUE;
113        goto out;
114    }
115
116    ftab->exportName = (EXPORT_NAME_FN_PTR)dlsym(gssLib, EXPORT_NAME);
117    if (ftab->exportName == NULL) {
118        failed = TRUE;
119        goto out;
120    }
121
122    ftab->displayName = (DISPLAY_NAME_FN_PTR)dlsym(gssLib, DISPLAY_NAME);
123    if (ftab->displayName == NULL) {
124        failed = TRUE;
125        goto out;
126    }
127
128    ftab->acquireCred = (ACQUIRE_CRED_FN_PTR)dlsym(gssLib, ACQUIRE_CRED);
129    if (ftab->acquireCred == NULL) {
130        failed = TRUE;
131        goto out;
132    }
133
134    ftab->releaseCred = (RELEASE_CRED_FN_PTR)dlsym(gssLib, RELEASE_CRED);
135    if (ftab->releaseCred == NULL) {
136        failed = TRUE;
137        goto out;
138    }
139
140    ftab->inquireCred = (INQUIRE_CRED_FN_PTR)dlsym(gssLib, INQUIRE_CRED);
141    if (ftab->inquireCred == NULL) {
142        failed = TRUE;
143        goto out;
144    }
145
146    ftab->importSecContext = (IMPORT_SEC_CONTEXT_FN_PTR)
147                        dlsym(gssLib, IMPORT_SEC_CONTEXT);
148    if (ftab->importSecContext == NULL) {
149        failed = TRUE;
150        goto out;
151    }
152
153    ftab->initSecContext = (INIT_SEC_CONTEXT_FN_PTR)
154                        dlsym(gssLib, INIT_SEC_CONTEXT);
155    if (ftab->initSecContext == NULL) {
156        failed = TRUE;
157        goto out;
158    }
159
160    ftab->acceptSecContext = (ACCEPT_SEC_CONTEXT_FN_PTR)
161                        dlsym(gssLib, ACCEPT_SEC_CONTEXT);
162    if (ftab->acceptSecContext == NULL) {
163        failed = TRUE;
164        goto out;
165    }
166
167    ftab->inquireContext = (INQUIRE_CONTEXT_FN_PTR)
168                        dlsym(gssLib, INQUIRE_CONTEXT);
169    if (ftab->inquireContext == NULL) {
170        failed = TRUE;
171        goto out;
172    }
173
174    ftab->deleteSecContext = (DELETE_SEC_CONTEXT_FN_PTR)
175                        dlsym(gssLib, DELETE_SEC_CONTEXT);
176    if (ftab->deleteSecContext == NULL) {
177        failed = TRUE;
178        goto out;
179    }
180
181    ftab->contextTime = (CONTEXT_TIME_FN_PTR)dlsym(gssLib, CONTEXT_TIME);
182    if (ftab->contextTime == NULL) {
183        failed = TRUE;
184        goto out;
185    }
186
187    ftab->wrapSizeLimit = (WRAP_SIZE_LIMIT_FN_PTR)
188                        dlsym(gssLib, WRAP_SIZE_LIMIT);
189    if (ftab->wrapSizeLimit == NULL) {
190        failed = TRUE;
191        goto out;
192    }
193
194    ftab->exportSecContext = (EXPORT_SEC_CONTEXT_FN_PTR)
195                        dlsym(gssLib, EXPORT_SEC_CONTEXT);
196    if (ftab->exportSecContext == NULL) {
197        failed = TRUE;
198        goto out;
199    }
200
201    ftab->getMic = (GET_MIC_FN_PTR)dlsym(gssLib, GET_MIC);
202    if (ftab->getMic == NULL) {
203        failed = TRUE;
204        goto out;
205    }
206
207    ftab->verifyMic = (VERIFY_MIC_FN_PTR)dlsym(gssLib, VERIFY_MIC);
208    if (ftab->verifyMic == NULL) {
209        failed = TRUE;
210        goto out;
211    }
212
213    ftab->wrap = (WRAP_FN_PTR)dlsym(gssLib, WRAP);
214    if (ftab->wrap == NULL) {
215        failed = TRUE;
216        goto out;
217    }
218
219    ftab->unwrap = (UNWRAP_FN_PTR)dlsym(gssLib, UNWRAP);
220    if (ftab->unwrap == NULL) {
221        failed = TRUE;
222        goto out;
223    }
224
225    ftab->indicateMechs = (INDICATE_MECHS_FN_PTR)dlsym(gssLib, INDICATE_MECHS);
226    if (ftab->indicateMechs == NULL) {
227        failed = TRUE;
228        goto out;
229    }
230
231    ftab->inquireNamesForMech = (INQUIRE_NAMES_FOR_MECH_FN_PTR)
232                        dlsym(gssLib, INQUIRE_NAMES_FOR_MECH);
233    if (ftab->inquireNamesForMech == NULL) {
234        failed = TRUE;
235        goto out;
236    }
237
238    ftab->addOidSetMember = (ADD_OID_SET_MEMBER_FN_PTR)
239                        dlsym(gssLib, ADD_OID_SET_MEMBER);
240    if (ftab->addOidSetMember == NULL) {
241        failed = TRUE;
242        goto out;
243    }
244
245    ftab->displayStatus = (DISPLAY_STATUS_FN_PTR)
246                        dlsym(gssLib, DISPLAY_STATUS);
247    if (ftab->displayStatus == NULL) {
248        failed = TRUE;
249        goto out;
250    }
251
252    ftab->createEmptyOidSet = (CREATE_EMPTY_OID_SET_FN_PTR)
253                        dlsym(gssLib, CREATE_EMPTY_OID_SET);
254    if (ftab->createEmptyOidSet == NULL) {
255        failed = TRUE;
256        goto out;
257    }
258
259    ftab->releaseOidSet = (RELEASE_OID_SET_FN_PTR)
260                        dlsym(gssLib, RELEASE_OID_SET);
261    if (ftab->releaseOidSet == NULL) {
262        failed = TRUE;
263        goto out;
264    }
265
266    ftab->releaseBuffer = (RELEASE_BUFFER_FN_PTR)
267                        dlsym(gssLib, RELEASE_BUFFER);
268    if (ftab->releaseBuffer == NULL) {
269        failed = TRUE;
270        goto out;
271    }
272
273    ftab->mechs = GSS_C_NO_OID_SET;
274    major = (*ftab->indicateMechs)(&minor, &(ftab->mechs));
275    if (ftab->mechs == NULL || ftab->mechs == GSS_C_NO_OID_SET) {
276        failed = TRUE;
277        goto out;
278    }
279
280
281out:
282    if (failed == TRUE) {
283        error = dlerror();
284        if (gssLib != NULL) dlclose(gssLib);
285        if (ftab != NULL) free(ftab);
286    }
287    return error;
288}
289