home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / msdn_vcb / samples / vc98 / sdk / com / dcom / dcomperm / srvcmgmt.cpp < prev    next >
C/C++ Source or Header  |  1996-07-19  |  6KB  |  217 lines

  1. /*++
  2.  
  3. DCOM Permission Configuration Sample
  4. Copyright (c) 1996, Microsoft Corporation. All rights reserved.
  5.  
  6. Module Name:
  7.  
  8.     srvcmgmt.cpp
  9.  
  10. Abstract:
  11.  
  12.     Routines to manage RunAs and Service settings for DCOM servers
  13.  
  14. Author:
  15.  
  16.     Michael Nelson
  17.  
  18. Environment:
  19.  
  20.     Windows NT
  21.  
  22. --*/
  23.  
  24. #include <windows.h>
  25. #include <stdio.h>
  26. #include <conio.h>
  27. #include <tchar.h>
  28. #include "ntsecapi.h"
  29. #include "dcomperm.h"
  30.  
  31. DWORD GetRunAsPassword (
  32.     LPTSTR AppID,
  33.     LPTSTR Password
  34.     )
  35. {
  36.     LSA_OBJECT_ATTRIBUTES objectAttributes;
  37.     HANDLE                policyHandle = NULL;
  38.     LSA_UNICODE_STRING    lsaKeyString;
  39.     PLSA_UNICODE_STRING   lsaPasswordString;
  40.     WCHAR                 key [4 + GUIDSTR_MAX + 1];
  41.     WCHAR                 wideAppID [GUIDSTR_MAX + 1];
  42.     ULONG                 returnValue;
  43.  
  44. #ifndef UNICODE
  45.     STR2UNI (wideAppID, AppID);
  46. #else
  47.     lstrcpy (wideAppID, AppID);
  48. #endif
  49.  
  50.     wcscpy (key, L"SCM:");
  51.     wcscat (key, wideAppID);
  52.  
  53.     lsaKeyString.Length = (USHORT) ((wcslen (key) + 1) * sizeof (WCHAR));
  54.     lsaKeyString.MaximumLength = (GUIDSTR_MAX + 5) * sizeof (WCHAR);
  55.     lsaKeyString.Buffer = key;
  56.  
  57.     //
  58.     // Open the local security policy
  59.     //
  60.  
  61.     memset (&objectAttributes, 0x00, sizeof (LSA_OBJECT_ATTRIBUTES));
  62.     objectAttributes.Length = sizeof (LSA_OBJECT_ATTRIBUTES);
  63.  
  64.     returnValue = LsaOpenPolicy (NULL,
  65.                                  &objectAttributes,
  66.                                  POLICY_GET_PRIVATE_INFORMATION,
  67.                                  &policyHandle);
  68.  
  69.     if (returnValue != ERROR_SUCCESS)
  70.         return returnValue;
  71.  
  72.     //
  73.     // Read the user's password
  74.     //
  75.  
  76.     returnValue = LsaRetrievePrivateData (policyHandle,
  77.                                           &lsaKeyString,
  78.                                           &lsaPasswordString);
  79.  
  80.     if (returnValue != ERROR_SUCCESS)
  81.     {
  82.         LsaClose (policyHandle);
  83.         return returnValue;
  84.     }
  85.  
  86.     LsaClose (policyHandle);
  87.  
  88. #ifndef UNICODE
  89.     UNI2STR (Password, lsaPasswordString->Buffer);
  90. #else
  91.     wcscpy (Password, lsaPasswordString->Buffer);
  92. #endif
  93.  
  94.     return ERROR_SUCCESS;
  95. }
  96.  
  97. DWORD SetRunAsPassword (
  98.     LPTSTR AppID,
  99.     LPTSTR Principal,
  100.     LPTSTR Password
  101.     )
  102. {
  103.     LSA_OBJECT_ATTRIBUTES objectAttributes;
  104.     HANDLE                policyHandle = NULL;
  105.     LSA_UNICODE_STRING    lsaKeyString;
  106.     LSA_UNICODE_STRING    lsaPasswordString;
  107.     WCHAR                 key [4 + GUIDSTR_MAX + 1];
  108.     WCHAR                 wideAppID [GUIDSTR_MAX + 1];
  109.     WCHAR                 widePassword [256];
  110.     DWORD                 returnValue;
  111.  
  112. #ifndef UNICODE
  113.     STR2UNI (wideAppID, AppID);
  114.     STR2UNI (widePassword, Password);
  115. #else
  116.     wcscpy (wideAppID, AppID);
  117.     wcscpy (widePassword, Password);
  118. #endif
  119.  
  120.     wcscpy (key, L"SCM:");
  121.     wcscat (key, wideAppID);
  122.  
  123.     lsaKeyString.Length = (USHORT) ((wcslen (key) + 1) * sizeof (WCHAR));
  124.     lsaKeyString.MaximumLength = (GUIDSTR_MAX + 5) * sizeof (WCHAR);
  125.     lsaKeyString.Buffer = key;
  126.  
  127.     lsaPasswordString.Length = (USHORT) ((wcslen (widePassword) + 1) * sizeof (WCHAR));
  128.     lsaPasswordString.Buffer = widePassword;
  129.     lsaPasswordString.MaximumLength = lsaPasswordString.Length;
  130.  
  131.     //
  132.     // Open the local security policy
  133.     //
  134.  
  135.     memset (&objectAttributes, 0x00, sizeof (LSA_OBJECT_ATTRIBUTES));
  136.     objectAttributes.Length = sizeof (LSA_OBJECT_ATTRIBUTES);
  137.  
  138.     returnValue = LsaOpenPolicy (NULL,
  139.                                  &objectAttributes,
  140.                                  POLICY_CREATE_SECRET,
  141.                                  &policyHandle);
  142.  
  143.     if (returnValue != ERROR_SUCCESS)
  144.         return returnValue;
  145.  
  146.     //
  147.     // Store the user's password
  148.     //
  149.  
  150.     returnValue = LsaStorePrivateData (policyHandle,
  151.                                        &lsaKeyString,
  152.                                        &lsaPasswordString);
  153.  
  154.     if (returnValue != ERROR_SUCCESS)
  155.     {
  156.         LsaClose (policyHandle);
  157.         return returnValue;
  158.     }
  159.  
  160.     LsaClose (policyHandle);
  161.  
  162.     returnValue = SetAccountRights (Principal, TEXT("SeBatchLogonRight"));
  163.     if (returnValue != ERROR_SUCCESS)
  164.         return returnValue;
  165.  
  166.     return ERROR_SUCCESS;
  167. }
  168.  
  169. DWORD
  170. SetAccountRights (
  171.     LPTSTR User,
  172.     LPTSTR Privilege
  173.     )
  174. {
  175.     LSA_HANDLE            policyHandle;
  176.     LSA_OBJECT_ATTRIBUTES objectAttributes;
  177.     PSID                  principalSID;
  178.     LSA_UNICODE_STRING    lsaPrivilegeString;
  179.     WCHAR                 widePrivilege [256];
  180.  
  181. #ifdef _UNICODE
  182.     lstrcpy (widePrivilege, Privilege);
  183. #else
  184.     STR2UNI (widePrivilege, Privilege);
  185. #endif
  186.  
  187.     memset (&objectAttributes, 0, sizeof(LSA_OBJECT_ATTRIBUTES));
  188.     if (LsaOpenPolicy (NULL,
  189.                        &objectAttributes,
  190.                        POLICY_CREATE_ACCOUNT | POLICY_LOOKUP_NAMES,
  191.                        &policyHandle) != ERROR_SUCCESS)
  192.     {
  193.         return GetLastError();
  194.     }
  195.  
  196.     GetPrincipalSID (User, &principalSID);
  197.  
  198.     lsaPrivilegeString.Length = (USHORT) (wcslen (widePrivilege) * sizeof (WCHAR));
  199.     lsaPrivilegeString.MaximumLength = (USHORT) (lsaPrivilegeString.Length + sizeof (WCHAR));
  200.     lsaPrivilegeString.Buffer = widePrivilege;
  201.  
  202.     if (LsaAddAccountRights (policyHandle,
  203.                              principalSID,
  204.                              &lsaPrivilegeString,
  205.                              1) != ERROR_SUCCESS)
  206.     {
  207.         free (principalSID);
  208.         LsaClose (policyHandle);
  209.         return GetLastError();
  210.     }
  211.  
  212.     free (principalSID);
  213.     LsaClose (policyHandle);
  214.  
  215.     return ERROR_SUCCESS;
  216. }
  217.