PERFORCE change 136117 for review
Aaron Meihm
alm at FreeBSD.org
Sun Feb 24 19:32:55 UTC 2008
http://perforce.freebsd.org/chv.cgi?CH=136117
Change 136117 by alm at alm_praetorian on 2008/02/24 19:32:22
Split things up a bit.
Affected files ...
.. //depot/projects/trustedbsd/netauditd/Makefile#3 edit
.. //depot/projects/trustedbsd/netauditd/component.c#1 add
.. //depot/projects/trustedbsd/netauditd/conf.c#1 add
.. //depot/projects/trustedbsd/netauditd/netauditd.c#8 edit
.. //depot/projects/trustedbsd/netauditd/netauditd.h#5 edit
Differences ...
==== //depot/projects/trustedbsd/netauditd/Makefile#3 (text+ko) ====
@@ -1,5 +1,5 @@
PROG= netauditd
-SRCS= netauditd.c
+SRCS= netauditd.c component.c conf.c
NO_MAN=
CFLAGS+= -Wall -ggdb
==== //depot/projects/trustedbsd/netauditd/netauditd.c#8 (text+ko) ====
@@ -2,8 +2,6 @@
* Copyright (c) 2008
* Aaron Meihm. All rights reserved.
*
- * $Id: netauditd.c,v 1.9 2008/02/17 19:30:52 alm Exp $
- *
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
@@ -50,285 +48,10 @@
#include "netauditd.h"
-const struct conf_ent {
- char *c_mode;
- char *c_type;
- int c_min_args;
- int (*c_func)(args_t *);
-} conftab[] = {
- { "src", "pipe", 4, conf_parse_src_pipe },
- { "src", "net", 5, conf_parse_net },
- { "dst", "trail", 5, conf_parse_dst_trail },
- { "dst", "net", 6, conf_parse_net },
- { NULL, NULL, 0, NULL }
-};
-
char *conf_path = "/usr/local/etc/netauditd.conf";
int debug_flag;
-TAILQ_HEAD(au_srclist, au_cmpnt) au_srclist;
-TAILQ_HEAD(au_dstlist, au_cmpnt) au_dstlist;
-
-struct au_cmpnt *
-component_init(int is_src, char *type, char *name)
-{
- struct au_cmpnt *new;
-
- new = malloc(sizeof(struct au_cmpnt));
- if (new == NULL)
- return (NULL);
- memset(new, 0, sizeof(struct au_cmpnt));
- if (is_src) {
- if (strcmp(type, "net") == 0)
- new->ac_type = NETAUDIT_SRC_NET;
- else if (strcmp(type, "pipe") == 0)
- new->ac_type = NETAUDIT_SRC_PIPE;
- else {
- free(new);
- return (NULL);
- }
- }
- else {
- if (strcmp(type, "net") == 0)
- new->ac_type = NETAUDIT_DST_NET;
- else if (strcmp(type, "trail") == 0)
- new->ac_type = NETAUDIT_DST_TRAIL;
- else {
- free(new);
- return (NULL);
- }
- }
- if ((new->ac_name = strdup(name)) == NULL) {
- free(new);
- return (NULL);
- }
- TAILQ_INIT(&new->ac_oq);
- return (new);
-}
-
-int
-component_clear_oq(struct au_cmpnt *ptr)
-{
- struct au_queue_ent *a, *b;
-
- if (TAILQ_EMPTY(&ptr->ac_oq))
- return (0);
- TAILQ_FOREACH_SAFE(a, &ptr->ac_oq, aq_glue, b) {
- TAILQ_REMOVE(&ptr->ac_oq, a, aq_glue);
- a->aq_ptr->ar_refcount--;
- if (a->aq_ptr->ar_refcount == 0) {
- free(a->aq_ptr->ar_rec);
- free(a->aq_ptr);
- }
- free(a);
- }
- return (1);
-}
-
void
-component_destroy(struct au_cmpnt *ptr)
-{
- if (ptr->ac_name != NULL)
- free(ptr->ac_name);
- if (ptr->ac_path != NULL)
- free(ptr->ac_path);
- if (ptr->ac_ainfo != NULL)
- freeaddrinfo(ptr->ac_ainfo);
- if (ptr->ac_consumers != NULL)
- free(ptr->ac_consumers);
- component_clear_oq(ptr);
- free(ptr);
-}
-
-struct au_cmpnt *
-conf_get_src(char *name)
-{
- struct au_cmpnt *ret;
-
- TAILQ_FOREACH(ret, &au_srclist, ac_glue) {
- if (strcmp(ret->ac_name, name) == 0)
- return (ret);
- }
- return (NULL);
-}
-
-int
-conf_parse_net(args_t *a)
-{
- struct addrinfo hints;
- struct au_cmpnt *new;
- char *host, *svc;
- int is_src = 0;
-
- if (strcmp(a->args[0], "src") == 0)
- is_src = 1;
- else if (strcmp(a->args[0], "dst") != 0)
- return (-1);
- host = a->args[3];
- svc = a->args[4];
- if ((new = component_init(is_src, a->args[2], a->args[1])) == NULL)
- exit(2);
- memset(&hints, 0, sizeof(hints));
- hints.ai_family = PF_UNSPEC;
- hints.ai_socktype = SOCK_STREAM;
- if (is_src) {
- hints.ai_flags = AI_PASSIVE;
- new->ac_type = NETAUDIT_SRC_NET;
- }
- else
- new->ac_type = NETAUDIT_DST_NET;
- if (getaddrinfo(host, svc, &hints, &new->ac_ainfo) != 0) {
- free(new);
- return (-1);
- }
- if (is_src)
- TAILQ_INSERT_TAIL(&au_srclist, new, ac_glue);
- else {
- if (conf_link_consumers(new, a, 5) == -1) {
- component_destroy(new);
- return (-1);
- }
- TAILQ_INSERT_TAIL(&au_dstlist, new, ac_glue);
- }
- return (0);
-}
-
-int
-conf_link_consumers(struct au_cmpnt *ac, args_t *a, int start)
-{
- struct au_cmpnt *ptr;
- int i;
-
- for (i = start; i < a->args_n; i++) {
- if ((ptr = conf_get_src(a->args[i])) == NULL)
- return (-1);
- dprintf("%s <- %s\n", ac->ac_name, ptr->ac_name);
- if (ptr->ac_consumers == NULL) {
- ptr->ac_consumers = malloc(sizeof(struct au_cmpnt *));
- ptr->ac_nconsumers = 1;
- }
- else {
- ptr->ac_nconsumers++;
- ptr->ac_consumers = realloc(ptr->ac_consumers,
- sizeof(struct au_cmpnt *) * ptr->ac_nconsumers);
- }
- ptr->ac_consumers[ptr->ac_nconsumers - 1] = ac;
- }
- return (0);
-}
-
-int
-conf_parse_src_pipe(args_t *a)
-{
- struct au_cmpnt *new;
-
- if ((new = component_init(1, a->args[2], a->args[1])) == NULL)
- exit(2);
- if ((new->ac_path = strdup(a->args[3])) == NULL)
- exit(2);
- TAILQ_INSERT_TAIL(&au_srclist, new, ac_glue);
- return (0);
-}
-
-int
-conf_parse_dst_trail(args_t *a)
-{
- struct au_cmpnt *new;
-
- if ((new = component_init(0, a->args[2], a->args[1])) == NULL)
- exit(2);
- if ((new->ac_path = strdup(a->args[3])) == NULL)
- exit(2);
- if (conf_link_consumers(new, a, 4) == -1) {
- component_destroy(new);
- return (-1);
- }
- TAILQ_INSERT_TAIL(&au_dstlist, new, ac_glue);
- return (0);
-}
-
-args_t *
-conf_parse_args(char *buf)
-{
- args_t *ret;
- char *s0, *ptr;
-
- ret = malloc(sizeof(args_t));
- if (ret == NULL)
- exit(2);
- memset(ret, 0, sizeof(args_t));
- for (s0 = buf; (ptr = strsep(&s0, " ")) != NULL;) {
- ret->args[ret->args_n] = strdup(ptr);
- if (ret->args[ret->args_n] == NULL)
- exit(2);
- ret->args_n++;
- if (ret->args_n == MAX_ARGUMENTS)
- break;
- }
- return (ret);
-}
-
-void
-conf_free_args(args_t *a)
-{
- int i;
-
- for (i = 0; i < a->args_n; i++)
- free(a->args[i]);
- free(a);
-}
-
-void
-conf_parse(char *buf, int lc)
-{
- const struct conf_ent *cptr;
- args_t *a;
- int i;
-
- if (buf == NULL)
- return;
- for (i = 0; i < strlen(buf); i++)
- if (buf[i] == '\n') {
- buf[i] = '\0';
- break;
- }
- if ((buf[0] == '#') || (buf[0] == '\0'))
- return;
- dprintf("%d: \"%s\"\n", lc, buf);
- a = conf_parse_args(buf);
- /* Ensure three arguments are present */
- if (a->args_n < 3) {
- fprintf(stderr, "netauditd: Syntax error: %s:%d\n", conf_path,
- lc);
- exit(1);
- }
- for (cptr = conftab; cptr->c_type != NULL; cptr++) {
- if ((strcmp(cptr->c_mode, a->args[0]) == 0) &&
- (strcmp(cptr->c_type, a->args[2]) == 0)) {
- if (a->args_n < cptr->c_min_args) {
- fprintf(stderr,
- "netauditd: Syntax error: %s:%d\n",
- conf_path, lc);
- exit(1);
- }
- if (cptr->c_func(a) == -1) {
- fprintf(stderr,
- "netauditd: Syntax error: %s:%d\n",
- conf_path, lc);
- exit(1);
- }
- break;
- }
- }
- if (cptr->c_type == NULL) { /* Command not found */
- fprintf(stderr, "netauditd: Syntax error: %s:%d\n",
- conf_path, lc);
- exit(1);
- }
- conf_free_args(a);
-}
-
-void
dprintf(char *fmt, ...)
{
char buf[2048];
@@ -342,20 +65,6 @@
fprintf(stderr, "debug: %s", buf);
}
-void
-conf_load(char *path)
-{
- char confbuf[1024];
- FILE *fp;
- int lc = 0;
-
- if ((fp = fopen(path, "r")) == NULL)
- err(1, "%s", path);
- while (fgets(confbuf, sizeof(confbuf), fp) != NULL)
- conf_parse(confbuf, ++lc);
- fclose(fp);
-}
-
int
main(int argc, char *argv[])
{
==== //depot/projects/trustedbsd/netauditd/netauditd.h#5 (text+ko) ====
@@ -2,8 +2,6 @@
* Copyright (c) 2008
* Aaron Meihm. All rights reserved.
*
- * $Id: netauditd.h,v 1.5 2008/02/17 19:30:52 alm Exp $
- *
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
@@ -78,10 +76,17 @@
int args_n;
} args_t;
+typedef TAILQ_HEAD(, au_cmpnt) au_cmpnt_head_t;
+extern au_cmpnt_head_t au_srclist;
+extern au_cmpnt_head_t au_dstlist;
+
+extern char *conf_path;
+
int component_clear_oq(struct au_cmpnt *);
void component_destroy(struct au_cmpnt *);
+struct au_cmpnt *component_get_src(char *);
struct au_cmpnt *component_init(int, char *, char *);
-struct au_cmpnt *conf_get_src(char *);
+
void conf_load(char *);
int conf_link_consumers(struct au_cmpnt *, args_t *, int);
void conf_parse(char *, int);
@@ -90,6 +95,7 @@
int conf_parse_net(args_t *);
args_t *conf_parse_args(char *);
void conf_free_args(args_t *);
+
void dprintf(char *, ...);
void netaudit_establish(void);
void netaudit_oq(void);
More information about the p4-projects
mailing list