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 (buf1[0] != '+' || strspn(buf1 + 1, " \t") < strlen(buf1 + 1)) {
157 if (outlevel >= O_VERBOSE) {
158 report(stdout, GT_("Received malformed challenge to \"%s GSSAPI\"!\n"), command);
164 /* now start the security context initialisation loop... */
165 sec_token = GSS_C_NO_BUFFER;
166 context = GSS_C_NO_CONTEXT;
167 if (outlevel >= O_VERBOSE)
168 report(stdout, GT_("Sending credentials\n"));
170 send_token.length = 0;
171 send_token.value = NULL;
172 maj_stat = gss_init_sec_context(&min_stat,
177 GSS_C_MUTUAL_FLAG | GSS_C_SEQUENCE_FLAG,
179 GSS_C_NO_CHANNEL_BINDINGS,
186 if (maj_stat!=GSS_S_COMPLETE && maj_stat!=GSS_S_CONTINUE_NEEDED) {
187 if (outlevel >= O_VERBOSE)
188 decode_status("gss_init_sec_context", maj_stat, min_stat);
189 (void)gss_release_name(&min_stat, &target_name);
192 /* wake up server and cancel authentication */
193 suppress_tags = TRUE;
195 suppress_tags = FALSE;
197 result = gen_recv(sock, buf1, sizeof buf1);
198 if (outlevel >= O_VERBOSE)
199 report(stderr, GT_("Error exchanging credentials\n"));
204 to64frombits(buf1, send_token.value, send_token.length);
205 gss_release_buffer(&min_stat, &send_token);
207 suppress_tags = TRUE;
208 gen_send(sock, "%s", buf1);
209 suppress_tags = FALSE;
211 if (maj_stat == GSS_S_CONTINUE_NEEDED) {
212 result = gen_recv(sock, buf1, sizeof buf1);
214 gss_release_name(&min_stat, &target_name);
217 request_buf.length = from64tobits(buf2, buf1 + 2, sizeof(buf2));
218 if ((int)request_buf.length == -1) /* in case of bad data */
219 request_buf.length = 0;
220 request_buf.value = buf2;
221 sec_token = &request_buf;
224 (maj_stat == GSS_S_CONTINUE_NEEDED);
225 gss_release_name(&min_stat, &target_name);
227 /* get security flags and buffer size */
228 result = gen_recv(sock, buf1, sizeof buf1);
232 request_buf.length = from64tobits(buf2, buf1 + 2, sizeof(buf2));
233 if ((int)request_buf.length == -1) /* in case of bad data */
234 request_buf.length = 0;
235 request_buf.value = buf2;
237 maj_stat = gss_unwrap(&min_stat, context,
238 &request_buf, &send_token, &cflags, &quality);
239 if (maj_stat != GSS_S_COMPLETE) {
240 decode_status("gss_unwrap", maj_stat, min_stat);
241 report(stderr, GT_("Couldn't unwrap security level data\n"));
242 gss_release_buffer(&min_stat, &send_token);
245 if (outlevel >= O_DEBUG)
246 report(stdout, GT_("Credential exchange complete\n"));
247 /* first octet is security levels supported. We want none, for now */
248 server_conf_flags = ((char *)send_token.value)[0];
249 if ( !(((char *)send_token.value)[0] & GSSAUTH_P_NONE) ) {
250 report(stderr, GT_("Server requires integrity and/or privacy\n"));
251 gss_release_buffer(&min_stat, &send_token);
254 ((char *)send_token.value)[0] = 0;
255 buf_size = ntohl(*((long *)send_token.value));
256 /* we don't care about buffer size if we don't wrap data */
257 gss_release_buffer(&min_stat, &send_token);
258 if (outlevel >= O_DEBUG) {
259 report(stdout, GT_("Unwrapped security level flags: %s%s%s\n"),
260 server_conf_flags & GSSAUTH_P_NONE ? "N" : "-",
261 server_conf_flags & GSSAUTH_P_INTEGRITY ? "I" : "-",
262 server_conf_flags & GSSAUTH_P_PRIVACY ? "C" : "-");
263 report(stdout, GT_("Maximum GSS token size is %ld\n"),buf_size);
266 /* now respond in kind (hack!!!) */
267 buf_size = htonl(buf_size); /* do as they do... only matters if we do enc */
268 memcpy(buf1, &buf_size, 4);
269 buf1[0] = GSSAUTH_P_NONE;
270 strlcpy(buf1+4, username, sizeof(buf1) - 4); /* server decides if princ is user */
271 request_buf.length = 4 + strlen(username) + 1;
272 request_buf.value = buf1;
273 maj_stat = gss_wrap(&min_stat, context, 0, GSS_C_QOP_DEFAULT, &request_buf,
274 &cflags, &send_token);
275 if (maj_stat != GSS_S_COMPLETE) {
276 report(stderr, GT_("Error creating security level request\n"));
279 to64frombits(buf1, send_token.value, send_token.length);
281 suppress_tags = TRUE;
282 result = gen_transact(sock, "%s", buf1);
283 suppress_tags = FALSE;
285 /* flush security context */
286 if (outlevel >= O_DEBUG)
287 report(stdout, GT_("Releasing GSS credentials\n"));
288 maj_stat = gss_delete_sec_context(&min_stat, &context, &send_token);
289 if (maj_stat != GSS_S_COMPLETE) {
290 decode_status("gss_delete_sec_context", maj_stat, min_stat);
291 report(stderr, GT_("Error releasing credentials\n"));
294 /* send_token may contain a notification to the server to flush
295 * credentials. RFC 1731 doesn't specify what to do, and since this
296 * support is only for authentication, we'll assume the server
297 * knows enough to flush its own credentials */
298 gss_release_buffer(&min_stat, &send_token);
307 /* gssapi.c ends here */