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 |
11 | const int ngsslibs = 4; |
12 | const char *const gsslibnames[4] = { |
b3d375b2 |
13 | "libgssapi (Heimdal)", |
14 | "libgssapi_krb5 (MIT Kerberos)", |
15 | "libgss (Sun)", |
1e00c92b |
16 | "User-specified GSSAPI library", |
b3d375b2 |
17 | }; |
4ceef224 |
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 }, |
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 | |
31 | static 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. */ |
4a693cfc |
56 | struct ssh_gss_liblist *ssh_gss_setup(Conf *conf) |
42af6a67 |
57 | { |
b3d375b2 |
58 | void *gsslib; |
4a693cfc |
59 | char *gsspath; |
1e00c92b |
60 | struct ssh_gss_liblist *list = snew(struct ssh_gss_liblist); |
42af6a67 |
61 | |
1e00c92b |
62 | list->libraries = snewn(4, struct ssh_gss_library); |
63 | list->nlibraries = 0; |
42af6a67 |
64 | |
b3d375b2 |
65 | /* Heimdal's GSSAPI Library */ |
66 | if ((gsslib = dlopen("libgssapi.so.2", RTLD_LAZY)) != NULL) |
1e00c92b |
67 | gss_init(&list->libraries[list->nlibraries++], gsslib, |
b3d375b2 |
68 | 0, "Using GSSAPI from libgssapi.so.2"); |
42af6a67 |
69 | |
b3d375b2 |
70 | /* MIT Kerberos's GSSAPI Library */ |
71 | if ((gsslib = dlopen("libgssapi_krb5.so.2", RTLD_LAZY)) != NULL) |
1e00c92b |
72 | gss_init(&list->libraries[list->nlibraries++], gsslib, |
b3d375b2 |
73 | 1, "Using GSSAPI from libgssapi_krb5.so.2"); |
42af6a67 |
74 | |
b3d375b2 |
75 | /* Sun's GSSAPI Library */ |
76 | if ((gsslib = dlopen("libgss.so.1", RTLD_LAZY)) != NULL) |
1e00c92b |
77 | gss_init(&list->libraries[list->nlibraries++], gsslib, |
b3d375b2 |
78 | 2, "Using GSSAPI from libgss.so.1"); |
1e00c92b |
79 | |
80 | /* User-specified GSSAPI library */ |
4a693cfc |
81 | gsspath = conf_get_filename(conf, CONF_ssh_gss_custom)->path; |
82 | if (*gsspath && (gsslib = dlopen(gsspath, RTLD_LAZY)) != NULL) |
1e00c92b |
83 | gss_init(&list->libraries[list->nlibraries++], gsslib, |
84 | 3, dupprintf("Using GSSAPI from user-specified" |
4a693cfc |
85 | " library '%s'", gsspath)); |
1e00c92b |
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); |
42af6a67 |
112 | } |
113 | |
1e00c92b |
114 | #elif !defined NO_GSSAPI |
115 | |
116 | const int ngsslibs = 1; |
117 | const char *const gsslibnames[1] = { |
118 | "static", |
119 | }; |
4ceef224 |
120 | const struct keyvalwhere gsslibkeywords[] = { |
121 | { "static", 0, -1, -1 }, |
1e00c92b |
122 | }; |
42af6a67 |
123 | |
b3d375b2 |
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 | */ |
42af6a67 |
129 | |
b3d375b2 |
130 | #include <gssapi/gssapi.h> |
42af6a67 |
131 | |
b3d375b2 |
132 | /* Dynamically load gssapi libs. */ |
4a693cfc |
133 | struct ssh_gss_liblist *ssh_gss_setup(Conf *conf) |
42af6a67 |
134 | { |
1e00c92b |
135 | struct ssh_gss_liblist *list = snew(struct ssh_gss_liblist); |
42af6a67 |
136 | |
1e00c92b |
137 | list->libraries = snew(struct ssh_gss_library); |
138 | list->nlibraries = 1; |
139 | |
140 | list->libraries[0].gsslogmsg = "Using statically linked GSSAPI"; |
42af6a67 |
141 | |
b3d375b2 |
142 | #define BIND_GSS_FN(name) \ |
1e00c92b |
143 | list->libraries[0].u.gssapi.name = (t_gss_##name) gss_##name |
42af6a67 |
144 | |
b3d375b2 |
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); |
42af6a67 |
153 | |
b3d375b2 |
154 | #undef BIND_GSS_FN |
42af6a67 |
155 | |
1e00c92b |
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); |
42af6a67 |
165 | } |
166 | |
b3d375b2 |
167 | #endif /* NO_LIBDL */ |
168 | |
169 | #endif /* NO_GSSAPI */ |