Remove no-longer-used svn_*_get_mergeinfo_for_tree APIs.
[svn.git] / subversion / svnserve / cyrus_auth.c
blob7dd74193b369712bf23ed25ac6ff8c038d90c600
1 /*
2 * sasl_auth.c : Functions for SASL-based authentication
4 * ====================================================================
5 * Copyright (c) 2006-2007 CollabNet. All rights reserved.
7 * This software is licensed as described in the file COPYING, which
8 * you should have received as part of this distribution. The terms
9 * are also available at http://subversion.tigris.org/license-1.html.
10 * If newer versions of this license are posted there, you may use a
11 * newer version instead, at your option.
13 * This software consists of voluntary contributions made by many
14 * individuals. For exact contribution history, see the revision
15 * history and logs, available at http://subversion.tigris.org/.
16 * ====================================================================
19 #include "svn_private_config.h"
20 #ifdef SVN_HAVE_SASL
22 #define APR_WANT_STRFUNC
23 #include <apr_want.h>
24 #include <apr_general.h>
25 #include <apr_strings.h>
27 #include "svn_types.h"
28 #include "svn_string.h"
29 #include "svn_pools.h"
30 #include "svn_error.h"
31 #include "svn_ra_svn.h"
32 #include "svn_base64.h"
34 #include "private/svn_atomic.h"
35 #include "private/ra_svn_sasl.h"
37 #include "server.h"
39 /* SASL calls this function before doing anything with a username, which gives
40 us an opportunity to do some sanity-checking. If the username contains
41 an '@', SASL interprets the part following the '@' as the name of the
42 authentication realm, and worst of all, this realm overrides the one that
43 we pass to sasl_server_new(). If we didn't check this, a user that could
44 successfully authenticate in one realm would be able to authenticate
45 in any other realm, simply by appending '@realm' to his username. */
46 static int canonicalize_username(sasl_conn_t *conn,
47 void *context, /* not used */
48 const char *in, /* the username */
49 unsigned inlen, /* its length */
50 unsigned flags, /* not used */
51 const char *user_realm,
52 char *out, /* the output buffer */
53 unsigned out_max, unsigned *out_len)
55 int realm_len = strlen(user_realm);
56 char *pos;
58 *out_len = inlen;
60 /* If the username contains an '@', the part after the '@' is the realm
61 that the user wants to authenticate in. */
62 pos = memchr(in, '@', inlen);
63 if (pos)
65 /* The only valid realm is user_realm (i.e. the repository's realm).
66 If the user gave us another realm, complain. */
67 if (strncmp(pos+1, user_realm, inlen-(pos-in+1)) != 0)
68 return SASL_BADPROT;
70 else
71 *out_len += realm_len + 1;
73 /* First, check that the output buffer is large enough. */
74 if (*out_len > out_max)
75 return SASL_BADPROT;
77 /* Copy the username part. */
78 strncpy(out, in, inlen);
80 /* If necessary, copy the realm part. */
81 if (!pos)
83 out[inlen] = '@';
84 strncpy(&out[inlen+1], user_realm, realm_len);
87 return SASL_OK;
90 static sasl_callback_t callbacks[] =
92 { SASL_CB_CANON_USER, canonicalize_username, NULL },
93 { SASL_CB_LIST_END, NULL, NULL }
96 static svn_error_t *initialize(apr_pool_t *pool)
98 int result;
99 apr_status_t status;
101 status = svn_ra_svn__sasl_common_init(pool);
102 if (status)
103 return svn_error_wrap_apr(status,
104 _("Could not initialize the SASL library"));
106 /* The second parameter tells SASL to look for a configuration file
107 named subversion.conf. */
108 result = sasl_server_init(callbacks, "subversion");
109 if (result != SASL_OK)
111 svn_error_t *err = svn_error_create(SVN_ERR_RA_NOT_AUTHORIZED, NULL,
112 sasl_errstring(result, NULL, NULL));
113 return svn_error_quick_wrap(err,
114 _("Could not initialize the SASL library"));
116 return SVN_NO_ERROR;
119 svn_error_t *cyrus_init(apr_pool_t *pool)
121 SVN_ERR(svn_atomic__init_once(&svn_ra_svn__sasl_status, initialize, pool));
122 return SVN_NO_ERROR;
125 /* Tell the client the authentication failed. This is only used during
126 the authentication exchange (i.e. inside try_auth()). */
127 static svn_error_t *
128 fail_auth(svn_ra_svn_conn_t *conn, apr_pool_t *pool, sasl_conn_t *sasl_ctx)
130 const char *msg = sasl_errdetail(sasl_ctx);
131 SVN_ERR(svn_ra_svn_write_tuple(conn, pool, "w(c)", "failure", msg));
132 return svn_ra_svn_flush(conn, pool);
135 /* Used if we run into a SASL error outside try_auth(). */
136 static svn_error_t *
137 fail_cmd(svn_ra_svn_conn_t *conn, apr_pool_t *pool, sasl_conn_t *sasl_ctx)
139 svn_error_t *err = svn_error_create(SVN_ERR_RA_NOT_AUTHORIZED, NULL,
140 sasl_errdetail(sasl_ctx));
141 SVN_ERR(svn_ra_svn_write_cmd_failure(conn, pool, err));
142 return svn_ra_svn_flush(conn, pool);
145 static svn_error_t *try_auth(svn_ra_svn_conn_t *conn,
146 sasl_conn_t *sasl_ctx,
147 apr_pool_t *pool,
148 server_baton_t *b,
149 svn_boolean_t *success)
151 const char *out, *mech;
152 const svn_string_t *arg = NULL, *in;
153 unsigned int outlen;
154 int result;
155 svn_boolean_t use_base64;
157 *success = FALSE;
159 /* Read the client's chosen mech and the initial token. */
160 SVN_ERR(svn_ra_svn_read_tuple(conn, pool, "w(?s)", &mech, &in));
162 if (strcmp(mech, "EXTERNAL") == 0 && !in)
163 in = svn_string_create(b->tunnel_user, pool);
164 else if (in)
165 in = svn_base64_decode_string(in, pool);
167 /* For CRAM-MD5, we don't base64-encode stuff. */
168 use_base64 = (strcmp(mech, "CRAM-MD5") != 0);
170 result = sasl_server_start(sasl_ctx, mech,
171 in ? in->data : NULL,
172 in ? in->len : 0, &out, &outlen);
174 if (result != SASL_OK && result != SASL_CONTINUE)
175 return fail_auth(conn, pool, sasl_ctx);
177 while (result == SASL_CONTINUE)
179 svn_ra_svn_item_t *item;
181 arg = svn_string_ncreate(out, outlen, pool);
182 /* Encode what we send to the client. */
183 if (use_base64)
184 arg = svn_base64_encode_string(arg, pool);
186 SVN_ERR(svn_ra_svn_write_tuple(conn, pool, "w(s)", "step", arg));
188 /* Read and decode the client response. */
189 SVN_ERR(svn_ra_svn_read_item(conn, pool, &item));
190 if (item->kind != SVN_RA_SVN_STRING)
191 return SVN_NO_ERROR;
193 in = item->u.string;
194 if (use_base64)
195 in = svn_base64_decode_string(in, pool);
196 result = sasl_server_step(sasl_ctx, in->data, in->len, &out, &outlen);
199 if (result != SASL_OK)
200 return fail_auth(conn, pool, sasl_ctx);
202 /* Send our last response, if necessary. */
203 if (outlen)
204 arg = svn_base64_encode_string(svn_string_ncreate(out, outlen, pool),
205 pool);
206 else
207 arg = NULL;
209 *success = TRUE;
210 SVN_ERR(svn_ra_svn_write_tuple(conn, pool, "w(?s)", "success", arg));
212 return SVN_NO_ERROR;
215 static apr_status_t sasl_dispose_cb(void *data)
217 sasl_conn_t *sasl_ctx = (sasl_conn_t*) data;
218 sasl_dispose(&sasl_ctx);
219 return APR_SUCCESS;
222 svn_error_t *cyrus_auth_request(svn_ra_svn_conn_t *conn,
223 apr_pool_t *pool,
224 server_baton_t *b,
225 enum access_type required,
226 svn_boolean_t needs_username)
228 sasl_conn_t *sasl_ctx;
229 apr_pool_t *subpool;
230 apr_status_t apr_err;
231 const char *localaddrport = NULL, *remoteaddrport = NULL;
232 const char *mechlist, *val;
233 char hostname[APRMAXHOSTLEN + 1];
234 sasl_security_properties_t secprops;
235 svn_boolean_t success, no_anonymous;
236 int mech_count, result = SASL_OK;
238 SVN_ERR(svn_ra_svn__get_addresses(&localaddrport, &remoteaddrport,
239 conn, pool));
240 apr_err = apr_gethostname(hostname, sizeof(hostname), pool);
241 if (apr_err)
243 svn_error_t *err = svn_error_wrap_apr(apr_err, _("Can't get hostname"));
244 SVN_ERR(svn_ra_svn_write_cmd_failure(conn, pool, err));
245 return svn_ra_svn_flush(conn, pool);
248 /* Create a SASL context. SASL_SUCCESS_DATA tells SASL that the protocol
249 supports sending data along with the final "success" message. */
250 result = sasl_server_new("svn",
251 hostname, b->realm,
252 localaddrport, remoteaddrport,
253 NULL, SASL_SUCCESS_DATA,
254 &sasl_ctx);
255 if (result != SASL_OK)
257 svn_error_t *err = svn_error_create(SVN_ERR_RA_NOT_AUTHORIZED, NULL,
258 sasl_errstring(result, NULL, NULL));
259 SVN_ERR(svn_ra_svn_write_cmd_failure(conn, pool, err));
260 return svn_ra_svn_flush(conn, pool);
263 /* Make sure the context is always destroyed. */
264 apr_pool_cleanup_register(b->pool, sasl_ctx, sasl_dispose_cb,
265 apr_pool_cleanup_null);
267 /* Initialize security properties. */
268 svn_ra_svn__default_secprops(&secprops);
270 /* Don't allow PLAIN or LOGIN, since we don't support TLS yet. */
271 secprops.security_flags = SASL_SEC_NOPLAINTEXT;
273 /* Don't allow ANONYMOUS if a username is required. */
274 no_anonymous = needs_username || get_access(b, UNAUTHENTICATED) < required;
275 if (no_anonymous)
276 secprops.security_flags |= SASL_SEC_NOANONYMOUS;
278 svn_config_get(b->cfg, &val,
279 SVN_CONFIG_SECTION_SASL,
280 SVN_CONFIG_OPTION_MIN_SSF,
281 "0");
282 secprops.min_ssf = atoi(val);
284 svn_config_get(b->cfg, &val,
285 SVN_CONFIG_SECTION_SASL,
286 SVN_CONFIG_OPTION_MAX_SSF,
287 "256");
288 secprops.max_ssf = atoi(val);
290 /* Set security properties. */
291 result = sasl_setprop(sasl_ctx, SASL_SEC_PROPS, &secprops);
292 if (result != SASL_OK)
293 return fail_cmd(conn, pool, sasl_ctx);
295 /* SASL needs to know if we are externally authenticated. */
296 if (b->tunnel_user)
297 result = sasl_setprop(sasl_ctx, SASL_AUTH_EXTERNAL, b->tunnel_user);
298 if (result != SASL_OK)
299 return fail_cmd(conn, pool, sasl_ctx);
301 /* Get the list of mechanisms. */
302 result = sasl_listmech(sasl_ctx, NULL, NULL, " ", NULL,
303 &mechlist, NULL, &mech_count);
305 if (result != SASL_OK)
306 return fail_cmd(conn, pool, sasl_ctx);
308 if (mech_count == 0)
310 svn_error_t *err = svn_error_create(SVN_ERR_RA_NOT_AUTHORIZED, NULL,
311 _("Could not obtain the list"
312 " of SASL mechanisms"));
313 SVN_ERR(svn_ra_svn_write_cmd_failure(conn, pool, err));
314 return svn_ra_svn_flush(conn, pool);
317 /* Send the list of mechanisms and the realm to the client. */
318 SVN_ERR(svn_ra_svn_write_cmd_response(conn, pool, "(w)c",
319 mechlist, b->realm));
321 /* The main authentication loop. */
322 subpool = svn_pool_create(pool);
325 svn_pool_clear(subpool);
326 SVN_ERR(try_auth(conn, sasl_ctx, subpool, b, &success));
328 while (!success);
329 svn_pool_destroy(subpool);
331 SVN_ERR(svn_ra_svn__enable_sasl_encryption(conn, sasl_ctx, pool));
333 if (no_anonymous)
335 char *p;
336 const void *user;
338 /* Get the authenticated username. */
339 result = sasl_getprop(sasl_ctx, SASL_USERNAME, &user);
341 if (result != SASL_OK)
342 return fail_cmd(conn, pool, sasl_ctx);
344 if ((p = strchr(user, '@')) != NULL)
345 /* Drop the realm part. */
346 b->user = apr_pstrndup(b->pool, user, p - (char *)user);
347 else
349 svn_error_t *err;
350 err = svn_error_create(SVN_ERR_RA_NOT_AUTHORIZED, NULL,
351 _("Couldn't obtain the authenticated"
352 " username"));
353 SVN_ERR(svn_ra_svn_write_cmd_failure(conn, pool, err));
354 return svn_ra_svn_flush(conn, pool);
358 return SVN_NO_ERROR;
361 #endif /* SVN_HAVE_SASL */