2 * config_auth.c : authentication files in the user config area
4 * ====================================================================
5 * Licensed to the Apache Software Foundation (ASF) under one
6 * or more contributor license agreements. See the NOTICE file
7 * distributed with this work for additional information
8 * regarding copyright ownership. The ASF licenses this file
9 * to you under the Apache License, Version 2.0 (the
10 * "License"); you may not use this file except in compliance
11 * with the License. You may obtain a copy of the License at
13 * http://www.apache.org/licenses/LICENSE-2.0
15 * Unless required by applicable law or agreed to in writing,
16 * software distributed under the License is distributed on an
17 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
18 * KIND, either express or implied. See the License for the
19 * specific language governing permissions and limitations
21 * ====================================================================
26 #include "svn_dirent_uri.h"
29 #include "svn_pools.h"
30 #include "config_impl.h"
34 #include "svn_private_config.h"
36 #include "private/svn_auth_private.h"
39 svn_auth__file_path(const char **path,
40 const char *cred_kind,
41 const char *realmstring,
42 const char *config_dir,
45 const char *authdir_path, *hexname;
46 svn_checksum_t *checksum;
48 /* Construct the path to the directory containing the creds files,
49 e.g. "~/.subversion/auth/svn.simple". The last component is
50 simply the cred_kind. */
51 SVN_ERR(svn_config_get_user_config_path(&authdir_path, config_dir,
52 SVN_CONFIG__AUTH_SUBDIR, pool));
55 authdir_path = svn_dirent_join(authdir_path, cred_kind, pool);
57 /* Construct the basename of the creds file. It's just the
58 realmstring converted into an md5 hex string. */
59 SVN_ERR(svn_checksum(&checksum, svn_checksum_md5, realmstring,
60 strlen(realmstring), pool));
61 hexname = svn_checksum_to_cstring(checksum, pool);
63 *path = svn_dirent_join(authdir_path, hexname, pool);
73 svn_config_read_auth_data(apr_hash_t **hash,
74 const char *cred_kind,
75 const char *realmstring,
76 const char *config_dir,
80 const char *auth_path;
84 SVN_ERR(svn_auth__file_path(&auth_path, cred_kind, realmstring, config_dir,
89 SVN_ERR(svn_io_check_path(auth_path, &kind, pool));
90 if (kind == svn_node_file)
93 svn_string_t *stored_realm;
95 SVN_ERR_W(svn_stream_open_readonly(&stream, auth_path, pool, pool),
96 _("Unable to open auth file for reading"));
98 *hash = apr_hash_make(pool);
100 SVN_ERR_W(svn_hash_read2(*hash, stream, SVN_HASH_TERMINATOR, pool),
101 apr_psprintf(pool, _("Error parsing '%s'"),
102 svn_dirent_local_style(auth_path, pool)));
104 stored_realm = svn_hash_gets(*hash, SVN_CONFIG_REALMSTRING_KEY);
106 if (!stored_realm || strcmp(stored_realm->data, realmstring) != 0)
107 *hash = NULL; /* Hash collision, or somebody tampering with storage */
109 SVN_ERR(svn_stream_close(stream));
117 svn_config_write_auth_data(apr_hash_t *hash,
118 const char *cred_kind,
119 const char *realmstring,
120 const char *config_dir,
123 svn_stream_t *stream;
124 const char *auth_path, *tmp_path;
126 SVN_ERR(svn_auth__file_path(&auth_path, cred_kind, realmstring, config_dir,
129 return svn_error_create(SVN_ERR_NO_AUTH_FILE_PATH, NULL,
130 _("Unable to locate auth file"));
132 /* Add the realmstring to the hash, so programs (or users) can
133 verify exactly which set of credentials this file holds.
134 ### What if realmstring key is already in the hash? */
135 svn_hash_sets(hash, SVN_CONFIG_REALMSTRING_KEY,
136 svn_string_create(realmstring, pool));
138 SVN_ERR_W(svn_stream_open_unique(&stream, &tmp_path,
139 svn_dirent_dirname(auth_path, pool),
140 svn_io_file_del_on_pool_cleanup,
142 _("Unable to open auth file for writing"));
143 SVN_ERR_W(svn_hash_write2(hash, stream, SVN_HASH_TERMINATOR, pool),
144 apr_psprintf(pool, _("Error writing hash to '%s'"),
145 svn_dirent_local_style(auth_path, pool)));
146 SVN_ERR(svn_stream_close(stream));
147 SVN_ERR(svn_io_file_rename(tmp_path, auth_path, pool));
149 /* To be nice, remove the realmstring from the hash again, just in
150 case the caller wants their hash unchanged.
151 ### Should we also do this when a write error occurs? */
152 svn_hash_sets(hash, SVN_CONFIG_REALMSTRING_KEY, NULL);
159 svn_config_walk_auth_data(const char *config_dir,
160 svn_config_auth_walk_func_t walk_func,
162 apr_pool_t *scratch_pool)
165 apr_pool_t *iterpool;
166 svn_boolean_t finished = FALSE;
167 const char *cred_kinds[] =
169 SVN_AUTH_CRED_SIMPLE,
170 SVN_AUTH_CRED_USERNAME,
171 SVN_AUTH_CRED_SSL_CLIENT_CERT,
172 SVN_AUTH_CRED_SSL_CLIENT_CERT_PW,
173 SVN_AUTH_CRED_SSL_SERVER_TRUST,
177 iterpool = svn_pool_create(scratch_pool);
178 for (i = 0; cred_kinds[i]; i++)
180 const char *item_path;
181 const char *dir_path;
184 apr_pool_t *itempool;
185 apr_hash_index_t *hi;
187 svn_pool_clear(iterpool);
192 SVN_ERR(svn_auth__file_path(&item_path, cred_kinds[i], "!", config_dir,
195 dir_path = svn_dirent_dirname(item_path, iterpool);
197 err = svn_io_get_dirents3(&nodes, dir_path, TRUE, iterpool, iterpool);
200 if (!APR_STATUS_IS_ENOENT(err->apr_err)
201 && !SVN__APR_STATUS_IS_ENOTDIR(err->apr_err))
202 return svn_error_trace(err);
204 svn_error_clear(err);
208 itempool = svn_pool_create(iterpool);
209 for (hi = apr_hash_first(iterpool, nodes); hi; hi = apr_hash_next(hi))
211 svn_io_dirent2_t *dirent = apr_hash_this_val(hi);
212 svn_stream_t *stream;
213 apr_hash_t *creds_hash;
214 const svn_string_t *realm;
215 svn_boolean_t delete_file = FALSE;
220 if (dirent->kind != svn_node_file)
223 svn_pool_clear(itempool);
225 item_path = svn_dirent_join(dir_path, apr_hash_this_key(hi),
228 err = svn_stream_open_readonly(&stream, item_path,
232 /* Ignore this file. There are no credentials in it anyway */
233 svn_error_clear(err);
237 creds_hash = apr_hash_make(itempool);
238 err = svn_hash_read2(creds_hash, stream,
239 SVN_HASH_TERMINATOR, itempool);
240 err = svn_error_compose_create(err, svn_stream_close(stream));
243 /* Ignore this file. There are no credentials in it anyway */
244 svn_error_clear(err);
248 realm = svn_hash_gets(creds_hash, SVN_CONFIG_REALMSTRING_KEY);
250 continue; /* Not an auth file */
252 err = walk_func(&delete_file, walk_baton, cred_kinds[i],
253 realm->data, creds_hash, itempool);
254 if (err && err->apr_err == SVN_ERR_CEASE_INVOCATION)
256 svn_error_clear(err);
264 /* Delete the file on disk */
265 SVN_ERR(svn_io_remove_file2(item_path, TRUE, itempool));
270 svn_pool_destroy(iterpool);