Back to index

openldap  2.4.31
result.c
Go to the documentation of this file.
00001 /* result.c - shell backend result reading function */
00002 /* $OpenLDAP$ */
00003 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
00004  *
00005  * Copyright 1998-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 /* Portions Copyright (c) 1995 Regents of the University of Michigan.
00017  * All rights reserved.
00018  *
00019  * Redistribution and use in source and binary forms are permitted
00020  * provided that this notice is preserved and that due credit is given
00021  * to the University of Michigan at Ann Arbor. The name of the University
00022  * may not be used to endorse or promote products derived from this
00023  * software without specific prior written permission. This software
00024  * is provided ``as is'' without express or implied warranty.
00025  */
00026 /* ACKNOWLEDGEMENTS:
00027  * This work was originally developed by the University of Michigan
00028  * (as part of U-MICH LDAP).
00029  */
00030 
00031 #include "portable.h"
00032 
00033 #include <stdio.h>
00034 
00035 #include <ac/errno.h>
00036 #include <ac/string.h>
00037 #include <ac/socket.h>
00038 #include <ac/unistd.h>
00039 
00040 #include "slap.h"
00041 #include "shell.h"
00042 
00043 int
00044 read_and_send_results(
00045     Operation *op,
00046     SlapReply *rs,
00047     FILE      *fp )
00048 {
00049        int    bsize, len;
00050        char   *buf, *bp;
00051        char   line[BUFSIZ];
00052        char   ebuf[128];
00053 
00054        /* read in the result and send it along */
00055        buf = (char *) ch_malloc( BUFSIZ );
00056        buf[0] = '\0';
00057        bsize = BUFSIZ;
00058        bp = buf;
00059        while ( !feof(fp) ) {
00060               errno = 0;
00061               if ( fgets( line, sizeof(line), fp ) == NULL ) {
00062                      if ( errno == EINTR ) continue;
00063 
00064                      Debug( LDAP_DEBUG_ANY, "shell: fgets failed: %s (%d)\n",
00065                             AC_STRERROR_R(errno, ebuf, sizeof ebuf), errno, 0 ); 
00066                      break;
00067               }
00068 
00069               Debug( LDAP_DEBUG_SHELL, "shell search reading line (%s)\n",
00070                   line, 0, 0 );
00071 
00072               /* ignore lines beginning with # (LDIFv1 comments) */
00073               if ( *line == '#' ) {
00074                      continue;
00075               }
00076 
00077               /* ignore lines beginning with DEBUG: */
00078               if ( strncasecmp( line, "DEBUG:", 6 ) == 0 ) {
00079                      continue;
00080               }
00081 
00082               len = strlen( line );
00083               while ( bp + len + 1 - buf > bsize ) {
00084                      size_t offset = bp - buf;
00085                      bsize += BUFSIZ;
00086                      buf = (char *) ch_realloc( buf, bsize );
00087                      bp = &buf[offset];
00088               }
00089               strcpy( bp, line );
00090               bp += len;
00091 
00092               /* line marked the end of an entry or result */
00093               if ( *line == '\n' ) {
00094                      if ( strncasecmp( buf, "RESULT", 6 ) == 0 ) {
00095                             break;
00096                      }
00097 
00098                      if ( (rs->sr_entry = str2entry( buf )) == NULL ) {
00099                             Debug( LDAP_DEBUG_ANY, "str2entry(%s) failed\n",
00100                                 buf, 0, 0 );
00101                      } else {
00102                             rs->sr_attrs = op->oq_search.rs_attrs;
00103                             rs->sr_flags = REP_ENTRY_MODIFIABLE;
00104                             send_search_entry( op, rs );
00105                             entry_free( rs->sr_entry );
00106                             rs->sr_attrs = NULL;
00107                      }
00108 
00109                      bp = buf;
00110               }
00111        }
00112        (void) str2result( buf, &rs->sr_err, (char **)&rs->sr_matched, (char **)&rs->sr_text );
00113 
00114        /* otherwise, front end will send this result */
00115        if ( rs->sr_err != 0 || op->o_tag != LDAP_REQ_BIND ) {
00116               send_ldap_result( op, rs );
00117        }
00118 
00119        free( buf );
00120 
00121        return( rs->sr_err );
00122 }
00123 
00124 void
00125 print_suffixes(
00126     FILE      *fp,
00127     Backend   *be
00128 )
00129 {
00130        int    i;
00131 
00132        for ( i = 0; be->be_suffix[i].bv_val != NULL; i++ ) {
00133               fprintf( fp, "suffix: %s\n", be->be_suffix[i].bv_val );
00134        }
00135 }