Back to index

openldap  2.4.31
result.c
Go to the documentation of this file.
00001 /* result.c - sock backend result reading function */
00002 /* $OpenLDAP$ */
00003 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
00004  *
00005  * Copyright 2007-2012 The OpenLDAP Foundation.
00006  * All rights reserved.
00007  *
00008  * Redistribution and use in source and binary forms, with or without
00009  * modification, are permitted only as authorized by the OpenLDAP
00010  * Public License.
00011  *
00012  * A copy of this license is available in the file LICENSE in the
00013  * top-level directory of the distribution or, alternatively, at
00014  * <http://www.OpenLDAP.org/license.html>.
00015  */
00016 /* ACKNOWLEDGEMENTS:
00017  * This work was initially developed by Brian Candler for inclusion
00018  * in OpenLDAP Software.
00019  */
00020 
00021 #include "portable.h"
00022 
00023 #include <stdio.h>
00024 
00025 #include <ac/errno.h>
00026 #include <ac/string.h>
00027 #include <ac/socket.h>
00028 #include <ac/unistd.h>
00029 
00030 #include "slap.h"
00031 #include "back-sock.h"
00032 
00033 /*
00034  * FIXME: make a RESULT section compulsory from the socket response.
00035  * Otherwise, a partial/aborted response is treated as 'success'.
00036  * This is a divergence from the back-shell protocol, but makes things
00037  * more robust.
00038  */
00039 
00040 int
00041 sock_read_and_send_results(
00042     Operation *op,
00043     SlapReply *rs,
00044     FILE      *fp )
00045 {
00046        int    bsize, len;
00047        char   *buf, *bp;
00048        char   line[BUFSIZ];
00049        char   ebuf[128];
00050 
00051        /* read in the result and send it along */
00052        buf = (char *) ch_malloc( BUFSIZ );
00053        buf[0] = '\0';
00054        bsize = BUFSIZ;
00055        bp = buf;
00056        while ( !feof(fp) ) {
00057               errno = 0;
00058               if ( fgets( line, sizeof(line), fp ) == NULL ) {
00059                      if ( errno == EINTR ) continue;
00060 
00061                      Debug( LDAP_DEBUG_ANY, "sock: fgets failed: %s (%d)\n",
00062                             AC_STRERROR_R(errno, ebuf, sizeof ebuf), errno, 0 ); 
00063                      break;
00064               }
00065 
00066               Debug( LDAP_DEBUG_SHELL, "sock search reading line (%s)\n",
00067                   line, 0, 0 );
00068 
00069               /* ignore lines beginning with # (LDIFv1 comments) */
00070               if ( *line == '#' ) {
00071                      continue;
00072               }
00073 
00074               /* ignore lines beginning with DEBUG: */
00075               if ( strncasecmp( line, "DEBUG:", 6 ) == 0 ) {
00076                      continue;
00077               }
00078 
00079               len = strlen( line );
00080               while ( bp + len + 1 - buf > bsize ) {
00081                      size_t offset = bp - buf;
00082                      bsize += BUFSIZ;
00083                      buf = (char *) ch_realloc( buf, bsize );
00084                      bp = &buf[offset];
00085               }
00086               strcpy( bp, line );
00087               bp += len;
00088 
00089               /* line marked the end of an entry or result */
00090               if ( *line == '\n' ) {
00091                      if ( strncasecmp( buf, "RESULT", 6 ) == 0 ) {
00092                             break;
00093                      }
00094                      if ( strncasecmp( buf, "CONTINUE", 8 ) == 0 ) {
00095                             struct sockinfo      *si = (struct sockinfo *) op->o_bd->be_private;
00096                             /* Only valid when operating as an overlay! */
00097                             assert( si->si_ops != 0 );
00098                             rs->sr_err = SLAP_CB_CONTINUE;
00099                             goto skip;
00100                      }
00101 
00102                      if ( (rs->sr_entry = str2entry( buf )) == NULL ) {
00103                             Debug( LDAP_DEBUG_ANY, "str2entry(%s) failed\n",
00104                                 buf, 0, 0 );
00105                      } else {
00106                             rs->sr_attrs = op->oq_search.rs_attrs;
00107                             rs->sr_flags = REP_ENTRY_MODIFIABLE;
00108                             send_search_entry( op, rs );
00109                             entry_free( rs->sr_entry );
00110                             rs->sr_attrs = NULL;
00111                      }
00112 
00113                      bp = buf;
00114               }
00115        }
00116        (void) str2result( buf, &rs->sr_err, (char **)&rs->sr_matched, (char **)&rs->sr_text );
00117 
00118        /* otherwise, front end will send this result */
00119        if ( rs->sr_err != 0 || op->o_tag != LDAP_REQ_BIND ) {
00120               send_ldap_result( op, rs );
00121        }
00122 
00123 skip:
00124        ch_free( buf );
00125 
00126        return( rs->sr_err );
00127 }
00128 
00129 void
00130 sock_print_suffixes(
00131     FILE      *fp,
00132     Backend   *be
00133 )
00134 {
00135        int    i;
00136 
00137        for ( i = 0; be->be_suffix[i].bv_val != NULL; i++ ) {
00138               fprintf( fp, "suffix: %s\n", be->be_suffix[i].bv_val );
00139        }
00140 }
00141 
00142 void
00143 sock_print_conn(
00144     FILE      *fp,
00145     Connection       *conn,
00146     struct sockinfo *si
00147 )
00148 {
00149        if ( conn == NULL ) return;
00150 
00151        if( si->si_extensions & SOCK_EXT_BINDDN ) {
00152               fprintf( fp, "binddn: %s\n",
00153                      conn->c_dn.bv_len ? conn->c_dn.bv_val : "" );
00154        }
00155        if( si->si_extensions & SOCK_EXT_PEERNAME ) {
00156               fprintf( fp, "peername: %s\n",
00157                      conn->c_peer_name.bv_len ? conn->c_peer_name.bv_val : "" );
00158        }
00159        if( si->si_extensions & SOCK_EXT_SSF ) {
00160               fprintf( fp, "ssf: %d\n", conn->c_ssf );
00161        }
00162        if( si->si_extensions & SOCK_EXT_CONNID ) {
00163               fprintf( fp, "connid: %lu\n", conn->c_connid );
00164        }
00165 }