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