]> git.sur5r.net Git - openldap/blob - servers/slapd/tools/slapadd.c
55b536c43dcde2143c23038713e7898105602103
[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 || e->e_ndn[0] == '\0' ) {
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, is_entry_referral(e) ) != be )
82                 {
83                         fprintf( stderr, "%s: database (%s) not configured to "
84                                 "hold dn=\"%s\" (line=%d)\n",
85                                 progname,
86                                 be ? be->be_suffix[0] : "<none>",
87                                 e->e_dn, lineno );
88                         rc = EXIT_FAILURE;
89                         entry_free( e );
90                         if( continuemode ) continue;
91                         break;
92                 }
93
94                 if( global_schemacheck ) {
95                         /* check schema */
96                         const char *text;
97                         char textbuf[SLAP_TEXT_BUFLEN];
98                         size_t textlen = sizeof textbuf;
99
100                         rc = entry_schema_check( e, NULL, &text, textbuf, textlen );
101
102                         if( rc != LDAP_SUCCESS ) {
103                                 fprintf( stderr, "%s: dn=\"%s\" (line=%d): %s\n",
104                                         progname, e->e_dn, lineno, text );
105                                 rc = EXIT_FAILURE;
106                                 entry_free( e );
107                                 if( continuemode ) continue;
108                                 break;
109                         }
110                 }
111
112                 id = be->be_entry_put( be, e );
113                 if( id == NOID ) {
114                         fprintf( stderr, "%s: could not add entry dn=\"%s\" (line=%d)\n",
115                                 progname, e->e_dn, lineno );
116                         rc = EXIT_FAILURE;
117                         entry_free( e );
118                         if( continuemode ) continue;
119                         break;
120
121                 }
122                 
123                 if ( verbose ) {
124                         fprintf( stderr, "added: \"%s\" (%08lx)\n",
125                                 e->e_dn, (long) id );
126                 }
127
128                 entry_free( e );
129         }
130
131         ch_free( buf );
132
133         be->be_entry_close( be );
134
135         if( be->be_sync ) {
136                 be->be_sync( be );
137         }
138
139         slap_tool_destroy();
140         return rc;
141 }