2 * Copyright (c) 2003-2004 Apple Computer, 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@
23 Change History (most recent first):
26 Revision 1.3 2004/08/26 04:46:49 shersche
27 Add -q switch for silent operation
29 Revision 1.2 2004/06/23 16:39:14 shersche
30 Fix extraneous warnings regarding implict casts
32 Submitted by: Scott Herscher (sherscher@apple.com)
34 Revision 1.1 2004/06/18 04:14:26 rpantos
37 Revision 1.1 2004/01/30 03:02:58 bradley
38 NameSpace Provider Tool for installing, removing, list, etc. NameSpace Providers.
45 #include "CommonServices.h"
46 #include "DebugServices.h"
51 //===========================================================================================================================
53 //===========================================================================================================================
55 int main( int argc
, char *argv
[] );
56 DEBUG_LOCAL
void Usage( void );
57 DEBUG_LOCAL
int ProcessArgs( int argc
, char *argv
[] );
58 DEBUG_LOCAL OSStatus
InstallNSP( const char *inName
, const char *inGUID
, const char *inPath
);
59 DEBUG_LOCAL OSStatus
RemoveNSP( const char *inGUID
);
60 DEBUG_LOCAL OSStatus
EnableNSP( const char *inGUID
, BOOL inEnable
);
61 DEBUG_LOCAL OSStatus
ListNameSpaces( void );
62 DEBUG_LOCAL OSStatus
ReorderNameSpaces( void );
64 DEBUG_LOCAL WCHAR
* CharToWCharString( const char *inCharString
, WCHAR
*outWCharString
);
65 DEBUG_LOCAL
char * GUIDtoString( const GUID
*inGUID
, char *outString
);
66 DEBUG_LOCAL OSStatus
StringToGUID( const char *inCharString
, GUID
*outGUID
);
68 DEBUG_LOCAL BOOL gToolQuietMode
= FALSE
;
70 //===========================================================================================================================
72 //===========================================================================================================================
74 int main( int argc
, char *argv
[] )
78 debug_initialize( kDebugOutputTypeMetaConsole
);
79 debug_set_property( kDebugPropertyTagPrintLevel
, kDebugLevelVerbose
);
81 err
= ProcessArgs( argc
, argv
);
85 //===========================================================================================================================
87 //===========================================================================================================================
89 DEBUG_LOCAL
void Usage( void )
91 fprintf( stderr
, "\n" );
92 fprintf( stderr
, "NSP Tool 1.0d1\n" );
93 fprintf( stderr
, " Name Space Provider Tool\n" );
94 fprintf( stderr
, "\n" );
96 fprintf( stderr
, " -install <name> <guid> <path> - Installs a Name Space Provider\n" );
97 fprintf( stderr
, "\n" );
98 fprintf( stderr
, " <name> Name of the NSP\n" );
99 fprintf( stderr
, " <guid> GUID of the NSP\n" );
100 fprintf( stderr
, " <path> Path to the NSP file\n" );
101 fprintf( stderr
, "\n" );
103 fprintf( stderr
, " -remove <guid> - Removes a Name Space Provider\n" );
104 fprintf( stderr
, "\n" );
105 fprintf( stderr
, " <guid> GUID of the NSP\n" );
106 fprintf( stderr
, "\n" );
108 fprintf( stderr
, " -enable/-disable <guid> - Enables or Disables a Name Space Provider\n" );
109 fprintf( stderr
, "\n" );
110 fprintf( stderr
, " <guid> GUID of the NSP\n" );
111 fprintf( stderr
, "\n" );
113 fprintf( stderr
, " -list - Lists Name Space Providers\n" );
114 fprintf( stderr
, " -reorder - Reorders Name Space Providers\n" );
115 fprintf( stderr
, " -q - Enable quiet mode\n" );
116 fprintf( stderr
, " -h[elp] - Help\n" );
117 fprintf( stderr
, "\n" );
120 //===========================================================================================================================
122 //===========================================================================================================================
124 DEBUG_LOCAL
int ProcessArgs( int argc
, char* argv
[] )
138 for( i
= 1; i
< argc
; ++i
)
140 if( strcmp( argv
[ i
], "-install" ) == 0 )
144 if( argc
<= ( i
+ 3 ) )
146 fprintf( stderr
, "\n### ERROR: missing arguments for %s\n\n", argv
[ i
] );
157 name
= "DotLocalNSP";
161 guid
= "B600E6E9-553B-4a19-8696-335E5C896153";
164 err
= InstallNSP( name
, guid
, path
);
165 require_noerr( err
, exit
);
167 else if( strcmp( argv
[ i
], "-remove" ) == 0 )
171 if( argc
<= ( i
+ 1 ) )
173 fprintf( stderr
, "\n### ERROR: missing arguments for %s\n\n", argv
[ i
] );
181 guid
= "B600E6E9-553B-4a19-8696-335E5C896153";
184 err
= RemoveNSP( guid
);
185 require_noerr( err
, exit
);
187 else if( ( strcmp( argv
[ i
], "-enable" ) == 0 ) ||
188 ( strcmp( argv
[ i
], "-disable" ) == 0 ) )
194 enable
= ( strcmp( argv
[ i
], "-enable" ) == 0 );
195 if( argc
<= ( i
+ 1 ) )
197 fprintf( stderr
, "\n### ERROR: missing arguments for %s\n\n", argv
[ i
] );
204 err
= EnableNSP( guid
, enable
);
205 require_noerr( err
, exit
);
207 else if( strcmp( argv
[ i
], "-list" ) == 0 )
211 err
= ListNameSpaces();
212 require_noerr( err
, exit
);
214 else if( strcmp( argv
[ i
], "-reorder" ) == 0 )
218 err
= ReorderNameSpaces();
219 require_noerr( err
, exit
);
221 else if( strcmp( argv
[ i
], "-q" ) == 0 )
223 gToolQuietMode
= TRUE
;
225 else if( ( strcmp( argv
[ i
], "-help" ) == 0 ) ||
226 ( strcmp( argv
[ i
], "-h" ) == 0 ) )
236 fprintf( stderr
, "\n### ERROR: unknown argment: \"%s\"\n\n", argv
[ i
] );
252 //===========================================================================================================================
254 //===========================================================================================================================
256 OSStatus
InstallNSP( const char *inName
, const char *inGUID
, const char *inPath
)
263 WCHAR path
[ MAX_PATH
];
265 require_action( inName
&& ( *inName
!= '\0' ), exit
, err
= kParamErr
);
266 require_action( inGUID
&& ( *inGUID
!= '\0' ), exit
, err
= kParamErr
);
267 require_action( inPath
&& ( *inPath
!= '\0' ), exit
, err
= kParamErr
);
269 size
= strlen( inName
);
270 require_action( size
< sizeof_array( name
), exit
, err
= kSizeErr
);
271 CharToWCharString( inName
, name
);
273 err
= StringToGUID( inGUID
, &guid
);
274 require_noerr( err
, exit
);
276 size
= strlen( inPath
);
277 require_action( size
< sizeof_array( path
), exit
, err
= kSizeErr
);
278 CharToWCharString( inPath
, path
);
280 err
= WSAStartup( MAKEWORD( 2, 2 ), &wsd
);
281 err
= translate_errno( err
== 0, errno_compat(), WSAEINVAL
);
282 require_noerr( err
, exit
);
284 err
= WSCInstallNameSpace( name
, path
, NS_DNS
, 1, &guid
);
285 err
= translate_errno( err
== 0, errno_compat(), WSAEINVAL
);
287 require_noerr( err
, exit
);
291 fprintf( stderr
, "Installed NSP \"%s\" (%s) at %s\n", inName
, inGUID
, inPath
);
297 fprintf( stderr
, "### FAILED (%d) to install \"%s\" (%s) Name Space Provider at %s\n", err
, inName
, inGUID
, inPath
);
302 //===========================================================================================================================
304 //===========================================================================================================================
306 DEBUG_LOCAL OSStatus
RemoveNSP( const char *inGUID
)
312 require_action( inGUID
&& ( *inGUID
!= '\0' ), exit
, err
= kParamErr
);
314 err
= StringToGUID( inGUID
, &guid
);
315 require_noerr( err
, exit
);
317 err
= WSAStartup( MAKEWORD( 2, 2 ), &wsd
);
318 err
= translate_errno( err
== 0, errno_compat(), WSAEINVAL
);
319 require_noerr( err
, exit
);
321 err
= WSCUnInstallNameSpace( &guid
);
322 err
= translate_errno( err
== 0, errno_compat(), WSAEINVAL
);
324 require_noerr( err
, exit
);
328 fprintf( stderr
, "Removed NSP %s\n", inGUID
);
334 fprintf( stderr
, "### FAILED (%d) to remove %s Name Space Provider\n", err
, inGUID
);
339 //===========================================================================================================================
341 //===========================================================================================================================
343 DEBUG_LOCAL OSStatus
EnableNSP( const char *inGUID
, BOOL inEnable
)
349 require_action( inGUID
&& ( *inGUID
!= '\0' ), exit
, err
= kParamErr
);
351 err
= StringToGUID( inGUID
, &guid
);
352 require_noerr( err
, exit
);
354 err
= WSAStartup( MAKEWORD( 2, 2 ), &wsd
);
355 err
= translate_errno( err
== 0, errno_compat(), WSAEINVAL
);
356 require_noerr( err
, exit
);
358 err
= WSCEnableNSProvider( &guid
, inEnable
);
359 err
= translate_errno( err
== 0, errno_compat(), WSAEINVAL
);
361 require_noerr( err
, exit
);
365 fprintf( stderr
, "Removed NSP %s\n", inGUID
);
371 fprintf( stderr
, "### FAILED (%d) to remove %s Name Space Provider\n", err
, inGUID
);
376 //===========================================================================================================================
378 //===========================================================================================================================
380 DEBUG_LOCAL OSStatus
ListNameSpaces( void )
388 WSANAMESPACE_INFO
* array
;
394 err
= WSAStartup( MAKEWORD( 2, 2 ), &wsd
);
395 err
= translate_errno( err
== 0, errno_compat(), WSAEINVAL
);
396 require_noerr( err
, exit
);
399 // Build an array of all the NSPs. Call it first with NULL to get the size, allocate a buffer, then get them into it.
402 n
= WSAEnumNameSpaceProviders( &size
, NULL
);
403 err
= translate_errno( n
!= SOCKET_ERROR
, (OSStatus
) GetLastError(), kUnknownErr
);
404 require_action( err
== WSAEFAULT
, exit
, err
= kUnknownErr
);
406 array
= (WSANAMESPACE_INFO
*) malloc( size
);
407 require_action( array
, exit
, err
= kNoMemoryErr
);
409 n
= WSAEnumNameSpaceProviders( &size
, array
);
410 err
= translate_errno( n
!= SOCKET_ERROR
, (OSStatus
) GetLastError(), kUnknownErr
);
411 require_noerr( err
, exit
);
413 fprintf( stdout
, "\n" );
414 for( i
= 0; i
< n
; ++i
)
416 fprintf( stdout
, "Name Space %d\n", i
+ 1 );
417 fprintf( stdout
, " NSProviderId: %s\n", GUIDtoString( &array
[ i
].NSProviderId
, s
) );
418 fprintf( stdout
, " dwNameSpace: %d\n", array
[ i
].dwNameSpace
);
419 fprintf( stdout
, " fActive: %s\n", array
[ i
].fActive
? "YES" : "NO" );
420 fprintf( stdout
, " dwVersion: %d\n", array
[ i
].dwVersion
);
421 fprintf( stdout
, " lpszIdentifier: \"%s\"\n", array
[ i
].lpszIdentifier
);
422 fprintf( stdout
, "\n" );
437 fprintf( stderr
, "### FAILED (%d) to list Name Space Providers\n", err
);
442 //===========================================================================================================================
444 //===========================================================================================================================
446 DEBUG_LOCAL OSStatus
ReorderNameSpaces( void )
454 WSANAMESPACE_INFO
* array
;
456 WCHAR path
[ MAX_PATH
];
461 err
= WSAStartup( MAKEWORD( 2, 2 ), &wsd
);
462 err
= translate_errno( err
== 0, errno_compat(), WSAEINVAL
);
463 require_noerr( err
, exit
);
466 // Build an array of all the NSPs. Call it first with NULL to get the size, allocate a buffer, then get them into it.
469 n
= WSAEnumNameSpaceProviders( &size
, NULL
);
470 err
= translate_errno( n
!= SOCKET_ERROR
, (OSStatus
) GetLastError(), kUnknownErr
);
471 require_action( err
== WSAEFAULT
, exit
, err
= kUnknownErr
);
473 array
= (WSANAMESPACE_INFO
*) malloc( size
);
474 require_action( array
, exit
, err
= kNoMemoryErr
);
476 n
= WSAEnumNameSpaceProviders( &size
, array
);
477 err
= translate_errno( n
!= SOCKET_ERROR
, (OSStatus
) GetLastError(), kUnknownErr
);
478 require_noerr( err
, exit
);
480 // Find the "Tcpip" NSP.
482 for( i
= 0; i
< n
; ++i
)
484 if( strcmp( array
[ i
].lpszIdentifier
, "Tcpip" ) == 0 )
489 require_action( i
< n
, exit
, err
= kNotFoundErr
);
491 // Uninstall it then re-install it to move it to the end.
493 size
= (DWORD
) strlen( array
[ i
].lpszIdentifier
);
494 require_action( size
< sizeof_array( name
), exit
, err
= kSizeErr
);
495 CharToWCharString( array
[ i
].lpszIdentifier
, name
);
497 size
= (DWORD
) strlen( "%SystemRoot%\\System32\\mswsock.dll" );
498 require_action( size
< sizeof_array( path
), exit
, err
= kSizeErr
);
499 CharToWCharString( "%SystemRoot%\\System32\\mswsock.dll", path
);
501 err
= WSCUnInstallNameSpace( &array
[ i
].NSProviderId
);
502 err
= translate_errno( err
== 0, errno_compat(), WSAEINVAL
);
503 require_noerr( err
, exit
);
505 err
= WSCInstallNameSpace( name
, path
, NS_DNS
, array
[ i
].dwVersion
, &array
[ i
].NSProviderId
);
506 err
= translate_errno( err
== 0, errno_compat(), WSAEINVAL
);
507 require_noerr( err
, exit
);
511 fprintf( stderr
, "Reordered \"Tcpip\" NSP to to the bottom of the NSP chain\n" );
525 fprintf( stderr
, "### FAILED (%d) to reorder Name Space Providers\n", err
);
534 //===========================================================================================================================
536 //===========================================================================================================================
538 DEBUG_LOCAL WCHAR
* CharToWCharString( const char *inCharString
, WCHAR
*outWCharString
)
544 check( inCharString
);
545 check( outWCharString
);
548 dst
= outWCharString
;
554 } while( c
!= '\0' );
556 return( outWCharString
);
559 //===========================================================================================================================
561 //===========================================================================================================================
563 DEBUG_LOCAL
char * GUIDtoString( const GUID
*inGUID
, char *outString
)
568 sprintf( outString
, "%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x",
569 inGUID
->Data1
, inGUID
->Data2
, inGUID
->Data3
,
570 inGUID
->Data4
[ 0 ], inGUID
->Data4
[ 1 ], inGUID
->Data4
[ 2 ], inGUID
->Data4
[ 3 ],
571 inGUID
->Data4
[ 4 ], inGUID
->Data4
[ 5 ], inGUID
->Data4
[ 6 ], inGUID
->Data4
[ 7 ] );
575 //===========================================================================================================================
577 //===========================================================================================================================
579 DEBUG_LOCAL OSStatus
StringToGUID( const char *inCharString
, GUID
*outGUID
)
585 check( inCharString
);
588 n
= sscanf( inCharString
, "%lX-%hX-%hX-%02X%02X-%02X%02X%02X%02X%02X%02X",
589 &outGUID
->Data1
, &outGUID
->Data2
, &outGUID
->Data3
,
590 &v
[ 0 ], &v
[ 1 ], &v
[ 2 ], &v
[ 3 ], &v
[ 4 ], &v
[ 5 ], &v
[ 6 ], &v
[ 7 ] );
591 require_action( n
== 11, exit
, err
= kFormatErr
);
593 outGUID
->Data4
[ 0 ] = (unsigned char) v
[ 0 ];
594 outGUID
->Data4
[ 1 ] = (unsigned char) v
[ 1 ];
595 outGUID
->Data4
[ 2 ] = (unsigned char) v
[ 2 ];
596 outGUID
->Data4
[ 3 ] = (unsigned char) v
[ 3 ];
597 outGUID
->Data4
[ 4 ] = (unsigned char) v
[ 4 ];
598 outGUID
->Data4
[ 5 ] = (unsigned char) v
[ 5 ];
599 outGUID
->Data4
[ 6 ] = (unsigned char) v
[ 6 ];
600 outGUID
->Data4
[ 7 ] = (unsigned char) v
[ 7 ];