2 * Copyright (c) 2006-2011 Apple Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
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
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.
21 * @APPLE_LICENSE_HEADER_END@
25 * Modification History
27 * June 26, 2006 Allan Nathanson <ajn@apple.com>
34 #include <sys/param.h>
35 #include <sys/types.h>
36 #include <sys/socket.h>
38 #include <sys/sysctl.h>
41 #include <net/if_dl.h>
42 #include <netinet/in.h>
43 #include <arpa/inet.h>
44 #include <netdb_async.h>
46 #include <smb_server_prefs.h>
48 #include <CoreFoundation/CoreFoundation.h>
49 #include <CoreFoundation/CFStringDefaultEncoding.h> // for __CFStringGetInstallationEncodingAndRegion()
50 #include <SystemConfiguration/SystemConfiguration.h>
51 #include <SystemConfiguration/SCValidation.h>
52 #include <SystemConfiguration/SCPrivate.h> // for SCLog(), SCPrint()
54 #define HW_MODEL_LEN 64 // Note: must be >= NETBIOS_NAME_LEN (below)
56 #define NETBIOS_NAME_LEN 16
58 #define SMB_STARTUP_DELAY 60.0
59 #define SMB_DEBOUNCE_DELAY 5.0
61 static SCDynamicStoreRef store
= NULL
;
62 static CFRunLoopSourceRef rls
= NULL
;
64 static Boolean dnsActive
= FALSE
;
65 static CFMachPortRef dnsPort
= NULL
;
66 static struct timeval dnsQueryStart
;
68 static CFRunLoopTimerRef timer
= NULL
;
70 static Boolean _verbose
= FALSE
;
76 static enum { Unknown
, Client
, Server
} isServer
= Unknown
;
78 if (isServer
== Unknown
) {
82 ret
= stat("/System/Library/CoreServices/ServerVersion.plist", &statbuf
);
83 isServer
= (ret
== 0) ? Server
: Client
;
86 return (isServer
== Server
) ? TRUE
: FALSE
;
93 static CFAbsoluteTime bt
= 0;
96 int mib
[2] = { CTL_KERN
, KERN_BOOTTIME
};
98 size_t tv_len
= sizeof(tv
);
100 if (sysctl(mib
, sizeof(mib
) / sizeof(mib
[0]), &tv
, &tv_len
, NULL
, 0) == -1) {
101 SCLog(TRUE
, LOG_ERR
, CFSTR("sysctl() CTL_KERN/KERN_BOOTTIME failed: %s"), strerror(errno
));
102 return kCFAbsoluteTimeIntervalSince1970
;
105 // Note: we need to convert from Unix time to CF time.
106 bt
= (CFTimeInterval
)tv
.tv_sec
- kCFAbsoluteTimeIntervalSince1970
;
107 bt
+= (1.0E-6 * (CFTimeInterval
)tv
.tv_usec
);
115 copy_default_name(void)
118 char hwModel
[HW_MODEL_LEN
];
119 int mib
[] = { CTL_HW
, HW_MODEL
};
120 size_t n
= sizeof(hwModel
);
122 CFMutableStringRef str
;
125 bzero(&hwModel
, sizeof(hwModel
));
126 ret
= sysctl(mib
, sizeof(mib
) / sizeof(mib
[0]), &hwModel
, &n
, NULL
, 0);
128 SCLog(TRUE
, LOG_ERR
, CFSTR("sysctl() CTL_HW/HW_MODEL failed: %s"), strerror(errno
));
133 hwModel
[NETBIOS_NAME_LEN
- 1] = '\0';
135 // trim everything after (and including) a comma
136 cp
= index(hwModel
, ',');
141 // trim any trailing digits
144 if (!isdigit(hwModel
[n
- 1])) {
150 // start off with the [trunated] HW model
151 str
= CFStringCreateMutable(NULL
, 0);
152 CFStringAppendFormat(str
, NULL
, CFSTR("%s"), hwModel
);
155 // if there is room for at least one byte (two hex characters)
156 // of the MAC address than append that to the NetBIOS name.
158 // NETBIOS_NAME_LEN max length
159 // -1 the last byte is reserved
162 if (n
< (NETBIOS_NAME_LEN
- 1 - 3)) {
163 SCNetworkInterfaceRef interface
;
165 interface
= _SCNetworkInterfaceCreateWithBSDName(NULL
, CFSTR("en0"),
166 kIncludeNoVirtualInterfaces
);
167 if (interface
!= NULL
) {
168 CFMutableStringRef en0_MAC
;
170 en0_MAC
= (CFMutableStringRef
)SCNetworkInterfaceGetHardwareAddressString(interface
);
171 if (en0_MAC
!= NULL
) {
174 // remove ":" characters from MAC address string
175 en0_MAC
= CFStringCreateMutableCopy(NULL
, 0, en0_MAC
);
176 CFStringFindAndReplace(en0_MAC
,
179 CFRangeMake(0, CFStringGetLength(en0_MAC
)),
183 // compute how may bytes (characters) to append
184 // ... and limit that number to 6
186 // NETBIOS_NAME_LEN max length
187 // -1 the last byte is reserved
191 n
= ((NETBIOS_NAME_LEN
- 1 - n
- 1) / 2) * 2;
196 // remove what we don't want
197 en0_MAC_len
= CFStringGetLength(en0_MAC
);
198 if (en0_MAC_len
> n
) {
199 CFStringDelete(en0_MAC
, CFRangeMake(0, en0_MAC_len
- n
));
203 CFStringAppendFormat(str
, NULL
, CFSTR("-%@"), en0_MAC
);
207 CFRelease(interface
);
211 CFStringUppercase(str
, NULL
);
216 static CFDictionaryRef
217 smb_copy_global_configuration(SCDynamicStoreRef store
)
219 CFDictionaryRef dict
;
222 key
= SCDynamicStoreKeyCreateNetworkGlobalEntity(NULL
,
223 kSCDynamicStoreDomainState
,
225 dict
= SCDynamicStoreCopyValue(store
, key
);
229 if (isA_CFDictionary(dict
)) {
236 dict
= CFDictionaryCreate(NULL
, // allocator
240 &kCFTypeDictionaryKeyCallBacks
,
241 &kCFTypeDictionaryValueCallBacks
);
247 update_pref(SCPreferencesRef prefs
, CFStringRef key
, CFTypeRef newVal
, Boolean
*changed
)
251 curVal
= SCPreferencesGetValue(prefs
, key
);
252 if (!_SC_CFEqual(curVal
, newVal
)) {
253 if (newVal
!= NULL
) {
254 SCPreferencesSetValue(prefs
, key
, newVal
);
256 SCPreferencesRemoveValue(prefs
, key
);
267 smb_set_configuration(SCDynamicStoreRef store
, CFDictionaryRef dict
)
270 Boolean changed
= FALSE
;
271 UInt32 dosCodepage
= 0;
272 CFStringEncoding dosEncoding
= 0;
273 CFStringEncoding macEncoding
= kCFStringEncodingMacRoman
;
274 uint32_t macRegion
= 0;
276 SCPreferencesRef prefs
;
279 prefs
= SCPreferencesCreate(NULL
, CFSTR("smb-configuration"), CFSTR(kSMBPreferencesAppID
));
282 CFSTR("smb_set_configuration: SCPreferencesCreate() failed: %s"),
283 SCErrorString(SCError()));
287 ok
= SCPreferencesLock(prefs
, TRUE
);
290 CFSTR("smb_set_configuration: SCPreferencesLock() failed: %s"),
291 SCErrorString(SCError()));
295 if (!isMacOSXServer()) {
296 // Server description
297 str
= SCDynamicStoreCopyComputerName(store
, &macEncoding
);
298 update_pref(prefs
, CFSTR(kSMBPrefServerDescription
), str
, &changed
);
302 if (macEncoding
== kCFStringEncodingMacRoman
) {
304 CFDictionaryRef dict
;
307 key
= SCDynamicStoreKeyCreateComputerName(NULL
);
308 dict
= SCDynamicStoreCopyValue(store
, key
);
311 if (isA_CFDictionary(dict
)) {
315 num
= CFDictionaryGetValue(dict
, kSCPropSystemComputerNameRegion
);
316 if (isA_CFNumber(num
) &&
317 CFNumberGetValue(num
, kCFNumberSInt32Type
, &val
)) {
318 macRegion
= (uint32_t)val
;
328 // Important: must have root acccess (eUID==0) to access the config file!
329 __CFStringGetInstallationEncodingAndRegion((uint32_t *)&macEncoding
, &macRegion
);
331 _SC_dos_encoding_and_codepage(macEncoding
, macRegion
, &dosEncoding
, &dosCodepage
);
332 str
= CFStringCreateWithFormat(NULL
, NULL
, CFSTR("%d"), dosCodepage
);
333 update_pref(prefs
, CFSTR(kSMBPrefDOSCodePage
), str
, &changed
);
338 str
= CFDictionaryGetValue(dict
, kSCPropNetSMBNetBIOSName
);
339 str
= isA_CFString(str
);
340 update_pref(prefs
, CFSTR(kSMBPrefNetBIOSName
), str
, &changed
);
343 str
= CFDictionaryGetValue(dict
, kSCPropNetSMBNetBIOSNodeType
);
344 str
= isA_CFString(str
);
346 if (CFEqual(str
, kSCValNetSMBNetBIOSNodeTypeBroadcast
)) {
348 str
= CFSTR(kSMBPrefNetBIOSNodeBroadcast
);
349 } else if (CFEqual(str
, kSCValNetSMBNetBIOSNodeTypePeer
)) {
351 str
= CFSTR(kSMBPrefNetBIOSNodePeer
);
352 } else if (CFEqual(str
, kSCValNetSMBNetBIOSNodeTypeMixed
)) {
354 str
= CFSTR(kSMBPrefNetBIOSNodeMixed
);
355 } else if (CFEqual(str
, kSCValNetSMBNetBIOSNodeTypeHybrid
)) {
357 str
= CFSTR(kSMBPrefNetBIOSNodeHybrid
);
362 update_pref(prefs
, CFSTR(kSMBPrefNetBIOSNodeType
), str
, &changed
);
364 #ifdef ADD_NETBIOS_SCOPE
366 str
= CFDictionaryGetValue(dict
, kSCPropNetSMBNetBIOSScope
);
367 str
= isA_CFString(str
);
368 update_pref(prefs
, CFSTR(kSMBPrefNetBIOSScope
), str
, &changed
);
369 #endif // ADD_NETBIOS_SCOPE
372 array
= CFDictionaryGetValue(dict
, kSCPropNetSMBWINSAddresses
);
373 array
= isA_CFArray(array
);
374 update_pref(prefs
, CFSTR(kSMBPrefWINSServerAddressList
), array
, &changed
);
376 // Workgroup (or domain)
377 str
= CFDictionaryGetValue(dict
, kSCPropNetSMBWorkgroup
);
378 str
= isA_CFString(str
);
379 update_pref(prefs
, CFSTR(kSMBPrefWorkgroup
), str
, &changed
);
382 ok
= SCPreferencesCommitChanges(prefs
);
384 SCLog((SCError() != EROFS
), LOG_ERR
,
385 CFSTR("smb_set_configuration: SCPreferencesCommitChanges() failed: %s"),
386 SCErrorString(SCError()));
390 ok
= SCPreferencesApplyChanges(prefs
);
393 CFSTR("smb_set_configuration: SCPreferencesApplyChanges() failed: %s"),
394 SCErrorString(SCError()));
401 (void) SCPreferencesUnlock(prefs
);
408 copy_primary_service(SCDynamicStoreRef store
)
410 CFDictionaryRef dict
;
412 CFStringRef serviceID
= NULL
;
414 key
= SCDynamicStoreKeyCreateNetworkGlobalEntity(NULL
,
415 kSCDynamicStoreDomainState
,
417 dict
= SCDynamicStoreCopyValue(store
, key
);
421 if (isA_CFDictionary(dict
)) {
422 serviceID
= CFDictionaryGetValue(dict
, kSCDynamicStorePropNetPrimaryService
);
423 if (isA_CFString(serviceID
)) {
437 copy_primary_ip(SCDynamicStoreRef store
, CFStringRef serviceID
)
439 CFStringRef address
= NULL
;
440 CFDictionaryRef dict
;
443 key
= SCDynamicStoreKeyCreateNetworkServiceEntity(NULL
,
444 kSCDynamicStoreDomainState
,
447 dict
= SCDynamicStoreCopyValue(store
, key
);
451 if (isA_CFDictionary(dict
)) {
452 CFArrayRef addresses
;
454 addresses
= CFDictionaryGetValue(dict
, kSCPropNetIPv4Addresses
);
455 if (isA_CFArray(addresses
) && (CFArrayGetCount(addresses
) > 0)) {
456 address
= CFArrayGetValueAtIndex(addresses
, 0);
457 if (isA_CFString(address
)) {
472 reverseDNSComplete(int32_t status
, char *host
, char *serv
, void *context
)
474 CFDictionaryRef dict
;
475 struct timeval dnsQueryComplete
;
476 struct timeval dnsQueryElapsed
;
478 SCDynamicStoreRef store
= (SCDynamicStoreRef
)context
;
480 (void) gettimeofday(&dnsQueryComplete
, NULL
);
481 timersub(&dnsQueryComplete
, &dnsQueryStart
, &dnsQueryElapsed
);
482 SCLog(_verbose
, LOG_INFO
,
483 CFSTR("async DNS complete%s (query time = %d.%3.3d)"),
484 ((status
== 0) && (host
!= NULL
)) ? "" : ", host not found",
485 dnsQueryElapsed
.tv_sec
,
486 dnsQueryElapsed
.tv_usec
/ 1000);
488 // get network configuration
489 dict
= smb_copy_global_configuration(store
);
491 // use NetBIOS name from network configuration (if available)
492 name
= CFDictionaryGetValue(dict
, kSCPropNetSMBNetBIOSName
);
493 if ((name
!= NULL
) && _SC_CFStringIsValidNetBIOSName(name
)) {
494 SCLog(TRUE
, LOG_INFO
, CFSTR("NetBIOS name (network configuration) = %@"), name
);
498 // use reverse DNS name, if available
502 * if [reverse] DNS query was successful
507 dot
= strchr(host
, '.');
508 name
= CFStringCreateWithBytes(NULL
,
510 (dot
!= NULL
) ? dot
- host
: strlen(host
),
511 kCFStringEncodingUTF8
,
514 if (_SC_CFStringIsValidNetBIOSName(name
)) {
515 CFMutableDictionaryRef newDict
;
517 SCLog(TRUE
, LOG_INFO
, CFSTR("NetBIOS name (reverse DNS query) = %@"), name
);
518 newDict
= CFDictionaryCreateMutableCopy(NULL
, 0, dict
);
519 CFDictionarySetValue(newDict
, kSCPropNetSMBNetBIOSName
, name
);
532 #if defined(EAI_NODATA) && (EAI_NODATA != EAI_NONAME)
536 * if no name available
544 SCLog(TRUE
, LOG_ERR
, CFSTR("getnameinfo() failed: %s"), gai_strerror(status
));
547 // try local (multicast DNS) name, if available
548 name
= SCDynamicStoreCopyLocalHostName(store
);
550 if (_SC_CFStringIsValidNetBIOSName(name
)) {
551 CFMutableDictionaryRef newDict
;
553 SCLog(TRUE
, LOG_INFO
, CFSTR("NetBIOS name (multicast DNS) = %@"), name
);
554 newDict
= CFDictionaryCreateMutableCopy(NULL
, 0, dict
);
555 CFDictionarySetValue(newDict
, kSCPropNetSMBNetBIOSName
, name
);
564 // use "default" name
565 name
= copy_default_name();
567 CFMutableDictionaryRef newDict
;
569 SCLog(TRUE
, LOG_INFO
, CFSTR("NetBIOS name (default) = %@"), name
);
570 newDict
= CFDictionaryCreateMutableCopy(NULL
, 0, dict
);
571 CFDictionarySetValue(newDict
, kSCPropNetSMBNetBIOSName
, name
);
579 // update SMB configuration
580 smb_set_configuration(store
, dict
);
582 if (host
!= NULL
) free(host
);
583 if (dict
!= NULL
) CFRelease(dict
);
584 if (serv
!= NULL
) free(serv
);
591 replyMPCopyDescription(const void *info
)
593 SCDynamicStoreRef store
= (SCDynamicStoreRef
)info
;
595 return CFStringCreateWithFormat(NULL
,
597 CFSTR("<getnameinfo_async_start reply MP> {store = %p}"),
603 getnameinfo_async_handleCFReply(CFMachPortRef port
, void *msg
, CFIndex size
, void *info
)
605 mach_port_t mp
= MACH_PORT_NULL
;
608 if (port
!= dnsPort
) {
609 // we've received a callback on the async DNS port but since the
610 // associated CFMachPort doesn't match than the request must have
611 // already been cancelled.
612 SCLog(TRUE
, LOG_ERR
, CFSTR("getnameinfo_async_handleCFReply(): port != dnsPort"));
616 mp
= CFMachPortGetPort(port
);
617 CFMachPortInvalidate(dnsPort
);
621 status
= getnameinfo_async_handle_reply(msg
);
622 if ((status
== 0) && dnsActive
&& (mp
!= MACH_PORT_NULL
)) {
623 CFMachPortContext context
= { 0
627 , replyMPCopyDescription
629 CFRunLoopSourceRef rls
;
631 // if request has been re-queued
632 dnsPort
= _SC_CFMachPortCreateWithPort("IPMonitor/smb-configuration/re-queue",
634 getnameinfo_async_handleCFReply
,
636 rls
= CFMachPortCreateRunLoopSource(NULL
, dnsPort
, 0);
637 CFRunLoopAddSource(CFRunLoopGetCurrent(), rls
, kCFRunLoopDefaultMode
);
646 start_dns_query(SCDynamicStoreRef store
, CFStringRef address
)
650 struct sockaddr_in sin
;
651 struct sockaddr_in6 sin6
;
654 SCNetworkReachabilityFlags flags
;
658 if (_SC_cfstring_to_cstring(address
, buf
, sizeof(buf
), kCFStringEncodingASCII
) == NULL
) {
659 SCLog(TRUE
, LOG_ERR
, CFSTR("could not convert [primary] address"));
663 if (_SC_string_to_sockaddr(buf
, AF_UNSPEC
, (void *)&addr
, sizeof(addr
)) == NULL
) {
664 /* if not an IP[v6] address */
665 SCLog(TRUE
, LOG_ERR
, CFSTR("could not parse [primary] address"));
669 ok
= _SC_checkResolverReachabilityByAddress(&store
, &flags
, &haveDNS
, &addr
.sa
);
671 if (!(flags
& kSCNetworkReachabilityFlagsReachable
) ||
672 (flags
& kSCNetworkReachabilityFlagsConnectionRequired
)) {
673 // if not reachable *OR* connection required
679 CFMachPortContext context
= { 0
683 , replyMPCopyDescription
687 CFRunLoopSourceRef rls
;
689 (void) gettimeofday(&dnsQueryStart
, NULL
);
691 error
= getnameinfo_async_start(&mp
,
694 NI_NAMEREQD
, // flags
703 dnsPort
= _SC_CFMachPortCreateWithPort("IPMonitor/smb-configuration",
705 getnameinfo_async_handleCFReply
,
707 rls
= CFMachPortCreateRunLoopSource(NULL
, dnsPort
, 0);
708 CFRunLoopAddSource(CFRunLoopGetCurrent(), rls
, kCFRunLoopDefaultMode
);
719 smb_update_configuration(__unused CFRunLoopTimerRef _timer
, void *info
)
721 CFStringRef address
= NULL
;
722 CFDictionaryRef dict
;
724 CFStringRef serviceID
= NULL
;
725 SCDynamicStoreRef store
= (SCDynamicStoreRef
)info
;
727 // get network configuration
728 dict
= smb_copy_global_configuration(store
);
730 // use NetBIOS name from network configuration (if available)
731 name
= CFDictionaryGetValue(dict
, kSCPropNetSMBNetBIOSName
);
732 if ((name
!= NULL
) && _SC_CFStringIsValidNetBIOSName(name
)) {
733 SCLog(TRUE
, LOG_INFO
, CFSTR("NetBIOS name (network configuration) = %@"), name
);
737 // get primary service ID
738 serviceID
= copy_primary_service(store
);
739 if (serviceID
== NULL
) {
740 // if no primary service
744 // get DNS name associated with primary IP, if available
745 address
= copy_primary_ip(store
, serviceID
);
746 if (address
!= NULL
) {
749 // start reverse DNS query using primary IP address
750 ok
= start_dns_query(store
, address
);
759 // get local (multicast DNS) name, if available
761 name
= SCDynamicStoreCopyLocalHostName(store
);
763 if (_SC_CFStringIsValidNetBIOSName(name
)) {
764 CFMutableDictionaryRef newDict
;
766 SCLog(TRUE
, LOG_INFO
, CFSTR("NetBIOS name (multicast DNS) = %@"), name
);
767 newDict
= CFDictionaryCreateMutableCopy(NULL
, 0, dict
);
768 CFDictionarySetValue(newDict
, kSCPropNetSMBNetBIOSName
, name
);
777 // get "default" name
778 name
= copy_default_name();
780 CFMutableDictionaryRef newDict
;
782 SCLog(TRUE
, LOG_INFO
, CFSTR("NetBIOS name (default) = %@"), name
);
783 newDict
= CFDictionaryCreateMutableCopy(NULL
, 0, dict
);
784 CFDictionarySetValue(newDict
, kSCPropNetSMBNetBIOSName
, name
);
792 // update SMB configuration
793 smb_set_configuration(store
, dict
);
797 if (address
!= NULL
) CFRelease(address
);
798 if (dict
!= NULL
) CFRelease(dict
);
799 if (serviceID
!= NULL
) CFRelease(serviceID
);
802 CFRunLoopTimerInvalidate(timer
);
812 configuration_changed(SCDynamicStoreRef store
, CFArrayRef changedKeys
, void *info
)
814 CFRunLoopTimerContext context
= { 0, (void *)store
, CFRetain
, CFRelease
, NULL
};
815 CFAbsoluteTime time_boot
;
816 CFAbsoluteTime time_now
;
818 // if active, cancel any in-progress attempt to resolve the primary IP address
819 if (dnsPort
!= NULL
) {
820 mach_port_t mp
= CFMachPortGetPort(dnsPort
);
822 /* cancel the outstanding DNS query */
823 CFMachPortInvalidate(dnsPort
);
827 getnameinfo_async_cancel(mp
);
830 // if active, cancel any queued configuration change
832 CFRunLoopTimerInvalidate(timer
);
837 // queue configuration change
838 time_boot
= boottime() + SMB_STARTUP_DELAY
;
839 time_now
= CFAbsoluteTimeGetCurrent() + SMB_DEBOUNCE_DELAY
;
841 timer
= CFRunLoopTimerCreate(NULL
,
842 time_now
> time_boot
? time_now
: time_boot
,
846 smb_update_configuration
,
848 CFRunLoopAddTimer(CFRunLoopGetCurrent(), timer
, kCFRunLoopDefaultMode
);
856 load_smb_configuration(Boolean verbose
)
859 CFMutableArrayRef keys
= NULL
;
860 CFMutableArrayRef patterns
= NULL
;
866 /* initialize a few globals */
868 store
= SCDynamicStoreCreate(NULL
, CFSTR("smb-configuration"), configuration_changed
, NULL
);
871 CFSTR("SCDynamicStoreCreate() failed: %s"),
872 SCErrorString(SCError()));
876 /* establish notification keys and patterns */
878 keys
= CFArrayCreateMutable(NULL
, 0, &kCFTypeArrayCallBacks
);
879 patterns
= CFArrayCreateMutable(NULL
, 0, &kCFTypeArrayCallBacks
);
881 /* ...watch for primary service / interface changes */
882 key
= SCDynamicStoreKeyCreateNetworkGlobalEntity(NULL
,
883 kSCDynamicStoreDomainState
,
885 CFArrayAppendValue(keys
, key
);
888 /* ...watch for DNS configuration changes */
889 key
= SCDynamicStoreKeyCreateNetworkGlobalEntity(NULL
,
890 kSCDynamicStoreDomainState
,
892 CFArrayAppendValue(keys
, key
);
895 /* ...watch for SMB configuration changes */
896 key
= SCDynamicStoreKeyCreateNetworkGlobalEntity(NULL
,
897 kSCDynamicStoreDomainState
,
899 CFArrayAppendValue(keys
, key
);
902 /* ...watch for ComputerName changes */
903 key
= SCDynamicStoreKeyCreateComputerName(NULL
);
904 CFArrayAppendValue(keys
, key
);
907 /* ...watch for local (multicast DNS) hostname changes */
908 key
= SCDynamicStoreKeyCreateHostNames(NULL
);
909 CFArrayAppendValue(keys
, key
);
912 /* register the keys/patterns */
913 if (!SCDynamicStoreSetNotificationKeys(store
, keys
, patterns
)) {
915 CFSTR("SCDynamicStoreSetNotificationKeys() failed: %s"),
916 SCErrorString(SCError()));
920 rls
= SCDynamicStoreCreateRunLoopSource(NULL
, store
, 0);
923 CFSTR("SCDynamicStoreCreateRunLoopSource() failed: %s"),
924 SCErrorString(SCError()));
927 CFRunLoopAddSource(CFRunLoopGetCurrent(), rls
, kCFRunLoopDefaultMode
);
935 if (keys
!= NULL
) CFRelease(keys
);
936 if (patterns
!= NULL
) CFRelease(patterns
);
937 if (store
!= NULL
) CFRelease(store
);
944 main(int argc
, char **argv
)
950 CFStringRef serviceID
;
951 SCDynamicStoreRef store
;
954 if ((argc
> 1) && (strcmp(argv
[1], "-d") == 0)) {
960 store
= SCDynamicStoreCreate(NULL
, CFSTR("smb-configuration"), NULL
, NULL
);
962 SCPrint(TRUE
, stdout
,
963 CFSTR("SCDynamicStoreCreate() failed: %s\n"),
964 SCErrorString(SCError()));
968 // get "default" name
969 name
= copy_default_name();
971 SCPrint(TRUE
, stdout
, CFSTR("default name = %@\n"), name
);
975 // get primary service
976 serviceID
= copy_primary_service(store
);
977 if (serviceID
!= NULL
) {
978 SCPrint(TRUE
, stdout
, CFSTR("primary service ID = %@\n"), serviceID
);
980 SCPrint(TRUE
, stdout
, CFSTR("No primary service\n"));
984 if ((argc
== (2+1)) && (argv
[1][0] == 's')) {
985 if (serviceID
!= NULL
) CFRelease(serviceID
);
986 serviceID
= CFStringCreateWithCString(NULL
, argv
[2], kCFStringEncodingUTF8
);
987 SCPrint(TRUE
, stdout
, CFSTR("alternate service ID = %@\n"), serviceID
);
990 // get primary IP address
991 address
= copy_primary_ip(store
, serviceID
);
992 CFRelease(serviceID
);
993 if (address
!= NULL
) {
994 SCPrint(TRUE
, stdout
, CFSTR("primary address = %@\n"), address
);
996 if ((argc
== (2+1)) && (argv
[1][0] == 'a')) {
997 if (address
!= NULL
) CFRelease(address
);
998 address
= CFStringCreateWithCString(NULL
, argv
[2], kCFStringEncodingUTF8
);
999 SCPrint(TRUE
, stdout
, CFSTR("alternate primary address = %@\n"), address
);
1002 // start reverse DNS query using primary IP address
1003 (void) start_dns_query(store
, address
);
1009 smb_update_configuration(NULL
, (void *)store
);
1018 _sc_verbose
= (argc
> 1) ? TRUE
: FALSE
;
1020 load_smb_configuration((argc
> 1) ? TRUE
: FALSE
);