{
    "content": [
        {
            "type": "text",
            "text": "# Net::LDAP::Examples (perldoc)\n\n## NAME\n\nNet::LDAP::Examples - PERL LDAP by Example\n\n## DESCRIPTION\n\nThe following examples are of course PERL code, found to work with the Net::LDAP modules.\n\n## Sections\n\n- **NAME**\n- **DESCRIPTION**\n- **CODE**\n- **LDAP SCHEMA RETRIEVAL**\n- **BUGS**\n- **COPYRIGHT**\n\nUse structuredContent.sections for detailed options, examples, and full documentation.\n"
        }
    ],
    "structuredContent": {
        "command": "Net::LDAP::Examples",
        "section": "",
        "mode": "perldoc",
        "summary": "Net::LDAP::Examples - PERL LDAP by Example",
        "synopsis": null,
        "tldr_summary": null,
        "tldr_examples": [],
        "tldr_source": null,
        "flags": [],
        "examples": [],
        "see_also": [],
        "section_outline": [
            {
                "name": "NAME",
                "lines": 2,
                "subsections": []
            },
            {
                "name": "DESCRIPTION",
                "lines": 26,
                "subsections": []
            },
            {
                "name": "CODE",
                "lines": 340,
                "subsections": []
            },
            {
                "name": "LDAP SCHEMA RETRIEVAL",
                "lines": 64,
                "subsections": []
            },
            {
                "name": "BUGS",
                "lines": 5,
                "subsections": []
            },
            {
                "name": "COPYRIGHT",
                "lines": 2,
                "subsections": []
            }
        ],
        "sections": {
            "NAME": {
                "content": "Net::LDAP::Examples - PERL LDAP by Example\n",
                "subsections": []
            },
            "DESCRIPTION": {
                "content": "The following examples are of course PERL code, found to work with the Net::LDAP modules.\n\nThe intent of this document is to give the reader a *cut and paste* jump start to getting an\nLDAP application working.\n\nBelow you will find snippets of code that should work as-is with only a small amount of work to\ncorrect any variable assignments and LDAP specifics, e.g. Distinguished Name Syntax, related to\nthe user's own implementation.\n\nThe *S*tandard *O*perating *P*rocedure that is followed here is:\n\n1 Package - use Net::LDAP\n2 Initialization - new\n3 Binding - bind\n4 Operation - add modify moddn search\n4.1 Processing - displaying data from a search\n5 Error - displaying error information\n6 Unbinding - unbind\n\nLook to each of these for a snippet of code to meet your needs.\n\nWhat is not covered in these examples at this time:\n\n*abandon* and *compare* methods\n*callback* subroutines\n",
                "subsections": []
            },
            "CODE": {
                "content": "PACKAGE - Definitions\nuse Net::LDAP;\n\nINITIALIZING\n$ldap = Net::LDAP->new ( \"yourLDAPhost.yourCompany.com\" ) or die \"$@\";\n\nBINDING\n$mesg = $ldap->bind ( version => 3 );          # use for searches\n\n$mesg = $ldap->bind ( \"$userToAuthenticate\",\npassword => \"$passwd\",\nversion => 3 );          # use for changes/edits\n\n# see your LDAP administrator for information concerning the\n# user authentication setup at your site.\n\nOPERATION - Generating a SEARCH\nsub LDAPsearch\n{\nmy ($ldap,$searchString,$attrs,$base) = @;\n\n# if they don't pass a base... set it for them\n\nif (!$base ) { $base = \"o=mycompany, c=mycountry\"; }\n\n# if they don't pass an array of attributes...\n# set up something for them\n\nif (!$attrs ) { $attrs = [ 'cn','mail' ]; }\n\nmy $result = $ldap->search ( base    => \"$base\",\nscope   => \"sub\",\nfilter  => \"$searchString\",\nattrs   =>  $attrs\n);\n\n}\n\nmy @Attrs = ( );               # request all available attributes\n# to be returned.\n\nmy $result = LDAPsearch ( $ldap, \"sn=*\", \\@Attrs );\n\nPROCESSING - Displaying SEARCH Results\n#------------\n#\n# Accessing the data as if in a structure\n#  i.e. Using the \"asstruct\"  method\n#\n\nmy $href = $result->asstruct;\n\n# get an array of the DN names\n\nmy @arrayOfDNs  = keys %$href;        # use DN hashes\n\n# process each DN using it as a key\n\nforeach ( @arrayOfDNs ) {\nprint $, \"\\n\";\nmy $valref = $$href{$};\n\n# get an array of the attribute names\n# passed for this one DN.\nmy @arrayOfAttrs = sort keys %$valref; #use Attr hashes\n\nmy $attrName;\nforeach $attrName (@arrayOfAttrs) {\n\n# skip any binary data: yuck!\nnext if ( $attrName =~ /;binary$/ );\n\n# get the attribute value (pointer) using the\n# attribute name as the hash\nmy $attrVal =  @$valref{$attrName};\nprint \"\\t $attrName: @$attrVal \\n\";\n}\nprint \"#-------------------------------\\n\";\n# End of that DN\n}\n#\n#  end of asstruct method\n#\n#--------\n\n\n#------------\n#\n# handle each of the results independently\n# ... i.e. using the walk through method\n#\nmy @entries = $result->entries;\n\nmy $entr;\nforeach $entr ( @entries ) {\nprint \"DN: \", $entr->dn, \"\\n\";\n\nmy $attr;\nforeach $attr ( sort $entr->attributes ) {\n# skip binary we can't handle\nnext if ( $attr =~ /;binary$/ );\nprint \"  $attr : \", $entr->getvalue ( $attr ) ,\"\\n\";\n}\n\nprint \"#-------------------------------\\n\";\n}\n\n#\n# end of walk through method\n#------------\n\nOPERATION - Modifying entries\n#\n#   Modify\n#\n#  for each of the modifies below you'll need to supply\n#  a full DN (Distinguished Name) for the $dn variable.\n#   example:\n#    cn=Jo User,ou=person,o=mycompany,c=mycountry\n#\n#   I would recommend doing a search (listed above)\n#   then use the dn returned to populate the $dn variable.\n\n#\n#  Do we only have one result returned from the search?\n\nif ( $result->count != 1 ) { exit; }  # Nope.. exit\n\nmy $dn = $entries[0]->dn;         # yes.. get the DN\n\n#######################################\n#\n#   MODIFY using a HASH\n#\n\nmy %ReplaceHash = ( keyword => \"x\", proxy => \"x\" );\n\nmy $result = LDAPmodifyUsingHash ( $ldap, $dn, \\%ReplaceHash );\n\nsub LDAPmodifyUsingHash\n{\nmy ($ldap, $dn, $whatToChange ) = @;\nmy $result = $ldap->modify ( $dn,\nreplace => { %$whatToChange }\n);\nreturn $result;\n}\n\n#######################################\n#\n#   MODIFY using a ARRAY List\n#\n\nmy @ReplaceArrayList = [ 'keyword', \"xxxxxxxxxx\",\n'proxy' , \"yyyyyyyyyy\"   ];\n\nmy $result = LDAPmodifyUsingArrayList ( $ldap, $dn, \\@ReplaceArrayList );\n\nsub LDAPmodifyUsingArrayList\n{\nmy ($ldap, $dn, $whatToChange ) = @;\nmy $result = $ldap->modify ( $dn,\nchanges => [\nreplace => @$whatToChange\n]\n);\nreturn $result;\n}\n\n#######################################\n#\n#   MODIFY using a ARRAY\n#\n\nmy @ReplaceArray = ( 'keyword', \"xxxxxxxxxx\" ,\n'proxy' , \"yyyyyyyyyy\"   );\n\nmy $result = LDAPmodifyUsingArray ( $ldap, $dn, \\@ReplaceArray );\n\nsub LDAPmodifyUsingArray\n{\nmy ($ldap, $dn, $whatToChange ) = @;\nmy $result = $ldap->modify ( $dn,\nchanges => [\nreplace => [ @$whatToChange ]\n]\n);\nreturn $result;\n}\n\n#######################################\n#\n#   MODIFY an existing record using 'Changes'\n#    (or combination of add/delete/replace)\n#\n\nmy @whatToChange;\nmy @ReplaceArray;\nmy @DeleteArray;\nmy @AddArray;\n\npush @AddArray, 'cn', \"me myself\";\npush @ReplaceArray, 'sn', '!@#$%^&*()+Hello There';\npush @ReplaceArray, 'cn', \"me myself I\";\npush @DeleteArray, 'cn', \"me myself\";\n\nif ( $#ReplaceArray > 0 ) {\npush @whatToChange, 'replace';\npush @whatToChange, \\@ReplaceArray;\n}\nif ( $#DeleteArray > 0 ) {\npush @whatToChange, 'delete';\npush @whatToChange, \\@DeleteArray;\n}\nif ( $#AddArray > 0 ) {\npush @whatToChange, 'add';\npush @whatToChange, \\@AddArray;\n}\n\n$result = LDAPmodify ( $ldap, $dn, \\@whatToChange );\n\nsub LDAPmodify\n{\nmy ($ldap, $dn, $whatToChange) = @;\n\nmy $result = $ldap->modify ( $dn,\nchanges => [\n@$whatToChange\n]\n);\nreturn $result;\n}\n\nOPERATION - Changing the RDN\nmy $newRDN = \"cn=Joseph User\";\n\nmy $result = LDAPrdnChange ( $ldap, $dn, $newRDN, \"archive\" );\n\n\nsub LDAPrdnChange\n{\nmy ($ldap,$dn,$whatToChange,$action) = @;\n\nmy $branch;\n\n#\n# if the archive action is selected, move this\n# entry to another place in the directory.\n#\nif ( $action =~ /archive/i )  {\n$branch = \"ou=newbranch, o=mycompany, c=mycountry\";\n}\n\n#\n# use the 'deleteoldrdn' to keep from getting\n# multivalues in the NAMING attribute.\n# in most cases that would be the 'CN' attribute\n#\nmy $result = $ldap->moddn ( $dn,\nnewrdn => $whatToChange,\ndeleteoldrdn => '1',\nnewsuperior => $branch\n);\n\nreturn $result;\n\n}\n\nOPERATION - Adding a new Record\nmy $DNbranch = \"ou=bailiwick, o=mycompany, c=mycountry\";\n\n#\n# check with your Directory Schema or Administrator\n# for the correct objectClass... I'm sure it'll be different\n#\nmy $CreateArray = [\nobjectClass => [ \"top\", \"person\", \"organizationalPerson\", \"inetOrgPerson\" ],\ncn => \"Jane User\",\nuid => \"0000001\",\nsn => \"User\",\nmail => 'JaneUser@mycompany.com'\n];\n\n#\n# create the  new DN to look like this\n# \" cn=Jo User + uid=0000001 , ou=bailiwick, o=mycompany, c=mycountry \"\n#\n# NOTE: this DN  MUST be changed to meet your implementation\n#\n\nmy $NewDN = \"@$CreateArray[2]=\".\n\"@$CreateArray[3]+\".\n\"@$CreateArray[4]=\".\n\"@$CreateArray[5],\".\n$DNbranch;\n\nLDAPentryCreate($ldap, $NewDN, $CreateArray);\n\n#\n# CreateArray is a reference to an anonymous array\n# you have to dereference it in the  subroutine it's\n# passed to.\n#\n\nsub LDAPentryCreate\n{\nmy ($ldap, $dn, $whatToCreate) = @;\nmy $result = $ldap->add ( $dn, attrs => [ @$whatToCreate ] );\nreturn $result;\n}\n\nERROR - Retrieving and Displaying ERROR information\nif ( $result->code ) {\n#\n# if we've got an error... record it\n#\nLDAPerror ( \"Searching\", $result );\n}\n\nsub LDAPerror\n{\nmy ($from, $mesg) = @;\nprint \"Return code: \", $mesg->code;\nprint \"\\tMessage: \", $mesg->errorname;\nprint \" :\",          $mesg->errortext;\nprint \"MessageID: \", $mesg->mesgid;\nprint \"\\tDN: \", $mesg->dn;\n\n#---\n# Programmer note:\n#\n#  \"$mesg->error\" DOESN'T work!!!\n#\n#print \"\\tMessage: \", $mesg->error;\n#-----\n}\n\nUNBIND\n$ldap->unbind;\n",
                "subsections": []
            },
            "LDAP SCHEMA RETRIEVAL": {
                "content": "The following code snippet shows how to retrieve schema information.\n\nThe first procedure is to initialize a new LDAP object using the same procedures as listed at\nthe beginning of this document.\n\nThe second procedure is to bind to your directory server. Some servers may require\nauthentication to retrieve the schema from the directory server. This procedure is listed at the\nbeginning of this document too.\n\nAfter a successful bind you are ready to retrieve the schema information. You do this by\ninitializing a schema object.\n\n$schema = $ldap->schema ( );\n\nIn this case Net::LDAP will attempt to determine the dn under which the schema can be found.\nFirst it will look for the attribute \"subschemaSubentry\" in the root DSE. If that cannot be\nfound then it will default to the assumption of \"cn=schema\"\n\nAlternatively you can specify the dn where the schema is to be found with\n\n$schema = $ldap->schema ( dn => $dn );\n\nOnce we have a dn to search for, Net::LDAP will fetch the schema entry with\n\n$mesg = $self->search ( base   => $dn,\nscope  => 'base',\nfilter => '(objectClass=subschema)',\n);\n\nOnce the schema object has been initialized, schema methods are used to retrieve the data. There\nare a number of ways this can be done. Information on the schema methods can be found in the\nNet::LDAP::Schema pod documentation.\n\nThe following is a code snippet showing how to get and display information about returned\nattributes.\n\n#\n# Get the attributes\n#\n\n@attributes = $schema->allattributes ( );\n\n#\n# Display the attributes\n#\n\nforeach $ar ( @attributes ) {\nprint \"attributeType: \", $ar->{name}, \"\\n\";\n\n#\n# Print all the details\n#\n\nforeach $key ( keys %{$ar} ) {\nprint join ( \"\\n\\t\\t\", \"\\t$key:\",\nref ( $ar->{$key} ) ? @{$ar->{$key}} : $ar->{$key}\n), \"\\n\";\n}\n}\n\nThe process is the basically the same for getting objectClass information. Where\nschema->allattributes() is used, substitute schema->allobjectclasses(). From that point on the\nprocess is the same for both objectClasses and attributes.\n",
                "subsections": []
            },
            "BUGS": {
                "content": "None known, but there may be some\n\nAUTHOR  (of this document)\nRussell Biggs <rgb@ticnet.com>\n",
                "subsections": []
            },
            "COPYRIGHT": {
                "content": "All rights to this document are hereby relinquished to Graham Barr.\n",
                "subsections": []
            }
        }
    }
}