2 * gssapi.c -- GSSAPI authentication (see RFC 1508)
4 * For license terms, see the file COPYING in this directory.
11 #if defined(STDC_HEADERS)
14 #include "fetchmail.h"
20 #include <sys/types.h>
21 #include <netinet/in.h> /* for htonl/ntohl */
27 # if defined(HAVE_GSSAPI_H) && !defined(HAVE_GSSAPI_GSSAPI_H)
30 # ifdef HAVE_GSSAPI_GSSAPI_H
31 # include <gssapi/gssapi.h>
33 # ifdef HAVE_GSSAPI_GSSAPI_GENERIC_H
34 # include <gssapi/gssapi_generic.h>
36 # ifndef HAVE_GSS_C_NT_HOSTBASED_SERVICE
37 # define GSS_C_NT_HOSTBASED_SERVICE gss_nt_service_name
41 static void decode_subr(const char *m, uint32_t code, int type)
43 uint32_t maj, min, context;
44 gss_buffer_desc msg = GSS_C_EMPTY_BUFFER;
48 maj = gss_display_status(&min, code, type, GSS_C_NO_OID,
51 if (maj != GSS_S_COMPLETE) {
52 report(stderr, GT_("GSSAPI error in gss_display_status called from <%s>\n"), m);
55 report(stderr, GT_("GSSAPI error %s: %.*s\n"), m,
56 (int)msg.length, (char *)msg.value);
57 (void)gss_release_buffer(&min, &msg);
61 static void decode_status(const char *m, uint32_t major, uint32_t minor)
63 decode_subr(m, major, GSS_C_GSS_CODE);
64 decode_subr(m, minor, GSS_C_MECH_CODE);
67 #define GSSAUTH_P_NONE 1
68 #define GSSAUTH_P_INTEGRITY 2
69 #define GSSAUTH_P_PRIVACY 4
71 static int import_name(const char *service, const char *hostname,
72 gss_name_t *target_name, flag verbose)
76 OM_uint32 maj_stat, min_stat;
77 gss_buffer_desc request_buf;
79 /* first things first: get an imap ticket for host */
80 buf1siz = strlen(service) + 1 + strlen(hostname) + 1;
81 buf1 = (char *)xmalloc(buf1siz);
82 snprintf(buf1, buf1siz, "%s@%s", service, hostname);
83 request_buf.value = buf1;
84 request_buf.length = strlen(buf1) + 1;
85 maj_stat = gss_import_name(&min_stat, &request_buf,
86 GSS_C_NT_HOSTBASED_SERVICE, target_name);
87 if (maj_stat != GSS_S_COMPLETE) {
88 decode_status("gss_import_name", maj_stat, min_stat);
89 report(stderr, GT_("Couldn't get service name for [%s]\n"), buf1);
92 else if (outlevel >= O_DEBUG && verbose) {
93 (void)gss_display_name(&min_stat, *target_name, &request_buf, NULL);
94 report(stderr, GT_("Using service name [%s]\n"),
95 (char *)request_buf.value);
97 (void)gss_release_buffer(&min_stat, &request_buf);
102 /* If we don't have suitable credentials, don't bother trying GSSAPI, but
103 * fail right away. This is to avoid that a server - such as Microsoft
104 * Exchange 2007 - gets wedged and refuses different authentication
105 * mechanisms afterwards. */
106 int check_gss_creds(const char *service, const char *hostname)
108 OM_uint32 maj_stat, min_stat;
110 gss_name_t target_name;
112 (void)import_name(service, hostname, &target_name, FALSE);
113 (void)gss_release_name(&min_stat, &target_name);
115 maj_stat = gss_inquire_cred(&min_stat, GSS_C_NO_CREDENTIAL,
116 NULL, NULL, &cu, NULL);
117 if (maj_stat != GSS_S_COMPLETE
118 || (cu != GSS_C_INITIATE && cu != GSS_C_BOTH)) {
119 if (outlevel >= O_DEBUG) {
120 decode_status("gss_inquire_cred", maj_stat, min_stat);
121 report(stderr, GT_("No suitable GSSAPI credentials found. Skipping GSSAPI authentication.\n"));
122 report(stderr, GT_("If you want to use GSSAPI, you need credentials first, possibly from kinit.\n"));
130 int do_gssauth(int sock, const char *command, const char *service,
131 const char *hostname, const char *username)
133 gss_buffer_desc request_buf, send_token;
134 gss_buffer_t sec_token;
135 gss_name_t target_name;
136 gss_ctx_id_t context;
139 OM_uint32 maj_stat, min_stat;
140 char buf1[8192], buf2[8192], server_conf_flags;
141 unsigned long buf_size;
144 result = import_name(service, hostname, &target_name, TRUE);
148 gen_send(sock, "%s GSSAPI", command);
150 /* upon receipt of the GSSAPI authentication request, server returns
151 * a null data ready challenge to us. */
152 result = gen_recv(sock, buf1, sizeof buf1);
156 if (strcmp(buf1, "+ ")) {
157 if (outlevel >= O_VERBOSE) {
158 report(stdout, GT_("Warning: received malformed challenge to \"%s GSSAPI\"!\n"), command);
163 /* now start the security context initialisation loop... */
164 sec_token = GSS_C_NO_BUFFER;
165 context = GSS_C_NO_CONTEXT;
166 if (outlevel >= O_VERBOSE)
167 report(stdout, GT_("Sending credentials\n"));
169 send_token.length = 0;
170 send_token.value = NULL;
171 maj_stat = gss_init_sec_context(&min_stat,
176 GSS_C_MUTUAL_FLAG | GSS_C_SEQUENCE_FLAG,
178 GSS_C_NO_CHANNEL_BINDINGS,
185 if (maj_stat!=GSS_S_COMPLETE && maj_stat!=GSS_S_CONTINUE_NEEDED) {
186 if (outlevel >= O_VERBOSE)
187 decode_status("gss_init_sec_context", maj_stat, min_stat);
188 (void)gss_release_name(&min_stat, &target_name);
190 /* wake up server and cancel authentication */
191 suppress_tags = TRUE;
193 suppress_tags = FALSE;
195 result = gen_recv(sock, buf1, sizeof buf1);
196 if (outlevel >= O_VERBOSE)
197 report(stderr, GT_("Error exchanging credentials\n"));
202 to64frombits(buf1, send_token.value, send_token.length);
203 gss_release_buffer(&min_stat, &send_token);
205 suppress_tags = TRUE;
206 gen_send(sock, "%s", buf1);
207 suppress_tags = FALSE;
209 if (maj_stat == GSS_S_CONTINUE_NEEDED) {
210 result = gen_recv(sock, buf1, sizeof buf1);
212 gss_release_name(&min_stat, &target_name);
215 request_buf.length = from64tobits(buf2, buf1 + 2, sizeof(buf2));
216 if ((int)request_buf.length == -1) /* in case of bad data */
217 request_buf.length = 0;
218 request_buf.value = buf2;
219 sec_token = &request_buf;
222 (maj_stat == GSS_S_CONTINUE_NEEDED);
223 gss_release_name(&min_stat, &target_name);
225 /* get security flags and buffer size */
226 result = gen_recv(sock, buf1, sizeof buf1);
230 request_buf.length = from64tobits(buf2, buf1 + 2, sizeof(buf2));
231 if ((int)request_buf.length == -1) /* in case of bad data */
232 request_buf.length = 0;
233 request_buf.value = buf2;
235 maj_stat = gss_unwrap(&min_stat, context,
236 &request_buf, &send_token, &cflags, &quality);
237 if (maj_stat != GSS_S_COMPLETE) {
238 decode_status("gss_unwrap", maj_stat, min_stat);
239 report(stderr, GT_("Couldn't unwrap security level data\n"));
240 gss_release_buffer(&min_stat, &send_token);
243 if (outlevel >= O_DEBUG)
244 report(stdout, GT_("Credential exchange complete\n"));
245 /* first octet is security levels supported. We want none, for now */
246 server_conf_flags = ((char *)send_token.value)[0];
247 if ( !(((char *)send_token.value)[0] & GSSAUTH_P_NONE) ) {
248 report(stderr, GT_("Server requires integrity and/or privacy\n"));
249 gss_release_buffer(&min_stat, &send_token);
252 ((char *)send_token.value)[0] = 0;
253 buf_size = ntohl(*((long *)send_token.value));
254 /* we don't care about buffer size if we don't wrap data */
255 gss_release_buffer(&min_stat, &send_token);
256 if (outlevel >= O_DEBUG) {
257 report(stdout, GT_("Unwrapped security level flags: %s%s%s\n"),
258 server_conf_flags & GSSAUTH_P_NONE ? "N" : "-",
259 server_conf_flags & GSSAUTH_P_INTEGRITY ? "I" : "-",
260 server_conf_flags & GSSAUTH_P_PRIVACY ? "C" : "-");
261 report(stdout, GT_("Maximum GSS token size is %ld\n"),buf_size);
264 /* now respond in kind (hack!!!) */
265 buf_size = htonl(buf_size); /* do as they do... only matters if we do enc */
266 memcpy(buf1, &buf_size, 4);
267 buf1[0] = GSSAUTH_P_NONE;
268 strlcpy(buf1+4, username, sizeof(buf1) - 4); /* server decides if princ is user */
269 request_buf.length = 4 + strlen(username) + 1;
270 request_buf.value = buf1;
271 maj_stat = gss_wrap(&min_stat, context, 0, GSS_C_QOP_DEFAULT, &request_buf,
272 &cflags, &send_token);
273 if (maj_stat != GSS_S_COMPLETE) {
274 report(stderr, GT_("Error creating security level request\n"));
277 to64frombits(buf1, send_token.value, send_token.length);
279 suppress_tags = TRUE;
280 result = gen_transact(sock, "%s", buf1);
281 suppress_tags = FALSE;
283 /* flush security context */
284 if (outlevel >= O_DEBUG)
285 report(stdout, GT_("Releasing GSS credentials\n"));
286 maj_stat = gss_delete_sec_context(&min_stat, &context, &send_token);
287 if (maj_stat != GSS_S_COMPLETE) {
288 decode_status("gss_delete_sec_context", maj_stat, min_stat);
289 report(stderr, GT_("Error releasing credentials\n"));
292 /* send_token may contain a notification to the server to flush
293 * credentials. RFC 1731 doesn't specify what to do, and since this
294 * support is only for authentication, we'll assume the server
295 * knows enough to flush its own credentials */
296 gss_release_buffer(&min_stat, &send_token);
305 /* gssapi.c ends here */