]> git.sur5r.net Git - openldap/blob - tests/progs/slapd-modify.c
449d1fc19e3c23caec86325dd1b8a31e7ae2de32
[openldap] / tests / progs / slapd-modify.c
1 /* $OpenLDAP$ */
2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
3  *
4  * Copyright 1999-2004 The OpenLDAP Foundation.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted only as authorized by the OpenLDAP
9  * Public License.
10  *
11  * A copy of this license is available in file LICENSE in the
12  * top-level directory of the distribution or, alternatively, at
13  * <http://www.OpenLDAP.org/license.html>.
14  */
15
16 #include "portable.h"
17
18 #include <stdio.h>
19
20 #include <ac/stdlib.h>
21
22 #include <ac/ctype.h>
23 #include <ac/param.h>
24 #include <ac/socket.h>
25 #include <ac/string.h>
26 #include <ac/unistd.h>
27 #include <ac/wait.h>
28
29 #define LDAP_DEPRECATED 1
30 #include <ldap.h>
31
32 #define LOOPS   100
33
34 static void
35 do_modify( char *uri, char *host, int port, char *manager, char *passwd, char *entry, 
36                 char *attr, char *value, int maxloop );
37
38
39 static void
40 usage( char *name )
41 {
42         fprintf( stderr, "usage: %s [-h <host>] -p port -D <managerDN> -w <passwd> -e <entry> [-l <loops>]\n",
43                         name );
44         exit( EXIT_FAILURE );
45 }
46
47 int
48 main( int argc, char **argv )
49 {
50         int             i;
51         char            *uri = NULL;
52         char        *host = "localhost";
53         int                     port = -1;
54         char            *manager = NULL;
55         char            *passwd = NULL;
56         char            *entry = NULL;
57         char            *ava = NULL;
58         char            *value = NULL;
59         int                     loops = LOOPS;
60
61         while ( (i = getopt( argc, argv, "H:h:p:D:w:e:a:l:" )) != EOF ) {
62                 switch( i ) {
63                         case 'H':               /* the server uri */
64                                 uri = strdup( optarg );
65                         break;
66                         case 'h':               /* the servers host */
67                                 host = strdup( optarg );
68                         break;
69
70                         case 'p':               /* the servers port */
71                                 port = atoi( optarg );
72                         break;
73                         case 'D':               /* the servers manager */
74                                 manager = strdup( optarg );
75                         break;
76
77                         case 'w':               /* the server managers password */
78                                 passwd = strdup( optarg );
79                         break;
80                         case 'e':               /* entry to modify */
81                                 entry = strdup( optarg );
82                         break;
83                         case 'a':
84                                 ava = strdup( optarg );
85                         break;
86                         case 'l':               /* the number of loops */
87                                 loops = atoi( optarg );
88                         break;
89
90                         default:
91                                 usage( argv[0] );
92                         break;
93                 }
94         }
95
96         if (( entry == NULL ) || ( ava == NULL ) || ( port == -1 && uri == NULL ))
97                 usage( argv[0] );
98
99         if ( *entry == '\0' ) {
100
101                 fprintf( stderr, "%s: invalid EMPTY entry DN.\n",
102                                 argv[0] );
103                 exit( EXIT_FAILURE );
104
105         }
106         if ( *ava  == '\0' ) {
107                 fprintf( stderr, "%s: invalid EMPTY AVA.\n",
108                                 argv[0] );
109                 exit( EXIT_FAILURE );
110         }
111         
112         if ( !( value = strchr( ava, ':' ))) {
113                 fprintf( stderr, "%s: invalid AVA.\n",
114                                 argv[0] );
115                 exit( EXIT_FAILURE );
116         }
117         *value++ = '\0'; 
118         while ( *value && isspace( (unsigned char) *value ))
119                 value++;
120
121         do_modify( uri, host, port, manager, passwd, entry, ava, value, loops );
122         exit( EXIT_SUCCESS );
123 }
124
125
126 static void
127 do_modify( char *uri, char *host, int port, char *manager,
128         char *passwd, char *entry, char* attr, char* value, int maxloop )
129 {
130         LDAP    *ld = NULL;
131         int     i;
132         pid_t   pid;
133
134         pid = getpid();
135         
136         struct ldapmod mod;
137         struct ldapmod *mods[2];
138         char *values[2] = { value, NULL };
139
140         mod.mod_op = LDAP_MOD_ADD;
141         mod.mod_type = attr;
142         mod.mod_values = values;
143         mods[0] = &mod;
144         mods[1] = NULL;
145
146         
147         if ( uri ) {
148                 ldap_initialize( &ld, uri );
149         } else {
150                 ld = ldap_init( host, port );
151         }
152         if ( ld == NULL ) {
153                 perror( "ldap_init" );
154                 exit( EXIT_FAILURE );
155         }
156
157         {
158                 int version = LDAP_VERSION3;
159                 (void) ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION,
160                         &version ); 
161         }
162
163         if ( ldap_bind_s( ld, manager, passwd, LDAP_AUTH_SIMPLE ) != LDAP_SUCCESS ) {
164                 ldap_perror( ld, "ldap_bind" );
165                  exit( EXIT_FAILURE );
166         }
167
168
169         fprintf( stderr, "PID=%ld - Modify(%d): entry=\"%s\".\n",
170                  (long) pid, maxloop, entry );
171
172         for ( i = 0; i < maxloop; i++ ) {
173                 int         rc;
174
175                 mod.mod_op = LDAP_MOD_ADD;
176                 if (( rc = ldap_modify_s( ld, entry, mods )) != LDAP_SUCCESS ) {
177                         ldap_perror( ld, "ldap_modify" );
178                         if ( rc != LDAP_NO_SUCH_OBJECT ) break;
179                         continue;
180                 }
181                 
182                 mod.mod_op = LDAP_MOD_DELETE;
183                 if (( rc = ldap_modify_s( ld, entry, mods )) != LDAP_SUCCESS ) {
184                         ldap_perror( ld, "ldap_modify" );
185                         if ( rc != LDAP_NO_SUCH_OBJECT ) break;
186                         continue;
187                 }
188
189         }
190
191         fprintf( stderr, " PID=%ld - Modify done.\n", (long) pid );
192
193         ldap_unbind( ld );
194 }
195
196