Skip to content

Commit 6d5395c

Browse files
committed
clients/authconf.{c,h}, docs/man/upscli_read_authconf.txt: add searching for default nutauth.conf in user home or NUT_CONFPATH locations [#3329]
Signed-off-by: Jim Klimov <jimklimov+nut@gmail.com>
1 parent b9f2ce1 commit 6d5395c

3 files changed

Lines changed: 58 additions & 6 deletions

File tree

clients/authconf.c

Lines changed: 48 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include <stdio.h>
1818
#include <stdlib.h>
1919
#include <string.h>
20+
#include <sys/stat.h>
2021

2122
static upscli_authconf_t *authconf_list = NULL;
2223
static upscli_authconf_t *current_section = NULL;
@@ -573,14 +574,56 @@ int upscli_read_authconf(const char *filename, int fatal_errors)
573574
{
574575
char fn[NUT_PATH_MAX + 1];
575576

576-
if (!filename) {
577-
snprintf(fn, sizeof(fn), "%s/nutauth.conf", confpath());
578-
filename = fn;
579-
}
580-
581577
/* Ensure we start fresh if called multiple times */
582578
upscli_free_authconf_list();
583579

580+
if (!filename) {
581+
/* Select a starting point - whichever default expected file exists;
582+
* it may INCLUDE further files as wanted by user or site sysadmin.
583+
*/
584+
struct stat st;
585+
char *s = NULL;
586+
587+
s = getenv("HOME");
588+
if (s) {
589+
if (snprintf(fn, sizeof(fn), "%s/.config/nut/nutauth.conf", s) > 0) {
590+
if (stat(fn, &st) == 0) {
591+
filename = fn;
592+
goto found;
593+
}
594+
upsdebugx(5, "%s: tried to default '%s' but it was not there", __func__, fn);
595+
}
596+
597+
if (snprintf(fn, sizeof(fn), "%s/.nutauth.conf", s) > 0) {
598+
if (stat(fn, &st) == 0) {
599+
filename = fn;
600+
goto found;
601+
}
602+
upsdebugx(5, "%s: tried to default '%s' but it was not there", __func__, fn);
603+
}
604+
}
605+
606+
if (snprintf(fn, sizeof(fn), "%s/nutauth.conf", confpath()) > 0) {
607+
if (stat(fn, &st) == 0) {
608+
filename = fn;
609+
goto found;
610+
}
611+
upsdebugx(5, "%s: tried to default '%s' but it was not there", __func__, fn);
612+
}
613+
614+
found:
615+
if (filename) {
616+
upsdebugx(1, "%s: defaulted to %s", __func__, filename);
617+
} else {
618+
if (fatal_errors) {
619+
fatalx(EXIT_FAILURE, "Can't open a user/site-provided default nutauth.conf file");
620+
} else {
621+
upslogx(LOG_WARNING, "Can't open a user/site-provided default nutauth.conf file");
622+
return -1;
623+
}
624+
}
625+
}
626+
584627
return parse_authconf_file(filename, fatal_errors, 1);
585628
}
586629

clients/authconf.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,10 @@ upscli_authconf_t *upscli_free_authconf(upscli_authconf_t *node);
4545
void upscli_free_authconf_list(void);
4646

4747
/** Read the authentication configuration file (usually nutauth.conf)
48-
* returns -1 on error, 1 on success
48+
* If filename==NULL, tries to locate per-user ${HOME}/.config/nut/nutauth.conf
49+
* and ${HOME}/.nutauth.conf, or site default ${nutconfdir}/nutauth.conf
50+
* (whichever is found first); then one can follow `INCLUDE` trail if needed.
51+
* Returns -1 on error, 1 on success
4952
*/
5053
int upscli_read_authconf(const char *filename, int fatal_errors);
5154

docs/man/upscli_read_authconf.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,12 @@ The *upscli_read_authconf()* function reads the specified 'filename' (which
2222
is usually the path to *nutauth.conf*) and populates an internal list of
2323
authentication and SSL configurations.
2424

25+
If 'filename' is `NULL`, the function tries to locate either a per-user
26+
`${HOME}/.config/nut/nutauth.conf` or `${HOME}/.nutauth.conf`, or a site
27+
default `${NUT_CONFDIR}/nutauth.conf` (whichever is found first). Such a
28+
file may `INCLUDE` further configurations (e.g. hop from a per-user file
29+
to load server-wide defaults) if desired.
30+
2531
The file structure is similar to *ups.conf*, with global defaults and
2632
per-server sections named like `[@localhost:12345]` for host defaults,
2733
or `[username@localhost:12345]` for specific account overrides.

0 commit comments

Comments
 (0)