1/* 2 * Copyright (c) 2000-2001,2003-2004,2006 Apple Computer, Inc. All Rights Reserved. 3 * 4 * @APPLE_LICENSE_HEADER_START@ 5 * 6 * This file contains Original Code and/or Modifications of Original Code 7 * as defined in and that are subject to the Apple Public Source License 8 * Version 2.0 (the 'License'). You may not use this file except in 9 * compliance with the License. Please obtain a copy of the License at 10 * http://www.opensource.apple.com/apsl/ and read it before using this 11 * file. 12 * 13 * The Original Code and all software distributed under the License are 14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER 15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, 16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, 17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. 18 * Please see the License for the specific language governing rights and 19 * limitations under the License. 20 * 21 * @APPLE_LICENSE_HEADER_END@ 22 */ 23#include <security_cdsa_utilities/cssmdbname.h> 24#include <security_cdsa_utilities/cssmbridge.h> 25#include <security_utilities/utilities.h> 26 27CssmNetAddress::CssmNetAddress(CSSM_DB_RECORDTYPE inAddressType, const CssmData &inAddress) 28{ 29 AddressType = inAddressType; 30 Address.Length = inAddress.Length; 31 if (Address.Length > 0) 32 { 33 Address.Data = new uint8[Address.Length]; 34 memcpy (Address.Data, inAddress.Data, Address.Length); 35 } 36 else 37 Address.Data = NULL; 38} 39 40CssmNetAddress::CssmNetAddress(const CSSM_NET_ADDRESS &other) 41{ 42 AddressType = other.AddressType; 43 Address.Length = other.Address.Length; 44 if (Address.Length > 0) 45 { 46 Address.Data = new uint8[Address.Length]; 47 memcpy (Address.Data, other.Address.Data, Address.Length); 48 } 49 else 50 Address.Data = NULL; 51} 52 53CssmNetAddress::~CssmNetAddress() 54{ 55 if (Address.Length > 0) 56 delete Address.Data; 57} 58 59void DbName::CanonicalizeName() 60{ 61 if (mDbNameValid) 62 { 63 char* s = cached_realpath(mDbName.c_str(), NULL); 64 if (s != NULL) 65 { 66 mCanonicalName = s; 67 free(s); 68 } 69 else 70 { 71 // the most likely situation here is that the file doesn't exist. 72 // we will pull the path apart and try again. 73 74 // search backward for the delimiter 75 ptrdiff_t n = mDbName.length() - 1; 76 77 // all subpaths must be tested, because there may be more than just 78 // the file name that doesn't exist. 79 while (n > 0) 80 { 81 while (n > 0 && mDbName[n] != '/') // if the delimiter is 0, we would never 82 // have gotten here in the first place 83 { 84 n -= 1; 85 } 86 87 if (n > 0) 88 { 89 string tmpPath = mDbName.substr(0, n); 90 s = cached_realpath(tmpPath.c_str(), NULL); 91 if (s != NULL) 92 { 93 mCanonicalName = s; 94 free(s); 95 mCanonicalName += mDbName.substr(n, mDbName.length() - n); 96 return; 97 } 98 } 99 100 n -= 1; 101 } 102 103 // if we get here, all other paths have failed. Just reuse the original string. 104 mCanonicalName = mDbName; 105 } 106 } 107} 108 109 110 111DbName::DbName(const char *inDbName, const CSSM_NET_ADDRESS *inDbLocation) 112 : mDbName(inDbName ? inDbName : ""), mDbNameValid(inDbName), mDbLocation(NULL) 113{ 114 if (inDbLocation) 115 { 116 mDbLocation = new CssmNetAddress(*inDbLocation); 117 } 118 119 CanonicalizeName(); 120} 121 122DbName::DbName(const DbName &other) 123 : mDbName(other.mDbName), mDbNameValid(other.mDbNameValid), mDbLocation(NULL) 124{ 125 if (other.mDbLocation) 126 { 127 mDbLocation = new CssmNetAddress(*other.mDbLocation); 128 } 129 130 CanonicalizeName(); 131} 132 133DbName & 134DbName::operator =(const DbName &other) 135{ 136 mDbName = other.mDbName; 137 mDbNameValid = other.mDbNameValid; 138 if (other.mDbLocation) 139 { 140 mDbLocation = new CssmNetAddress(*other.mDbLocation); 141 } 142 143 return *this; 144} 145 146DbName::~DbName() 147{ 148 delete mDbLocation; 149} 150