From dd6cdbe89c7c82d3927d68850e0286c8130ce77f Mon Sep 17 00:00:00 2001 From: Arnaud Quette Date: Wed, 13 Mar 2019 16:32:17 +0100 Subject: [PATCH 1/3] Use str* API to check for valid numeric value Closes networkupstools/nut#676 Signed-off-by: Arnaud Quette --- server/conf.c | 42 ++++++++++++++++++------------------------ 1 file changed, 18 insertions(+), 24 deletions(-) diff --git a/server/conf.c b/server/conf.c index e6046ed7c9..8d925c9fcb 100644 --- a/server/conf.c +++ b/server/conf.c @@ -116,44 +116,40 @@ static void ups_update(const char *fn, const char *name, const char *desc) /* return 1 if usable, 0 if not */ static int parse_upsd_conf_args(int numargs, char **arg) { + unsigned int temp; + /* everything below here uses up through arg[1] */ if (numargs < 2) return 0; /* MAXAGE */ if (!strcmp(arg[0], "MAXAGE")) { - if (isdigit(arg[1])) { - maxage = atoi(arg[1]); + if (str_to_uint(arg[1], &temp, 10)) { + maxage = temp; return 1; } - else { - upslogx(LOG_ERR, "MAXAGE has non numeric value (%s)!", arg[1]); - return 0; - } + upslogx(LOG_ERR, "Could not convert MAXAGE (%s) to an unsigned int (%s)!", arg[1], strerror(errno)); + return 0; } /* TRACKINGDELAY */ if (!strcmp(arg[0], "TRACKINGDELAY")) { - if (isdigit(arg[1])) { - tracking_delay = atoi(arg[1]); + if (str_to_uint(arg[1], &temp, 10)) { + tracking_delay = temp; return 1; } - else { - upslogx(LOG_ERR, "TRACKINGDELAY has non numeric value (%s)!", arg[1]); - return 0; - } + upslogx(LOG_ERR, "Could not convert TRACKINGDELAY (%s) to an unsigned int (%s)!", arg[1], strerror(errno)); + return 0; } /* MAXCONN */ if (!strcmp(arg[0], "MAXCONN")) { - if (isdigit(arg[1])) { - maxconn = atoi(arg[1]); + if (str_to_uint(arg[1], &temp, 10)) { + maxconn = temp; return 1; } - else { - upslogx(LOG_ERR, "MAXCONN has non numeric value (%s)!", arg[1]); - return 0; - } + upslogx(LOG_ERR, "Could not convert MAXCONN (%s) to an unsigned int (%s)!", arg[1], strerror(errno)); + return 0; } /* STATEPATH */ @@ -187,14 +183,12 @@ static int parse_upsd_conf_args(int numargs, char **arg) #ifdef WITH_CLIENT_CERTIFICATE_VALIDATION /* CERTREQUEST (0 | 1 | 2) */ if (!strcmp(arg[0], "CERTREQUEST")) { - if (isdigit(arg[1])) { - certrequest = atoi(arg[1]); + if (str_to_uint(arg[1], &temp, 10)) { + certrequest = temp; return 1; } - else { - upslogx(LOG_ERR, "CERTREQUEST has non numeric value (%s)!", arg[1]); - return 0; - } + upslogx(LOG_ERR, "Could not convert CERTREQUEST (%s) to an unsigned int (%s)!", arg[1], strerror(errno)); + return 0; } #endif /* WITH_CLIENT_CERTIFICATE_VALIDATION */ #endif /* WITH_OPENSSL | WITH_NSS */ From f88b0876527ce43f9a10642181ca5470bbee9ace Mon Sep 17 00:00:00 2001 From: Arnaud Quette Date: Fri, 15 Mar 2019 09:16:39 +0100 Subject: [PATCH 2/3] Change variable type from signed to unsigned int Signed-off-by: Arnaud Quette --- server/netssl.c | 2 +- server/netssl.h | 2 +- server/sstate.c | 2 +- server/sstate.h | 2 +- server/upsd.c | 13 +++++++------ server/upsd.h | 2 +- 6 files changed, 12 insertions(+), 11 deletions(-) diff --git a/server/netssl.c b/server/netssl.c index c2f409899d..d1c818e261 100644 --- a/server/netssl.c +++ b/server/netssl.c @@ -46,7 +46,7 @@ char *certname = NULL; char *certpasswd = NULL; #ifdef WITH_CLIENT_CERTIFICATE_VALIDATION -int certrequest = 0; +unsigned int certrequest = 0; #endif /* WITH_CLIENT_CERTIFICATE_VALIDATION */ static int ssl_initialized = 0; diff --git a/server/netssl.h b/server/netssl.h index 8f68168969..33f73ade0f 100644 --- a/server/netssl.h +++ b/server/netssl.h @@ -32,7 +32,7 @@ extern char *certfile; extern char *certname; extern char *certpasswd; #ifdef WITH_CLIENT_CERTIFICATE_VALIDATION -extern int certrequest; +extern unsigned int certrequest; #endif /* WITH_CLIENT_CERTIFICATE_VALIDATION */ /* List possible values for certrequested */ diff --git a/server/sstate.c b/server/sstate.c index 0365cd30f6..28910dbf3e 100644 --- a/server/sstate.c +++ b/server/sstate.c @@ -341,7 +341,7 @@ const cmdlist_t *sstate_getcmdlist(const upstype_t *ups) return ups->cmdlist; } -int sstate_dead(upstype_t *ups, int maxage) +int sstate_dead(upstype_t *ups, unsigned int maxage) { time_t now; double elapsed; diff --git a/server/sstate.h b/server/sstate.h index fe8485ced3..0b274c108c 100644 --- a/server/sstate.h +++ b/server/sstate.h @@ -47,7 +47,7 @@ const cmdlist_t *sstate_getcmdlist(const upstype_t *ups); void sstate_makeinfolist(const upstype_t *ups, char *buf, size_t bufsize); void sstate_makerwlist(const upstype_t *ups, char *buf, size_t bufsize); void sstate_makeinstcmdlist_t(const upstype_t *ups, char *buf, size_t bufsize); -int sstate_dead(upstype_t *ups, int maxage); +int sstate_dead(upstype_t *ups, unsigned int maxage); void sstate_infofree(upstype_t *ups); void sstate_cmdfree(upstype_t *ups); int sstate_sendline(upstype_t *ups, const char *buf); diff --git a/server/upsd.c b/server/upsd.c index a49b51110f..855a3ce0fe 100644 --- a/server/upsd.c +++ b/server/upsd.c @@ -52,13 +52,13 @@ int deny_severity = LOG_WARNING; upstype_t *firstups = NULL; /* default 15 seconds before data is marked stale */ - int maxage = 15; + unsigned int maxage = 15; /* default to 1h before cleaning up status tracking entries */ - int tracking_delay = 3600; + unsigned int tracking_delay = 3600; /* preloaded to {OPEN_MAX} in main, can be overridden via upsd.conf */ - int maxconn = 0; + unsigned int maxconn = 0; /* preloaded to STATEPATH in main, can be overridden via upsd.conf */ char *statepath = NULL; @@ -691,7 +691,7 @@ static void upsd_cleanup(void) void poll_reload(void) { - int ret; + unsigned int ret; ret = sysconf(_SC_OPEN_MAX); @@ -922,7 +922,8 @@ int nut_uuid_v4(char *uuid_str) /* service requests and check on new data */ static void mainloop(void) { - int i, ret, nfds = 0; + unsigned int i, nfds = 0; + int ret; upstype_t *ups; nut_ctype_t *client, *cnext; @@ -1246,7 +1247,7 @@ int main(int argc, char **argv) chroot_start(chroot_path); } - /* default to system limit (may be overridden in upsd.conf */ + /* default to system limit (may be overridden in upsd.conf) */ maxconn = sysconf(_SC_OPEN_MAX); /* handle upsd.conf */ diff --git a/server/upsd.h b/server/upsd.h index 2cd3eb399e..7bff8a5f75 100644 --- a/server/upsd.h +++ b/server/upsd.h @@ -91,7 +91,7 @@ int tracking_is_enabled(void); /* declarations from upsd.c */ -extern int maxage, maxconn, tracking_delay; +extern unsigned int maxage, maxconn, tracking_delay; extern char *statepath, *datapath; extern upstype_t *firstups; extern nut_ctype_t *firstclient; From 3d79006c2ec3b038f71e1d9441aaf73f744b77ab Mon Sep 17 00:00:00 2001 From: Arnaud Quette Date: Fri, 15 Mar 2019 09:20:12 +0100 Subject: [PATCH 3/3] Simplify error logging Signed-off-by: Arnaud Quette --- server/conf.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/server/conf.c b/server/conf.c index 8d925c9fcb..6243c936ef 100644 --- a/server/conf.c +++ b/server/conf.c @@ -128,7 +128,7 @@ static int parse_upsd_conf_args(int numargs, char **arg) maxage = temp; return 1; } - upslogx(LOG_ERR, "Could not convert MAXAGE (%s) to an unsigned int (%s)!", arg[1], strerror(errno)); + upslogx(LOG_ERR, "Could not convert %s (%s) to an unsigned int (%s)!", arg[0], arg[1], strerror(errno)); return 0; } @@ -138,7 +138,7 @@ static int parse_upsd_conf_args(int numargs, char **arg) tracking_delay = temp; return 1; } - upslogx(LOG_ERR, "Could not convert TRACKINGDELAY (%s) to an unsigned int (%s)!", arg[1], strerror(errno)); + upslogx(LOG_ERR, "Could not convert %s (%s) to an unsigned int (%s)!", arg[0], arg[1], strerror(errno)); return 0; } @@ -148,7 +148,7 @@ static int parse_upsd_conf_args(int numargs, char **arg) maxconn = temp; return 1; } - upslogx(LOG_ERR, "Could not convert MAXCONN (%s) to an unsigned int (%s)!", arg[1], strerror(errno)); + upslogx(LOG_ERR, "Could not convert %s (%s) to an unsigned int (%s)!", arg[0], arg[1], strerror(errno)); return 0; } @@ -187,7 +187,7 @@ static int parse_upsd_conf_args(int numargs, char **arg) certrequest = temp; return 1; } - upslogx(LOG_ERR, "Could not convert CERTREQUEST (%s) to an unsigned int (%s)!", arg[1], strerror(errno)); + upslogx(LOG_ERR, "Could not convert %s (%s) to an unsigned int (%s)!", arg[0], arg[1], strerror(errno)); return 0; } #endif /* WITH_CLIENT_CERTIFICATE_VALIDATION */