pike.git / lib / modules / Protocols.pmod / LDAP.pmod / client.pike

version» Context lines:

pike.git/lib/modules/Protocols.pmod/LDAP.pmod/client.pike:1:   // LDAP client protocol implementation for Pike.   // - // $Id: client.pike,v 1.15 2000/07/14 15:29:39 hop Exp $ + // $Id: client.pike,v 1.16 2000/07/20 14:54:41 hop Exp $   //   // Honza Petrous, hop@unibase.cz   //   // ----------------------------------------------------------------------   //   // History:   //   // v0.0 1998-05-25 Starting up!   // v1.0 1998-06-21 Core functions (open, bind, unbind, delete, add,   // compare, search), only V2 operations,
pike.git/lib/modules/Protocols.pmod/LDAP.pmod/client.pike:43:   // 1999-03-28 - rewritten ldap_[op] startup code   //   // v1.11 1999-04-10 - search filter now processed multiple wild '*' chars   // [ Escaping untested, yet ]   // v1.13 2000-02-12 - fixed search NOT op bug (end revision normalized)   //   // v1.14 2000-02-17 - added decoding of UTF8 strings for v3 protocol   //   // newer versions - see CVS at roxen.com (hop)   // + // - corrected deUTF8 values in result + // - + //   // Specifications:   //   // RFC 1558 (search filter representations)   // RFC 1777,1778,1779 (version2 spec)   // RFC 1823 (v2 API)   // RFC 2251,2252,2253,2254,2255,2256 (version3 spec)   // draft-ietf-asid-ldap-c-api-00.txt (v3 API)   // RFC2279 (UTF-8)   //   // Interesting, applicable
pike.git/lib/modules/Protocols.pmod/LDAP.pmod/client.pike:82:   #define ASN1_GET_ATTR_NAME(X) ((X)->elements[0]->value)       inherit .protocol;       private int binded = 0; // flag for v2 operations    private string ldap_basedn = ""; // baseDN    private int ldap_scope = 0; // 0: base, 1: onelevel, 2: subtree    private int ldap_deref = 0; // 0: ...    private int ldap_sizelimit = 0;    private int ldap_timelimit = 0; +  private mapping lauth = ([]);             class result // ------------------    {       private int resultcode = LDAP_SUCCESS;    //private string resultstring = LDAP_SUCCESS_STR;    private int entrycnt = 0;    private int actnum = 0;
pike.git/lib/modules/Protocols.pmod/LDAP.pmod/client.pike:103:       int error_number() { return(resultcode); }       string error_string() { return(ldap_errlist[resultcode]); }       int num_entries() { return(entrycnt); }       int count_entries() { return(entrycnt - actnum); }       -  private array _get_attr_values(object x) { +  private array _get_attr_values(int ver, object x) {       array res = ({});       if(!sizeof(x->elements))    return(res);    foreach(x->elements[1]->elements, object val1)    res += ({ val1->value }); -  +  if(ver == 3) { +  // deUTF8 +  res = Array.map(res, utf8_to_string); +  }    return(res);    }       private array _New_decode(array ar) {       array res = ({});    array entry1;    mapping attrs;       foreach(ar, string raw1) {    attrs = (["dn":({ASN1_DECODE_DN(raw1)})]);    entry1 = ASN1_GET_ATTR_ARRAY(raw1);    foreach(entry1, object attr1) { -  attrs += ([ASN1_GET_ATTR_NAME(attr1):_get_attr_values(attr1)]); +  attrs += ([ASN1_GET_ATTR_NAME(attr1):_get_attr_values(ldap_version, attr1)]);    }    res += ({attrs});    }    -  if(ldap_version == 3) { -  // deUTF8 -  res = Array.map(res, utf8_to_string); -  } -  +     return (res);    } // _New_decode       object|int create(array rawres, int|void stuff) {    // rawres: array of result in raw format, but WITHOUT LDAP PDU !!!    // stuff: 1=bind result; ...       int lastel = sizeof(rawres) - 1;       if (lastel < 0) {
pike.git/lib/modules/Protocols.pmod/LDAP.pmod/client.pike:225:    }       private int chk_binded() {    // For version 2: we must be 'binded' first !!!       if ((ldap_version == 2) && !binded) {    seterr (LDAP_PROTOCOL_ERROR);    THROW(({"LDAP: Must binded first.\n",backtrace()}));    return(-ldap_errno);    } +  if ((ldap_version == 3) && !binded) +  bind();    return(0);    }       private int chk_dn(string dn) {       if ((!dn) || (!sizeof(dn))) {    seterr (LDAP_INVALID_DN_SYNTAX);    THROW(({"LDAP: Invalid DN syntax.\n",backtrace()}));    return(-ldap_errno);    }    return(0);    }       // API function (ldap_open)    //    // create(string|void server)    // -  // server: server name (hostname or IP, default: 127.0.0.1) -  // with optional port number (default: 389) +  // server: server URL in form "ldap://hostname/basedn???!bindname=    void create(string|void server)    { -  int port = LDAP_DEFAULT_PORT; +        if(!server || !sizeof(server))    server = (string)LDAP_DEFAULT_HOST; -  else -  if(predef::search(server,":")>0) { -  port = (int)((server / ":")[1]); -  server = (server / ":")[0]; +  +  lauth = parse_url(server); +  +  if(!stringp(lauth->scheme) || (lauth->scheme != "ldap")) { +  THROW(({"Unknown scheme in server URL.\n",backtrace()}));    }    -  ::create(server, port); -  if(!::connected) -  { +  ::create(lauth->host||LDAP_DEFAULT_HOST, lauth->port||LDAP_DEFAULT_PORT); +  if(!::connected) {    THROW(({"Failed to connect to LDAP server.\n",backtrace()}));    }    DWRITE(sprintf("client.create: remote = %s\n", query_address())); -  DWRITE_HI("client.OPEN: " + server + " - OK\n"); +  DWRITE_HI("client.OPEN: " + lauth->host||LDAP_DEFAULT_HOST + (string)(lauth->port||LDAP_DEFAULT_PORT) + " - OK\n");       binded = 0;    -  +  if(lauth->scope) +  set_scope(lauth->scope); +  if(lauth->basedn) +  set_basedn(lauth->basedn); +     } // create       private mixed send_bind_op(string name, string password) {    // Simple BIND operation       object msgval, vers, namedn, auth, app;       vers = Standards.ASN1.Types.asn1_integer(ldap_version);    namedn = Standards.ASN1.Types.asn1_octet_string(name);    auth = ASN1_CONTEXT_OCTET_STRING(0, password);
pike.git/lib/modules/Protocols.pmod/LDAP.pmod/client.pike:301:       int id;    mixed raw;    object rv;       if (!proto)    proto = LDAP_DEFAULT_VERSION;    if (chk_ver())    return(-ldap_errno);    if (!stringp(name)) -  name = ""; +  name = mappingp(lauth->ext) ? lauth->ext->bindname||"" : "";    if (!stringp(password))    password = "";    ldap_version = proto;    if(ldap_version == 3) {    name = string_to_utf8(name);    password = string_to_utf8(password);    }    if(intp(raw = send_bind_op(name, password))) {    THROW(({error_string()+"\n",backtrace()}));    return(-ldap_errno);
pike.git/lib/modules/Protocols.pmod/LDAP.pmod/client.pike:970:    url = url[sizeof(s)+4..];       res += ([ "host" : (s / ":")[0] ]);       if(sizeof(s / ":") > 1)    res += ([ "port" : (s / ":")[1] ]);       ar = url / "?";       switch (sizeof(ar)) { -  case 5: res += ([ "ext" : ar[4] ]); +  case 5: if (sizeof(ar[4])) { +  mapping extensions = ([]); +  foreach(ar[4] / ",", string ext) { +  int ix = predef::search(ext, "="); +  if(ix) +  extensions += ([ ext[..(ix-1)] : replace(ext[ix+1..],QUOTED_COMMA, ",") ]); +  } +  if (sizeof(extensions)) +  res += ([ "ext" : extensions ]); +  } +  //case 5: res += ([ "ext" : ar[4] ]);    case 4: res += ([ "filter" : ar[3] ]); -  case 3: res += ([ "scope" : ar[2] ]); +  case 3: switch (ar[2]) { +  case "sub": res += ([ "scope" : 2 ]); break; +  case "one": res += ([ "scope" : 1 ]); break; +  default: res += ([ "scope" : 0]); // = "base" +  }    case 2: res += sizeof(ar[1]) ? ([ "attributes" : ar[1] / "," ]) : ([]); -  case 1: res += ([ "dn" : ar[0] ]); +  case 1: res += ([ "basedn" : ar[0] ]);    }    -  //write("DEB: mapping: [%O] \n", res); -  +     return (res);       } //parse_uri         #endif