Fix lock_fd... How did I manage to break that?
[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 opkg_conf_set_default_dest(const char *default_dest_name)
88 {
89 pkg_dest_list_elt_t *iter;
90 pkg_dest_t *dest;
91
92 for (iter = void_list_first(&conf->pkg_dest_list); iter; iter = void_list_next(&conf->pkg_dest_list, iter)) {
93 dest = (pkg_dest_t *)iter->data;
94 if (strcmp(dest->name, default_dest_name) == 0) {
95 conf->default_dest = dest;
96 conf->restrict_to_default_dest = 1;
97 return 0;
98 }
99 }
100
101 opkg_msg(ERROR, "Unknown dest name: `%s'.\n", default_dest_name);
102
103 return 1;
104 }
105
106 static int
107 set_and_load_pkg_src_list(pkg_src_list_t *pkg_src_list)
108 {
109 pkg_src_list_elt_t *iter;
110 pkg_src_t *src;
111 char *list_file;
112
113 for (iter = void_list_first(pkg_src_list); iter; iter = void_list_next(pkg_src_list, iter)) {
114 src = (pkg_src_t *)iter->data;
115 if (src == NULL) {
116 continue;
117 }
118
119 sprintf_alloc(&list_file, "%s/%s",
120 conf->restrict_to_default_dest ? conf->default_dest->lists_dir : conf->lists_dir,
121 src->name);
122
123 if (file_exists(list_file)) {
124 if (pkg_hash_add_from_file(list_file, src, NULL, 0)) {
125 free(list_file);
126 return -1;
127 }
128 }
129 free(list_file);
130 }
131
132 return 0;
133 }
134
135 static int
136 set_and_load_pkg_dest_list(nv_pair_list_t *nv_pair_list)
137 {
138 nv_pair_list_elt_t *iter;
139 nv_pair_t *nv_pair;
140 pkg_dest_t *dest;
141 char *root_dir;
142
143 for (iter = nv_pair_list_first(nv_pair_list); iter; iter = nv_pair_list_next(nv_pair_list, iter)) {
144 nv_pair = (nv_pair_t *)iter->data;
145
146 if (conf->offline_root) {
147 sprintf_alloc(&root_dir, "%s%s", conf->offline_root, nv_pair->value);
148 } else {
149 root_dir = xstrdup(nv_pair->value);
150 }
151 dest = pkg_dest_list_append(&conf->pkg_dest_list, nv_pair->name, root_dir, conf->lists_dir);
152 free(root_dir);
153 if (dest == NULL) {
154 continue;
155 }
156 if (conf->default_dest == NULL) {
157 conf->default_dest = dest;
158 }
159 if (file_exists(dest->status_file_name)) {
160 if (pkg_hash_add_from_file(dest->status_file_name,
161 NULL, dest, 1))
162 return -1;
163 }
164 }
165
166 return 0;
167 }
168
169 static int
170 opkg_conf_set_option(const char *name, const char *value)
171 {
172 int i = 0;
173 while (options[i].name) {
174 if (strcmp(options[i].name, name) == 0) {
175 switch (options[i].type) {
176 case OPKG_OPT_TYPE_BOOL:
177 if (*(int *)options[i].value) {
178 opkg_msg(ERROR, "Duplicate boolean option %s, "
179 "leaving this option on.\n", name);
180 return 0;
181 }
182 *((int * const)options[i].value) = 1;
183 return 0;
184 case OPKG_OPT_TYPE_INT:
185 if (value) {
186 if (*(int *)options[i].value) {
187 opkg_msg(ERROR, "Duplicate option %s, "
188 "using first seen value \"%d\".\n",
189 name, *((int *)options[i].value));
190 return 0;
191 }
192 *((int * const)options[i].value) = atoi(value);
193 return 0;
194 } else {
195 opkg_msg(ERROR, "Option %s needs an argument\n",
196 name);
197 return -1;
198 }
199 case OPKG_OPT_TYPE_STRING:
200 if (value) {
201 if (*(char **)options[i].value) {
202 opkg_msg(ERROR, "Duplicate option %s, "
203 "using first seen value \"%s\".\n",
204 name, *((char **)options[i].value));
205 return 0;
206 }
207 *((char ** const)options[i].value) = xstrdup(value);
208 return 0;
209 } else {
210 opkg_msg(ERROR, "Option %s needs an argument\n",
211 name);
212 return -1;
213 }
214 }
215 }
216 i++;
217 }
218
219 opkg_msg(ERROR, "Unrecognized option: %s=%s\n", name, value);
220 return -1;
221 }
222
223 static int
224 opkg_conf_parse_file(const char *filename,
225 pkg_src_list_t *pkg_src_list,
226 nv_pair_list_t *tmp_dest_nv_pair_list)
227 {
228 int err;
229 FILE *file;
230 regex_t valid_line_re, comment_re;
231 #define regmatch_size 12
232 regmatch_t regmatch[regmatch_size];
233
234 file = fopen(filename, "r");
235 if (file == NULL) {
236 opkg_perror(ERROR, "Failed to open %s", filename);
237 return -1;
238 }
239
240 opkg_msg(INFO, "Loading conf file %s.\n", filename);
241
242 err = xregcomp(&comment_re,
243 "^[[:space:]]*(#.*|[[:space:]]*)$",
244 REG_EXTENDED);
245 if (err) {
246 return -1;
247 }
248 err = xregcomp(&valid_line_re, "^[[:space:]]*(\"([^\"]*)\"|([^[:space:]]*))[[:space:]]*(\"([^\"]*)\"|([^[:space:]]*))[[:space:]]*(\"([^\"]*)\"|([^[:space:]]*))([[:space:]]+([^[:space:]]+))?[[:space:]]*$", REG_EXTENDED);
249 if (err) {
250 return -1;
251 }
252
253 while(1) {
254 int line_num = 0;
255 char *line;
256 char *type, *name, *value, *extra;
257
258 line = file_read_line_alloc(file);
259 line_num++;
260 if (line == NULL) {
261 break;
262 }
263
264 if (regexec(&comment_re, line, 0, 0, 0) == 0) {
265 goto NEXT_LINE;
266 }
267
268 if (regexec(&valid_line_re, line, regmatch_size, regmatch, 0) == REG_NOMATCH) {
269 opkg_msg(ERROR, "%s:%d: Ignoring invalid line: `%s'\n",
270 filename, line_num, line);
271 goto NEXT_LINE;
272 }
273
274 /* This has to be so ugly to deal with optional quotation marks */
275 if (regmatch[2].rm_so > 0) {
276 type = xstrndup(line + regmatch[2].rm_so,
277 regmatch[2].rm_eo - regmatch[2].rm_so);
278 } else {
279 type = xstrndup(line + regmatch[3].rm_so,
280 regmatch[3].rm_eo - regmatch[3].rm_so);
281 }
282 if (regmatch[5].rm_so > 0) {
283 name = xstrndup(line + regmatch[5].rm_so,
284 regmatch[5].rm_eo - regmatch[5].rm_so);
285 } else {
286 name = xstrndup(line + regmatch[6].rm_so,
287 regmatch[6].rm_eo - regmatch[6].rm_so);
288 }
289 if (regmatch[8].rm_so > 0) {
290 value = xstrndup(line + regmatch[8].rm_so,
291 regmatch[8].rm_eo - regmatch[8].rm_so);
292 } else {
293 value = xstrndup(line + regmatch[9].rm_so,
294 regmatch[9].rm_eo - regmatch[9].rm_so);
295 }
296 extra = NULL;
297 if (regmatch[11].rm_so > 0) {
298 extra = xstrndup (line + regmatch[11].rm_so,
299 regmatch[11].rm_eo - regmatch[11].rm_so);
300 }
301
302 /* We use the tmp_dest_nv_pair_list below instead of
303 conf->pkg_dest_list because we might encounter an
304 offline_root option later and that would invalidate the
305 directories we would have computed in
306 pkg_dest_list_init. (We do a similar thing with
307 tmp_src_nv_pair_list for sake of symmetry.) */
308 if (strcmp(type, "option") == 0) {
309 opkg_conf_set_option(name, value);
310 } else if (strcmp(type, "src") == 0) {
311 if (!nv_pair_list_find((nv_pair_list_t*) pkg_src_list, name)) {
312 pkg_src_list_append (pkg_src_list, name, value, extra, 0);
313 } else {
314 opkg_msg(ERROR, "Duplicate src declaration (%s %s). "
315 "Skipping.\n", name, value);
316 }
317 } else if (strcmp(type, "src/gz") == 0) {
318 if (!nv_pair_list_find((nv_pair_list_t*) pkg_src_list, name)) {
319 pkg_src_list_append (pkg_src_list, name, value, extra, 1);
320 } else {
321 opkg_msg(ERROR, "Duplicate src declaration (%s %s). "
322 "Skipping.\n", name, value);
323 }
324 } else if (strcmp(type, "dest") == 0) {
325 nv_pair_list_append(tmp_dest_nv_pair_list, name, value);
326 } else if (strcmp(type, "lists_dir") == 0) {
327 conf->lists_dir = xstrdup(value);
328 } else if (strcmp(type, "arch") == 0) {
329 opkg_msg(INFO, "Supported arch %s priority (%s)\n", name, value);
330 if (!value) {
331 opkg_msg(NOTICE, "No priority given for architecture %s,"
332 "defaulting to 10\n", name);
333 value = xstrdup("10");
334 }
335 nv_pair_list_append(&conf->arch_list, name, value);
336 } else {
337 opkg_msg(ERROR, "Ignoring unknown configuration "
338 "parameter: %s %s %s\n", type, name, value);
339 return -1;
340 }
341
342 free(type);
343 free(name);
344 free(value);
345 if (extra)
346 free (extra);
347
348 NEXT_LINE:
349 free(line);
350 }
351
352 regfree(&comment_re);
353 regfree(&valid_line_re);
354 fclose(file);
355
356 return 0;
357 }
358
359 int
360 opkg_conf_write_status_files(void)
361 {
362 pkg_dest_list_elt_t *iter;
363 pkg_dest_t *dest;
364 pkg_vec_t *all;
365 pkg_t *pkg;
366 int i, ret = 0;
367
368 if (conf->noaction)
369 return 0;
370
371 list_for_each_entry(iter, &conf->pkg_dest_list.head, node) {
372 dest = (pkg_dest_t *)iter->data;
373
374 dest->status_fp = fopen(dest->status_file_name, "w");
375 if (dest->status_fp == NULL) {
376 opkg_perror(ERROR, "Can't open status file %s",
377 dest->status_file_name);
378 ret = -1;
379 }
380 }
381
382 all = pkg_vec_alloc();
383 pkg_hash_fetch_available(all);
384
385 for(i = 0; i < all->len; i++) {
386 pkg = all->pkgs[i];
387 /* We don't need most uninstalled packages in the status file */
388 if (pkg->state_status == SS_NOT_INSTALLED
389 && (pkg->state_want == SW_UNKNOWN
390 || pkg->state_want == SW_DEINSTALL
391 || pkg->state_want == SW_PURGE)) {
392 continue;
393 }
394 if (pkg->dest == NULL) {
395 opkg_msg(ERROR, "Internal error: package %s has a NULL dest\n",
396 pkg->name);
397 continue;
398 }
399 if (pkg->dest->status_fp)
400 pkg_print_status(pkg, pkg->dest->status_fp);
401 }
402
403 pkg_vec_free(all);
404
405 list_for_each_entry(iter, &conf->pkg_dest_list.head, node) {
406 dest = (pkg_dest_t *)iter->data;
407 fclose(dest->status_fp);
408 }
409
410 return ret;
411 }
412
413
414 char *
415 root_filename_alloc(char *filename)
416 {
417 char *root_filename;
418 sprintf_alloc(&root_filename, "%s%s",
419 (conf->offline_root ? conf->offline_root : ""), filename);
420 return root_filename;
421 }
422
423 int
424 opkg_conf_init(const args_t *args)
425 {
426 int err;
427 char *tmp_dir_base, *tmp2;
428 nv_pair_list_t tmp_dest_nv_pair_list;
429 char *lock_file = NULL;
430 glob_t globbuf;
431 char *etc_opkg_conf_pattern;
432
433 conf->restrict_to_default_dest = 0;
434 conf->default_dest = NULL;
435 #if defined(HAVE_PATHFINDER)
436 conf->check_x509_path = 1;
437 #endif
438
439 pkg_src_list_init(&conf->pkg_src_list);
440
441 nv_pair_list_init(&tmp_dest_nv_pair_list);
442 pkg_dest_list_init(&conf->pkg_dest_list);
443
444 nv_pair_list_init(&conf->arch_list);
445
446 if (args->conf_file) {
447 struct stat stat_buf;
448 err = stat(args->conf_file, &stat_buf);
449 if (err == 0)
450 if (opkg_conf_parse_file(args->conf_file,
451 &conf->pkg_src_list, &tmp_dest_nv_pair_list)<0) {
452 /* Memory leakage from opkg_conf_parse-file */
453 return -1;
454 }
455 }
456
457 if (conf->offline_root)
458 sprintf_alloc(&etc_opkg_conf_pattern, "%s/etc/opkg/*.conf", conf->offline_root);
459 else {
460 const char *conf_file_dir = getenv("OPKG_CONF_DIR");
461 if (conf_file_dir == NULL)
462 conf_file_dir = ARGS_DEFAULT_CONF_FILE_DIR;
463 sprintf_alloc(&etc_opkg_conf_pattern, "%s/*.conf", conf_file_dir);
464 }
465 memset(&globbuf, 0, sizeof(globbuf));
466 err = glob(etc_opkg_conf_pattern, 0, NULL, &globbuf);
467 free (etc_opkg_conf_pattern);
468 if (!err) {
469 int i;
470 for (i = 0; i < globbuf.gl_pathc; i++) {
471 if (globbuf.gl_pathv[i])
472 if (args->conf_file &&
473 !strcmp(args->conf_file, globbuf.gl_pathv[i]))
474 continue;
475 if ( opkg_conf_parse_file(globbuf.gl_pathv[i],
476 &conf->pkg_src_list, &tmp_dest_nv_pair_list)<0) {
477 /* Memory leakage from opkg_conf_parse-file */
478 return -1;
479 }
480 }
481 }
482 globfree(&globbuf);
483
484 /* check for lock file */
485 if (conf->offline_root)
486 sprintf_alloc (&lock_file, "%s/%s/lock", conf->offline_root, OPKG_STATE_DIR_PREFIX);
487 else
488 sprintf_alloc (&lock_file, "%s/lock", OPKG_STATE_DIR_PREFIX);
489
490 lock_fd = creat(lock_file, S_IRUSR | S_IWUSR | S_IRGRP);
491 if (lock_fd == -1) {
492 opkg_perror(ERROR, "Could not create lock file %s", lock_file);
493 free(lock_file);
494 return -1;
495 }
496
497 if (lockf(lock_fd, F_TLOCK, (off_t)0) == -1) {
498 opkg_perror(ERROR, "Could not lock %s", lock_file);
499 free(lock_file);
500 return -1;
501 }
502
503 free(lock_file);
504
505 if (conf->tmp_dir)
506 tmp_dir_base = conf->tmp_dir;
507 else
508 tmp_dir_base = getenv("TMPDIR");
509 sprintf_alloc(&tmp2, "%s/%s",
510 tmp_dir_base ? tmp_dir_base : OPKG_CONF_DEFAULT_TMP_DIR_BASE,
511 OPKG_CONF_TMP_DIR_SUFFIX);
512 if (conf->tmp_dir)
513 free(conf->tmp_dir);
514 conf->tmp_dir = mkdtemp(tmp2);
515 if (conf->tmp_dir == NULL) {
516 opkg_perror(ERROR, "Creating temp dir %s failed", tmp2);
517 return -1;
518 }
519
520 pkg_hash_init();
521 hash_table_init("file-hash", &conf->file_hash, OPKG_CONF_DEFAULT_HASH_LEN);
522 hash_table_init("obs-file-hash", &conf->obs_file_hash, OPKG_CONF_DEFAULT_HASH_LEN/16);
523
524 if (conf->lists_dir == NULL)
525 conf->lists_dir = xstrdup(OPKG_CONF_LISTS_DIR);
526
527 if (conf->offline_root) {
528 char *tmp;
529 sprintf_alloc(&tmp, "%s/%s", conf->offline_root, conf->lists_dir);
530 free(conf->lists_dir);
531 conf->lists_dir = tmp;
532 }
533
534 /* if no architectures were defined, then default all, noarch, and host architecture */
535 if (nv_pair_list_empty(&conf->arch_list)) {
536 nv_pair_list_append(&conf->arch_list, "all", "1");
537 nv_pair_list_append(&conf->arch_list, "noarch", "1");
538 nv_pair_list_append(&conf->arch_list, HOST_CPU_STR, "10");
539 }
540
541 /* Even if there is no conf file, we'll need at least one dest. */
542 if (nv_pair_list_empty(&tmp_dest_nv_pair_list)) {
543 nv_pair_list_append(&tmp_dest_nv_pair_list,
544 OPKG_CONF_DEFAULT_DEST_NAME,
545 OPKG_CONF_DEFAULT_DEST_ROOT_DIR);
546 }
547
548 if (!(args->nocheckfordirorfile)) {
549
550 if (!(args->noreadfeedsfile)) {
551 if (set_and_load_pkg_src_list(&conf->pkg_src_list)) {
552 nv_pair_list_deinit(&tmp_dest_nv_pair_list);
553 return -1;
554 }
555 }
556
557 /* Now that we have resolved conf->offline_root, we can commit to
558 the directory names for the dests and load in all the package
559 lists. */
560 if (set_and_load_pkg_dest_list(&tmp_dest_nv_pair_list)) {
561 nv_pair_list_deinit(&tmp_dest_nv_pair_list);
562 return -1;
563 }
564
565 if (args->dest) {
566 err = opkg_conf_set_default_dest(args->dest);
567 if (err) {
568 nv_pair_list_deinit(&tmp_dest_nv_pair_list);
569 return -1;
570 }
571 }
572 }
573 nv_pair_list_deinit(&tmp_dest_nv_pair_list);
574
575 return 0;
576 }
577
578 void
579 opkg_conf_deinit(void)
580 {
581 int i;
582 char **tmp;
583
584 rm_r(conf->tmp_dir);
585
586 free(conf->lists_dir);
587
588 pkg_src_list_deinit(&conf->pkg_src_list);
589 pkg_dest_list_deinit(&conf->pkg_dest_list);
590 nv_pair_list_deinit(&conf->arch_list);
591
592 for (i=0; options[i].name; i++) {
593 if (options[i].type == OPKG_OPT_TYPE_STRING) {
594 tmp = (char **)options[i].value;
595 if (*tmp) {
596 free(*tmp);
597 *tmp = NULL;
598 }
599 }
600 }
601
602 if (conf->verbosity >= DEBUG) {
603 hash_print_stats(&conf->pkg_hash);
604 hash_print_stats(&conf->file_hash);
605 hash_print_stats(&conf->obs_file_hash);
606 }
607
608 pkg_hash_deinit();
609 hash_table_deinit(&conf->file_hash);
610 hash_table_deinit(&conf->obs_file_hash);
611
612 /* lockf may be defined with warn_unused_result */
613 if (lockf(lock_fd, F_ULOCK, (off_t)0) != 0) {
614 opkg_perror(ERROR, "unlock failed");
615 }
616
617 close(lock_fd);
618 }