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 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290
|
/**
* \file client/utils.c
*
* \brief General/Generic functions for the fwknop client.
*/
/* Fwknop is developed primarily by the people listed in the file 'AUTHORS'.
* Copyright (C) 2009-2015 fwknop developers and contributors. For a full
* list of contributors, see the file 'CREDITS'.
*
* License (GNU General Public License):
*
* 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; either version 2
* of the License, or (at your option) any later version.
*
* 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
*
*****************************************************************************
*/
#include "common.h"
#include "fwknop_common.h"
#include "utils.h"
#ifndef WIN32
#include <arpa/inet.h>
#endif
static void *get_in_addr(struct sockaddr *sa);
/**
* Structure to handle a protocol string and its associated integer value
*/
typedef struct fko_protocol
{
const char str[PROTOCOL_BUFSIZE]; /*!< String which represents a protocol value for the FKO library */
int val; /*!< Value of the protocol according to the FKO library */
} fko_protocol_t;
static fko_protocol_t fko_protocol_array[] =
{
{ "udpraw", FKO_PROTO_UDP_RAW },
{ "udp", FKO_PROTO_UDP },
{ "tcpraw", FKO_PROTO_TCP_RAW },
{ "tcp", FKO_PROTO_TCP },
{ "icmp", FKO_PROTO_ICMP },
{ "http", FKO_PROTO_HTTP }
};
int
verify_file_perms_ownership(const char *file, int fd)
{
int res = 1;
#if HAVE_FSTAT && HAVE_STAT
struct stat st;
/* Every file that the fwknop client deals with should be owned
* by the user and permissions set to 600 (user read/write)
*/
if((fd >= 0 && fstat(fd, &st) == 0) || stat(file, &st) == 0)
{
/* Make sure it is a regular file
*/
if(S_ISREG(st.st_mode) != 1 && S_ISLNK(st.st_mode) != 1)
{
log_msg(LOG_VERBOSITY_ERROR,
"[-] file: %s is not a regular file or symbolic link.",
file
);
/* when we start in enforcing this instead of just warning
* the user
res = 0;
*/
}
if((st.st_mode & (S_IRWXU|S_IRWXG|S_IRWXO)) != (S_IRUSR|S_IWUSR))
{
log_msg(LOG_VERBOSITY_ERROR,
"[-] file: %s permissions should only be user read/write (0600, -rw-------)",
file
);
/* when we start in enforcing this instead of just warning
* the user
res = 0;
*/
}
if(st.st_uid != getuid())
{
log_msg(LOG_VERBOSITY_ERROR, "[-] file: %s not owned by current effective user id",
file);
/* when we start in enforcing this instead of just warning
* the user
res = 0;
*/
}
}
else
{
/* if the path doesn't exist, just return, but otherwise something
* went wrong
*/
if(errno != ENOENT)
{
log_msg(LOG_VERBOSITY_ERROR, "[-] stat() against file: %s returned: %s",
file, strerror(errno));
res = 0;
}
}
#endif
return res;
}
/**
* @brief Grab the sin address from the sockaddr structure.
*
* This function returns the sin address as a sockaddr_in or sockaddr_in6
* structure according to the family set (ipv4 or ipv6) in the sockaddr
* structure.
*
* @param sa sockaddr strcuture
*
* @return the sin addr if the sa family is AF_INET or the sin6_addr otherwise.
*/
static void *
get_in_addr(struct sockaddr *sa)
{
if (sa->sa_family == AF_INET)
{
return &(((struct sockaddr_in*)sa)->sin_addr);
}
else
{
return &(((struct sockaddr_in6*)sa)->sin6_addr);
}
}
/**
* @brief Resolve a domain name as an IP address.
*
* @param dns_str Name of the host to resolve.
* @param hints Hints to reduce the number of result from getaddrinfo()
* @param ip_str String where to store the resolve ip address
* @param ip_bufsize Number of bytes available in the ip_str buffer
* @param opts Client command line options
*
* @return 0 if successful, 1 if an error occurred.
*/
int
resolve_dst_addr(const char *dns_str, struct addrinfo *hints,
char *ip_str, size_t ip_bufsize, fko_cli_options_t *opts)
{
int error; /* Function error return code */
struct addrinfo *result; /* Result of getaddrinfo() */
struct addrinfo *rp; /* Element of the linked list returned by getaddrinfo() */
#if WIN32 && WINVER <= 0x0600
struct sockaddr_in *in;
char *win_ip;
#else
struct sockaddr_in *sai_remote; /* Remote host information as a sockaddr_in structure */
#endif
/* Try to resolve the host name */
error = getaddrinfo(dns_str, NULL, hints, &result);
if (error != 0)
fprintf(stderr, "resolve_dst_addr() : %s\n", gai_strerror(error));
else
{
error = 1;
/* Go through the linked list of addrinfo structures */
for (rp = result; rp != NULL; rp = rp->ai_next)
{
/* Apply --server-resolve-ipv4 criteria
*/
if(opts->spa_server_resolve_ipv4)
{
if(rp->ai_family != AF_INET)
{
log_msg(LOG_VERBOSITY_DEBUG, "Non-IPv4 resolution");
continue;
}
}
memset(ip_str, 0, ip_bufsize);
#if WIN32 && WINVER <= 0x0600
/* On older Windows systems (anything before Vista?),
* we use inet_ntoa for now.
*/
in = (struct sockaddr_in*)(rp->ai_addr);
win_ip = inet_ntoa(in->sin_addr);
if (win_ip != NULL && (strlcpy(ip_str, win_ip, ip_bufsize) > 0))
#else
sai_remote = (struct sockaddr_in *)get_in_addr((struct sockaddr *)(rp->ai_addr));
if (inet_ntop(rp->ai_family, sai_remote, ip_str, ip_bufsize) != NULL)
#endif
{
error = 0;
break;
}
else
log_msg(LOG_VERBOSITY_ERROR, "resolve_dst_addr() : inet_ntop (%d) - %s",
errno, strerror(errno));
}
/* Free our result from getaddrinfo() */
freeaddrinfo(result);
}
return error;
}
/**
* @brief Return a protocol string according to a protocol integer value
*
* This function checks if the protocol integer is valid, and write the protocol
* string associated.
*
* @param proto protocol inetger value (UDP_RAW, UDP, TCPRAW...)
* @param proto_str Buffer to write the protocol string
* @param proto_size size of the protocol string buffer
*
* @return -1 if the protocol integer value is not supported, 0 otherwise
*/
short
proto_inttostr(int proto, char *proto_str, size_t proto_size)
{
short proto_error = -1;
unsigned char ndx_proto; /* Index for the fko_protocol_t structure */
/* Initialize the protocol string */
memset(proto_str, 0, proto_size);
/* Look into the fko_protocol_array to find out the right protocol */
for (ndx_proto = 0 ; ndx_proto < ARRAY_SIZE(fko_protocol_array) ; ndx_proto++)
{
/* If the protocol matches, grab it */
if (fko_protocol_array[ndx_proto].val == proto)
{
strlcpy(proto_str, fko_protocol_array[ndx_proto].str, proto_size);
proto_error = 0;
break;
}
}
return proto_error;
}
/**
* @brief Convert a protocol string to its integer value.
*
* @param pr_str Protocol string (UDP_RAW, UDP, TCPRAW...)
*
* @return -1 if the protocol string is not supported, otherwise the protocol value
*/
short
proto_strtoint(const char *pr_str)
{
unsigned char ndx_proto; /* Index for the fko_protocol_t structure */
int proto_int = -1; /* Protocol integer value */
/* Look into the fko_protocol_array to find out the right protocol */
for (ndx_proto = 0 ; ndx_proto < ARRAY_SIZE(fko_protocol_array) ; ndx_proto++)
{
/* If the protocol matches, grab it */
if (strcasecmp(pr_str, fko_protocol_array[ndx_proto].str) == 0)
{
proto_int = fko_protocol_array[ndx_proto].val;
break;
}
}
return proto_int;
}
/***EOF***/
|