| 1 |
/* result.c - sock backend result reading function */ |
| 2 |
/* $OpenLDAP$ */ |
| 3 |
/* This work is part of OpenLDAP Software <http://www.openldap.org/>. |
| 4 |
* |
| 5 |
* Copyright 2007-2024 The OpenLDAP Foundation. |
| 6 |
* All rights reserved. |
| 7 |
* |
| 8 |
* Redistribution and use in source and binary forms, with or without |
| 9 |
* modification, are permitted only as authorized by the OpenLDAP |
| 10 |
* Public License. |
| 11 |
* |
| 12 |
* A copy of this license is available in the file LICENSE in the |
| 13 |
* top-level directory of the distribution or, alternatively, at |
| 14 |
* <http://www.OpenLDAP.org/license.html>. |
| 15 |
*/ |
| 16 |
/* ACKNOWLEDGEMENTS: |
| 17 |
* This work was initially developed by Brian Candler for inclusion |
| 18 |
* in OpenLDAP Software. |
| 19 |
*/ |
| 20 |
|
| 21 |
#include "portable.h" |
| 22 |
|
| 23 |
#include <stdio.h> |
| 24 |
|
| 25 |
#include <ac/errno.h> |
| 26 |
#include <ac/string.h> |
| 27 |
#include <ac/socket.h> |
| 28 |
#include <ac/unistd.h> |
| 29 |
|
| 30 |
#include "slap.h" |
| 31 |
#include "back-sock.h" |
| 32 |
|
| 33 |
/* |
| 34 |
* FIXME: make a RESULT section compulsory from the socket response. |
| 35 |
* Otherwise, a partial/aborted response is treated as 'success'. |
| 36 |
* This is a divergence from the back-shell protocol, but makes things |
| 37 |
* more robust. |
| 38 |
*/ |
| 39 |
|
| 40 |
int |
| 41 |
sock_read_and_send_results( |
| 42 |
Operation *op, |
| 43 |
SlapReply *rs, |
| 44 |
FILE *fp ) |
| 45 |
{ |
| 46 |
int bsize, len; |
| 47 |
char *buf, *bp; |
| 48 |
char line[BUFSIZ]; |
| 49 |
char ebuf[128]; |
| 50 |
|
| 51 |
(void) fflush(fp); |
| 52 |
/* read in the result and send it along */ |
| 53 |
buf = (char *) ch_malloc( BUFSIZ ); |
| 54 |
buf[0] = '\0'; |
| 55 |
bsize = BUFSIZ; |
| 56 |
bp = buf; |
| 57 |
while ( !feof(fp) ) { |
| 58 |
errno = 0; |
| 59 |
if ( fgets( line, sizeof(line), fp ) == NULL ) { |
| 60 |
int saved_errno = errno; |
| 61 |
if ( errno == EINTR ) continue; |
| 62 |
|
| 63 |
Debug( LDAP_DEBUG_ANY, "sock: fgets failed: %s (%d)\n", |
| 64 |
AC_STRERROR_R(saved_errno, ebuf, sizeof ebuf), saved_errno ); |
| 65 |
break; |
| 66 |
} |
| 67 |
|
| 68 |
Debug( LDAP_DEBUG_SHELL, "sock search reading line (%s)\n", |
| 69 |
line ); |
| 70 |
|
| 71 |
/* ignore lines beginning with # (LDIFv1 comments) */ |
| 72 |
if ( *line == '#' ) { |
| 73 |
continue; |
| 74 |
} |
| 75 |
|
| 76 |
/* ignore lines beginning with DEBUG: */ |
| 77 |
if ( strncasecmp( line, "DEBUG:", 6 ) == 0 ) { |
| 78 |
continue; |
| 79 |
} |
| 80 |
|
| 81 |
if ( strncasecmp( line, "CONTINUE", 8 ) == 0 ) { |
| 82 |
struct sockinfo *si = (struct sockinfo *) op->o_bd->be_private; |
| 83 |
/* Only valid when operating as an overlay! */ |
| 84 |
assert( si->si_ops != 0 ); |
| 85 |
rs->sr_err = SLAP_CB_CONTINUE; |
| 86 |
goto skip; |
| 87 |
} |
| 88 |
|
| 89 |
len = strlen( line ); |
| 90 |
while ( bp + len + 1 - buf > bsize ) { |
| 91 |
size_t offset = bp - buf; |
| 92 |
bsize += BUFSIZ; |
| 93 |
buf = (char *) ch_realloc( buf, bsize ); |
| 94 |
bp = &buf[offset]; |
| 95 |
} |
| 96 |
strcpy( bp, line ); |
| 97 |
bp += len; |
| 98 |
|
| 99 |
/* line marked the end of an entry or result */ |
| 100 |
if ( *line == '\n' ) { |
| 101 |
if ( strncasecmp( buf, "RESULT", 6 ) == 0 ) { |
| 102 |
break; |
| 103 |
} |
| 104 |
|
| 105 |
if ( (rs->sr_entry = str2entry( buf )) == NULL ) { |
| 106 |
Debug( LDAP_DEBUG_ANY, "str2entry(%s) failed\n", |
| 107 |
buf ); |
| 108 |
} else { |
| 109 |
rs->sr_attrs = op->oq_search.rs_attrs; |
| 110 |
rs->sr_flags = REP_ENTRY_MODIFIABLE; |
| 111 |
send_search_entry( op, rs ); |
| 112 |
entry_free( rs->sr_entry ); |
| 113 |
rs->sr_attrs = NULL; |
| 114 |
} |
| 115 |
|
| 116 |
bp = buf; |
| 117 |
} |
| 118 |
} |
| 119 |
(void) str2result( buf, &rs->sr_err, (char **)&rs->sr_matched, (char **)&rs->sr_text ); |
| 120 |
|
| 121 |
/* otherwise, front end will send this result */ |
| 122 |
if ( rs->sr_err != 0 || op->o_tag != LDAP_REQ_BIND ) { |
| 123 |
send_ldap_result( op, rs ); |
| 124 |
} |
| 125 |
|
| 126 |
skip: |
| 127 |
ch_free( buf ); |
| 128 |
|
| 129 |
return( rs->sr_err ); |
| 130 |
} |
| 131 |
|
| 132 |
void |
| 133 |
sock_print_suffixes( |
| 134 |
FILE *fp, |
| 135 |
Backend *be |
| 136 |
) |
| 137 |
{ |
| 138 |
int i; |
| 139 |
|
| 140 |
for ( i = 0; be->be_suffix[i].bv_val != NULL; i++ ) { |
| 141 |
fprintf( fp, "suffix: %s\n", be->be_suffix[i].bv_val ); |
| 142 |
} |
| 143 |
} |
| 144 |
|
| 145 |
void |
| 146 |
sock_print_conn( |
| 147 |
FILE *fp, |
| 148 |
Connection *conn, |
| 149 |
struct sockinfo *si |
| 150 |
) |
| 151 |
{ |
| 152 |
if ( conn == NULL ) return; |
| 153 |
|
| 154 |
if( si->si_extensions & SOCK_EXT_BINDDN ) { |
| 155 |
fprintf( fp, "binddn: %s\n", |
| 156 |
conn->c_dn.bv_len ? conn->c_dn.bv_val : "" ); |
| 157 |
} |
| 158 |
if( si->si_extensions & SOCK_EXT_PEERNAME ) { |
| 159 |
fprintf( fp, "peername: %s\n", |
| 160 |
conn->c_peer_name.bv_len ? conn->c_peer_name.bv_val : "" ); |
| 161 |
} |
| 162 |
if( si->si_extensions & SOCK_EXT_SSF ) { |
| 163 |
fprintf( fp, "ssf: %d\n", conn->c_ssf ); |
| 164 |
} |
| 165 |
if( si->si_extensions & SOCK_EXT_CONNID ) { |
| 166 |
fprintf( fp, "connid: %lu\n", conn->c_connid ); |
| 167 |
} |
| 168 |
} |