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
|
/**
* \file server/utils.c
*
* \brief General/Generic functions for the fwknop server.
*/
/* 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 "fwknopd_common.h"
#include "utils.h"
#include "log_msg.h"
#include "replay_cache.h"
#include "config_init.h"
#include "fw_util.h"
#include "cmd_cycle.h"
/* Basic directory/binary checks (stat() and whether the path is actually
* a directory or an executable).
*/
static int
is_valid_path(const char *path, const int file_type)
{
if(strnlen(path, MAX_PATH_LEN) == MAX_PATH_LEN)
{
log_msg(LOG_ERR, "[-] Provided path is too long");
return(0);
}
#if HAVE_STAT || HAVE_LSTAT
struct stat st;
/* If we are unable to stat the given path, then return with error.
*/
#if HAVE_LSTAT /* prefer lstat() to stat() */
if(lstat(path, &st) != 0)
{
log_msg(LOG_ERR, "[-] unable to lstat() path: %s: %s",
path, strerror(errno));
return(0);
}
#else
if(stat(path, &st) != 0)
{
log_msg(LOG_ERR, "[-] unable to stat() path: %s: %s",
path, strerror(errno));
return(0);
}
#endif
if(file_type == IS_DIR)
{
if(!S_ISDIR(st.st_mode))
return(0);
}
else if(file_type == IS_EXE)
{
if(!S_ISREG(st.st_mode) || ! (st.st_mode & S_IXUSR))
return(0);
}
else if(file_type == IS_FILE)
{
if(!S_ISREG(st.st_mode))
return(0);
}
else
return(0);
#endif /* HAVE_STAT || HAVE_LSTAT */
return(1);
}
int
is_valid_dir(const char *path)
{
return is_valid_path(path, IS_DIR);
}
int
is_valid_exe(const char *path)
{
return is_valid_path(path, IS_EXE);
}
int
is_valid_file(const char *path)
{
return is_valid_path(path, IS_FILE);
}
int
verify_file_perms_ownership(const char *file, int fd)
{
#if HAVE_FSTAT && HAVE_STAT
struct stat st;
/* Every file that fwknopd 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_WARNING,
"[-] file: %s is not a regular file or symbolic link.",
file
);
return 0;
}
if((st.st_mode & (S_IRWXU|S_IRWXG|S_IRWXO)) != (S_IRUSR|S_IWUSR))
{
log_msg(LOG_WARNING,
"[-] 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_WARNING, "[-] 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_ERR, "[-] stat() against file: %s returned: %s",
file, strerror(errno));
return 0;
}
}
#endif
return 1;
}
void
truncate_partial_line(char *str)
{
int i, have_newline=0;
if(str != NULL && str[0] != 0x0)
{
for (i=0; i < strlen(str); i++)
{
if(str[i] == 0x0a)
{
have_newline = 1;
break;
}
}
/* Don't zero out any data unless there is at least
* one newline
*/
if(have_newline)
{
for (i=strlen(str)-1; i > 0; i--)
{
if(str[i] == 0x0a)
break;
str[i] = 0x0;
}
}
}
return;
}
/* Simple test to see if a string only contains digits
*/
int
is_digits(const char * const str)
{
int i;
if (str != NULL && str[0] != 0x0)
{
for (i=0; i<strlen(str); i++)
{
if(!isdigit((int)(unsigned char)str[i]))
return 0;
}
}
return 1;
}
void
clean_exit(fko_srv_options_t *opts, unsigned int fw_cleanup_flag, unsigned int exit_status)
{
#if HAVE_LIBFIU
if(opts->config[CONF_FAULT_INJECTION_TAG] != NULL)
{
fiu_disable(opts->config[CONF_FAULT_INJECTION_TAG]);
}
#endif
if(!opts->test && opts->enable_fw && (fw_cleanup_flag == FW_CLEANUP))
fw_cleanup(opts);
#if USE_FILE_CACHE
free_replay_list(opts);
#endif
free_logging();
free_cmd_cycle_list(opts);
free_configs(opts);
exit(exit_status);
}
/***EOF***/
|