Click here to Skip to main content
Click here to Skip to main content

Tagged as

Low-level Security Classes

, 1 Mar 2000
Rate this:
Please Sign up or sign in to vote.
A set of classes to encapsulate the Win32 Security APIs
  • Download demo project - 17 Kb
  • Download source files - 4 Kb
  • If you have ever tried to do something simple like set a security descriptor for a Registry key you know what a pain the Win32 Security APIs can be. The process of initializing the various structures, allocating and deallocating memory and testing for error conditions is tedious and you end up with at least a page and a half of code which is difficult to read and doesn't lend itself to being reused next time you want to do the same thing. There had to be a better way, I thought. So I wrote a set of classes which take care of much of the donkey-work for you.

    All of the classes correspond to the structures used by the low-level Security APIs - ACEs, ACLs, SIDs, TRUSTEEs, Security Descriptors. Their member functions are in most cases the same as the calls you would use when working with the API. The crucial differences are:

    1. Often, a single constructor call is all that is needed to allocate and initialize an object
    2. The classes which need to allocate memory will release it safely when the object goes out of scope.

    Other than that, they are just thin wrappers. You can still get at the underlying objects if you need to, and you can also pass them to any of the API functions expecting a security object.

    The best documentation I can give you is some sample code, so here is an example of how to create a secure Registry key which can be read by any user but only changed by administrators. To compare this with code which does the same thing using only API calls, see Setting a Security Descriptor for a new object in the MSDN library. Notice how much longer it is.

    // Create a Registry key, granting KEY_READ access to everyone and 
    // full control to the Administrators group.
    // Initialize 2 EXPLICIT_ACCESS structures: 1 for Everyone,
    // 1 for Administrators. 
    CSid sidEveryone(CSid::WST_EVERYONE);
    CSid sidAdmins(CSid::WST_LOCALADMINS);
    CTrustee trEveryone(TRUSTEE_IS_WELL_KNOWN_GROUP, sidEveryone);
    CTrustee trAdmins(TRUSTEE_IS_GROUP, sidAdmins);
    ea[0] = CExplicitAccess(KEY_READ, SET_ACCESS, NO_INHERITANCE, trEveryone);
    ea[1] = CExplicitAccess(KEY_ALL_ACCESS, SET_ACCESS, NO_INHERITANCE, trAdmins);		

    Note the constructor for CSid which allows you to create a SID for any of the well-known groups such as Administrators, ordinary users, etc.

    // Create a new ACL and set the EA entries in it
    CAcl acl;
    if(acl.SetEntriesInAcl(2, ea) == ERROR_SUCCESS)
    	// Initialize a security descriptor and add our ACL to it  
    	CSecurityDescriptor sd;
    		TRUE,     // fDaclPresent flag   
    		FALSE))   // not a default DACL 
    		// Initialize a security attributes structure.
    		CSecurityAttributes sa(sd, FALSE);
    		// Use the security attributes to set the security descriptor 
    		// when you create a key.
    		DWORD	dwDisposition;
    		HKEY	hKey;
    		RegCreateKeyEx(HKEY_CURRENT_USER, "mykey1", 0, "", 0, 
    			KEY_READ | KEY_WRITE, sa, &hKey, &dwDisposition); 

    That's it! If you have any suggestions or problems to report, post them here. There are plenty of ways this idea could be extended if you are of an inclination to do so.


    This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

    A list of licenses authors might use can be found here


    About the Author

    Peter Kenyon
    Web Developer
    New Zealand New Zealand
    No Biography provided

    Comments and Discussions

    GeneralChanging a directory's security options in Win2000 PinmemberReza Hemmati11-Jul-01 2:50 
    QuestionHow to validate a username and password? PinmemberAlvaro Mendez12-Jan-01 9:51 
    AnswerRe: How to validate a username and password? PinmemberTodd Jeffreys12-Jan-01 12:02 
    GeneralRe: How to validate a username and password? PinmemberAlvaro Mendez12-Jan-01 13:09 
    Thanks for your help! Smile | :)
    I had also posted this question in CodeGuru and was lucky to receive an equally great response. Here's the link to it. I went ahead and implemented the second option. It was a bit of a pain since I had to dig out code from the SockAuth sample, but it works fine (without ever needing to reboot).
    Thanks again,
    GeneralRe: How to validate a username and password? PinmemberAnonymous27-Apr-01 5:30 
    GeneralRe: How to validate a username and password? Pinmemberthe link to codeguru12-Nov-01 3:18 
    GeneralInvalid SID error when creating ACL PinsussJeff Mcaffee28-Sep-00 12:14 
    GeneralRe: Invalid SID error when creating ACL PinsussPeter Kenyon28-Sep-00 13:09 
    GeneralRe: Invalid SID error when creating ACL PinmemberAnonymous28-Dec-00 13:45 

    General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

    Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

    | Advertise | Privacy | Terms of Use | Mobile
    Web04 | 2.8.150326.1 | Last Updated 2 Mar 2000
    Article Copyright 2000 by Peter Kenyon
    Everything else Copyright © CodeProject, 1999-2015
    Layout: fixed | fluid