Skip to content

Commit

Permalink
Merge pull request #127 from farsightsec/rte-1760-fix-null-operator
Browse files Browse the repository at this point in the history
Rte 1760 fix null operator
  • Loading branch information
shw700 authored Oct 6, 2023
2 parents 672249d + 89118f4 commit 947e408
Show file tree
Hide file tree
Showing 9 changed files with 151 additions and 40 deletions.
6 changes: 3 additions & 3 deletions nmsg/msgmod/message.c
Original file line number Diff line number Diff line change
Expand Up @@ -491,12 +491,12 @@ nmsg_message_to_pres(struct nmsg_message *msg, char **pres, const char *endline)
}

nmsg_res
_nmsg_message_to_json(nmsg_message_t msg, struct nmsg_strbuf *sb) {
_nmsg_message_to_json(nmsg_output_t output, nmsg_message_t msg, struct nmsg_strbuf *sb) {
if (msg->mod == NULL)
return (nmsg_res_failure);
switch (msg->mod->plugin->type) {
case nmsg_msgmod_type_transparent:
return (_nmsg_message_payload_to_json(msg, sb));
return (_nmsg_message_payload_to_json(output, msg, sb));
case nmsg_msgmod_type_opaque:
return (nmsg_res_notimpl);
default:
Expand All @@ -510,7 +510,7 @@ nmsg_message_to_json(nmsg_message_t msg, char **json) {
struct nmsg_strbuf_storage sbs;
struct nmsg_strbuf *sb = _nmsg_strbuf_init(&sbs);

res = _nmsg_message_to_json(msg, sb);
res = _nmsg_message_to_json(NULL, msg, sb);
if (res == nmsg_res_success) {
*json = _nmsg_strbuf_detach(sb);
}
Expand Down
2 changes: 1 addition & 1 deletion nmsg/msgmod/transparent.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ _nmsg_msgmod_pres_to_payload_finalize(struct nmsg_msgmod *mod, void *cl,
uint8_t **pbuf, size_t *sz);

nmsg_res
_nmsg_message_payload_to_json(struct nmsg_message *msg, struct nmsg_strbuf *sb);
_nmsg_message_payload_to_json(nmsg_output_t output, struct nmsg_message *msg, struct nmsg_strbuf *sb);

nmsg_res
_nmsg_message_payload_to_json_load(struct nmsg_message *msg,
Expand Down
42 changes: 30 additions & 12 deletions nmsg/msgmod/transparent_payload.c
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@ _nmsg_message_payload_to_pres_load(struct nmsg_message *msg,
}

nmsg_res
_nmsg_message_payload_to_json(struct nmsg_message *msg, struct nmsg_strbuf *sb) {
_nmsg_message_payload_to_json(nmsg_output_t output, struct nmsg_message *msg, struct nmsg_strbuf *sb) {
Nmsg__NmsgPayload *np;
ProtobufCMessage *m;
nmsg_res res;
Expand All @@ -285,6 +285,7 @@ _nmsg_message_payload_to_json(struct nmsg_message *msg, struct nmsg_strbuf *sb)

struct nmsg_msgmod_field *field;
const char *vname, *mname;
uint32_t oper_val = 0, group_val = 0, source_val = 0;

struct tm tm;
time_t t;
Expand Down Expand Up @@ -329,29 +330,46 @@ _nmsg_message_payload_to_json(struct nmsg_message *msg, struct nmsg_strbuf *sb)
declare_json_value(sb, "mname", false);
append_json_value_string(sb, mname, strlen(mname));

if (np->has_source) {
sb_tmp_len = snprintf(sb_tmp, sizeof(sb_tmp), "%08x", np->source);
if (output != NULL && output->json->source != 0)
source_val = output->json->source;
else if (np->has_source)
source_val = np->source;

if (source_val != 0) {
sb_tmp_len = snprintf(sb_tmp, sizeof(sb_tmp), "%08x", source_val);
declare_json_value(sb, "source", false);
append_json_value_string(sb, sb_tmp, sb_tmp_len);
}

if (np->has_operator_) {
const char *operator = nmsg_alias_by_key(nmsg_alias_operator, np->operator_);
if (output != NULL && output->json->operator != 0)
oper_val = output->json->operator;
else if (np->has_operator_)
oper_val = np->operator_;

if (oper_val != 0) {
const char *operator = nmsg_alias_by_key(nmsg_alias_operator, oper_val);
declare_json_value(sb, "operator", false);
if (operator != NULL) {

if (operator != NULL)
append_json_value_string(sb, operator, strlen(operator));
} else {
append_json_value_int(sb, np->operator_);
}
else
append_json_value_int(sb, oper_val);
}

if (np->has_group) {
const char *group = nmsg_alias_by_key(nmsg_alias_group, np->group);
if (output != NULL && output->json->group != 0)
group_val = output->json->group;
else if (np->has_group)
group_val = np->group;

if (group_val != 0) {
const char *group = nmsg_alias_by_key(nmsg_alias_group, group_val);
declare_json_value(sb, "group", false);

if (group != NULL)
append_json_value_string(sb, group, strlen(group));
else
append_json_value_int(sb, np->group);
append_json_value_int(sb, group_val);

}

declare_json_value(sb, "message", false);
Expand Down
39 changes: 36 additions & 3 deletions nmsg/output.c
Original file line number Diff line number Diff line change
Expand Up @@ -291,20 +291,53 @@ nmsg_output_set_endline(nmsg_output_t output, const char *endline) {

void
nmsg_output_set_source(nmsg_output_t output, unsigned source) {
if (output->type == nmsg_output_type_stream)
switch(output->type) {
case nmsg_output_type_stream:
output->stream->source = source;
break;
case nmsg_output_type_pres:
output->pres->source = source;
break;
case nmsg_output_type_json:
output->json->source = source;
break;
default:
break;
}
}

void
nmsg_output_set_operator(nmsg_output_t output, unsigned operator) {
if (output->type == nmsg_output_type_stream)
switch(output->type) {
case nmsg_output_type_stream:
output->stream->operator = operator;
break;
case nmsg_output_type_pres:
output->pres->operator = operator;
break;
case nmsg_output_type_json:
output->json->operator = operator;
break;
default:
break;
}
}

void
nmsg_output_set_group(nmsg_output_t output, unsigned group) {
if (output->type == nmsg_output_type_stream)
switch(output->type) {
case nmsg_output_type_stream:
output->stream->group = group;
break;
case nmsg_output_type_pres:
output->pres->group = group;
break;
case nmsg_output_type_json:
output->json->group = group;
break;
default:
break;
}
}

void
Expand Down
2 changes: 1 addition & 1 deletion nmsg/output_json.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ _output_json_write(nmsg_output_t output, nmsg_message_t msg) {
struct nmsg_strbuf_storage sbs;
struct nmsg_strbuf *sb = _nmsg_strbuf_init(&sbs);

res = _nmsg_message_to_json(msg, sb);
res = _nmsg_message_to_json(output, msg, sb);
if (res != nmsg_res_success)
goto out;

Expand Down
55 changes: 42 additions & 13 deletions nmsg/output_pres.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,12 @@
nmsg_res
_output_pres_write(nmsg_output_t output, nmsg_message_t msg) {
Nmsg__NmsgPayload *np;
const char *vname = NULL;
const char *mname = NULL;
char *pres_data;
char op_buf[sizeof("4294967295")] = {0}, group_buf[sizeof("4294967295")] = {0};
const char *op_str = op_buf, *group_str = group_buf;
const char *vname = NULL, *mname = NULL;
char when[32];
char *pres_data;
uint32_t oper_val = 0, group_val = 0, source_val = 0;
nmsg_msgmod_t mod;
nmsg_res res;
struct tm tm;
Expand All @@ -52,23 +54,50 @@ _output_pres_write(nmsg_output_t output, nmsg_message_t msg) {
}
vname = nmsg_msgmod_vid_to_vname(np->vid);
mname = nmsg_msgmod_msgtype_to_mname(np->vid, np->msgtype);

if (output->pres->operator != 0)
oper_val = output->pres->operator;
else if (np->has_operator_)
oper_val = np->operator_;

if (oper_val != 0) {
op_str = nmsg_alias_by_key(nmsg_alias_operator, oper_val);

if (op_str == NULL) {
snprintf(op_buf, sizeof(op_buf), "%"PRIu32, oper_val);
op_str = op_buf;
}
}

if (output->pres->group != 0)
group_val = output->pres->group;
else if (np->has_group)
group_val = np->group;

if (group_val != 0) {
group_str = nmsg_alias_by_key(nmsg_alias_group, group_val);

if (group_str == NULL) {
snprintf(group_buf, sizeof(group_buf), "%"PRIu32, group_val);
group_str = group_buf;
}
}

if (output->pres->source != 0)
source_val = output->pres->source;
else if (np->has_source)
source_val = np->source;

fprintf(output->pres->fp, "[%zu] [%s.%09u] [%d:%d %s %s] "
"[%08x] [%s] [%s] %s%s",
np->has_payload ? np->payload.len : 0,
when, np->time_nsec,
np->vid, np->msgtype,
vname ? vname : "(unknown)",
mname ? mname : "(unknown)",
np->has_source ? np->source : 0,

np->has_operator_ ?
nmsg_alias_by_key(nmsg_alias_operator, np->operator_)
: "",

np->has_group ?
nmsg_alias_by_key(nmsg_alias_group, np->group)
: "",

source_val,
op_str,
group_str,
output->pres->endline, pres_data);
fputs("\n", output->pres->fp);
if (output->pres->flush)
Expand Down
8 changes: 7 additions & 1 deletion nmsg/private.h
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,9 @@ struct nmsg_pres {
FILE *fp;
bool flush;
char *endline;
unsigned source;
unsigned operator;
unsigned group;
};

/* nmsg_json: used by nmsg_input and nmsg_output */
Expand All @@ -242,6 +245,9 @@ struct nmsg_json {
FILE *fp;
int orig_fd;
bool flush;
unsigned source;
unsigned operator;
unsigned group;
};

/* nmsg_stream_input: used by nmsg_input */
Expand Down Expand Up @@ -469,7 +475,7 @@ nmsg_res _nmsg_message_serialize(struct nmsg_message *msg);
nmsg_message_t _nmsg_message_from_payload(Nmsg__NmsgPayload *np);
nmsg_message_t _nmsg_message_dup(struct nmsg_message *msg);
nmsg_res _nmsg_message_dup_protobuf(const struct nmsg_message *msg, ProtobufCMessage **dst);
nmsg_res _nmsg_message_to_json(nmsg_message_t msg, struct nmsg_strbuf *sb);
nmsg_res _nmsg_message_to_json(nmsg_output_t output, nmsg_message_t msg, struct nmsg_strbuf *sb);

/* from msgmodset.c */

Expand Down
3 changes: 2 additions & 1 deletion src/nmsgtool.c
Original file line number Diff line number Diff line change
Expand Up @@ -384,7 +384,8 @@ usage(const char *msg) {
if (msg)
fprintf(stderr, "%s: usage error: %s\n", argv_program, msg);
nmsg_io_destroy(&ctx.io);
exit(argv_usage(args, ARGV_USAGE_DEFAULT));
argv_usage(args, ARGV_USAGE_DEFAULT);
exit(msg == NULL ? EXIT_SUCCESS : EXIT_FAILURE);
}

void
Expand Down
34 changes: 29 additions & 5 deletions src/process_args.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <ctype.h>

#include "nmsgtool.h"

Expand Down Expand Up @@ -62,6 +63,22 @@ droproot(nmsgtool_ctx *c, FILE *fp_pidfile) {
argv_program, c->username);
}

/* Convert string to non-zero unsigned 32 bit val, returning zero on failure. */
static uint32_t
read_uint32_nz(const char *str)
{
char *t;
unsigned long val;

val = strtoul(str, &t, 0);
if (*t != '\0')
return 0;
else if (val > UINT32_MAX)
return 0;

return (uint32_t)val;
}

void
process_args(nmsgtool_ctx *c) {
char *t;
Expand Down Expand Up @@ -141,17 +158,18 @@ process_args(nmsgtool_ctx *c) {

/* set source, operator, group */
if (c->set_source_str != NULL) {
c->set_source = (unsigned) strtoul(c->set_source_str, &t, 0);
if (*t != '\0')
c->set_source = read_uint32_nz(c->set_source_str);
if (c->set_source == 0)
usage("invalid source ID");
if (c->debug >= 2)
fprintf(stderr, "%s: nmsg source set to %#.08x\n",
argv_program, c->set_source);
}

if (c->set_operator_str != NULL) {
c->set_operator = nmsg_alias_by_value(nmsg_alias_operator,
c->set_operator_str);
if (c->set_operator == 0)
c->set_operator = read_uint32_nz(c->set_operator_str);
if (c->set_operator == 0)
usage("unknown operator name");
if (c->debug >= 2)
Expand All @@ -164,6 +182,8 @@ process_args(nmsgtool_ctx *c) {
if (c->set_group_str != NULL) {
c->set_group = nmsg_alias_by_value(nmsg_alias_group,
c->set_group_str);
if (c->set_group == 0)
c->set_group = read_uint32_nz(c->set_group_str);
if (c->set_group == 0)
usage("unknown group name");
if (c->debug >= 2)
Expand All @@ -175,8 +195,8 @@ process_args(nmsgtool_ctx *c) {

/* get source, operator, group */
if (c->get_source_str != NULL) {
c->get_source = (unsigned) strtoul(c->get_source_str, &t, 0);
if (*t != '\0')
c->get_source = read_uint32_nz(c->get_source_str);
if (c->get_source == 0)
usage("invalid filter source ID");
if (c->debug >= 2)
fprintf(stderr, "%s: nmsg source filter set to "
Expand All @@ -187,6 +207,8 @@ process_args(nmsgtool_ctx *c) {
if (c->get_operator_str != NULL) {
c->get_operator = nmsg_alias_by_value(nmsg_alias_operator,
c->get_operator_str);
if (c->get_operator == 0)
c->get_operator = read_uint32_nz(c->get_operator_str);
if (c->get_operator == 0)
usage("unknown filter operator name");
if (c->debug >= 2)
Expand All @@ -200,6 +222,8 @@ process_args(nmsgtool_ctx *c) {
if (c->get_group_str != NULL) {
c->get_group = nmsg_alias_by_value(nmsg_alias_group,
c->get_group_str);
if (c->get_group == 0)
c->get_group = read_uint32_nz(c->get_group_str);
if (c->get_group == 0)
usage("unknown filter group name");
if (c->debug >= 2)
Expand Down

0 comments on commit 947e408

Please sign in to comment.