]> git.sur5r.net Git - openldap/blob - servers/slapd/back-shell/modify.c
f18822b82eae6612897c8d1d6e5d9fcdd9900e8a
[openldap] / servers / slapd / back-shell / modify.c
1 /* modify.c - shell backend modify function */
2 /* $OpenLDAP$ */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4  *
5  * Copyright 1998-2008 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 /* Portions Copyright (c) 1995 Regents of the University of Michigan.
17  * All rights reserved.
18  *
19  * Redistribution and use in source and binary forms are permitted
20  * provided that this notice is preserved and that due credit is given
21  * to the University of Michigan at Ann Arbor. The name of the University
22  * may not be used to endorse or promote products derived from this
23  * software without specific prior written permission. This software
24  * is provided ``as is'' without express or implied warranty.
25  */
26 /* ACKNOWLEDGEMENTS:
27  * This work was originally developed by the University of Michigan
28  * (as part of U-MICH LDAP).
29  */
30
31 #include "portable.h"
32
33 #include <stdio.h>
34
35 #include <ac/string.h>
36 #include <ac/socket.h>
37
38 #include "slap.h"
39 #include "shell.h"
40
41 int
42 shell_back_modify(
43     Operation   *op,
44     SlapReply   *rs )
45 {
46         Modification *mod;
47         struct shellinfo        *si = (struct shellinfo *) op->o_bd->be_private;
48         AttributeDescription *entry = slap_schema.si_ad_entry;
49         Modifications *ml  = op->orm_modlist;
50         Entry e;
51         FILE                    *rfp, *wfp;
52         int                     i;
53
54         if ( si->si_modify == NULL ) {
55                 send_ldap_error( op, rs, LDAP_UNWILLING_TO_PERFORM,
56                     "modify not implemented" );
57                 return( -1 );
58         }
59
60         e.e_id = NOID;
61         e.e_name = op->o_req_dn;
62         e.e_nname = op->o_req_ndn;
63         e.e_attrs = NULL;
64         e.e_ocflags = 0;
65         e.e_bv.bv_len = 0;
66         e.e_bv.bv_val = NULL;
67         e.e_private = NULL;
68
69         if ( ! access_allowed( op, &e,
70                 entry, NULL, ACL_WRITE, NULL ) )
71         {
72                 send_ldap_error( op, rs, LDAP_INSUFFICIENT_ACCESS, NULL );
73                 return -1;
74         }
75
76         if ( forkandexec( si->si_modify, &rfp, &wfp ) == (pid_t)-1 ) {
77                 send_ldap_error( op, rs, LDAP_OTHER,
78                     "could not fork/exec" );
79                 return( -1 );
80         }
81
82         /* write out the request to the modify process */
83         fprintf( wfp, "MODIFY\n" );
84         fprintf( wfp, "msgid: %ld\n", (long) op->o_msgid );
85         print_suffixes( wfp, op->o_bd );
86         fprintf( wfp, "dn: %s\n", op->o_req_dn.bv_val );
87         for ( ; ml != NULL; ml = ml->sml_next ) {
88                 mod = &ml->sml_mod;
89
90                 /* FIXME: should use LDIF routines to deal with binary data */
91
92                 switch ( mod->sm_op ) {
93                 case LDAP_MOD_ADD:
94                         fprintf( wfp, "add: %s\n", mod->sm_desc->ad_cname.bv_val );
95                         break;
96
97                 case LDAP_MOD_DELETE:
98                         fprintf( wfp, "delete: %s\n", mod->sm_desc->ad_cname.bv_val );
99                         break;
100
101                 case LDAP_MOD_REPLACE:
102                         fprintf( wfp, "replace: %s\n", mod->sm_desc->ad_cname.bv_val );
103                         break;
104                 }
105
106                 if( mod->sm_values != NULL ) {
107                         for ( i = 0; mod->sm_values[i].bv_val != NULL; i++ ) {
108                                 fprintf( wfp, "%s: %s\n", mod->sm_desc->ad_cname.bv_val,
109                                         mod->sm_values[i].bv_val /* binary! */ );
110                         }
111                 }
112
113                 fprintf( wfp, "-\n" );
114         }
115         fclose( wfp );
116
117         /* read in the results and send them along */
118         read_and_send_results( op, rs, rfp );
119         fclose( rfp );
120         return( 0 );
121 }