00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #include <windows.h>
00025 #include <stdio.h>
00026 #include <stdlib.h>
00027 #include <conio.h>
00028 #include <tchar.h>
00029 #include <malloc.h>
00030 #include "ntsecapi.h"
00031 #include "dcomperm.h"
00032
00033 DWORD GetRunAsPassword (
00034 LPTSTR AppID,
00035 LPTSTR Password
00036 )
00037 {
00038 LSA_OBJECT_ATTRIBUTES objectAttributes;
00039 HANDLE policyHandle = NIL;
00040 LSA_UNICODE_STRING lsaKeyString;
00041 PLSA_UNICODE_STRING lsaPasswordString = NIL;
00042 WCHAR key [4 + GUIDSTR_MAX + 1];
00043 WCHAR wideAppID [GUIDSTR_MAX + 1];
00044 ULONG returnValue = 0;
00045
00046 #ifndef UNICODE
00047 STR2UNI (wideAppID, AppID);
00048 #else
00049 lstrcpy (wideAppID, AppID);
00050 #endif
00051
00052 wcscpy (key, L"SCM:");
00053 wcscat (key, wideAppID);
00054
00055 lsaKeyString.Length = (USHORT) ((wcslen (key) + 1) * sizeof (WCHAR));
00056 lsaKeyString.MaximumLength = (GUIDSTR_MAX + 5) * sizeof (WCHAR);
00057 lsaKeyString.Buffer = key;
00058
00059
00060
00061
00062
00063 memset (&objectAttributes, 0x00, sizeof (LSA_OBJECT_ATTRIBUTES));
00064 objectAttributes.Length = sizeof (LSA_OBJECT_ATTRIBUTES);
00065
00066 returnValue = LsaOpenPolicy (NULL,
00067 &objectAttributes,
00068 POLICY_GET_PRIVATE_INFORMATION,
00069 &policyHandle);
00070
00071 if (returnValue != ERROR_SUCCESS)
00072 return returnValue;
00073
00074
00075
00076
00077
00078 returnValue = LsaRetrievePrivateData (policyHandle,
00079 &lsaKeyString,
00080 &lsaPasswordString);
00081
00082 if (returnValue != ERROR_SUCCESS)
00083 {
00084 LsaClose (policyHandle);
00085 return returnValue;
00086 }
00087
00088 LsaClose (policyHandle);
00089
00090 #ifndef UNICODE
00091 UNI2STR (Password, lsaPasswordString->Buffer);
00092 #else
00093 wcscpy (Password, lsaPasswordString->Buffer);
00094 #endif
00095
00096 return ERROR_SUCCESS;
00097 }
00098
00099 DWORD SetRunAsPassword (
00100 LPTSTR AppID,
00101 LPTSTR Principal,
00102 LPTSTR Password
00103 )
00104 {
00105 LSA_OBJECT_ATTRIBUTES objectAttributes;
00106 HANDLE policyHandle = NULL;
00107 LSA_UNICODE_STRING lsaKeyString;
00108 LSA_UNICODE_STRING lsaPasswordString;
00109 WCHAR key [4 + GUIDSTR_MAX + 1];
00110 WCHAR wideAppID [GUIDSTR_MAX + 1];
00111 WCHAR widePassword [256];
00112 DWORD returnValue = 0;
00113
00114 #ifndef UNICODE
00115 STR2UNI (wideAppID, AppID);
00116 STR2UNI (widePassword, Password);
00117 #else
00118 wcscpy (wideAppID, AppID);
00119 wcscpy (widePassword, Password);
00120 #endif
00121
00122 wcscpy (key, L"SCM:");
00123 wcscat (key, wideAppID);
00124
00125 lsaKeyString.Length = (USHORT) ((wcslen (key) + 1) * sizeof (WCHAR));
00126 lsaKeyString.MaximumLength = (GUIDSTR_MAX + 5) * sizeof (WCHAR);
00127 lsaKeyString.Buffer = key;
00128
00129 lsaPasswordString.Length = (USHORT) ((wcslen (widePassword) + 1) * sizeof (WCHAR));
00130 lsaPasswordString.Buffer = widePassword;
00131 lsaPasswordString.MaximumLength = lsaPasswordString.Length;
00132
00133
00134
00135
00136
00137 memset (&objectAttributes, 0x00, sizeof (LSA_OBJECT_ATTRIBUTES));
00138 objectAttributes.Length = sizeof (LSA_OBJECT_ATTRIBUTES);
00139
00140 returnValue = LsaOpenPolicy (NULL,
00141 &objectAttributes,
00142 POLICY_CREATE_SECRET,
00143 &policyHandle);
00144
00145 if (returnValue != ERROR_SUCCESS)
00146 return returnValue;
00147
00148
00149
00150
00151
00152 returnValue = LsaStorePrivateData (policyHandle,
00153 &lsaKeyString,
00154 &lsaPasswordString);
00155
00156 if (returnValue != ERROR_SUCCESS)
00157 {
00158 LsaClose (policyHandle);
00159 return returnValue;
00160 }
00161
00162 LsaClose (policyHandle);
00163
00164 returnValue = SetAccountRights (Principal, TEXT("SeBatchLogonRight"));
00165 if (returnValue != ERROR_SUCCESS)
00166 return returnValue;
00167
00168 return ERROR_SUCCESS;
00169 }
00170
00171 DWORD
00172 SetAccountRights (
00173 LPTSTR User,
00174 LPTSTR Privilege
00175 )
00176 {
00177 LSA_HANDLE policyHandle = NULL;
00178 LSA_OBJECT_ATTRIBUTES objectAttributes;
00179 PSID principalSID = NULL;
00180 LSA_UNICODE_STRING lsaPrivilegeString;
00181 WCHAR widePrivilege [256];
00182
00183 #ifdef _UNICODE
00184 lstrcpy (widePrivilege, Privilege);
00185 #else
00186 STR2UNI (widePrivilege, Privilege);
00187 #endif
00188
00189 memset (&objectAttributes, 0, sizeof(LSA_OBJECT_ATTRIBUTES));
00190 if (LsaOpenPolicy (NULL,
00191 &objectAttributes,
00192 POLICY_CREATE_ACCOUNT | POLICY_LOOKUP_NAMES,
00193 &policyHandle) != ERROR_SUCCESS)
00194 {
00195 return GetLastError();
00196 }
00197
00198 GetPrincipalSID (User, &principalSID);
00199
00200 lsaPrivilegeString.Length = (USHORT) (wcslen (widePrivilege) * sizeof (WCHAR));
00201 lsaPrivilegeString.MaximumLength = (USHORT) (lsaPrivilegeString.Length + sizeof (WCHAR));
00202 lsaPrivilegeString.Buffer = widePrivilege;
00203
00204 if (LsaAddAccountRights (policyHandle,
00205 principalSID,
00206 &lsaPrivilegeString,
00207 1) != ERROR_SUCCESS)
00208 {
00209 if (principalSID) free (principalSID);
00210 LsaClose (policyHandle);
00211 return GetLastError();
00212 }
00213
00214 free (principalSID);
00215 LsaClose (policyHandle);
00216
00217 return ERROR_SUCCESS;
00218 }
00219