-
Notifications
You must be signed in to change notification settings - Fork 3
/
airscan-conf.c
494 lines (425 loc) · 12.9 KB
/
airscan-conf.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
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
/* AirScan (a.k.a. eSCL) backend for SANE
*
* Copyright (C) 2019 and up by Alexander Pevzner (pzz@apevzner.com)
* See LICENSE for license terms and conditions
*
* Configuration file loader
*/
#include "airscan.h"
#include <dirent.h>
#include <stdlib.h>
#include <string.h>
#include <arpa/inet.h>
/* Configuration data
*/
conf_data conf = CONF_INIT;
static conf_data conf_init = CONF_INIT;
/* Revert conf.devices list
*/
static void
conf_device_list_revert (void)
{
conf_device *list = conf.devices, *prev = NULL, *next;
while (list != NULL) {
next = list->next;
list->next = prev;
prev = list;
list = next;
}
conf.devices = prev;
}
/* Free conf.devices list
*/
static void
conf_device_list_free (void)
{
conf_device *list = conf.devices, *next;
conf.devices = NULL;
while (list != NULL) {
next = list->next;
mem_free((char*) list->name);
http_uri_free(list->uri);
devid_free(list->devid);
mem_free(list);
list = next;
}
}
/* Prepend device conf.devices list
*/
static void
conf_device_list_prepend (const char *name, http_uri *uri, ID_PROTO proto)
{
conf_device *dev = mem_new(conf_device, 1);
dev->name = str_dup(name);
dev->devid = devid_alloc();
dev->proto = proto;
dev->uri = uri;
dev->next = conf.devices;
conf.devices = dev;
}
/* Find device in conf.devices list
*/
static conf_device*
conf_device_list_lookup (const char *name) {
conf_device *dev = conf.devices;
while (dev != NULL && strcmp(dev->name, name)) {
dev = dev->next;
}
return dev;
}
/* Revert conf.blacklist list
*/
static void
conf_blacklist_revert (void)
{
conf_blacklist *list = conf.blacklist, *prev = NULL, *next;
while (list != NULL) {
next = list->next;
list->next = prev;
prev = list;
list = next;
}
conf.blacklist = prev;
}
/* Free conf.blacklist
*/
static void
conf_blacklist_free (void)
{
while (conf.blacklist != NULL) {
conf_blacklist *next = conf.blacklist->next;
mem_free((char*) conf.blacklist->name);
mem_free((char*) conf.blacklist->model);
conf.blacklist = next;
}
}
/* Expand path name. The returned string must be eventually
* released with mem_free()
*/
static const char*
conf_expand_path (const char *path)
{
const char *prefix = "";
char *ret;
if (path[0] == '~' && (path[1] == '\0' || path[1] == '/')) {
const char *home = os_homedir();
if (home != NULL) {
prefix = home;
path ++;
} else {
return NULL;
}
}
ret = str_concat(prefix, path, NULL);
ret = str_terminate(ret, '/');
return ret;
}
/* Report configuration file error
*/
static void
conf_perror (const inifile_record *rec, const char *format, ...)
{
char buf[1024];
va_list ap;
va_start(ap, format);
vsnprintf(buf, sizeof(buf), format, ap);
va_end(ap);
log_debug(NULL, "%s:%d: %s", rec->file, rec->line, buf);
}
/* Decode a device configuration
*/
static void
conf_decode_device (const inifile_record *rec) {
http_uri *uri = NULL;
ID_PROTO proto = ID_PROTO_ESCL;
const char *uri_name = rec->tokc > 0 ? rec->tokv[0] : NULL;
const char *proto_name = rec->tokc > 1 ? rec->tokv[1] : NULL;
if (conf_device_list_lookup(rec->variable) != NULL) {
conf_perror(rec, "device already defined");
} else if (!strcmp(rec->value, CONF_DEVICE_DISABLE)) {
conf_device_list_prepend(rec->variable, NULL, ID_PROTO_UNKNOWN);
} else if (rec->tokc != 1 && rec->tokc != 2) {
conf_perror(rec, "usage: \"device name\" = URL[,protocol]");
} else if ((uri = http_uri_new(uri_name, true)) == NULL) {
conf_perror(rec, "invalid URL");
} else if (proto_name != NULL&&
(proto = id_proto_by_name(proto_name)) == ID_PROTO_UNKNOWN) {
conf_perror(rec, "protocol must be \"escl\" or \"wsd\"");
}
if (uri != NULL && proto != ID_PROTO_UNKNOWN) {
conf_device_list_prepend(rec->variable, uri, proto);
} else {
http_uri_free(uri);
}
}
/* Parse binary option
*/
static void
conf_load_bool (const inifile_record *rec, bool *out,
const char *n_true, const char *n_false)
{
if (inifile_match_name(rec->value, n_true)) {
*out = true;
} else if (inifile_match_name(rec->value, n_false)) {
*out = false;
} else {
conf_perror(rec, "usage: %s = %s | %s", rec->variable, n_true, n_false);
}
}
/* Parse network address with mask
*/
static void
conf_load_netaddr (const inifile_record *rec, ip_network *net)
{
char *addr, *mask;
int af;
int maxmask;
memset(net, 0, sizeof(*net));
/* Split into address and mask */
addr = alloca(strlen(rec->value) + 1);
strcpy(addr, rec->value);
mask = strchr(addr, '/');
if (mask != NULL) {
*mask = '\0';
mask ++;
}
/* Parse address */
if (strchr(addr, ':') == NULL) {
af = AF_INET;
maxmask = 32;
} else {
af = AF_INET6;
maxmask = 128;
}
if (inet_pton(af, addr, &net->addr.ip) != 1) {
conf_perror(rec, "invalid IP address %s", addr);
return;
}
/* Parse mask, if any */
if (mask != NULL) {
unsigned long l;
char *end;
l = strtoul(mask, &end, 10);
if (end == mask || *end != '\0') {
conf_perror(rec, "invalid network mask %s", mask);
return;
}
if (l == 0 || l > (unsigned long) maxmask) {
conf_perror(rec, "network mask out of range");
return;
}
net->mask = (int) l;
} else {
net->mask = maxmask;
}
/* Indicate success; all other return values already filled */
net->addr.af = af;
}
/* Load configuration from opened inifile
*/
static void
conf_load_from_ini (inifile *ini)
{
const inifile_record *rec;
while ((rec = inifile_read(ini)) != NULL) {
switch (rec->type) {
case INIFILE_SYNTAX:
conf_perror(rec, "syntax error");
break;
case INIFILE_VARIABLE:
if (inifile_match_name(rec->section, "devices")) {
conf_decode_device(rec);
} else if (inifile_match_name(rec->section, "options")) {
if (inifile_match_name(rec->variable, "discovery")) {
conf_load_bool(rec, &conf.discovery, "enable", "disable");
} else if (inifile_match_name(rec->variable, "model")) {
conf_load_bool(rec, &conf.model_is_netname,
"network", "hardware");
} else if (inifile_match_name(rec->variable, "protocol")) {
conf_load_bool(rec, &conf.proto_auto, "auto", "manual");
} else if (inifile_match_name(rec->variable, "ws-discovery")) {
if (inifile_match_name(rec->value, "fast")) {
conf.wsdd_mode = WSDD_FAST;
} else if (inifile_match_name(rec->value, "full")) {
conf.wsdd_mode = WSDD_FULL;
} else if (inifile_match_name(rec->value, "off")) {
conf.wsdd_mode = WSDD_OFF;
} else {
conf_perror(rec, "usage: %s = fast | full | off",
rec->variable);
}
} else if (inifile_match_name(rec->variable, "socket_dir")) {
mem_free((char*) conf.socket_dir);
conf.socket_dir = conf_expand_path(rec->value);
if (conf.socket_dir == NULL) {
conf_perror(rec, "failed to expand socket_dir path");
}
}
} else if (inifile_match_name(rec->section, "debug")) {
if (inifile_match_name(rec->variable, "trace")) {
mem_free((char*) conf.dbg_trace);
conf.dbg_trace = conf_expand_path(rec->value);
if (conf.dbg_trace == NULL) {
conf_perror(rec, "failed to expand path");
}
} else if (inifile_match_name(rec->variable, "enable")) {
conf_load_bool(rec, &conf.dbg_enabled, "true", "false");
} else if (inifile_match_name(rec->variable, "hexdump")) {
conf_load_bool(rec, &conf.dbg_hexdump, "true", "false");
}
} else if (inifile_match_name(rec->section, "blacklist")) {
conf_blacklist *ent = NULL;
if (inifile_match_name(rec->variable, "name")) {
ent = mem_new(conf_blacklist, 1);
ent->name = str_dup(rec->value);
} else if (inifile_match_name(rec->variable, "model")) {
ent = mem_new(conf_blacklist, 1);
ent->model = str_dup(rec->value);
} else if (inifile_match_name(rec->variable, "ip")) {
ip_network net;
conf_load_netaddr(rec, &net);
if (net.addr.af != AF_UNSPEC) {
ent = mem_new(conf_blacklist, 1);
ent->net = net;
}
}
if (ent != NULL) {
ent->next = conf.blacklist;
conf.blacklist = ent;
}
}
break;
default:
break;
}
}
/* Trace implies console log
*/
if (conf.dbg_trace != NULL) {
conf.dbg_enabled = true;
}
}
/* Load configuration from the particular file
*/
static void
conf_load_from_file (const char *name)
{
log_debug(NULL, "loading configuration file %s", name);
inifile *ini = inifile_open(name);
if (ini != NULL) {
conf_load_from_ini(ini);
inifile_close(ini);
}
}
/* Load configuration from the specified directory
*
* This function uses its path parameter as its temporary
* buffer and doesn't guarantee to preserve its content
*
* The `path' can be reallocated by this function; old
* value is consumed and new is returned
*/
static char*
conf_load_from_dir (char *path)
{
path = str_terminate(path, '/');
/* Load from CONFIG_AIRSCAN_CONF file */
size_t len = mem_len(path);
path = str_append(path, CONFIG_AIRSCAN_CONF);
conf_load_from_file(path);
/* Scan CONFIG_AIRSCAN_D directory */
path = str_resize(path, len);
path = str_append(path, CONFIG_AIRSCAN_D);
path = str_terminate(path, '/');
len = mem_len(path);
DIR *dir = opendir(path);
if (dir) {
struct dirent *ent;
while ((ent = readdir(dir)) != NULL) {
path = str_resize(path, len);
path = str_append(path, ent->d_name);
conf_load_from_file(path);
}
closedir(dir);
}
return path;
}
/* Load configuration from environment
*/
static void
conf_load_from_env (void)
{
const char *env;
env = getenv(CONFIG_ENV_AIRSCAN_DEBUG);
if (env != NULL) {
if (inifile_match_name(env, "true")) {
conf.dbg_enabled = true;
} else if (inifile_match_name(env, "false")) {
conf.dbg_enabled = false;
} else {
unsigned long v;
char *end;
v = strtoul(env, &end, 0);
if (env != end && *end == '\0') {
conf.dbg_enabled = v != 0;
} else {
log_debug(NULL, "usage: %s=true|false",
CONFIG_ENV_AIRSCAN_DEBUG);
}
}
}
}
/* Load configuration. Returns non-NULL (default configuration)
* even if configuration file cannot be loaded
*/
void
conf_load (void)
{
char *dir_list = str_new();
char *path = str_new();
char *s;
/* Reset the configuration */
conf = conf_init;
conf.socket_dir = str_dup(CONFIG_DEFAULT_SOCKET_DIR);
/* Look to configuration path in environment */
s = getenv(CONFIG_PATH_ENV);
if (s != NULL) {
dir_list = str_assign(dir_list, s);
}
/* Append default directories */
dir_list = str_terminate(dir_list, ':');
dir_list = str_append(dir_list, CONFIG_SANE_CONFIG_DIR);
/* Iterate over the dir_list */
for (s = dir_list; ; s ++) {
if (*s == ':' || *s == '\0') {
path = conf_load_from_dir(path);
str_trunc(path);
} else {
path = str_append_c(path, *s);
}
if (*s == '\0') {
break;
}
}
/* Load configuration from environment */
conf_load_from_env();
/* Cleanup and exit */
conf_device_list_revert();
conf_blacklist_revert();
mem_free(dir_list);
mem_free(path);
}
/* Free resources, allocated by conf_load, and reset configuration
* data into initial state
*/
void
conf_unload (void)
{
conf_device_list_free();
conf_blacklist_free();
mem_free((char*) conf.dbg_trace);
mem_free((char*) conf.socket_dir);
conf = conf_init;
}
/* vim:ts=8:sw=4:et
*/