]> git.sur5r.net Git - openldap/blob - servers/slapd/tools/slapadd.c
Changed backglue configuration. Added noSubordinates arg to select_backend
[openldap] / servers / slapd / tools / slapadd.c
1 /* $OpenLDAP$ */
2 /*
3  * Copyright 1998-2000 The OpenLDAP Foundation, All Rights Reserved.
4  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
5  */
6 #include "portable.h"
7
8 #include <stdio.h>
9
10 #include <ac/stdlib.h>
11
12 #include <ac/ctype.h>
13 #include <ac/string.h>
14 #include <ac/socket.h>
15 #include <ac/unistd.h>
16
17 #include <lber.h>
18 #include <ldif.h>
19
20 #include "slapcommon.h"
21
22 int
23 main( int argc, char **argv )
24 {
25         char            *buf;
26         int         lineno;
27         int         lmax;
28         int                     rc = EXIT_SUCCESS;
29
30         slap_tool_init( "slapadd", SLAPADD, argc, argv );
31
32         if( !be->be_entry_open ||
33                 !be->be_entry_close ||
34                 !be->be_entry_put )
35         {
36                 fprintf( stderr, "%s: database doesn't support necessary operations.\n",
37                         progname );
38                 exit( EXIT_FAILURE );
39         }
40
41         buf = NULL;
42         lmax = 0;
43         lineno = 0;
44
45         if( be->be_entry_open( be, 1 ) != 0 ) {
46                 fprintf( stderr, "%s: could not open database.\n",
47                         progname );
48                 exit( EXIT_FAILURE );
49         }
50
51         while( ldif_read_record( ldiffp, &lineno, &buf, &lmax ) ) {
52                 ID id;
53                 Entry *e = str2entry( buf );
54
55                 if( e == NULL ) {
56                         fprintf( stderr, "%s: could not parse entry (line=%d)\n",
57                                 progname, lineno );
58                         rc = EXIT_FAILURE;
59                         if( continuemode ) continue;
60                         break;
61                 }
62
63                 /* make sure the DN is valid */
64                 if( dn_normalize( e->e_ndn ) == NULL || e->e_ndn[0] == '\0' ) {
65                         fprintf( stderr, "%s: invalid dn=\"%s\" (line=%d)\n",
66                                 progname, e->e_dn, lineno );
67                         rc = EXIT_FAILURE;
68                         entry_free( e );
69                         if( continuemode ) continue;
70                         break;
71                 }
72
73                 /* make sure the DN is not empty */
74                 if( e->e_ndn == '\0' ) {
75                         fprintf( stderr, "%s: empty dn=\"%s\" (line=%d)\n",
76                                 progname, e->e_dn, lineno );
77                         rc = EXIT_FAILURE;
78                         entry_free( e );
79                         if( continuemode ) continue;
80                         break;
81                 }
82
83                 /* check backend */
84                 if( select_backend( e->e_ndn, is_entry_referral(e), nosubs ) != be )
85                 {
86                         fprintf( stderr, "%s: database (%s) not configured to "
87                                 "hold dn=\"%s\" (line=%d)\n",
88                                 progname,
89                                 be ? be->be_suffix[0] : "<none>",
90                                 e->e_dn, lineno );
91                         rc = EXIT_FAILURE;
92                         entry_free( e );
93                         if( continuemode ) continue;
94                         break;
95                 }
96
97                 if( global_schemacheck ) {
98                         /* check schema */
99                         const char *text;
100                         char textbuf[SLAP_TEXT_BUFLEN];
101                         size_t textlen = sizeof textbuf;
102
103                         rc = entry_schema_check( e, NULL, &text, textbuf, textlen );
104
105                         if( rc != LDAP_SUCCESS ) {
106                                 fprintf( stderr, "%s: dn=\"%s\" (line=%d): %s\n",
107                                         progname, e->e_dn, lineno, text );
108                                 rc = EXIT_FAILURE;
109                                 entry_free( e );
110                                 if( continuemode ) continue;
111                                 break;
112                         }
113                 }
114
115                 id = be->be_entry_put( be, e );
116                 if( id == NOID ) {
117                         fprintf( stderr, "%s: could not add entry dn=\"%s\" (line=%d)\n",
118                                 progname, e->e_dn, lineno );
119                         rc = EXIT_FAILURE;
120                         entry_free( e );
121                         if( continuemode ) continue;
122                         break;
123
124                 }
125                 
126                 if ( verbose ) {
127                         fprintf( stderr, "added: \"%s\" (%08lx)\n",
128                                 e->e_dn, (long) id );
129                 }
130
131                 entry_free( e );
132         }
133
134         ch_free( buf );
135
136         be->be_entry_close( be );
137
138         if( be->be_sync ) {
139                 be->be_sync( be );
140         }
141
142         slap_tool_destroy();
143         return rc;
144 }