macOS opendirectory : extract all domains from active directory ODNode object

Viewed 169

I'd like to obtain all available network domains which are bound to my active directory server.

from command-line I use odutil show nodenames which retrieves :

Name                                          State  Refs Type         External Locked Hidden
--------------------------------------------- ------ ---- ------------ -------- ------ ------
/Active Directory                             Online 3                          X      X
/Active Directory/TEST                               10   
/Active Directory/TEST/All Domains            Online 4    Virtual node
/Active Directory/TEST/Global Catalog         Online 6    Virtual node                 X
/Active Directory/TEST/domain.com             Online 6    Virtual node                 X
/Configure                                    Online 2                          X      X
/Contacts                                     Online 3
/LDAPv3                                       Online 1                          X      X
/Local                                        Online 2                          X      X
/Local/Default                                Online 36                         X
/Search                                       Online 19

I only care about the Active directory based nodes since I'd like to list all domains attached to those nodes only.

in the case above both /Active Directory/myserver/Global Catalog and /Active Directory/myserver/domain.com are hidden.

unfortunately, I couldn't find a way to get nodes which are marked as hidden from the command-line output. Perhaps anybody knows how ?

My Code to iterate those nodes is :

    ODNode * node = [ODNode nodeWithSession:[ODSession defaultSession] 
                                       type:kODNodeTypeAuthentication error:nil];
    NSArray * subnodes = (__bridge NSArray *)ODNodeCopySubnodeNames((__bridge ODNodeRef)node, NULL);

which retrieved only the non-hidden nodes : /Active Directory/myserver/All Domains

EDIT :

in windows there's a dedicated command extracting domain for local computer :

GetComputerNameExW(ComputerNameDnsDomain, domainName, &dwSize))

I wish to find the alternative for macOS.

1 Answers

Perhaps it help anyone, I've found a way to extract connected domain without depending on assigned users by simply iterating the subnode, identify the subnode which refers to remote active-directory service, and extract the domain name out of the node's details :

    NSError *err;
    ODNode * node = [ODNode nodeWithSession:[ODSession defaultSession] 
                                       type:kODNodeTypeAuthentication error:&err];

    if (err) {
        NSLog(@"couldn't fetch parent node, reason = @%", [err description]);
        return;
    }

    NSArray * subnodeNames = [node subnodeNamesAndReturnError:&err];
    if (err) {
       NSLog(@"couldn't fetach subnodeNames. reason = %s", [err description]);
        return;
    }

    
    for (NSString * subnodeName in subnodeNames) {
        ODNode * subnodeObject = [ODNode nodeWithSession:[ODSession defaultSession]
                                                    name:subnodeName
                                                   error:&err];
        if (err) {
           NSLog(@"couldn't fetch ODNode for @%. reason: @%",
                     subnodeName, [err description]);
            continue;
        }
        
        id subnodeDetails = [subnodeObject nodeDetailsForKeys:nil error:&err];
        if (err) {
           NSLog(@"couldn't fetch ODNode details for %@. reason: %@",
                     subnodeName, [err description]);
            continue;
        }

        NSArray * domainName = subnodeDetails[@"dsAttrTypeNative:DomainName"];
        if (domainName) {
            NSLog(@"found default domain = %@", [domainName lastObject]);
            return;
        }
    }
Related