modify.c revision 1.1.1.4
1/*	$NetBSD: modify.c,v 1.1.1.4 2014/05/28 09:58:51 tron Exp $	*/
2
3/* modify.c - sock backend modify function */
4/* $OpenLDAP$ */
5/* This work is part of OpenLDAP Software <http://www.openldap.org/>.
6 *
7 * Copyright 2007-2014 The OpenLDAP Foundation.
8 * All rights reserved.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted only as authorized by the OpenLDAP
12 * Public License.
13 *
14 * A copy of this license is available in the file LICENSE in the
15 * top-level directory of the distribution or, alternatively, at
16 * <http://www.OpenLDAP.org/license.html>.
17 */
18/* ACKNOWLEDGEMENTS:
19 * This work was initially developed by Brian Candler for inclusion
20 * in OpenLDAP Software.
21 */
22
23#include "portable.h"
24
25#include <stdio.h>
26
27#include <ac/string.h>
28#include <ac/socket.h>
29
30#include "slap.h"
31#include "back-sock.h"
32#include "ldif.h"
33
34int
35sock_back_modify(
36    Operation	*op,
37    SlapReply	*rs )
38{
39	Modification *mod;
40	struct sockinfo	*si = (struct sockinfo *) op->o_bd->be_private;
41	AttributeDescription *entry = slap_schema.si_ad_entry;
42	Modifications *ml  = op->orm_modlist;
43	Entry e;
44	FILE			*fp;
45	int			i;
46
47	e.e_id = NOID;
48	e.e_name = op->o_req_dn;
49	e.e_nname = op->o_req_ndn;
50	e.e_attrs = NULL;
51	e.e_ocflags = 0;
52	e.e_bv.bv_len = 0;
53	e.e_bv.bv_val = NULL;
54	e.e_private = NULL;
55
56	if ( ! access_allowed( op, &e,
57		entry, NULL, ACL_WRITE, NULL ) )
58	{
59		send_ldap_error( op, rs, LDAP_INSUFFICIENT_ACCESS, NULL );
60		return -1;
61	}
62
63	if ( (fp = opensock( si->si_sockpath )) == NULL ) {
64		send_ldap_error( op, rs, LDAP_OTHER,
65		    "could not open socket" );
66		return( -1 );
67	}
68
69	/* write out the request to the modify process */
70	fprintf( fp, "MODIFY\n" );
71	fprintf( fp, "msgid: %ld\n", (long) op->o_msgid );
72	sock_print_conn( fp, op->o_conn, si );
73	sock_print_suffixes( fp, op->o_bd );
74	fprintf( fp, "dn: %s\n", op->o_req_dn.bv_val );
75	for ( ; ml != NULL; ml = ml->sml_next ) {
76		mod = &ml->sml_mod;
77
78		switch ( mod->sm_op ) {
79		case LDAP_MOD_ADD:
80			fprintf( fp, "add: %s\n", mod->sm_desc->ad_cname.bv_val );
81			break;
82
83		case LDAP_MOD_DELETE:
84			fprintf( fp, "delete: %s\n", mod->sm_desc->ad_cname.bv_val );
85			break;
86
87		case LDAP_MOD_REPLACE:
88			fprintf( fp, "replace: %s\n", mod->sm_desc->ad_cname.bv_val );
89			break;
90		}
91
92		if( mod->sm_values != NULL ) {
93			for ( i = 0; mod->sm_values[i].bv_val != NULL; i++ ) {
94				char *text = ldif_put_wrap( LDIF_PUT_VALUE,
95					mod->sm_desc->ad_cname.bv_val,
96					mod->sm_values[i].bv_val,
97					mod->sm_values[i].bv_len, LDIF_LINE_WIDTH_MAX );
98				if ( text ) {
99					fprintf( fp, "%s", text );
100					ber_memfree( text );
101				} else {
102					break;
103				}
104			}
105		}
106
107		fprintf( fp, "-\n" );
108	}
109	fprintf( fp, "\n" );
110
111	/* read in the results and send them along */
112	sock_read_and_send_results( op, rs, fp );
113	fclose( fp );
114	return( 0 );
115}
116