summaryrefslogtreecommitdiffabout
path: root/src/accfile.c
blob: 10ac1fb4b7ac910e246f2ada2407cc0d3b161bbe (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
/* This file is part of Eclat.
   Copyright (C) 2012, 2013 Sergey Poznyakoff.
 
   Eclat is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 3, or (at your option)
   any later version.
 
   Eclat is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.
 
   You should have received a copy of the GNU General Public License
   along with Eclat.  If not, see <http://www.gnu.org/licenses/>. */

#include "eclat.h"
#include <glob.h>

char *access_file_name;

static void
skipline(FILE *fp)
{
	int c;

	while ((c = getc(fp)) != EOF && c != '\n')
		;
}

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;
	const char *p;

	debug(ECLAT_DEBCAT_MAIN, 1, ("trying file %s", filename));
	fp = fopen(filename, "r");
	if (!fp) {
		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;
	}
	
	while ((c = getc(fp)) != EOF) {
		line++;
		while (c != EOF && (c == ' ' || c == '\t'))
			c = getc(fp);
		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) {
			for (p = id; c != EOF && c != ':' && *p == c; p++)
				c = getc(fp);
			if (c == EOF)
				break;
			if (c == '\n')
				continue;
			if (c != ':') {
				skipline(fp);
				continue;
			}
		} else {
			acc = grecs_txtacc_create();
			while (c != EOF && c != ':') {
				if (c == '\n') {
					err("%s:%u: incomplete line",
					    filename, line);
					break;
				}
				grecs_txtacc_grow_char(acc, c);
				c = getc(fp);
			}
			if (c == ':') {
				grecs_txtacc_grow_char(acc, 0);
				*access_key_ptr = grecs_txtacc_finish(acc, 1);
			}
			grecs_txtacc_free(acc);
		}
		break;
	}

	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);
		grecs_txtacc_grow_char(acc, 0);
		*secret_key_ptr = grecs_txtacc_finish(acc, 1);
		grecs_txtacc_free(acc);
		rc = 0;
	} else
		rc = 1;

	fclose(fp);

	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;
	
	if (!access_file_name) {
		debug(ECLAT_DEBCAT_MAIN, 1, ("no access file given"));
		return 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();
	case GLOB_NOMATCH:
		debug(ECLAT_DEBCAT_MAIN, 1, 
		      ("No file matching %s", access_file_name));
		return 1;
	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.