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.

380 lines
6.7 KiB

  1. //+--------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. // Copyright (C) Microsoft Corporation, 1998
  5. //
  6. // File: Scrpdata.cpp
  7. //
  8. // Contents:
  9. //
  10. // History: 9-Aug-99 NishadM Created
  11. //
  12. //---------------------------------------------------------------------------
  13. #include "gptext.h"
  14. #include "scrpdata.h"
  15. LPCWSTR g_pwszScriptTypes[] =
  16. {
  17. TEXT("Undefined"),
  18. LOGON_VALUE,
  19. LOGOFF_VALUE,
  20. STARTUP_VALUE,
  21. SHUTDOWN_VALUE,
  22. };
  23. PRSOP_ScriptList
  24. CreateScriptList( ScriptType type )
  25. {
  26. PRSOP_ScriptList pList = (PRSOP_ScriptList) LocalAlloc( LMEM_ZEROINIT, sizeof( RSOP_ScriptList ) );
  27. if ( pList )
  28. {
  29. SetScriptType( pList, type );
  30. }
  31. return pList;
  32. }
  33. RSOPScriptList
  34. CreateScriptListOfStr( LPCWSTR szScriptType )
  35. {
  36. ScriptType type = Undefined;
  37. RSOPScriptList pList = 0;
  38. if ( szScriptType )
  39. {
  40. if ( !lstrcmpi( szScriptType, LOGON_VALUE ) )
  41. {
  42. type = Logon;
  43. }
  44. else if ( !lstrcmpi( szScriptType, LOGOFF_VALUE ) )
  45. {
  46. type = Logoff;
  47. }
  48. else if ( !lstrcmpi( szScriptType, STARTUP_VALUE ) )
  49. {
  50. type = Startup;
  51. }
  52. else if ( !lstrcmpi( szScriptType, SHUTDOWN_VALUE ) )
  53. {
  54. type = Shutdown;
  55. }
  56. if ( type != Undefined )
  57. {
  58. pList = (RSOPScriptList) CreateScriptList( type );
  59. }
  60. }
  61. return pList;
  62. }
  63. void
  64. DestroyScriptList( RSOPScriptList pList )
  65. {
  66. if ( pList )
  67. {
  68. PRSOP_Script pCommand = ((PRSOP_ScriptList)pList)->scriptCommand;
  69. while ( pCommand )
  70. {
  71. PRSOP_Script pTemp = pCommand->pNextCommand;
  72. //
  73. // destroy the script command and params
  74. //
  75. if ( pCommand->szCommand )
  76. {
  77. LocalFree( pCommand->szCommand );
  78. }
  79. if ( pCommand->szParams )
  80. {
  81. LocalFree( pCommand->szParams );
  82. }
  83. pCommand = pTemp;
  84. }
  85. //
  86. // destroy the list
  87. //
  88. LocalFree( pList );
  89. }
  90. }
  91. BOOL
  92. AddScript( RSOPScriptList pList, LPCWSTR szCommand, LPCWSTR szParams, SYSTEMTIME* pExecTime )
  93. {
  94. PRSOP_Script pCommand = 0;
  95. if ( pList )
  96. {
  97. //
  98. // Alloc the script
  99. //
  100. pCommand = (PRSOP_Script) LocalAlloc( LMEM_ZEROINIT, sizeof( RSOP_Script ) );
  101. if ( pCommand )
  102. {
  103. //
  104. // Alloc the command and params
  105. //
  106. pCommand->szCommand = (LPTSTR) LocalAlloc( 0, ( wcslen( szCommand ) + 1 ) * sizeof( WCHAR ) );
  107. pCommand->szParams = (LPTSTR) LocalAlloc( 0, ( wcslen( szParams ) + 1 ) * sizeof( WCHAR ) );
  108. if ( pCommand->szParams && pCommand->szCommand )
  109. {
  110. //
  111. // deep copy
  112. //
  113. wcscpy( pCommand->szCommand, szCommand );
  114. wcscpy( pCommand->szParams, szParams );
  115. memcpy( &pCommand->executionTime, pExecTime, sizeof(SYSTEMTIME));
  116. //
  117. // build the list
  118. //
  119. if ( ((PRSOP_ScriptList)pList)->listTail )
  120. {
  121. ((PRSOP_ScriptList)pList)->listTail->pNextCommand = pCommand;
  122. }
  123. if ( !((PRSOP_ScriptList)pList)->scriptCommand )
  124. {
  125. //
  126. // first script command
  127. //
  128. ((PRSOP_ScriptList)pList)->scriptCommand = pCommand;
  129. ((PRSOP_ScriptList)pList)->listTail = pCommand;
  130. }
  131. ((PRSOP_ScriptList)pList)->listTail = pCommand;
  132. }
  133. else
  134. {
  135. //
  136. // cleanup
  137. //
  138. if ( pCommand->szCommand )
  139. {
  140. LocalFree( pCommand->szCommand );
  141. }
  142. if ( pCommand->szParams )
  143. {
  144. LocalFree( pCommand->szParams );
  145. }
  146. LocalFree( pCommand );
  147. pCommand = 0;
  148. }
  149. }
  150. }
  151. if ( pCommand )
  152. {
  153. //
  154. // bump command count
  155. //
  156. ((PRSOP_ScriptList)pList)->nCommand++;
  157. return TRUE;
  158. }
  159. else
  160. {
  161. return FALSE;;
  162. }
  163. }
  164. ScriptType
  165. GetScriptType( PRSOP_ScriptList pList )
  166. {
  167. //
  168. // accessor
  169. //
  170. if ( pList )
  171. {
  172. return pList->type;
  173. }
  174. return Undefined;
  175. }
  176. void
  177. SetScriptType( PRSOP_ScriptList pList, ScriptType type )
  178. {
  179. //
  180. // accessor
  181. //
  182. if ( pList )
  183. {
  184. pList->type = type;
  185. }
  186. }
  187. ULONG
  188. GetScriptCount( PRSOP_ScriptList pList )
  189. {
  190. //
  191. // accessor
  192. //
  193. if ( pList )
  194. {
  195. return pList->nCommand;
  196. }
  197. return 0;
  198. }
  199. void
  200. GetFirstScript( PRSOP_ScriptList pList, void** pHandle, LPCWSTR* pszCommand, LPCWSTR* pszParams, SYSTEMTIME** pExecTime )
  201. {
  202. //
  203. // sanity check
  204. //
  205. if ( pList )
  206. {
  207. if ( pHandle )
  208. {
  209. //
  210. // first command of the list
  211. //
  212. *pHandle = ( void* ) pList->scriptCommand;
  213. }
  214. if ( pList->scriptCommand )
  215. {
  216. //
  217. // accessor
  218. // list owns the memory
  219. //
  220. if ( pszCommand )
  221. {
  222. *pszCommand = pList->scriptCommand->szCommand;
  223. }
  224. if ( pszParams )
  225. {
  226. *pszParams = pList->scriptCommand->szParams;
  227. }
  228. if ( pExecTime )
  229. {
  230. *pExecTime = &pList->scriptCommand->executionTime;
  231. }
  232. }
  233. }
  234. }
  235. void
  236. GetNextScript( PRSOP_ScriptList pList, void** pHandle, LPCWSTR* pszCommand, LPCWSTR* pszParams, SYSTEMTIME** pExecTime )
  237. {
  238. //
  239. // sanity checks
  240. //
  241. if ( pList )
  242. {
  243. if ( pHandle && *pHandle )
  244. {
  245. //
  246. // context marker
  247. //
  248. PRSOP_Script pCommand = ( PRSOP_Script ) *pHandle;
  249. pCommand = pCommand->pNextCommand;
  250. if ( pCommand )
  251. {
  252. //
  253. // accessor
  254. // list owns the memory
  255. //
  256. if ( pszCommand )
  257. {
  258. *pszCommand = pCommand->szCommand;
  259. }
  260. if ( pszParams )
  261. {
  262. *pszParams = pCommand->szParams;
  263. }
  264. if ( pExecTime )
  265. {
  266. *pExecTime = &pCommand->executionTime;
  267. }
  268. }
  269. *pHandle = pCommand;
  270. }
  271. }
  272. }
  273. //
  274. // extracts {08D5A77B-E6E3-4C2B-A952-A2BA4C3AFE63} from
  275. // \\domain\sysvol\domain-dns-path\Policies\{08D5A77B-E6E3-4C2B-A952-A2BA4C3AFE63}\User\Scripts\Scripts.ini
  276. // allocates and returns string
  277. //
  278. LPWSTR
  279. GPOIDFromPath( LPCWSTR wszPath )
  280. {
  281. LPWSTR wszGpoId = 0;
  282. while ( *wszPath && *(wszPath+1) )
  283. {
  284. if ( *wszPath == L'\\' && *(wszPath+1) == L'{' )
  285. {
  286. LPCWSTR wszTemp = ++wszPath;
  287. while ( *wszTemp && *wszTemp != L'}' )
  288. {
  289. wszTemp++;
  290. }
  291. if ( *wszTemp && ( wszTemp - wszPath ) == 37 )
  292. {
  293. //
  294. // wcslen("{08D5A77B-E6E3-4C2B-A952-A2BA4C3AFE63}") == 39
  295. // including terminating character.
  296. //
  297. wszGpoId = (LPWSTR) LocalAlloc( 0, 40 * sizeof( WCHAR ) );
  298. if ( wszGpoId )
  299. {
  300. wcsncpy( wszGpoId, wszPath, 38 );
  301. wszGpoId[38] = 0;
  302. }
  303. }
  304. break;
  305. }
  306. wszPath++;
  307. }
  308. if (!wszGpoId && !(*(wszPath+1)))
  309. {
  310. wszGpoId = (LPWSTR) LocalAlloc(LPTR, 19 * sizeof( WCHAR ) );
  311. if ( wszGpoId )
  312. {
  313. lstrcpyW (wszGpoId, L"Local Group Policy");
  314. }
  315. }
  316. return wszGpoId;
  317. }