| 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 | gchar *LOCATION = NULL, *DEPLOYMENT = NULL; |
| 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 */ |
| 93 | |
| 94 | /* add any sysctl strings that suggest virtualization here */ |
| 95 | const struct SYSCTL_LOOKUP_TABLE chassis_indicator_table[] = |
| 96 | { |
| 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 */ |
| 106 | |
| 107 | /* archs to check against when determining if machine is server */ |
| 108 | const gchar *server_archs[] = { |
| 109 | "hppa", |
| 110 | "sparc", |
| 111 | "sparc64" |
| 112 | }; |
| 113 | |
| 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"; |
| 117 | /* XXX */ |
| 118 | static const guint LOCATION_MAXSIZE = 4096; |
| 119 | static const guint DEPLOYMENT_MAXSIZE = 4096; |
| 120 | |
| 121 | |
| 122 | /* --- begin method/property/dbus signal code --- */ |
| 123 | |
| 124 | /* TODO free some strings here */ |
| 125 | static gboolean |
| 126 | on_handle_set_hostname(Hostname1 *hn1_passed_interf, |
| 127 | GDBusMethodInvocation *invoc, |
| 128 | const gchar *greet, |
| 129 | gpointer data) { |
| 130 | GVariant *params; |
| 131 | gchar *proposed_hostname, *valid_hostname_buf; |
| 132 | const gchar *bus_name; |
| 133 | gboolean policykit_auth, ret, try_to_set; |
| 134 | size_t check_length; |
| 135 | check_auth_result is_authed; |
| 136 | |
| 137 | proposed_hostname = NULL; |
| 138 | ret = try_to_set = FALSE; |
| 139 | |
| 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); |
| 143 | |
| 144 | /* verify caller has correct permissions via polkit */ |
| 145 | is_authed = polkit_try_auth(bus_name, "org.freedesktop.hostname1.set-hostname", policykit_auth); |
| 146 | |
| 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) { |
| 179 | |
| 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); |
| 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); |
| 187 | |
| 188 | } else { |
| 189 | |
| 190 | HOSTNAME = valid_hostname_buf; |
| 191 | hostname1_set_hostname(hn1_passed_interf, HOSTNAME); |
| 192 | g_ptr_array_add(hostnamed_freeable, valid_hostname_buf); |
| 193 | ret = TRUE; |
| 194 | hostname1_complete_set_hostname(hn1_passed_interf, invoc); |
| 195 | } |
| 196 | } |
| 197 | |
| 198 | return ret; |
| 199 | } |
| 200 | |
| 201 | static gboolean |
| 202 | on_handle_set_static_hostname(Hostname1 *hn1_passed_interf, |
| 203 | GDBusMethodInvocation *invoc, |
| 204 | const gchar *greet, |
| 205 | gpointer data) { |
| 206 | |
| 207 | GVariant *params; |
| 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; |
| 211 | size_t check_length; |
| 212 | check_auth_result is_authed; |
| 213 | |
| 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 */ |
| 223 | is_authed = polkit_try_auth(bus_name, "org.freedesktop.hostname1.set-static-hostname", policykit_auth); |
| 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, " ", '-'); |
| 269 | hostname1_set_static_hostname(hn1_passed_interf, STATIC_HOSTNAME); |
| 270 | g_ptr_array_add(hostnamed_freeable, valid_static_hostname_buf); |
| 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)) |
| 276 | g_printf("could not to write to %s! are you root?\n", OS_HOSTNAME_PATH); |
| 277 | |
| 278 | if(bsd_hostname_try) |
| 279 | g_free(bsd_hostname_try); |
| 280 | |
| 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); |
| 284 | } |
| 285 | } |
| 286 | |
| 287 | return ret; |
| 288 | } |
| 289 | |
| 290 | static gboolean |
| 291 | on_handle_set_pretty_hostname(Hostname1 *hn1_passed_interf, |
| 292 | GDBusMethodInvocation *invoc, |
| 293 | const gchar *greet, |
| 294 | gpointer data) { |
| 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; |
| 302 | |
| 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 */ |
| 311 | is_authed = polkit_try_auth(bus_name, "org.freedesktop.hostname1.set-pretty-hostname", policykit_auth); |
| 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 | |
| 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); |
| 363 | } |
| 364 | } |
| 365 | |
| 366 | return ret; |
| 367 | } |
| 368 | |
| 369 | static gboolean |
| 370 | on_handle_set_chassis(Hostname1 *hn1_passed_interf, |
| 371 | GDBusMethodInvocation *invoc, |
| 372 | const gchar *greet, |
| 373 | gpointer data) { |
| 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; |
| 380 | |
| 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 */ |
| 392 | is_authed = polkit_try_auth(bus_name, "org.freedesktop.hostname1.set-chassis", policykit_auth); |
| 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); |
| 438 | ret = TRUE; |
| 439 | |
| 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); |
| 442 | } |
| 443 | } |
| 444 | |
| 445 | return ret; |
| 446 | } |
| 447 | |
| 448 | static gboolean |
| 449 | on_handle_set_icon_name(Hostname1 *hn1_passed_interf, |
| 450 | GDBusMethodInvocation *invoc, |
| 451 | const gchar *greet, |
| 452 | gpointer data) { |
| 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; |
| 459 | |
| 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 */ |
| 468 | is_authed = polkit_try_auth(bus_name, "org.freedesktop.hostname1.set-icon-name", policykit_auth); |
| 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); |
| 511 | ret = TRUE; |
| 512 | |
| 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); |
| 515 | |
| 516 | } |
| 517 | } |
| 518 | |
| 519 | return ret; |
| 520 | } |
| 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 | } |
| 636 | |
| 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 | |
| 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); |
| 655 | return HOSTNAME; |
| 656 | } |
| 657 | |
| 658 | g_ptr_array_add(hostnamed_freeable, hostname_buf); |
| 659 | HOSTNAME = hostname_buf; |
| 660 | hostname1_set_hostname(hostnamed_interf, HOSTNAME); |
| 661 | |
| 662 | return HOSTNAME; |
| 663 | } |
| 664 | |
| 665 | const gchar * |
| 666 | our_get_static_hostname() { |
| 667 | |
| 668 | if(STATIC_HOSTNAME && g_strcmp0(STATIC_HOSTNAME, "")) |
| 669 | return STATIC_HOSTNAME; |
| 670 | else if(HOSTNAME) |
| 671 | return HOSTNAME; |
| 672 | |
| 673 | return "localhost.home.network"; |
| 674 | } |
| 675 | |
| 676 | const gchar * |
| 677 | our_get_pretty_hostname() { |
| 678 | |
| 679 | if(PRETTY_HOSTNAME) |
| 680 | return PRETTY_HOSTNAME; |
| 681 | |
| 682 | return ""; |
| 683 | } |
| 684 | |
| 685 | const gchar * |
| 686 | our_get_chassis() { |
| 687 | |
| 688 | if(CHASSIS) |
| 689 | return CHASSIS; |
| 690 | |
| 691 | return "desktop"; /* this leads to the most generic beheivor in the unlikely case its returned */ |
| 692 | } |
| 693 | |
| 694 | const gchar * |
| 695 | our_get_icon_name() { |
| 696 | |
| 697 | if(ICON) |
| 698 | return ICON; |
| 699 | |
| 700 | return ""; |
| 701 | } |
| 702 | |
| 703 | const gchar * |
| 704 | our_get_kernel_name() { |
| 705 | |
| 706 | if(KERN_NAME) |
| 707 | return KERN_NAME; |
| 708 | |
| 709 | return ""; |
| 710 | } |
| 711 | |
| 712 | const gchar * |
| 713 | our_get_kernel_version() { |
| 714 | |
| 715 | if(KERN_VERS) |
| 716 | return KERN_VERS; |
| 717 | |
| 718 | return ""; |
| 719 | } |
| 720 | |
| 721 | const gchar * |
| 722 | our_get_kernel_release() { |
| 723 | |
| 724 | if(KERN_RELEASE) |
| 725 | return KERN_RELEASE; |
| 726 | |
| 727 | return ""; |
| 728 | } |
| 729 | |
| 730 | const gchar * |
| 731 | our_get_os_cpename() { |
| 732 | |
| 733 | /* XXX needs to parse /etc/os-release (fallback to /usr/local/lib/os-release) */ |
| 734 | return ""; |
| 735 | } |
| 736 | |
| 737 | const gchar * |
| 738 | our_get_os_pretty_name() { |
| 739 | |
| 740 | return "OpenBSD"; |
| 741 | } |
| 742 | |
| 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 | } |
| 758 | /* --- end method/property/dbus signal code, begin bus/name handlers --- */ |
| 759 | |
| 760 | static void hostnamed_on_bus_acquired(GDBusConnection *conn, |
| 761 | const gchar *name, |
| 762 | gpointer user_data) { |
| 763 | |
| 764 | g_printf("got bus/name, exporting %s's interface...\n", name); |
| 765 | |
| 766 | hostnamed_interf = hostname1_skeleton_new(); |
| 767 | |
| 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); |
| 776 | |
| 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()); |
| 790 | |
| 791 | |
| 792 | if(!g_dbus_interface_skeleton_export(G_DBUS_INTERFACE_SKELETON(hostnamed_interf), |
| 793 | conn, |
| 794 | "/org/freedesktop/hostname1", |
| 795 | NULL)) { |
| 796 | |
| 797 | g_printf("failed to export %s's interface!\n", name); /* unusual edge case, TODO check errno */ |
| 798 | hostnamed_mem_clean(); |
| 799 | |
| 800 | } else { |
| 801 | dbus_interface_exported = TRUE; |
| 802 | g_printf("exported %s's interface on the system bus...\n", name); |
| 803 | } |
| 804 | } |
| 805 | |
| 806 | static void hostnamed_on_name_acquired(GDBusConnection *conn, |
| 807 | const gchar *name, |
| 808 | gpointer user_data) { |
| 809 | |
| 810 | g_printf("success!\n"); |
| 811 | } |
| 812 | |
| 813 | static void hostnamed_on_name_lost(GDBusConnection *conn, |
| 814 | const gchar *name, |
| 815 | gpointer user_data) { |
| 816 | |
| 817 | if(!conn) { |
| 818 | |
| 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 | } |
| 822 | |
| 823 | g_printf("lost name %s, exiting...\n", name); |
| 824 | |
| 825 | hostnamed_mem_clean(); |
| 826 | } |
| 827 | |
| 828 | /* --- end bus/name handlers, begin misc unix functions --- */ |
| 829 | |
| 830 | /* safe call to clean and then exit |
| 831 | * this stops our GMainLoop safely before letting main() return */ |
| 832 | void hostnamed_mem_clean() { |
| 833 | |
| 834 | g_printf("exiting...\n"); |
| 835 | |
| 836 | if(dbus_interface_exported) |
| 837 | g_dbus_interface_skeleton_unexport(G_DBUS_INTERFACE_SKELETON(hostnamed_interf)); |
| 838 | |
| 839 | if(g_main_loop_is_running(hostnamed_loop)) |
| 840 | g_main_loop_quit(hostnamed_loop); |
| 841 | |
| 842 | } |
| 843 | |
| 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) { |
| 846 | |
| 847 | g_printf("caught SIGINT/HUP/TERM, exiting\n"); |
| 848 | |
| 849 | hostnamed_mem_clean(); |
| 850 | return G_SOURCE_REMOVE; |
| 851 | } |
| 852 | |
| 853 | void set_signal_handlers() { |
| 854 | |
| 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); |
| 859 | |
| 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 */ |
| 863 | } |
| 864 | |
| 865 | int main() { |
| 866 | |
| 867 | hostnamed_freeable = g_ptr_array_new(); |
| 868 | |
| 869 | /* TODO: check for valid, writable config at init. if no, complain to `make install` */ |
| 870 | |
| 871 | get_bsd_hostname("adsf"); /* TODO KILL ME */ |
| 872 | |
| 873 | CHASSIS = ICON = OS_CPENAME = 0; |
| 874 | KERN_NAME = KERN_RELEASE = KERN_VERS = 0; |
| 875 | HOSTNAME = STATIC_HOSTNAME = PRETTY_HOSTNAME = NULL; |
| 876 | |
| 877 | set_signal_handlers(); |
| 878 | |
| 879 | if(!determine_chassis_and_icon() || !set_uname_properties() || !set_names()) |
| 880 | return 1; |
| 881 | |
| 882 | hostnamed_loop = g_main_loop_new(NULL, TRUE); |
| 883 | |
| 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, |
| 890 | NULL, |
| 891 | NULL); |
| 892 | |
| 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); |
| 896 | |
| 897 | /* guaranteed unownable */ |
| 898 | g_bus_unown_name(bus_descriptor); |
| 899 | |
| 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); |
| 902 | |
| 903 | return 0; |
| 904 | } |
| 905 | |
| 906 | gboolean set_names() { |
| 907 | |
| 908 | /* (1) set up */ |
| 909 | gchar *hostname_buf, *static_hostname_buf, *pretty_hostname_buf, *location_buf, *deployment_buf; |
| 910 | size_t hostname_divider; |
| 911 | |
| 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); |
| 917 | |
| 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); |
| 923 | |
| 924 | /* (2) set HOSTNAME */ |
| 925 | if(gethostname(hostname_buf, MAXHOSTNAMELEN) || !g_strcmp0(hostname_buf, "")) |
| 926 | HOSTNAME = "localhost"; |
| 927 | |
| 928 | HOSTNAME = hostname_buf; |
| 929 | |
| 930 | /* this bit gets you the /etc/myname style hostname |
| 931 | hostname_divider = strcspn(hostname_buf, "."); |
| 932 | strncpy(ret, hostname_buf, hostname_divider); */ |
| 933 | |
| 934 | /* (3) set PRETTY_HOSTNAME */ |
| 935 | if((pretty_hostname_buf = config_get(OS_CONFIG_PATH, "PRETTY_HOSTNAME"))) |
| 936 | PRETTY_HOSTNAME = pretty_hostname_buf; |
| 937 | |
| 938 | else |
| 939 | PRETTY_HOSTNAME = ""; |
| 940 | |
| 941 | |
| 942 | /* (4) set STATIC_HOSTNAME */ |
| 943 | if(!gethostname(static_hostname_buf, MAXHOSTNAMELEN)) |
| 944 | STATIC_HOSTNAME = static_hostname_buf; |
| 945 | |
| 946 | else |
| 947 | STATIC_HOSTNAME = ""; |
| 948 | |
| 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; |
| 956 | } |
| 957 | |
| 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 | |
| 980 | gboolean determine_chassis_and_icon() { |
| 981 | |
| 982 | const size_t bufsize = 4096; |
| 983 | |
| 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]; |
| 987 | unsigned int i; |
| 988 | gboolean UNSURE_CHASSIS_FLAG, UNSURE_ICON_FLAG; |
| 989 | |
| 990 | hwproduct_size = hwmodel_size = hwvendor_size = hwmachine_size = bufsize; |
| 991 | UNSURE_CHASSIS_FLAG = UNSURE_ICON_FLAG = FALSE; |
| 992 | i = 0; |
| 993 | |
| 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 | |
| 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 | |
| 1004 | hwproduct_name[0] = CTL_HW; |
| 1005 | hwproduct_name[1] = HW_PRODUCT; |
| 1006 | |
| 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 | |
| 1013 | hwmachine_name[0] = CTL_HW; |
| 1014 | hwmachine_name[1] = HW_MACHINE; |
| 1015 | |
| 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 | |
| 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 | |
| 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 | |
| 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) { |
| 1066 | |
| 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; |
| 1079 | |
| 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; |
| 1139 | } |
| 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; |
| 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 | } |