1 /*	$NetBSD: modify.c,v 1.1.1.3 2010/12/12 15:23:22 adam Exp $	*/
2 
3 /* modify.c - sock backend modify function */
4 /* OpenLDAP: pkg/ldap/servers/slapd/back-sock/modify.c,v 1.3.2.3 2010/04/13 20:23:41 kurt Exp */
5 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
6  *
7  * Copyright 2007-2010 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 
33 int
34 sock_back_modify(
35     Operation	*op,
36     SlapReply	*rs )
37 {
38 	Modification *mod;
39 	struct sockinfo	*si = (struct sockinfo *) op->o_bd->be_private;
40 	AttributeDescription *entry = slap_schema.si_ad_entry;
41 	Modifications *ml  = op->orm_modlist;
42 	Entry e;
43 	FILE			*fp;
44 	int			i;
45 
46 	e.e_id = NOID;
47 	e.e_name = op->o_req_dn;
48 	e.e_nname = op->o_req_ndn;
49 	e.e_attrs = NULL;
50 	e.e_ocflags = 0;
51 	e.e_bv.bv_len = 0;
52 	e.e_bv.bv_val = NULL;
53 	e.e_private = NULL;
54 
55 	if ( ! access_allowed( op, &e,
56 		entry, NULL, ACL_WRITE, NULL ) )
57 	{
58 		send_ldap_error( op, rs, LDAP_INSUFFICIENT_ACCESS, NULL );
59 		return -1;
60 	}
61 
62 	if ( (fp = opensock( si->si_sockpath )) == NULL ) {
63 		send_ldap_error( op, rs, LDAP_OTHER,
64 		    "could not open socket" );
65 		return( -1 );
66 	}
67 
68 	/* write out the request to the modify process */
69 	fprintf( fp, "MODIFY\n" );
70 	fprintf( fp, "msgid: %ld\n", (long) op->o_msgid );
71 	sock_print_conn( fp, op->o_conn, si );
72 	sock_print_suffixes( fp, op->o_bd );
73 	fprintf( fp, "dn: %s\n", op->o_req_dn.bv_val );
74 	for ( ; ml != NULL; ml = ml->sml_next ) {
75 		mod = &ml->sml_mod;
76 
77 		/* FIXME: should use LDIF routines to deal with binary data */
78 
79 		switch ( mod->sm_op ) {
80 		case LDAP_MOD_ADD:
81 			fprintf( fp, "add: %s\n", mod->sm_desc->ad_cname.bv_val );
82 			break;
83 
84 		case LDAP_MOD_DELETE:
85 			fprintf( fp, "delete: %s\n", mod->sm_desc->ad_cname.bv_val );
86 			break;
87 
88 		case LDAP_MOD_REPLACE:
89 			fprintf( fp, "replace: %s\n", mod->sm_desc->ad_cname.bv_val );
90 			break;
91 		}
92 
93 		if( mod->sm_values != NULL ) {
94 			for ( i = 0; mod->sm_values[i].bv_val != NULL; i++ ) {
95 				fprintf( fp, "%s: %s\n", mod->sm_desc->ad_cname.bv_val,
96 					mod->sm_values[i].bv_val /* binary! */ );
97 			}
98 		}
99 
100 		fprintf( fp, "-\n" );
101 	}
102 	fprintf( fp, "\n" );
103 
104 	/* read in the results and send them along */
105 	sock_read_and_send_results( op, rs, fp );
106 	fclose( fp );
107 	return( 0 );
108 }
109