Back to index

openldap  2.4.31
modify.c
Go to the documentation of this file.
00001 /* modify.c - shell backend modify function */
00002 /* $OpenLDAP$ */
00003 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
00004  *
00005  * Copyright 1998-2012 The OpenLDAP Foundation.
00006  * All rights reserved.
00007  *
00008  * Redistribution and use in source and binary forms, with or without
00009  * modification, are permitted only as authorized by the OpenLDAP
00010  * Public License.
00011  *
00012  * A copy of this license is available in the file LICENSE in the
00013  * top-level directory of the distribution or, alternatively, at
00014  * <http://www.OpenLDAP.org/license.html>.
00015  */
00016 /* Portions Copyright (c) 1995 Regents of the University of Michigan.
00017  * All rights reserved.
00018  *
00019  * Redistribution and use in source and binary forms are permitted
00020  * provided that this notice is preserved and that due credit is given
00021  * to the University of Michigan at Ann Arbor. The name of the University
00022  * may not be used to endorse or promote products derived from this
00023  * software without specific prior written permission. This software
00024  * is provided ``as is'' without express or implied warranty.
00025  */
00026 /* ACKNOWLEDGEMENTS:
00027  * This work was originally developed by the University of Michigan
00028  * (as part of U-MICH LDAP).
00029  */
00030 
00031 #include "portable.h"
00032 
00033 #include <stdio.h>
00034 
00035 #include <ac/string.h>
00036 #include <ac/socket.h>
00037 
00038 #include "slap.h"
00039 #include "shell.h"
00040 #include "ldif.h"
00041 
00042 int
00043 shell_back_modify(
00044     Operation *op,
00045     SlapReply *rs )
00046 {
00047        Modification *mod;
00048        struct shellinfo     *si = (struct shellinfo *) op->o_bd->be_private;
00049        AttributeDescription *entry = slap_schema.si_ad_entry;
00050        Modifications *ml  = op->orm_modlist;
00051        Entry e;
00052        FILE                 *rfp, *wfp;
00053        int                  i;
00054 
00055        if ( si->si_modify == NULL ) {
00056               send_ldap_error( op, rs, LDAP_UNWILLING_TO_PERFORM,
00057                   "modify not implemented" );
00058               return( -1 );
00059        }
00060 
00061        e.e_id = NOID;
00062        e.e_name = op->o_req_dn;
00063        e.e_nname = op->o_req_ndn;
00064        e.e_attrs = NULL;
00065        e.e_ocflags = 0;
00066        e.e_bv.bv_len = 0;
00067        e.e_bv.bv_val = NULL;
00068        e.e_private = NULL;
00069 
00070        if ( ! access_allowed( op, &e,
00071               entry, NULL, ACL_WRITE, NULL ) )
00072        {
00073               send_ldap_error( op, rs, LDAP_INSUFFICIENT_ACCESS, NULL );
00074               return -1;
00075        }
00076 
00077        if ( forkandexec( si->si_modify, &rfp, &wfp ) == (pid_t)-1 ) {
00078               send_ldap_error( op, rs, LDAP_OTHER,
00079                   "could not fork/exec" );
00080               return( -1 );
00081        }
00082 
00083        /* write out the request to the modify process */
00084        fprintf( wfp, "MODIFY\n" );
00085        fprintf( wfp, "msgid: %ld\n", (long) op->o_msgid );
00086        print_suffixes( wfp, op->o_bd );
00087        fprintf( wfp, "dn: %s\n", op->o_req_dn.bv_val );
00088        for ( ; ml != NULL; ml = ml->sml_next ) {
00089               mod = &ml->sml_mod;
00090 
00091               switch ( mod->sm_op ) {
00092               case LDAP_MOD_ADD:
00093                      fprintf( wfp, "add: %s\n", mod->sm_desc->ad_cname.bv_val );
00094                      break;
00095 
00096               case LDAP_MOD_DELETE:
00097                      fprintf( wfp, "delete: %s\n", mod->sm_desc->ad_cname.bv_val );
00098                      break;
00099 
00100               case LDAP_MOD_REPLACE:
00101                      fprintf( wfp, "replace: %s\n", mod->sm_desc->ad_cname.bv_val );
00102                      break;
00103               }
00104 
00105               if( mod->sm_values != NULL ) {
00106                      for ( i = 0; mod->sm_values[i].bv_val != NULL; i++ ) {
00107                             char *out = ldif_put( LDIF_PUT_VALUE,
00108                                    mod->sm_desc->ad_cname.bv_val,
00109                                    mod->sm_values[i].bv_val,
00110                                    mod->sm_values[i].bv_len );
00111                             if ( out ) {
00112                                    fprintf( wfp, "%s", out );
00113                                    ber_memfree( out );
00114                             }
00115                      }
00116               }
00117 
00118               fprintf( wfp, "-\n" );
00119        }
00120        fclose( wfp );
00121 
00122        /* read in the results and send them along */
00123        read_and_send_results( op, rs, rfp );
00124        fclose( rfp );
00125        return( 0 );
00126 }