]> Pileus Git - ~andy/fetchmail/blob - gssapi.c
GSSAPI: Be more tolerant about server's SASL challenge.
[~andy/fetchmail] / gssapi.c
1 /*
2  * gssapi.c -- GSSAPI authentication (see RFC 1508)
3  *
4  * For license terms, see the file COPYING in this directory.
5  */
6
7 #include  "config.h"
8 #include  <stdio.h>
9 #include  <string.h>
10 #include  <ctype.h>
11 #if defined(STDC_HEADERS)
12 #include  <stdlib.h>
13 #endif
14 #include  "fetchmail.h"
15 #include  "socket.h"
16
17 #include  "i18n.h"
18 #include "fm_md5.h"
19
20 #include <sys/types.h>
21 #include <netinet/in.h>  /* for htonl/ntohl */
22
23 #ifdef GSSAPI
24 #  ifdef HAVE_GSS_H
25 #    include <gss.h>
26 #  else
27 #  if defined(HAVE_GSSAPI_H) && !defined(HAVE_GSSAPI_GSSAPI_H)
28 #    include <gssapi.h>
29 #  endif
30 #  ifdef HAVE_GSSAPI_GSSAPI_H
31 #    include <gssapi/gssapi.h>
32 #  endif
33 #  ifdef HAVE_GSSAPI_GSSAPI_GENERIC_H
34 #    include <gssapi/gssapi_generic.h>
35 #  endif
36 #  ifndef HAVE_GSS_C_NT_HOSTBASED_SERVICE
37 #    define GSS_C_NT_HOSTBASED_SERVICE gss_nt_service_name
38 #  endif
39 #  endif
40
41 static void decode_subr(const char *m, uint32_t code, int type)
42 {
43     uint32_t maj, min, context;
44     gss_buffer_desc msg = GSS_C_EMPTY_BUFFER;
45
46     context = 0;
47     do {
48         maj = gss_display_status(&min, code, type, GSS_C_NO_OID,
49                 &context, &msg);
50
51         if (maj != GSS_S_COMPLETE) {
52             report(stderr, GT_("GSSAPI error in gss_display_status called from <%s>\n"), m);
53             break;
54         }
55         report(stderr, GT_("GSSAPI error %s: %.*s\n"), m,
56                 (int)msg.length, (char *)msg.value);
57         (void)gss_release_buffer(&min, &msg);
58     } while(context);
59 }
60
61 static void decode_status(const char *m, uint32_t major, uint32_t minor)
62 {
63     decode_subr(m, major, GSS_C_GSS_CODE);
64     decode_subr(m, minor, GSS_C_MECH_CODE);
65 }
66
67 #define GSSAUTH_P_NONE      1
68 #define GSSAUTH_P_INTEGRITY 2
69 #define GSSAUTH_P_PRIVACY   4
70
71 static int import_name(const char *service, const char *hostname,
72         gss_name_t *target_name, flag verbose)
73 {
74     char *buf1;
75     size_t buf1siz;
76     OM_uint32 maj_stat, min_stat;
77     gss_buffer_desc request_buf;
78
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);
90         return PS_AUTHFAIL;
91     }
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);
96     }
97     (void)gss_release_buffer(&min_stat, &request_buf);
98
99     return PS_SUCCESS;
100 }
101
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)
107 {
108     OM_uint32 maj_stat, min_stat;
109     gss_cred_usage_t cu;
110     gss_name_t target_name;
111
112     (void)import_name(service, hostname, &target_name, FALSE);
113     (void)gss_release_name(&min_stat, &target_name);
114
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"));
123         }
124         return PS_AUTHFAIL;
125     }
126
127     return PS_SUCCESS;
128 }
129
130 int do_gssauth(int sock, const char *command, const char *service,
131                 const char *hostname, const char *username)
132 {
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;
137     gss_qop_t quality;
138     int cflags;
139     OM_uint32 maj_stat, min_stat;
140     char buf1[8192], buf2[8192], server_conf_flags;
141     unsigned long buf_size;
142     int result;
143
144     result = import_name(service, hostname, &target_name, TRUE);
145     if (result)
146         return result;
147
148     gen_send(sock, "%s GSSAPI", command);
149
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);
153     if (result)
154         return result;
155
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);
159         }
160         goto cancelfail;
161     }
162
163
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"));
169     do {
170         send_token.length = 0;
171         send_token.value = NULL;
172         maj_stat = gss_init_sec_context(&min_stat,
173                                         GSS_C_NO_CREDENTIAL,
174                                         &context,
175                                         target_name,
176                                         GSS_C_NO_OID,
177                                         GSS_C_MUTUAL_FLAG | GSS_C_SEQUENCE_FLAG,
178                                         0,
179                                         GSS_C_NO_CHANNEL_BINDINGS,
180                                         sec_token,
181                                         NULL,
182                                         &send_token,
183                                         NULL,
184                                         NULL);
185
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);
190
191 cancelfail:
192             /* wake up server and cancel authentication */
193             suppress_tags = TRUE;
194             gen_send(sock, "*");
195             suppress_tags = FALSE;
196
197             result = gen_recv(sock, buf1, sizeof buf1);
198             if (outlevel >= O_VERBOSE)
199                 report(stderr, GT_("Error exchanging credentials\n"));
200             if (result)
201                 return result;
202             return PS_AUTHFAIL;
203         }
204         to64frombits(buf1, send_token.value, send_token.length);
205         gss_release_buffer(&min_stat, &send_token);
206
207         suppress_tags = TRUE;
208         gen_send(sock, "%s", buf1);
209         suppress_tags = FALSE;
210
211         if (maj_stat == GSS_S_CONTINUE_NEEDED) {
212             result = gen_recv(sock, buf1, sizeof buf1);
213             if (result) {
214                 gss_release_name(&min_stat, &target_name);
215                 return result;
216             }
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;
222         }
223     } while
224         (maj_stat == GSS_S_CONTINUE_NEEDED);
225     gss_release_name(&min_stat, &target_name);
226
227     /* get security flags and buffer size */
228     result = gen_recv(sock, buf1, sizeof buf1);
229     if (result)
230         return result;
231
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;
236
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);
243         return PS_AUTHFAIL;
244     }
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);
252         return PS_AUTHFAIL;
253     }
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);
264     }
265
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"));
277         return PS_AUTHFAIL;
278     }
279     to64frombits(buf1, send_token.value, send_token.length);
280
281     suppress_tags = TRUE;
282     result = gen_transact(sock, "%s", buf1);
283     suppress_tags = FALSE;
284
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"));
292         return PS_AUTHFAIL;
293     }
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);
299
300     if (result)
301         return(result);
302     else
303         return(PS_SUCCESS);
304 }       
305 #endif /* GSSAPI */
306
307 /* gssapi.c ends here */