|
17 | 17 | #include <stdio.h> |
18 | 18 | #include <stdlib.h> |
19 | 19 | #include <string.h> |
| 20 | +#include <sys/stat.h> |
20 | 21 |
|
21 | 22 | static upscli_authconf_t *authconf_list = NULL; |
22 | 23 | static upscli_authconf_t *current_section = NULL; |
@@ -573,14 +574,56 @@ int upscli_read_authconf(const char *filename, int fatal_errors) |
573 | 574 | { |
574 | 575 | char fn[NUT_PATH_MAX + 1]; |
575 | 576 |
|
576 | | - if (!filename) { |
577 | | - snprintf(fn, sizeof(fn), "%s/nutauth.conf", confpath()); |
578 | | - filename = fn; |
579 | | - } |
580 | | - |
581 | 577 | /* Ensure we start fresh if called multiple times */ |
582 | 578 | upscli_free_authconf_list(); |
583 | 579 |
|
| 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 | + |
584 | 627 | return parse_authconf_file(filename, fatal_errors, 1); |
585 | 628 | } |
586 | 629 |
|
|
0 commit comments