1afa612e924676a14ff711f16be3c799f57c8754
[project/opkg-lede.git] / libopkg / opkg_conf.c
1 /* opkg_conf.c - the opkg package management system
2
3 Carl D. Worth
4
5 Copyright (C) 2001 University of Southern California
6
7 This program is free software; you can redistribute it and/or
8 modify it under the terms of the GNU General Public License as
9 published by the Free Software Foundation; either version 2, or (at
10 your option) any later version.
11
12 This program is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 General Public License for more details.
16 */
17
18 #include "includes.h"
19 #include "opkg_conf.h"
20
21 #include "xregex.h"
22 #include "sprintf_alloc.h"
23 #include "args.h"
24 #include "opkg_message.h"
25 #include "file_util.h"
26 #include "opkg_defines.h"
27 #include "libbb/libbb.h"
28
29 #include <sys/types.h>
30 #include <sys/stat.h>
31 #include <fcntl.h>
32 #include <errno.h>
33 #include <glob.h>
34
35 static int lock_fd;
36
37 static opkg_conf_t _conf;
38 opkg_conf_t *conf = &_conf;
39
40 /*
41 * Config file options
42 */
43 opkg_option_t options[] = {
44 { "cache", OPKG_OPT_TYPE_STRING, &_conf.cache},
45 { "force_defaults", OPKG_OPT_TYPE_BOOL, &_conf.force_defaults },
46 { "force_maintainer", OPKG_OPT_TYPE_BOOL, &_conf.force_maintainer },
47 { "force_depends", OPKG_OPT_TYPE_BOOL, &_conf.force_depends },
48 { "force_overwrite", OPKG_OPT_TYPE_BOOL, &_conf.force_overwrite },
49 { "force_downgrade", OPKG_OPT_TYPE_BOOL, &_conf.force_downgrade },
50 { "force_reinstall", OPKG_OPT_TYPE_BOOL, &_conf.force_reinstall },
51 { "force_space", OPKG_OPT_TYPE_BOOL, &_conf.force_space },
52 { "check_signature", OPKG_OPT_TYPE_BOOL, &_conf.check_signature },
53 { "ftp_proxy", OPKG_OPT_TYPE_STRING, &_conf.ftp_proxy },
54 { "http_proxy", OPKG_OPT_TYPE_STRING, &_conf.http_proxy },
55 { "no_proxy", OPKG_OPT_TYPE_STRING, &_conf.no_proxy },
56 { "test", OPKG_OPT_TYPE_BOOL, &_conf.noaction },
57 { "noaction", OPKG_OPT_TYPE_BOOL, &_conf.noaction },
58 { "nodeps", OPKG_OPT_TYPE_BOOL, &_conf.nodeps },
59 { "offline_root", OPKG_OPT_TYPE_STRING, &_conf.offline_root },
60 { "proxy_passwd", OPKG_OPT_TYPE_STRING, &_conf.proxy_passwd },
61 { "proxy_user", OPKG_OPT_TYPE_STRING, &_conf.proxy_user },
62 { "query-all", OPKG_OPT_TYPE_BOOL, &_conf.query_all },
63 { "tmp_dir", OPKG_OPT_TYPE_STRING, &_conf.tmp_dir },
64 { "verbosity", OPKG_OPT_TYPE_INT, &_conf.verbosity },
65 #if defined(HAVE_OPENSSL)
66 { "signature_ca_file", OPKG_OPT_TYPE_STRING, &_conf.signature_ca_file },
67 { "signature_ca_path", OPKG_OPT_TYPE_STRING, &_conf.signature_ca_path },
68 #endif
69 #if defined(HAVE_PATHFINDER)
70 { "check_x509_path", OPKG_OPT_TYPE_BOOL, &_conf.check_x509_path },
71 #endif
72 #if defined(HAVE_SSLCURL) && defined(HAVE_CURL)
73 { "ssl_engine", OPKG_OPT_TYPE_STRING, &_conf.ssl_engine },
74 { "ssl_cert", OPKG_OPT_TYPE_STRING, &_conf.ssl_cert },
75 { "ssl_cert_type", OPKG_OPT_TYPE_STRING, &_conf.ssl_cert_type },
76 { "ssl_key", OPKG_OPT_TYPE_STRING, &_conf.ssl_key },
77 { "ssl_key_type", OPKG_OPT_TYPE_STRING, &_conf.ssl_key_type },
78 { "ssl_key_passwd", OPKG_OPT_TYPE_STRING, &_conf.ssl_key_passwd },
79 { "ssl_ca_file", OPKG_OPT_TYPE_STRING, &_conf.ssl_ca_file },
80 { "ssl_ca_path", OPKG_OPT_TYPE_STRING, &_conf.ssl_ca_path },
81 { "ssl_dont_verify_peer", OPKG_OPT_TYPE_BOOL, &_conf.ssl_dont_verify_peer },
82 #endif
83 { NULL, 0, NULL }
84 };
85
86 static int
87 resolve_pkg_dest_list(nv_pair_list_t *nv_pair_list, const char *default_dest_name)
88 {
89 nv_pair_list_elt_t *iter;
90 nv_pair_t *nv_pair;
91 pkg_dest_t *dest;
92 char *root_dir;
93
94 for (iter = nv_pair_list_first(nv_pair_list); iter; iter = nv_pair_list_next(nv_pair_list, iter)) {
95 nv_pair = (nv_pair_t *)iter->data;
96
97 if (conf->offline_root) {
98 sprintf_alloc(&root_dir, "%s%s", conf->offline_root, nv_pair->value);
99 } else {
100 root_dir = xstrdup(nv_pair->value);
101 }
102
103 dest = pkg_dest_list_append(&conf->pkg_dest_list, nv_pair->name, root_dir, conf->lists_dir);
104 free(root_dir);
105
106 if (conf->default_dest == NULL)
107 conf->default_dest = dest;
108
109 if (default_dest_name && !strcmp(dest->name, default_dest_name)) {
110 conf->default_dest = dest;
111 conf->restrict_to_default_dest = 1;
112 }
113 }
114
115 if (default_dest_name && !conf->restrict_to_default_dest) {
116 opkg_msg(ERROR, "Unknown dest name: `%s'.\n", default_dest_name);
117 return -1;
118 }
119
120 return 0;
121 }
122
123 static int
124 opkg_conf_set_option(const char *name, const char *value)
125 {
126 int i = 0;
127 while (options[i].name) {
128 if (strcmp(options[i].name, name) == 0) {
129 switch (options[i].type) {
130 case OPKG_OPT_TYPE_BOOL:
131 if (*(int *)options[i].value) {
132 opkg_msg(ERROR, "Duplicate boolean option %s, "
133 "leaving this option on.\n", name);
134 return 0;
135 }
136 *((int * const)options[i].value) = 1;
137 return 0;
138 case OPKG_OPT_TYPE_INT:
139 if (value) {
140 if (*(int *)options[i].value) {
141 opkg_msg(ERROR, "Duplicate option %s, "
142 "using first seen value \"%d\".\n",
143 name, *((int *)options[i].value));
144 return 0;
145 }
146 *((int * const)options[i].value) = atoi(value);
147 return 0;
148 } else {
149 opkg_msg(ERROR, "Option %s needs an argument\n",
150 name);
151 return -1;
152 }
153 case OPKG_OPT_TYPE_STRING:
154 if (value) {
155 if (*(char **)options[i].value) {
156 opkg_msg(ERROR, "Duplicate option %s, "
157 "using first seen value \"%s\".\n",
158 name, *((char **)options[i].value));
159 return 0;
160 }
161 *((char ** const)options[i].value) = xstrdup(value);
162 return 0;
163 } else {
164 opkg_msg(ERROR, "Option %s needs an argument\n",
165 name);
166 return -1;
167 }
168 }
169 }
170 i++;
171 }
172
173 opkg_msg(ERROR, "Unrecognized option: %s=%s\n", name, value);
174 return -1;
175 }
176
177 static int
178 opkg_conf_parse_file(const char *filename,
179 pkg_src_list_t *pkg_src_list,
180 nv_pair_list_t *tmp_dest_nv_pair_list)
181 {
182 int err;
183 FILE *file;
184 regex_t valid_line_re, comment_re;
185 #define regmatch_size 12
186 regmatch_t regmatch[regmatch_size];
187
188 file = fopen(filename, "r");
189 if (file == NULL) {
190 opkg_perror(ERROR, "Failed to open %s", filename);
191 return -1;
192 }
193
194 opkg_msg(INFO, "Loading conf file %s.\n", filename);
195
196 err = xregcomp(&comment_re,
197 "^[[:space:]]*(#.*|[[:space:]]*)$",
198 REG_EXTENDED);
199 if (err) {
200 return -1;
201 }
202 err = xregcomp(&valid_line_re, "^[[:space:]]*(\"([^\"]*)\"|([^[:space:]]*))[[:space:]]*(\"([^\"]*)\"|([^[:space:]]*))[[:space:]]*(\"([^\"]*)\"|([^[:space:]]*))([[:space:]]+([^[:space:]]+))?[[:space:]]*$", REG_EXTENDED);
203 if (err) {
204 return -1;
205 }
206
207 while(1) {
208 int line_num = 0;
209 char *line;
210 char *type, *name, *value, *extra;
211
212 line = file_read_line_alloc(file);
213 line_num++;
214 if (line == NULL) {
215 break;
216 }
217
218 if (regexec(&comment_re, line, 0, 0, 0) == 0) {
219 goto NEXT_LINE;
220 }
221
222 if (regexec(&valid_line_re, line, regmatch_size, regmatch, 0) == REG_NOMATCH) {
223 opkg_msg(ERROR, "%s:%d: Ignoring invalid line: `%s'\n",
224 filename, line_num, line);
225 goto NEXT_LINE;
226 }
227
228 /* This has to be so ugly to deal with optional quotation marks */
229 if (regmatch[2].rm_so > 0) {
230 type = xstrndup(line + regmatch[2].rm_so,
231 regmatch[2].rm_eo - regmatch[2].rm_so);
232 } else {
233 type = xstrndup(line + regmatch[3].rm_so,
234 regmatch[3].rm_eo - regmatch[3].rm_so);
235 }
236 if (regmatch[5].rm_so > 0) {
237 name = xstrndup(line + regmatch[5].rm_so,
238 regmatch[5].rm_eo - regmatch[5].rm_so);
239 } else {
240 name = xstrndup(line + regmatch[6].rm_so,
241 regmatch[6].rm_eo - regmatch[6].rm_so);
242 }
243 if (regmatch[8].rm_so > 0) {
244 value = xstrndup(line + regmatch[8].rm_so,
245 regmatch[8].rm_eo - regmatch[8].rm_so);
246 } else {
247 value = xstrndup(line + regmatch[9].rm_so,
248 regmatch[9].rm_eo - regmatch[9].rm_so);
249 }
250 extra = NULL;
251 if (regmatch[11].rm_so > 0) {
252 extra = xstrndup (line + regmatch[11].rm_so,
253 regmatch[11].rm_eo - regmatch[11].rm_so);
254 }
255
256 /* We use the tmp_dest_nv_pair_list below instead of
257 conf->pkg_dest_list because we might encounter an
258 offline_root option later and that would invalidate the
259 directories we would have computed in
260 pkg_dest_list_init. (We do a similar thing with
261 tmp_src_nv_pair_list for sake of symmetry.) */
262 if (strcmp(type, "option") == 0) {
263 opkg_conf_set_option(name, value);
264 } else if (strcmp(type, "src") == 0) {
265 if (!nv_pair_list_find((nv_pair_list_t*) pkg_src_list, name)) {
266 pkg_src_list_append (pkg_src_list, name, value, extra, 0);
267 } else {
268 opkg_msg(ERROR, "Duplicate src declaration (%s %s). "
269 "Skipping.\n", name, value);
270 }
271 } else if (strcmp(type, "src/gz") == 0) {
272 if (!nv_pair_list_find((nv_pair_list_t*) pkg_src_list, name)) {
273 pkg_src_list_append (pkg_src_list, name, value, extra, 1);
274 } else {
275 opkg_msg(ERROR, "Duplicate src declaration (%s %s). "
276 "Skipping.\n", name, value);
277 }
278 } else if (strcmp(type, "dest") == 0) {
279 nv_pair_list_append(tmp_dest_nv_pair_list, name, value);
280 } else if (strcmp(type, "lists_dir") == 0) {
281 conf->lists_dir = xstrdup(value);
282 } else if (strcmp(type, "arch") == 0) {
283 opkg_msg(INFO, "Supported arch %s priority (%s)\n", name, value);
284 if (!value) {
285 opkg_msg(NOTICE, "No priority given for architecture %s,"
286 "defaulting to 10\n", name);
287 value = xstrdup("10");
288 }
289 nv_pair_list_append(&conf->arch_list, name, value);
290 } else {
291 opkg_msg(ERROR, "Ignoring unknown configuration "
292 "parameter: %s %s %s\n", type, name, value);
293 return -1;
294 }
295
296 free(type);
297 free(name);
298 free(value);
299 if (extra)
300 free (extra);
301
302 NEXT_LINE:
303 free(line);
304 }
305
306 regfree(&comment_re);
307 regfree(&valid_line_re);
308 fclose(file);
309
310 return 0;
311 }
312
313 int
314 opkg_conf_write_status_files(void)
315 {
316 pkg_dest_list_elt_t *iter;
317 pkg_dest_t *dest;
318 pkg_vec_t *all;
319 pkg_t *pkg;
320 int i, ret = 0;
321
322 if (conf->noaction)
323 return 0;
324
325 list_for_each_entry(iter, &conf->pkg_dest_list.head, node) {
326 dest = (pkg_dest_t *)iter->data;
327
328 dest->status_fp = fopen(dest->status_file_name, "w");
329 if (dest->status_fp == NULL) {
330 opkg_perror(ERROR, "Can't open status file %s",
331 dest->status_file_name);
332 ret = -1;
333 }
334 }
335
336 all = pkg_vec_alloc();
337 pkg_hash_fetch_available(all);
338
339 for(i = 0; i < all->len; i++) {
340 pkg = all->pkgs[i];
341 /* We don't need most uninstalled packages in the status file */
342 if (pkg->state_status == SS_NOT_INSTALLED
343 && (pkg->state_want == SW_UNKNOWN
344 || pkg->state_want == SW_DEINSTALL
345 || pkg->state_want == SW_PURGE)) {
346 continue;
347 }
348 if (pkg->dest == NULL) {
349 opkg_msg(ERROR, "Internal error: package %s has a NULL dest\n",
350 pkg->name);
351 continue;
352 }
353 if (pkg->dest->status_fp)
354 pkg_print_status(pkg, pkg->dest->status_fp);
355 }
356
357 pkg_vec_free(all);
358
359 list_for_each_entry(iter, &conf->pkg_dest_list.head, node) {
360 dest = (pkg_dest_t *)iter->data;
361 fclose(dest->status_fp);
362 }
363
364 return ret;
365 }
366
367
368 char *
369 root_filename_alloc(char *filename)
370 {
371 char *root_filename;
372 sprintf_alloc(&root_filename, "%s%s",
373 (conf->offline_root ? conf->offline_root : ""), filename);
374 return root_filename;
375 }
376
377 int
378 opkg_conf_init(const args_t *args)
379 {
380 int err;
381 char *tmp_dir_base, *tmp2;
382 nv_pair_list_t tmp_dest_nv_pair_list;
383 char *lock_file = NULL;
384 glob_t globbuf;
385 char *etc_opkg_conf_pattern;
386
387 conf->restrict_to_default_dest = 0;
388 conf->default_dest = NULL;
389 #if defined(HAVE_PATHFINDER)
390 conf->check_x509_path = 1;
391 #endif
392
393 pkg_src_list_init(&conf->pkg_src_list);
394
395 nv_pair_list_init(&tmp_dest_nv_pair_list);
396 pkg_dest_list_init(&conf->pkg_dest_list);
397
398 nv_pair_list_init(&conf->arch_list);
399
400 if (!conf->offline_root)
401 conf->offline_root = xstrdup(getenv("OFFLINE_ROOT"));
402
403 if (args->conf_file) {
404 struct stat stat_buf;
405 err = stat(args->conf_file, &stat_buf);
406 if (err == 0)
407 if (opkg_conf_parse_file(args->conf_file,
408 &conf->pkg_src_list, &tmp_dest_nv_pair_list)<0) {
409 /* Memory leakage from opkg_conf_parse-file */
410 return -1;
411 }
412 }
413
414 if (conf->offline_root)
415 sprintf_alloc(&etc_opkg_conf_pattern, "%s/etc/opkg/*.conf", conf->offline_root);
416 else {
417 const char *conf_file_dir = getenv("OPKG_CONF_DIR");
418 if (conf_file_dir == NULL)
419 conf_file_dir = ARGS_DEFAULT_CONF_FILE_DIR;
420 sprintf_alloc(&etc_opkg_conf_pattern, "%s/*.conf", conf_file_dir);
421 }
422 memset(&globbuf, 0, sizeof(globbuf));
423 err = glob(etc_opkg_conf_pattern, 0, NULL, &globbuf);
424 free (etc_opkg_conf_pattern);
425 if (!err) {
426 int i;
427 for (i = 0; i < globbuf.gl_pathc; i++) {
428 if (globbuf.gl_pathv[i])
429 if (args->conf_file &&
430 !strcmp(args->conf_file, globbuf.gl_pathv[i]))
431 continue;
432 if ( opkg_conf_parse_file(globbuf.gl_pathv[i],
433 &conf->pkg_src_list, &tmp_dest_nv_pair_list)<0) {
434 /* Memory leakage from opkg_conf_parse-file */
435 return -1;
436 }
437 }
438 }
439 globfree(&globbuf);
440
441 /* check for lock file */
442 if (conf->offline_root)
443 sprintf_alloc (&lock_file, "%s/%s/lock", conf->offline_root, OPKG_STATE_DIR_PREFIX);
444 else
445 sprintf_alloc (&lock_file, "%s/lock", OPKG_STATE_DIR_PREFIX);
446
447 lock_fd = creat(lock_file, S_IRUSR | S_IWUSR | S_IRGRP);
448 if (lock_fd == -1) {
449 opkg_perror(ERROR, "Could not create lock file %s", lock_file);
450 free(lock_file);
451 return -1;
452 }
453
454 if (lockf(lock_fd, F_TLOCK, (off_t)0) == -1) {
455 opkg_perror(ERROR, "Could not lock %s", lock_file);
456 free(lock_file);
457 return -1;
458 }
459
460 free(lock_file);
461
462 if (conf->tmp_dir)
463 tmp_dir_base = conf->tmp_dir;
464 else
465 tmp_dir_base = getenv("TMPDIR");
466 sprintf_alloc(&tmp2, "%s/%s",
467 tmp_dir_base ? tmp_dir_base : OPKG_CONF_DEFAULT_TMP_DIR_BASE,
468 OPKG_CONF_TMP_DIR_SUFFIX);
469 if (conf->tmp_dir)
470 free(conf->tmp_dir);
471 conf->tmp_dir = mkdtemp(tmp2);
472 if (conf->tmp_dir == NULL) {
473 opkg_perror(ERROR, "Creating temp dir %s failed", tmp2);
474 return -1;
475 }
476
477 pkg_hash_init();
478 hash_table_init("file-hash", &conf->file_hash, OPKG_CONF_DEFAULT_HASH_LEN);
479 hash_table_init("obs-file-hash", &conf->obs_file_hash, OPKG_CONF_DEFAULT_HASH_LEN/16);
480
481 if (conf->lists_dir == NULL)
482 conf->lists_dir = xstrdup(OPKG_CONF_LISTS_DIR);
483
484 if (conf->offline_root) {
485 char *tmp;
486 sprintf_alloc(&tmp, "%s/%s", conf->offline_root, conf->lists_dir);
487 free(conf->lists_dir);
488 conf->lists_dir = tmp;
489 }
490
491 /* if no architectures were defined, then default all, noarch, and host architecture */
492 if (nv_pair_list_empty(&conf->arch_list)) {
493 nv_pair_list_append(&conf->arch_list, "all", "1");
494 nv_pair_list_append(&conf->arch_list, "noarch", "1");
495 nv_pair_list_append(&conf->arch_list, HOST_CPU_STR, "10");
496 }
497
498 /* Even if there is no conf file, we'll need at least one dest. */
499 if (nv_pair_list_empty(&tmp_dest_nv_pair_list)) {
500 nv_pair_list_append(&tmp_dest_nv_pair_list,
501 OPKG_CONF_DEFAULT_DEST_NAME,
502 OPKG_CONF_DEFAULT_DEST_ROOT_DIR);
503 }
504
505 err = resolve_pkg_dest_list(&tmp_dest_nv_pair_list, args->dest);
506 nv_pair_list_deinit(&tmp_dest_nv_pair_list);
507
508 if (err)
509 return -1;
510
511 return 0;
512 }
513
514 void
515 opkg_conf_deinit(void)
516 {
517 int i;
518 char **tmp;
519
520 rm_r(conf->tmp_dir);
521
522 free(conf->lists_dir);
523
524 pkg_src_list_deinit(&conf->pkg_src_list);
525 pkg_dest_list_deinit(&conf->pkg_dest_list);
526 nv_pair_list_deinit(&conf->arch_list);
527
528 for (i=0; options[i].name; i++) {
529 if (options[i].type == OPKG_OPT_TYPE_STRING) {
530 tmp = (char **)options[i].value;
531 if (*tmp) {
532 free(*tmp);
533 *tmp = NULL;
534 }
535 }
536 }
537
538 if (conf->verbosity >= DEBUG) {
539 hash_print_stats(&conf->pkg_hash);
540 hash_print_stats(&conf->file_hash);
541 hash_print_stats(&conf->obs_file_hash);
542 }
543
544 pkg_hash_deinit();
545 hash_table_deinit(&conf->file_hash);
546 hash_table_deinit(&conf->obs_file_hash);
547
548 /* lockf may be defined with warn_unused_result */
549 if (lockf(lock_fd, F_ULOCK, (off_t)0) != 0) {
550 opkg_perror(ERROR, "unlock failed");
551 }
552
553 close(lock_fd);
554 }