1 /* -*- Mode: C; tab-width: 4 -*-
3 * Copyright (c) 2003-2004 Apple Computer, Inc. All rights reserved.
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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 Change History (most recent first):
20 Revision 1.4 2006/08/14 23:26:06 cheshire
21 Re-licensed mDNSResponder daemon source code under Apache License, Version 2.0
23 Revision 1.3 2004/08/26 04:46:49 shersche
24 Add -q switch for silent operation
26 Revision 1.2 2004/06/23 16:39:14 shersche
27 Fix extraneous warnings regarding implict casts
29 Submitted by: Scott Herscher (sherscher@apple.com)
31 Revision 1.1 2004/06/18 04:14:26 rpantos
34 Revision 1.1 2004/01/30 03:02:58 bradley
35 NameSpace Provider Tool for installing, removing, list, etc. NameSpace Providers.
42 #include "CommonServices.h"
43 #include "DebugServices.h"
48 //===========================================================================================================================
50 //===========================================================================================================================
52 int main( int argc
, char *argv
[] );
53 DEBUG_LOCAL
void Usage( void );
54 DEBUG_LOCAL
int ProcessArgs( int argc
, char *argv
[] );
55 DEBUG_LOCAL OSStatus
InstallNSP( const char *inName
, const char *inGUID
, const char *inPath
);
56 DEBUG_LOCAL OSStatus
RemoveNSP( const char *inGUID
);
57 DEBUG_LOCAL OSStatus
EnableNSP( const char *inGUID
, BOOL inEnable
);
58 DEBUG_LOCAL OSStatus
ListNameSpaces( void );
59 DEBUG_LOCAL OSStatus
ReorderNameSpaces( void );
61 DEBUG_LOCAL WCHAR
* CharToWCharString( const char *inCharString
, WCHAR
*outWCharString
);
62 DEBUG_LOCAL
char * GUIDtoString( const GUID
*inGUID
, char *outString
);
63 DEBUG_LOCAL OSStatus
StringToGUID( const char *inCharString
, GUID
*outGUID
);
65 DEBUG_LOCAL BOOL gToolQuietMode
= FALSE
;
67 //===========================================================================================================================
69 //===========================================================================================================================
71 int main( int argc
, char *argv
[] )
75 debug_initialize( kDebugOutputTypeMetaConsole
);
76 debug_set_property( kDebugPropertyTagPrintLevel
, kDebugLevelVerbose
);
78 err
= ProcessArgs( argc
, argv
);
82 //===========================================================================================================================
84 //===========================================================================================================================
86 DEBUG_LOCAL
void Usage( void )
88 fprintf( stderr
, "\n" );
89 fprintf( stderr
, "NSP Tool 1.0d1\n" );
90 fprintf( stderr
, " Name Space Provider Tool\n" );
91 fprintf( stderr
, "\n" );
93 fprintf( stderr
, " -install <name> <guid> <path> - Installs a Name Space Provider\n" );
94 fprintf( stderr
, "\n" );
95 fprintf( stderr
, " <name> Name of the NSP\n" );
96 fprintf( stderr
, " <guid> GUID of the NSP\n" );
97 fprintf( stderr
, " <path> Path to the NSP file\n" );
98 fprintf( stderr
, "\n" );
100 fprintf( stderr
, " -remove <guid> - Removes a Name Space Provider\n" );
101 fprintf( stderr
, "\n" );
102 fprintf( stderr
, " <guid> GUID of the NSP\n" );
103 fprintf( stderr
, "\n" );
105 fprintf( stderr
, " -enable/-disable <guid> - Enables or Disables a Name Space Provider\n" );
106 fprintf( stderr
, "\n" );
107 fprintf( stderr
, " <guid> GUID of the NSP\n" );
108 fprintf( stderr
, "\n" );
110 fprintf( stderr
, " -list - Lists Name Space Providers\n" );
111 fprintf( stderr
, " -reorder - Reorders Name Space Providers\n" );
112 fprintf( stderr
, " -q - Enable quiet mode\n" );
113 fprintf( stderr
, " -h[elp] - Help\n" );
114 fprintf( stderr
, "\n" );
117 //===========================================================================================================================
119 //===========================================================================================================================
121 DEBUG_LOCAL
int ProcessArgs( int argc
, char* argv
[] )
135 for( i
= 1; i
< argc
; ++i
)
137 if( strcmp( argv
[ i
], "-install" ) == 0 )
141 if( argc
<= ( i
+ 3 ) )
143 fprintf( stderr
, "\n### ERROR: missing arguments for %s\n\n", argv
[ i
] );
154 name
= "DotLocalNSP";
158 guid
= "B600E6E9-553B-4a19-8696-335E5C896153";
161 err
= InstallNSP( name
, guid
, path
);
162 require_noerr( err
, exit
);
164 else if( strcmp( argv
[ i
], "-remove" ) == 0 )
168 if( argc
<= ( i
+ 1 ) )
170 fprintf( stderr
, "\n### ERROR: missing arguments for %s\n\n", argv
[ i
] );
178 guid
= "B600E6E9-553B-4a19-8696-335E5C896153";
181 err
= RemoveNSP( guid
);
182 require_noerr( err
, exit
);
184 else if( ( strcmp( argv
[ i
], "-enable" ) == 0 ) ||
185 ( strcmp( argv
[ i
], "-disable" ) == 0 ) )
191 enable
= ( strcmp( argv
[ i
], "-enable" ) == 0 );
192 if( argc
<= ( i
+ 1 ) )
194 fprintf( stderr
, "\n### ERROR: missing arguments for %s\n\n", argv
[ i
] );
201 err
= EnableNSP( guid
, enable
);
202 require_noerr( err
, exit
);
204 else if( strcmp( argv
[ i
], "-list" ) == 0 )
208 err
= ListNameSpaces();
209 require_noerr( err
, exit
);
211 else if( strcmp( argv
[ i
], "-reorder" ) == 0 )
215 err
= ReorderNameSpaces();
216 require_noerr( err
, exit
);
218 else if( strcmp( argv
[ i
], "-q" ) == 0 )
220 gToolQuietMode
= TRUE
;
222 else if( ( strcmp( argv
[ i
], "-help" ) == 0 ) ||
223 ( strcmp( argv
[ i
], "-h" ) == 0 ) )
233 fprintf( stderr
, "\n### ERROR: unknown argment: \"%s\"\n\n", argv
[ i
] );
249 //===========================================================================================================================
251 //===========================================================================================================================
253 OSStatus
InstallNSP( const char *inName
, const char *inGUID
, const char *inPath
)
260 WCHAR path
[ MAX_PATH
];
262 require_action( inName
&& ( *inName
!= '\0' ), exit
, err
= kParamErr
);
263 require_action( inGUID
&& ( *inGUID
!= '\0' ), exit
, err
= kParamErr
);
264 require_action( inPath
&& ( *inPath
!= '\0' ), exit
, err
= kParamErr
);
266 size
= strlen( inName
);
267 require_action( size
< sizeof_array( name
), exit
, err
= kSizeErr
);
268 CharToWCharString( inName
, name
);
270 err
= StringToGUID( inGUID
, &guid
);
271 require_noerr( err
, exit
);
273 size
= strlen( inPath
);
274 require_action( size
< sizeof_array( path
), exit
, err
= kSizeErr
);
275 CharToWCharString( inPath
, path
);
277 err
= WSAStartup( MAKEWORD( 2, 2 ), &wsd
);
278 err
= translate_errno( err
== 0, errno_compat(), WSAEINVAL
);
279 require_noerr( err
, exit
);
281 err
= WSCInstallNameSpace( name
, path
, NS_DNS
, 1, &guid
);
282 err
= translate_errno( err
== 0, errno_compat(), WSAEINVAL
);
284 require_noerr( err
, exit
);
288 fprintf( stderr
, "Installed NSP \"%s\" (%s) at %s\n", inName
, inGUID
, inPath
);
294 fprintf( stderr
, "### FAILED (%d) to install \"%s\" (%s) Name Space Provider at %s\n", err
, inName
, inGUID
, inPath
);
299 //===========================================================================================================================
301 //===========================================================================================================================
303 DEBUG_LOCAL OSStatus
RemoveNSP( const char *inGUID
)
309 require_action( inGUID
&& ( *inGUID
!= '\0' ), exit
, err
= kParamErr
);
311 err
= StringToGUID( inGUID
, &guid
);
312 require_noerr( err
, exit
);
314 err
= WSAStartup( MAKEWORD( 2, 2 ), &wsd
);
315 err
= translate_errno( err
== 0, errno_compat(), WSAEINVAL
);
316 require_noerr( err
, exit
);
318 err
= WSCUnInstallNameSpace( &guid
);
319 err
= translate_errno( err
== 0, errno_compat(), WSAEINVAL
);
321 require_noerr( err
, exit
);
325 fprintf( stderr
, "Removed NSP %s\n", inGUID
);
331 fprintf( stderr
, "### FAILED (%d) to remove %s Name Space Provider\n", err
, inGUID
);
336 //===========================================================================================================================
338 //===========================================================================================================================
340 DEBUG_LOCAL OSStatus
EnableNSP( const char *inGUID
, BOOL inEnable
)
346 require_action( inGUID
&& ( *inGUID
!= '\0' ), exit
, err
= kParamErr
);
348 err
= StringToGUID( inGUID
, &guid
);
349 require_noerr( err
, exit
);
351 err
= WSAStartup( MAKEWORD( 2, 2 ), &wsd
);
352 err
= translate_errno( err
== 0, errno_compat(), WSAEINVAL
);
353 require_noerr( err
, exit
);
355 err
= WSCEnableNSProvider( &guid
, inEnable
);
356 err
= translate_errno( err
== 0, errno_compat(), WSAEINVAL
);
358 require_noerr( err
, exit
);
362 fprintf( stderr
, "Removed NSP %s\n", inGUID
);
368 fprintf( stderr
, "### FAILED (%d) to remove %s Name Space Provider\n", err
, inGUID
);
373 //===========================================================================================================================
375 //===========================================================================================================================
377 DEBUG_LOCAL OSStatus
ListNameSpaces( void )
385 WSANAMESPACE_INFO
* array
;
391 err
= WSAStartup( MAKEWORD( 2, 2 ), &wsd
);
392 err
= translate_errno( err
== 0, errno_compat(), WSAEINVAL
);
393 require_noerr( err
, exit
);
396 // Build an array of all the NSPs. Call it first with NULL to get the size, allocate a buffer, then get them into it.
399 n
= WSAEnumNameSpaceProviders( &size
, NULL
);
400 err
= translate_errno( n
!= SOCKET_ERROR
, (OSStatus
) GetLastError(), kUnknownErr
);
401 require_action( err
== WSAEFAULT
, exit
, err
= kUnknownErr
);
403 array
= (WSANAMESPACE_INFO
*) malloc( size
);
404 require_action( array
, exit
, err
= kNoMemoryErr
);
406 n
= WSAEnumNameSpaceProviders( &size
, array
);
407 err
= translate_errno( n
!= SOCKET_ERROR
, (OSStatus
) GetLastError(), kUnknownErr
);
408 require_noerr( err
, exit
);
410 fprintf( stdout
, "\n" );
411 for( i
= 0; i
< n
; ++i
)
413 fprintf( stdout
, "Name Space %d\n", i
+ 1 );
414 fprintf( stdout
, " NSProviderId: %s\n", GUIDtoString( &array
[ i
].NSProviderId
, s
) );
415 fprintf( stdout
, " dwNameSpace: %d\n", array
[ i
].dwNameSpace
);
416 fprintf( stdout
, " fActive: %s\n", array
[ i
].fActive
? "YES" : "NO" );
417 fprintf( stdout
, " dwVersion: %d\n", array
[ i
].dwVersion
);
418 fprintf( stdout
, " lpszIdentifier: \"%s\"\n", array
[ i
].lpszIdentifier
);
419 fprintf( stdout
, "\n" );
434 fprintf( stderr
, "### FAILED (%d) to list Name Space Providers\n", err
);
439 //===========================================================================================================================
441 //===========================================================================================================================
443 DEBUG_LOCAL OSStatus
ReorderNameSpaces( void )
451 WSANAMESPACE_INFO
* array
;
453 WCHAR path
[ MAX_PATH
];
458 err
= WSAStartup( MAKEWORD( 2, 2 ), &wsd
);
459 err
= translate_errno( err
== 0, errno_compat(), WSAEINVAL
);
460 require_noerr( err
, exit
);
463 // Build an array of all the NSPs. Call it first with NULL to get the size, allocate a buffer, then get them into it.
466 n
= WSAEnumNameSpaceProviders( &size
, NULL
);
467 err
= translate_errno( n
!= SOCKET_ERROR
, (OSStatus
) GetLastError(), kUnknownErr
);
468 require_action( err
== WSAEFAULT
, exit
, err
= kUnknownErr
);
470 array
= (WSANAMESPACE_INFO
*) malloc( size
);
471 require_action( array
, exit
, err
= kNoMemoryErr
);
473 n
= WSAEnumNameSpaceProviders( &size
, array
);
474 err
= translate_errno( n
!= SOCKET_ERROR
, (OSStatus
) GetLastError(), kUnknownErr
);
475 require_noerr( err
, exit
);
477 // Find the "Tcpip" NSP.
479 for( i
= 0; i
< n
; ++i
)
481 if( strcmp( array
[ i
].lpszIdentifier
, "Tcpip" ) == 0 )
486 require_action( i
< n
, exit
, err
= kNotFoundErr
);
488 // Uninstall it then re-install it to move it to the end.
490 size
= (DWORD
) strlen( array
[ i
].lpszIdentifier
);
491 require_action( size
< sizeof_array( name
), exit
, err
= kSizeErr
);
492 CharToWCharString( array
[ i
].lpszIdentifier
, name
);
494 size
= (DWORD
) strlen( "%SystemRoot%\\System32\\mswsock.dll" );
495 require_action( size
< sizeof_array( path
), exit
, err
= kSizeErr
);
496 CharToWCharString( "%SystemRoot%\\System32\\mswsock.dll", path
);
498 err
= WSCUnInstallNameSpace( &array
[ i
].NSProviderId
);
499 err
= translate_errno( err
== 0, errno_compat(), WSAEINVAL
);
500 require_noerr( err
, exit
);
502 err
= WSCInstallNameSpace( name
, path
, NS_DNS
, array
[ i
].dwVersion
, &array
[ i
].NSProviderId
);
503 err
= translate_errno( err
== 0, errno_compat(), WSAEINVAL
);
504 require_noerr( err
, exit
);
508 fprintf( stderr
, "Reordered \"Tcpip\" NSP to to the bottom of the NSP chain\n" );
522 fprintf( stderr
, "### FAILED (%d) to reorder Name Space Providers\n", err
);
531 //===========================================================================================================================
533 //===========================================================================================================================
535 DEBUG_LOCAL WCHAR
* CharToWCharString( const char *inCharString
, WCHAR
*outWCharString
)
541 check( inCharString
);
542 check( outWCharString
);
545 dst
= outWCharString
;
551 } while( c
!= '\0' );
553 return( outWCharString
);
556 //===========================================================================================================================
558 //===========================================================================================================================
560 DEBUG_LOCAL
char * GUIDtoString( const GUID
*inGUID
, char *outString
)
565 sprintf( outString
, "%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x",
566 inGUID
->Data1
, inGUID
->Data2
, inGUID
->Data3
,
567 inGUID
->Data4
[ 0 ], inGUID
->Data4
[ 1 ], inGUID
->Data4
[ 2 ], inGUID
->Data4
[ 3 ],
568 inGUID
->Data4
[ 4 ], inGUID
->Data4
[ 5 ], inGUID
->Data4
[ 6 ], inGUID
->Data4
[ 7 ] );
572 //===========================================================================================================================
574 //===========================================================================================================================
576 DEBUG_LOCAL OSStatus
StringToGUID( const char *inCharString
, GUID
*outGUID
)
582 check( inCharString
);
585 n
= sscanf( inCharString
, "%lX-%hX-%hX-%02X%02X-%02X%02X%02X%02X%02X%02X",
586 &outGUID
->Data1
, &outGUID
->Data2
, &outGUID
->Data3
,
587 &v
[ 0 ], &v
[ 1 ], &v
[ 2 ], &v
[ 3 ], &v
[ 4 ], &v
[ 5 ], &v
[ 6 ], &v
[ 7 ] );
588 require_action( n
== 11, exit
, err
= kFormatErr
);
590 outGUID
->Data4
[ 0 ] = (unsigned char) v
[ 0 ];
591 outGUID
->Data4
[ 1 ] = (unsigned char) v
[ 1 ];
592 outGUID
->Data4
[ 2 ] = (unsigned char) v
[ 2 ];
593 outGUID
->Data4
[ 3 ] = (unsigned char) v
[ 3 ];
594 outGUID
->Data4
[ 4 ] = (unsigned char) v
[ 4 ];
595 outGUID
->Data4
[ 5 ] = (unsigned char) v
[ 5 ];
596 outGUID
->Data4
[ 6 ] = (unsigned char) v
[ 6 ];
597 outGUID
->Data4
[ 7 ] = (unsigned char) v
[ 7 ];