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"
38 /* Helper for svn_config_{read|write}_auth_data. Return a path to a
39 file within ~/.subversion/auth/ that holds CRED_KIND credentials
40 within REALMSTRING. If no path is available *PATH will be set to
43 svn_auth__file_path(const char **path,
44 const char *cred_kind,
45 const char *realmstring,
46 const char *config_dir,
49 const char *authdir_path, *hexname;
50 svn_checksum_t *checksum;
52 /* Construct the path to the directory containing the creds files,
53 e.g. "~/.subversion/auth/svn.simple". The last component is
54 simply the cred_kind. */
55 SVN_ERR(svn_config_get_user_config_path(&authdir_path, config_dir,
56 SVN_CONFIG__AUTH_SUBDIR, pool));
59 authdir_path = svn_dirent_join(authdir_path, cred_kind, pool);
61 /* Construct the basename of the creds file. It's just the
62 realmstring converted into an md5 hex string. */
63 SVN_ERR(svn_checksum(&checksum, svn_checksum_md5, realmstring,
64 strlen(realmstring), pool));
65 hexname = svn_checksum_to_cstring(checksum, pool);
67 *path = svn_dirent_join(authdir_path, hexname, pool);
77 svn_config_read_auth_data(apr_hash_t **hash,
78 const char *cred_kind,
79 const char *realmstring,
80 const char *config_dir,
84 const char *auth_path;
88 SVN_ERR(svn_auth__file_path(&auth_path, cred_kind, realmstring, config_dir,
93 SVN_ERR(svn_io_check_path(auth_path, &kind, pool));
94 if (kind == svn_node_file)
97 svn_string_t *stored_realm;
99 SVN_ERR_W(svn_stream_open_readonly(&stream, auth_path, pool, pool),
100 _("Unable to open auth file for reading"));
102 *hash = apr_hash_make(pool);
104 SVN_ERR_W(svn_hash_read2(*hash, stream, SVN_HASH_TERMINATOR, pool),
105 apr_psprintf(pool, _("Error parsing '%s'"),
106 svn_dirent_local_style(auth_path, pool)));
108 stored_realm = svn_hash_gets(*hash, SVN_CONFIG_REALMSTRING_KEY);
110 if (!stored_realm || strcmp(stored_realm->data, realmstring) != 0)
111 *hash = NULL; /* Hash collision, or somebody tampering with storage */
113 SVN_ERR(svn_stream_close(stream));
121 svn_config_write_auth_data(apr_hash_t *hash,
122 const char *cred_kind,
123 const char *realmstring,
124 const char *config_dir,
127 apr_file_t *authfile = NULL;
128 svn_stream_t *stream;
129 const char *auth_path;
131 SVN_ERR(svn_auth__file_path(&auth_path, cred_kind, realmstring, config_dir,
134 return svn_error_create(SVN_ERR_NO_AUTH_FILE_PATH, NULL,
135 _("Unable to locate auth file"));
137 /* Add the realmstring to the hash, so programs (or users) can
138 verify exactly which set of credentials this file holds. */
139 svn_hash_sets(hash, SVN_CONFIG_REALMSTRING_KEY,
140 svn_string_create(realmstring, pool));
142 SVN_ERR_W(svn_io_file_open(&authfile, auth_path,
143 (APR_WRITE | APR_CREATE | APR_TRUNCATE
145 APR_OS_DEFAULT, pool),
146 _("Unable to open auth file for writing"));
148 stream = svn_stream_from_aprfile2(authfile, FALSE, pool);
149 SVN_ERR_W(svn_hash_write2(hash, stream, SVN_HASH_TERMINATOR, pool),
150 apr_psprintf(pool, _("Error writing hash to '%s'"),
151 svn_dirent_local_style(auth_path, pool)));
153 SVN_ERR(svn_stream_close(stream));
155 /* To be nice, remove the realmstring from the hash again, just in
156 case the caller wants their hash unchanged. */
157 svn_hash_sets(hash, SVN_CONFIG_REALMSTRING_KEY, NULL);
164 svn_config_walk_auth_data(const char *config_dir,
165 svn_config_auth_walk_func_t walk_func,
167 apr_pool_t *scratch_pool)
170 apr_pool_t *iterpool;
171 svn_boolean_t finished = FALSE;
172 const char *cred_kinds[] =
174 SVN_AUTH_CRED_SIMPLE,
175 SVN_AUTH_CRED_USERNAME,
176 SVN_AUTH_CRED_SSL_CLIENT_CERT,
177 SVN_AUTH_CRED_SSL_CLIENT_CERT_PW,
178 SVN_AUTH_CRED_SSL_SERVER_TRUST,
182 iterpool = svn_pool_create(scratch_pool);
183 for (i = 0; cred_kinds[i]; i++)
185 const char *item_path;
186 const char *dir_path;
189 apr_pool_t *itempool;
190 apr_hash_index_t *hi;
192 svn_pool_clear(iterpool);
197 SVN_ERR(svn_auth__file_path(&item_path, cred_kinds[i], "!", config_dir,
200 dir_path = svn_dirent_dirname(item_path, iterpool);
202 err = svn_io_get_dirents3(&nodes, dir_path, TRUE, iterpool, iterpool);
205 if (!APR_STATUS_IS_ENOENT(err->apr_err)
206 && !SVN__APR_STATUS_IS_ENOTDIR(err->apr_err))
207 return svn_error_trace(err);
209 svn_error_clear(err);
213 itempool = svn_pool_create(iterpool);
214 for (hi = apr_hash_first(iterpool, nodes); hi; hi = apr_hash_next(hi))
216 svn_io_dirent2_t *dirent = svn__apr_hash_index_val(hi);
217 svn_stream_t *stream;
218 apr_hash_t *creds_hash;
219 const svn_string_t *realm;
220 svn_boolean_t delete_file = FALSE;
225 if (dirent->kind != svn_node_file)
228 svn_pool_clear(itempool);
230 item_path = svn_dirent_join(dir_path, svn__apr_hash_index_key(hi),
233 err = svn_stream_open_readonly(&stream, item_path,
237 /* Ignore this file. There are no credentials in it anyway */
238 svn_error_clear(err);
242 creds_hash = apr_hash_make(itempool);
243 err = svn_hash_read2(creds_hash, stream,
244 SVN_HASH_TERMINATOR, itempool);
245 err = svn_error_compose_create(err, svn_stream_close(stream));
248 /* Ignore this file. There are no credentials in it anyway */
249 svn_error_clear(err);
253 realm = svn_hash_gets(creds_hash, SVN_CONFIG_REALMSTRING_KEY);
255 continue; /* Not an auth file */
257 err = walk_func(&delete_file, walk_baton, cred_kinds[i],
258 realm->data, creds_hash, itempool);
259 if (err && err->apr_err == SVN_ERR_CEASE_INVOCATION)
261 svn_error_clear(err);
269 /* Delete the file on disk */
270 SVN_ERR(svn_io_remove_file2(item_path, TRUE, itempool));
275 svn_pool_destroy(iterpool);