Skip to content

Commit

Permalink
clients/upsclient.c: upscli_splitname(): handle "upsname:port" shortc…
Browse files Browse the repository at this point in the history
…ut attempts

Signed-off-by: Jim Klimov <[email protected]>
  • Loading branch information
jimklimov committed Jan 6, 2025
1 parent e9ed4e2 commit 2c8a0f7
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 10 deletions.
2 changes: 2 additions & 0 deletions NEWS.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -414,6 +414,8 @@ logic that was previously the content of `upsdrv_shutdown()` methods was often
relocated into new `shutdown.default` INSTCMD definitions. [#2670]
- common code:
* `upscli_splitname()` should now recognize `upsname:port` typos (missing
the `@hostname` part) and error out gracefully.
* introduced a `NUT_DEBUG_SYSLOG` environment variable to tweak activation
of syslog message emission (and related detachment of `stderr` when
backgrounding), primarily useful for NIT and perhaps systemd. Most
Expand Down
28 changes: 18 additions & 10 deletions clients/upsclient.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
Copyright (C)
2002 Russell Kroll <[email protected]>
2008 Arjen de Korte <[email protected]>
2020 - 2024 Jim Klimov <[email protected]>
2020 - 2025 Jim Klimov <[email protected]>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
Expand Down Expand Up @@ -1622,7 +1622,7 @@ ssize_t upscli_readline(UPSCONN_t *ups, char *buf, size_t buflen)
/* split upsname[@hostname[:port]] into separate components */
int upscli_splitname(const char *buf, char **upsname, char **hostname, uint16_t *port)
{
char *s, tmp[SMALLBUF], *last = NULL;
char *sat, *ssc, tmp[SMALLBUF], *last = NULL;

/* paranoia */
if ((!buf) || (!upsname) || (!hostname) || (!port)) {
Expand All @@ -1634,10 +1634,11 @@ int upscli_splitname(const char *buf, char **upsname, char **hostname, uint16_t
return -1;
}

s = strchr(tmp, '@');
sat = strchr(tmp, '@');
ssc = strchr(tmp, ':');

/* someone passed a "@hostname" string? */
if (s == tmp) {
if (sat == tmp) {
fprintf(stderr, "upscli_splitname: got empty upsname string\n");
return -1;
}
Expand All @@ -1653,13 +1654,20 @@ int upscli_splitname(const char *buf, char **upsname, char **hostname, uint16_t
return -1;
}

/*
/*
fprintf(stderr, "upscli_splitname3: got buf='%s', tmp='%s', upsname='%s', possible hostname:port='%s'\n",
NUT_STRARG(buf), NUT_STRARG(tmp), NUT_STRARG(*upsname), NUT_STRARG((s ? s+1 : s)));
*/
NUT_STRARG(buf), NUT_STRARG(tmp), NUT_STRARG(*upsname), NUT_STRARG((sat ? sat+1 : sat)));
*/

/* only a upsname is specified, fill in defaults */
if (s == NULL) {
if (sat == NULL) {
if (ssc) {
/* TOTHINK: Consult isdigit(ssc+1) to shortcut
* `upsname:port` into `upsname@localhost:port`? */
fprintf(stderr, "upscli_splitname: port specified, but not a hostname\n");
return -1;
}

if ((*hostname = xstrdup("localhost")) == NULL) {
fprintf(stderr, "upscli_splitname: xstrdup failed\n");
return -1;
Expand All @@ -1670,12 +1678,12 @@ int upscli_splitname(const char *buf, char **upsname, char **hostname, uint16_t
}

/* someone passed a "upsname@" string? */
if (!(*(s+1))) {
if (!(*(sat+1))) {
fprintf(stderr, "upscli_splitname: got the @ separator and then an empty hostname[:port] string\n");
return -1;
}

return upscli_splitaddr(s+1, hostname, port);
return upscli_splitaddr(sat+1, hostname, port);
}

/* split hostname[:port] into separate components */
Expand Down

0 comments on commit 2c8a0f7

Please sign in to comment.