Browse Source

+ add src/strlfuncs.[ch]

* use strlcpy() instead strncpy()
master
Alex 'AdUser' Z 9 years ago
parent
commit
48358c3cec
  1. 6
      src/CMakeLists.txt
  2. 2
      src/common.h
  3. 20
      src/config.c
  4. 2
      src/ipaddr.c
  5. 8
      src/jail.c
  6. 2
      src/logfile.c
  7. 9
      src/main.c
  8. 83
      src/strlfuncs.c
  9. 25
      src/strlfuncs.h
  10. 8
      t/CMakeLists.txt

6
src/CMakeLists.txt

@ -1,15 +1,15 @@
set(CMAKE_INCLUDE_CURRENT_DIR ON) set(CMAKE_INCLUDE_CURRENT_DIR ON)
set(SOURCES "main.c" "logfile.c" "log.c" "matches.c" "ipaddr.c" "filelist.c" "filter.c" "config.c" "jail.c" "backend.c") set(SOURCES "main.c" "strlfuncs.c" "logfile.c" "log.c" "matches.c" "ipaddr.c" "filelist.c" "filter.c" "config.c" "jail.c" "backend.c")
add_executable("f2b" ${SOURCES}) add_executable("f2b" ${SOURCES})
target_link_libraries(f2b "dl") target_link_libraries(f2b "dl")
set(SOURCES "backend-test.c" "log.c" "config.c" "backend.c") set(SOURCES "strlfuncs.c" "backend-test.c" "log.c" "config.c" "backend.c")
add_executable("backend-test" ${SOURCES}) add_executable("backend-test" ${SOURCES})
target_link_libraries("backend-test" "dl") target_link_libraries("backend-test" "dl")
set(SOURCES "filter-test.c" "log.c" "config.c" "filter.c") set(SOURCES "strlfuncs.c" "filter-test.c" "log.c" "config.c" "filter.c")
add_executable("filter-test" ${SOURCES}) add_executable("filter-test" ${SOURCES})
target_link_libraries("filter-test" "dl") target_link_libraries("filter-test" "dl")

2
src/common.h

@ -24,6 +24,8 @@
#include <time.h> #include <time.h>
#include <unistd.h> #include <unistd.h>
#include "strlfuncs.h"
#define UNUSED(x) (void)(x) #define UNUSED(x) (void)(x)
#define FREE(x) free(x), x = NULL #define FREE(x) free(x), x = NULL

20
src/config.c

@ -17,8 +17,7 @@ f2b_config_param_create(const char *src) {
char *p, *key, *value; char *p, *key, *value;
size_t len; size_t len;
strncpy(line, src, sizeof(line)); strlcpy(line, src, sizeof(line));
line[CONFIG_LINE_MAX - 1] = '\0';
/* strip spaces before key */ /* strip spaces before key */
key = line; key = line;
@ -61,10 +60,8 @@ f2b_config_param_create(const char *src) {
return NULL; return NULL;
if ((param = calloc(1, sizeof(f2b_config_param_t))) != NULL) { if ((param = calloc(1, sizeof(f2b_config_param_t))) != NULL) {
strncpy(param->name, key, sizeof(param->name)); strlcpy(param->name, key, sizeof(param->name));
strncpy(param->value, value, sizeof(param->value)); strlcpy(param->value, value, sizeof(param->value));
param->name [CONFIG_KEY_MAX - 1] = '\0';
param->value[CONFIG_VAL_MAX - 1] = '\0';
return param; return param;
} }
@ -92,7 +89,7 @@ f2b_config_param_append(f2b_config_param_t *list, f2b_config_param_t *param, boo
if (replace && (p = f2b_config_param_find(list, param->name)) != NULL) { if (replace && (p = f2b_config_param_find(list, param->name)) != NULL) {
/* found param with same name */ /* found param with same name */
strncpy(p->value, param->value, sizeof(p->value)); strlcpy(p->value, param->value, sizeof(p->value));
free(param); free(param);
return list; return list;
} }
@ -114,8 +111,7 @@ f2b_config_section_create(const char *src) {
assert(*src == '['); assert(*src == '[');
src++; src++;
strncpy(line, src, sizeof(line)); strlcpy(line, src, sizeof(line));
line[CONFIG_LINE_MAX - 1] = '\0';
if ((end = strchr(line, ']')) == NULL) if ((end = strchr(line, ']')) == NULL)
return NULL; return NULL;
@ -139,21 +135,21 @@ f2b_config_section_create(const char *src) {
name = "backend:"; name = "backend:";
if (strncmp(line, name, strlen(name)) == 0) { if (strncmp(line, name, strlen(name)) == 0) {
section->type = t_backend; section->type = t_backend;
strncpy(section->name, line + strlen(name), sizeof(section->name)); strlcpy(section->name, line + strlen(name), sizeof(section->name));
return section; return section;
} }
name = "filter:"; name = "filter:";
if (strncmp(line, name, strlen(name)) == 0) { if (strncmp(line, name, strlen(name)) == 0) {
section->type = t_filter; section->type = t_filter;
strncpy(section->name, line + strlen(name), sizeof(section->name)); strlcpy(section->name, line + strlen(name), sizeof(section->name));
return section; return section;
} }
name = "jail:"; name = "jail:";
if (strncmp(line, name, strlen(name)) == 0) { if (strncmp(line, name, strlen(name)) == 0) {
section->type = t_jail; section->type = t_jail;
strncpy(section->name, line + strlen(name), sizeof(section->name)); strlcpy(section->name, line + strlen(name), sizeof(section->name));
return section; return section;
} }

2
src/ipaddr.c

@ -15,7 +15,7 @@ f2b_ipaddr_create(const char *addr, size_t matches) {
assert(matches != 0); assert(matches != 0);
if ((a = calloc(1, sizeof(f2b_ipaddr_t))) != NULL) { if ((a = calloc(1, sizeof(f2b_ipaddr_t))) != NULL) {
strncpy(a->text, addr, sizeof(a->text)); strlcpy(a->text, addr, sizeof(a->text));
if (strchr(addr, ':') == NULL) { if (strchr(addr, ':') == NULL) {
a->type = AF_INET; a->type = AF_INET;
if (inet_pton(a->type, addr, &a->binary.v4) < 1) if (inet_pton(a->type, addr, &a->binary.v4) < 1)

8
src/jail.c

@ -26,7 +26,7 @@ f2b_jail_parse_compound_value(const char *value, char *name, char *init) {
if ((p = strchr(value, ':')) == NULL) { if ((p = strchr(value, ':')) == NULL) {
/* param = name */ /* param = name */
strncpy(name, value, CONFIG_KEY_MAX); strlcpy(name, value, CONFIG_KEY_MAX);
return; return;
} }
@ -37,8 +37,8 @@ f2b_jail_parse_compound_value(const char *value, char *name, char *init) {
return; return;
} }
strncpy(name, value, len); strlcpy(name, value, len);
strncpy(init, (p + 1), CONFIG_VAL_MAX); strlcpy(init, (p + 1), CONFIG_VAL_MAX);
return; return;
} }
@ -175,7 +175,7 @@ f2b_jail_create(f2b_config_section_t *section) {
} }
memcpy(jail, &defaults, sizeof(f2b_jail_t)); memcpy(jail, &defaults, sizeof(f2b_jail_t));
strncpy(jail->name, section->name, sizeof(jail->name)); strlcpy(jail->name, section->name, sizeof(jail->name));
f2b_jail_apply_config(jail, section); f2b_jail_apply_config(jail, section);
return jail; return jail;

2
src/logfile.c

@ -22,7 +22,7 @@ f2b_logfile_open(f2b_logfile_t *file, const char *filename) {
if (!(S_ISREG(st.st_mode) || S_ISFIFO(st.st_mode))) if (!(S_ISREG(st.st_mode) || S_ISFIFO(st.st_mode)))
return false; return false;
strncpy(file->path, filename, sizeof(file->path)); strlcpy(file->path, filename, sizeof(file->path));
memcpy(&file->st, &st, sizeof(st)); memcpy(&file->st, &st, sizeof(st));
if ((file->fd = fopen(filename, "r")) == NULL) if ((file->fd = fopen(filename, "r")) == NULL)

9
src/main.c

@ -83,7 +83,7 @@ update_opts_from_config(f2b_config_section_t *section) {
} }
if ((pa = f2b_config_param_find(section->param, "pidfile")) != NULL) { if ((pa = f2b_config_param_find(section->param, "pidfile")) != NULL) {
strncpy(opts.pidfile_path, pa->value, sizeof(opts.pidfile_path)); strlcpy(opts.pidfile_path, pa->value, sizeof(opts.pidfile_path));
opts.pidfile_path[sizeof(opts.pidfile_path) - 1] = '\0'; opts.pidfile_path[sizeof(opts.pidfile_path) - 1] = '\0';
} }
@ -98,9 +98,7 @@ update_opts_from_config(f2b_config_section_t *section) {
f2b_log_to_stderr(); f2b_log_to_stderr();
} else if (strcmp(pa->value, "file") == 0) { } else if (strcmp(pa->value, "file") == 0) {
if (pb && *pb->value != '\0') { if (pb && *pb->value != '\0') {
size_t len = sizeof(opts.logfile_path); strlcpy(opts.logfile_path, pb->value, sizeof(opts.logfile_path));
strncpy(opts.logfile_path, pb->value, len - 1);
opts.logfile_path[len - 1] = '\0';
f2b_log_to_file(opts.logfile_path); f2b_log_to_file(opts.logfile_path);
} else { } else {
f2b_log_msg(log_warn, "you must set 'logfile' option with 'logdest = file'"); f2b_log_msg(log_warn, "you must set 'logfile' option with 'logdest = file'");
@ -125,8 +123,7 @@ int main(int argc, char *argv[]) {
while ((opt = getopt(argc, argv, "c:dh")) != -1) { while ((opt = getopt(argc, argv, "c:dh")) != -1) {
switch (opt) { switch (opt) {
case 'c': case 'c':
strncpy(opts.config_path, optarg, sizeof(opts.config_path)); strlcpy(opts.config_path, optarg, sizeof(opts.config_path));
opts.config_path[sizeof(opts.config_path) - 1] = '\0';
break; break;
case 'd': case 'd':
opts.daemon = true; opts.daemon = true;

83
src/strlfuncs.c

@ -0,0 +1,83 @@
/* $OpenBSD: strlcpy.c,v 1.13 2015/08/31 02:53:57 guenther Exp $ */
/*
* Copyright (c) 1998, 2015 Todd C. Miller <Todd.Miller@courtesan.com>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <sys/types.h>
#include <string.h>
/*
* Copy string src to buffer dst of size dsize. At most dsize-1
* chars will be copied. Always NUL terminates (unless dsize == 0).
* Returns strlen(src); if retval >= dsize, truncation occurred.
*/
size_t
strlcpy(char *dst, const char *src, size_t dsize) {
const char *osrc = src;
size_t nleft = dsize;
/* Copy as many bytes as will fit. */
if (nleft != 0) {
while (--nleft != 0) {
if ((*dst++ = *src++) == '\0')
break;
}
}
/* Not enough room in dst, add NUL and traverse rest of src. */
if (nleft == 0) {
if (dsize != 0)
*dst = '\0'; /* NUL-terminate dst */
while (*src++)
;
}
return(src - osrc - 1); /* count does not include NUL */
}
/*
* Appends src to string dst of size dsize (unlike strncat, dsize is the
* full size of dst, not space left). At most dsize-1 characters
* will be copied. Always NUL terminates (unless dsize <= strlen(dst)).
* Returns strlen(src) + MIN(dsize, strlen(initial dst)).
* If retval >= dsize, truncation occurred.
*/
size_t
strlcat(char *dst, const char *src, size_t dsize) {
const char *odst = dst;
const char *osrc = src;
size_t n = dsize;
size_t dlen;
/* Find the end of dst and adjust bytes left but don't go past end. */
while (n-- != 0 && *dst != '\0')
dst++;
dlen = dst - odst;
n = dsize - dlen;
if (n-- == 0)
return(dlen + strlen(src));
while (*src != '\0') {
if (n != 0) {
*dst++ = *src;
n--;
}
src++;
}
*dst = '\0';
return(dlen + (src - osrc)); /* count does not include NUL */
}

25
src/strlfuncs.h

@ -0,0 +1,25 @@
#ifndef HAS_STRLFUNCS_
#define HAS_STRLFUNCS_
/* $OpenBSD: strlcpy.c,v 1.13 2015/08/31 02:53:57 guenther Exp $ */
/*
* Copyright (c) 1998, 2015 Todd C. Miller <Todd.Miller@courtesan.com>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
size_t strlcat(char *dst, const char *src, size_t dsize);
size_t strlcpy(char *dst, const char *src, size_t dsize);
#endif /* HAS_STRLFUNCS_ */

8
t/CMakeLists.txt

@ -2,10 +2,10 @@ enable_testing()
set(SRC_DIR "../src") set(SRC_DIR "../src")
add_executable("t_logfile" "t_logfile.c" "${SRC_DIR}/logfile.c") add_executable("t_logfile" "t_logfile.c" "${SRC_DIR}/strlfuncs.c" "${SRC_DIR}/logfile.c")
add_executable("t_matches" "t_matches.c" "${SRC_DIR}/matches.c") add_executable("t_matches" "t_matches.c" "${SRC_DIR}/strlfuncs.c" "${SRC_DIR}/matches.c")
add_executable("t_ipaddr" "t_ipaddr.c" "${SRC_DIR}/ipaddr.c" "${SRC_DIR}/matches.c") add_executable("t_ipaddr" "t_ipaddr.c" "${SRC_DIR}/strlfuncs.c" "${SRC_DIR}/matches.c" "${SRC_DIR}/ipaddr.c")
add_executable("t_config_param" "t_config_param.c" "${SRC_DIR}/config.c" "${SRC_DIR}/log.c") add_executable("t_config_param" "t_config_param.c" "${SRC_DIR}/strlfuncs.c" "${SRC_DIR}/config.c" "${SRC_DIR}/log.c")
add_test("tests/f2b_logfile_*" "t_logfile") add_test("tests/f2b_logfile_*" "t_logfile")
add_test("tests/f2b_matches_*" "t_matches") add_test("tests/f2b_matches_*" "t_matches")

Loading…
Cancel
Save