Windows-Server-2003/sdktools/srvname/srvname.cxx

343 lines
11 KiB
C++

/*
* This program is used to edit the names that the NT LM server is using
* on the network (while the LM server is running).
*
* Type 'srvname -?' for usage
*
* IsaacHe 3/24/94
*
* Revision Histroy:
* JeffJuny 3/20/97: Fix ANSI/Unicode CRT conversion problem
* Mmccr 12/7/98: Added -m option, added param to err
*/
extern "C" {
#include <nt.h>
#include <ntrtl.h>
#include <nturtl.h>
#include <windows.h>
#include <stdlib.h>
#include <stdio.h>
#include <lm.h>
#include <string.h>
#include <locale.h>
}
/*
* print out an error message and return
*/
static void
err( char *text, WCHAR *name, ULONG code )
{
int i;
char msg[ 100 ];
i = FormatMessageA( FORMAT_MESSAGE_FROM_SYSTEM | sizeof( msg ),
NULL,
code,
0,
msg,
sizeof(msg),
NULL );
if( i )
fprintf( stderr, "%s%ws%ws%s %s\n", text?text:"", name?L" - ":L"", name?name:L"", text?" :":"", msg );
else
fprintf( stderr, "%s%ws%ws%s error %X\n", text?text:"", name?L" - ":L"", name?name:L"", text?" :":"", code );
}
/*
* Print out a handy usage message
*/
void
Usage( void )
{
fputs( "Usage: srvname [ options ] [ name ]\n"
" Options:\n"
" -s server point to 'server'\n"
" -d delete 'name' from the server's list\n"
" 'name' is added by default if -d not present\n"
" -D domain have 'name' act as if it is in 'domain'\n"
" -m multiple Append numbers from 1 to 'multiple' to 'name'\n"
" results in machine having multiple + 1 names added\n",
stderr);
}
__cdecl
main( int argc, char *argv[] )
{
DWORD retval;
DWORD NameMultiple = 0;
DWORD NumberLength = 0;
LPWSTR TargetServerName = NULL;
WCHAR serverNameBuf[ 100 ];
LPWSTR DomainName = NULL;
WCHAR domainNameBuf[ 100 ];
WCHAR newServerNameBuf[ 100 ];
WCHAR safenewServerNameBuf[ 100 ];
LPWSTR NewServerName = NULL;
CHAR *NewName = NULL;
CHAR ComputerNameBuf[MAX_COMPUTERNAME_LENGTH + 1];
DWORD dwbuflen =sizeof( ComputerNameBuf );
BOOLEAN DeleteTheName = FALSE;
PSERVER_INFO_100 si100;
DWORD j;
int i;
WCHAR wtempbuf[5];
char buf[ 500 ];
setlocale(LC_ALL, "");
for( i=1; i < argc; i++ ) {
if( argv[i][0] == '-' || argv[i][0] == '/' ) {
switch( argv[i][1] ) {
case 's':
if( i == argc-1 ) {
fputs( "Must supply a server name with -s option\n", stderr );
return 1;
}
mbstowcs( serverNameBuf, argv[ ++i ], sizeof( serverNameBuf )/sizeof(serverNameBuf[0]) );
TargetServerName = serverNameBuf;
break;
case 'D':
if( DeleteTheName == TRUE ) {
fputs( "-d and -D can not be used together\n", stderr );
return 1;
}
if( i == argc-1 ) {
fputs( "Must supply a domain name with -D option\n", stderr );
return 1;
}
mbstowcs( domainNameBuf, argv[ ++i ], sizeof( domainNameBuf )/sizeof(domainNameBuf[0]) );
DomainName = domainNameBuf;
break;
case 'm':
if( i == argc-1 ) {
fputs( "Must supply an integer multiple\n", stderr );
return 1;
}
NameMultiple = atoi(argv[++i]);
if( NameMultiple <= 0 ) {
fputs( "Multiple value is not a valid value\n", stderr );
return 1;
}
if( NameMultiple > 1024 ) {
fputs( "Multiple value must not be larger than 1024\n", stderr );
return 1;
}
NumberLength = strlen(argv[i]);
break;
case 'd':
DeleteTheName = TRUE;
break;
default:
fprintf( stderr, "%s : invalid option\n", argv[i] );
case '?':
Usage( );
return 1;
}
} else if( NewName == NULL ) {
NewName = argv[i];
mbstowcs( newServerNameBuf, NewName, sizeof( newServerNameBuf )/sizeof(newServerNameBuf[0]) );
NewServerName = newServerNameBuf;
} else {
Usage( );
return 1;
}
}
if( DeleteTheName == TRUE && NewName == NULL ) {
fputs( "You must supply the name to delete\n", stderr );
return 1;
}
if ((NewName == NULL)&&(NameMultiple == 0)) {
//
// Print the current list of transports
//
DWORD entriesread = 0, totalentries = 0, resumehandle = 0;
DWORD entriesread1 = 0;
PSERVER_TRANSPORT_INFO_0 psti0;
PSERVER_TRANSPORT_INFO_1 psti1;
DWORD total;
retval = NetServerTransportEnum ( TargetServerName,
1,
(LPBYTE *)&psti1,
(DWORD)-1,
&entriesread1,
&totalentries,
&resumehandle );
if( retval != NERR_Success ) {
entriesread1 = 0;
}
resumehandle = 0;
totalentries = 0;
retval = NetServerTransportEnum ( TargetServerName,
0,
(LPBYTE *)&psti0,
(DWORD)-1,
&entriesread,
&totalentries,
&resumehandle );
if( retval != NERR_Success ) {
err( "Could not get server transports", NULL, retval );
return retval;
}
if( entriesread != totalentries ) {
fprintf( stderr, "entries read = %d, total entries = %d\n", entriesread, totalentries );
fputs ( "Unable to read all the transport names!\n", stderr );
return 1;
}
for( total=i=0; i < (int)entriesread; i++ ) {
printf( "%-16.16s", psti0[i].svti0_transportaddress );
if( entriesread1 > (DWORD)i ) {
printf( "%-16.16ws", psti1[i].svti1_domain);
}
printf( " %-58.58ws", psti0[i].svti0_transportname );
printf( "%4u workstation%s\n",
psti0[i].svti0_numberofvcs,
psti0[i].svti0_numberofvcs != 1 ? "s" : "" );
total += psti0[i].svti0_numberofvcs;
}
if( total ) {
printf( " %s-----\n",
entriesread1?" ":"" );
printf( " %s%7u\n",
entriesread1?" ":"", total );
}
return 0;
}
else if (NewName == NULL) {
if (TargetServerName == NULL)
GetComputerName(newServerNameBuf, &dwbuflen);
else
wcscpy(newServerNameBuf, TargetServerName);
NewServerName = newServerNameBuf;
wcstombs(ComputerNameBuf, NewServerName, sizeof(ComputerNameBuf));
NewName = ComputerNameBuf;
}
if (strlen(NewName) > MAX_COMPUTERNAME_LENGTH) {
fputs("The new name you have chosen exceeds the maximum computername length.\n"
"Please select a different name.\n",
stderr);
return 1;
}
if (NameMultiple > 0) {
if ((NumberLength + strlen(NewName)) > MAX_COMPUTERNAME_LENGTH) {
i = MAX_COMPUTERNAME_LENGTH - strlen(NewName);
i = NumberLength - i;
fputs( "The Multiple you have chosen when concatinated with the servername\n"
"exceeds the maximum computername length. Try reducing your\n",
stderr);
if ((DWORD) i == NumberLength) {
fputs( "chosen servernames length.\n", stderr);
}
else
fprintf( stderr, "multiple by %d orders of magnitude\n", i );
return 1;
}
}
wcscpy(safenewServerNameBuf, NewServerName);
if( DeleteTheName == FALSE ) {
for (j = 0; j <= NameMultiple; j++)
{
if (j) {
wcscpy(NewServerName, safenewServerNameBuf);
_itow(j, wtempbuf, 10);
wcscat(NewServerName, wtempbuf);
}
//
// Add the new name to all of the transports
//
retval = NetServerComputerNameAdd( TargetServerName, DomainName, NewServerName );
if( retval != NERR_Success ) {
err( "NetServerComputerNameAdd", NewServerName, retval );
}
else {
printf("Added Name: %ws on Server: %ws%ws%ws\n",
NewServerName,
TargetServerName,
DomainName?L", Domain: ":L"",
DomainName?DomainName:L"");
}
}
return retval;
}
//
// Must be wanting to delete the name from all the networks.
//
//
// Make sure we don't delete the 'real name' that the server is known by. Pre 3.51
// servers did not ACL protect this api!
//
retval = NetServerGetInfo( TargetServerName, 100, (LPBYTE *)&si100 );
if( retval != STATUS_SUCCESS ) {
err( "Can not get target server name", NULL, retval );
return retval;
}
if( si100 == NULL ) {
fputs( "NetServerGetInfo returned a NULL ptr, but no error!\n", stderr );
return 1;
}
for (j = 0; j <= NameMultiple; j++)
{
if (j) {
wcscpy(NewServerName, safenewServerNameBuf);
_itow(j, wtempbuf, 10);
wcscat(NewServerName, wtempbuf);
}
if( !_wcsicmp( si100->sv100_name, NewServerName ) ) {
fprintf( stderr, "The primary name of %ws is %ws.\n",
TargetServerName ? TargetServerName : L"this server",
NewServerName );
fputs( "\tYou can not delete the primary name of the server\n", stderr );
}
else {
retval = NetServerComputerNameDel( TargetServerName, NewServerName );
if( retval != STATUS_SUCCESS ) {
err( "NetServerComputerNameDelete", NewServerName, retval );
}
else {
printf("Deleted Name: %ws on Server: %ws%ws%ws\n",
NewServerName,
TargetServerName,
DomainName?L", Domain: ":L"",
DomainName?DomainName:L"");
}
}
}
return 0;
}