Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

This all started because I need to check if user accounts are disabled or not (Along with many other things) The answer is in the FLAGS field of the hash retunred from NetUserEnum but, I can't figure out how to break it all out of the dword. My question is, what is the method to break out the readable format of the FLAGS field? Thanks much for the help, David Ross

Replies are listed 'Best First'.
Re: Win32::Lanman::NetUserEnum ... FLAGS
by BrowserUk (Patriarch) on Nov 06, 2003 at 21:17 UTC

    I find that manipulating the bitfield constants exported by many modules including Win32::Lanman to be APITA. It would be much easier if you could iterate through the bit fields. There is mention in the docs of the same constants being exported as a hash %NET_STATUS_DESCRIPTION, but it also says that it is a work in progress and I couldn't get it to work.

    A while ago, I came up with this method of iterating the constants I want from a module, provided they can be selected with a regex pattern.

    #! perl -slw use strict; use Win32::Lanman; Win32::Lanman::NetUserEnum( 'HIAWATHA', 0, \my @users ) or die $^E; for my $user ( @users) { print $user->{name}; printf "%35s : %d\n", $_, !!( $user->{flags} & do{ no strict 'refs'; &$_ } ) for grep{ /^UF_/ } keys %Win32::Lanman::; } __END__ Fred1 UF_SETTABLE_BITS : 1 UF_SMARTCARD_REQUIRED : 0 UF_DONT_REQUIRE_PREAUTH : 0 UF_NORMAL_ACCOUNT : 1 UF_LOCKOUT : 0 UF_HOMEDIR_REQUIRED : 0 UF_SERVER_TRUST_ACCOUNT : 0 UF_PASSWD_CANT_CHANGE : 1 UF_ENCRYPTED_TEXT_PASSWORD_ALLOWED : 0 UF_NOT_DELEGATED : 0 UF_USE_DES_KEY_ONLY : 0 UF_WORKSTATION_TRUST_ACCOUNT : 0 UF_PASSWD_NOTREQD : 0 UF_MACHINE_ACCOUNT_MASK : 0 UF_TRUSTED_FOR_DELEGATION : 0 UF_ACCOUNT_TYPE_MASK : 1 UF_INTERDOMAIN_TRUST_ACCOUNT : 0 UF_MNS_LOGON_ACCOUNT : 0 UF_TEMP_DUPLICATE_ACCOUNT : 0 UF_ACCOUNTDISABLE : 1 UF_SCRIPT : 1 UF_DONT_EXPIRE_PASSWD : 1

    The value you posted below, 66049, translates to

    UF_SETTABLE_BITS : 1 UF_SMARTCARD_REQUIRED : 0 UF_DONT_REQUIRE_PREAUTH : 0 UF_NORMAL_ACCOUNT : 1 UF_LOCKOUT : 0 UF_HOMEDIR_REQUIRED : 0 UF_SERVER_TRUST_ACCOUNT : 0 UF_PASSWD_CANT_CHANGE : 0 UF_ENCRYPTED_TEXT_PASSWORD_ALLOWED : 0 UF_NOT_DELEGATED : 0 UF_USE_DES_KEY_ONLY : 0 UF_WORKSTATION_TRUST_ACCOUNT : 0 UF_PASSWD_NOTREQD : 0 UF_MACHINE_ACCOUNT_MASK : 0 UF_TRUSTED_FOR_DELEGATION : 0 UF_ACCOUNT_TYPE_MASK : 1 UF_INTERDOMAIN_TRUST_ACCOUNT : 0 UF_MNS_LOGON_ACCOUNT : 0 UF_TEMP_DUPLICATE_ACCOUNT : 0 UF_ACCOUNTDISABLE : 0 UF_SCRIPT : 1 UF_DONT_EXPIRE_PASSWD : 1

    As you can see, the UF_ACCOUNT_DISABLED flag is not set in that mask. Maybe you will find that useful.


    Examine what is said, not who speaks.
    "Efficiency is intelligent laziness." -David Dunham
    "Think for yourself!" - Abigail
    Hooray!
    Wanted!

Re: Win32::Lanman::NetUserEnum ... FLAGS
by rchiav (Deacon) on Nov 06, 2003 at 19:09 UTC
    The flags field is a bitmask. In order to see if a setting is flagged, you need to "and" the appropriate value against the flags value.

    For example, if

    ${$user}{'flags'} & UF_ACCOUNTDISABLE
    is true, then the Account Dsabled flag is set. Look through the POD for the module for all the UF_ constants. Those will be the flags you're going to "and" against.
      Thanks for the reply

      Can you think of any reason why this woudlnt work on a Win2K box?? I tried:
       print "Disabled" if ($info{'flags'} & UF_ACCOUNTDISABLE);
      But get nothing. The rest of the values in %info are there tho and if I just print $info{'flags'}; I get 66049
      Thanks in advance,
      David
        Are you sure that the account is disabled?

        From the sounds of it you might not know what I meant by "and"ing. In case you're a little confused about that, here's how it works.

        That number you see isn't used as a decimal number as you typed it. It's actually used as a binary number. Each binary digit is used as either ON(1) or OFF(0). Now each of those binary places can also be represented as a decimal number.

        00000001=1
        00000010=2
        00000100=4
        00001000=8
        00010000=16
        00100000=32
        etc..

        So... all of these numbers are represented as the UF_ constants. When you AND, you're checking if the bit represented in the UF_ variable is on.

        If, for the account you're checking, the account is disabled, I'm guessing you have a problem someplace else.

        Your other option is to use WMI. You can invoke it using Win32::OLE . Google for Win32_UserAccount and you should find some good information.