]> Pileus Git - ~andy/fetchmail/blob - gssapi.c
65582e52ac8e033db86acb267486fa78d8cc0d0c
[~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      * null data ready response. */
152     result = gen_recv(sock, buf1, sizeof buf1);
153     if (result)
154         return result;
155
156     /* now start the security context initialisation loop... */
157     sec_token = GSS_C_NO_BUFFER;
158     context = GSS_C_NO_CONTEXT;
159     if (outlevel >= O_VERBOSE)
160         report(stdout, GT_("Sending credentials\n"));
161     do {
162         send_token.length = 0;
163         send_token.value = NULL;
164         maj_stat = gss_init_sec_context(&min_stat,
165                                         GSS_C_NO_CREDENTIAL,
166                                         &context,
167                                         target_name,
168                                         GSS_C_NO_OID,
169                                         GSS_C_MUTUAL_FLAG | GSS_C_SEQUENCE_FLAG,
170                                         0,
171                                         GSS_C_NO_CHANNEL_BINDINGS,
172                                         sec_token,
173                                         NULL,
174                                         &send_token,
175                                         NULL,
176                                         NULL);
177
178         if (maj_stat!=GSS_S_COMPLETE && maj_stat!=GSS_S_CONTINUE_NEEDED) {
179             decode_status("gss_init_sec_context", maj_stat, min_stat);
180             (void)gss_release_name(&min_stat, &target_name);
181
182             /* wake up server and await NO response */
183             suppress_tags = TRUE;
184             gen_send(sock, "");
185             suppress_tags = FALSE;
186
187             result = gen_recv(sock, buf1, sizeof buf1);
188             report(stderr, GT_("Error exchanging credentials\n"));
189             if (result)
190                 return result;
191             return PS_AUTHFAIL;
192         }
193         to64frombits(buf1, send_token.value, send_token.length);
194         gss_release_buffer(&min_stat, &send_token);
195
196         suppress_tags = TRUE;
197         gen_send(sock, "%s", buf1);
198         suppress_tags = FALSE;
199
200         if (maj_stat == GSS_S_CONTINUE_NEEDED) {
201             result = gen_recv(sock, buf1, sizeof buf1);
202             if (result) {
203                 gss_release_name(&min_stat, &target_name);
204                 return result;
205             }
206             request_buf.length = from64tobits(buf2, buf1 + 2, sizeof(buf2));
207             if ((int)request_buf.length == -1)  /* in case of bad data */
208                 request_buf.length = 0;
209             request_buf.value = buf2;
210             sec_token = &request_buf;
211         }
212     } while
213         (maj_stat == GSS_S_CONTINUE_NEEDED);
214     gss_release_name(&min_stat, &target_name);
215
216     /* get security flags and buffer size */
217     result = gen_recv(sock, buf1, sizeof buf1);
218     if (result)
219         return result;
220
221     request_buf.length = from64tobits(buf2, buf1 + 2, sizeof(buf2));
222     if ((int)request_buf.length == -1)  /* in case of bad data */
223         request_buf.length = 0;
224     request_buf.value = buf2;
225
226     maj_stat = gss_unwrap(&min_stat, context,
227                           &request_buf, &send_token, &cflags, &quality);
228     if (maj_stat != GSS_S_COMPLETE) {
229         decode_status("gss_unwrap", maj_stat, min_stat);
230         report(stderr, GT_("Couldn't unwrap security level data\n"));
231         gss_release_buffer(&min_stat, &send_token);
232         return PS_AUTHFAIL;
233     }
234     if (outlevel >= O_DEBUG)
235         report(stdout, GT_("Credential exchange complete\n"));
236     /* first octet is security levels supported. We want none, for now */
237     server_conf_flags = ((char *)send_token.value)[0];
238     if ( !(((char *)send_token.value)[0] & GSSAUTH_P_NONE) ) {
239         report(stderr, GT_("Server requires integrity and/or privacy\n"));
240         gss_release_buffer(&min_stat, &send_token);
241         return PS_AUTHFAIL;
242     }
243     ((char *)send_token.value)[0] = 0;
244     buf_size = ntohl(*((long *)send_token.value));
245     /* we don't care about buffer size if we don't wrap data */
246     gss_release_buffer(&min_stat, &send_token);
247     if (outlevel >= O_DEBUG) {
248         report(stdout, GT_("Unwrapped security level flags: %s%s%s\n"),
249             server_conf_flags & GSSAUTH_P_NONE ? "N" : "-",
250             server_conf_flags & GSSAUTH_P_INTEGRITY ? "I" : "-",
251             server_conf_flags & GSSAUTH_P_PRIVACY ? "C" : "-");
252         report(stdout, GT_("Maximum GSS token size is %ld\n"),buf_size);
253     }
254
255     /* now respond in kind (hack!!!) */
256     buf_size = htonl(buf_size); /* do as they do... only matters if we do enc */
257     memcpy(buf1, &buf_size, 4);
258     buf1[0] = GSSAUTH_P_NONE;
259     strlcpy(buf1+4, username, sizeof(buf1) - 4); /* server decides if princ is user */
260     request_buf.length = 4 + strlen(username) + 1;
261     request_buf.value = buf1;
262     maj_stat = gss_wrap(&min_stat, context, 0, GSS_C_QOP_DEFAULT, &request_buf,
263         &cflags, &send_token);
264     if (maj_stat != GSS_S_COMPLETE) {
265         report(stderr, GT_("Error creating security level request\n"));
266         return PS_AUTHFAIL;
267     }
268     to64frombits(buf1, send_token.value, send_token.length);
269
270     suppress_tags = TRUE;
271     result = gen_transact(sock, "%s", buf1);
272     suppress_tags = FALSE;
273
274     /* flush security context */
275     if (outlevel >= O_DEBUG)
276         report(stdout, GT_("Releasing GSS credentials\n"));
277     maj_stat = gss_delete_sec_context(&min_stat, &context, &send_token);
278     if (maj_stat != GSS_S_COMPLETE) {
279         decode_status("gss_delete_sec_context", maj_stat, min_stat);
280         report(stderr, GT_("Error releasing credentials\n"));
281         return PS_AUTHFAIL;
282     }
283     /* send_token may contain a notification to the server to flush
284      * credentials. RFC 1731 doesn't specify what to do, and since this
285      * support is only for authentication, we'll assume the server
286      * knows enough to flush its own credentials */
287     gss_release_buffer(&min_stat, &send_token);
288
289     if (result)
290         return(result);
291     else
292         return(PS_SUCCESS);
293 }       
294 #endif /* GSSAPI */
295
296 /* gssapi.c ends here */