gnunet-config.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  1. /*
  2. This file is part of GNUnet.
  3. Copyright (C) 2012 GNUnet e.V.
  4. GNUnet is free software: you can redistribute it and/or modify it
  5. under the terms of the GNU Affero General Public License as published
  6. by the Free Software Foundation, either version 3 of the License,
  7. or (at your option) any later version.
  8. GNUnet is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Affero General Public License for more details.
  12. You should have received a copy of the GNU Affero General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. SPDX-License-Identifier: AGPL3.0-or-later
  15. */
  16. /**
  17. * @file util/gnunet-config.c
  18. * @brief tool to access and manipulate GNUnet configuration files
  19. * @author Christian Grothoff
  20. */
  21. #include "platform.h"
  22. #include "gnunet_util_lib.h"
  23. /**
  24. * Name of the section
  25. */
  26. static char *section;
  27. /**
  28. * Name of the option
  29. */
  30. static char *option;
  31. /**
  32. * Value to set
  33. */
  34. static char *value;
  35. /**
  36. * Backend to check if the respective plugin is
  37. * loadable. NULL if no check is to be performed.
  38. * The value is the "basename" of the plugin to load.
  39. */
  40. static char *backend_check;
  41. /**
  42. * Treat option as a filename.
  43. */
  44. static int is_filename;
  45. /**
  46. * Whether to show the sections.
  47. */
  48. static int list_sections;
  49. /**
  50. * Return value from 'main'.
  51. */
  52. static int global_ret;
  53. /**
  54. * Should we generate a configuration file that is clean and
  55. * only contains the deltas to the defaults?
  56. */
  57. static int rewrite;
  58. /**
  59. * Print each option in a given section.
  60. *
  61. * @param cls closure
  62. * @param section name of the section
  63. * @param option name of the option
  64. * @param value value of the option
  65. */
  66. static void
  67. print_option (void *cls,
  68. const char *section,
  69. const char *option,
  70. const char *value)
  71. {
  72. const struct GNUNET_CONFIGURATION_Handle *cfg = cls;
  73. (void) section;
  74. if (is_filename)
  75. {
  76. char *value_fn;
  77. char *fn;
  78. GNUNET_assert (GNUNET_OK ==
  79. GNUNET_CONFIGURATION_get_value_filename (cfg,
  80. section,
  81. option,
  82. &value_fn));
  83. fn = GNUNET_STRINGS_filename_expand (value_fn);
  84. if (NULL == fn)
  85. fn = value_fn;
  86. else
  87. GNUNET_free (value_fn);
  88. fprintf (stdout, "%s = %s\n", option, fn);
  89. GNUNET_free (fn);
  90. }
  91. else
  92. {
  93. fprintf (stdout, "%s = %s\n", option, value);
  94. }
  95. }
  96. /**
  97. * Print out given section name.
  98. *
  99. * @param cls unused
  100. * @param section a section in the configuration file
  101. */
  102. static void
  103. print_section_name (void *cls, const char *section)
  104. {
  105. (void) cls;
  106. fprintf (stdout, "%s\n", section);
  107. }
  108. /**
  109. * Main function that will be run by the scheduler.
  110. *
  111. * @param cls closure
  112. * @param args remaining command-line arguments
  113. * @param cfgfile name of the configuration file used (for saving,
  114. * can be NULL!)
  115. * @param cfg configuration
  116. */
  117. static void
  118. run (void *cls,
  119. char *const *args,
  120. const char *cfgfile,
  121. const struct GNUNET_CONFIGURATION_Handle *cfg)
  122. {
  123. struct GNUNET_CONFIGURATION_Handle *out = NULL;
  124. struct GNUNET_CONFIGURATION_Handle *diff = NULL;
  125. char *cfg_fn;
  126. (void) cls;
  127. (void) args;
  128. if (NULL != backend_check)
  129. {
  130. char *name;
  131. GNUNET_asprintf (&name, "libgnunet_plugin_%s", backend_check);
  132. global_ret = (GNUNET_OK == GNUNET_PLUGIN_test (name)) ? 0 : 77;
  133. GNUNET_free (name);
  134. return;
  135. }
  136. if (rewrite)
  137. {
  138. struct GNUNET_CONFIGURATION_Handle *def;
  139. def = GNUNET_CONFIGURATION_create ();
  140. if (GNUNET_OK != GNUNET_CONFIGURATION_load (def, NULL))
  141. {
  142. fprintf (stderr, _ ("failed to load configuration defaults"));
  143. global_ret = 1;
  144. return;
  145. }
  146. diff = GNUNET_CONFIGURATION_get_diff (def, cfg);
  147. cfg = diff;
  148. }
  149. if (((! rewrite) && (NULL == section)) || list_sections)
  150. {
  151. if (! list_sections)
  152. {
  153. fprintf (stderr,
  154. _ ("%s or %s argument is required\n"),
  155. "--section",
  156. "--list-sections");
  157. global_ret = 1;
  158. }
  159. else
  160. {
  161. fprintf (stderr, _ ("The following sections are available:\n"));
  162. GNUNET_CONFIGURATION_iterate_sections (cfg, &print_section_name, NULL);
  163. }
  164. goto cleanup;
  165. }
  166. if ((NULL != section) && (NULL == value))
  167. {
  168. if (NULL == option)
  169. {
  170. GNUNET_CONFIGURATION_iterate_section_values (cfg,
  171. section,
  172. &print_option,
  173. (void *) cfg);
  174. }
  175. else
  176. {
  177. if (is_filename)
  178. {
  179. if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_filename (cfg,
  180. section,
  181. option,
  182. &value))
  183. {
  184. GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR, section, option);
  185. global_ret = 3;
  186. goto cleanup;
  187. }
  188. }
  189. else
  190. {
  191. if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_string (cfg,
  192. section,
  193. option,
  194. &value))
  195. {
  196. GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR, section, option);
  197. global_ret = 3;
  198. goto cleanup;
  199. }
  200. }
  201. fprintf (stdout, "%s\n", value);
  202. }
  203. }
  204. else if (NULL != section)
  205. {
  206. if (NULL == option)
  207. {
  208. fprintf (stderr, _ ("--option argument required to set value\n"));
  209. global_ret = 1;
  210. goto cleanup;
  211. }
  212. out = GNUNET_CONFIGURATION_dup (cfg);
  213. GNUNET_CONFIGURATION_set_value_string (out, section, option, value);
  214. }
  215. cfg_fn = NULL;
  216. if (NULL == cfgfile)
  217. {
  218. const char *xdg = getenv ("XDG_CONFIG_HOME");
  219. if (NULL != xdg)
  220. GNUNET_asprintf (&cfg_fn,
  221. "%s%s%s",
  222. xdg,
  223. DIR_SEPARATOR_STR,
  224. GNUNET_OS_project_data_get ()->config_file);
  225. else
  226. cfg_fn = GNUNET_strdup (GNUNET_OS_project_data_get ()->user_config_file);
  227. cfgfile = cfg_fn;
  228. }
  229. if ((NULL != diff) || (NULL != out))
  230. {
  231. if (GNUNET_OK !=
  232. GNUNET_CONFIGURATION_write ((NULL == out) ? diff : out, cfgfile))
  233. global_ret = 2;
  234. }
  235. GNUNET_free (cfg_fn);
  236. if (NULL != out)
  237. GNUNET_CONFIGURATION_destroy (out);
  238. cleanup:
  239. if (NULL != diff)
  240. GNUNET_CONFIGURATION_destroy (diff);
  241. }
  242. /**
  243. * Program to manipulate configuration files.
  244. *
  245. * @param argc number of arguments from the command line
  246. * @param argv command line arguments
  247. * @return 0 ok, 1 on error
  248. */
  249. int
  250. main (int argc, char *const *argv)
  251. {
  252. struct GNUNET_GETOPT_CommandLineOption options[] =
  253. { GNUNET_GETOPT_option_flag (
  254. 'f',
  255. "filename",
  256. gettext_noop ("interpret option value as a filename (with $-expansion)"),
  257. &is_filename),
  258. GNUNET_GETOPT_option_exclusive (GNUNET_GETOPT_option_string (
  259. 'b',
  260. "supported-backend",
  261. "BACKEND",
  262. gettext_noop (
  263. "test if the current installation supports the specified BACKEND"),
  264. &backend_check)),
  265. GNUNET_GETOPT_option_string ('s',
  266. "section",
  267. "SECTION",
  268. gettext_noop (
  269. "name of the section to access"),
  270. &section),
  271. GNUNET_GETOPT_option_string ('o',
  272. "option",
  273. "OPTION",
  274. gettext_noop ("name of the option to access"),
  275. &option),
  276. GNUNET_GETOPT_option_string ('V',
  277. "value",
  278. "VALUE",
  279. gettext_noop ("value to set"),
  280. &value),
  281. GNUNET_GETOPT_option_flag ('S',
  282. "list-sections",
  283. gettext_noop (
  284. "print available configuration sections"),
  285. &list_sections),
  286. GNUNET_GETOPT_option_flag (
  287. 'w',
  288. "rewrite",
  289. gettext_noop (
  290. "write configuration file that only contains delta to defaults"),
  291. &rewrite),
  292. GNUNET_GETOPT_OPTION_END };
  293. int ret;
  294. if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
  295. return 2;
  296. ret =
  297. GNUNET_PROGRAM_run (argc,
  298. argv,
  299. "gnunet-config [OPTIONS]",
  300. gettext_noop ("Manipulate GNUnet configuration files"),
  301. options,
  302. &run,
  303. NULL);
  304. GNUNET_free_nz ((void *) argv);
  305. if (GNUNET_OK == ret)
  306. return global_ret;
  307. return ret;
  308. }
  309. /* end of gnunet-config.c */