aboutsummaryrefslogtreecommitdiff
path: root/src/accfile.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/accfile.c')
-rw-r--r--src/accfile.c80
1 files changed, 69 insertions, 11 deletions
diff --git a/src/accfile.c b/src/accfile.c
index 0c4912e..1d39717 100644
--- a/src/accfile.c
+++ b/src/accfile.c
@@ -15,6 +15,7 @@
along with Eclat. If not, see <http://www.gnu.org/licenses/>. */
#include "eclat.h"
+#include <glob.h>
char *access_file_name;
@@ -27,21 +28,27 @@ skipline(FILE *fp)
;
}
-int
-access_file_lookup(const char *id, char **access_key_ptr, char **secret_key_ptr)
+static int
+access_file_lookup(const char *filename,
+ const char *id, char **access_key_ptr, char **secret_key_ptr)
{
int rc;
int c;
FILE *fp;
size_t line = 0;
struct grecs_txtacc *acc;
-
- if (!access_file_name)
- return 1;
- fp = fopen(access_file_name, "r");
+ const char *p;
+
+ debug(ECLAT_DEBCAT_MAIN, 1, ("trying file %s", filename));
+ fp = fopen(filename, "r");
if (!fp) {
- err("cannot open \"%s\": %s",
- access_file_name, strerror(errno));
+ if (errno == EACCES)
+ debug(ECLAT_DEBCAT_MAIN, 1,
+ ("cannot open \"%s\": %s",
+ filename, strerror(errno)));
+ else
+ err("cannot open \"%s\": %s",
+ filename, strerror(errno));
return 1;
}
@@ -52,14 +59,30 @@ access_file_lookup(const char *id, char **access_key_ptr, char **secret_key_ptr)
if (c == '\n')
continue;
if (c == '#') {
+ if (id) {
+ c = getc(fp);
+ if (c == ':') {
+ p = id;
+ while (*p && (c = getc(fp)) == *p)
+ p++;
+ if (*p == 0 &&
+ ((c = getc(fp)) == ' ' ||
+ c == '\t' ||
+ c == '\n')) {
+ id = NULL;
+ if (c != '\n')
+ skipline(fp);
+ continue;
+ }
+ } else
+ ungetc(c, fp);
+ }
skipline(fp);
continue;
}
if (c == EOF)
break;
if (id) {
- const char *p;
-
for (p = id; c != EOF && c != ':' && *p == c; p++)
c = getc(fp);
if (c == EOF)
@@ -75,7 +98,7 @@ access_file_lookup(const char *id, char **access_key_ptr, char **secret_key_ptr)
while (c != EOF && c != ':') {
if (c == '\n') {
err("%s:%u: incomplete line",
- access_file_name, line);
+ filename, line);
break;
}
grecs_txtacc_grow_char(acc, c);
@@ -91,6 +114,8 @@ access_file_lookup(const char *id, char **access_key_ptr, char **secret_key_ptr)
}
if (c == ':') {
+ debug(ECLAT_DEBCAT_MAIN, 1, ("%s:%u: found credentials",
+ filename, line));
acc = grecs_txtacc_create();
while ((c = getc(fp)) != EOF && c != '\n')
grecs_txtacc_grow_char(acc, c);
@@ -104,3 +129,36 @@ access_file_lookup(const char *id, char **access_key_ptr, char **secret_key_ptr)
return rc;
}
+
+static int
+globerrfunc(const char *epath, int eerrno)
+{
+ err("%s: %s", epath, strerror(eerrno));
+ return 1;
+}
+
+int
+get_access_creds(const char *id, char **access_key_ptr, char **secret_key_ptr)
+{
+ int i;
+ glob_t g;
+ int rc = 1;
+
+ debug(ECLAT_DEBCAT_MAIN, 1, ("Looking for authentication credentials"));
+ switch (glob(access_file_name, 0, globerrfunc, &g)) {
+ case 0:
+ break;
+ case GLOB_NOSPACE:
+ grecs_alloc_die();
+ default:
+ err("globbing error");
+ return 1;
+ }
+
+ for (i = 0; rc != 0 && i < g.gl_pathc; i++)
+ rc = access_file_lookup(g.gl_pathv[i], id,
+ access_key_ptr, secret_key_ptr);
+ globfree(&g);
+
+ return rc;
+}

Return to:

Send suggestions and report system problems to the System administrator.