xref: /titanic_52/usr/src/cmd/cmd-inet/usr.lib/mdnsd/PlatformCommon.c (revision 5eb92cf2b27ec0d138dc9e8eedd5b6ecf4cfec0c)
1 /* -*- Mode: C; tab-width: 4 -*-
2  *
3  * Copyright (c) 2004 Apple Computer, Inc. All rights reserved.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *     http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16 
17     Change History (most recent first):
18 
19 $Log: PlatformCommon.c,v $
20 Revision 1.7  2006/08/14 23:24:56  cheshire
21 Re-licensed mDNSResponder daemon source code under Apache License, Version 2.0
22 
23 Revision 1.6  2005/04/08 21:30:16  ksekar
24 <rdar://problem/4007457> Compiling problems with mDNSResponder-98 on Solaris/Sparc v9
25 Patch submitted by Bernd Kuhls
26 
27 Revision 1.5  2005/02/01 19:33:30  ksekar
28 <rdar://problem/3985239> Keychain format too restrictive
29 
30 Revision 1.4  2005/01/19 19:19:21  ksekar
31 <rdar://problem/3960191> Need a way to turn off domain discovery
32 
33 Revision 1.3  2004/12/13 17:46:52  cheshire
34 Use sizeof(buf) instead of fixed constant 1024
35 
36 Revision 1.2  2004/12/01 03:30:29  cheshire
37 <rdar://problem/3889346> Add Unicast DNS support to mDNSPosix
38 
39 Revision 1.1  2004/12/01 01:51:35  cheshire
40 Move ReadDDNSSettingsFromConfFile() from mDNSMacOSX.c to PlatformCommon.c
41 
42  */
43 
44 #pragma ident	"%Z%%M%	%I%	%E% SMI"
45 
46 #include <stdio.h>				// Needed for fopen() etc.
47 #include <unistd.h>				// Needed for close()
48 #include <string.h>				// Needed for strlen() etc.
49 #include <errno.h>			// Needed for errno etc.
50 #include <sys/socket.h>			// Needed for socket() etc.
51 #include <netinet/in.h>			// Needed for sockaddr_in
52 
53 #include "mDNSEmbeddedAPI.h"	// Defines the interface provided to the client layer above
54 #include "PlatformCommon.h"
55 
56 #ifdef NOT_HAVE_SOCKLEN_T
57     typedef unsigned int socklen_t;
58 #endif
59 
60 // Bind a UDP socket to a global destination to find the default route's interface address
61 mDNSexport void FindDefaultRouteIP(mDNSAddr *a)
62 	{
63 	struct sockaddr_in addr;
64 	socklen_t len = sizeof(addr);
65 	int sock = socket(AF_INET,SOCK_DGRAM,0);
66 	a->type = mDNSAddrType_None;
67 	if (sock == -1) return;
68 	addr.sin_family = AF_INET;
69 	addr.sin_port = 1;	// Not important, any port and public address will do
70 	addr.sin_addr.s_addr = 0x11111111;
71 	if ((connect(sock,(const struct sockaddr*)&addr,sizeof(addr))) == -1) { close(sock); return; }
72 	if ((getsockname(sock,(struct sockaddr*)&addr, &len)) == -1) { close(sock); return; }
73 	close(sock);
74 	a->type = mDNSAddrType_IPv4;
75 	a->ip.v4.NotAnInteger = addr.sin_addr.s_addr;
76 	}
77 
78 // dst must be at least MAX_ESCAPED_DOMAIN_NAME bytes, and option must be less than 32 bytes in length
79 mDNSlocal mDNSBool GetConfigOption(char *dst, const char *option, FILE *f)
80 	{
81 	char buf[32+1+MAX_ESCAPED_DOMAIN_NAME];	// Option name, one space, option value
82 	unsigned int len = strlen(option);
83 	if (len + 1 + MAX_ESCAPED_DOMAIN_NAME > sizeof(buf)-1) { LogMsg("GetConfigOption: option %s too long", option); return mDNSfalse; }
84 	fseek(f, 0, SEEK_SET);  // set position to beginning of stream
85 	while (fgets(buf, sizeof(buf), f))		// Read at most sizeof(buf)-1 bytes from file, and append '\0' C-string terminator
86 		{
87 		if (!strncmp(buf, option, len))
88 			{
89 			strncpy(dst, buf + len + 1, MAX_ESCAPED_DOMAIN_NAME-1);
90 			if (dst[MAX_ESCAPED_DOMAIN_NAME-1]) dst[MAX_ESCAPED_DOMAIN_NAME-1] = '\0';
91 			len = strlen(dst);
92 			if (len && dst[len-1] == '\n') dst[len-1] = '\0';  // chop newline
93 			return mDNStrue;
94 			}
95 		}
96 	debugf("Option %s not set", option);
97 	return mDNSfalse;
98 	}
99 
100 mDNSexport void ReadDDNSSettingsFromConfFile(mDNS *const m, const char *const filename, domainname *const hostname, domainname *const domain, mDNSBool *DomainDiscoveryDisabled)
101 	{
102 	char buf   [MAX_ESCAPED_DOMAIN_NAME];
103 	char secret[MAX_ESCAPED_DOMAIN_NAME] = "";
104 	mStatus err;
105 	FILE *f = fopen(filename, "r");
106 
107     if (hostname)                 hostname->c[0] = 0;
108     if (domain)                   domain->c[0] = 0;
109 	if (DomainDiscoveryDisabled) *DomainDiscoveryDisabled = mDNSfalse;
110 
111 	if (f)
112 		{
113 		if (DomainDiscoveryDisabled && GetConfigOption(buf, "DomainDiscoveryDisabled", f) && !strcasecmp(buf, "true")) *DomainDiscoveryDisabled = mDNStrue;
114 		if (hostname && GetConfigOption(buf, "hostname", f) && !MakeDomainNameFromDNSNameString(hostname, buf)) goto badf;
115 		if (domain && GetConfigOption(buf, "zone", f) && !MakeDomainNameFromDNSNameString(domain, buf)) goto badf;
116 		GetConfigOption(secret, "secret-64", f);  // failure means no authentication
117 		fclose(f);
118 		f = NULL;
119 		}
120 	else
121 		{
122 		if (errno != ENOENT) LogMsg("ERROR: Config file exists, but cannot be opened.");
123 		return;
124 		}
125 
126 	if (domain && domain->c[0] && secret[0])
127 		{
128 		// for now we assume keyname = service reg domain and we use same key for service and hostname registration
129 		err = mDNS_SetSecretForZone(m, domain, domain, secret);
130 		if (err) LogMsg("ERROR: mDNS_SetSecretForZone returned %d for domain %##s", err, domain->c);
131 		}
132 
133 	return;
134 
135 	badf:
136 	LogMsg("ERROR: malformatted config file");
137 	if (f) fclose(f);
138 	}
139