]> git.sur5r.net Git - openldap/blob - servers/slapd/back-shell/modify.c
f0e4a4224e0c0fa1819a6233dae1e3084827c2af
[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-2017 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 #include "ldif.h"
41
42 int
43 shell_back_modify(
44     Operation   *op,
45     SlapReply   *rs )
46 {
47         Modification *mod;
48         struct shellinfo        *si = (struct shellinfo *) op->o_bd->be_private;
49         AttributeDescription *entry = slap_schema.si_ad_entry;
50         Modifications *ml  = op->orm_modlist;
51         Entry e;
52         FILE                    *rfp, *wfp;
53         int                     i;
54
55         if ( si->si_modify == NULL ) {
56                 send_ldap_error( op, rs, LDAP_UNWILLING_TO_PERFORM,
57                     "modify not implemented" );
58                 return( -1 );
59         }
60
61         e.e_id = NOID;
62         e.e_name = op->o_req_dn;
63         e.e_nname = op->o_req_ndn;
64         e.e_attrs = NULL;
65         e.e_ocflags = 0;
66         e.e_bv.bv_len = 0;
67         e.e_bv.bv_val = NULL;
68         e.e_private = NULL;
69
70         if ( ! access_allowed( op, &e,
71                 entry, NULL, ACL_WRITE, NULL ) )
72         {
73                 send_ldap_error( op, rs, LDAP_INSUFFICIENT_ACCESS, NULL );
74                 return -1;
75         }
76
77         if ( forkandexec( si->si_modify, &rfp, &wfp ) == (pid_t)-1 ) {
78                 send_ldap_error( op, rs, LDAP_OTHER,
79                     "could not fork/exec" );
80                 return( -1 );
81         }
82
83         /* write out the request to the modify process */
84         fprintf( wfp, "MODIFY\n" );
85         fprintf( wfp, "msgid: %ld\n", (long) op->o_msgid );
86         print_suffixes( wfp, op->o_bd );
87         fprintf( wfp, "dn: %s\n", op->o_req_dn.bv_val );
88         for ( ; ml != NULL; ml = ml->sml_next ) {
89                 mod = &ml->sml_mod;
90
91                 switch ( mod->sm_op ) {
92                 case LDAP_MOD_ADD:
93                         fprintf( wfp, "add: %s\n", mod->sm_desc->ad_cname.bv_val );
94                         break;
95
96                 case LDAP_MOD_DELETE:
97                         fprintf( wfp, "delete: %s\n", mod->sm_desc->ad_cname.bv_val );
98                         break;
99
100                 case LDAP_MOD_REPLACE:
101                         fprintf( wfp, "replace: %s\n", mod->sm_desc->ad_cname.bv_val );
102                         break;
103                 }
104
105                 if( mod->sm_values != NULL ) {
106                         for ( i = 0; mod->sm_values[i].bv_val != NULL; i++ ) {
107                                 char *out = ldif_put( LDIF_PUT_VALUE,
108                                         mod->sm_desc->ad_cname.bv_val,
109                                         mod->sm_values[i].bv_val,
110                                         mod->sm_values[i].bv_len );
111                                 if ( out ) {
112                                         fprintf( wfp, "%s", out );
113                                         ber_memfree( out );
114                                 }
115                         }
116                 }
117
118                 fprintf( wfp, "-\n" );
119         }
120         fclose( wfp );
121
122         /* read in the results and send them along */
123         read_and_send_results( op, rs, rfp );
124         fclose( rfp );
125         return( 0 );
126 }