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.

346 lines
8.4 KiB

  1. #include <windows.h>
  2. #include <tchar.h>
  3. #include <stdio.h>
  4. TCHAR szADT[MAX_PATH];
  5. BOOL ParseCommandLine (int argc, char *argv[]);
  6. UINT CreateNestedDirectory(LPCTSTR lpDirectory);
  7. LPTSTR CheckSlash (LPTSTR lpDir);
  8. int __cdecl main( int argc, char *argv[])
  9. {
  10. LPTSTR lpEnd, lpRoot;
  11. HANDLE hFile;
  12. INT i;
  13. if (!ParseCommandLine (argc, argv)) {
  14. return 1;
  15. }
  16. if (!CreateNestedDirectory (szADT)) {
  17. return 1;
  18. }
  19. lpRoot = CheckSlash (szADT);
  20. lstrcpy (lpRoot, TEXT("GPT.ini"));
  21. if (!WritePrivateProfileString (TEXT("General"),
  22. TEXT("GUID"),
  23. TEXT("<GUID goes here>"),
  24. szADT)) {
  25. return 1;
  26. }
  27. if (!WritePrivateProfileString (TEXT("General"),
  28. TEXT("Class Store"),
  29. TEXT(" <Path to a Class Store goes here>"),
  30. szADT)) {
  31. return 1;
  32. }
  33. *(lpRoot - 1) = TEXT('\0');
  34. for (i=0; i < 2; i++ ) {
  35. lpRoot = CheckSlash (szADT);
  36. if (i == 0) {
  37. lstrcpy (lpRoot, TEXT("User"));
  38. } else {
  39. lstrcpy (lpRoot, TEXT("Machine"));
  40. }
  41. lpEnd = CheckSlash (szADT);
  42. lstrcpy (lpEnd, TEXT("Applications\\Assigned\\Alpha"));
  43. if (!CreateNestedDirectory (szADT)) {
  44. return 1;
  45. }
  46. lstrcpy (lpEnd, TEXT("Applications\\Assigned\\x86\\WinNT"));
  47. if (!CreateNestedDirectory (szADT)) {
  48. return 1;
  49. }
  50. lstrcpy (lpEnd, TEXT("Applications\\Assigned\\x86\\Win95"));
  51. if (!CreateNestedDirectory (szADT)) {
  52. return 1;
  53. }
  54. lstrcpy (lpEnd, TEXT("Applications\\Published\\Alpha"));
  55. if (!CreateNestedDirectory (szADT)) {
  56. return 1;
  57. }
  58. lstrcpy (lpEnd, TEXT("Applications\\Published\\x86\\WinNT"));
  59. if (!CreateNestedDirectory (szADT)) {
  60. return 1;
  61. }
  62. lstrcpy (lpEnd, TEXT("Applications\\Published\\x86\\Win95"));
  63. if (!CreateNestedDirectory (szADT)) {
  64. return 1;
  65. }
  66. if (i > 0) {
  67. lstrcpy (lpEnd, TEXT("OS Upgrades"));
  68. if (!CreateNestedDirectory (szADT)) {
  69. return 1;
  70. }
  71. }
  72. lstrcpy (lpEnd, TEXT("Profile"));
  73. if (!CreateNestedDirectory (szADT)) {
  74. return 1;
  75. }
  76. lstrcpy (lpEnd, TEXT("Power Schemes"));
  77. if (!CreateNestedDirectory (szADT)) {
  78. return 1;
  79. }
  80. lstrcpy (lpEnd, TEXT("Scripts"));
  81. if (!CreateNestedDirectory (szADT)) {
  82. return 1;
  83. }
  84. lstrcpy (lpEnd, TEXT("Registry.txt"));
  85. hFile = CreateFile (szADT, GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE,
  86. NULL, CREATE_NEW, FILE_ATTRIBUTE_NORMAL, NULL);
  87. if (hFile != INVALID_HANDLE_VALUE) {
  88. CloseHandle (hFile);
  89. }
  90. lstrcpy (lpEnd, TEXT("Profile"));
  91. lpEnd = CheckSlash (szADT);
  92. lstrcpy (lpEnd, TEXT("Application Data"));
  93. if (!CreateNestedDirectory (szADT)) {
  94. return 1;
  95. }
  96. lstrcpy (lpEnd, TEXT("Desktop\\My Documents"));
  97. if (!CreateNestedDirectory (szADT)) {
  98. return 1;
  99. }
  100. lstrcpy (lpEnd, TEXT("Favorites"));
  101. if (!CreateNestedDirectory (szADT)) {
  102. return 1;
  103. }
  104. lstrcpy (lpEnd, TEXT("NetHood"));
  105. if (!CreateNestedDirectory (szADT)) {
  106. return 1;
  107. }
  108. lstrcpy (lpEnd, TEXT("PrintHood"));
  109. if (!CreateNestedDirectory (szADT)) {
  110. return 1;
  111. }
  112. lstrcpy (lpEnd, TEXT("Recent"));
  113. if (!CreateNestedDirectory (szADT)) {
  114. return 1;
  115. }
  116. lstrcpy (lpEnd, TEXT("SendTo"));
  117. if (!CreateNestedDirectory (szADT)) {
  118. return 1;
  119. }
  120. lstrcpy (lpEnd, TEXT("Templates"));
  121. if (!CreateNestedDirectory (szADT)) {
  122. return 1;
  123. }
  124. lstrcpy (lpEnd, TEXT("Start Menu\\Programs\\Startup"));
  125. if (!CreateNestedDirectory (szADT)) {
  126. return 1;
  127. }
  128. if (i == 0) {
  129. lstrcpy (lpRoot, TEXT("User\\Scripts\\Scripts.ini"));
  130. } else {
  131. lstrcpy (lpRoot, TEXT("Machine\\Scripts\\Scripts.ini"));
  132. }
  133. if (i == 0) {
  134. if (!WritePrivateProfileString (TEXT("Logon"),
  135. TEXT("CommandLine"),
  136. TEXT(" <Command line goes here>"),
  137. szADT)) {
  138. return 1;
  139. }
  140. if (!WritePrivateProfileString (TEXT("Logoff"),
  141. TEXT("CommandLine"),
  142. TEXT(" <Command line goes here>"),
  143. szADT)) {
  144. return 1;
  145. }
  146. } else {
  147. if (!WritePrivateProfileString (TEXT("Boot"),
  148. TEXT("CommandLine"),
  149. TEXT(" <Command line goes here>"),
  150. szADT)) {
  151. return 1;
  152. }
  153. if (!WritePrivateProfileString (TEXT("Shutdown"),
  154. TEXT("CommandLine"),
  155. TEXT(" <Command line goes here>"),
  156. szADT)) {
  157. return 1;
  158. }
  159. }
  160. *(lpRoot - 1) = TEXT('\0');
  161. }
  162. _tprintf (TEXT("\r\nThe %s Group Policy Template (GPT) was successfully created.\r\n\r\n"), szADT);
  163. _tprintf (TEXT("Add this directory to the %%SystemRoot%%\\DS.INI file on each client machine.\r\n"));
  164. _tprintf (TEXT("The Path entry can have multiple ADT paths each separated by a semi-colon.\r\n"));
  165. _tprintf (TEXT("The file format is:\r\n\r\n"));
  166. _tprintf (TEXT("[User ADT]\r\nPath=%s\r\n\r\n"), szADT);
  167. return 0;
  168. }
  169. BOOL ParseCommandLine (int argc, char *argv[])
  170. {
  171. if (argc != 2) {
  172. goto usage;
  173. }
  174. if (!lstrcmpi(argv[1], TEXT("/?"))) {
  175. goto usage;
  176. }
  177. if (!lstrcmpi(argv[1], TEXT("-?"))) {
  178. goto usage;
  179. }
  180. lstrcpy (szADT, argv[1]);
  181. return TRUE;
  182. usage:
  183. _tprintf (TEXT("\r\nusage: creategpt <gptpath>\r\n\r\n"));
  184. return FALSE;
  185. }
  186. UINT CreateNestedDirectory(LPCTSTR lpDirectory)
  187. {
  188. TCHAR szDirectory[MAX_PATH];
  189. LPTSTR lpEnd;
  190. if (CreateDirectory (lpDirectory, NULL)) {
  191. return 1;
  192. }
  193. if (GetLastError() == ERROR_ALREADY_EXISTS) {
  194. return ERROR_ALREADY_EXISTS;
  195. }
  196. lstrcpy (szDirectory, lpDirectory);
  197. lpEnd = szDirectory;
  198. if (szDirectory[1] == TEXT(':')) {
  199. lpEnd += 3;
  200. } else if (szDirectory[1] == TEXT('\\')) {
  201. lpEnd += 2;
  202. while (*lpEnd && *lpEnd != TEXT('\\')) {
  203. lpEnd++;
  204. }
  205. if (!(*lpEnd)) {
  206. return 0;
  207. }
  208. lpEnd++;
  209. while (*lpEnd && *lpEnd != TEXT('\\')) {
  210. lpEnd++;
  211. }
  212. if (!(*lpEnd)) {
  213. return 0;
  214. }
  215. lpEnd++;
  216. } else if (szDirectory[0] == TEXT('\\')) {
  217. lpEnd++;
  218. }
  219. while (*lpEnd) {
  220. while (*lpEnd && *lpEnd != TEXT('\\')) {
  221. lpEnd++;
  222. }
  223. if (*lpEnd == TEXT('\\')) {
  224. *lpEnd = TEXT('\0');
  225. if (!CreateDirectory (szDirectory, NULL)) {
  226. if (GetLastError() != ERROR_ALREADY_EXISTS) {
  227. return 0;
  228. }
  229. }
  230. *lpEnd = TEXT('\\');
  231. lpEnd++;
  232. }
  233. }
  234. if (CreateDirectory (szDirectory, NULL)) {
  235. return 1;
  236. }
  237. if (GetLastError() == ERROR_ALREADY_EXISTS) {
  238. return ERROR_ALREADY_EXISTS;
  239. }
  240. return 0;
  241. }
  242. LPTSTR CheckSlash (LPTSTR lpDir)
  243. {
  244. DWORD dwStrLen;
  245. LPTSTR lpEnd;
  246. lpEnd = lpDir + lstrlen(lpDir);
  247. if (*(lpEnd - 1) != TEXT('\\')) {
  248. *lpEnd = TEXT('\\');
  249. lpEnd++;
  250. *lpEnd = TEXT('\0');
  251. }
  252. return lpEnd;
  253. }