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