Leaked source code of windows server 2003
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.

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