Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Net::LDAP GUID...

by muffinman (Initiate)
on Apr 06, 2011 at 20:26 UTC ( [id://897883]=perlquestion: print w/replies, xml ) Need Help??

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

Hello

I'm talking to active directory through Net::LDAP, and I want to extract user GUIDs in a readable form.

If I query a user, I don't get a GUID field, but instead a nasty contraption called objectGUID. This, I believe is supposed to be a 128bit binary field. In a debugger, it looks like this:

  'objectGUID' => 'Q?Ot?qM?r/?'

I have a GuidToString routine, filthily stolen from the internets, which I have posted below. In a nutshell, I'm looking for a hex string in the form: "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX"

The below code gives me that, but something is just not right, especially as most of the strings I get back have > 8 0's on the end in most cases.

Has anybody played with this before? Afaik, guid is the only thing that can never change on a users ad account, and I need to record these for a intranet based web app.

Any help much appreciated...

Here's that routine...

sub GuidToString { return sprintf "%08X-%04X-%04X-%02X%02X-%02X%02X%02X%02X%02X%0 +2X", unpack("I", $_[0]), unpack("S", substr($_[0], 4, 2)), unpack("S", substr($_[0], 6, 2)), unpack("C", substr($_[0], 8, 1)), unpack("C", substr($_[0], 9, 1)), unpack("C", substr($_[0], 10, 1)), unpack("C", substr($_[0], 11, 1)), unpack("C", substr($_[0], 12, 1)), unpack("C", substr($_[0], 13, 1)), unpack("C", substr($_[0], 14, 1)), unpack("C", substr($_[0], 15, 1)); }

Replies are listed 'Best First'.
Re: Net::LDAP GUID...
by Anonymous Monk on Apr 06, 2011 at 20:54 UTC
      sub GuidToString
      {
          my $stringGUID = unpack("H*", shift);
          $stringGUID =~ s/^(\w\w)(\w\w)(\w\w)(\w\w)(\w\w)(\w\w)(\w\w)(\w\w)(\w\w\w\w)}/$4$3$2$1-$6$5-$8$7-$9-/;
          return $stringGUID;
      }
      
        I like the GuidToString sub but the extra curly brace was a little tough to find. This works great. sub GuidToString { my $stringGUID = unpack("H*", shift); $stringGUID =~ s/^(\w\w)(\w\w)(\w\w)(\w\w)(\w\w)(\w\w)(\w\w)(\w\w)(\w\w\w\w)/$4$3$2$1-$6$5-$8$7-$9-/; return $stringGUID; } Thanks,

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://897883]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others taking refuge in the Monastery: (3)
As of 2024-04-25 18:48 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found