pop3.c: GCC 4.9 warnings
[s-mailx.git] / imap_gssapi.h
blobf0c551138322c886098aeda85a51e89045b6683b
1 /*@ S-nail - a mail user agent derived from Berkeley Mail.
2 *@ Implementation of IMAP GSS-API authentication according to RFC 1731.
3 *@ TODO GSS-API should also be joined into "a VFS". TODO LEAKS (error path)
5 * Copyright (c) 2000-2004 Gunnar Ritter, Freiburg i. Br., Germany.
6 * Copyright (c) 2012 - 2014 Steffen (Daode) Nurpmeso <sdaoden@users.sf.net>.
7 */
8 /*
9 * Copyright (c) 2004 Gunnar Ritter.
10 * All rights reserved.
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 * 3. All advertising materials mentioning features or use of this software
21 * must display the following acknowledgement:
22 * This product includes software developed by Gunnar Ritter
23 * and his contributors.
24 * 4. Neither the name of Gunnar Ritter nor the names of his contributors
25 * may be used to endorse or promote products derived from this software
26 * without specific prior written permission.
28 * THIS SOFTWARE IS PROVIDED BY GUNNAR RITTER AND CONTRIBUTORS ``AS IS'' AND
29 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
30 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
31 * ARE DISCLAIMED. IN NO EVENT SHALL GUNNAR RITTER OR CONTRIBUTORS BE LIABLE
32 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
34 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
35 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
36 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
37 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38 * SUCH DAMAGE.
42 * Partially derived from sample code in:
44 * GSS-API Programming Guide
45 * Part No: 816-1331-11
46 * Sun Microsystems, Inc. 4150 Network Circle Santa Clara, CA 95054 U.S.A.
48 * (c) 2002 Sun Microsystems
51 * Copyright 1994 by OpenVision Technologies, Inc.
53 * Permission to use, copy, modify, distribute, and sell this software
54 * and its documentation for any purpose is hereby granted without fee,
55 * provided that the above copyright notice appears in all copies and
56 * that both that copyright notice and this permission notice appear in
57 * supporting documentation, and that the name of OpenVision not be used
58 * in advertising or publicity pertaining to distribution of the software
59 * without specific, written prior permission. OpenVision makes no
60 * representations about the suitability of this software for any
61 * purpose. It is provided "as is" without express or implied warranty.
63 * OPENVISION DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
64 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
65 * EVENT SHALL OPENVISION BE LIABLE FOR ANY SPECIAL, INDIRECT OR
66 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF
67 * USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
68 * OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
69 * PERFORMANCE OF THIS SOFTWARE.
72 #ifdef HAVE_GSSAPI
73 #ifndef GSSAPI_REG_INCLUDE
74 # include <gssapi/gssapi.h>
75 # ifdef GSSAPI_OLD_STYLE
76 # include <gssapi/gssapi_generic.h>
77 # define GSS_C_NT_HOSTBASED_SERVICE gss_nt_service_name
78 # endif
79 #else
80 # include <gssapi.h>
81 #endif
83 static void _imap_gssapi_error1(const char *s, OM_uint32 code, int typ);
84 static void _imap_gssapi_error(const char *s, OM_uint32 maj_stat,
85 OM_uint32 min_stat);
87 static void
88 _imap_gssapi_error1(const char *s, OM_uint32 code, int typ)
90 OM_uint32 maj_stat, min_stat;
91 gss_buffer_desc msg = GSS_C_EMPTY_BUFFER;
92 OM_uint32 msg_ctx = 0;
93 NYD_ENTER;
95 do {
96 maj_stat = gss_display_status(&min_stat, code, typ, GSS_C_NO_OID,
97 &msg_ctx, &msg);
98 if (maj_stat == GSS_S_COMPLETE) {
99 fprintf(stderr, "GSS error: %s / %s\n", s, (char*)msg.value);
100 if (msg.length != 0)
101 gss_release_buffer(&min_stat, &msg);
102 } else {
103 fprintf(stderr, "GSS error: %s / unknown\n", s);
104 break;
106 } while (msg_ctx);
107 NYD_LEAVE;
110 static void
111 _imap_gssapi_error(const char *s, OM_uint32 maj_stat, OM_uint32 min_stat)
113 NYD_ENTER;
114 _imap_gssapi_error1(s, maj_stat, GSS_C_GSS_CODE);
115 _imap_gssapi_error1(s, min_stat, GSS_C_MECH_CODE);
116 NYD_LEAVE;
119 static enum okay
120 _imap_gssapi(struct mailbox *mp, struct ccred *ccred)
122 char o[LINESIZE];
123 struct str in, out;
124 gss_buffer_desc send_tok, recv_tok, *token_ptr;
125 gss_name_t target_name;
126 gss_ctx_id_t gss_context;
127 OM_uint32 maj_stat, min_stat, ret_flags;
128 int conf_state;
129 FILE *queuefp = NULL;
130 char *server, *cp;
131 enum okay ok = STOP;
132 NYD_X;
134 { size_t i = strlen(mp->mb_imap_account) +1;
135 server = salloc(i);
136 memcpy(server, mp->mb_imap_account, i);
138 if (!strncmp(server, "imap://", 7))
139 server += 7;
140 else if (!strncmp(server, "imaps://", 8))
141 server += 8;
142 if ((cp = UNCONST(last_at_before_slash(server))) != NULL)
143 server = &cp[1];
144 for (cp = server; *cp; cp++)
145 *cp = lowerconv(*cp);
146 send_tok.value = salloc(send_tok.length = strlen(server) + 6);
147 snprintf(send_tok.value, send_tok.length, "imap@%s", server);
148 maj_stat = gss_import_name(&min_stat, &send_tok, GSS_C_NT_HOSTBASED_SERVICE,
149 &target_name);
150 if (maj_stat != GSS_S_COMPLETE) {
151 _imap_gssapi_error(send_tok.value, maj_stat, min_stat);
152 return STOP;
154 token_ptr = GSS_C_NO_BUFFER;
155 gss_context = GSS_C_NO_CONTEXT;
156 maj_stat = gss_init_sec_context(&min_stat,
157 GSS_C_NO_CREDENTIAL,
158 &gss_context,
159 target_name,
160 GSS_C_NO_OID,
161 GSS_C_MUTUAL_FLAG|GSS_C_SEQUENCE_FLAG,
163 GSS_C_NO_CHANNEL_BINDINGS,
164 token_ptr,
165 NULL,
166 &send_tok,
167 &ret_flags,
168 NULL);
169 if (maj_stat != GSS_S_COMPLETE && maj_stat != GSS_S_CONTINUE_NEEDED) {
170 _imap_gssapi_error("initializing GSS context", maj_stat, min_stat);
171 gss_release_name(&min_stat, &target_name);
172 return STOP;
174 snprintf(o, sizeof o, "%s AUTHENTICATE GSSAPI\r\n", tag(1));
175 IMAP_OUT(o, 0, return STOP);
177 * No response data expected.
179 imap_answer(mp, 1);
180 if (response_type != RESPONSE_CONT)
181 return STOP;
182 while (maj_stat == GSS_S_CONTINUE_NEEDED) {
184 * Pass token obtained from first gss_init_sec_context() call.
186 b64_encode_buf(&out, send_tok.value, send_tok.length,
187 B64_SALLOC | B64_CRLF);
188 gss_release_buffer(&min_stat, &send_tok);
189 IMAP_OUT(out.s, 0, return STOP);
190 imap_answer(mp, 1);
191 if (response_type != RESPONSE_CONT)
192 return STOP;
193 out.s = NULL;
194 in.s = responded_text;
195 in.l = strlen(responded_text);
196 b64_decode(&out, &in, NULL);
197 recv_tok.value = out.s;
198 recv_tok.length = out.l;
199 token_ptr = &recv_tok;
200 maj_stat = gss_init_sec_context(&min_stat,
201 GSS_C_NO_CREDENTIAL,
202 &gss_context,
203 target_name,
204 GSS_C_NO_OID,
205 GSS_C_MUTUAL_FLAG|GSS_C_SEQUENCE_FLAG,
207 GSS_C_NO_CHANNEL_BINDINGS,
208 token_ptr,
209 NULL,
210 &send_tok,
211 &ret_flags,
212 NULL);
213 free(out.s);
214 if (maj_stat != GSS_S_COMPLETE && maj_stat != GSS_S_CONTINUE_NEEDED) {
215 _imap_gssapi_error("initializing context", maj_stat, min_stat);
216 gss_release_name(&min_stat, &target_name);
217 return STOP;
221 * Pass token obtained from second gss_init_sec_context() call.
223 gss_release_name(&min_stat, &target_name);
224 b64_encode_buf(&out, send_tok.value, send_tok.length, B64_SALLOC | B64_CRLF);
225 gss_release_buffer(&min_stat, &send_tok);
226 IMAP_OUT(out.s, 0, return STOP);
228 * First octet: bit-mask with protection mechanisms.
229 * Second to fourth octet: maximum message size in network byte order.
231 * This code currently does not care about the values.
233 imap_answer(mp, 1);
234 if (response_type != RESPONSE_CONT)
235 return STOP;
236 out.s = NULL;
237 in.s = responded_text;
238 in.l = strlen(responded_text);
239 b64_decode(&out, &in, NULL);
240 recv_tok.value = out.s;
241 recv_tok.length = out.l;
242 maj_stat = gss_unwrap(&min_stat, gss_context, &recv_tok, &send_tok,
243 &conf_state, NULL);
244 free(out.s);
245 if (maj_stat != GSS_S_COMPLETE) {
246 _imap_gssapi_error("unwrapping data", maj_stat, min_stat);
247 return STOP;
250 * First octet: bit-mask with protection mechanisms (1 = no protection
251 * mechanism).
252 * Second to fourth octet: maximum message size in network byte order.
253 * Fifth and following octets: user name string.
255 o[0] = 1;
256 o[1] = 0;
257 o[2] = o[3] = (char)0377;
258 snprintf(&o[4], sizeof o - 4, "%s", ccred->cc_user.s);
259 send_tok.value = o;
260 send_tok.length = strlen(&o[4]) + 5;
261 maj_stat = gss_wrap(&min_stat, gss_context, 0, GSS_C_QOP_DEFAULT, &send_tok,
262 &conf_state, &recv_tok);
263 if (maj_stat != GSS_S_COMPLETE) {
264 _imap_gssapi_error("wrapping data", maj_stat, min_stat);
265 return STOP;
267 b64_encode_buf(&out, recv_tok.value, recv_tok.length, B64_SALLOC | B64_CRLF);
268 IMAP_OUT(out.s, MB_COMD, return STOP);
269 while (mp->mb_active & MB_COMD)
270 ok = imap_answer(mp, 1);
271 gss_delete_sec_context(&min_stat, &gss_context, &recv_tok);
272 gss_release_buffer(&min_stat, &recv_tok);
273 return ok;
276 # undef GSS_C_NT_HOSTBASED_SERVICE
277 #endif /* HAVE_GSSAPI */
279 /* vim:set fenc=utf-8:s-it-mode */