3b82e3c1 |
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 | |
36575bff |
17 | #include <unistd.h> |
18 | #include <fcntl.h> |
19 | #include <gio/gio.h> |
20 | |
21 | static GKeyFile *config; |
d099276b |
22 | static int config_descr; |
36575bff |
23 | |
5b435dd1 |
24 | static gchar *data_dir; |
25 | |
8f18585d |
26 | /*static int hostnamed_ispect_xml_descr, hostnamed_dbus_xml_descr; |
5b435dd1 |
27 | static int localed_ispect_xml_descr, localed_dbus_xml_descr; |
28 | static int timedated_ispect_xml_descr, timedated_dbus_xml_descr; |
29 | static int logind_ispect_xml_descr, logind_dbus_xml_descr; |
30 | |
8f18585d |
31 | /*TODO depending on builtin xml flag, these should be matched to checksums */ /* |
5b435dd1 |
32 | static gchar **hostnamed_ispect_xml, hostnamed_dbus_xml; |
33 | static gchar **localed_ispect_xml, localed_dbus_xml; |
34 | static gchar **timedated_ispect_xml, timedated_dbus_xml; |
8f18585d |
35 | static gchar **logind_ispect_xml, logind_dbus_xml; */ |
5b435dd1 |
36 | |
36575bff |
37 | static const gchar *CONFIG_KEYS[] = { |
1cd5e6fe |
38 | "PrettyHostname", |
39 | "IconName", |
40 | "ChassisType" |
36575bff |
41 | }; |
42 | |
43 | /* NULL if key doesn't exist */ |
44 | gchar *get_option(gchar *key, gchar *group) { |
45 | |
1cd5e6fe |
46 | if(!group) |
47 | group = "default"; |
36575bff |
48 | |
1cd5e6fe |
49 | return g_key_file_get_string(config, group, key, NULL); |
36575bff |
50 | } |
51 | |
52 | /* false if key isn't already defined or value is invalid */ |
53 | gboolean set_option(gchar *key, gchar *value, gchar *group) { |
54 | |
1cd5e6fe |
55 | if(!group) |
56 | group = "default"; |
36575bff |
57 | |
1cd5e6fe |
58 | if(!g_key_file_get_string(config, group, key, NULL)) |
59 | return FALSE; |
36575bff |
60 | |
1cd5e6fe |
61 | //TODO safteycheck value |
62 | g_key_file_set_string(config, group, key, value); |
63 | return TRUE; |
36575bff |
64 | } |
65 | |
66 | /* initial load/check */ |
36575bff |
67 | gboolean config_init() { |
68 | |
1cd5e6fe |
69 | if(config) |
70 | return TRUE; //already init'd |
39df6847 |
71 | |
1cd5e6fe |
72 | config = g_key_file_new(); |
73 | |
74 | const gchar *config_path; |
75 | GStatBuf *config_lstat; |
39df6847 |
76 | |
1cd5e6fe |
77 | config_path = "/etc/systemd_compat.conf"; |
39df6847 |
78 | |
1cd5e6fe |
79 | /* does conf exist? */ |
80 | if(g_lstat(config_path, config_lstat)) { |
39df6847 |
81 | |
1cd5e6fe |
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 | } |
39df6847 |
87 | |
1cd5e6fe |
88 | int config_descr; |
89 | config_descr = g_open(config_path, O_CREAT, 644); |
39df6847 |
90 | |
1cd5e6fe |
91 | gchar *posix_hostname; |
92 | posix_hostname = g_malloc(HOST_NAME_MAX); |
39df6847 |
93 | |
1cd5e6fe |
94 | gethostname(posix_hostname, HOST_NAME_MAX); |
39df6847 |
95 | |
1cd5e6fe |
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 |
39df6847 |
100 | |
1cd5e6fe |
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 | } |
39df6847 |
106 | |
1cd5e6fe |
107 | g_printf("wrote config to %s\n", config_path); |
39df6847 |
108 | |
1cd5e6fe |
109 | g_free(posix_hostname); |
39df6847 |
110 | |
1cd5e6fe |
111 | return TRUE; |
36575bff |
112 | |
1cd5e6fe |
113 | /* it does exist, read it */ |
114 | } else { |
36575bff |
115 | |
1cd5e6fe |
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; |
36575bff |
121 | |
1cd5e6fe |
122 | g_printf("could not read config at %s! exiting..", config_path); |
123 | return FALSE; |
124 | } |
446ae274 |
125 | } |
8f18585d |
126 | |
127 | gboolean init_xml() { |
128 | |
129 | const gchar * const *data_dir_prefix; |
130 | |
131 | data_dir_prefix = g_get_system_data_dirs(); |
132 | data_dir = g_strconcat(data_dir_prefix[0], "systemd_compat", NULL); |
133 | |
134 | GStatBuf *xml_lstat; |
135 | |
136 | /* does xml dir exist? */ |
137 | if(g_lstat(data_dir, xml_lstat)) { |
138 | |
139 | /* if not, can we write it? */ |
140 | if(g_access(data_dir_prefix[0], W_OK)) { |
141 | g_printf("no write permissions for %s! exiting...\n", data_dir_prefix[0]); |
142 | return FALSE; |
143 | } |
144 | |
145 | g_printf("creating xml data directory %s...\n", data_dir); |
146 | if(g_mkdir(data_dir, 644)) { |
147 | g_printf("failed to create dir %s...\n", data_dir); |
148 | return FALSE; |
149 | } |
150 | |
151 | //set_xml_descriptors(); |
8f18585d |
152 | } |
36575bff |
153 | } |
446ae274 |
154 | |
8f18585d |
155 | //LEFTOFF |
156 | |
157 | /* gchar *posix_hostname; |
158 | posix_hostname = g_malloc(255); |
159 | |
160 | gethostname(posix_hostname, 255); |
161 | |
162 | g_key_file_set_string(config, "hostnamed", "Hostname", posix_hostname); |
163 | g_key_file_set_string(config, "hostnamed", "PrettyHostname", ""); |
164 | g_key_file_set_string(config, "hostnamed", "IconName", "Computer"); |
165 | g_key_file_set_string(config, "hostnamed", "ChassisType", "laptop"); //TODO set these correctly |
166 | |
167 | if(!g_key_file_save_to_file(config, config_path, NULL)) { |
168 | g_printf("failed to write config to %s!\n", config_path); |
169 | g_free(posix_hostname); |
170 | return FALSE; |
171 | } |
172 | |
173 | g_printf("wrote config to %s\n", config_path); |
174 | |
175 | g_free(posix_hostname); |
176 | |
177 | return TRUE; |
178 | |
179 | /* it does exist, read it */ /* |
180 | } else { |
181 | |
182 | if(!g_access(config_path, W_OK)) { |
183 | g_printf("%s\n", "no write permissions for /etc/! exiting.."); |
184 | return FALSE; |
185 | } else if(g_key_file_load_from_file(config, config_path, G_KEY_FILE_KEEP_COMMENTS, NULL)) { |
186 | config_descr = g_open(config_path, O_RDWR, 644); |
187 | return TRUE; |
188 | } |
189 | |
190 | g_printf("could not read config at %s! exiting..", config_path); |
191 | return FALSE; |
192 | } |
193 | |
194 | |
195 | return TRUE; |
196 | } */ |
197 | |
446ae274 |
198 | static void set_xml_descriptors() { |
8f18585d |
199 | |
200 | } |
201 | |
202 | void clean_config() { |
203 | |
204 | //TODO g_ptr_array all of this |
205 | g_free(config); |
206 | g_free(data_dir); |
207 | g_close(config_descr, NULL); |
446ae274 |
208 | } |