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.

468 lines
14 KiB

  1. //+-----------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. //
  5. // Copyright (c) Microsoft Corporation 1991 - 1998
  6. //
  7. // File: ntlmtest.cpp
  8. //
  9. // Contents:
  10. //
  11. //
  12. // History: 07-Dec-98 Created ChandanS
  13. //
  14. // Comments: This program tests DsGetDcName and logon on Win9x
  15. //
  16. //------------------------------------------------------------------------
  17. // NT Headers
  18. extern "C"
  19. {
  20. #ifndef WIN32_CHICAGO
  21. #include <nt.h>
  22. #include <ntrtl.h>
  23. #include <nturtl.h>
  24. #endif
  25. #include <windows.h>
  26. }
  27. #define UF_NORMAL_ACCOUNT 0x0200
  28. // Cairo Headers
  29. extern "C"
  30. {
  31. // #define SECURITY_NTLM
  32. #include <security.h>
  33. #include <dsgetdc.h>
  34. #ifndef WIN32_CHICAGO
  35. #include <secmisc.h>
  36. #endif
  37. }
  38. // C headers
  39. extern "C"
  40. {
  41. #include <conio.h>
  42. #include <ctype.h>
  43. #include <stdio.h>
  44. #include <string.h>
  45. #include <tchar.h>
  46. #ifndef WIN32_CHICAGO
  47. #include <wcstr.h>
  48. #endif
  49. #include <stdlib.h>
  50. #include <dnsapi.h>
  51. }
  52. void
  53. PrintFlags(DWORD DcFlags)
  54. {
  55. DWORD Flags = DcFlags;
  56. printf("Flags: \t\t\t\t");
  57. if (Flags & DS_PDC_FLAG)
  58. {
  59. printf(" DS_PDC_FLAG ");
  60. }
  61. if (Flags & DS_GC_FLAG)
  62. {
  63. printf(" DS_GC_FLAG ");
  64. }
  65. if (Flags & DS_LDAP_FLAG)
  66. {
  67. printf(" DS_LDAP_FLAG ");
  68. }
  69. if (Flags & DS_DS_FLAG)
  70. {
  71. printf(" DS_DS_FLAG ");
  72. }
  73. if (Flags & DS_KDC_FLAG)
  74. {
  75. printf(" DS_KDC_FLAG ");
  76. }
  77. if (Flags & DS_TIMESERV_FLAG)
  78. {
  79. printf(" DS_TIMESERV_FLAG ");
  80. }
  81. if (Flags & DS_CLOSEST_FLAG)
  82. {
  83. printf(" DS_CLOSEST_FLAG ");
  84. }
  85. if (Flags & DS_WRITABLE_FLAG)
  86. {
  87. printf(" DS_WRITABLE_FLAG ");
  88. }
  89. if (Flags & DS_GOOD_TIMESERV_FLAG)
  90. {
  91. printf(" DS_GOOD_TIMESERV_FLAG ");
  92. }
  93. if (Flags & DS_PING_FLAGS)
  94. {
  95. printf(" DS_PING_FLAGS ");
  96. }
  97. if (Flags & DS_DNS_CONTROLLER_FLAG)
  98. {
  99. printf(" DS_DNS_CONTROLLER_FLAG ");
  100. }
  101. if (Flags & DS_DNS_DOMAIN_FLAG)
  102. {
  103. printf(" DS_DNS_DOMAIN_FLAG ");
  104. }
  105. if (Flags & DS_DNS_FOREST_FLAG)
  106. {
  107. printf(" DS_DNS_FOREST_FLAG ");
  108. }
  109. printf("\n");
  110. }
  111. SECURITY_STATUS
  112. test_dsgetdcnamea(LPSTR pDomain, LPSTR pUser, DWORD Flags)
  113. {
  114. SECURITY_STATUS scRet;
  115. int i;
  116. DWORD (WINAPI *pDsGetDcNameA) (LPCSTR, LPCSTR, GUID *, LPCSTR, ULONG, PDOMAIN_CONTROLLER_INFOA *);
  117. DWORD (WINAPI *pDsGetDcNameWithAccountA) (LPCSTR, LPCSTR, ULONG, LPCSTR, GUID *, LPCSTR, ULONG, PDOMAIN_CONTROLLER_INFOA *);
  118. HINSTANCE hInstance = NULL;
  119. ULONG NetStatus = 0;
  120. PDOMAIN_CONTROLLER_INFOA Controller = NULL;
  121. PDOMAIN_CONTROLLER_INFOA ControllerWithAccount = NULL;
  122. CHAR szBuf[] = "";
  123. hInstance = LoadLibrary("logonsrv");
  124. if (hInstance == NULL)
  125. {
  126. printf("LOCATOR: Error %ld Can't load logonsrv.dll. trying netapi32\r\n", GetLastError());
  127. }
  128. if (hInstance == NULL)
  129. {
  130. hInstance = LoadLibrary("netapi32");
  131. }
  132. if (hInstance == NULL)
  133. {
  134. printf("LOCATOR: Error %ld Can't load netapi32.dll.\r\n", GetLastError());
  135. }
  136. if (hInstance != NULL)
  137. {
  138. pDsGetDcNameA = (DWORD (WINAPI *)(LPCSTR, LPCSTR, GUID *,
  139. LPCSTR, ULONG,
  140. PDOMAIN_CONTROLLER_INFOA *))
  141. GetProcAddress(hInstance, "DsGetDcNameA");
  142. if (pDsGetDcNameA == NULL)
  143. {
  144. printf("LOCATOR: No DsGetDcNameA\n");
  145. }
  146. else
  147. {
  148. NetStatus = (*pDsGetDcNameA)(NULL,
  149. pDomain,
  150. NULL,
  151. NULL,
  152. Flags,
  153. &Controller);
  154. printf("LOCATOR: DsGetdcNameA returns 0x%x\r\n", NetStatus);
  155. if (NetStatus == 0)
  156. {
  157. printf("DomainControllerName: \t\t\"%s\"\n", Controller->DomainControllerName ? Controller->DomainControllerName : szBuf);
  158. printf("DomainControllerAddress:\t\"%s\"\n", Controller->DomainControllerAddress ? Controller->DomainControllerAddress : szBuf );
  159. printf("DomainControllerAddressType: \t%d\n", Controller->DomainControllerAddressType );
  160. printf("DomainGuid : \t\n");
  161. printf("DomainName: \t\t\t\"%s\"\n", Controller->DomainName);
  162. printf("DnsForestName: \t\t\t\"%s\"\n", Controller->DnsForestName ? Controller->DnsForestName : szBuf);
  163. PrintFlags(Controller->Flags);
  164. printf("DcSiteName: \t\t\t\"%s\"\n", Controller->DcSiteName ? Controller->DcSiteName : szBuf);
  165. printf("ClientSiteName: \t\t\"%s\"\n", Controller->ClientSiteName ? Controller->ClientSiteName : szBuf);
  166. }
  167. }
  168. pDsGetDcNameWithAccountA = (DWORD (WINAPI *)(LPCSTR, LPCSTR, ULONG, LPCSTR, GUID *,
  169. LPCSTR, ULONG,
  170. PDOMAIN_CONTROLLER_INFOA *))
  171. GetProcAddress(hInstance, "DsGetDcNameWithAccountA");
  172. if (pDsGetDcNameWithAccountA == NULL)
  173. {
  174. printf("LOCATOR: No DsGetDcNameWithAccountA\n");
  175. }
  176. else
  177. {
  178. NetStatus = (*pDsGetDcNameWithAccountA)(NULL,
  179. pUser,
  180. (pUser == NULL) ? 0 : UF_NORMAL_ACCOUNT,
  181. pDomain,
  182. NULL,
  183. NULL,
  184. Flags,
  185. &Controller);
  186. printf("LOCATOR: DsGetdcNameWithAccountA returns 0x%x\r\n", NetStatus);
  187. if (NetStatus == 0)
  188. {
  189. printf("DomainControllerName: \t\t\"%s\"\n", Controller->DomainControllerName ? Controller->DomainControllerName : szBuf);
  190. printf("DomainControllerAddress:\t\"%s\"\n", Controller->DomainControllerAddress ? Controller->DomainControllerAddress : szBuf );
  191. printf("DomainControllerAddressType: \t%d\n", Controller->DomainControllerAddressType );
  192. printf("DomainGuid : \t\n");
  193. printf("DomainName: \t\t\t\"%s\"\n", Controller->DomainName);
  194. printf("DnsForestName: \t\t\t\"%s\"\n", Controller->DnsForestName ? Controller->DnsForestName : szBuf);
  195. PrintFlags(Controller->Flags);
  196. printf("DcSiteName: \t\t\t\"%s\"\n", Controller->DcSiteName ? Controller->DcSiteName : szBuf);
  197. printf("ClientSiteName: \t\t\"%s\"\n", Controller->ClientSiteName ? Controller->ClientSiteName : szBuf);
  198. }
  199. }
  200. if (hInstance)
  201. {
  202. FreeLibrary(hInstance);
  203. }
  204. }
  205. #if 0
  206. hInstance = LoadLibrary("kerberos");
  207. if (hInstance == NULL)
  208. {
  209. printf("NTLMTEST: Can't load kerberos.dll.\r\n");
  210. }
  211. else
  212. {
  213. INIT_SECURITY_INTERFACE InitSecurityInterface = NULL;
  214. InitSecurityInterface = (INIT_SECURITY_INTERFACE) GetProcAddress(hInstance, SECURITY_ENTRYPOINTA);
  215. if ( NULL == InitSecurityInterface)
  216. {
  217. printf("NTLMTEST: No InitSecurityInterface\n");
  218. }
  219. else
  220. {
  221. PSecurityFunctionTable Table = InitSecurityInterface();
  222. if (Table != NULL)
  223. {
  224. if (Table->SspiLogonUser != NULL)
  225. {
  226. if (pAuthData && pAuthData->User && pAuthData->Domain && pAuthData->Password)
  227. {
  228. scRet = Table->SspiLogonUser("kerberos",
  229. pAuthData->User,
  230. pAuthData->Domain,
  231. pAuthData->Password);
  232. printf("NTLMTEST: SspiLogonUserA returns 0x%x\r\n", scRet);
  233. }
  234. }
  235. else
  236. {
  237. printf("NTLMTEST: No SspiLogonUser\n");
  238. }
  239. }
  240. else
  241. {
  242. printf("NTLMTEST: No table\n");
  243. }
  244. }
  245. printf("NTLMTEST: Freeing secur32.dll.\r\n");
  246. FreeLibrary(hInstance);
  247. }
  248. #endif
  249. return(S_OK);
  250. }
  251. void
  252. Usage(BOOL fVerbose)
  253. {
  254. printf("Usage:\tlocator [/domain:Domain] [/user:User] [/force] [/dsreq] [/dspref] [/gc] [/pdc]\n \t [/ip] [/kdc] [/time] [/write] [/goodtime] [avoidself]\n \t [/onlyldap] [/isflatname] [/isdnsname] [/retdns] [/retflat]\n\tlocator /? \n");
  255. if (fVerbose)
  256. {
  257. printf("Domain: domain to look up the dc in.\n");
  258. printf("\n/? : Display this message.\n");
  259. }
  260. exit(1);
  261. }
  262. char *
  263. ArgValue(char *arg)
  264. {
  265. char *retval = strchr(arg + 2, ':');
  266. if (retval != NULL)
  267. retval++;
  268. else
  269. retval = arg + strlen(arg);
  270. return retval;
  271. }
  272. enum {
  273. NoAction,
  274. #define DOMAIN "/Domain"
  275. Domain,
  276. #define USER "/User"
  277. User,
  278. #define FORCE "/Force"
  279. Force,
  280. #define DSREQ "/DsReq"
  281. DsReq,
  282. #define DSPREF "/DsPref"
  283. DsPref,
  284. #define GC "/Gc"
  285. Gc,
  286. #define PDC "/pdc"
  287. pdc,
  288. #define IP "/ip"
  289. ip,
  290. #define KDC "/kdc"
  291. kdc,
  292. #define TIME "/time"
  293. time,
  294. #define WRITE "/write"
  295. write,
  296. #define GOODTIME "/goodtime"
  297. goodtime,
  298. #define AVOIDSELF "/avoidself"
  299. avoidself,
  300. #define ONLYLDAP "/onlyldap"
  301. onlyldap,
  302. #define ISFLATNAME "/isflatname"
  303. isflatname,
  304. #define ISDNSNAME "/isdnsname"
  305. isdnsname,
  306. #define RETDNS "/retdns"
  307. retdns,
  308. #define RETFLAT "/retflat"
  309. retflat,
  310. #define HELP "/?"
  311. help
  312. } Action = NoAction;
  313. int
  314. _cdecl main(int argc, char *argv[])
  315. {
  316. LPTSTR Tmp = NULL, pDomain = NULL, pUser = NULL;
  317. int i = 1, Len = 0;
  318. LPSTR Arg = NULL;
  319. DWORD Flags = 0;
  320. for (i = 1; i < argc; i++)
  321. {
  322. Arg = argv[i];
  323. #define DS_FORCE_REDISCOVERY 0x00000001
  324. #define DS_DIRECTORY_SERVICE_REQUIRED 0x00000010
  325. #define DS_DIRECTORY_SERVICE_PREFERRED 0x00000020
  326. #define DS_GC_SERVER_REQUIRED 0x00000040
  327. #define DS_PDC_REQUIRED 0x00000080
  328. #define DS_IP_REQUIRED 0x00000200
  329. #define DS_KDC_REQUIRED 0x00000400
  330. #define DS_TIMESERV_REQUIRED 0x00000800
  331. #define DS_WRITABLE_REQUIRED 0x00001000
  332. #define DS_GOOD_TIMESERV_PREFERRED 0x00002000
  333. #define DS_AVOID_SELF 0x00004000
  334. #define DS_ONLY_LDAP_NEEDED 0x00008000
  335. #define DS_IS_FLAT_NAME 0x00010000
  336. #define DS_IS_DNS_NAME 0x00020000
  337. #define DS_RETURN_DNS_NAME 0x40000000
  338. #define DS_RETURN_FLAT_NAME 0x80000000
  339. if ( _strnicmp( Arg, DOMAIN, sizeof(DOMAIN)-1) == 0 ) {
  340. pDomain = ArgValue(argv[i]);
  341. }
  342. else if ( _strnicmp( Arg, USER, sizeof(USER)-1) == 0 ) {
  343. pUser = ArgValue(argv[i]);
  344. }
  345. else if ( _strnicmp( Arg, FORCE, sizeof(FORCE) -1) == 0 ) {
  346. Flags |= DS_FORCE_REDISCOVERY;
  347. }
  348. else if ( _strnicmp( Arg, DSREQ, sizeof(DSREQ) - 1) == 0 ) {
  349. Flags |= DS_DIRECTORY_SERVICE_REQUIRED;
  350. }
  351. else if ( _strnicmp( Arg, DSPREF, sizeof(DSPREF) -1) == 0 ) {
  352. Flags |= DS_DIRECTORY_SERVICE_PREFERRED;
  353. }
  354. else if ( _strnicmp( Arg, GC, sizeof(GC) -1) == 0 ) {
  355. Flags |= DS_GC_SERVER_REQUIRED;
  356. }
  357. else if ( _strnicmp( Arg, PDC, sizeof(PDC) -1) == 0 ) {
  358. Flags |= DS_PDC_REQUIRED;
  359. }
  360. else if ( _strnicmp( Arg, IP, sizeof(IP) -1) == 0 ) {
  361. Flags |= DS_IP_REQUIRED;
  362. }
  363. else if ( _strnicmp( Arg, KDC, sizeof(KDC) -1) == 0 ) {
  364. Flags |= DS_KDC_REQUIRED;
  365. }
  366. else if ( _strnicmp( Arg, TIME, sizeof(TIME) - 1) == 0 ) {
  367. Flags |= DS_TIMESERV_REQUIRED;
  368. }
  369. else if ( _strnicmp( Arg, WRITE, sizeof(WRITE) - 1) == 0 ) {
  370. Flags |= DS_WRITABLE_REQUIRED;
  371. }
  372. else if ( _strnicmp( Arg, GOODTIME, sizeof(GOODTIME) - 1) == 0 ) {
  373. Flags |= DS_GOOD_TIMESERV_PREFERRED;
  374. }
  375. else if ( _strnicmp( Arg, AVOIDSELF, sizeof(AVOIDSELF)-1) == 0 ) {
  376. Flags |= DS_AVOID_SELF;
  377. }
  378. else if ( _strnicmp( Arg, ONLYLDAP, sizeof(ONLYLDAP) -1 ) == 0 ) {
  379. Flags |= DS_ONLY_LDAP_NEEDED;
  380. }
  381. else if ( _strnicmp( Arg, ISFLATNAME, sizeof(ISFLATNAME)-1) == 0 ) {
  382. Flags |= DS_IS_FLAT_NAME;
  383. }
  384. else if ( _strnicmp( Arg, ISDNSNAME, sizeof(ISDNSNAME) - 1) == 0 ) {
  385. Flags |= DS_IS_DNS_NAME;
  386. }
  387. else if ( _strnicmp( Arg, RETDNS, sizeof(RETDNS) - 1) == 0 ) {
  388. Flags |= DS_RETURN_DNS_NAME;
  389. }
  390. else if ( _strnicmp( Arg, RETFLAT, sizeof(RETFLAT) -1 ) == 0 ) {
  391. Flags |= DS_RETURN_FLAT_NAME;
  392. }
  393. else if ( _strnicmp( Arg, HELP, sizeof(HELP) -1 ) == 0 ) {
  394. Usage(TRUE);
  395. }
  396. }
  397. if (pDomain != NULL)
  398. {
  399. Len = lstrlen(pDomain);
  400. Tmp = (LPTSTR) LocalAlloc(0, Len+1);
  401. strcpy(Tmp, pDomain);
  402. Tmp[Len] = '\0';
  403. pDomain = Tmp;
  404. Tmp = NULL;
  405. }
  406. if (pUser != NULL)
  407. {
  408. Len = lstrlen(pUser);
  409. Tmp = (LPTSTR) LocalAlloc(0, Len+1);
  410. strcpy(Tmp, pUser);
  411. Tmp[Len] = '\0';
  412. pUser = Tmp;
  413. Tmp = NULL;
  414. }
  415. // Call the test function to do the work
  416. test_dsgetdcnamea(pDomain, pUser, Flags);
  417. if (pDomain)
  418. {
  419. LocalFree(pDomain);
  420. pDomain = NULL;
  421. }
  422. if (pUser)
  423. {
  424. LocalFree(pUser);
  425. pUser = NULL;
  426. }
  427. return 0;
  428. }