Cleanups of the GSSAPI support. On Windows, standard GSS libraries
[u/mdw/putty] / unix / uxgss.c
CommitLineData
d47accad 1#include "putty.h"
bf84a03a 2#ifndef NO_GSSAPI
b3d375b2 3#include "pgssapi.h"
42af6a67 4#include "sshgss.h"
b3d375b2 5#include "sshgssc.h"
6
7/* Unix code to set up the GSSAPI library list. */
8
1e00c92b 9#if !defined NO_LIBDL && !defined NO_GSSAPI
b3d375b2 10
1e00c92b 11const int ngsslibs = 4;
12const char *const gsslibnames[4] = {
b3d375b2 13 "libgssapi (Heimdal)",
14 "libgssapi_krb5 (MIT Kerberos)",
15 "libgss (Sun)",
1e00c92b 16 "User-specified GSSAPI library",
b3d375b2 17};
18const struct keyval gsslibkeywords[] = {
19 { "libgssapi", 0 },
20 { "libgssapi_krb5", 1 },
21 { "libgss", 2 },
1e00c92b 22 { "custom", 3 },
b3d375b2 23};
24
b3d375b2 25/*
26 * Run-time binding against a choice of GSSAPI implementations. We
27 * try loading several libraries, and produce an entry in
28 * ssh_gss_libraries[] for each one.
29 */
30
31static void gss_init(struct ssh_gss_library *lib, void *dlhandle,
32 int id, const char *msg)
42af6a67 33{
b3d375b2 34 lib->id = id;
35 lib->gsslogmsg = msg;
1e00c92b 36 lib->handle = dlhandle;
42af6a67 37
b3d375b2 38#define BIND_GSS_FN(name) \
39 lib->u.gssapi.name = (t_gss_##name) dlsym(dlhandle, "gss_" #name)
42af6a67 40
b3d375b2 41 BIND_GSS_FN(delete_sec_context);
42 BIND_GSS_FN(display_status);
43 BIND_GSS_FN(get_mic);
44 BIND_GSS_FN(import_name);
45 BIND_GSS_FN(init_sec_context);
46 BIND_GSS_FN(release_buffer);
47 BIND_GSS_FN(release_cred);
48 BIND_GSS_FN(release_name);
42af6a67 49
b3d375b2 50#undef BIND_GSS_FN
42af6a67 51
b3d375b2 52 ssh_gssapi_bind_fns(lib);
42af6a67 53}
54
b3d375b2 55/* Dynamically load gssapi libs. */
1e00c92b 56struct ssh_gss_liblist *ssh_gss_setup(const Config *cfg)
42af6a67 57{
b3d375b2 58 void *gsslib;
1e00c92b 59 struct ssh_gss_liblist *list = snew(struct ssh_gss_liblist);
42af6a67 60
1e00c92b 61 list->libraries = snewn(4, struct ssh_gss_library);
62 list->nlibraries = 0;
42af6a67 63
b3d375b2 64 /* Heimdal's GSSAPI Library */
65 if ((gsslib = dlopen("libgssapi.so.2", RTLD_LAZY)) != NULL)
1e00c92b 66 gss_init(&list->libraries[list->nlibraries++], gsslib,
b3d375b2 67 0, "Using GSSAPI from libgssapi.so.2");
42af6a67 68
b3d375b2 69 /* MIT Kerberos's GSSAPI Library */
70 if ((gsslib = dlopen("libgssapi_krb5.so.2", RTLD_LAZY)) != NULL)
1e00c92b 71 gss_init(&list->libraries[list->nlibraries++], gsslib,
b3d375b2 72 1, "Using GSSAPI from libgssapi_krb5.so.2");
42af6a67 73
b3d375b2 74 /* Sun's GSSAPI Library */
75 if ((gsslib = dlopen("libgss.so.1", RTLD_LAZY)) != NULL)
1e00c92b 76 gss_init(&list->libraries[list->nlibraries++], gsslib,
b3d375b2 77 2, "Using GSSAPI from libgss.so.1");
1e00c92b 78
79 /* User-specified GSSAPI library */
80 if (cfg->ssh_gss_custom.path[0] &&
81 (gsslib = dlopen(cfg->ssh_gss_custom.path, RTLD_LAZY)) != NULL)
82 gss_init(&list->libraries[list->nlibraries++], gsslib,
83 3, dupprintf("Using GSSAPI from user-specified"
84 " library '%s'", cfg->ssh_gss_custom.path));
85
86 return list;
87}
88
89void ssh_gss_cleanup(struct ssh_gss_liblist *list)
90{
91 int i;
92
93 /*
94 * dlopen and dlclose are defined to employ reference counting
95 * in the case where the same library is repeatedly dlopened, so
96 * even in a multiple-sessions-per-process context it's safe to
97 * naively dlclose everything here without worrying about
98 * destroying it under the feet of another SSH instance still
99 * using it.
100 */
101 for (i = 0; i < list->nlibraries; i++) {
102 dlclose(list->libraries[i].handle);
103 if (list->libraries[i].id == 3) {
104 /* The 'custom' id involves a dynamically allocated message.
105 * Note that we must cast away the 'const' to free it. */
106 sfree((char *)list->libraries[i].gsslogmsg);
107 }
108 }
109 sfree(list->libraries);
110 sfree(list);
42af6a67 111}
112
1e00c92b 113#elif !defined NO_GSSAPI
114
115const int ngsslibs = 1;
116const char *const gsslibnames[1] = {
117 "static",
118};
119const struct keyval gsslibkeywords[] = {
120 { "static", 0 },
121};
42af6a67 122
b3d375b2 123/*
124 * Link-time binding against GSSAPI. Here we just construct a single
125 * library structure containing pointers to the functions we linked
126 * against.
127 */
42af6a67 128
b3d375b2 129#include <gssapi/gssapi.h>
42af6a67 130
b3d375b2 131/* Dynamically load gssapi libs. */
1e00c92b 132struct ssh_gss_liblist *ssh_gss_setup(const Config *cfg)
42af6a67 133{
1e00c92b 134 struct ssh_gss_liblist *list = snew(struct ssh_gss_liblist);
42af6a67 135
1e00c92b 136 list->libraries = snew(struct ssh_gss_library);
137 list->nlibraries = 1;
138
139 list->libraries[0].gsslogmsg = "Using statically linked GSSAPI";
42af6a67 140
b3d375b2 141#define BIND_GSS_FN(name) \
1e00c92b 142 list->libraries[0].u.gssapi.name = (t_gss_##name) gss_##name
42af6a67 143
b3d375b2 144 BIND_GSS_FN(delete_sec_context);
145 BIND_GSS_FN(display_status);
146 BIND_GSS_FN(get_mic);
147 BIND_GSS_FN(import_name);
148 BIND_GSS_FN(init_sec_context);
149 BIND_GSS_FN(release_buffer);
150 BIND_GSS_FN(release_cred);
151 BIND_GSS_FN(release_name);
42af6a67 152
b3d375b2 153#undef BIND_GSS_FN
42af6a67 154
1e00c92b 155 ssh_gssapi_bind_fns(&list->libraries[0]);
156
157 return list;
158}
159
160void ssh_gss_cleanup(struct ssh_gss_liblist *list)
161{
162 sfree(list->libraries);
163 sfree(list);
42af6a67 164}
165
b3d375b2 166#endif /* NO_LIBDL */
167
168#endif /* NO_GSSAPI */