Commit | Line | Data |
---|---|---|
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 | ||
a35a69c5 | 17 | #include <unistd.h> |
18 | #include <limits.h> | |
0f339959 | 19 | #include <signal.h> |
f8257c5d | 20 | #include <string.h> |
fd8852d9 | 21 | |
d15318db | 22 | #include <sys/param.h> |
f1ad9351 | 23 | #include <sys/sysctl.h> |
f8257c5d | 24 | #include <sys/sensors.h> |
25 | #include <sys/ioctl.h> | |
bd24ff31 | 26 | #include <sys/utsname.h> |
f8257c5d | 27 | |
28 | #include <machine/apmvar.h> | |
d15318db | 29 | |
4c04b514 | 30 | #include <glib/gprintf.h> |
8caf1f61 | 31 | #include <glib-unix.h> |
904d744d | 32 | #include <polkit/polkit.h> |
4c04b514 | 33 | |
1e8c7c88 | 34 | #include "hostnamed-gen.h" |
0f339959 | 35 | #include "hostnamed.h" |
a35a69c5 | 36 | |
ed4cf3c8 | 37 | #include "../../util.h" |
483e90b7 | 38 | |
a1bcc33c | 39 | /* format: { |
40 | * (1) string to be matched against runtime machine's sysctl output. | |
41 | * can be either the exact string or a substring contained | |
42 | * within sysctl strings. no "guesses" here, a match should | |
43 | * reliably indicate the chassis/icon. | |
44 | * | |
45 | * (2) string describing chassis type divulged by (1). | |
46 | * must be one of "desktop", "laptop", "server", | |
47 | * "tablet", "handset", "vm", "container" or NULL | |
48 | * if only icon string can be ascertained. "vm" refers | |
49 | * to operating systems running on baremetal hypervisors | |
50 | * (hardware virtualization, like XEN) while "container" | |
51 | * refers to OSs running on shared hypervisors like | |
52 | * virtualbox or VMware. consider the distinction carefully | |
53 | * as common virtualization software like KVM may share | |
54 | * characteristics of both "vm" and "container" types. | |
55 | * | |
56 | * (3) string specifying icon to use. follows XDG icon spec. | |
57 | * see http://standards.freedesktop.org/icon-naming-spec/icon-naming-spec-latest.html | |
58 | * for allowed strings. | |
59 | * | |
60 | * (4) chassis precedence bit. TRUE if (2) is defined and | |
61 | * we're certain it is the proper string. FALSE in the | |
62 | * circumstance (2) may be the correct string, unless | |
63 | * a match with this bit set to TRUE overrides it. | |
64 | * if (2) is NULL, this bit is inconsequential. | |
65 | * | |
66 | * (5) icon precedence bit. see previous definition. | |
67 | * } */ | |
68 | struct SYSCTL_LOOKUP_TABLE { | |
69 | gchar *match_string; | |
70 | gchar *chassis; | |
71 | gchar *icon; | |
72 | gboolean chassis_precedence; | |
73 | gboolean icon_precedence; | |
74 | }; | |
75 | ||
fd8852d9 | 76 | GPtrArray *hostnamed_freeable; |
1be94ede | 77 | Hostname1 *hostnamed_interf; |
3d53b501 | 78 | |
0f339959 | 79 | GMainLoop *hostnamed_loop; |
80 | ||
81 | guint bus_descriptor; | |
82 | gboolean dbus_interface_exported; /* reliable because of gdbus operational guarantees */ | |
83 | ||
19c6b83d | 84 | gchar *HOSTNAME, *STATIC_HOSTNAME, *PRETTY_HOSTNAME; |
a1bcc33c | 85 | gchar *CHASSIS, *ICON; |
19c6b83d | 86 | gchar *KERN_NAME, *KERN_RELEASE, *KERN_VERS, *OS_CPENAME; |
9d3b5b68 | 87 | gchar *LOCATION = NULL, *DEPLOYMENT = NULL; |
a1bcc33c | 88 | |
89 | /* TODO no specific vm or laptop icon in gnome | |
90 | * NOTE paravirtualization on xen is only available for linuxes right now | |
91 | * dmesg on linux systems reveals xen and virtualization method (HVM or PVM) | |
92 | * but we will worry about those later */ | |
c7028b11 | 93 | |
94 | /* add any sysctl strings that suggest virtualization here */ | |
a1bcc33c | 95 | const struct SYSCTL_LOOKUP_TABLE chassis_indicator_table[] = |
96 | { | |
87df323f | 97 | { "QEMU Virtual CPU", "vm", NULL, FALSE, FALSE }, /* could be QEMU running in userspace or as part of KVM */ |
6edc347a | 98 | { "KVM", "vm", "drive-multidisk", FALSE, FALSE }, |
b21074ae | 99 | { "SmartDC HVM", "vm", "drive-multidisk", TRUE, TRUE }, /* illumos-joyent kvm */ |
87df323f | 100 | { "VirtualBox", "vm", "drive-multidisk", TRUE, TRUE }, |
101 | { "VMware, Inc.", "vm", "drive-multidisk", TRUE, TRUE }, | |
102 | { "VMware Virtual Platform", "vm", "drive-multidisk", TRUE, TRUE }, | |
103 | { "Parallels", "vm", "drive-multidisk", TRUE, TRUE }, /* need verification */ | |
c7028b11 | 104 | { "Xen", "vm", "drive-multidisk", FALSE, FALSE } |
87df323f | 105 | }; /* TODO: chroots, etc. are the actual "containers", add them */ |
76b67a18 | 106 | |
f8257c5d | 107 | /* archs to check against when determining if machine is server */ |
108 | const gchar *server_archs[] = { | |
109 | "hppa", | |
110 | "sparc", | |
111 | "sparc64" | |
112 | }; | |
f1ad9351 | 113 | |
b08f1622 | 114 | static const gchar *DEFAULT_DOMAIN = ""; /* blank domains are OK for now */ |
df8fc341 | 115 | static const gchar *OS_HOSTNAME_PATH = "/etc/myname"; |
de46ace4 | 116 | static const gchar *OS_CONFIG_PATH = "/etc/machine-info"; |
9d3b5b68 | 117 | /* XXX */ |
118 | static const guint LOCATION_MAXSIZE = 4096; | |
119 | static const guint DEPLOYMENT_MAXSIZE = 4096; | |
120 | ||
df8fc341 | 121 | |
a1bcc33c | 122 | /* --- begin method/property/dbus signal code --- */ |
f1ad9351 | 123 | |
2bc9066a | 124 | /* TODO free some strings here */ |
3d53b501 | 125 | static gboolean |
1be94ede | 126 | on_handle_set_hostname(Hostname1 *hn1_passed_interf, |
3d53b501 | 127 | GDBusMethodInvocation *invoc, |
128 | const gchar *greet, | |
129 | gpointer data) { | |
5b70f403 | 130 | GVariant *params; |
131 | gchar *proposed_hostname, *valid_hostname_buf; | |
2bc9066a | 132 | const gchar *bus_name; |
133 | gboolean policykit_auth, ret, try_to_set; | |
3ccecdd6 | 134 | size_t check_length; |
2bc9066a | 135 | check_auth_result is_authed; |
5b70f403 | 136 | |
5b70f403 | 137 | proposed_hostname = NULL; |
2bc9066a | 138 | ret = try_to_set = FALSE; |
5b70f403 | 139 | |
140 | params = g_dbus_method_invocation_get_parameters(invoc); | |
141 | g_variant_get(params, "(sb)", &proposed_hostname, &policykit_auth); | |
2bc9066a | 142 | bus_name = g_dbus_method_invocation_get_sender(invoc); |
5b70f403 | 143 | |
2bc9066a | 144 | /* verify caller has correct permissions via polkit */ |
5fd84921 | 145 | is_authed = polkit_try_auth(bus_name, "org.freedesktop.hostname1.set-hostname", policykit_auth); |
5b70f403 | 146 | |
2bc9066a | 147 | switch(is_authed) { |
148 | ||
149 | case AUTHORIZED_NATIVELY: | |
150 | case AUTHORIZED_BY_PROMPT: | |
151 | try_to_set = TRUE; | |
152 | break; | |
153 | ||
154 | case UNAUTHORIZED_NATIVELY: | |
155 | case UNAUTHORIZED_FAILED_PROMPT: | |
156 | g_dbus_method_invocation_return_dbus_error(invoc, "org.freedesktop.hostname1.Error.EACCES", "Insufficient permissions to set hostname."); | |
157 | break; | |
158 | ||
159 | case ERROR_BADBUS: | |
160 | g_dbus_method_invocation_return_dbus_error(invoc, "org.freedesktop.hostname1.Error.EFAULT", "Provided bus name is invalid."); | |
161 | break; | |
162 | ||
163 | case ERROR_BADACTION: | |
164 | g_dbus_method_invocation_return_dbus_error(invoc, "org.freedesktop.hostname1.Error.EFAULT", "Provided action ID is invalid."); | |
165 | break; | |
166 | ||
167 | case ERROR_GENERIC: | |
168 | default: | |
169 | g_dbus_method_invocation_return_dbus_error(invoc, "org.freedesktop.hostname1.Error.ECANCELED", "Failed to set hostname for unknown reason."); | |
170 | break; | |
171 | } | |
172 | ||
173 | /* verify passed hostname's validity */ | |
174 | if(try_to_set && proposed_hostname && (valid_hostname_buf = g_hostname_to_ascii(proposed_hostname))) { | |
175 | ||
176 | check_length = strnlen(valid_hostname_buf, MAXHOSTNAMELEN + 1); | |
177 | ||
178 | if(check_length > MAXHOSTNAMELEN) { | |
5b70f403 | 179 | |
3ccecdd6 | 180 | g_dbus_method_invocation_return_dbus_error(invoc, "org.freedesktop.hostname1.Error.ENAMETOOLONG", "Hostname string exceeded maximum length."); |
2bc9066a | 181 | g_free(valid_hostname_buf); |
182 | ||
183 | } else if(sethostname(proposed_hostname, check_length)) { | |
184 | ||
185 | g_dbus_method_invocation_return_dbus_error(invoc, "org.freedesktop.hostname1.Error.ECANCELED", "Failed to set hostname for unknown reason."); | |
186 | g_free(valid_hostname_buf); | |
5b70f403 | 187 | |
2bc9066a | 188 | } else { |
5b70f403 | 189 | |
2bc9066a | 190 | HOSTNAME = valid_hostname_buf; |
3ccecdd6 | 191 | hostname1_set_hostname(hn1_passed_interf, HOSTNAME); |
2bc9066a | 192 | g_ptr_array_add(hostnamed_freeable, valid_hostname_buf); |
3ccecdd6 | 193 | ret = TRUE; |
194 | hostname1_complete_set_hostname(hn1_passed_interf, invoc); | |
195 | } | |
196 | } | |
5b70f403 | 197 | |
198 | return ret; | |
3d53b501 | 199 | } |
200 | ||
201 | static gboolean | |
1be94ede | 202 | on_handle_set_static_hostname(Hostname1 *hn1_passed_interf, |
3d53b501 | 203 | GDBusMethodInvocation *invoc, |
204 | const gchar *greet, | |
205 | gpointer data) { | |
933f62bb | 206 | |
207 | GVariant *params; | |
5ea060f8 | 208 | gchar *proposed_static_hostname, *valid_static_hostname_buf, *bsd_hostname_try; |
933f62bb | 209 | const gchar *bus_name; |
210 | gboolean policykit_auth, ret, try_to_set; | |
211 | size_t check_length; | |
212 | check_auth_result is_authed; | |
5ea060f8 | 213 | |
933f62bb | 214 | |
215 | proposed_static_hostname = NULL; | |
216 | ret = try_to_set = FALSE; | |
217 | ||
218 | params = g_dbus_method_invocation_get_parameters(invoc); | |
219 | g_variant_get(params, "(sb)", &proposed_static_hostname, &policykit_auth); | |
220 | bus_name = g_dbus_method_invocation_get_sender(invoc); | |
221 | ||
222 | /* verify caller has correct permissions via polkit */ | |
5fd84921 | 223 | is_authed = polkit_try_auth(bus_name, "org.freedesktop.hostname1.set-static-hostname", policykit_auth); |
933f62bb | 224 | |
225 | switch(is_authed) { | |
226 | ||
227 | case AUTHORIZED_NATIVELY: | |
228 | case AUTHORIZED_BY_PROMPT: | |
229 | try_to_set = TRUE; | |
230 | break; | |
231 | ||
232 | case UNAUTHORIZED_NATIVELY: | |
233 | case UNAUTHORIZED_FAILED_PROMPT: | |
234 | g_dbus_method_invocation_return_dbus_error(invoc, "org.freedesktop.hostname1.Error.EACCES", "Insufficient permissions to set static hostname."); | |
235 | break; | |
236 | ||
237 | case ERROR_BADBUS: | |
238 | g_dbus_method_invocation_return_dbus_error(invoc, "org.freedesktop.hostname1.Error.EFAULT", "Provided bus name is invalid."); | |
239 | break; | |
240 | ||
241 | case ERROR_BADACTION: | |
242 | g_dbus_method_invocation_return_dbus_error(invoc, "org.freedesktop.hostname1.Error.EFAULT", "Provided action ID is invalid."); | |
243 | break; | |
244 | ||
245 | case ERROR_GENERIC: | |
246 | default: | |
247 | g_dbus_method_invocation_return_dbus_error(invoc, "org.freedesktop.hostname1.Error.ECANCELED", "Failed to set static hostname for unknown reason."); | |
248 | break; | |
249 | } | |
250 | ||
251 | /* verify passed hostname's validity */ | |
252 | if(try_to_set && proposed_static_hostname && (valid_static_hostname_buf = g_hostname_to_ascii(proposed_static_hostname))) { | |
253 | ||
254 | check_length = strnlen(valid_static_hostname_buf, MAXHOSTNAMELEN + 1); | |
255 | ||
256 | if(check_length > MAXHOSTNAMELEN) { | |
257 | ||
258 | g_dbus_method_invocation_return_dbus_error(invoc, "org.freedesktop.hostname1.Error.ENAMETOOLONG", "Static hostname string exceeded maximum length."); | |
259 | g_free(valid_static_hostname_buf); | |
260 | ||
261 | } else if(!(STATIC_HOSTNAME = valid_static_hostname_buf)) { | |
262 | ||
263 | g_dbus_method_invocation_return_dbus_error(invoc, "org.freedesktop.hostname1.Error.ECANCELED", "Failed to set static hostname for unknown reason."); | |
264 | g_free(valid_static_hostname_buf); | |
265 | ||
266 | } else { | |
267 | ||
268 | g_strdelimit(STATIC_HOSTNAME, " ", '-'); | |
057ab9c2 | 269 | hostname1_set_static_hostname(hn1_passed_interf, STATIC_HOSTNAME); |
933f62bb | 270 | g_ptr_array_add(hostnamed_freeable, valid_static_hostname_buf); |
5ea060f8 | 271 | |
272 | /* set string in OS_HOSTNAME_PATH ("/etc/myname" on bsd) */ | |
273 | bsd_hostname_try = get_bsd_hostname(STATIC_HOSTNAME); | |
274 | GError *debug_error; | |
275 | if(!bsd_hostname_try || !g_file_set_contents(OS_HOSTNAME_PATH, bsd_hostname_try, -1, &debug_error)) | |
de46ace4 | 276 | g_printf("could not to write to %s! are you root?\n", OS_HOSTNAME_PATH); |
5ea060f8 | 277 | |
278 | if(bsd_hostname_try) | |
279 | g_free(bsd_hostname_try); | |
280 | ||
281 | /* call sethostname(3) too */ | |
057ab9c2 | 282 | ret = (!sethostname(valid_static_hostname_buf, MAXHOSTNAMELEN)) ? TRUE : FALSE; /* TODO set /etc/myname, guarantee domain or substitue .home.network" */ |
933f62bb | 283 | hostname1_complete_set_static_hostname(hn1_passed_interf, invoc); |
284 | } | |
285 | } | |
286 | ||
287 | return ret; | |
3d53b501 | 288 | } |
289 | ||
290 | static gboolean | |
1be94ede | 291 | on_handle_set_pretty_hostname(Hostname1 *hn1_passed_interf, |
3d53b501 | 292 | GDBusMethodInvocation *invoc, |
293 | const gchar *greet, | |
294 | gpointer data) { | |
933f62bb | 295 | |
296 | GVariant *params; | |
297 | gchar *proposed_pretty_hostname, *valid_pretty_hostname_buf, *computed_static_hostname; | |
298 | const gchar *bus_name; | |
299 | gboolean policykit_auth, ret, try_to_set; | |
300 | size_t check_length; | |
301 | check_auth_result is_authed; | |
933f62bb | 302 | |
933f62bb | 303 | proposed_pretty_hostname = NULL; |
304 | ret = try_to_set = FALSE; | |
305 | ||
306 | params = g_dbus_method_invocation_get_parameters(invoc); | |
307 | g_variant_get(params, "(sb)", &proposed_pretty_hostname, &policykit_auth); | |
308 | bus_name = g_dbus_method_invocation_get_sender(invoc); | |
309 | ||
310 | /* verify caller has correct permissions via polkit */ | |
5fd84921 | 311 | is_authed = polkit_try_auth(bus_name, "org.freedesktop.hostname1.set-pretty-hostname", policykit_auth); |
933f62bb | 312 | |
313 | switch(is_authed) { | |
314 | ||
315 | case AUTHORIZED_NATIVELY: | |
316 | case AUTHORIZED_BY_PROMPT: | |
317 | try_to_set = TRUE; | |
318 | break; | |
319 | ||
320 | case UNAUTHORIZED_NATIVELY: | |
321 | case UNAUTHORIZED_FAILED_PROMPT: | |
322 | g_dbus_method_invocation_return_dbus_error(invoc, "org.freedesktop.hostname1.Error.EACCES", "Insufficient permissions to set pretty hostname."); | |
323 | break; | |
324 | ||
325 | case ERROR_BADBUS: | |
326 | g_dbus_method_invocation_return_dbus_error(invoc, "org.freedesktop.hostname1.Error.EFAULT", "Provided bus name is invalid."); | |
327 | break; | |
328 | ||
329 | case ERROR_BADACTION: | |
330 | g_dbus_method_invocation_return_dbus_error(invoc, "org.freedesktop.hostname1.Error.EFAULT", "Provided action ID is invalid."); | |
331 | break; | |
332 | ||
333 | case ERROR_GENERIC: | |
334 | default: | |
335 | g_dbus_method_invocation_return_dbus_error(invoc, "org.freedesktop.hostname1.Error.ECANCELED", "Failed to set pretty hostname for unknown reason."); | |
336 | break; | |
337 | } | |
338 | ||
339 | /* verify passed hostname's validity */ | |
340 | if(try_to_set && proposed_pretty_hostname && (valid_pretty_hostname_buf = g_locale_to_utf8(proposed_pretty_hostname, -1, 0, 0, NULL))) { | |
341 | ||
342 | check_length = strnlen(valid_pretty_hostname_buf, MAXHOSTNAMELEN + 1); | |
343 | ||
344 | if(check_length > MAXHOSTNAMELEN) { | |
345 | ||
346 | g_dbus_method_invocation_return_dbus_error(invoc, "org.freedesktop.hostname1.Error.ENAMETOOLONG", "Static hostname string exceeded maximum length."); | |
347 | g_free(valid_pretty_hostname_buf); | |
348 | ||
349 | } else if(!(PRETTY_HOSTNAME = valid_pretty_hostname_buf)) { | |
350 | ||
351 | g_dbus_method_invocation_return_dbus_error(invoc, "org.freedesktop.hostname1.Error.ECANCELED", "Failed to set pretty hostname for unknown reason."); | |
352 | g_free(valid_pretty_hostname_buf); | |
353 | ||
354 | } else { | |
355 | ||
356 | hostname1_set_pretty_hostname(hn1_passed_interf, PRETTY_HOSTNAME); | |
357 | g_ptr_array_add(hostnamed_freeable, valid_pretty_hostname_buf); | |
358 | hostname1_complete_set_pretty_hostname(hn1_passed_interf, invoc); | |
359 | ret = TRUE; | |
360 | ||
de46ace4 | 361 | if(!config_set(OS_CONFIG_PATH, "PRETTY_HOSTNAME", PRETTY_HOSTNAME)) |
362 | g_printf("could not write to %s! are you root?\n", OS_CONFIG_PATH); | |
933f62bb | 363 | } |
364 | } | |
365 | ||
933f62bb | 366 | return ret; |
3d53b501 | 367 | } |
368 | ||
369 | static gboolean | |
1be94ede | 370 | on_handle_set_chassis(Hostname1 *hn1_passed_interf, |
3d53b501 | 371 | GDBusMethodInvocation *invoc, |
372 | const gchar *greet, | |
373 | gpointer data) { | |
933f62bb | 374 | |
375 | GVariant *params; | |
376 | gchar *proposed_chassis_name, *valid_chassis_name_buf; | |
377 | const gchar *bus_name; | |
378 | gboolean policykit_auth, ret, try_to_set; | |
379 | check_auth_result is_authed; | |
933f62bb | 380 | |
933f62bb | 381 | proposed_chassis_name = NULL; |
382 | ret = try_to_set = FALSE; | |
383 | valid_chassis_name_buf = (gchar *)g_malloc0(8192); | |
384 | ||
385 | params = g_dbus_method_invocation_get_parameters(invoc); | |
386 | g_variant_get(params, "(sb)", &proposed_chassis_name, &policykit_auth); | |
387 | bus_name = g_dbus_method_invocation_get_sender(invoc); | |
388 | ||
389 | g_strlcpy(valid_chassis_name_buf, proposed_chassis_name, (gsize)64); | |
390 | ||
391 | /* verify caller has correct permissions via polkit */ | |
5fd84921 | 392 | is_authed = polkit_try_auth(bus_name, "org.freedesktop.hostname1.set-chassis", policykit_auth); |
933f62bb | 393 | |
394 | switch(is_authed) { | |
395 | ||
396 | case AUTHORIZED_NATIVELY: | |
397 | case AUTHORIZED_BY_PROMPT: | |
398 | try_to_set = TRUE; | |
399 | break; | |
400 | ||
401 | case UNAUTHORIZED_NATIVELY: | |
402 | case UNAUTHORIZED_FAILED_PROMPT: | |
403 | g_dbus_method_invocation_return_dbus_error(invoc, "org.freedesktop.hostname1.Error.EACCES", "Insufficient permissions to set chassis type."); | |
404 | break; | |
405 | ||
406 | case ERROR_BADBUS: | |
407 | g_dbus_method_invocation_return_dbus_error(invoc, "org.freedesktop.hostname1.Error.EFAULT", "Provided bus name is invalid."); | |
408 | break; | |
409 | ||
410 | case ERROR_BADACTION: | |
411 | g_dbus_method_invocation_return_dbus_error(invoc, "org.freedesktop.hostname1.Error.EFAULT", "Provided action ID is invalid."); | |
412 | break; | |
413 | ||
414 | case ERROR_GENERIC: | |
415 | default: | |
416 | g_dbus_method_invocation_return_dbus_error(invoc, "org.freedesktop.hostname1.Error.ECANCELED", "Failed to set chassis type for unknown reason."); | |
417 | break; | |
418 | } | |
419 | ||
420 | /* verify passed chassis type's validity */ | |
421 | if(try_to_set && proposed_chassis_name) { | |
422 | ||
423 | if(!is_valid_chassis_type(proposed_chassis_name)) { | |
424 | ||
425 | g_dbus_method_invocation_return_dbus_error(invoc, "org.freedesktop.hostname1.Error.ECANCELED", "Chassis type must be 'desktop', 'laptop', 'server', 'tablet', 'handset', 'vm', or 'container'."); | |
426 | g_free(valid_chassis_name_buf); | |
427 | ||
428 | } else if(!(CHASSIS = valid_chassis_name_buf)) { | |
429 | ||
430 | g_dbus_method_invocation_return_dbus_error(invoc, "org.freedesktop.hostname1.Error.ECANCELED", "Failed to set chassis type for unknown reason."); | |
431 | g_free(valid_chassis_name_buf); | |
432 | ||
433 | } else { | |
434 | ||
435 | hostname1_set_chassis(hn1_passed_interf, CHASSIS); | |
436 | g_ptr_array_add(hostnamed_freeable, valid_chassis_name_buf); | |
437 | hostname1_complete_set_chassis(hn1_passed_interf, invoc); | |
de46ace4 | 438 | ret = TRUE; |
933f62bb | 439 | |
de46ace4 | 440 | if(!config_set(OS_CONFIG_PATH, "CHASSIS", valid_chassis_name_buf)) |
441 | g_printf("could not write to %s! are you root?\n", OS_CONFIG_PATH); | |
933f62bb | 442 | } |
443 | } | |
444 | ||
933f62bb | 445 | return ret; |
3d53b501 | 446 | } |
447 | ||
448 | static gboolean | |
1be94ede | 449 | on_handle_set_icon_name(Hostname1 *hn1_passed_interf, |
3d53b501 | 450 | GDBusMethodInvocation *invoc, |
451 | const gchar *greet, | |
452 | gpointer data) { | |
933f62bb | 453 | |
454 | GVariant *params; | |
455 | gchar *proposed_icon_name, *valid_icon_name_buf; | |
456 | const gchar *bus_name; | |
457 | gboolean policykit_auth, ret, try_to_set; | |
458 | check_auth_result is_authed; | |
933f62bb | 459 | |
933f62bb | 460 | proposed_icon_name = NULL; |
461 | ret = try_to_set = FALSE; | |
462 | ||
463 | params = g_dbus_method_invocation_get_parameters(invoc); | |
464 | g_variant_get(params, "(sb)", &proposed_icon_name, &policykit_auth); | |
465 | bus_name = g_dbus_method_invocation_get_sender(invoc); | |
466 | ||
467 | /* verify caller has correct permissions via polkit */ | |
5fd84921 | 468 | is_authed = polkit_try_auth(bus_name, "org.freedesktop.hostname1.set-icon-name", policykit_auth); |
933f62bb | 469 | |
470 | switch(is_authed) { | |
471 | ||
472 | case AUTHORIZED_NATIVELY: | |
473 | case AUTHORIZED_BY_PROMPT: | |
474 | try_to_set = TRUE; | |
475 | break; | |
476 | ||
477 | case UNAUTHORIZED_NATIVELY: | |
478 | case UNAUTHORIZED_FAILED_PROMPT: | |
479 | g_dbus_method_invocation_return_dbus_error(invoc, "org.freedesktop.hostname1.Error.EACCES", "Insufficient permissions to set icon name."); | |
480 | break; | |
481 | ||
482 | case ERROR_BADBUS: | |
483 | g_dbus_method_invocation_return_dbus_error(invoc, "org.freedesktop.hostname1.Error.EFAULT", "Provided bus name is invalid."); | |
484 | break; | |
485 | ||
486 | case ERROR_BADACTION: | |
487 | g_dbus_method_invocation_return_dbus_error(invoc, "org.freedesktop.hostname1.Error.EFAULT", "Provided action ID is invalid."); | |
488 | break; | |
489 | ||
490 | case ERROR_GENERIC: | |
491 | default: | |
492 | g_dbus_method_invocation_return_dbus_error(invoc, "org.freedesktop.hostname1.Error.ECANCELED", "Failed to set icon name for unknown reason."); | |
493 | break; | |
494 | } | |
495 | ||
496 | /* verify passed chassis type's validity */ | |
497 | if(try_to_set && proposed_icon_name) { | |
498 | ||
499 | g_strlcpy(valid_icon_name_buf, proposed_icon_name, (gsize)64); | |
500 | ||
501 | if(!(ICON = valid_icon_name_buf)) { | |
502 | ||
503 | g_dbus_method_invocation_return_dbus_error(invoc, "org.freedesktop.hostname1.Error.ECANCELED", "Failed to set icon name for unknown reason."); | |
504 | g_free(valid_icon_name_buf); | |
505 | ||
506 | } else { | |
507 | ||
508 | hostname1_set_icon_name(hn1_passed_interf, ICON); | |
509 | g_ptr_array_add(hostnamed_freeable, valid_icon_name_buf); | |
510 | hostname1_complete_set_icon_name(hn1_passed_interf, invoc); | |
de46ace4 | 511 | ret = TRUE; |
933f62bb | 512 | |
de46ace4 | 513 | if(!config_set(OS_CONFIG_PATH, "ICON_NAME", valid_icon_name_buf)) |
514 | g_printf("could not write to %s! are you root?\n", OS_CONFIG_PATH); | |
933f62bb | 515 | |
933f62bb | 516 | } |
517 | } | |
518 | ||
933f62bb | 519 | return ret; |
3d53b501 | 520 | } |
9d3b5b68 | 521 | static gboolean |
522 | on_handle_set_location(Hostname1 *hn1_passed_interf, | |
523 | GDBusMethodInvocation *invoc, | |
524 | const gchar *greet, | |
525 | gpointer data) { | |
526 | GVariant *params; | |
527 | const gchar *bus_name; | |
528 | gchar *proposed_location, *valid_location_buf; | |
529 | gboolean policykit_auth, ret, try_to_set; | |
530 | check_auth_result is_authed; | |
531 | ||
532 | ret = try_to_set = FALSE; | |
533 | ||
534 | params = g_dbus_method_invocation_get_parameters(invoc); | |
535 | g_variant_get(params, "(sb)", &proposed_location, &policykit_auth); | |
536 | bus_name = g_dbus_method_invocation_get_sender(invoc); | |
537 | ||
538 | /* verify caller has correct permissions via polkit */ | |
539 | is_authed = polkit_try_auth(bus_name, "org.freedesktop.hostname1.set-location", policykit_auth); | |
540 | ||
541 | switch(is_authed) { | |
542 | ||
543 | case AUTHORIZED_NATIVELY: | |
544 | case AUTHORIZED_BY_PROMPT: | |
545 | try_to_set = TRUE; | |
546 | break; | |
547 | ||
548 | case UNAUTHORIZED_NATIVELY: | |
549 | case UNAUTHORIZED_FAILED_PROMPT: | |
550 | g_dbus_method_invocation_return_dbus_error(invoc, "org.freedesktop.hostname1.Error.EACCES", "Insufficient permissions to set location."); | |
551 | return FALSE; | |
552 | break; | |
553 | ||
554 | case ERROR_BADBUS: | |
555 | g_dbus_method_invocation_return_dbus_error(invoc, "org.freedesktop.hostname1.Error.EFAULT", "Provided bus name is invalid."); | |
556 | return FALSE; | |
557 | break; | |
558 | ||
559 | case ERROR_BADACTION: | |
560 | g_dbus_method_invocation_return_dbus_error(invoc, "org.freedesktop.hostname1.Error.EFAULT", "Provided action ID is invalid."); | |
561 | return FALSE; | |
562 | break; | |
563 | ||
564 | case ERROR_GENERIC: | |
565 | default: | |
566 | g_dbus_method_invocation_return_dbus_error(invoc, "org.freedesktop.hostname1.Error.ECANCELED", "Failed to set location for unknown reason."); | |
567 | return FALSE; | |
568 | break; | |
569 | } | |
570 | /* XXX follow systemd impl here */ | |
571 | LOCATION = (gchar *) g_malloc0(LOCATION_MAXSIZE); | |
572 | g_strlcpy(LOCATION, proposed_location, LOCATION_MAXSIZE); | |
573 | hostname1_set_location(hn1_passed_interf, LOCATION); | |
574 | g_ptr_array_add(hostnamed_freeable, valid_location_buf); | |
575 | hostname1_complete_set_location(hn1_passed_interf, invoc); | |
576 | return TRUE; | |
577 | } | |
578 | ||
579 | static gboolean | |
580 | on_handle_set_deployment(Hostname1 *hn1_passed_interf, | |
581 | GDBusMethodInvocation *invoc, | |
582 | const gchar *greet, | |
583 | gpointer data) { | |
584 | GVariant *params; | |
585 | const gchar *bus_name; | |
586 | gchar *proposed_deployment, *valid_deployment_buf; | |
587 | gboolean policykit_auth, ret, try_to_set; | |
588 | check_auth_result is_authed; | |
589 | ||
590 | ret = try_to_set = FALSE; | |
591 | ||
592 | params = g_dbus_method_invocation_get_parameters(invoc); | |
593 | g_variant_get(params, "(sb)", &proposed_deployment, &policykit_auth); | |
594 | bus_name = g_dbus_method_invocation_get_sender(invoc); | |
595 | ||
596 | /* verify caller has correct permissions via polkit */ | |
597 | is_authed = polkit_try_auth(bus_name, "org.freedesktop.hostname1.set-deployment", policykit_auth); | |
598 | ||
599 | switch(is_authed) { | |
600 | ||
601 | case AUTHORIZED_NATIVELY: | |
602 | case AUTHORIZED_BY_PROMPT: | |
603 | try_to_set = TRUE; | |
604 | break; | |
605 | ||
606 | case UNAUTHORIZED_NATIVELY: | |
607 | case UNAUTHORIZED_FAILED_PROMPT: | |
608 | g_dbus_method_invocation_return_dbus_error(invoc, "org.freedesktop.hostname1.Error.EACCES", "Insufficient permissions to set deployment."); | |
609 | return FALSE; | |
610 | break; | |
611 | ||
612 | case ERROR_BADBUS: | |
613 | g_dbus_method_invocation_return_dbus_error(invoc, "org.freedesktop.hostname1.Error.EFAULT", "Provided bus name is invalid."); | |
614 | return FALSE; | |
615 | break; | |
616 | ||
617 | case ERROR_BADACTION: | |
618 | g_dbus_method_invocation_return_dbus_error(invoc, "org.freedesktop.hostname1.Error.EFAULT", "Provided action ID is invalid."); | |
619 | return FALSE; | |
620 | break; | |
621 | ||
622 | case ERROR_GENERIC: | |
623 | default: | |
624 | g_dbus_method_invocation_return_dbus_error(invoc, "org.freedesktop.hostname1.Error.ECANCELED", "Failed to set deployment for unknown reason."); | |
625 | return FALSE; | |
626 | break; | |
627 | } | |
628 | /* XXX follow systemd impl here */ | |
629 | DEPLOYMENT = (gchar *) g_malloc0(DEPLOYMENT_MAXSIZE); | |
630 | g_strlcpy(DEPLOYMENT, proposed_deployment, DEPLOYMENT_MAXSIZE); | |
631 | hostname1_set_deployment(hn1_passed_interf, DEPLOYMENT); | |
632 | g_ptr_array_add(hostnamed_freeable, valid_deployment_buf); | |
633 | hostname1_complete_set_deployment(hn1_passed_interf, invoc); | |
634 | return TRUE; | |
635 | } | |
3d53b501 | 636 | |
76b67a18 | 637 | /* note: all hostnamed/hostname1's properties are read-only, |
638 | * and do not need set_ functions, gdbus-codegen realized | |
639 | * this from the XML and handled the to-be error of trying | |
640 | * to set a read-only property's value | |
641 | */ | |
642 | ||
643 | const gchar * | |
644 | our_get_hostname() { | |
645 | ||
3ccecdd6 | 646 | gchar *hostname_buf; |
647 | hostname_buf = (gchar *)g_malloc0(MAXHOSTNAMELEN); | |
648 | ||
649 | if(gethostname(hostname_buf, MAXHOSTNAMELEN)) | |
650 | return "localhost.home.network"; /* TODO bomb out here probably */ | |
651 | ||
652 | else if(!g_strcmp0(HOSTNAME, hostname_buf)) { | |
653 | ||
654 | g_free(hostname_buf); | |
19c6b83d | 655 | return HOSTNAME; |
3ccecdd6 | 656 | } |
657 | ||
658 | g_ptr_array_add(hostnamed_freeable, hostname_buf); | |
659 | HOSTNAME = hostname_buf; | |
660 | hostname1_set_hostname(hostnamed_interf, HOSTNAME); | |
19c6b83d | 661 | |
3ccecdd6 | 662 | return HOSTNAME; |
76b67a18 | 663 | } |
664 | ||
665 | const gchar * | |
666 | our_get_static_hostname() { | |
667 | ||
3ccecdd6 | 668 | if(STATIC_HOSTNAME && g_strcmp0(STATIC_HOSTNAME, "")) |
19c6b83d | 669 | return STATIC_HOSTNAME; |
670 | else if(HOSTNAME) | |
671 | return HOSTNAME; | |
672 | ||
3ccecdd6 | 673 | return "localhost.home.network"; |
76b67a18 | 674 | } |
675 | ||
676 | const gchar * | |
677 | our_get_pretty_hostname() { | |
678 | ||
19c6b83d | 679 | if(PRETTY_HOSTNAME) |
680 | return PRETTY_HOSTNAME; | |
681 | ||
a2fffc07 | 682 | return ""; |
76b67a18 | 683 | } |
684 | ||
685 | const gchar * | |
686 | our_get_chassis() { | |
687 | ||
f8257c5d | 688 | if(CHASSIS) |
689 | return CHASSIS; | |
f1ad9351 | 690 | |
19c6b83d | 691 | return "desktop"; /* this leads to the most generic beheivor in the unlikely case its returned */ |
76b67a18 | 692 | } |
693 | ||
694 | const gchar * | |
695 | our_get_icon_name() { | |
696 | ||
f8257c5d | 697 | if(ICON) |
698 | return ICON; | |
699 | ||
700 | return ""; | |
76b67a18 | 701 | } |
702 | ||
703 | const gchar * | |
704 | our_get_kernel_name() { | |
705 | ||
bd24ff31 | 706 | if(KERN_NAME) |
707 | return KERN_NAME; | |
708 | ||
709 | return ""; | |
76b67a18 | 710 | } |
711 | ||
712 | const gchar * | |
713 | our_get_kernel_version() { | |
714 | ||
bd24ff31 | 715 | if(KERN_VERS) |
716 | return KERN_VERS; | |
717 | ||
718 | return ""; | |
76b67a18 | 719 | } |
720 | ||
721 | const gchar * | |
722 | our_get_kernel_release() { | |
723 | ||
bd24ff31 | 724 | if(KERN_RELEASE) |
725 | return KERN_RELEASE; | |
726 | ||
727 | return ""; | |
76b67a18 | 728 | } |
729 | ||
730 | const gchar * | |
731 | our_get_os_cpename() { | |
732 | ||
3808ecc5 AJ |
733 | /* XXX needs to parse /etc/os-release (fallback to /usr/local/lib/os-release) */ |
734 | return ""; | |
76b67a18 | 735 | } |
736 | ||
737 | const gchar * | |
738 | our_get_os_pretty_name() { | |
739 | ||
bd24ff31 | 740 | return "OpenBSD"; |
76b67a18 | 741 | } |
742 | ||
9d3b5b68 | 743 | const gchar * |
744 | our_get_location() { | |
745 | ||
746 | if(LOCATION) | |
747 | return LOCATION; | |
748 | return ""; | |
749 | } | |
750 | ||
751 | const gchar * | |
752 | our_get_deployment() { | |
753 | ||
754 | if(DEPLOYMENT) | |
755 | return DEPLOYMENT; | |
756 | return ""; | |
757 | } | |
1ce41045 | 758 | /* --- end method/property/dbus signal code, begin bus/name handlers --- */ |
0df0018d | 759 | |
5b005882 | 760 | static void hostnamed_on_bus_acquired(GDBusConnection *conn, |
9cab3afe | 761 | const gchar *name, |
762 | gpointer user_data) { | |
d1e1db9e | 763 | |
0f339959 | 764 | g_printf("got bus/name, exporting %s's interface...\n", name); |
d1e1db9e | 765 | |
90f54407 | 766 | hostnamed_interf = hostname1_skeleton_new(); |
3d53b501 | 767 | |
76b67a18 | 768 | /* attach function pointers to generated struct's method handlers */ |
3d53b501 | 769 | g_signal_connect(hostnamed_interf, "handle-set-hostname", G_CALLBACK(on_handle_set_hostname), NULL); |
770 | g_signal_connect(hostnamed_interf, "handle-set-static-hostname", G_CALLBACK(on_handle_set_static_hostname), NULL); | |
771 | g_signal_connect(hostnamed_interf, "handle-set-pretty-hostname", G_CALLBACK(on_handle_set_pretty_hostname), NULL); | |
772 | g_signal_connect(hostnamed_interf, "handle-set-chassis", G_CALLBACK(on_handle_set_chassis), NULL); | |
773 | g_signal_connect(hostnamed_interf, "handle-set-icon-name", G_CALLBACK(on_handle_set_icon_name), NULL); | |
9d3b5b68 | 774 | g_signal_connect(hostnamed_interf, "handle-set-deployment", G_CALLBACK(on_handle_set_deployment), NULL); |
775 | g_signal_connect(hostnamed_interf, "handle-set-location", G_CALLBACK(on_handle_set_location), NULL); | |
3d53b501 | 776 | |
76b67a18 | 777 | /* set our properties before export */ |
1be94ede | 778 | hostname1_set_hostname(hostnamed_interf, our_get_hostname()); |
779 | hostname1_set_static_hostname(hostnamed_interf, our_get_static_hostname()); | |
780 | hostname1_set_pretty_hostname(hostnamed_interf, our_get_pretty_hostname()); | |
781 | hostname1_set_chassis(hostnamed_interf, our_get_chassis()); | |
782 | hostname1_set_icon_name(hostnamed_interf, our_get_icon_name()); | |
783 | hostname1_set_kernel_name(hostnamed_interf, our_get_kernel_name()); | |
784 | hostname1_set_kernel_version(hostnamed_interf, our_get_kernel_version()); | |
785 | hostname1_set_kernel_release(hostnamed_interf, our_get_kernel_release()); | |
786 | hostname1_set_operating_system_cpename(hostnamed_interf, our_get_os_cpename()); | |
787 | hostname1_set_operating_system_pretty_name(hostnamed_interf, our_get_os_pretty_name()); | |
9d3b5b68 | 788 | hostname1_set_deployment(hostnamed_interf, our_get_deployment()); |
789 | hostname1_set_location(hostnamed_interf, our_get_location()); | |
790 | ||
76b67a18 | 791 | |
3d53b501 | 792 | if(!g_dbus_interface_skeleton_export(G_DBUS_INTERFACE_SKELETON(hostnamed_interf), |
1be94ede | 793 | conn, |
794 | "/org/freedesktop/hostname1", | |
795 | NULL)) { | |
3d53b501 | 796 | |
0f339959 | 797 | g_printf("failed to export %s's interface!\n", name); /* unusual edge case, TODO check errno */ |
90f54407 | 798 | hostnamed_mem_clean(); |
3d53b501 | 799 | |
0f339959 | 800 | } else { |
90f54407 | 801 | dbus_interface_exported = TRUE; |
802 | g_printf("exported %s's interface on the system bus...\n", name); | |
803 | } | |
0f339959 | 804 | } |
1e8c7c88 | 805 | |
0f339959 | 806 | static void hostnamed_on_name_acquired(GDBusConnection *conn, |
90f54407 | 807 | const gchar *name, |
0f339959 | 808 | gpointer user_data) { |
809 | ||
810 | g_printf("success!\n"); | |
1e8c7c88 | 811 | } |
812 | ||
5b005882 | 813 | static void hostnamed_on_name_lost(GDBusConnection *conn, |
1be94ede | 814 | const gchar *name, |
815 | gpointer user_data) { | |
80043b36 | 816 | |
90f54407 | 817 | if(!conn) { |
0f339959 | 818 | |
90f54407 | 819 | g_printf("failed to connect to the system bus while trying to acquire name '%s': either dbus-daemon isn't running or we don't have permission to push names and/or their interfaces to it.\n", name); |
820 | hostnamed_mem_clean(); | |
821 | } | |
0f339959 | 822 | |
509599f0 | 823 | g_printf("lost name %s, exiting...\n", name); |
fd8852d9 | 824 | |
1cd5e6fe | 825 | hostnamed_mem_clean(); |
0f339959 | 826 | } |
827 | ||
828 | /* --- end bus/name handlers, begin misc unix functions --- */ | |
829 | ||
830 | /* safe call to clean and then exit | |
509599f0 | 831 | * this stops our GMainLoop safely before letting main() return */ |
0f339959 | 832 | void hostnamed_mem_clean() { |
833 | ||
90f54407 | 834 | g_printf("exiting...\n"); |
0f339959 | 835 | |
90f54407 | 836 | if(dbus_interface_exported) |
837 | g_dbus_interface_skeleton_unexport(G_DBUS_INTERFACE_SKELETON(hostnamed_interf)); | |
0f339959 | 838 | |
90f54407 | 839 | if(g_main_loop_is_running(hostnamed_loop)) |
840 | g_main_loop_quit(hostnamed_loop); | |
fd8852d9 | 841 | |
ea207ed3 | 842 | } |
843 | ||
0f339959 | 844 | /* wrapper for glib's unix signal handling; called only once if terminatating signal is raised against us */ |
845 | gboolean unix_sig_terminate_handler(gpointer data) { | |
c62bceb7 | 846 | |
90f54407 | 847 | g_printf("caught SIGINT/HUP/TERM, exiting\n"); |
0f339959 | 848 | |
90f54407 | 849 | hostnamed_mem_clean(); |
850 | return G_SOURCE_REMOVE; | |
0f339959 | 851 | } |
852 | ||
853 | void set_signal_handlers() { | |
854 | ||
90f54407 | 855 | /* we don't care about its descriptor, we never need to unregister these */ |
856 | g_unix_signal_add(SIGINT, unix_sig_terminate_handler, NULL); | |
857 | g_unix_signal_add(SIGHUP, unix_sig_terminate_handler, NULL); | |
858 | g_unix_signal_add(SIGTERM, unix_sig_terminate_handler, NULL); | |
04cc16f2 | 859 | |
90f54407 | 860 | /* TODO: the "only once" guarantee only counts towards specific signals. |
861 | * make sure calling a SIGINT and SIGHUP doesn't cause term_handler() | |
862 | * to be called twice */ | |
0f339959 | 863 | } |
864 | ||
865 | int main() { | |
bd24ff31 | 866 | |
867 | hostnamed_freeable = g_ptr_array_new(); | |
868 | ||
a0ebb315 | 869 | /* TODO: check for valid, writable config at init. if no, complain to `make install` */ |
bd24ff31 | 870 | |
df8fc341 | 871 | get_bsd_hostname("adsf"); /* TODO KILL ME */ |
872 | ||
bd24ff31 | 873 | CHASSIS = ICON = OS_CPENAME = 0; |
874 | KERN_NAME = KERN_RELEASE = KERN_VERS = 0; | |
baf05b70 | 875 | HOSTNAME = STATIC_HOSTNAME = PRETTY_HOSTNAME = NULL; |
483e90b7 | 876 | |
90f54407 | 877 | set_signal_handlers(); |
387173cb | 878 | |
46835f3e | 879 | if(!determine_chassis_and_icon() || !set_uname_properties() || !set_names()) |
a1bcc33c | 880 | return 1; |
881 | ||
baf05b70 | 882 | hostnamed_loop = g_main_loop_new(NULL, TRUE); |
fd8852d9 | 883 | |
90f54407 | 884 | bus_descriptor = g_bus_own_name(G_BUS_TYPE_SYSTEM, |
0f339959 | 885 | "org.freedesktop.hostname1", |
886 | G_BUS_NAME_OWNER_FLAGS_NONE, | |
887 | hostnamed_on_bus_acquired, | |
888 | hostnamed_on_name_acquired, | |
889 | hostnamed_on_name_lost, | |
890 | NULL, | |
891 | NULL); | |
496f5d66 | 892 | |
90f54407 | 893 | g_main_loop_run(hostnamed_loop); |
894 | /* runs until single g_main_loop_quit() call is raised inside <interface>_mem_clean() */ | |
895 | g_main_loop_unref(hostnamed_loop); | |
1e8c7c88 | 896 | |
90f54407 | 897 | /* guaranteed unownable */ |
898 | g_bus_unown_name(bus_descriptor); | |
c62bceb7 | 899 | |
90f54407 | 900 | /* at this point no operations can occur with our data, it is safe to free it + its container */ |
901 | g_ptr_array_free(hostnamed_freeable, TRUE); | |
7323a4e4 | 902 | |
90f54407 | 903 | return 0; |
a35a69c5 | 904 | } |
905 | ||
46835f3e | 906 | gboolean set_names() { |
907 | ||
baf05b70 | 908 | /* (1) set up */ |
9d3b5b68 | 909 | gchar *hostname_buf, *static_hostname_buf, *pretty_hostname_buf, *location_buf, *deployment_buf; |
46835f3e | 910 | size_t hostname_divider; |
911 | ||
baf05b70 | 912 | hostname_buf = (gchar*) g_malloc0(MAXHOSTNAMELEN); |
913 | static_hostname_buf = (gchar*) g_malloc0(4096); | |
914 | pretty_hostname_buf = (gchar*) g_malloc0(4096); | |
9d3b5b68 | 915 | location_buf = (gchar*) g_malloc0(LOCATION_MAXSIZE); |
916 | deployment_buf = (gchar*) g_malloc0(DEPLOYMENT_MAXSIZE); | |
46835f3e | 917 | |
baf05b70 | 918 | g_ptr_array_add(hostnamed_freeable, hostname_buf); |
919 | g_ptr_array_add(hostnamed_freeable, static_hostname_buf); | |
920 | g_ptr_array_add(hostnamed_freeable, pretty_hostname_buf); | |
9d3b5b68 | 921 | g_ptr_array_add(hostnamed_freeable, location_buf); |
922 | g_ptr_array_add(hostnamed_freeable, deployment_buf); | |
46835f3e | 923 | |
baf05b70 | 924 | /* (2) set HOSTNAME */ |
925 | if(gethostname(hostname_buf, MAXHOSTNAMELEN) || !g_strcmp0(hostname_buf, "")) | |
926 | HOSTNAME = "localhost"; | |
46835f3e | 927 | |
baf05b70 | 928 | HOSTNAME = hostname_buf; |
46835f3e | 929 | |
baf05b70 | 930 | /* this bit gets you the /etc/myname style hostname |
931 | hostname_divider = strcspn(hostname_buf, "."); | |
932 | strncpy(ret, hostname_buf, hostname_divider); */ | |
46835f3e | 933 | |
baf05b70 | 934 | /* (3) set PRETTY_HOSTNAME */ |
de46ace4 | 935 | if((pretty_hostname_buf = config_get(OS_CONFIG_PATH, "PRETTY_HOSTNAME"))) |
baf05b70 | 936 | PRETTY_HOSTNAME = pretty_hostname_buf; |
de46ace4 | 937 | |
baf05b70 | 938 | else |
939 | PRETTY_HOSTNAME = ""; | |
940 | ||
45e09604 | 941 | |
942 | /* (4) set STATIC_HOSTNAME */ | |
de46ace4 | 943 | if(!gethostname(static_hostname_buf, MAXHOSTNAMELEN)) |
45e09604 | 944 | STATIC_HOSTNAME = static_hostname_buf; |
46835f3e | 945 | |
45e09604 | 946 | else |
947 | STATIC_HOSTNAME = ""; | |
46835f3e | 948 | |
9d3b5b68 | 949 | /* XXX */ |
950 | location_buf = ""; | |
951 | LOCATION = location_buf; | |
952 | deployment_buf = ""; | |
953 | DEPLOYMENT = deployment_buf; | |
954 | ||
955 | return (HOSTNAME && STATIC_HOSTNAME && PRETTY_HOSTNAME && LOCATION && DEPLOYMENT) ? TRUE : FALSE; | |
46835f3e | 956 | } |
957 | ||
bd24ff31 | 958 | gboolean set_uname_properties() { |
959 | ||
960 | struct utsname un; | |
961 | ||
962 | if(-1 == uname(&un)) | |
963 | return FALSE; | |
964 | ||
965 | KERN_NAME = (gchar*)g_malloc0(sizeof(un.sysname)); | |
966 | g_ptr_array_add(hostnamed_freeable, KERN_NAME); | |
967 | g_strlcpy(KERN_NAME, un.sysname, sizeof(un.sysname)); | |
968 | ||
969 | KERN_RELEASE = (gchar*)g_malloc0(sizeof(un.release)); | |
970 | g_ptr_array_add(hostnamed_freeable, KERN_RELEASE); | |
971 | g_strlcpy(KERN_RELEASE, un.release, sizeof(un.release)); | |
972 | ||
973 | KERN_VERS = (gchar*)g_malloc0(sizeof(un.version)); | |
974 | g_ptr_array_add(hostnamed_freeable, KERN_VERS); | |
975 | g_strlcpy(KERN_VERS, un.version, sizeof(un.version)); | |
976 | ||
977 | return TRUE; | |
978 | } | |
979 | ||
a1bcc33c | 980 | gboolean determine_chassis_and_icon() { |
981 | ||
b81ab32a | 982 | const size_t bufsize = 4096; |
983 | ||
f8257c5d | 984 | char *hwproduct, *hwmodel, *hwvendor, *hwmachine; |
985 | size_t hwproduct_size, hwmodel_size, hwvendor_size, hwmachine_size; | |
986 | int hwproduct_name[2], hwmodel_name[2], hwvendor_name[2], hwmachine_name[2]; | |
f1ad9351 | 987 | unsigned int i; |
f8257c5d | 988 | gboolean UNSURE_CHASSIS_FLAG, UNSURE_ICON_FLAG; |
989 | ||
b81ab32a | 990 | hwproduct_size = hwmodel_size = hwvendor_size = hwmachine_size = bufsize; |
68e777c6 | 991 | UNSURE_CHASSIS_FLAG = UNSURE_ICON_FLAG = FALSE; |
5ac7f542 | 992 | i = 0; |
f1ad9351 | 993 | |
b81ab32a | 994 | hwproduct = (char*)g_malloc0(4096); |
995 | hwmodel = (char*)g_malloc0(4096); | |
996 | hwvendor = (char*)g_malloc0(4096); | |
997 | hwmachine = (char*)g_malloc0(4096); | |
998 | ||
bd24ff31 | 999 | g_ptr_array_add(hostnamed_freeable, hwproduct); |
1000 | g_ptr_array_add(hostnamed_freeable, hwmodel); | |
1001 | g_ptr_array_add(hostnamed_freeable, hwvendor); | |
1002 | g_ptr_array_add(hostnamed_freeable, hwmachine); | |
1003 | ||
a1bcc33c | 1004 | hwproduct_name[0] = CTL_HW; |
1005 | hwproduct_name[1] = HW_PRODUCT; | |
f1ad9351 | 1006 | |
a1bcc33c | 1007 | hwmodel_name[0] = CTL_HW; |
1008 | hwmodel_name[1] = HW_MODEL; | |
1009 | ||
1010 | hwvendor_name[0] = CTL_HW; | |
1011 | hwvendor_name[1] = HW_VENDOR; | |
1012 | ||
f8257c5d | 1013 | hwmachine_name[0] = CTL_HW; |
1014 | hwmachine_name[1] = HW_MACHINE; | |
1015 | ||
a1bcc33c | 1016 | /* pass NULL buffer to check size first, then pass hw to be filled according to freshly-set hw_size */ |
1017 | if(-1 == sysctl(hwproduct_name, 2, NULL, &hwproduct_size, NULL, 0) || -1 == sysctl(hwproduct_name, 2, hwproduct, &hwproduct_size, NULL, 0)) | |
1018 | return FALSE; | |
1019 | ||
1020 | if(-1 == sysctl(hwmodel_name, 2, NULL, &hwmodel_size, NULL, 0) || -1 == sysctl(hwmodel_name, 2, hwmodel, &hwmodel_size, NULL, 0)) | |
1021 | return FALSE; | |
1022 | ||
1023 | if(-1 == sysctl(hwvendor_name, 2, NULL, &hwvendor_size, NULL, 0) || -1 == sysctl(hwvendor_name, 2, hwvendor, &hwvendor_size, NULL, 0)) | |
1024 | return FALSE; | |
1025 | ||
f8257c5d | 1026 | if(-1 == sysctl(hwmachine_name, 2, NULL, &hwmachine_size, NULL, 0) || -1 == sysctl(hwmachine_name, 2, hwmachine, &hwmachine_size, NULL, 0)) |
1027 | return FALSE; | |
1028 | ||
a1bcc33c | 1029 | /* TODO: test for laptop, if not, dmidecode for desktop vs. server |
1030 | * probably move this code to vm test func and set a global after running it early, once */ | |
1031 | ||
f8257c5d | 1032 | for(; i < G_N_ELEMENTS(chassis_indicator_table); i++) { |
1033 | if(strcasestr(hwproduct, chassis_indicator_table[i].match_string) | |
1034 | || strcasestr(hwmodel, chassis_indicator_table[i].match_string) | |
1035 | || strcasestr(hwvendor, chassis_indicator_table[i].match_string)) { | |
1036 | ||
1037 | if(!UNSURE_CHASSIS_FLAG && chassis_indicator_table[i].chassis) { | |
1038 | ||
1039 | UNSURE_CHASSIS_FLAG = chassis_indicator_table[i].chassis_precedence; | |
1040 | CHASSIS = chassis_indicator_table[i].chassis; | |
1041 | } | |
1042 | ||
1043 | if(!UNSURE_ICON_FLAG && chassis_indicator_table[i].icon) { | |
1044 | ||
1045 | UNSURE_ICON_FLAG = chassis_indicator_table[i].icon_precedence; | |
1046 | ICON = chassis_indicator_table[i].icon; | |
1047 | } | |
1048 | } | |
1049 | } | |
1050 | ||
1051 | if(up_native_is_laptop()) { | |
1052 | ||
1053 | if(!CHASSIS) | |
1054 | CHASSIS = "laptop"; | |
1055 | if(!ICON) | |
1056 | ICON = "input-touchpad"; /* TODO pull an icon package that actually has the icons we're looking for */ | |
1057 | ||
1058 | } else if(is_server(hwmachine)) { | |
1059 | ||
1060 | if(!CHASSIS) | |
1061 | CHASSIS = "server"; | |
1062 | if(!ICON) | |
1063 | ICON = "uninterruptible-power-supply"; | |
1064 | ||
1065 | } else if(!CHASSIS || !ICON) { | |
f1ad9351 | 1066 | |
f8257c5d | 1067 | if(!CHASSIS) |
1068 | CHASSIS = "desktop"; | |
1069 | if(!ICON) | |
1070 | ICON = "computer"; | |
1071 | } | |
1072 | ||
1073 | return (CHASSIS && ICON); | |
1074 | } | |
1075 | ||
1076 | gboolean is_server(gchar *arch) { | |
1077 | ||
1078 | unsigned int i; | |
a1bcc33c | 1079 | |
f8257c5d | 1080 | for(; i < G_N_ELEMENTS(server_archs); i++) |
1081 | if(strcasestr(arch, server_archs[i])) | |
1082 | return TRUE; | |
1083 | ||
1084 | return FALSE; | |
1085 | } | |
1086 | ||
1087 | gboolean up_native_is_laptop() { | |
1088 | ||
1089 | struct apm_power_info bstate; | |
1090 | struct sensordev acpiac; | |
1091 | ||
1092 | if (up_native_get_sensordev("acpiac0", &acpiac)) | |
1093 | return TRUE; | |
1094 | ||
1095 | if (-1 == ioctl(up_apm_get_fd(), APM_IOC_GETPOWER, &bstate)) | |
1096 | g_error("ioctl on apm fd failed : %s", g_strerror(errno)); | |
1097 | ||
1098 | return bstate.ac_state != APM_AC_UNKNOWN; | |
1099 | } | |
1100 | ||
1101 | int up_apm_get_fd() { | |
1102 | ||
1103 | static int apm_fd = 0; | |
1104 | ||
1105 | if(apm_fd == 0) { | |
1106 | ||
1107 | g_debug("apm_fd is not initialized yet, opening"); | |
1108 | ||
1109 | /* open /dev/apm */ | |
1110 | if((apm_fd = open("/dev/apm", O_RDONLY)) == -1) { | |
1111 | if(errno != ENXIO && errno != ENOENT) | |
1112 | g_error("cannot open device file"); | |
1113 | } | |
1114 | } | |
1115 | ||
1116 | return apm_fd; | |
1117 | } | |
1118 | ||
1119 | gboolean up_native_get_sensordev(const char * id, struct sensordev * snsrdev) { | |
1120 | ||
1121 | int devn; | |
1122 | size_t sdlen = sizeof(struct sensordev); | |
1123 | int mib[] = {CTL_HW, HW_SENSORS, 0, 0 ,0}; | |
1124 | ||
1125 | for (devn = 0 ; ; devn++) { | |
1126 | mib[2] = devn; | |
1127 | if(sysctl(mib, 3, snsrdev, &sdlen, NULL, 0) == -1) { | |
1128 | if(errno == ENXIO) | |
1129 | continue; | |
1130 | if(errno == ENOENT) | |
1131 | break; | |
1132 | } | |
1133 | ||
1134 | if (!strcmp(snsrdev->xname, id)) | |
1135 | return TRUE; | |
1136 | } | |
1137 | ||
1138 | return FALSE; | |
f1ad9351 | 1139 | } |
933f62bb | 1140 | |
1141 | static gboolean is_valid_chassis_type(gchar *test) { | |
1142 | ||
1143 | if(!g_strcmp0(test, "desktop") || | |
1144 | !g_strcmp0(test, "laptop") || | |
1145 | !g_strcmp0(test, "server") || | |
1146 | !g_strcmp0(test, "tablet") || | |
1147 | !g_strcmp0(test, "handset") || | |
1148 | !g_strcmp0(test, "vm") || | |
1149 | !g_strcmp0(test, "container") || | |
1150 | !g_strcmp0(test, "")) | |
1151 | return TRUE; | |
1152 | ||
1153 | return FALSE; | |
df8fc341 | 1154 | } |
1155 | ||
1156 | /* returns a proper, bsd-style FQDN hostname safe to write to /etc/myname | |
1157 | * if proposed_hostname does not contain an appended domain, the one in /etc/myname is substituted. | |
1158 | * failing that, DEFAULT_DOMAIN is used. NULL if proposed_hostname is invalid | |
1159 | * returns string that should be g_free()'d, or NULL if passed an invalid hostname */ | |
1160 | static gchar *get_bsd_hostname(gchar *proposed_hostname) { | |
1161 | ||
1162 | gchar *bsd_hostname, *ascii_translated_hostname, **myname_contents, *passed_domain, *temp_buf; | |
1163 | size_t domain_len, check_len; | |
1164 | gboolean read_result; | |
1165 | ||
1166 | g_strdelimit(proposed_hostname, "`~!@#$%^&*()_=+[{]}|:;'\"\\", '-'); | |
1167 | ||
1168 | ascii_translated_hostname = g_hostname_to_ascii(proposed_hostname); | |
1169 | check_len = strnlen(ascii_translated_hostname, MAXHOSTNAMELEN); | |
1170 | ||
1171 | if(!ascii_translated_hostname || !check_len || check_len > MAXHOSTNAMELEN || !g_strcmp0("", ascii_translated_hostname) || !g_strcmp0(".", ascii_translated_hostname)) { | |
1172 | ||
1173 | bsd_hostname = NULL; | |
1174 | passed_domain = NULL; | |
1175 | myname_contents = NULL; | |
1176 | ||
1177 | } else if((passed_domain = has_domain(ascii_translated_hostname))) { | |
1178 | ||
1179 | bsd_hostname = (gchar *) g_malloc0(MAXHOSTNAMELEN); | |
1180 | g_strlcpy(bsd_hostname, ascii_translated_hostname, MAXHOSTNAMELEN); | |
1181 | ||
1182 | passed_domain = NULL; | |
1183 | myname_contents = NULL; | |
1184 | ||
1185 | } else { | |
1186 | ||
1187 | myname_contents = (gchar **) g_malloc0(MAXHOSTNAMELEN * 2); | |
1188 | read_result = g_file_get_contents(OS_HOSTNAME_PATH, myname_contents, NULL, NULL); | |
1189 | ||
1190 | if(read_result && (passed_domain = has_domain(myname_contents[0]))) { | |
1191 | ||
1192 | domain_len = strnlen(passed_domain, MAXHOSTNAMELEN); | |
1193 | ||
1194 | if((domain_len + check_len) > MAXHOSTNAMELEN) | |
1195 | bsd_hostname = NULL; | |
1196 | else | |
1197 | bsd_hostname = g_strconcat(ascii_translated_hostname, passed_domain, NULL); | |
1198 | ||
1199 | } else if(myname_contents[0]) { | |
1200 | ||
1201 | g_printf("%s does not contain a proper FQDN! this is a significant error on BSD machines, otherwise OK.\nfalling back to default domain, '%s'\n", OS_HOSTNAME_PATH, DEFAULT_DOMAIN); | |
1202 | ||
1203 | domain_len = strnlen(DEFAULT_DOMAIN, MAXHOSTNAMELEN); | |
1204 | ||
1205 | if((domain_len + check_len) > MAXHOSTNAMELEN) | |
1206 | bsd_hostname = NULL; | |
1207 | else | |
1208 | bsd_hostname = g_strconcat(ascii_translated_hostname, DEFAULT_DOMAIN, NULL); | |
1209 | ||
1210 | } else { | |
1211 | ||
1212 | g_printf("could not read hostname at %s, this is a major error\n", OS_HOSTNAME_PATH); | |
1213 | bsd_hostname = NULL; | |
1214 | passed_domain = (gchar *) g_malloc0(MAXHOSTNAMELEN); | |
1215 | } | |
1216 | } | |
1217 | ||
1218 | if(passed_domain) | |
1219 | g_free(passed_domain); | |
1220 | if(myname_contents) | |
1221 | g_free(myname_contents); | |
1222 | ||
1223 | if(bsd_hostname && !strchr(bsd_hostname, '\n')) { | |
1224 | ||
1225 | temp_buf = bsd_hostname; | |
1226 | bsd_hostname = g_strconcat(bsd_hostname, "\n", NULL); | |
1227 | g_free(temp_buf); | |
1228 | } | |
1229 | ||
1230 | return bsd_hostname; | |
1231 | } | |
1232 | ||
1233 | /* returns NULL if no domain, otherwise append-appropriate domain string you must g_free() | |
1234 | * i.e. has_domain("foo.bar.com") returns ".bar.com" | |
1235 | * only pass g_hostname_to_ascii'd strings */ | |
1236 | static gchar *has_domain(const gchar *test) { | |
1237 | ||
1238 | size_t hostname_len, full_len; | |
1239 | gchar *ret; | |
1240 | ||
1241 | hostname_len = strcspn(test, "."); | |
1242 | full_len = strnlen(test, MAXHOSTNAMELEN); | |
1243 | ||
1244 | if(full_len == hostname_len) | |
1245 | return NULL; | |
1246 | ||
1247 | ret = (gchar *) g_malloc0(MAXHOSTNAMELEN); | |
1248 | g_strlcpy(ret, &test[hostname_len], MAXHOSTNAMELEN); | |
1249 | ||
1250 | return ret; | |
1251 | } |