2 * simple_auth.c : Simple SASL-based authentication, used in case
3 * Cyrus SASL isn't available.
5 * ====================================================================
6 * Licensed to the Apache Software Foundation (ASF) under one
7 * or more contributor license agreements. See the NOTICE file
8 * distributed with this work for additional information
9 * regarding copyright ownership. The ASF licenses this file
10 * to you under the Apache License, Version 2.0 (the
11 * "License"); you may not use this file except in compliance
12 * with the License. You may obtain a copy of the License at
14 * http://www.apache.org/licenses/LICENSE-2.0
16 * Unless required by applicable law or agreed to in writing,
17 * software distributed under the License is distributed on an
18 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
19 * KIND, either express or implied. See the License for the
20 * specific language governing permissions and limitations
22 * ====================================================================
25 #include "svn_private_config.h"
27 #define APR_WANT_STRFUNC
29 #include <apr_general.h>
30 #include <apr_strings.h>
32 #include "svn_types.h"
33 #include "svn_string.h"
34 #include "svn_error.h"
36 #include "svn_ra_svn.h"
40 svn_boolean_t svn_ra_svn__find_mech(const apr_array_header_t *mechlist,
44 svn_ra_svn_item_t *elt;
46 for (i = 0; i < mechlist->nelts; i++)
48 elt = &APR_ARRAY_IDX(mechlist, i, svn_ra_svn_item_t);
49 if (elt->kind == SVN_RA_SVN_WORD && strcmp(elt->u.word, mech) == 0)
55 /* Read the "success" response to ANONYMOUS or EXTERNAL authentication. */
56 static svn_error_t *read_success(svn_ra_svn_conn_t *conn, apr_pool_t *pool)
58 const char *status, *arg;
60 SVN_ERR(svn_ra_svn__read_tuple(conn, pool, "w(?c)", &status, &arg));
61 if (strcmp(status, "failure") == 0 && arg)
62 return svn_error_createf(SVN_ERR_RA_NOT_AUTHORIZED, NULL,
63 _("Authentication error from server: %s"), arg);
64 else if (strcmp(status, "success") != 0 || arg)
65 return svn_error_create(SVN_ERR_RA_NOT_AUTHORIZED, NULL,
66 _("Unexpected server response to authentication"));
71 svn_ra_svn__do_internal_auth(svn_ra_svn__session_baton_t *sess,
72 const apr_array_header_t *mechlist,
73 const char *realm, apr_pool_t *pool)
75 svn_ra_svn_conn_t *conn = sess->conn;
76 const char *realmstring, *user, *password, *msg;
77 svn_auth_iterstate_t *iterstate;
80 realmstring = apr_psprintf(pool, "%s %s", sess->realm_prefix, realm);
82 if (sess->is_tunneled && svn_ra_svn__find_mech(mechlist, "EXTERNAL"))
84 /* Ask the server to use the tunnel connection environment (on
85 * Unix, that means uid) to determine the authentication name. */
86 SVN_ERR(svn_ra_svn__auth_response(conn, pool, "EXTERNAL", ""));
87 return read_success(conn, pool);
89 else if (svn_ra_svn__find_mech(mechlist, "ANONYMOUS"))
91 SVN_ERR(svn_ra_svn__auth_response(conn, pool, "ANONYMOUS", ""));
92 return read_success(conn, pool);
94 else if (svn_ra_svn__find_mech(mechlist, "CRAM-MD5"))
96 SVN_ERR(svn_auth_first_credentials(&creds, &iterstate,
97 SVN_AUTH_CRED_SIMPLE, realmstring,
98 sess->callbacks->auth_baton, pool));
100 return svn_error_create(SVN_ERR_RA_NOT_AUTHORIZED, NULL,
101 _("Can't get password"));
104 user = ((svn_auth_cred_simple_t *) creds)->username;
105 password = ((svn_auth_cred_simple_t *) creds)->password;
106 SVN_ERR(svn_ra_svn__auth_response(conn, pool, "CRAM-MD5", NULL));
107 SVN_ERR(svn_ra_svn__cram_client(conn, pool, user, password, &msg));
110 SVN_ERR(svn_auth_next_credentials(&creds, iterstate, pool));
113 return svn_error_createf(SVN_ERR_RA_NOT_AUTHORIZED, NULL,
114 _("Authentication error from server: %s"),
116 SVN_ERR(svn_auth_save_credentials(iterstate, pool));
120 return svn_error_create(SVN_ERR_RA_SVN_NO_MECHANISMS, NULL, NULL);