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