2 Copyright (C) 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013
3 Ben Kibbey <bjk@luxsci.net>
5 This file is part of pwmd.
7 Pwmd is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 2 of the License, or
10 (at your option) any later version.
12 Pwmd is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with Pwmd. If not, see <http://www.gnu.org/licenses/>.
26 #include <sys/types.h>
32 #include "pwmd-error.h"
36 #include "util-misc.h"
38 #include "util-slist.h"
39 #include "util-string.h"
42 #define DEFAULT_PINENTRY_TIMEOUT "30"
44 #define INVALID_VALUE(file, line) do { \
46 log_write(_("%s(%i): invalid value for parameter."), file, line); \
51 PARAM_INT
, PARAM_CHARP
, PARAM_LONG
, PARAM_LONGLONG
, PARAM_CHARPP
,
52 PARAM_BOOL
, PARAM_ULONG
, PARAM_ULONGLONG
55 static struct config_params_s
61 { "backup", PARAM_BOOL
, "1"},
62 { "socket_path", PARAM_CHARP
, NULL
},
63 { "socket_perms", PARAM_CHARP
, NULL
},
64 { "passphrase", PARAM_CHARP
, NULL
},
65 { "passphrase_file", PARAM_CHARP
, NULL
},
66 { "agent_env_file", PARAM_CHARP
, NULL
},
67 { "log_path", PARAM_CHARP
, "~/.pwmd/log"},
68 { "enable_logging", PARAM_BOOL
, "0"},
69 { "log_level", PARAM_INT
, "0"},
70 { "disable_mlockall", PARAM_BOOL
, "1"},
71 { "cache_timeout", PARAM_INT
, "-1"},
72 { "cache_push", PARAM_CHARPP
, NULL
},
73 { "disable_list_and_dump", PARAM_BOOL
, "0"},
74 { "recursion_depth", PARAM_INT
, "100"},
75 { "syslog", PARAM_BOOL
, "0"},
76 { "xfer_progress", PARAM_INT
, "8196"},
77 { "allowed", PARAM_CHARPP
, NULL
},
78 { "nbits", PARAM_INT
, "2048"},
79 { "algo", PARAM_CHARP
, "rsa"},
80 { "cipher", PARAM_CHARP
, "aes256"},
81 { "kill_scd", PARAM_BOOL
, "0"},
82 { "cipher_iterations", PARAM_ULONGLONG
, "0"},
83 { "cipher_progress", PARAM_LONG
, DEFAULT_ITERATION_PROGRESS
},
84 { "priority", PARAM_INT
, INVALID_PRIORITY
},
85 { "keepalive_interval", PARAM_INT
, "5"},
86 { "tcp_port", PARAM_INT
, "6466"},
87 { "enable_tcp", PARAM_BOOL
, "0"},
88 { "tcp_require_key", PARAM_BOOL
, "0"},
89 { "tcp_wait", PARAM_INT
, "0"},
90 { "tcp_bind", PARAM_CHARP
, "any"},
91 { "tcp_interface", PARAM_CHARP
, NULL
},
92 { "tls_timeout", PARAM_INT
, "300"},
93 { "tls_cipher_suite", PARAM_CHARP
, "SECURE256"},
94 { "tls_use_crl", PARAM_BOOL
, "1"},
95 { "tls_access", PARAM_CHARPP
, NULL
},
96 { "pinentry_path", PARAM_CHARP
, PINENTRY_PATH
},
97 { "pinentry_timeout", PARAM_INT
, DEFAULT_PINENTRY_TIMEOUT
},
112 unsigned long ultype
;
113 unsigned long long ulltype
;
120 struct slist_s
*params
;
123 static struct section_s
*config_find_section (struct slist_s
*config
,
125 static int new_param (struct section_s
*section
, const char *filename
,
126 int lineno
, const char *name
, const char *value
,
128 static void free_section (struct section_s
*s
);
129 static int set_defaults (struct slist_s
**config
);
132 section_remove_param (struct section_s
*section
, const char *name
)
134 unsigned i
, t
= slist_length (section
->params
);
136 for (i
= 0; i
< t
; i
++)
138 struct param_s
*p
= slist_nth_data (section
->params
, i
);
143 if (!strcmp (p
->name
, name
))
148 xfree (p
->value
.cptype
);
151 strv_free (p
->value
.cpptype
);
155 section
->params
= slist_remove (section
->params
, p
);
165 MUTEX_LOCK (&rcfile_mutex
);
166 unsigned i
, t
= slist_length (global_config
);
168 for (i
= 0; i
< t
; i
++)
170 struct section_s
*s
= slist_nth_data (global_config
, i
);
174 section_remove_param (s
, "passphrase");
177 MUTEX_UNLOCK (&rcfile_mutex
);
180 static struct param_s
*
181 config_has_param (struct section_s
*s
, const char *what
)
183 unsigned i
, t
= slist_length (s
->params
);
185 for (i
= 0; i
< t
; i
++)
187 struct param_s
*p
= slist_nth_data (s
->params
, i
);
191 if (!strcmp (p
->name
, what
))
198 static struct param_s
*
199 config_get_param (struct slist_s
*config
,
200 const char *section
, const char *what
, int *exists
)
202 unsigned i
, t
= slist_length (config
);
206 for (i
= 0; i
< t
; i
++)
209 struct section_s
*s
= slist_nth_data (config
, i
);
214 if (strcmp (s
->name
, section
))
217 p
= config_has_param (s
, what
);
228 static struct section_s
*
229 new_section (struct slist_s
**config
, const char *name
)
232 struct section_s
*s
= xcalloc (1, sizeof (struct section_s
));
237 s
->name
= str_dup (name
);
240 log_write ("%s", pwmd_strerror (ENOMEM
));
245 tmp
= slist_append (*config
, s
);
248 log_write ("%s", pwmd_strerror (ENOMEM
));
259 config_set_string_param (struct slist_s
**config
, const char *section
,
260 const char *name
, const char *value
)
262 struct section_s
*s
= config_find_section (*config
, section
);
266 s
= new_section (config
, section
);
271 return new_param (s
, NULL
, 0, name
, value
, PARAM_CHARP
);
275 config_get_string_param (struct slist_s
*config
, const char *section
,
276 const char *what
, int *exists
)
278 struct param_s
*p
= config_get_param (config
, section
, what
, exists
);
279 return *exists
&& p
->value
.cptype
? str_dup (p
->value
.cptype
) : NULL
;
283 config_set_int_param (struct slist_s
**config
, const char *section
,
284 const char *name
, const char *value
)
286 struct section_s
*s
= config_find_section (*config
, section
);
290 s
= new_section (config
, section
);
295 return new_param (s
, NULL
, 0, name
, value
, PARAM_INT
);
299 config_get_int_param (struct slist_s
*config
, const char *section
,
300 const char *what
, int *exists
)
302 struct param_s
*p
= config_get_param (config
, section
, what
, exists
);
303 return *exists
? p
->value
.itype
: -1;
307 config_set_bool_param (struct slist_s
**config
, const char *section
,
308 const char *name
, const char *value
)
310 struct section_s
*s
= config_find_section (*config
, section
);
314 s
= new_section (config
, section
);
319 return new_param (s
, NULL
, 0, name
, value
, PARAM_BOOL
);
323 config_get_bool_param (struct slist_s
*config
, const char *section
,
324 const char *what
, int *exists
)
326 return config_get_int_param (config
, section
, what
, exists
);
330 config_set_long_param (struct slist_s
**config
, const char *section
,
331 const char *name
, const char *value
)
333 struct section_s
*s
= config_find_section (*config
, section
);
337 s
= new_section (config
, section
);
342 return new_param (s
, NULL
, 0, name
, value
, PARAM_LONG
);
346 config_get_ulong_param (struct slist_s
*config
, const char *section
,
347 const char *what
, int *exists
)
349 struct param_s
*p
= config_get_param (config
, section
, what
, exists
);
350 return *exists
? p
->value
.ultype
: 0;
354 config_get_long_param (struct slist_s
*config
, const char *section
,
355 const char *what
, int *exists
)
357 struct param_s
*p
= config_get_param (config
, section
, what
, exists
);
358 return *exists
? p
->value
.ltype
: -1;
362 config_set_longlong_param (struct slist_s
**config
, const char *section
,
363 const char *name
, const char *value
)
365 struct section_s
*s
= config_find_section (*config
, section
);
369 s
= new_section (config
, section
);
374 return new_param (s
, NULL
, 0, name
, value
, PARAM_LONGLONG
);
378 config_get_longlong_param (struct slist_s
*config
,
379 const char *section
, const char *what
, int *exists
)
381 struct param_s
*p
= config_get_param (config
, section
, what
, exists
);
382 return *exists
? p
->value
.lltype
: -1;
386 config_get_ulonglong_param (struct slist_s
*config
,
388 const char *what
, int *exists
)
390 struct param_s
*p
= config_get_param (config
, section
, what
, exists
);
391 return *exists
? p
->value
.ulltype
: 0;
395 config_set_list_param (struct slist_s
**config
, const char *section
,
396 const char *name
, const char *value
)
398 struct section_s
*s
= config_find_section (*config
, section
);
402 s
= new_section (config
, section
);
407 return new_param (s
, NULL
, 0, name
, value
, PARAM_CHARPP
);
411 config_get_list_param (struct slist_s
*config
, const char *section
,
412 const char *what
, int *exists
)
414 struct param_s
*p
= config_get_param (config
, section
, what
, exists
);
415 return *exists
&& p
->value
.cpptype
? strv_dup (p
->value
.cpptype
) : NULL
;
419 config_get_string (const char *section
, const char *what
)
422 const char *where
= section
? section
: "global";
425 MUTEX_LOCK (&rcfile_mutex
);
426 val
= config_get_string_param (global_config
, where
, what
, &exists
);
427 if (!exists
&& strcmp (section
? section
: "", "global"))
428 val
= config_get_string_param (global_config
, "global", what
, &exists
);
430 MUTEX_UNLOCK (&rcfile_mutex
);
435 config_get_list (const char *section
, const char *what
)
438 const char *where
= section
? section
: "global";
441 MUTEX_LOCK (&rcfile_mutex
);
442 val
= config_get_list_param (global_config
, where
, what
, &exists
);
443 if (!exists
&& strcmp (section
? section
: "", "global"))
444 val
= config_get_list_param (global_config
, "global", what
, &exists
);
446 MUTEX_UNLOCK (&rcfile_mutex
);
451 config_get_integer (const char *section
, const char *what
)
454 const char *where
= section
? section
: "global";
457 MUTEX_LOCK (&rcfile_mutex
);
458 val
= config_get_int_param (global_config
, where
, what
, &exists
);
459 if (!exists
&& strcmp (section
? section
: "", "global"))
460 val
= config_get_int_param (global_config
, "global", what
, &exists
);
462 MUTEX_UNLOCK (&rcfile_mutex
);
467 config_get_longlong (const char *section
, const char *what
)
470 const char *where
= section
? section
: "global";
473 MUTEX_LOCK (&rcfile_mutex
);
474 val
= config_get_longlong_param (global_config
, where
, what
, &exists
);
475 if (!exists
&& strcmp (section
? section
: "", "global"))
476 val
= config_get_longlong_param (global_config
, "global", what
, &exists
);
478 MUTEX_UNLOCK (&rcfile_mutex
);
483 config_get_ulonglong (const char *section
, const char *what
)
485 unsigned long long val
= 0;
486 const char *where
= section
? section
: "global";
489 MUTEX_LOCK (&rcfile_mutex
);
490 val
= config_get_ulonglong_param (global_config
, where
, what
, &exists
);
491 if (!exists
&& strcmp (section
? section
: "", "global"))
492 val
= config_get_ulonglong_param (global_config
, "global", what
, &exists
);
494 MUTEX_UNLOCK (&rcfile_mutex
);
499 config_get_long (const char *section
, const char *what
)
502 const char *where
= section
? section
: "global";
505 MUTEX_LOCK (&rcfile_mutex
);
506 val
= config_get_long_param (global_config
, where
, what
, &exists
);
507 if (!exists
&& strcmp (section
? section
: "", "global"))
508 val
= config_get_long_param (global_config
, "global", what
, &exists
);
510 MUTEX_UNLOCK (&rcfile_mutex
);
515 config_get_ulong (const char *section
, const char *what
)
517 unsigned long val
= 0;
518 const char *where
= section
? section
: "global";
521 MUTEX_LOCK (&rcfile_mutex
);
522 val
= config_get_ulong_param (global_config
, where
, what
, &exists
);
523 if (!exists
&& strcmp (section
? section
: "", "global"))
524 val
= config_get_ulong_param (global_config
, "global", what
, &exists
);
526 MUTEX_UNLOCK (&rcfile_mutex
);
531 config_get_boolean (const char *section
, const char *what
)
533 return config_get_integer (section
, what
);
537 config_get_value (const char *section
, const char *what
)
539 const char *where
= section
? section
: "global";
546 unsigned long long ullval
;
551 MUTEX_LOCK (&rcfile_mutex
);
553 for (i
= 0; config_params
[i
].name
; i
++)
555 if (!strcmp (config_params
[i
].name
, what
))
557 switch (config_params
[i
].type
)
561 ival
= config_get_int_param (global_config
, where
, what
,
563 if (!exists
&& strcmp (section
? section
: "", "global"))
564 ival
= config_get_int_param (global_config
, "global", what
,
566 result
= str_asprintf ("%i", ival
);
569 cpval
= config_get_string_param (global_config
, where
, what
,
571 if (!exists
&& strcmp (section
? section
: "", "global"))
573 config_get_string_param (global_config
, "global", what
,
578 lval
= config_get_long_param (global_config
, where
, what
,
580 if (!exists
&& strcmp (section
? section
: "", "global"))
581 lval
= config_get_long_param (global_config
, "global", what
,
583 result
= str_asprintf ("%li", lval
);
586 ulval
= config_get_ulong_param (global_config
, where
, what
,
588 if (!exists
&& strcmp (section
? section
: "", "global"))
589 ulval
= config_get_ulong_param (global_config
, "global", what
,
591 result
= str_asprintf ("%lu", ulval
);
594 llval
= config_get_longlong_param (global_config
, where
, what
,
596 if (!exists
&& strcmp (section
? section
: "", "global"))
597 llval
= config_get_longlong_param (global_config
, "global",
599 result
= str_asprintf ("%lli", llval
);
601 case PARAM_ULONGLONG
:
602 ullval
= config_get_ulonglong_param (global_config
, where
, what
,
604 if (!exists
&& strcmp (section
? section
: "", "global"))
605 ullval
= config_get_ulonglong_param (global_config
, "global",
607 result
= str_asprintf ("%llu", ullval
);
610 cppval
= config_get_list_param (global_config
, where
, what
,
612 if (!exists
&& strcmp (section
? section
: "", "global"))
613 cppval
= config_get_list_param (global_config
, "global", what
,
617 result
= strv_join (",", cppval
);
625 MUTEX_UNLOCK (&rcfile_mutex
);
630 set_defaults (struct slist_s
**config
)
637 for (i
= 0; config_params
[i
].name
; i
++)
639 switch (config_params
[i
].type
)
642 config_get_bool_param (*config
, "global", config_params
[i
].name
,
646 if (config_set_bool_param
647 (config
, "global", config_params
[i
].name
,
648 config_params
[i
].value
))
653 config_get_int_param (*config
, "global", config_params
[i
].name
,
657 if (config_set_int_param
658 (config
, "global", config_params
[i
].name
,
659 config_params
[i
].value
))
664 s
= config_get_string_param (*config
, "global",
665 config_params
[i
].name
, &exists
);
667 if (!exists
&& config_params
[i
].value
)
669 if (config_set_string_param (config
, "global",
670 config_params
[i
].name
,
671 config_params
[i
].value
))
676 list
= config_get_list_param (*config
, "global",
677 config_params
[i
].name
, &exists
);
679 if (!exists
&& config_params
[i
].value
)
681 if (config_set_list_param (config
, "global",
682 config_params
[i
].name
,
683 config_params
[i
].value
))
688 config_get_long_param (*config
, "global", config_params
[i
].name
,
692 if (config_set_long_param
693 (config
, "global", config_params
[i
].name
,
694 config_params
[i
].value
))
699 config_get_longlong_param (*config
, "global", config_params
[i
].name
,
703 if (config_set_longlong_param (config
, "global",
704 config_params
[i
].name
,
705 config_params
[i
].value
))
712 list
= config_get_list_param (*config
, "global", "allowed", &exists
);
716 if (config_set_list_param
717 (config
, "global", "allowed", get_username ()))
721 max_recursion_depth
= config_get_int_param (*config
, "global",
722 "recursion_depth", &exists
);
723 disable_list_and_dump
= config_get_bool_param (*config
, "global",
724 "disable_list_and_dump",
728 config_get_bool_param (*config
, "global", "disable_mlockall", &exists
);
736 static struct section_s
*
737 config_find_section (struct slist_s
*config
, const char *name
)
739 unsigned i
, t
= slist_length (config
);
741 for (i
= 0; i
< t
; i
++)
743 struct section_s
*s
= slist_nth_data (config
, i
);
745 if (!strcmp (s
->name
, name
))
752 /* Append a new parameter to the list of parameters for a file
753 * section. When an existing parameter of the same name exists, its
757 new_param (struct section_s
*section
, const char *filename
, int lineno
,
758 const char *name
, const char *value
, int type
)
760 struct param_s
*param
= NULL
;
763 unsigned i
, t
= slist_length (section
->params
);
766 for (i
= 0; i
< t
; i
++)
768 struct param_s
*p
= slist_nth_data (section
->params
, i
);
772 if (!strcmp (name
, p
->name
))
782 param
= xcalloc (1, sizeof (struct param_s
));
785 log_write ("%s", pwmd_strerror (ENOMEM
));
789 param
->name
= str_dup (name
);
793 log_write ("%s", pwmd_strerror (ENOMEM
));
803 if (!strcasecmp (value
, "no") || !strcasecmp (value
, "0")
804 || !strcasecmp (value
, "false"))
805 param
->value
.itype
= 0;
806 else if (!strcasecmp (value
, "yes") || !strcasecmp (value
, "1")
807 || !strcasecmp (value
, "true"))
808 param
->value
.itype
= 1;
811 INVALID_VALUE (filename
, lineno
);
814 param
->type
= PARAM_INT
;
817 xfree (param
->value
.cptype
);
818 param
->value
.cptype
= value
? str_dup (value
) : NULL
;
819 if (value
&& !param
->value
.cptype
)
821 log_write ("%s", pwmd_strerror (ENOMEM
));
826 strv_free (param
->value
.cpptype
);
827 param
->value
.cpptype
= value
? str_split (value
, ",", 0) : NULL
;
828 if (value
&& !param
->value
.cpptype
)
830 log_write ("%s", pwmd_strerror (ENOMEM
));
835 param
->value
.itype
= strtol (value
, &e
, 10);
838 INVALID_VALUE (filename
, lineno
);
843 param
->value
.ltype
= strtol (value
, &e
, 10);
846 INVALID_VALUE (filename
, lineno
);
851 param
->value
.lltype
= strtoll (value
, &e
, 10);
854 INVALID_VALUE (filename
, lineno
);
858 case PARAM_ULONGLONG
:
859 param
->value
.ulltype
= strtoull (value
, &e
, 10);
862 INVALID_VALUE (filename
, lineno
);
867 param
->value
.ultype
= strtoul (value
, &e
, 10);
870 INVALID_VALUE (filename
, lineno
);
879 tmp
= slist_append (section
->params
, param
);
882 log_write ("%s", pwmd_strerror (ENOMEM
));
886 section
->params
= tmp
;
896 config_parse (const char *filename
)
898 struct slist_s
*tmpconfig
= NULL
, *tmp
;
899 struct section_s
*cur_section
= NULL
;
904 FILE *fp
= fopen (filename
, "r");
908 log_write ("%s: %s", filename
,
909 pwmd_strerror (gpg_error_from_syserror ()));
914 log_write (_("Using defaults!"));
918 for (; (s
= fgets (buf
, sizeof (buf
), fp
)); lineno
++)
920 char line
[LINE_MAX
] = { 0 };
931 /* New file section. */
934 struct section_s
*section
;
935 char *p
= strchr (++s
, ']');
939 log_write (_("%s(%i): unbalanced braces"), filename
,
944 len
= strlen (s
) - strlen (p
);
945 memcpy (line
, s
, len
);
948 section
= config_find_section (tmpconfig
, line
);
951 log_write (_("%s(%i): section '%s' already exists!"),
952 filename
, lineno
, line
);
956 if (!strcmp (line
, "global"))
959 section
= xcalloc (1, sizeof (struct section_s
));
960 section
->name
= str_dup (line
);
964 tmp
= slist_append (tmpconfig
, cur_section
);
967 log_write ("%s", pwmd_strerror (ENOMEM
));
974 cur_section
= section
;
980 log_write (_("%s(%i): parameter outside of section!"), filename
,
985 /* Parameters for each section. */
986 for (int m
= 0; config_params
[m
].name
; m
++)
988 size_t len
= strlen (config_params
[m
].name
);
990 if (!strncmp (s
, config_params
[m
].name
, len
))
994 while (*p
&& *p
== ' ')
997 if (!*p
|| *p
!= '=')
1001 while (*p
&& isspace (*p
))
1005 if (new_param (cur_section
, filename
, lineno
, s
, p
,
1006 config_params
[m
].type
))
1016 log_write (_("%s(%i): unknown parameter"), filename
, lineno
);
1028 tmp
= slist_append (tmpconfig
, cur_section
);
1031 log_write ("%s", pwmd_strerror (ENOMEM
));
1041 ("WARNING: %s: could not find a [global] configuration section!"),
1045 if (set_defaults (&tmpconfig
))
1052 config_free (tmpconfig
);
1053 free_section (cur_section
);
1058 free_section (struct section_s
*s
)
1065 struct param_s
*p
= slist_nth_data (s
->params
, 0);
1070 section_remove_param (s
, p
->name
);
1077 config_free (struct slist_s
*config
)
1081 struct section_s
*s
= slist_nth_data (config
, 0);
1086 config
= slist_remove (config
, s
);