1/*
2 * Copyright (c) 2008 Apple Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
11 * file.
12 *
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
20 *
21 * @APPLE_LICENSE_HEADER_END@
22 */
23
24
25#include <sys/types.h>
26#include <stdio.h>
27#include <string.h>
28#include <dns_sd.h>
29#include <err.h>
30#include <unistd.h>
31
32
33void
34TXTRegisterCallback(DNSServiceRef sdRef __attribute__((unused)),
35		    DNSRecordRef RecordRef __attribute__((unused)),
36		    DNSServiceFlags flags __attribute__((unused)),
37		    DNSServiceErrorType errorCode __attribute__((unused)),
38		    void *context __attribute__((unused)))
39{
40}
41
42
43int
44main(int argc __attribute__((unused)), char **argv __attribute__((unused)))
45{
46    DNSServiceErrorType error;
47    DNSServiceRef dnsRef;
48    int i, fd;
49
50    if (argc < 2)
51	errx(1, "argc < 2");
52
53    for (i = 1; i < argc; i++) {
54	DNSRecordRef recordRef;
55	char *hostname = argv[i];
56	char *recordName;
57	char *realm;
58	size_t len;
59	const char *prefix = "LKDC:SHA1.fake";
60
61	asprintf(&recordName, "_kerberos.%s.", hostname);
62	if (recordName == NULL)
63	    errx(1, "malloc");
64
65	len = strlen(prefix) + strlen(hostname);
66	asprintf(&realm, "%c%s%s", (int)len, prefix, hostname);
67	if (realm == NULL)
68	    errx(1, "malloc");
69
70	error = DNSServiceCreateConnection(&dnsRef);
71	if (error)
72	    errx(1, "DNSServiceCreateConnection");
73
74	error =  DNSServiceRegisterRecord(dnsRef,
75					  &recordRef,
76					  kDNSServiceFlagsShared | kDNSServiceFlagsAllowRemoteQuery,
77					  0,
78					  recordName,
79					  kDNSServiceType_TXT,
80					  kDNSServiceClass_IN,
81					  len+1,
82					  realm,
83					  300,
84					  TXTRegisterCallback,
85					  NULL);
86	if (error)
87	    errx(1, "DNSServiceRegisterRecord: %d", error);
88    }
89
90    fd = DNSServiceRefSockFD(dnsRef);
91
92    while (1) {
93	int ret;
94	fd_set rfd;
95
96	FD_ZERO(&rfd);
97	FD_SET(fd, &rfd);
98
99	ret = select(fd + 1, &rfd, NULL, NULL, NULL);
100	if (ret == 0)
101	    errx(1, "timeout ?");
102	else if (ret < 0)
103	    err(1, "select");
104
105	if (FD_ISSET(fd, &rfd))
106	    DNSServiceProcessResult(dnsRef);
107    }
108}
109