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