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