From eb5b69d278b1430750a5e1d74bd2804cbc4b14e8 Mon Sep 17 00:00:00 2001 From: Alex Dowad Date: Fri, 4 Apr 2014 16:31:24 +0200 Subject: all: unified framework for reporting errors/warnings/info messages to stderr/syslog --- src/squark-auth-snmp.c | 64 ++++++++++++++++++++------------------------------ 1 file changed, 25 insertions(+), 39 deletions(-) (limited to 'src/squark-auth-snmp.c') diff --git a/src/squark-auth-snmp.c b/src/squark-auth-snmp.c index 9a35d33..05832d4 100644 --- a/src/squark-auth-snmp.c +++ b/src/squark-auth-snmp.c @@ -19,7 +19,6 @@ #include #include -#include #include #include @@ -31,12 +30,7 @@ #include "addr.h" #include "authdb.h" #include "filterdb.h" - -#if 0 -#define dbg_printf(args...) printf(args) -#else -#define dbg_printf(args...) -#endif +#include "reporting.h" /* Compile time configurables */ #define SWITCH_HASH_SIZE 128 @@ -110,7 +104,6 @@ struct switch_info; static int num_queries = 0; static int running = TRUE; static int kick_out = FALSE; -static int do_syslog = FALSE; static struct sqdb db; static struct authdb adb; @@ -615,12 +608,7 @@ static void auth_completed(struct auth_context *auth) authdb_commit_login(token, &entry, current_time, &adbc); } - if (do_syslog) { - syslog(LOG_AUTHPRIV | LOG_INFO, - "%s authenticated as %.*s", - addr_print(&auth->addr), - uf.len, uf.ptr); - } + report_private_message(REPORT_INFO, "%s authenticated as %.*s", addr_print(&auth->addr), uf.len, uf.ptr); } else { if (token != NULL) authdb_commit_logout(token); @@ -629,12 +617,7 @@ static void auth_completed(struct auth_context *auth) blob_push(&b, BLOB_STRLEN(auth->token)); blob_push(&b, BLOB_STR(" ERR\n")); - if (do_syslog) { - syslog(LOG_AUTHPRIV | LOG_WARNING, - "%s failed: %s", - addr_print(&auth->addr), - auth->status_msg); - } + report_private_message(REPORT_WARNING, "%s failed: %s", addr_print(&auth->addr), auth->status_msg); } b = blob_pushed(BLOB_BUF(tmp), b); write(STDOUT_FILENO, b.ptr, b.len); @@ -746,7 +729,7 @@ static int auth_handle_portinfo_reply(int oper, netsnmp_session *s, int reqid, n snprintf(auth->status_msg, sizeof(auth->status_msg)-1, "required info missing: info_available=%08x", auth->info_available); - dbg_printf("%s\n", auth->status_msg); + report_debug("%s\n", auth->status_msg); done: if (kick_out && auth_ok(auth)) @@ -798,7 +781,7 @@ static void auth_query_port_info(struct auth_context *auth) snprintf(auth->status_msg, sizeof(auth->status_msg)-1, "%s: query port info (%d)", si->session->peername, auth->local_port); - dbg_printf("%s\n", auth->status_msg); + report_debug("%s\n", auth->status_msg); auth_talk_snmp(auth, si->session, pdu, auth_handle_portinfo_reply); } @@ -830,12 +813,12 @@ static int auth_handle_lldp_reply(int oper, netsnmp_session *s, int reqid, netsn blob_pull_oid(&res); blob_pull_iana_afn(&res, &spi->link_partner); - dbg_printf("%s: lldp neighbour is %s\n", + report_debug("%s: lldp neighbour is %s\n", s->peername, addr_print(&spi->link_partner)); if (management_prefix != 0 && addr_prefix_cmp(&spi->link_partner, &management_subnet, management_prefix) != 0) { - dbg_printf("%s: not matching %s/%d\n", + report_debug("%s: not matching %s/%d\n", s->peername, addr_print(&management_subnet), management_prefix); addr_invalidate(&spi->link_partner); } @@ -917,7 +900,7 @@ static void auth_query_lldp(struct auth_context *auth, int root_query) "%s: query LLDP tables (%s, base port %d)", si->session->peername, root_query ? "link" : "lacp slaves", auth->lldp_port[0]); - dbg_printf("%s\n", auth->status_msg); + report_debug("%s\n", auth->status_msg); cache_talk_snmp(&spi->cache_control, si->session, pdu, auth_handle_lldp_reply, auth); } @@ -998,7 +981,7 @@ static void auth_query_fib(struct auth_context *auth) "%s: probe FIB (%sBRIDGE-MIB)", si->session->peername, si->q_vlan_fdb_id>=0 ? "Q-" : ""); - dbg_printf("%s\n", auth->status_msg); + report_debug("%s\n", auth->status_msg); auth_talk_snmp(auth, si->session, pdu, auth_handle_fib_reply); } @@ -1059,7 +1042,7 @@ static void auth_query_switch_info(struct auth_context *auth) snprintf(auth->status_msg, sizeof(auth->status_msg)-1, "%s: refresh switch information", si->session->peername); - dbg_printf("%s\n", auth->status_msg); + report_debug("%s\n", auth->status_msg); if (!cache_refresh(&si->cache_control, auth, auth_query_fib)) return; @@ -1067,7 +1050,7 @@ static void auth_query_switch_info(struct auth_context *auth) snprintf(auth->status_msg, sizeof(auth->status_msg)-1, "%s: query switch information", si->session->peername); - dbg_printf("%s\n", auth->status_msg); + report_debug("%s\n", auth->status_msg); pdu = snmp_pdu_create(SNMP_MSG_GET); snmp_add_null_var(pdu, oid_const(SNMPv2_MIB_sysName)); @@ -1138,7 +1121,7 @@ void start_authentication(blob_t token, blob_t ip) "%s: map IP %s to MAC on VLAN %d", l3_root_dev->session->peername, addr_print(&auth->addr), l3_if_ndx); - dbg_printf("%s\n", auth->status_msg); + report_debug("%s\n", auth->status_msg); auth_talk_snmp(auth, l3_root_dev->session, pdu, auth_handle_arp_reply); } @@ -1218,11 +1201,12 @@ int main(int argc, char **argv) fd_set fdset; int opt, fds, block, i, rc = 1; + reporting_init("squark-auth-snmp"); + setenv("MIBS", "", 1); init_snmp("squark-auth-snmp"); - openlog("squark-auth-snmp", LOG_PID, LOG_DAEMON); - while ((opt = getopt(argc, argv, "Vc:r:i:R:v:f:T:KsM:")) != -1) { + while ((opt = getopt(argc, argv, "Vc:r:i:R:v:f:T:KsM:sq")) != -1) { switch (opt) { case 'V': fprintf(stderr, "squark-auth-snmp %s\n", squark_version); @@ -1251,32 +1235,34 @@ int main(int argc, char **argv) case 'K': kick_out = TRUE; break; - case 's': - do_syslog = TRUE; - break; case 'M': if (!addr_parse_prefix(BLOB_STRLEN(optarg), &management_subnet, &management_prefix)) { - fprintf(stderr, "'%s' is not a valid network prefix\n", optarg); + report_error("'%s' is not a valid network prefix\n", optarg); return 1; } break; + case 's': + reporting_use_syslog(1); + break; + case 'q': + reporting_verbosity(REPORT_ALERT); + break; } } argc -= optind; argv += optind; if (l3_root == NULL || l3_ifname == NULL || l2_vlan == NULL) { - fprintf(stderr, "Mandatory information missing\n"); + report_error("Mandatory information missing\n"); return 1; } if (sqdb_open(&db, squark_dbname) < 0) { - fprintf(stderr, "%s: failed to open squarkdb\n", - squark_dbname); + report_error("%s: failed to open squarkdb\n", squark_dbname); goto err_sqdb; } if (authdb_open(&adb, &adbc, &db) < 0) { - fprintf(stderr, "Failed to initialize authdb\n"); + report_error("Failed to initialize authdb\n"); goto err_adb; } -- cgit v1.2.3