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

version» Context lines:

pike.git/lib/modules/Protocols.pmod/LDAP.pmod/protocol.pike:1:   #pike __REAL_VERSION__      // LDAP client protocol implementation for Pike.   // - // $Id: protocol.pike,v 1.22 2008/06/28 16:49:54 nilsson Exp $ + // $Id: protocol.pike,v 1.23 2008/10/29 14:19:08 mast Exp $   //   // Honza Petrous, hop@unibase.cz   //   // ----------------------------------------------------------------------   //   // History:   //   // v1.9 1999-02-19 created separate file   // - improved checking of readed bytes in 'readmsg'   // v1.9.1.1
pike.git/lib/modules/Protocols.pmod/LDAP.pmod/protocol.pike:35:       /*private*/ string readbuf=""; // read buffer    private int ok; // read buffer status    /*private*/ string writebuf=""; // write buffer   // private written; // count of written chars    private function con_ok; // async callback functions    object conthread; // thread connection    array extra_args; // not used, yet   // /*private*/ int errno;    int connected = 0; -  object low_fd = Stdio.File(); // helper fd -  object ldapfd; // helper fd +  Stdio.File ldapfd; // helper fd      protected int last_io_time; // Timestamp when I/O on the fd was made last.       int seterr(int errno, void|string errstr) {    // Sets ldap_err* variables and returns errno       ldap_rem_errstr = errstr;    ldap_errno = errno;    return errno;    }       //! -  //! Returns the error number from the last transaction. +  //! Returns the error number from the last transaction. If the error +  //! is @[LDAP_SERVER_DOWN] then there was a socket error, and the +  //! I/O error number can be retrieved using +  //! @expr{@[ldapfd]->errno()@}.    //!    //! @seealso    //! @[error_string], @[server_error_string]    int error_number() { return ldap_errno; }       //!    //! Returns the description of the error from the last transaction.    //! This is the error string from the server, or a standard error    //! message corresponding to the error number if the server didn't    //! provide any description.    //! -  +  //! If @[error_number] returns @[LDAP_SERVER_DOWN] then this is the +  //! @[strerror] message corresponding to the I/O error for the +  //! connection. +  //!    //! @seealso    //! @[server_error_string], @[error_number]    string error_string() { return ldap_rem_errstr || ldap_error_strings[ldap_errno]; }      //! Returns the error string from the server, or zero if the server   //! didn't provide any.   //!   //! @seealso   //! @[error_string], @[error_number]   string server_error_string() {return ldap_rem_errstr;}
pike.git/lib/modules/Protocols.pmod/LDAP.pmod/protocol.pike:90:    protected void read_answer() {    // ----------------------    // Reads LDAP PDU (with defined msgid) from the server       int msglen = 0, ix, ofs;    string s, shlp;       if(sizeof(readbuf) < 2)    readbuf = ldapfd->read(2); // 1. byte = 0x0C, 2. byte = msglen    if (intp(readbuf) || (sizeof(readbuf) < 2)) { -  seterr (LDAP_TIMEOUT); -  DWRITE_HI("protocol.read_answer: ERROR: connection closed.\n"); -  THROW(({"LDAP: connection closed.\n",backtrace()})); +  seterr (LDAP_SERVER_DOWN, +  !readbuf ? strerror (ldapfd->errno()) : "Connection closed"); +  ERROR ("LDAP read error: %s\n", ldap_rem_errstr);    //return -ldap_errno;    return;    }    if (readbuf[0] != '0') {    seterr (LDAP_PROTOCOL_ERROR);    DWRITE_HI("protocol.read_answer: ERROR: retv=<"+sprintf("%O",readbuf)+">\n");    THROW(({"LDAP: Protocol mismatch.\n",backtrace()}));    //return -ldap_errno;    return;    }
pike.git/lib/modules/Protocols.pmod/LDAP.pmod/protocol.pike:119:    seterr (LDAP_PROTOCOL_ERROR);    THROW(({"LDAP: Protocol mismatch.\n",backtrace()}));    //return -ldap_errno;    return;    }    ofs = (msglen & 0x7f) + 2;    ix = ofs - sizeof(readbuf);    if(ix > 0)    s = ldapfd->read(ix);    if (!s || (sizeof(s) < ix)) { -  seterr (LDAP_PROTOCOL_ERROR); -  THROW(({"LDAP: Protocol mismatch.\n",backtrace()})); +  seterr (LDAP_SERVER_DOWN, +  !s ? strerror (ldapfd->errno()) : "Connection closed"); +  ERROR ("LDAP read error: %s\n", ldap_rem_errstr);    //return -ldap_errno;    return;    }    readbuf += s;    msglen = 0; // !!! RESTRICTION: 2^32 !!!    shlp = reverse(readbuf[2..ofs]);    for (ix=0; ix<sizeof(shlp); ix++) {    msglen += shlp[ix]*(1<<(ix*8));    }    }    ix = (ofs + msglen) - sizeof(readbuf);    if(ix > 0)    s = ldapfd->read(ix);    if (!s || (sizeof(s) < ix)) { -  seterr (LDAP_SERVER_DOWN); -  THROW(({"LDAP: connection closed by server.\n",backtrace()})); +  seterr (LDAP_CONNECT_ERROR, +  !s ? strerror (ldapfd->errno()) : "Connection closed"); +  ERROR ("LDAP read error: %s\n", ldap_rem_errstr);    //return -ldap_errno;    return;    }    readbuf += s;    //DWRITE(sprintf("protocol.read_answer: %s\n", .ldap_privates.ldap_der_decode(readbuf)->debug_string()));    DWRITE("protocol.read_answer: ok=1.\n");    ok = 1;       last_io_time = time();   
pike.git/lib/modules/Protocols.pmod/LDAP.pmod/protocol.pike:220:    if (objectp(msgval)) {    DWRITE(sprintf("protocol.do_op: msg = [%d]\n",sizeof(msgval->get_der())));    } else    DWRITE("protocol.do_op: msg is null!\n");       // call_out    writebuf = msgval->get_der();    rv = ldapfd->write(writebuf); // !!!!! - jak rozlisit async a sync ????    // call_out    if (rv < 2) { -  seterr (LDAP_SERVER_DOWN); -  THROW(({"LDAP: connection closed by server.\n",backtrace()})); +  seterr (LDAP_SERVER_DOWN, strerror (ldapfd->errno())); +  ERROR ("LDAP write error: %s\n", ldap_rem_errstr);    return -ldap_errno;    }    DWRITE(sprintf("protocol.do_op: write OK [%d bytes].\n",rv));    msgval = 0; msgid = 0;    writebuf= "";    readbuf= ""; // !!! NEni to pozde ?       last_io_time = time();       //`()();
pike.git/lib/modules/Protocols.pmod/LDAP.pmod/protocol.pike:250:   /* ------------ legacy support -----------------*/       string|int readmsg(int msgid) {    // Reads LDAP PDU (with defined msgid) from server, checks msgid ...       int msglen = 0, ix;    string|int retv;    string s, shlp;       retv = ldapfd->read(2); // 1. byte = 0x0C, 2. byte = msglen -  if (retv == -1) { -  seterr (LDAP_TIMEOUT); -  DWRITE_HI("protocol.readmsg: ERROR: connection timeout.\n"); -  THROW(({"LDAP: connection timeout.\n",backtrace()})); -  return -ldap_errno; -  } +     if (!retv || (sizeof(retv) != 2) || (retv[0] != '0')) { -  seterr (LDAP_PROTOCOL_ERROR); -  DWRITE_HI("protocol.readmsg: ERROR: retv=<"+sprintf("%O",retv)+">\n"); -  THROW(({"LDAP: Protocol mismatch.\n",backtrace()})); +  seterr (LDAP_SERVER_DOWN, +  !retv ? strerror (ldapfd->errno()) : "Connection closed"); +  ERROR ("LDAP read error: %s\n", ldap_rem_errstr);    return -ldap_errno;    }    DWRITE(sprintf("protocol.readmsg: sizeof = %d\n", sizeof(retv)));       msglen = retv[1];    if (msglen & 0x80) { // > 0x7f    if (msglen == 0x80) { // RFC does not allow implicitly defined length    seterr (LDAP_PROTOCOL_ERROR);    THROW(({"LDAP: Protocol mismatch.\n",backtrace()}));    return -ldap_errno;    }    s = ldapfd->read(msglen & 0x7f);    if (!s) { -  seterr (LDAP_PROTOCOL_ERROR); -  THROW(({"LDAP: Protocol mismatch.\n",backtrace()})); +  seterr (LDAP_SERVER_DOWN, strerror (ldapfd->errno())); +  ERROR ("LDAP read error: %s\n", ldap_rem_errstr);    return -ldap_errno;    }    retv += s;    msglen = 0; // !!! RESTRICTION: 2^32 !!!    shlp = reverse(s);    for (ix=0; ix<sizeof(shlp); ix++) {    msglen += shlp[ix]*(1<<(ix*8));    }    }    DWRITE(sprintf("protocol.readmsg: reading %d bytes.\n", msglen));    s = ldapfd->read(msglen);    if (!s | (sizeof(s) < msglen)) { -  seterr (LDAP_SERVER_DOWN); -  THROW(({"LDAP: connection closed by server.\n",backtrace()})); +  seterr (LDAP_SERVER_DOWN, +  !s ? strerror (ldapfd->errno()) : "Connection closed"); +  ERROR ("LDAP read error: %s\n", ldap_rem_errstr);    return -ldap_errno;    }    retv += s;    last_io_time = time();    DWRITE(sprintf("protocol.readmsg: %s\n", .ldap_privates.ldap_der_decode(retv)->debug_string()));    return retv;    }       int writemsg(object msgop) {    // Make LDAP PDU envelope for 'msgop' and send it
pike.git/lib/modules/Protocols.pmod/LDAP.pmod/protocol.pike:319:       if (objectp(msgval)) {    DWRITE(sprintf("protocol.writemsg: msg = [%d]\n",sizeof(msgval->get_der())));    } else    DWRITE("protocol.writemsg: msg is null!\n");       // call_out    rv = ldapfd->write(msgval->get_der());    // call_out    if (rv < 2) { -  seterr (LDAP_SERVER_DOWN); -  THROW(({"LDAP: connection closed by server.\n",backtrace()})); +  seterr (LDAP_SERVER_DOWN, strerror (ldapfd->errno())); +  ERROR ("LDAP write error: %s\n", ldap_rem_errstr);    return -ldap_errno;    }    DWRITE(sprintf("protocol.writemsg: write OK [%d bytes].\n",rv));    msgval = 0; msgid = 0;    last_io_time = time();    return msgnum;    }      #else   constant this_program_does_not_exist=1;   #endif