]> asedeno.scripts.mit.edu Git - PuTTY.git/blob - unix/uxgss.c
Cleanups of the GSSAPI support. On Windows, standard GSS libraries
[PuTTY.git] / 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 keyval gsslibkeywords[] = {
19     { "libgssapi", 0 },
20     { "libgssapi_krb5", 1 },
21     { "libgss", 2 },
22     { "custom", 3 },
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(const Config *cfg)
57 {
58     void *gsslib;
59     struct ssh_gss_liblist *list = snew(struct ssh_gss_liblist);
60
61     list->libraries = snewn(4, struct ssh_gss_library);
62     list->nlibraries = 0;
63
64     /* Heimdal's GSSAPI Library */
65     if ((gsslib = dlopen("libgssapi.so.2", RTLD_LAZY)) != NULL)
66         gss_init(&list->libraries[list->nlibraries++], gsslib,
67                  0, "Using GSSAPI from libgssapi.so.2");
68
69     /* MIT Kerberos's GSSAPI Library */
70     if ((gsslib = dlopen("libgssapi_krb5.so.2", RTLD_LAZY)) != NULL)
71         gss_init(&list->libraries[list->nlibraries++], gsslib,
72                  1, "Using GSSAPI from libgssapi_krb5.so.2");
73
74     /* Sun's GSSAPI Library */
75     if ((gsslib = dlopen("libgss.so.1", RTLD_LAZY)) != NULL)
76         gss_init(&list->libraries[list->nlibraries++], gsslib,
77                  2, "Using GSSAPI from libgss.so.1");
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
89 void 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);
111 }
112
113 #elif !defined NO_GSSAPI
114
115 const int ngsslibs = 1;
116 const char *const gsslibnames[1] = {
117     "static",
118 };
119 const struct keyval gsslibkeywords[] = {
120     { "static", 0 },
121 };
122
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  */
128
129 #include <gssapi/gssapi.h>
130
131 /* Dynamically load gssapi libs. */
132 struct ssh_gss_liblist *ssh_gss_setup(const Config *cfg)
133 {
134     struct ssh_gss_liblist *list = snew(struct ssh_gss_liblist);
135
136     list->libraries = snew(struct ssh_gss_library);
137     list->nlibraries = 1;
138
139     list->libraries[0].gsslogmsg = "Using statically linked GSSAPI";
140
141 #define BIND_GSS_FN(name) \
142     list->libraries[0].u.gssapi.name = (t_gss_##name) gss_##name
143
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);
152
153 #undef BIND_GSS_FN
154
155     ssh_gssapi_bind_fns(&list->libraries[0]);
156
157     return list;
158 }
159
160 void ssh_gss_cleanup(struct ssh_gss_liblist *list)
161 {
162     sfree(list->libraries);
163     sfree(list);
164 }
165
166 #endif /* NO_LIBDL */
167
168 #endif /* NO_GSSAPI */