]> git.sur5r.net Git - openldap/blob - servers/slapd/tools/slapadd.c
Add mra.o to linked objects
[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 "slapcommon.h"
18
19 int
20 main( int argc, char **argv )
21 {
22         char            *buf;
23         int         lineno;
24         int         lmax;
25         int                     rc = EXIT_SUCCESS;
26
27         slap_tool_init( "slapadd", SLAPADD, argc, argv );
28
29         if( !be->be_entry_open ||
30                 !be->be_entry_close ||
31                 !be->be_entry_put )
32         {
33                 fprintf( stderr, "%s: database doesn't support necessary operations.\n",
34                         progname );
35                 exit( EXIT_FAILURE );
36         }
37
38         buf = NULL;
39         lmax = 0;
40         lineno = 0;
41
42         if( be->be_entry_open( be, 1 ) != 0 ) {
43                 fprintf( stderr, "%s: could not open database.\n",
44                         progname );
45                 exit( EXIT_FAILURE );
46         }
47
48         while( ldif_read_record( ldiffp, &lineno, &buf, &lmax ) ) {
49                 ID id;
50                 Entry *e = str2entry( buf );
51
52                 if( e == NULL ) {
53                         fprintf( stderr, "%s: could not parse entry (line=%d)\n",
54                                 progname, lineno );
55                         rc = EXIT_FAILURE;
56                         if( continuemode ) continue;
57                         break;
58                 }
59
60                 /* make sure the DN is valid */
61                 if( dn_normalize( e->e_ndn ) == NULL ) {
62                         fprintf( stderr, "%s: invalid dn=\"%s\" (line=%d)\n",
63                                 progname, e->e_dn, lineno );
64                         rc = EXIT_FAILURE;
65                         entry_free( e );
66                         if( continuemode ) continue;
67                         break;
68                 }
69
70                 /* make sure the DN is not empty */
71                 if( e->e_ndn == '\0' ) {
72                         fprintf( stderr, "%s: empty dn=\"%s\" (line=%d)\n",
73                                 progname, e->e_dn, lineno );
74                         rc = EXIT_FAILURE;
75                         entry_free( e );
76                         if( continuemode ) continue;
77                         break;
78                 }
79
80                 /* check backend */
81                 if( select_backend( e->e_ndn ) != be ) {
82                         fprintf( stderr, "%s: database (%s) not configured to "
83                                 "hold dn=\"%s\" (line=%d)\n",
84                                 progname,
85                                 be ? be->be_suffix[0] : "<none>",
86                                 e->e_dn, lineno );
87                         rc = EXIT_FAILURE;
88                         entry_free( e );
89                         if( continuemode ) continue;
90                         break;
91                 }
92
93                 if( global_schemacheck ) {
94                         /* check schema */
95                         const char *text;
96                         if ( entry_schema_check( e, NULL, &text ) != LDAP_SUCCESS ) {
97                                 fprintf( stderr, "%s: dn=\"%s\" (line=%d): %s\n",
98                                         progname, e->e_dn, lineno, text );
99                                 rc = EXIT_FAILURE;
100                                 entry_free( e );
101                                 if( continuemode ) continue;
102                                 break;
103                         }
104                 }
105
106                 id = be->be_entry_put( be, e );
107                 if( id == NOID ) {
108                         fprintf( stderr, "%s: could not add entry dn=\"%s\" (line=%d)\n",
109                                 progname, e->e_dn, lineno );
110                         rc = EXIT_FAILURE;
111                         entry_free( e );
112                         if( continuemode ) continue;
113                         break;
114
115                 }
116                 
117                 if ( verbose ) {
118                         fprintf( stderr, "added: \"%s\" (%08lx)\n",
119                                 e->e_dn, (long) id );
120                 }
121
122                 entry_free( e );
123         }
124
125         ch_free( buf );
126
127         be->be_entry_close( be );
128
129         if( be->be_sync ) {
130                 be->be_sync( be );
131         }
132
133         slap_tool_destroy();
134         return rc;
135 }