Skip to content

Commit 1c1ce36

Browse files
authored
Merge pull request #3023 from jimklimov/issue-3023
Improve `upsd` debug log
2 parents ded7789 + b72bde4 commit 1c1ce36

File tree

1 file changed

+32
-2
lines changed

1 file changed

+32
-2
lines changed

server/upsd.c

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -705,7 +705,28 @@ int ups_available(const upstype_t *ups, nut_ctype_t *client)
705705
static void check_command(int cmdnum, nut_ctype_t *client, size_t numarg,
706706
const char **arg)
707707
{
708-
upsdebugx(6, "Entering %s: %s", __func__, numarg > 0 ? arg[0] : "<>");
708+
char *cmdstr = (numarg > 0 ? (char*)arg[0] : "<>");
709+
int cmdstr_allocated = 0;
710+
711+
if (nut_debug_level > 5 && numarg > 1
712+
&& (nut_debug_level > 9 || strcmp(arg[0], "PASSWORD")) /* Do not log credentials by default */
713+
) {
714+
/* Not xcalloc() here, not too fatal if we fail */
715+
char *s = calloc(LARGEBUF, sizeof(char));
716+
if (s) {
717+
size_t i;
718+
719+
snprintf(s, LARGEBUF, "%s", arg[0]);
720+
for (i = 1; i < numarg; i++) {
721+
snprintfcat(s, LARGEBUF, " [%s]", arg[i]);
722+
}
723+
724+
cmdstr = s;
725+
cmdstr_allocated = 1;
726+
}
727+
}
728+
729+
upsdebugx(6, "Entering %s: %s", __func__, cmdstr);
709730

710731
if (netcmds[cmdnum].flags & FLAG_USER) {
711732
/* command requires previous authentication */
@@ -716,12 +737,16 @@ static void check_command(int cmdnum, nut_ctype_t *client, size_t numarg,
716737
if (!client->username) {
717738
upsdebugx(1, "%s: client not logged in yet", __func__);
718739
send_err(client, NUT_ERR_USERNAME_REQUIRED);
740+
if (cmdstr_allocated)
741+
free(cmdstr);
719742
return;
720743
}
721744

722745
if (!client->password) {
723746
upsdebugx(1, "%s: client not logged in yet", __func__);
724747
send_err(client, NUT_ERR_PASSWORD_REQUIRED);
748+
if (cmdstr_allocated)
749+
free(cmdstr);
725750
return;
726751
}
727752

@@ -735,12 +760,17 @@ static void check_command(int cmdnum, nut_ctype_t *client, size_t numarg,
735760
"tcp-wrappers says access should be denied",
736761
__func__, client->username);
737762
send_err(client, NUT_ERR_ACCESS_DENIED);
763+
if (cmdstr_allocated)
764+
free(cmdstr);
738765
return;
739766
}
740767
#endif /* HAVE_WRAP */
741768
}
742769

743-
upsdebugx(6, "%s: Calling command handler for %s", __func__, numarg > 0 ? arg[0] : "<>");
770+
upsdebugx(6, "%s: Calling command handler for %s", __func__, cmdstr);
771+
772+
if (cmdstr_allocated)
773+
free(cmdstr);
744774

745775
/* looks good - call the command */
746776
netcmds[cmdnum].func(client, (numarg < 2) ? 0 : (numarg - 1), (numarg > 1) ? &arg[1] : NULL);

0 commit comments

Comments
 (0)