1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251
|
/*
Copyright 2024 Northern.tech AS
This file is part of CFEngine 3 - written and maintained by Northern.tech AS.
This program is free software; you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by the
Free Software Foundation; version 3.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
To the extent this program is licensed as part of the Enterprise
versions of CFEngine, the applicable Commercial Open Source License
(COSL) may apply to this file if you as a licensee so wish it. See
included file COSL.txt.
*/
#include <platform.h>
#include <communication.h>
#include <connection_info.h>
#include <stat_cache.h> /* Stat */
#include <alloc.h> /* xmalloc,... */
#include <logging.h> /* Log */
#include <misc_lib.h> /* ProgrammingError */
#include <buffer.h> /* Buffer */
#include <ip_address.h> /* IPAddress */
AgentConnection *NewAgentConn(const char *server, const char *port,
ConnectionFlags flags)
{
AgentConnection *conn = xcalloc(1, sizeof(AgentConnection));
conn->conn_info = ConnectionInfoNew();
conn->this_server = xstrdup(server);
conn->this_port = (port == NULL) ? NULL : xstrdup(port);
conn->flags = flags;
conn->encryption_type = 'c';
conn->authenticated = false;
return conn;
}
void DeleteAgentConn(AgentConnection *conn)
{
Stat *sp = conn->cache;
while (sp != NULL)
{
Stat *previous = sp;
sp = sp->next;
DestroyStatCache(previous);
}
ConnectionInfoDestroy(&conn->conn_info);
free(conn->this_server);
free(conn->this_port);
free(conn->session_key);
*conn = (AgentConnection) {0};
free(conn);
}
bool IsIPV6Address(char *name)
{
if (!name)
{
return false;
}
Buffer *buffer = BufferNewFrom(name, strlen(name));
if (!buffer)
{
return false;
}
IPAddress *ip_address = NULL;
bool is_ip = false;
is_ip = IPAddressIsIPAddress(buffer, &ip_address);
if (!is_ip)
{
BufferDestroy(buffer);
return false;
}
if (IPAddressType(ip_address) != IP_ADDRESS_TYPE_IPV6)
{
BufferDestroy(buffer);
IPAddressDestroy(&ip_address);
return false;
}
BufferDestroy(buffer);
IPAddressDestroy(&ip_address);
return true;
}
/*******************************************************************/
bool IsIPV4Address(char *name)
{
if (!name)
{
return false;
}
Buffer *buffer = BufferNewFrom(name, strlen(name));
if (!buffer)
{
return false;
}
IPAddress *ip_address = NULL;
bool is_ip = false;
is_ip = IPAddressIsIPAddress(buffer, &ip_address);
if (!is_ip)
{
BufferDestroy(buffer);
return false;
}
if (IPAddressType(ip_address) != IP_ADDRESS_TYPE_IPV4)
{
BufferDestroy(buffer);
IPAddressDestroy(&ip_address);
return false;
}
BufferDestroy(buffer);
IPAddressDestroy(&ip_address);
return true;
}
/*****************************************************************************/
/**
* @brief DNS lookup of hostname, store the address as string into dst of size
* dst_size.
* @return -1 in case of unresolvable hostname or other error.
*/
int Hostname2IPString(char *dst, const char *hostname, size_t dst_size)
{
int ret;
struct addrinfo *response = NULL, *ap;
struct addrinfo query = {
.ai_family = AF_UNSPEC,
.ai_socktype = SOCK_STREAM
};
if (dst_size < CF_MAX_IP_LEN)
{
ProgrammingError("Hostname2IPString got %zu, needs at least"
" %d length buffer for IPv6 portability!",
dst_size, CF_MAX_IP_LEN);
}
ret = getaddrinfo(hostname, NULL, &query, &response);
if (ret != 0)
{
Log(LOG_LEVEL_INFO,
"Unable to lookup hostname '%s' or cfengine service. (getaddrinfo: %s)",
hostname, gai_strerror(ret));
if (response != NULL)
{
freeaddrinfo(response);
}
return -1;
}
for (ap = response; ap != NULL; ap = ap->ai_next)
{
/* No lookup, just convert numeric IP to string. */
int ret2 = getnameinfo(ap->ai_addr, ap->ai_addrlen,
dst, dst_size, NULL, 0, NI_NUMERICHOST);
if (ret2 == 0)
{
freeaddrinfo(response);
return 0; /* Success */
}
}
assert(response != NULL); /* getaddrinfo() was successful */
freeaddrinfo(response);
Log(LOG_LEVEL_ERR,
"Hostname2IPString: ERROR even though getaddrinfo returned success!");
return -1;
}
/*****************************************************************************/
/**
* @brief Reverse DNS lookup of ipaddr, store the address as string into dst
* of size dst_size.
* @return -1 in case of unresolvable IP address or other error.
*/
int IPString2Hostname(char *dst, const char *ipaddr, size_t dst_size)
{
int ret;
struct addrinfo *response = NULL;
/* First convert ipaddr string to struct sockaddr, with no DNS query. */
struct addrinfo query = {
.ai_flags = AI_NUMERICHOST
};
ret = getaddrinfo(ipaddr, NULL, &query, &response);
if (ret != 0)
{
Log(LOG_LEVEL_ERR,
"Unable to convert IP address '%s'. (getaddrinfo: %s)",
ipaddr, gai_strerror(ret));
if (response != NULL)
{
freeaddrinfo(response);
}
return -1;
}
/* response should only have one reply, so no need to iterate over the
* response struct addrinfo. */
/* Reverse DNS lookup. NI_NAMEREQD forces an error if not resolvable. */
ret = getnameinfo(response->ai_addr, response->ai_addrlen,
dst, dst_size, NULL, 0, NI_NAMEREQD);
if (ret != 0)
{
Log(LOG_LEVEL_INFO,
"Couldn't reverse resolve '%s'. (getaddrinfo: %s)",
ipaddr, gai_strerror(ret));
freeaddrinfo(response);
return -1;
}
assert(response != NULL); /* getaddrinfo() was successful */
freeaddrinfo(response);
return 0; /* Success */
}
/*****************************************************************************/
unsigned short SocketFamily(int sd)
{
struct sockaddr_storage ss = {0};
socklen_t len = sizeof(ss);
if (getsockname(sd, (struct sockaddr *) &ss, &len) == -1)
{
Log(LOG_LEVEL_ERR, "Could not get socket family. (getsockname: %s)", GetErrorStr());
}
return ss.ss_family;
}
|