merge license, hostname buffer changes from master branch
[systembsd.git] / src / config.c
CommitLineData
baa40e28 1/*
2 * Copyright (c) 2014 Ian Sutton <ian@kremlin.cc>
3 *
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
16
1c38000a 17#include <unistd.h>
18#include <fcntl.h>
19#include <gio/gio.h>
20
21static GKeyFile *config;
7d10ea85 22static int config_descr;
1c38000a 23
4241079f 24static gchar *data_dir;
25
26static int hostnamed_ispect_xml_descr, hostnamed_dbus_xml_descr;
27static int localed_ispect_xml_descr, localed_dbus_xml_descr;
28static int timedated_ispect_xml_descr, timedated_dbus_xml_descr;
29static int logind_ispect_xml_descr, logind_dbus_xml_descr;
30
31/*TODO depending on builtin xml flag, these should be matched to checksums */
32static gchar **hostnamed_ispect_xml, hostnamed_dbus_xml;
33static gchar **localed_ispect_xml, localed_dbus_xml;
34static gchar **timedated_ispect_xml, timedated_dbus_xml;
35static gchar **logind_ispect_xml, logind_dbus_xml;
36
1c38000a 37static const gchar *CONFIG_KEYS[] = {
fbc0295f 38 "PrettyHostname",
39 "IconName",
40 "ChassisType"
1c38000a 41};
42
43/* NULL if key doesn't exist */
44gchar *get_option(gchar *key, gchar *group) {
45
fbc0295f 46 if(!group)
47 group = "default";
1c38000a 48
fbc0295f 49 return g_key_file_get_string(config, group, key, NULL);
1c38000a 50}
51
52/* false if key isn't already defined or value is invalid */
53gboolean set_option(gchar *key, gchar *value, gchar *group) {
54
fbc0295f 55 if(!group)
56 group = "default";
1c38000a 57
fbc0295f 58 if(!g_key_file_get_string(config, group, key, NULL))
59 return FALSE;
1c38000a 60
fbc0295f 61 //TODO safteycheck value
62 g_key_file_set_string(config, group, key, value);
63 return TRUE;
1c38000a 64}
65
66/* initial load/check */
1c38000a 67gboolean config_init() {
68
fbc0295f 69 if(config)
70 return TRUE; //already init'd
c3b84b0a 71
fbc0295f 72 config = g_key_file_new();
73
74 const gchar *config_path;
75 GStatBuf *config_lstat;
c3b84b0a 76
fbc0295f 77 config_path = "/etc/systemd_compat.conf";
c3b84b0a 78
fbc0295f 79 /* does conf exist? */
80 if(g_lstat(config_path, config_lstat)) {
c3b84b0a 81
fbc0295f 82 /* if not, can we write it */
83 if(g_access("/etc/", W_OK)) {
84 g_printf("%s\n", "no write permissions for /etc/! exiting..");
85 return FALSE;
86 }
c3b84b0a 87
fbc0295f 88 int config_descr;
89 config_descr = g_open(config_path, O_CREAT, 644);
c3b84b0a 90
fbc0295f 91 gchar *posix_hostname;
92 posix_hostname = g_malloc(HOST_NAME_MAX);
c3b84b0a 93
fbc0295f 94 gethostname(posix_hostname, HOST_NAME_MAX);
c3b84b0a 95
fbc0295f 96 g_key_file_set_string(config, "hostnamed", "Hostname", posix_hostname);
97 g_key_file_set_string(config, "hostnamed", "PrettyHostname", "");
98 g_key_file_set_string(config, "hostnamed", "IconName", "Computer");
99 g_key_file_set_string(config, "hostnamed", "ChassisType", "laptop"); //TODO set these correctly
c3b84b0a 100
fbc0295f 101 if(!g_key_file_save_to_file(config, config_path, NULL)) {
102 g_printf("failed to write config to %s!\n", config_path);
103 g_free(posix_hostname);
104 return FALSE;
105 }
c3b84b0a 106
fbc0295f 107 g_printf("wrote config to %s\n", config_path);
c3b84b0a 108
fbc0295f 109 g_free(posix_hostname);
c3b84b0a 110
fbc0295f 111 return TRUE;
1c38000a 112
fbc0295f 113 /* it does exist, read it */
114 } else {
1c38000a 115
fbc0295f 116 if(!g_access(config_path, W_OK)) {
117 g_printf("%s\n", "no write permissions for /etc/! exiting..");
118 return FALSE;
119 } else if(g_key_file_load_from_file(config, config_path, G_KEY_FILE_KEEP_COMMENTS, NULL))
120 return TRUE;
1c38000a 121
fbc0295f 122 g_printf("could not read config at %s! exiting..", config_path);
123 return FALSE;
124 }
1c38000a 125}
7d10ea85 126}