remove references to deprecated tzfile.h, use previously defined consts
[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
b08f1622 113static const gchar *DEFAULT_DOMAIN = ""; /* blank domains are OK for now */
df8fc341 114static const gchar *OS_HOSTNAME_PATH = "/etc/myname";
de46ace4 115static const gchar *OS_CONFIG_PATH = "/etc/machine-info";
df8fc341 116
a1bcc33c 117/* --- begin method/property/dbus signal code --- */
f1ad9351 118
2bc9066a 119/* TODO free some strings here */
3d53b501 120static gboolean
1be94ede 121on_handle_set_hostname(Hostname1 *hn1_passed_interf,
3d53b501 122 GDBusMethodInvocation *invoc,
123 const gchar *greet,
124 gpointer data) {
5b70f403 125 GVariant *params;
126 gchar *proposed_hostname, *valid_hostname_buf;
2bc9066a 127 const gchar *bus_name;
128 gboolean policykit_auth, ret, try_to_set;
3ccecdd6 129 size_t check_length;
2bc9066a 130 check_auth_result is_authed;
5b70f403 131
5b70f403 132 proposed_hostname = NULL;
2bc9066a 133 ret = try_to_set = FALSE;
5b70f403 134
135 params = g_dbus_method_invocation_get_parameters(invoc);
136 g_variant_get(params, "(sb)", &proposed_hostname, &policykit_auth);
2bc9066a 137 bus_name = g_dbus_method_invocation_get_sender(invoc);
5b70f403 138
2bc9066a 139 /* verify caller has correct permissions via polkit */
5fd84921 140 is_authed = polkit_try_auth(bus_name, "org.freedesktop.hostname1.set-hostname", policykit_auth);
5b70f403 141
2bc9066a 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) {
5b70f403 174
3ccecdd6 175 g_dbus_method_invocation_return_dbus_error(invoc, "org.freedesktop.hostname1.Error.ENAMETOOLONG", "Hostname string exceeded maximum length.");
2bc9066a 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);
5b70f403 182
2bc9066a 183 } else {
5b70f403 184
2bc9066a 185 HOSTNAME = valid_hostname_buf;
3ccecdd6 186 hostname1_set_hostname(hn1_passed_interf, HOSTNAME);
2bc9066a 187 g_ptr_array_add(hostnamed_freeable, valid_hostname_buf);
3ccecdd6 188 ret = TRUE;
189 hostname1_complete_set_hostname(hn1_passed_interf, invoc);
190 }
191 }
5b70f403 192
193 return ret;
3d53b501 194}
195
196static gboolean
1be94ede 197on_handle_set_static_hostname(Hostname1 *hn1_passed_interf,
3d53b501 198 GDBusMethodInvocation *invoc,
199 const gchar *greet,
200 gpointer data) {
933f62bb 201
202 GVariant *params;
5ea060f8 203 gchar *proposed_static_hostname, *valid_static_hostname_buf, *bsd_hostname_try;
933f62bb 204 const gchar *bus_name;
205 gboolean policykit_auth, ret, try_to_set;
206 size_t check_length;
207 check_auth_result is_authed;
5ea060f8 208
933f62bb 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 */
5fd84921 218 is_authed = polkit_try_auth(bus_name, "org.freedesktop.hostname1.set-static-hostname", policykit_auth);
933f62bb 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, " ", '-');
057ab9c2 264 hostname1_set_static_hostname(hn1_passed_interf, STATIC_HOSTNAME);
933f62bb 265 g_ptr_array_add(hostnamed_freeable, valid_static_hostname_buf);
5ea060f8 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))
de46ace4 271 g_printf("could not to write to %s! are you root?\n", OS_HOSTNAME_PATH);
5ea060f8 272
273 if(bsd_hostname_try)
274 g_free(bsd_hostname_try);
275
276 /* call sethostname(3) too */
057ab9c2 277 ret = (!sethostname(valid_static_hostname_buf, MAXHOSTNAMELEN)) ? TRUE : FALSE; /* TODO set /etc/myname, guarantee domain or substitue .home.network" */
933f62bb 278 hostname1_complete_set_static_hostname(hn1_passed_interf, invoc);
279 }
280 }
281
282 return ret;
3d53b501 283}
284
285static gboolean
1be94ede 286on_handle_set_pretty_hostname(Hostname1 *hn1_passed_interf,
3d53b501 287 GDBusMethodInvocation *invoc,
288 const gchar *greet,
289 gpointer data) {
933f62bb 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;
933f62bb 297
933f62bb 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 */
5fd84921 306 is_authed = polkit_try_auth(bus_name, "org.freedesktop.hostname1.set-pretty-hostname", policykit_auth);
933f62bb 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
de46ace4 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);
933f62bb 358 }
359 }
360
933f62bb 361 return ret;
3d53b501 362}
363
364static gboolean
1be94ede 365on_handle_set_chassis(Hostname1 *hn1_passed_interf,
3d53b501 366 GDBusMethodInvocation *invoc,
367 const gchar *greet,
368 gpointer data) {
933f62bb 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;
933f62bb 375
933f62bb 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 */
5fd84921 387 is_authed = polkit_try_auth(bus_name, "org.freedesktop.hostname1.set-chassis", policykit_auth);
933f62bb 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);
de46ace4 433 ret = TRUE;
933f62bb 434
de46ace4 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);
933f62bb 437 }
438 }
439
933f62bb 440 return ret;
3d53b501 441}
442
443static gboolean
1be94ede 444on_handle_set_icon_name(Hostname1 *hn1_passed_interf,
3d53b501 445 GDBusMethodInvocation *invoc,
446 const gchar *greet,
447 gpointer data) {
933f62bb 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;
933f62bb 454
933f62bb 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 */
5fd84921 463 is_authed = polkit_try_auth(bus_name, "org.freedesktop.hostname1.set-icon-name", policykit_auth);
933f62bb 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);
de46ace4 506 ret = TRUE;
933f62bb 507
de46ace4 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);
933f62bb 510
933f62bb 511 }
512 }
513
933f62bb 514 return ret;
3d53b501 515}
516
76b67a18 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
523const gchar *
524our_get_hostname() {
525
3ccecdd6 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);
19c6b83d 535 return HOSTNAME;
3ccecdd6 536 }
537
538 g_ptr_array_add(hostnamed_freeable, hostname_buf);
539 HOSTNAME = hostname_buf;
540 hostname1_set_hostname(hostnamed_interf, HOSTNAME);
19c6b83d 541
3ccecdd6 542 return HOSTNAME;
76b67a18 543}
544
545const gchar *
546our_get_static_hostname() {
547
3ccecdd6 548 if(STATIC_HOSTNAME && g_strcmp0(STATIC_HOSTNAME, ""))
19c6b83d 549 return STATIC_HOSTNAME;
550 else if(HOSTNAME)
551 return HOSTNAME;
552
3ccecdd6 553 return "localhost.home.network";
76b67a18 554}
555
556const gchar *
557our_get_pretty_hostname() {
558
19c6b83d 559 if(PRETTY_HOSTNAME)
560 return PRETTY_HOSTNAME;
561
a2fffc07 562 return "";
76b67a18 563}
564
565const gchar *
566our_get_chassis() {
567
f8257c5d 568 if(CHASSIS)
569 return CHASSIS;
f1ad9351 570
19c6b83d 571 return "desktop"; /* this leads to the most generic beheivor in the unlikely case its returned */
76b67a18 572}
573
574const gchar *
575our_get_icon_name() {
576
f8257c5d 577 if(ICON)
578 return ICON;
579
580 return "";
76b67a18 581}
582
583const gchar *
584our_get_kernel_name() {
585
bd24ff31 586 if(KERN_NAME)
587 return KERN_NAME;
588
589 return "";
76b67a18 590}
591
592const gchar *
593our_get_kernel_version() {
594
bd24ff31 595 if(KERN_VERS)
596 return KERN_VERS;
597
598 return "";
76b67a18 599}
600
601const gchar *
602our_get_kernel_release() {
603
bd24ff31 604 if(KERN_RELEASE)
605 return KERN_RELEASE;
606
607 return "";
76b67a18 608}
609
610const gchar *
611our_get_os_cpename() {
612
3808ecc5
AJ
613 /* XXX needs to parse /etc/os-release (fallback to /usr/local/lib/os-release) */
614 return "";
76b67a18 615}
616
617const gchar *
618our_get_os_pretty_name() {
619
bd24ff31 620 return "OpenBSD";
76b67a18 621}
622
1ce41045 623/* --- end method/property/dbus signal code, begin bus/name handlers --- */
0df0018d 624
5b005882 625static void hostnamed_on_bus_acquired(GDBusConnection *conn,
9cab3afe 626 const gchar *name,
627 gpointer user_data) {
d1e1db9e 628
0f339959 629 g_printf("got bus/name, exporting %s's interface...\n", name);
d1e1db9e 630
90f54407 631 hostnamed_interf = hostname1_skeleton_new();
3d53b501 632
76b67a18 633 /* attach function pointers to generated struct's method handlers */
3d53b501 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
76b67a18 640 /* set our properties before export */
1be94ede 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());
76b67a18 651
3d53b501 652 if(!g_dbus_interface_skeleton_export(G_DBUS_INTERFACE_SKELETON(hostnamed_interf),
1be94ede 653 conn,
654 "/org/freedesktop/hostname1",
655 NULL)) {
3d53b501 656
0f339959 657 g_printf("failed to export %s's interface!\n", name); /* unusual edge case, TODO check errno */
90f54407 658 hostnamed_mem_clean();
3d53b501 659
0f339959 660 } else {
ea207ed3 661
90f54407 662 dbus_interface_exported = TRUE;
663 g_printf("exported %s's interface on the system bus...\n", name);
664 }
0f339959 665}
1e8c7c88 666
0f339959 667static void hostnamed_on_name_acquired(GDBusConnection *conn,
90f54407 668 const gchar *name,
0f339959 669 gpointer user_data) {
670
671 g_printf("success!\n");
1e8c7c88 672}
673
5b005882 674static void hostnamed_on_name_lost(GDBusConnection *conn,
1be94ede 675 const gchar *name,
676 gpointer user_data) {
80043b36 677
90f54407 678 if(!conn) {
0f339959 679
90f54407 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 }
0f339959 683
509599f0 684 g_printf("lost name %s, exiting...\n", name);
fd8852d9 685
1cd5e6fe 686 hostnamed_mem_clean();
0f339959 687}
688
689/* --- end bus/name handlers, begin misc unix functions --- */
690
691/* safe call to clean and then exit
509599f0 692 * this stops our GMainLoop safely before letting main() return */
0f339959 693void hostnamed_mem_clean() {
694
90f54407 695 g_printf("exiting...\n");
0f339959 696
90f54407 697 if(dbus_interface_exported)
698 g_dbus_interface_skeleton_unexport(G_DBUS_INTERFACE_SKELETON(hostnamed_interf));
0f339959 699
90f54407 700 if(g_main_loop_is_running(hostnamed_loop))
701 g_main_loop_quit(hostnamed_loop);
fd8852d9 702
ea207ed3 703}
704
0f339959 705/* wrapper for glib's unix signal handling; called only once if terminatating signal is raised against us */
706gboolean unix_sig_terminate_handler(gpointer data) {
c62bceb7 707
90f54407 708 g_printf("caught SIGINT/HUP/TERM, exiting\n");
0f339959 709
90f54407 710 hostnamed_mem_clean();
711 return G_SOURCE_REMOVE;
0f339959 712}
713
714void set_signal_handlers() {
715
90f54407 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);
04cc16f2 720
90f54407 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 */
0f339959 724}
725
726int main() {
bd24ff31 727
728 hostnamed_freeable = g_ptr_array_new();
729
a0ebb315 730 /* TODO: check for valid, writable config at init. if no, complain to `make install` */
bd24ff31 731
df8fc341 732 get_bsd_hostname("adsf"); /* TODO KILL ME */
733
bd24ff31 734 CHASSIS = ICON = OS_CPENAME = 0;
735 KERN_NAME = KERN_RELEASE = KERN_VERS = 0;
baf05b70 736 HOSTNAME = STATIC_HOSTNAME = PRETTY_HOSTNAME = NULL;
483e90b7 737
90f54407 738 set_signal_handlers();
387173cb 739
46835f3e 740 if(!determine_chassis_and_icon() || !set_uname_properties() || !set_names())
a1bcc33c 741 return 1;
742
baf05b70 743 hostnamed_loop = g_main_loop_new(NULL, TRUE);
fd8852d9 744
90f54407 745 bus_descriptor = g_bus_own_name(G_BUS_TYPE_SYSTEM,
0f339959 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);
496f5d66 753
90f54407 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);
1e8c7c88 757
90f54407 758 /* guaranteed unownable */
759 g_bus_unown_name(bus_descriptor);
c62bceb7 760
90f54407 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);
7323a4e4 763
90f54407 764 return 0;
a35a69c5 765}
766
46835f3e 767gboolean set_names() {
768
baf05b70 769 /* (1) set up */
770 gchar *hostname_buf, *static_hostname_buf, *pretty_hostname_buf;
46835f3e 771 size_t hostname_divider;
772
baf05b70 773 hostname_buf = (gchar*) g_malloc0(MAXHOSTNAMELEN);
774 static_hostname_buf = (gchar*) g_malloc0(4096);
775 pretty_hostname_buf = (gchar*) g_malloc0(4096);
46835f3e 776
baf05b70 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);
46835f3e 780
baf05b70 781 /* (2) set HOSTNAME */
782 if(gethostname(hostname_buf, MAXHOSTNAMELEN) || !g_strcmp0(hostname_buf, ""))
783 HOSTNAME = "localhost";
46835f3e 784
baf05b70 785 HOSTNAME = hostname_buf;
46835f3e 786
baf05b70 787 /* this bit gets you the /etc/myname style hostname
788 hostname_divider = strcspn(hostname_buf, ".");
789 strncpy(ret, hostname_buf, hostname_divider); */
46835f3e 790
baf05b70 791 /* (3) set PRETTY_HOSTNAME */
de46ace4 792 if((pretty_hostname_buf = config_get(OS_CONFIG_PATH, "PRETTY_HOSTNAME")))
baf05b70 793 PRETTY_HOSTNAME = pretty_hostname_buf;
de46ace4 794
baf05b70 795 else
796 PRETTY_HOSTNAME = "";
797
45e09604 798
799 /* (4) set STATIC_HOSTNAME */
de46ace4 800 if(!gethostname(static_hostname_buf, MAXHOSTNAMELEN))
45e09604 801 STATIC_HOSTNAME = static_hostname_buf;
46835f3e 802
45e09604 803 else
804 STATIC_HOSTNAME = "";
46835f3e 805
baf05b70 806 return (HOSTNAME && STATIC_HOSTNAME && PRETTY_HOSTNAME) ? TRUE : FALSE;
46835f3e 807}
808
bd24ff31 809gboolean 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
a1bcc33c 831gboolean determine_chassis_and_icon() {
832
b81ab32a 833 const size_t bufsize = 4096;
834
f8257c5d 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];
f1ad9351 838 unsigned int i;
f8257c5d 839 gboolean UNSURE_CHASSIS_FLAG, UNSURE_ICON_FLAG;
840
b81ab32a 841 hwproduct_size = hwmodel_size = hwvendor_size = hwmachine_size = bufsize;
68e777c6 842 UNSURE_CHASSIS_FLAG = UNSURE_ICON_FLAG = FALSE;
5ac7f542 843 i = 0;
f1ad9351 844
b81ab32a 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
bd24ff31 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
a1bcc33c 855 hwproduct_name[0] = CTL_HW;
856 hwproduct_name[1] = HW_PRODUCT;
f1ad9351 857
a1bcc33c 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
f8257c5d 864 hwmachine_name[0] = CTL_HW;
865 hwmachine_name[1] = HW_MACHINE;
866
a1bcc33c 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
f8257c5d 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
a1bcc33c 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
f8257c5d 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) {
f1ad9351 917
f8257c5d 918 if(!CHASSIS)
919 CHASSIS = "desktop";
920 if(!ICON)
921 ICON = "computer";
922 }
923
924 return (CHASSIS && ICON);
925}
926
927gboolean is_server(gchar *arch) {
928
929 unsigned int i;
a1bcc33c 930
f8257c5d 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
938gboolean 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
952int 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
970gboolean 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;
f1ad9351 990}
933f62bb 991
992static 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;
df8fc341 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 */
1011static 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 */
1087static 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}