1 /* vim:tw=78:ts=8:sw=4:set ft=c: */
3 Copyright (C) 2006, 2007, 2008, 2009, 2010, 2011, 2012
4 Ben Kibbey <bjk@luxsci.net>
6 This file is part of pwmd.
8 Pwmd is free software: you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation, either version 2 of the License, or
11 (at your option) any later version.
13 Pwmd is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with Pwmd. If not, see <http://www.gnu.org/licenses/>.
25 #include <sys/types.h>
30 #include "pwmd-error.h"
37 /* This is a string so ealier versions of glib can be used. */
38 #define DEFAULT_CIPHER_ITERATIONS "0"
40 void clear_rcfile_keys()
46 groups
= g_key_file_get_groups(keyfileh
, &n
);
48 for (p
= groups
; *p
; p
++) {
49 if (g_key_file_has_key(keyfileh
, *p
, "passphrase", NULL
) == TRUE
)
50 g_key_file_remove_key(keyfileh
, *p
, "passphrase", NULL
);
56 gchar
*get_key_file_string(const gchar
*section
, const gchar
*what
)
61 MUTEX_LOCK(&rcfile_mutex
);
62 section
= section
? section
: "global";
64 if (g_key_file_has_key(keyfileh
, section
, what
, NULL
) == TRUE
) {
65 val
= g_key_file_get_string(keyfileh
, section
, what
, &grc
);
68 log_write("%s(%i): %s", __FILE__
, __LINE__
, grc
->message
);
73 if (g_key_file_has_key(keyfileh
, "global", what
, NULL
) == TRUE
) {
74 val
= g_key_file_get_string(keyfileh
, "global", what
, &grc
);
77 log_write("%s(%i): %s", __FILE__
, __LINE__
, grc
->message
);
83 MUTEX_UNLOCK(&rcfile_mutex
);
87 gint
get_key_file_integer(const gchar
*section
, const gchar
*what
)
92 MUTEX_LOCK(&rcfile_mutex
);
94 if (g_key_file_has_key(keyfileh
, section
? section
: "global", what
, NULL
) == TRUE
) {
95 val
= g_key_file_get_integer(keyfileh
, section
? section
: "global", what
, &grc
);
98 log_write("%s(%i): %s", __FILE__
, __LINE__
, grc
->message
);
103 if (g_key_file_has_key(keyfileh
, "global", what
, NULL
) == TRUE
) {
104 val
= g_key_file_get_integer(keyfileh
, "global", what
, &grc
);
107 log_write("%s(%i): %s", __FILE__
, __LINE__
, grc
->message
);
113 MUTEX_UNLOCK(&rcfile_mutex
);
117 gulong
get_key_file_ulong(const gchar
*section
, const gchar
*what
)
122 MUTEX_LOCK(&rcfile_mutex
);
124 if (g_key_file_has_key(keyfileh
, section
? section
: "global", what
, NULL
) == TRUE
) {
125 val
= g_key_file_get_string(keyfileh
, section
? section
: "global", what
, &grc
);
128 log_write("%s(%i): %s", __FILE__
, __LINE__
, grc
->message
);
133 if (g_key_file_has_key(keyfileh
, "global", what
, NULL
) == TRUE
) {
134 val
= g_key_file_get_string(keyfileh
, "global", what
, &grc
);
137 log_write("%s(%i): %s", __FILE__
, __LINE__
, grc
->message
);
143 MUTEX_UNLOCK(&rcfile_mutex
);
145 gulong n
= strtoul(val
, NULL
, 10);
153 gboolean
get_key_file_boolean(const gchar
*section
, const gchar
*what
)
155 gboolean val
= FALSE
;
158 MUTEX_LOCK(&rcfile_mutex
);
160 if (g_key_file_has_key(keyfileh
, section
? section
: "global", what
, NULL
)
162 val
= g_key_file_get_boolean(keyfileh
, section
? section
: "global",
166 log_write("%s(%i): %s", __FILE__
, __LINE__
, grc
->message
);
171 if (g_key_file_has_key(keyfileh
, "global", what
, NULL
) == TRUE
) {
172 val
= g_key_file_get_boolean(keyfileh
, "global", what
, &grc
);
175 log_write("%s(%i): %s", __FILE__
, __LINE__
, grc
->message
);
181 MUTEX_UNLOCK(&rcfile_mutex
);
185 GKeyFile
*parse_rcfile(gboolean specified
, gboolean cmdline
)
187 GKeyFile
*kf
= g_key_file_new();
190 g_key_file_set_list_separator(kf
, ',');
192 if (g_key_file_load_from_file(kf
, rcfile
, G_KEY_FILE_NONE
, &rc
) == FALSE
) {
193 log_write("%s: %s", rcfile
, rc
->message
);
195 if (cmdline
&& specified
) {
200 if (rc
->code
&& rc
->code
!= G_FILE_ERROR_NOENT
) {
206 set_rcfile_defaults(kf
);
210 void setup_logging(GKeyFile
*kf
)
212 gboolean n
= g_key_file_get_boolean(kf
, "global", "enable_logging", NULL
);
215 gchar
*p
= g_key_file_get_string(kf
, "global", "log_path", NULL
);
217 logfile
= expand_homedir(p
);
225 log_syslog
= g_key_file_get_boolean(kf
, "global", "syslog", NULL
);
229 * Make sure all settings are set to either the specified setting or a
232 void set_rcfile_defaults(GKeyFile
*kf
)
236 if (g_key_file_has_key(kf
, "global", "backup", NULL
) == FALSE
)
237 g_key_file_set_boolean(kf
, "global", "backup", TRUE
);
239 if (g_key_file_has_key(kf
, "global", "log_path", NULL
) == FALSE
) {
240 g_snprintf(buf
, sizeof(buf
), "~/.pwmd/log");
241 g_key_file_set_string(kf
, "global", "log_path", buf
);
244 if (g_key_file_has_key(kf
, "global", "enable_logging", NULL
) == FALSE
)
245 g_key_file_set_boolean(kf
, "global", "enable_logging", FALSE
);
248 if (g_key_file_has_key(kf
, "global", "disable_mlockall", NULL
) == FALSE
)
249 g_key_file_set_boolean(kf
, "global", "disable_mlockall", TRUE
);
252 if (g_key_file_has_key(kf
, "global", "cache_timeout", NULL
) == FALSE
)
253 g_key_file_set_integer(kf
, "global", "cache_timeout", -1);
255 if (g_key_file_has_key(kf
, "global", "disable_list_and_dump", NULL
) == FALSE
)
256 g_key_file_set_boolean(kf
, "global", "disable_list_and_dump", FALSE
);
258 if (g_key_file_has_key(kf
, "global", "recursion_depth", NULL
) == FALSE
)
259 g_key_file_set_integer(kf
, "global", "recursion_depth", 100);
261 max_recursion_depth
= g_key_file_get_integer(kf
, "global", "recursion_depth", NULL
);
262 disable_list_and_dump
= g_key_file_get_boolean(kf
, "global", "disable_list_and_dump", NULL
);
265 disable_mlock
= g_key_file_get_boolean(kf
, "global", "disable_mlockall", NULL
);
268 if (g_key_file_has_key(kf
, "global", "syslog", NULL
) == FALSE
)
269 g_key_file_set_boolean(kf
, "global", "syslog", FALSE
);
271 if (g_key_file_has_key(kf
, "global", "xfer_progress", NULL
) == FALSE
)
272 g_key_file_set_integer(kf
, "global", "xfer_progress", 8196);
274 if (!g_key_file_has_key(kf
, "global", "allowed", NULL
)) {
275 const gchar
*users
[] = { g_get_user_name(), NULL
};
277 g_key_file_set_string_list(kf
, "global", "allowed", users
, 1);
280 if (g_key_file_has_key(kf
, "global", "nbits", NULL
) == FALSE
)
281 g_key_file_set_integer(kf
, "global", "nbits", 2048);
283 if (g_key_file_has_key(kf
, "global", "algo", NULL
) == FALSE
)
284 g_key_file_set_string(kf
, "global", "algo", "rsa");
286 if (g_key_file_has_key(kf
, "global", "cipher", NULL
) == FALSE
)
287 g_key_file_set_string(kf
, "global", "cipher", "aes256");
289 if (g_key_file_has_key(kf
, "global", "kill_scd", NULL
) == FALSE
)
290 g_key_file_set_boolean(kf
, "global", "kill_scd", FALSE
);
293 if (g_key_file_has_key(kf
, "global", "tcp_port", NULL
) == FALSE
)
294 g_key_file_set_integer(kf
, "global", "tcp_port", 6466);
296 if (g_key_file_has_key(kf
, "global", "enable_tcp", NULL
) == FALSE
)
297 g_key_file_set_boolean(kf
, "global", "enable_tcp", FALSE
);
299 if (g_key_file_has_key(kf
, "global", "tcp_require_key", NULL
) == FALSE
)
300 g_key_file_set_boolean(kf
, "global", "tcp_require_key", FALSE
);
302 if (g_key_file_has_key(kf
, "global", "tcp_wait", NULL
) == FALSE
)
303 g_key_file_set_boolean(kf
, "global", "tcp_wait", 3);
305 if (g_key_file_has_key(kf
, "global", "tcp_bind", NULL
) == FALSE
)
306 g_key_file_set_string(kf
, "global", "tcp_bind", "any");
308 if (g_key_file_has_key(kf
, "global", "tls_cipher_suite", NULL
) == FALSE
)
309 g_key_file_set_string(kf
, "global", "tls_cipher_suite", "SECURE256");
311 if (g_key_file_has_key(kf
, "global", "tls_use_crl", NULL
) == FALSE
)
312 g_key_file_set_boolean(kf
, "global", "tls_use_crl", FALSE
);
314 if (g_key_file_has_key(kf
, "global", "tls_access", NULL
) == FALSE
)
315 g_key_file_set_string(kf
, "global", "tls_access", "");
318 if (g_key_file_has_key(kf
, "global", "cipher_iterations", NULL
) == FALSE
)
319 g_key_file_set_string(kf
, "global", "cipher_iterations",
320 DEFAULT_CIPHER_ITERATIONS
);
322 if (g_key_file_has_key(kf
, "global", "cipher_progress", NULL
) == FALSE
)
323 g_key_file_set_string(kf
, "global", "cipher_progress",
324 DEFAULT_ITERATION_PROGRESS
);