File: secrets.c

package info (click to toggle)
got 0.119-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 9,448 kB
  • sloc: ansic: 124,378; sh: 50,814; yacc: 4,353; makefile: 2,241; perl: 357
file content (295 lines) | stat: -rw-r--r-- 6,499 bytes parent folder | download | duplicates (2)
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
/*
 * Copyright (c) 2024 Omar Polo <op@openbsd.org>
 *
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

#include "got_compat.h"

#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "got_error.h"

#include "log.h"
#include "secrets.h"

static const struct got_error *
push(struct gotd_secrets *s, enum gotd_secret_type type, const char *label,
    const char *user, const char *pass, const char *hmac)
{
	size_t			 newcap, i;
	void			*t;

	if (s->len == s->cap) {
		newcap = s->cap + 16;
		t = reallocarray(s->secrets, newcap, sizeof(*s->secrets));
		if (t == NULL)
			return got_error_from_errno("reallocarray");
		s->secrets = t;
		s->cap = newcap;
	}

	i = s->len;
	memset(&s->secrets[i], 0, sizeof(s->secrets[i]));
	s->secrets[i].type = type;
	s->secrets[i].label = strdup(label);
	if (s->secrets[i].label == NULL)
		return got_error_from_errno("strdup");

	if (type == GOTD_SECRET_AUTH) {
		s->secrets[i].user = strdup(user);
		if (s->secrets[i].user == NULL)
			return got_error_from_errno("strdup");
		s->secrets[i].pass = strdup(pass);
		if (s->secrets[i].pass == NULL)
			return got_error_from_errno("strdup");
	} else {
		s->secrets[i].hmac = strdup(hmac);
		if (s->secrets[i].hmac == NULL)
			return got_error_from_errno("strdup");
	}

	s->len++;
	return NULL;
}

static char *
read_word(char **word, const char *path, int lineno, char *s)
{
	char			*p, quote = 0;
	int			 escape = 0;

	s += strspn(s, " \t");
	if (*s == '\0') {
		log_warnx("%s:%d syntax error", path, lineno);
		return NULL;
	}
	*word = s;

	p = s;
	while (*s) {
		if (escape) {
			escape = 0;
			*p++ = *s++;
			continue;
		}

		if (*s == '\\') {
			escape = 1;
			s++;
			continue;
		}

		if (*s == quote) {
			quote = 0;
			s++;
			continue;
		}

		if (*s == '\'' || *s == '\"') {
			quote = *s;
			s++;
			continue;
		}

		if (!quote && (*s == ' ' || *s == '\t')) {
			*p = '\0';
			return s + 1;
		}

		*p++ = *s++;
	}

	if (quote) {
		log_warnx("%s:%d no closing quote", path, lineno);
		return NULL;
	}

	if (escape) {
		log_warnx("%s:%d unterminated escape at end of line",
		    path, lineno);
		return NULL;
	}

	*p = '\0';
	return s;
}

static char *
read_keyword(char **kw, const char *path, int lineno, char *s)
{
	s += strspn(s, " \t");
	if (*s == '\0') {
		log_warnx("%s:%d syntax error", path, lineno);
		return NULL;
	}
	*kw = s;

	s += strcspn(s, " \t");
	if (*s != '\0')
		*s++ = '\0';
	return s;
}

static const struct got_error *
parse_line(struct gotd_secrets *secrets, const char *path, int lineno,
    char *line)
{
	char			*kw, *label;
	char			*user = NULL, *pass = NULL, *hmac = NULL;
	enum gotd_secret_type	 type;

	line = read_keyword(&kw, path, lineno, line);
	if (line == NULL)
		return got_error(GOT_ERR_PARSE_CONFIG);

	if (!strcmp(kw, "auth"))
		type = GOTD_SECRET_AUTH;
	else if (!strcmp(kw, "hmac"))
		type = GOTD_SECRET_HMAC;
	else {
		log_warnx("%s:%d syntax error", path, lineno);
		return got_error(GOT_ERR_PARSE_CONFIG);
	}

	line = read_word(&label, path, lineno, line);
	if (line == NULL)
		return got_error(GOT_ERR_PARSE_CONFIG);

	if (type == GOTD_SECRET_AUTH) {
		line = read_keyword(&kw, path, lineno, line);
		if (line == NULL)
			return got_error(GOT_ERR_PARSE_CONFIG);
		if (strcmp(kw, "user") != 0) {
			log_warnx("%s:%d syntax error", path, lineno);
			return got_error(GOT_ERR_PARSE_CONFIG);
		}

		line = read_word(&user, path, lineno, line);
		if (line == NULL)
			return got_error(GOT_ERR_PARSE_CONFIG);

		line = read_keyword(&kw, path, lineno, line);
		if (line == NULL)
			return got_error(GOT_ERR_PARSE_CONFIG);
		if (strcmp(kw, "password") != 0) {
			log_warnx("%s:%d syntax error", path, lineno);
			return got_error(GOT_ERR_PARSE_CONFIG);
		}

		line = read_word(&pass, path, lineno, line);
		if (line == NULL)
			return got_error(GOT_ERR_PARSE_CONFIG);
	} else {
		line = read_word(&hmac, path, lineno, line);
		if (line == NULL)
			return got_error(GOT_ERR_PARSE_CONFIG);
	}

	line += strspn(line, " \t");
	if (*line != '\0') {
		log_warnx("%s:%d syntax error", path, lineno);
		return got_error(GOT_ERR_PARSE_CONFIG);
	}

	if (gotd_secrets_get(secrets, type, label) != NULL) {
		log_warnx("%s:%d duplicate %s entry %s", path, lineno,
		    type == GOTD_SECRET_AUTH ? "auth" : "hmac", label);
		return got_error(GOT_ERR_PARSE_CONFIG);
	}

	return push(secrets, type, label, user, pass, hmac);
}

const struct got_error *
gotd_secrets_parse(const char *path, FILE *fp, struct gotd_secrets **s)
{
	const struct got_error	*err = NULL;
	int			 lineno = 0;
	char			*line = NULL;
	size_t			 linesize = 0;
	ssize_t			 linelen;
	char			*t;
	struct gotd_secrets	*secrets;

	*s = NULL;

	secrets = calloc(1, sizeof(*secrets));
	if (secrets == NULL)
		return got_error_from_errno("calloc");

	while ((linelen = getline(&line, &linesize, fp)) != -1) {
		lineno++;
		if (line[linelen - 1] == '\n')
			line[--linelen] = '\0';

		for (t = line; *t == ' ' || *t == '\t'; ++t)
			/* nop */ ;

		if (*t == '\0' || *t == '#')
			continue;

		err = parse_line(secrets, path, lineno, t);
		if (err)
			break;
	}
	free(line);
	if (ferror(fp) && err == NULL)
		err = got_error_from_errno("getline");

	if (err) {
		gotd_secrets_free(secrets);
		secrets = NULL;
	}

	*s = secrets;
	return err;
}

struct gotd_secret *
gotd_secrets_get(struct gotd_secrets *s, enum gotd_secret_type type,
    const char *label)
{
	size_t		 i;

	for (i = 0; i < s->len; ++i) {
		if (s->secrets[i].type != type)
			continue;
		if (strcmp(s->secrets[i].label, label) != 0)
			continue;
		return &s->secrets[i];
	}

	return NULL;
}

void
gotd_secrets_free(struct gotd_secrets *s)
{
	size_t		 i;

	if (s == NULL)
		return;

	for (i = 0; i < s->len; ++i) {
		free(s->secrets[i].label);
		free(s->secrets[i].user);
		free(s->secrets[i].pass);
		free(s->secrets[i].hmac);
	}

	free(s);
}