config_auth.c revision 362181
1/* 2 * config_auth.c : authentication files in the user config area 3 * 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 12 * 13 * http://www.apache.org/licenses/LICENSE-2.0 14 * 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 20 * under the License. 21 * ==================================================================== 22 */ 23 24 25 26#include "svn_dirent_uri.h" 27#include "svn_hash.h" 28#include "svn_io.h" 29#include "svn_pools.h" 30#include "config_impl.h" 31 32#include "auth.h" 33 34#include "svn_private_config.h" 35 36#include "private/svn_auth_private.h" 37 38svn_error_t * 39svn_auth__file_path(const char **path, 40 const char *cred_kind, 41 const char *realmstring, 42 const char *config_dir, 43 apr_pool_t *pool) 44{ 45 const char *authdir_path, *hexname; 46 svn_checksum_t *checksum; 47 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)); 53 if (authdir_path) 54 { 55 authdir_path = svn_dirent_join(authdir_path, cred_kind, pool); 56 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); 62 63 *path = svn_dirent_join(authdir_path, hexname, pool); 64 } 65 else 66 *path = NULL; 67 68 return SVN_NO_ERROR; 69} 70 71 72svn_error_t * 73svn_config_read_auth_data(apr_hash_t **hash, 74 const char *cred_kind, 75 const char *realmstring, 76 const char *config_dir, 77 apr_pool_t *pool) 78{ 79 svn_node_kind_t kind; 80 const char *auth_path; 81 82 *hash = NULL; 83 84 SVN_ERR(svn_auth__file_path(&auth_path, cred_kind, realmstring, config_dir, 85 pool)); 86 if (! auth_path) 87 return SVN_NO_ERROR; 88 89 SVN_ERR(svn_io_check_path(auth_path, &kind, pool)); 90 if (kind == svn_node_file) 91 { 92 svn_stream_t *stream; 93 svn_string_t *stored_realm; 94 95 SVN_ERR_W(svn_stream_open_readonly(&stream, auth_path, pool, pool), 96 _("Unable to open auth file for reading")); 97 98 *hash = apr_hash_make(pool); 99 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))); 103 104 stored_realm = svn_hash_gets(*hash, SVN_CONFIG_REALMSTRING_KEY); 105 106 if (!stored_realm || strcmp(stored_realm->data, realmstring) != 0) 107 *hash = NULL; /* Hash collision, or somebody tampering with storage */ 108 109 SVN_ERR(svn_stream_close(stream)); 110 } 111 112 return SVN_NO_ERROR; 113} 114 115 116svn_error_t * 117svn_config_write_auth_data(apr_hash_t *hash, 118 const char *cred_kind, 119 const char *realmstring, 120 const char *config_dir, 121 apr_pool_t *pool) 122{ 123 svn_stream_t *stream; 124 const char *auth_path, *tmp_path; 125 126 SVN_ERR(svn_auth__file_path(&auth_path, cred_kind, realmstring, config_dir, 127 pool)); 128 if (! auth_path) 129 return svn_error_create(SVN_ERR_NO_AUTH_FILE_PATH, NULL, 130 _("Unable to locate auth file")); 131 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)); 137 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, 141 pool, pool), 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_rename2(tmp_path, auth_path, FALSE, pool)); 148 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); 153 154 return SVN_NO_ERROR; 155} 156 157 158svn_error_t * 159svn_config_walk_auth_data(const char *config_dir, 160 svn_config_auth_walk_func_t walk_func, 161 void *walk_baton, 162 apr_pool_t *scratch_pool) 163{ 164 int i; 165 apr_pool_t *iterpool; 166 svn_boolean_t finished = FALSE; 167 const char *cred_kinds[] = 168 { 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, 174 NULL 175 }; 176 177 iterpool = svn_pool_create(scratch_pool); 178 for (i = 0; cred_kinds[i]; i++) 179 { 180 const char *item_path; 181 const char *dir_path; 182 apr_hash_t *nodes; 183 svn_error_t *err; 184 apr_pool_t *itempool; 185 apr_hash_index_t *hi; 186 187 svn_pool_clear(iterpool); 188 189 if (finished) 190 break; 191 192 SVN_ERR(svn_auth__file_path(&item_path, cred_kinds[i], "!", config_dir, 193 iterpool)); 194 195 dir_path = svn_dirent_dirname(item_path, iterpool); 196 197 err = svn_io_get_dirents3(&nodes, dir_path, TRUE, iterpool, iterpool); 198 if (err) 199 { 200 if (!APR_STATUS_IS_ENOENT(err->apr_err) 201 && !SVN__APR_STATUS_IS_ENOTDIR(err->apr_err)) 202 return svn_error_trace(err); 203 204 svn_error_clear(err); 205 continue; 206 } 207 208 itempool = svn_pool_create(iterpool); 209 for (hi = apr_hash_first(iterpool, nodes); hi; hi = apr_hash_next(hi)) 210 { 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; 216 217 if (finished) 218 break; 219 220 if (dirent->kind != svn_node_file) 221 continue; 222 223 svn_pool_clear(itempool); 224 225 item_path = svn_dirent_join(dir_path, apr_hash_this_key(hi), 226 itempool); 227 228 err = svn_stream_open_readonly(&stream, item_path, 229 itempool, itempool); 230 if (err) 231 { 232 /* Ignore this file. There are no credentials in it anyway */ 233 svn_error_clear(err); 234 continue; 235 } 236 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)); 241 if (err) 242 { 243 /* Ignore this file. There are no credentials in it anyway */ 244 svn_error_clear(err); 245 continue; 246 } 247 248 realm = svn_hash_gets(creds_hash, SVN_CONFIG_REALMSTRING_KEY); 249 if (! realm) 250 continue; /* Not an auth file */ 251 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) 255 { 256 svn_error_clear(err); 257 err = SVN_NO_ERROR; 258 finished = TRUE; 259 } 260 SVN_ERR(err); 261 262 if (delete_file) 263 { 264 /* Delete the file on disk */ 265 SVN_ERR(svn_io_remove_file2(item_path, TRUE, itempool)); 266 } 267 } 268 } 269 270 svn_pool_destroy(iterpool); 271 return SVN_NO_ERROR; 272} 273