Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

339 lines
11 KiB

  1. /*
  2. * This program is used to edit the names that the NT LM server is using
  3. * on the network (while the LM server is running).
  4. *
  5. * Type 'srvname -?' for usage
  6. *
  7. * IsaacHe 3/24/94
  8. *
  9. * Revision Histroy:
  10. * JeffJuny 3/20/97: Fix ANSI/Unicode CRT conversion problem
  11. * Mmccr 12/7/98: Added -m option, added param to err
  12. */
  13. extern "C" {
  14. #include <nt.h>
  15. #include <ntrtl.h>
  16. #include <nturtl.h>
  17. #include <windows.h>
  18. #include <stdlib.h>
  19. #include <stdio.h>
  20. #include <lm.h>
  21. #include <string.h>
  22. #include <locale.h>
  23. }
  24. /*
  25. * print out an error message and return
  26. */
  27. static void
  28. err( char *text, WCHAR *name, ULONG code )
  29. {
  30. int i;
  31. char msg[ 100 ];
  32. i = FormatMessageA( FORMAT_MESSAGE_FROM_SYSTEM | sizeof( msg ),
  33. NULL,
  34. code,
  35. 0,
  36. msg,
  37. sizeof(msg),
  38. NULL );
  39. if( i )
  40. fprintf( stderr, "%s%ws%ws%s %s\n", text?text:"", name?L" - ":L"", name?name:L"", text?" :":"", msg );
  41. else
  42. fprintf( stderr, "%s%ws%ws%s error %X\n", text?text:"", name?L" - ":L"", name?name:L"", text?" :":"", code );
  43. }
  44. /*
  45. * Print out a handy usage message
  46. */
  47. void
  48. Usage( char *name )
  49. {
  50. fprintf( stderr, "Usage: %s [ options ] [ name ]\n", name );
  51. fprintf( stderr, " Options:\n" );
  52. fprintf( stderr, " -s server point to 'server'\n" );
  53. fprintf( stderr, " -d delete 'name' from the server's list\n" );
  54. fprintf( stderr, " 'name' is added by default if -d not present\n" );
  55. fprintf( stderr, " -D domain have 'name' act as if it is in 'domain'\n" );
  56. fprintf( stderr, " -m multiple Append numbers from 1 to 'multiple' to 'name'\n" );
  57. fprintf( stderr, " results in machine having multiple + 1 names added\n" );
  58. }
  59. __cdecl
  60. main( int argc, char *argv[] )
  61. {
  62. DWORD retval;
  63. DWORD NameMultiple = 0;
  64. DWORD NumberLength = 0;
  65. LPWSTR TargetServerName = NULL;
  66. WCHAR serverNameBuf[ 100 ];
  67. LPWSTR DomainName = NULL;
  68. WCHAR domainNameBuf[ 100 ];
  69. WCHAR newServerNameBuf[ 100 ];
  70. WCHAR safenewServerNameBuf[ 100 ];
  71. LPWSTR NewServerName = NULL;
  72. CHAR *NewName = NULL;
  73. CHAR ComputerNameBuf[MAX_COMPUTERNAME_LENGTH + 1];
  74. DWORD dwbuflen =sizeof( ComputerNameBuf );
  75. BOOLEAN DeleteTheName = FALSE;
  76. PSERVER_INFO_100 si100;
  77. DWORD j;
  78. int i;
  79. WCHAR wtempbuf[5];
  80. char buf[ 500 ];
  81. setlocale(LC_ALL, "");
  82. for( i=1; i < argc; i++ ) {
  83. if( argv[i][0] == '-' || argv[i][0] == '/' ) {
  84. switch( argv[i][1] ) {
  85. case 's':
  86. if( i == argc-1 ) {
  87. fprintf( stderr, "Must supply a server name with -s option\n" );
  88. return 1;
  89. }
  90. mbstowcs( serverNameBuf, argv[ ++i ], sizeof( serverNameBuf ) );
  91. TargetServerName = serverNameBuf;
  92. break;
  93. case 'D':
  94. if( DeleteTheName == TRUE ) {
  95. fprintf( stderr, "-d and -D can not be used together\n" );
  96. return 1;
  97. }
  98. if( i == argc-1 ) {
  99. fprintf( stderr, "Must supply a domain name with -D option\n" );
  100. return 1;
  101. }
  102. mbstowcs( domainNameBuf, argv[ ++i ], sizeof( domainNameBuf ) );
  103. DomainName = domainNameBuf;
  104. break;
  105. case 'm':
  106. if( i == argc-1 ) {
  107. fprintf( stderr, "Must supply an integer multiple\n" );
  108. return 1;
  109. }
  110. NameMultiple = atoi(argv[++i]);
  111. if( NameMultiple <= 0 ) {
  112. fprintf( stderr, "Multiple value is not a valid value\n" );
  113. return 1;
  114. }
  115. if( NameMultiple > 1024 ) {
  116. fprintf( stderr, "Multiple value must not be larger than 1024\n" );
  117. return 1;
  118. }
  119. NumberLength = strlen(argv[i]);
  120. break;
  121. case 'd':
  122. DeleteTheName = TRUE;
  123. break;
  124. default:
  125. fprintf( stderr, "%s : invalid option\n", argv[i] );
  126. case '?':
  127. Usage( argv[0] );
  128. return 1;
  129. }
  130. } else if( NewName == NULL ) {
  131. NewName = argv[i];
  132. mbstowcs( newServerNameBuf, NewName, sizeof( newServerNameBuf ) );
  133. NewServerName = newServerNameBuf;
  134. } else {
  135. Usage( argv[0] );
  136. return 1;
  137. }
  138. }
  139. if( DeleteTheName == TRUE && NewName == NULL ) {
  140. fprintf( stderr, "You must supply the name to delete\n" );
  141. return 1;
  142. }
  143. if ((NewName == NULL)&&(NameMultiple == 0)) {
  144. //
  145. // Print the current list of transports
  146. //
  147. DWORD entriesread = 0, totalentries = 0, resumehandle = 0;
  148. DWORD entriesread1 = 0;
  149. PSERVER_TRANSPORT_INFO_0 psti0;
  150. PSERVER_TRANSPORT_INFO_1 psti1;
  151. DWORD total;
  152. retval = NetServerTransportEnum ( TargetServerName,
  153. 1,
  154. (LPBYTE *)&psti1,
  155. (DWORD)-1,
  156. &entriesread1,
  157. &totalentries,
  158. &resumehandle );
  159. if( retval != NERR_Success ) {
  160. entriesread1 = 0;
  161. }
  162. resumehandle = 0;
  163. totalentries = 0;
  164. retval = NetServerTransportEnum ( TargetServerName,
  165. 0,
  166. (LPBYTE *)&psti0,
  167. (DWORD)-1,
  168. &entriesread,
  169. &totalentries,
  170. &resumehandle );
  171. if( retval != NERR_Success ) {
  172. err( "Could not get server transports", NULL, retval );
  173. return retval;
  174. }
  175. if( entriesread != totalentries ) {
  176. fprintf( stderr, "entries read = %d, total entries = %d\n", entriesread, totalentries );
  177. fprintf( stderr, "Unable to read all the transport names!\n" );
  178. return 1;
  179. }
  180. for( total=i=0; i < (int)entriesread; i++ ) {
  181. printf( "%-16.16s", psti0[i].svti0_transportaddress );
  182. if( entriesread1 > (DWORD)i ) {
  183. printf( "%-16.16ws", psti1[i].svti1_domain);
  184. }
  185. printf( " %-58.58ws", psti0[i].svti0_transportname );
  186. printf( "%4u workstation%s\n",
  187. psti0[i].svti0_numberofvcs,
  188. psti0[i].svti0_numberofvcs != 1 ? "s" : "" );
  189. total += psti0[i].svti0_numberofvcs;
  190. }
  191. if( total ) {
  192. printf( " %s-----\n",
  193. entriesread1?" ":"" );
  194. printf( " %s%7u\n",
  195. entriesread1?" ":"", total );
  196. }
  197. return 0;
  198. }
  199. else if (NewName == NULL) {
  200. if (TargetServerName == NULL)
  201. GetComputerName(newServerNameBuf, &dwbuflen);
  202. else
  203. wcscpy(newServerNameBuf, TargetServerName);
  204. NewServerName = newServerNameBuf;
  205. wcstombs(ComputerNameBuf, NewServerName, sizeof(ComputerNameBuf));
  206. NewName = ComputerNameBuf;
  207. }
  208. if (strlen(NewName) > MAX_COMPUTERNAME_LENGTH) {
  209. fprintf( stderr, "The new name you have chosen exceeds the maximum computername length.\n" );
  210. fprintf( stderr, "Please select a different name.\n" );
  211. return 1;
  212. }
  213. if (NameMultiple > 0) {
  214. if ((NumberLength + strlen(NewName)) > MAX_COMPUTERNAME_LENGTH) {
  215. i = MAX_COMPUTERNAME_LENGTH - strlen(NewName);
  216. i = NumberLength - i;
  217. fprintf( stderr, "The Multiple you have chosen when concatinated with the servername\n" );
  218. fprintf( stderr, "exceeds the maximum computername length. Try reducing your\n" );
  219. if ((DWORD) i == NumberLength) {
  220. fprintf( stderr, "chosen servernames length.\n");
  221. }
  222. else
  223. fprintf( stderr, "multiple by %d orders of magnitude\n", i );
  224. return 1;
  225. }
  226. }
  227. wcscpy(safenewServerNameBuf, NewServerName);
  228. if( DeleteTheName == FALSE ) {
  229. for (j = 0; j <= NameMultiple; j++)
  230. {
  231. if (j) {
  232. wcscpy(NewServerName, safenewServerNameBuf);
  233. _itow(j, wtempbuf, 10);
  234. wcscat(NewServerName, wtempbuf);
  235. }
  236. //
  237. // Add the new name to all of the transports
  238. //
  239. retval = NetServerComputerNameAdd( TargetServerName, DomainName, NewServerName );
  240. if( retval != NERR_Success ) {
  241. err( "NetServerComputerNameAdd", NewServerName, retval );
  242. }
  243. else {
  244. printf("Added Name: %ws on Server: %ws%ws%ws\n",
  245. NewServerName,
  246. TargetServerName,
  247. DomainName?L", Domain: ":L"",
  248. DomainName?DomainName:L"");
  249. }
  250. }
  251. return retval;
  252. }
  253. //
  254. // Must be wanting to delete the name from all the networks.
  255. //
  256. //
  257. // Make sure we don't delete the 'real name' that the server is known by. Pre 3.51
  258. // servers did not ACL protect this api!
  259. //
  260. retval = NetServerGetInfo( TargetServerName, 100, (LPBYTE *)&si100 );
  261. if( retval != STATUS_SUCCESS ) {
  262. err( "Can not get target server name", NULL, retval );
  263. return retval;
  264. }
  265. if( si100 == NULL ) {
  266. fprintf( stderr, "NetServerGetInfo returned a NULL ptr, but no error!\n" );
  267. return 1;
  268. }
  269. for (j = 0; j <= NameMultiple; j++)
  270. {
  271. if (j) {
  272. wcscpy(NewServerName, safenewServerNameBuf);
  273. _itow(j, wtempbuf, 10);
  274. wcscat(NewServerName, wtempbuf);
  275. }
  276. if( !_wcsicmp( si100->sv100_name, NewServerName ) ) {
  277. fprintf( stderr, "The primary name of %ws is %ws.\n",
  278. TargetServerName ? TargetServerName : L"this server",
  279. NewServerName );
  280. fprintf( stderr, "\tYou can not delete the primary name of the server\n" );
  281. }
  282. else {
  283. retval = NetServerComputerNameDel( TargetServerName, NewServerName );
  284. if( retval != STATUS_SUCCESS ) {
  285. err( "NetServerComputerNameDelete", NewServerName, retval );
  286. }
  287. else {
  288. printf("Deleted Name: %ws on Server: %ws%ws%ws\n",
  289. NewServerName,
  290. TargetServerName,
  291. DomainName?L", Domain: ":L"",
  292. DomainName?DomainName:L"");
  293. }
  294. }
  295. }
  296. return 0;
  297. }