Skip to content

Commit 83b0fda

Browse files
authored
Merge pull request #3304 from jimklimov/issue-2524
Support providing CGI options for alternate template file names
2 parents 43a9336 + 8b444e3 commit 83b0fda

File tree

10 files changed

+443
-58
lines changed

10 files changed

+443
-58
lines changed

NEWS.adoc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -347,6 +347,9 @@ several `FSD` notifications into one executed action. [PR #3097]
347347
* Introduced a `@NUT_UPSSTATS_TEMPLATE@` command which the HTML template
348348
files now MUST start with (safety check that we are reading a template).
349349
[issue #3252, PR #3249]
350+
* (Experimental) Custom templates other than `upsstats{,-single}.html` can
351+
now be specified as CGI parameters, if locally permitted via `hosts.conf`.
352+
[issue #2524, PR #3304]
350353

351354
- `upssched` tool updates:
352355
* Previously in PR #2896 (NUT releases v2.8.3 and v2.8.4) the `UPSNAME` and

clients/cgilib.c

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,8 +87,15 @@ void extractcgiargs(void)
8787
while (ptr) {
8888
varname = ptr;
8989
eq = strchr(varname, '=');
90-
if (!eq) {
91-
ptr = strchr(varname, '&');
90+
amp = strchr(varname, '&');
91+
if (!eq
92+
|| (eq && amp && amp < eq)
93+
) {
94+
/* Last token is a flag (without assignment in sight),
95+
* OR we've got a flag token in the middle of a query
96+
* string, followed by another key=value pair later on.
97+
*/
98+
ptr = amp;
9299
if (ptr)
93100
*ptr++ = '\0';
94101

@@ -99,6 +106,8 @@ void extractcgiargs(void)
99106
continue;
100107
}
101108

109+
/* The nearest point of interest is a key=value pair,
110+
* maybe followed by another amp and flag or assignment... */
102111
*eq = '\0';
103112
value = eq + 1;
104113
amp = strchr(value, '&');
@@ -111,6 +120,8 @@ void extractcgiargs(void)
111120

112121
cleanvar = unescape(varname);
113122
cleanval = unescape(value);
123+
upsdebugx(3, "%s: parsearg('%s', '%s')<br/>",
124+
__func__, NUT_STRARG(cleanvar), NUT_STRARG(cleanval));
114125
parsearg(cleanvar, cleanval);
115126
free(cleanvar);
116127
free(cleanval);

clients/upsimage.c

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
Copyrights:
2121
(C) 1998 Russell Kroll <rkroll@exploits.org>
2222
(C) 2002 Simon Rozman <simon@rozman.net>
23+
(C) 2020-2026 Jim Klimov <jimklimov+nut@gmail.com>
2324
2425
This program is free software; you can redistribute it and/or modify
2526
it under the terms of the GNU General Public License as published by
@@ -619,14 +620,14 @@ int main(int argc, char **argv)
619620
double var = 0;
620621

621622
#ifdef WIN32
622-
/* Required ritual before calling any socket functions */
623-
static WSADATA WSAdata;
624-
static int WSA_Started = 0;
625-
if (!WSA_Started) {
626-
WSAStartup(2, &WSAdata);
627-
atexit((void(*)(void))WSACleanup);
628-
WSA_Started = 1;
629-
}
623+
/* Required ritual before calling any socket functions */
624+
static WSADATA WSAdata;
625+
static int WSA_Started = 0;
626+
if (!WSA_Started) {
627+
WSAStartup(2, &WSAdata);
628+
atexit((void(*)(void))WSACleanup);
629+
WSA_Started = 1;
630+
}
630631

631632
/* Avoid binary output conversions, e.g.
632633
* mangling what looks like CRLF on WIN32 */
@@ -646,6 +647,23 @@ int main(int argc, char **argv)
646647
nut_debug_level = i;
647648
}
648649

650+
#ifdef NUT_CGI_DEBUG_UPSIMAGE
651+
# if (NUT_CGI_DEBUG_UPSIMAGE - 0 < 1)
652+
# undef NUT_CGI_DEBUG_UPSIMAGE
653+
# define NUT_CGI_DEBUG_UPSIMAGE 6
654+
# endif
655+
/* Un-comment via make flags when developer-troubleshooting: */
656+
nut_debug_level = NUT_CGI_DEBUG_UPSIMAGE;
657+
#endif
658+
659+
if (nut_debug_level > 0) {
660+
cgilogbit_set();
661+
printf("Content-type: text/html\n");
662+
printf("Pragma: no-cache\n");
663+
printf("\n");
664+
printf("<p>NUT CGI Debugging enabled, level: %d</p>\n\n", nut_debug_level);
665+
}
666+
649667
extractcgiargs();
650668

651669
upscli_init_default_connect_timeout(NULL, NULL, UPSCLI_DEFAULT_CONNECT_TIMEOUT);

clients/upsset.c

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
/* upsset - CGI program to manage read/write variables
22
33
Copyright (C) 1999 Russell Kroll <rkroll@exploits.org>
4+
Copyright (C) 2020-2026 Jim Klimov <jimklimov+nut@gmail.com>
45
56
This program is free software; you can redistribute it and/or modify
67
it under the terms of the GNU General Public License as published by
@@ -1116,14 +1117,14 @@ int main(int argc, char **argv)
11161117
int i;
11171118

11181119
#ifdef WIN32
1119-
/* Required ritual before calling any socket functions */
1120-
static WSADATA WSAdata;
1121-
static int WSA_Started = 0;
1122-
if (!WSA_Started) {
1123-
WSAStartup(2, &WSAdata);
1124-
atexit((void(*)(void))WSACleanup);
1125-
WSA_Started = 1;
1126-
}
1120+
/* Required ritual before calling any socket functions */
1121+
static WSADATA WSAdata;
1122+
static int WSA_Started = 0;
1123+
if (!WSA_Started) {
1124+
WSAStartup(2, &WSAdata);
1125+
atexit((void(*)(void))WSACleanup);
1126+
WSA_Started = 1;
1127+
}
11271128

11281129
/* Avoid binary output conversions, e.g.
11291130
* mangling what looks like CRLF on WIN32 */
@@ -1136,7 +1137,9 @@ int main(int argc, char **argv)
11361137
NUT_UNUSED_VARIABLE(argv);
11371138
username = password = function = monups = NULL;
11381139

1139-
printf("Content-type: text/html\n\n");
1140+
printf("Content-type: text/html\n");
1141+
printf("Pragma: no-cache\n");
1142+
printf("\n");
11401143

11411144
/* NOTE: Caller must `export NUT_DEBUG_LEVEL` to see debugs for upsc
11421145
* and NUT methods called from it. This line aims to just initialize
@@ -1148,6 +1151,20 @@ int main(int argc, char **argv)
11481151
nut_debug_level = i;
11491152
}
11501153

1154+
#ifdef NUT_CGI_DEBUG_UPSSET
1155+
# if (NUT_CGI_DEBUG_UPSSET - 0 < 1)
1156+
# undef NUT_CGI_DEBUG_UPSSET
1157+
# define NUT_CGI_DEBUG_UPSSET 6
1158+
# endif
1159+
/* Un-comment via make flags when developer-troubleshooting: */
1160+
nut_debug_level = NUT_CGI_DEBUG_UPSSET;
1161+
#endif
1162+
1163+
if (nut_debug_level > 0) {
1164+
cgilogbit_set();
1165+
printf("<p>NUT CGI Debugging enabled, level: %d</p>\n\n", nut_debug_level);
1166+
}
1167+
11511168
/* see if the magic string is present in the config file */
11521169
check_conf();
11531170

0 commit comments

Comments
 (0)