2 * Copyright (c) 2014 Ian Sutton <ian@kremlin.cc>
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.
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.
22 #include <sys/param.h>
23 #include <sys/sysctl.h>
24 #include <sys/sensors.h>
25 #include <sys/ioctl.h>
26 #include <sys/utsname.h>
28 #include <machine/apmvar.h>
30 #include <glib/gprintf.h>
31 #include <glib-unix.h>
32 #include <polkit/polkit.h>
34 #include "hostnamed-gen.h"
35 #include "hostnamed.h"
37 #include "../../util.h"
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.
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.
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.
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.
66 * (5) icon precedence bit. see previous definition.
68 struct SYSCTL_LOOKUP_TABLE
{
72 gboolean chassis_precedence
;
73 gboolean icon_precedence
;
76 GPtrArray
*hostnamed_freeable
;
77 Hostname1
*hostnamed_interf
;
79 GMainLoop
*hostnamed_loop
;
82 gboolean dbus_interface_exported
; /* reliable because of gdbus operational guarantees */
84 gchar
*HOSTNAME
, *STATIC_HOSTNAME
, *PRETTY_HOSTNAME
;
85 gchar
*CHASSIS
, *ICON
;
86 gchar
*KERN_NAME
, *KERN_RELEASE
, *KERN_VERS
, *OS_CPENAME
;
87 gchar
*LOCATION
= NULL
, *DEPLOYMENT
= NULL
;
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 */
94 /* add any sysctl strings that suggest virtualization here */
95 const struct SYSCTL_LOOKUP_TABLE chassis_indicator_table
[] =
97 { "QEMU Virtual CPU", "vm", NULL
, FALSE
, FALSE
}, /* could be QEMU running in userspace or as part of KVM */
98 { "KVM", "vm", "drive-multidisk", FALSE
, FALSE
},
99 { "SmartDC HVM", "vm", "drive-multidisk", TRUE
, TRUE
}, /* illumos-joyent kvm */
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 */
104 { "Xen", "vm", "drive-multidisk", FALSE
, FALSE
}
105 }; /* TODO: chroots, etc. are the actual "containers", add them */
107 /* archs to check against when determining if machine is server */
108 const gchar
*server_archs
[] = {
114 static const gchar
*DEFAULT_DOMAIN
= ""; /* blank domains are OK for now */
115 static const gchar
*OS_HOSTNAME_PATH
= "/etc/myname";
116 static const gchar
*OS_CONFIG_PATH
= "/etc/machine-info";
118 static const guint LOCATION_MAXSIZE
= 4096;
119 static const guint DEPLOYMENT_MAXSIZE
= 4096;
122 /* --- begin method/property/dbus signal code --- */
124 /* TODO free some strings here */
126 on_handle_set_hostname(Hostname1
*hn1_passed_interf
,
127 GDBusMethodInvocation
*invoc
,
131 gchar
*proposed_hostname
, *valid_hostname_buf
;
132 const gchar
*bus_name
;
133 gboolean policykit_auth
, ret
, try_to_set
;
135 check_auth_result is_authed
;
137 proposed_hostname
= NULL
;
138 ret
= try_to_set
= FALSE
;
140 params
= g_dbus_method_invocation_get_parameters(invoc
);
141 g_variant_get(params
, "(sb)", &proposed_hostname
, &policykit_auth
);
142 bus_name
= g_dbus_method_invocation_get_sender(invoc
);
144 /* verify caller has correct permissions via polkit */
145 is_authed
= polkit_try_auth(bus_name
, "org.freedesktop.hostname1.set-hostname", policykit_auth
);
149 case AUTHORIZED_NATIVELY
:
150 case AUTHORIZED_BY_PROMPT
:
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.");
160 g_dbus_method_invocation_return_dbus_error(invoc
, "org.freedesktop.hostname1.Error.EFAULT", "Provided bus name is invalid.");
163 case ERROR_BADACTION
:
164 g_dbus_method_invocation_return_dbus_error(invoc
, "org.freedesktop.hostname1.Error.EFAULT", "Provided action ID is invalid.");
169 g_dbus_method_invocation_return_dbus_error(invoc
, "org.freedesktop.hostname1.Error.ECANCELED", "Failed to set hostname for unknown reason.");
173 /* verify passed hostname's validity */
174 if(try_to_set
&& proposed_hostname
&& (valid_hostname_buf
= g_hostname_to_ascii(proposed_hostname
))) {
176 check_length
= strnlen(valid_hostname_buf
, MAXHOSTNAMELEN
+ 1);
178 if(check_length
> MAXHOSTNAMELEN
) {
180 g_dbus_method_invocation_return_dbus_error(invoc
, "org.freedesktop.hostname1.Error.ENAMETOOLONG", "Hostname string exceeded maximum length.");
181 g_free(valid_hostname_buf
);
183 } else if(sethostname(proposed_hostname
, check_length
)) {
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
);
190 HOSTNAME
= valid_hostname_buf
;
191 hostname1_set_hostname(hn1_passed_interf
, HOSTNAME
);
192 g_ptr_array_add(hostnamed_freeable
, valid_hostname_buf
);
194 hostname1_complete_set_hostname(hn1_passed_interf
, invoc
);
202 on_handle_set_static_hostname(Hostname1
*hn1_passed_interf
,
203 GDBusMethodInvocation
*invoc
,
208 gchar
*proposed_static_hostname
, *valid_static_hostname_buf
, *bsd_hostname_try
;
209 const gchar
*bus_name
;
210 gboolean policykit_auth
, ret
, try_to_set
;
212 check_auth_result is_authed
;
215 proposed_static_hostname
= NULL
;
216 ret
= try_to_set
= FALSE
;
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
);
222 /* verify caller has correct permissions via polkit */
223 is_authed
= polkit_try_auth(bus_name
, "org.freedesktop.hostname1.set-static-hostname", policykit_auth
);
227 case AUTHORIZED_NATIVELY
:
228 case AUTHORIZED_BY_PROMPT
:
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.");
238 g_dbus_method_invocation_return_dbus_error(invoc
, "org.freedesktop.hostname1.Error.EFAULT", "Provided bus name is invalid.");
241 case ERROR_BADACTION
:
242 g_dbus_method_invocation_return_dbus_error(invoc
, "org.freedesktop.hostname1.Error.EFAULT", "Provided action ID is invalid.");
247 g_dbus_method_invocation_return_dbus_error(invoc
, "org.freedesktop.hostname1.Error.ECANCELED", "Failed to set static hostname for unknown reason.");
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
))) {
254 check_length
= strnlen(valid_static_hostname_buf
, MAXHOSTNAMELEN
+ 1);
256 if(check_length
> MAXHOSTNAMELEN
) {
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
);
261 } else if(!(STATIC_HOSTNAME
= valid_static_hostname_buf
)) {
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
);
268 g_strdelimit(STATIC_HOSTNAME
, " ", '-');
269 hostname1_set_static_hostname(hn1_passed_interf
, STATIC_HOSTNAME
);
270 g_ptr_array_add(hostnamed_freeable
, valid_static_hostname_buf
);
272 /* set string in OS_HOSTNAME_PATH ("/etc/myname" on bsd) */
273 bsd_hostname_try
= get_bsd_hostname(STATIC_HOSTNAME
);
275 if(!bsd_hostname_try
|| !g_file_set_contents(OS_HOSTNAME_PATH
, bsd_hostname_try
, -1, &debug_error
))
276 g_printf("could not to write to %s! are you root?\n", OS_HOSTNAME_PATH
);
279 g_free(bsd_hostname_try
);
281 /* call sethostname(3) too */
282 ret
= (!sethostname(valid_static_hostname_buf
, MAXHOSTNAMELEN
)) ? TRUE
: FALSE
; /* TODO set /etc/myname, guarantee domain or substitue .home.network" */
283 hostname1_complete_set_static_hostname(hn1_passed_interf
, invoc
);
291 on_handle_set_pretty_hostname(Hostname1
*hn1_passed_interf
,
292 GDBusMethodInvocation
*invoc
,
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
;
301 check_auth_result is_authed
;
303 proposed_pretty_hostname
= NULL
;
304 ret
= try_to_set
= FALSE
;
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
);
310 /* verify caller has correct permissions via polkit */
311 is_authed
= polkit_try_auth(bus_name
, "org.freedesktop.hostname1.set-pretty-hostname", policykit_auth
);
315 case AUTHORIZED_NATIVELY
:
316 case AUTHORIZED_BY_PROMPT
:
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.");
326 g_dbus_method_invocation_return_dbus_error(invoc
, "org.freedesktop.hostname1.Error.EFAULT", "Provided bus name is invalid.");
329 case ERROR_BADACTION
:
330 g_dbus_method_invocation_return_dbus_error(invoc
, "org.freedesktop.hostname1.Error.EFAULT", "Provided action ID is invalid.");
335 g_dbus_method_invocation_return_dbus_error(invoc
, "org.freedesktop.hostname1.Error.ECANCELED", "Failed to set pretty hostname for unknown reason.");
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
))) {
342 check_length
= strnlen(valid_pretty_hostname_buf
, MAXHOSTNAMELEN
+ 1);
344 if(check_length
> MAXHOSTNAMELEN
) {
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
);
349 } else if(!(PRETTY_HOSTNAME
= valid_pretty_hostname_buf
)) {
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
);
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
);
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
);
370 on_handle_set_chassis(Hostname1
*hn1_passed_interf
,
371 GDBusMethodInvocation
*invoc
,
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
;
381 proposed_chassis_name
= NULL
;
382 ret
= try_to_set
= FALSE
;
383 valid_chassis_name_buf
= (gchar
*)g_malloc0(8192);
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
);
389 g_strlcpy(valid_chassis_name_buf
, proposed_chassis_name
, (gsize
)64);
391 /* verify caller has correct permissions via polkit */
392 is_authed
= polkit_try_auth(bus_name
, "org.freedesktop.hostname1.set-chassis", policykit_auth
);
396 case AUTHORIZED_NATIVELY
:
397 case AUTHORIZED_BY_PROMPT
:
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.");
407 g_dbus_method_invocation_return_dbus_error(invoc
, "org.freedesktop.hostname1.Error.EFAULT", "Provided bus name is invalid.");
410 case ERROR_BADACTION
:
411 g_dbus_method_invocation_return_dbus_error(invoc
, "org.freedesktop.hostname1.Error.EFAULT", "Provided action ID is invalid.");
416 g_dbus_method_invocation_return_dbus_error(invoc
, "org.freedesktop.hostname1.Error.ECANCELED", "Failed to set chassis type for unknown reason.");
420 /* verify passed chassis type's validity */
421 if(try_to_set
&& proposed_chassis_name
) {
423 if(!is_valid_chassis_type(proposed_chassis_name
)) {
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
);
428 } else if(!(CHASSIS
= valid_chassis_name_buf
)) {
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
);
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
);
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
);
449 on_handle_set_icon_name(Hostname1
*hn1_passed_interf
,
450 GDBusMethodInvocation
*invoc
,
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
;
460 proposed_icon_name
= NULL
;
461 ret
= try_to_set
= FALSE
;
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
);
467 /* verify caller has correct permissions via polkit */
468 is_authed
= polkit_try_auth(bus_name
, "org.freedesktop.hostname1.set-icon-name", policykit_auth
);
472 case AUTHORIZED_NATIVELY
:
473 case AUTHORIZED_BY_PROMPT
:
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.");
483 g_dbus_method_invocation_return_dbus_error(invoc
, "org.freedesktop.hostname1.Error.EFAULT", "Provided bus name is invalid.");
486 case ERROR_BADACTION
:
487 g_dbus_method_invocation_return_dbus_error(invoc
, "org.freedesktop.hostname1.Error.EFAULT", "Provided action ID is invalid.");
492 g_dbus_method_invocation_return_dbus_error(invoc
, "org.freedesktop.hostname1.Error.ECANCELED", "Failed to set icon name for unknown reason.");
496 /* verify passed chassis type's validity */
497 if(try_to_set
&& proposed_icon_name
) {
499 g_strlcpy(valid_icon_name_buf
, proposed_icon_name
, (gsize
)64);
501 if(!(ICON
= valid_icon_name_buf
)) {
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
);
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
);
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
);
522 on_handle_set_location(Hostname1
*hn1_passed_interf
,
523 GDBusMethodInvocation
*invoc
,
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
;
532 ret
= try_to_set
= FALSE
;
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
);
538 /* verify caller has correct permissions via polkit */
539 is_authed
= polkit_try_auth(bus_name
, "org.freedesktop.hostname1.set-location", policykit_auth
);
543 case AUTHORIZED_NATIVELY
:
544 case AUTHORIZED_BY_PROMPT
:
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.");
555 g_dbus_method_invocation_return_dbus_error(invoc
, "org.freedesktop.hostname1.Error.EFAULT", "Provided bus name is invalid.");
559 case ERROR_BADACTION
:
560 g_dbus_method_invocation_return_dbus_error(invoc
, "org.freedesktop.hostname1.Error.EFAULT", "Provided action ID is invalid.");
566 g_dbus_method_invocation_return_dbus_error(invoc
, "org.freedesktop.hostname1.Error.ECANCELED", "Failed to set location for unknown reason.");
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
);
580 on_handle_set_deployment(Hostname1
*hn1_passed_interf
,
581 GDBusMethodInvocation
*invoc
,
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
;
590 ret
= try_to_set
= FALSE
;
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
);
596 /* verify caller has correct permissions via polkit */
597 is_authed
= polkit_try_auth(bus_name
, "org.freedesktop.hostname1.set-deployment", policykit_auth
);
601 case AUTHORIZED_NATIVELY
:
602 case AUTHORIZED_BY_PROMPT
:
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.");
613 g_dbus_method_invocation_return_dbus_error(invoc
, "org.freedesktop.hostname1.Error.EFAULT", "Provided bus name is invalid.");
617 case ERROR_BADACTION
:
618 g_dbus_method_invocation_return_dbus_error(invoc
, "org.freedesktop.hostname1.Error.EFAULT", "Provided action ID is invalid.");
624 g_dbus_method_invocation_return_dbus_error(invoc
, "org.freedesktop.hostname1.Error.ECANCELED", "Failed to set deployment for unknown reason.");
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
);
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
647 hostname_buf
= (gchar
*)g_malloc0(MAXHOSTNAMELEN
);
649 if(gethostname(hostname_buf
, MAXHOSTNAMELEN
))
650 return "localhost.home.network"; /* TODO bomb out here probably */
652 else if(!g_strcmp0(HOSTNAME
, hostname_buf
)) {
654 g_free(hostname_buf
);
658 g_ptr_array_add(hostnamed_freeable
, hostname_buf
);
659 HOSTNAME
= hostname_buf
;
660 hostname1_set_hostname(hostnamed_interf
, HOSTNAME
);
666 our_get_static_hostname() {
668 if(STATIC_HOSTNAME
&& g_strcmp0(STATIC_HOSTNAME
, ""))
669 return STATIC_HOSTNAME
;
673 return "localhost.home.network";
677 our_get_pretty_hostname() {
680 return PRETTY_HOSTNAME
;
691 return "desktop"; /* this leads to the most generic beheivor in the unlikely case its returned */
695 our_get_icon_name() {
704 our_get_kernel_name() {
713 our_get_kernel_version() {
722 our_get_kernel_release() {
731 our_get_os_cpename() {
733 /* XXX needs to parse /etc/os-release (fallback to /usr/local/lib/os-release) */
738 our_get_os_pretty_name() {
752 our_get_deployment() {
758 /* --- end method/property/dbus signal code, begin bus/name handlers --- */
760 static void hostnamed_on_bus_acquired(GDBusConnection
*conn
,
762 gpointer user_data
) {
764 g_printf("got bus/name, exporting %s's interface...\n", name
);
766 hostnamed_interf
= hostname1_skeleton_new();
768 /* attach function pointers to generated struct's method handlers */
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
);
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
);
777 /* set our properties before export */
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());
788 hostname1_set_deployment(hostnamed_interf
, our_get_deployment());
789 hostname1_set_location(hostnamed_interf
, our_get_location());
792 if(!g_dbus_interface_skeleton_export(G_DBUS_INTERFACE_SKELETON(hostnamed_interf
),
794 "/org/freedesktop/hostname1",
797 g_printf("failed to export %s's interface!\n", name
); /* unusual edge case, TODO check errno */
798 hostnamed_mem_clean();
801 dbus_interface_exported
= TRUE
;
802 g_printf("exported %s's interface on the system bus...\n", name
);
806 static void hostnamed_on_name_acquired(GDBusConnection
*conn
,
808 gpointer user_data
) {
810 g_printf("success!\n");
813 static void hostnamed_on_name_lost(GDBusConnection
*conn
,
815 gpointer user_data
) {
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();
823 g_printf("lost name %s, exiting...\n", name
);
825 hostnamed_mem_clean();
828 /* --- end bus/name handlers, begin misc unix functions --- */
830 /* safe call to clean and then exit
831 * this stops our GMainLoop safely before letting main() return */
832 void hostnamed_mem_clean() {
834 g_printf("exiting...\n");
836 if(dbus_interface_exported
)
837 g_dbus_interface_skeleton_unexport(G_DBUS_INTERFACE_SKELETON(hostnamed_interf
));
839 if(g_main_loop_is_running(hostnamed_loop
))
840 g_main_loop_quit(hostnamed_loop
);
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
) {
847 g_printf("caught SIGINT/HUP/TERM, exiting\n");
849 hostnamed_mem_clean();
850 return G_SOURCE_REMOVE
;
853 void set_signal_handlers() {
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
);
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 */
867 hostnamed_freeable
= g_ptr_array_new();
869 /* TODO: check for valid, writable config at init. if no, complain to `make install` */
871 get_bsd_hostname("adsf"); /* TODO KILL ME */
873 CHASSIS
= ICON
= OS_CPENAME
= 0;
874 KERN_NAME
= KERN_RELEASE
= KERN_VERS
= 0;
875 HOSTNAME
= STATIC_HOSTNAME
= PRETTY_HOSTNAME
= NULL
;
877 set_signal_handlers();
879 if(!determine_chassis_and_icon() || !set_uname_properties() || !set_names())
882 hostnamed_loop
= g_main_loop_new(NULL
, TRUE
);
884 bus_descriptor
= g_bus_own_name(G_BUS_TYPE_SYSTEM
,
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
,
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
);
897 /* guaranteed unownable */
898 g_bus_unown_name(bus_descriptor
);
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
);
906 gboolean
set_names() {
909 gchar
*hostname_buf
, *static_hostname_buf
, *pretty_hostname_buf
, *location_buf
, *deployment_buf
;
910 size_t hostname_divider
;
912 hostname_buf
= (gchar
*) g_malloc0(MAXHOSTNAMELEN
);
913 static_hostname_buf
= (gchar
*) g_malloc0(4096);
914 pretty_hostname_buf
= (gchar
*) g_malloc0(4096);
915 location_buf
= (gchar
*) g_malloc0(LOCATION_MAXSIZE
);
916 deployment_buf
= (gchar
*) g_malloc0(DEPLOYMENT_MAXSIZE
);
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
);
921 g_ptr_array_add(hostnamed_freeable
, location_buf
);
922 g_ptr_array_add(hostnamed_freeable
, deployment_buf
);
924 /* (2) set HOSTNAME */
925 if(gethostname(hostname_buf
, MAXHOSTNAMELEN
) || !g_strcmp0(hostname_buf
, ""))
926 HOSTNAME
= "localhost";
928 HOSTNAME
= hostname_buf
;
930 /* this bit gets you the /etc/myname style hostname
931 hostname_divider = strcspn(hostname_buf, ".");
932 strncpy(ret, hostname_buf, hostname_divider); */
934 /* (3) set PRETTY_HOSTNAME */
935 if((pretty_hostname_buf
= config_get(OS_CONFIG_PATH
, "PRETTY_HOSTNAME")))
936 PRETTY_HOSTNAME
= pretty_hostname_buf
;
939 PRETTY_HOSTNAME
= "";
942 /* (4) set STATIC_HOSTNAME */
943 if(!gethostname(static_hostname_buf
, MAXHOSTNAMELEN
))
944 STATIC_HOSTNAME
= static_hostname_buf
;
947 STATIC_HOSTNAME
= "";
951 LOCATION
= location_buf
;
953 DEPLOYMENT
= deployment_buf
;
955 return (HOSTNAME
&& STATIC_HOSTNAME
&& PRETTY_HOSTNAME
&& LOCATION
&& DEPLOYMENT
) ? TRUE
: FALSE
;
958 gboolean
set_uname_properties() {
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
));
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
));
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
));
980 gboolean
determine_chassis_and_icon() {
982 const size_t bufsize
= 4096;
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];
988 gboolean UNSURE_CHASSIS_FLAG
, UNSURE_ICON_FLAG
;
990 hwproduct_size
= hwmodel_size
= hwvendor_size
= hwmachine_size
= bufsize
;
991 UNSURE_CHASSIS_FLAG
= UNSURE_ICON_FLAG
= FALSE
;
994 hwproduct
= (char*)g_malloc0(4096);
995 hwmodel
= (char*)g_malloc0(4096);
996 hwvendor
= (char*)g_malloc0(4096);
997 hwmachine
= (char*)g_malloc0(4096);
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
);
1004 hwproduct_name
[0] = CTL_HW
;
1005 hwproduct_name
[1] = HW_PRODUCT
;
1007 hwmodel_name
[0] = CTL_HW
;
1008 hwmodel_name
[1] = HW_MODEL
;
1010 hwvendor_name
[0] = CTL_HW
;
1011 hwvendor_name
[1] = HW_VENDOR
;
1013 hwmachine_name
[0] = CTL_HW
;
1014 hwmachine_name
[1] = HW_MACHINE
;
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))
1020 if(-1 == sysctl(hwmodel_name
, 2, NULL
, &hwmodel_size
, NULL
, 0) || -1 == sysctl(hwmodel_name
, 2, hwmodel
, &hwmodel_size
, NULL
, 0))
1023 if(-1 == sysctl(hwvendor_name
, 2, NULL
, &hwvendor_size
, NULL
, 0) || -1 == sysctl(hwvendor_name
, 2, hwvendor
, &hwvendor_size
, NULL
, 0))
1026 if(-1 == sysctl(hwmachine_name
, 2, NULL
, &hwmachine_size
, NULL
, 0) || -1 == sysctl(hwmachine_name
, 2, hwmachine
, &hwmachine_size
, NULL
, 0))
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 */
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
)) {
1037 if(!UNSURE_CHASSIS_FLAG
&& chassis_indicator_table
[i
].chassis
) {
1039 UNSURE_CHASSIS_FLAG
= chassis_indicator_table
[i
].chassis_precedence
;
1040 CHASSIS
= chassis_indicator_table
[i
].chassis
;
1043 if(!UNSURE_ICON_FLAG
&& chassis_indicator_table
[i
].icon
) {
1045 UNSURE_ICON_FLAG
= chassis_indicator_table
[i
].icon_precedence
;
1046 ICON
= chassis_indicator_table
[i
].icon
;
1051 if(up_native_is_laptop()) {
1056 ICON
= "input-touchpad"; /* TODO pull an icon package that actually has the icons we're looking for */
1058 } else if(is_server(hwmachine
)) {
1063 ICON
= "uninterruptible-power-supply";
1065 } else if(!CHASSIS
|| !ICON
) {
1068 CHASSIS
= "desktop";
1073 return (CHASSIS
&& ICON
);
1076 gboolean
is_server(gchar
*arch
) {
1080 for(; i
< G_N_ELEMENTS(server_archs
); i
++)
1081 if(strcasestr(arch
, server_archs
[i
]))
1087 gboolean
up_native_is_laptop() {
1089 struct apm_power_info bstate
;
1090 struct sensordev acpiac
;
1092 if (up_native_get_sensordev("acpiac0", &acpiac
))
1095 if (-1 == ioctl(up_apm_get_fd(), APM_IOC_GETPOWER
, &bstate
))
1096 g_error("ioctl on apm fd failed : %s", g_strerror(errno
));
1098 return bstate
.ac_state
!= APM_AC_UNKNOWN
;
1101 int up_apm_get_fd() {
1103 static int apm_fd
= 0;
1107 g_debug("apm_fd is not initialized yet, opening");
1110 if((apm_fd
= open("/dev/apm", O_RDONLY
)) == -1) {
1111 if(errno
!= ENXIO
&& errno
!= ENOENT
)
1112 g_error("cannot open device file");
1119 gboolean
up_native_get_sensordev(const char * id
, struct sensordev
* snsrdev
) {
1122 size_t sdlen
= sizeof(struct sensordev
);
1123 int mib
[] = {CTL_HW
, HW_SENSORS
, 0, 0 ,0};
1125 for (devn
= 0 ; ; devn
++) {
1127 if(sysctl(mib
, 3, snsrdev
, &sdlen
, NULL
, 0) == -1) {
1134 if (!strcmp(snsrdev
->xname
, id
))
1141 static gboolean
is_valid_chassis_type(gchar
*test
) {
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
, ""))
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
) {
1162 gchar
*bsd_hostname
, *ascii_translated_hostname
, **myname_contents
, *passed_domain
, *temp_buf
;
1163 size_t domain_len
, check_len
;
1164 gboolean read_result
;
1166 g_strdelimit(proposed_hostname
, "`~!@#$%^&*()_=+[{]}|:;'\"\\", '-');
1168 ascii_translated_hostname
= g_hostname_to_ascii(proposed_hostname
);
1169 check_len
= strnlen(ascii_translated_hostname
, MAXHOSTNAMELEN
);
1171 if(!ascii_translated_hostname
|| !check_len
|| check_len
> MAXHOSTNAMELEN
|| !g_strcmp0("", ascii_translated_hostname
) || !g_strcmp0(".", ascii_translated_hostname
)) {
1173 bsd_hostname
= NULL
;
1174 passed_domain
= NULL
;
1175 myname_contents
= NULL
;
1177 } else if((passed_domain
= has_domain(ascii_translated_hostname
))) {
1179 bsd_hostname
= (gchar
*) g_malloc0(MAXHOSTNAMELEN
);
1180 g_strlcpy(bsd_hostname
, ascii_translated_hostname
, MAXHOSTNAMELEN
);
1182 passed_domain
= NULL
;
1183 myname_contents
= NULL
;
1187 myname_contents
= (gchar
**) g_malloc0(MAXHOSTNAMELEN
* 2);
1188 read_result
= g_file_get_contents(OS_HOSTNAME_PATH
, myname_contents
, NULL
, NULL
);
1190 if(read_result
&& (passed_domain
= has_domain(myname_contents
[0]))) {
1192 domain_len
= strnlen(passed_domain
, MAXHOSTNAMELEN
);
1194 if((domain_len
+ check_len
) > MAXHOSTNAMELEN
)
1195 bsd_hostname
= NULL
;
1197 bsd_hostname
= g_strconcat(ascii_translated_hostname
, passed_domain
, NULL
);
1199 } else if(myname_contents
[0]) {
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
);
1203 domain_len
= strnlen(DEFAULT_DOMAIN
, MAXHOSTNAMELEN
);
1205 if((domain_len
+ check_len
) > MAXHOSTNAMELEN
)
1206 bsd_hostname
= NULL
;
1208 bsd_hostname
= g_strconcat(ascii_translated_hostname
, DEFAULT_DOMAIN
, NULL
);
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
);
1219 g_free(passed_domain
);
1221 g_free(myname_contents
);
1223 if(bsd_hostname
&& !strchr(bsd_hostname
, '\n')) {
1225 temp_buf
= bsd_hostname
;
1226 bsd_hostname
= g_strconcat(bsd_hostname
, "\n", NULL
);
1230 return bsd_hostname
;
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
) {
1238 size_t hostname_len
, full_len
;
1241 hostname_len
= strcspn(test
, ".");
1242 full_len
= strnlen(test
, MAXHOSTNAMELEN
);
1244 if(full_len
== hostname_len
)
1247 ret
= (gchar
*) g_malloc0(MAXHOSTNAMELEN
);
1248 g_strlcpy(ret
, &test
[hostname_len
], MAXHOSTNAMELEN
);