-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathconfig.c
382 lines (343 loc) · 11.2 KB
/
config.c
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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
// Copyright 2020 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "config.h"
#include <alloca.h>
#include <ctype.h>
#include <errno.h>
#include <limits.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include "base64.h"
#include "ui.h"
static bool is_empty(const char *line) {
for (; isspace(*line); line++) {
}
return *line == '\0';
}
static bool is_comment(const char *line) {
return line[0] == '#' || line[0] == ';';
}
static bool is_section(const char *line) { return line[0] == '['; }
static bool is_name(const char *name) {
const char *p;
for (p = name; isalnum(*p) || *p == '_' || *p == '-'; p++) {
}
return *p == '\0' && p - name > 0;
}
static char *section_name(char *line) {
char *p;
for (p = line + 1; *p != ']' && *p != '\0'; p++) {
}
if (*p != ']') {
return NULL;
}
char *end = p;
if (!is_empty(p + 1)) {
return NULL;
}
*end = '\0';
if (is_name(line + 1)) {
return line + 1;
}
return NULL;
}
static void key_value(char *line, char **key, char **val) {
*key = NULL;
*val = NULL;
char *p;
for (p = line; !isspace(*p) && *p != '=' && *p != '\0'; p++) {
}
if (*p == '\0') {
return;
}
char *end = p;
for (; isspace(*p); p++) {
}
if (*p != '=') {
return;
}
for (p++; isspace(*p); p++) {
}
if (*p == '\0') {
return;
}
*end = '\0';
if (!is_name(line)) {
return;
}
// Trim whitespace at the end of the value.
int k = strlen(p) - 1;
for (; k >= 0 && isspace(p[k]); k--) {
}
p[k + 1] = '\0';
*key = line;
*val = p;
}
bool glome_login_parse_public_key(const char *encoded_key, uint8_t *public_key,
size_t public_key_size) {
if (public_key_size < GLOME_MAX_PUBLIC_KEY_LENGTH) {
errorf("ERROR: provided buffer has size %zu, need at least %d\n",
public_key_size, GLOME_MAX_PUBLIC_KEY_LENGTH);
return false;
}
size_t prefix_length = strlen(GLOME_LOGIN_PUBLIC_KEY_ID);
if (strncmp(encoded_key, GLOME_LOGIN_PUBLIC_KEY_ID, prefix_length)) {
errorf("ERROR: unsupported public key encoding: %s\n", encoded_key);
return false;
}
// Advance to the start of the base64-encoded key.
encoded_key += prefix_length;
while (*encoded_key != '\0' && isblank(*encoded_key)) {
encoded_key++;
}
// Truncate the encoded string to allow for appended comments.
size_t encoded_length = 0;
while (isgraph(encoded_key[encoded_length])) {
encoded_length++;
}
// Unfortunately we need an extra byte because 32B don't pack cleanly in
// base64.
uint8_t buf[GLOME_MAX_PUBLIC_KEY_LENGTH + 1] = {0};
size_t b = base64url_decode((uint8_t *)encoded_key, encoded_length, buf,
sizeof(buf));
if (b != GLOME_MAX_PUBLIC_KEY_LENGTH) {
errorf("ERROR: public key decoded to %zu bytes, expected %d\n", b,
GLOME_MAX_PUBLIC_KEY_LENGTH);
return false;
}
memcpy(public_key, buf, GLOME_MAX_PUBLIC_KEY_LENGTH);
return true;
}
static status_t assign_string_option(const char **option, const char *val) {
const char *copy = strdup(val);
if (copy == NULL) {
return status_createf("ERROR: failed to allocate memory for value: %s",
val);
}
*option = copy;
return STATUS_OK;
}
static status_t assign_positive_int_option(unsigned int *option,
const char *val) {
char *end;
errno = 0;
unsigned long n = strtoul(val, &end, 0); // NOLINT(runtime/int)
if (errno || val == end || *end != '\0' || n > UINT_MAX) {
return status_createf("ERROR: invalid value for option: %s", val);
}
*option = (unsigned int)n;
return STATUS_OK;
}
static status_t set_bitfield_option(glome_login_config_t *config, uint8_t bit) {
config->options |= bit;
return STATUS_OK;
}
static status_t clear_bitfield_option(glome_login_config_t *config,
uint8_t bit) {
config->options &= ~bit;
return STATUS_OK;
}
static bool boolean_true(const char *val) {
if (strcasecmp(val, "true") == 0) {
return true;
} else if (strcasecmp(val, "yes") == 0) {
return true;
} else if (strcasecmp(val, "on") == 0) {
return true;
} else if (strcmp(val, "1") == 0) {
return true;
}
return false;
}
static bool boolean_false(const char *val) {
if (strcasecmp(val, "false") == 0) {
return true;
} else if (strcasecmp(val, "no") == 0) {
return true;
} else if (strcasecmp(val, "off") == 0) {
return true;
} else if (strcmp(val, "0") == 0) {
return true;
}
return false;
}
static status_t update_bitfield_option(glome_login_config_t *config,
uint8_t bit, bool invert,
const char *val) {
if (boolean_true(val)) {
if (invert) {
return clear_bitfield_option(config, bit);
} else {
return set_bitfield_option(config, bit);
}
} else if (boolean_false(val)) {
if (invert) {
return set_bitfield_option(config, bit);
} else {
return clear_bitfield_option(config, bit);
}
} else {
return status_createf("ERROR: unrecognized boolean value: %s", val);
}
}
static status_t assign_key_option(uint8_t *dest, size_t dest_len,
const char *val) {
if (is_zeroed(dest, dest_len)) {
if (decode_hex(dest, dest_len, val)) {
return status_createf("ERROR: failed to hex decode service key: %s", val);
}
}
return STATUS_OK;
}
static status_t assign_key_version_option(glome_login_config_t *config,
const char *val) {
char *end;
errno = 0;
unsigned long n = strtoul(val, &end, 0); // NOLINT(runtime/int)
if (errno || val == end || *end != '\0' || n > 127) {
return status_createf("ERROR: '%s' is not a valid key version (0..127)",
val);
}
config->service_key_id = (unsigned int)n;
return STATUS_OK;
}
static status_t assign_default_option(glome_login_config_t *config,
const char *key, const char *val) {
if (strcmp(key, "auth-delay") == 0) {
return assign_positive_int_option(&config->auth_delay_sec, val);
} else if (strcmp(key, "input-timeout") == 0) {
return assign_positive_int_option(&config->input_timeout_sec, val);
} else if (strcmp(key, "config-path") == 0) {
return assign_string_option(&config->config_path, val);
} else if (strcmp(key, "ephemeral-key") == 0) {
return assign_key_option(config->secret_key, sizeof config->secret_key,
val);
} else if (strcmp(key, "min-authcode-len") == 0) {
return assign_positive_int_option(&config->min_authcode_len, val);
} else if (strcmp(key, "host-id") == 0) {
return assign_string_option(&config->host_id, val);
} else if (strcmp(key, "host-id-type") == 0) {
return assign_string_option(&config->host_id_type, val);
} else if (strcmp(key, "login-path") == 0) {
return assign_string_option(&config->login_path, val);
} else if (strcmp(key, "disable-syslog") == 0) {
return update_bitfield_option(config, SYSLOG, true, val);
} else if (strcmp(key, "print-secrets") == 0) {
return update_bitfield_option(config, INSECURE, false, val);
} else if (strcmp(key, "timeout") == 0) {
return assign_positive_int_option(&config->input_timeout_sec, val);
} else if (strcmp(key, "verbose") == 0) {
return update_bitfield_option(config, VERBOSE, false, val);
}
return status_createf("ERROR: unrecognized default option: %s", key);
}
static status_t assign_service_option(glome_login_config_t *config,
const char *key, const char *val) {
if (strcmp(key, "key") == 0) {
return assign_key_option(config->service_key, sizeof config->service_key,
val);
} else if (strcmp(key, "key-version") == 0) {
return assign_key_version_option(config, val);
} else if (strcmp(key, "url-prefix") == 0) {
// `url-prefix` support is provided only for backwards-compatiblity
// TODO: to be removed in the 1.0 release
size_t len = strlen(val);
char *url_prefix = malloc(len + 2);
if (url_prefix == NULL) {
return status_createf("ERROR: failed to allocate memory for url_prefix");
}
strncpy(url_prefix, val, len + 1);
url_prefix[len] = '/';
url_prefix[len + 1] = '\0';
config->prompt = url_prefix;
return STATUS_OK;
} else if (strcmp(key, "prompt") == 0) {
return assign_string_option(&config->prompt, val);
} else if (strcmp(key, "public-key") == 0) {
if (!glome_login_parse_public_key(val, config->service_key,
sizeof(config->service_key))) {
return status_createf("ERROR: failed to decode public-key");
}
return STATUS_OK;
}
return status_createf("ERROR: unrecognized service option: %s", key);
}
status_t glome_login_assign_config_option(glome_login_config_t *config,
const char *section, const char *key,
const char *val) {
if (section == NULL) {
return status_createf("ERROR: section name not set");
}
if (strcmp(section, "service") == 0) {
return assign_service_option(config, key, val);
} else if (strcmp(section, "default") == 0) {
return assign_default_option(config, key, val);
}
return status_createf("ERROR: section name not recognized: %s", section);
}
status_t glome_login_parse_config_file(glome_login_config_t *config) {
bool required = config->config_path != NULL;
if (!required) {
config->config_path = DEFAULT_CONFIG_FILE;
}
FILE *f = fopen(config->config_path, "r");
if (f == NULL) {
if (!required) {
return 0;
}
return status_createf("ERROR: config file could not be opened: %s\n",
strerror(errno));
}
char *line = NULL;
char *section = NULL;
char *key, *val;
size_t len = 0;
size_t lines = 0;
status_t status = STATUS_OK;
while (getline(&line, &len, f) != -1) {
lines++;
if (is_empty(line) || is_comment(line)) {
continue;
} else if (is_section(line)) {
char *s = section_name(line);
if (s == NULL) {
status = status_createf(
"ERROR: config file parsing failed in line %ld (bad section "
"name)\n",
lines);
break;
}
free(section);
section = strdup(s);
} else {
key_value(line, &key, &val);
if (key == NULL || val == NULL) {
status = status_createf(
"ERROR: config file parsing failed in line %ld (bad key/value)\n",
lines);
break;
}
status = glome_login_assign_config_option(
config, section ? section : "default", key, val);
if (status != STATUS_OK) {
break;
}
}
}
free(line);
free(section);
fclose(f);
return status;
}