#!/usr/local/bin/rexx
/*
 QUSERID.rexx - Jim Dutton - 03/20/2000, 03/30/2000

 query an LDAP database for the VM/MVS USERID (vmuserid/mvsuerid) and mail
 attributes for a given DN or VM/MVS userid

 Requires: REXX (obviously) and LDAPSEARCH (UMich, Netscape?, OpenLDAP)
*/
Address 'UNIX'
If arg() = 0  then
  Do
    say; say "Usage (one of the following; comments in parentheses):"
    say "    quserid <VM/MVS userid>"
    say "    quserid cn=<LDAP Common NAME> (for single word CN)"
    say "    quserid cn=<'LDAP Common NAME'> (for multiple word CN)"
    say "    quserid sn=<LDAP Surname>"
    say "    quserid mail=<'LDAP MAIL address'>"; say; Exit
  End
Else
  parse arg userid . 1 attr "=" remainder

Select
  When  attr = "cn"    then  filter = "cn="remainder
  When  attr = "sn"    then  filter = "sn="remainder
  When  attr = "mail"  then
        filter = "(|(mail="remainder")(othermailbox="remainder"))"
  Otherwise  filter = "(|(vmuserid="userid")(mvsuserid="userid"))"
End

/*say "filter:["filter"]"*/
ldapcmd    = "/usr/local/bin/ldapsearch"; ldaphost = "-h <LDAP host>"
ldapbase   = "-b o=<org>,c=US"
attributes = "dn vmuserid mvsuserid mail telephonenumber ou l"
null = ""; telenumber = "Unknown"; vmuserids = null; campus = "Unknown"
username = "Unknown"; mailaddr = "Unknown"; mvsuserids = null

ldapcmd ldaphost ldapbase '"'filter'"' attributes "| rxstack"
If rc \= 0  then
  Do
    say "*** LDAPSEARCH ERROR (rc="rc") ***"
    say "--- search terminated ---"; Exit rc
  End

Do queued()
  parse pull attr "=" remainder
  Select
    When attr = ""                 then  Call DisplayResult
    When attr = "cn"               then  parse var remainder username "," .
    When attr = "vmuserid"         then  vmuserids = vmuserids remainder
    When attr = "mvsuserid"        then  mvsuserids = mvsuserids remainder
    When attr = "mail"             then  mailaddr = remainder
    When attr = "telephonenumber"  then  telenumber = remainder
    When attr = "ou"               then  department = remainder
    When attr = "l"                then  campus = remainder
    Otherwise
  End
End

Call DisplayResult
Exit


DisplayResult:
  say "=================================================================="
  say "User Name       :" username
  If vmuserids  = null  then  vmuserids  = "None"
  If mvsuserids = null  then  mvsuserids = "None"
  say "VM Userid(s)    :" strip(vmuserids)
  say "MVS Userid(s)   :" strip(mvsuserids)
  say "E-mail address  :" mailaddr
  say "Telephone Number:" telenumber
  say "Department      :" department
  say "Campus          :" campus
  say "=================================================================="; say
  vmuserids = null; username = "Unknown"; mailaddr = "Unknown"
  telenumber = "Unknown"; campus = "Unknown"; mvsuserids = null
Return
