]> git.sur5r.net Git - openldap/blob - servers/slapd/tools/slapadd.c
Misc cleanup, lint removal, and minor optimizations
[openldap] / servers / slapd / tools / slapadd.c
1 /* $OpenLDAP$ */
2 /*
3  * Copyright 1998-2002 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 = NULL;
26         int         lineno;
27         int         lmax;
28         int                     rc = EXIT_SUCCESS;
29
30         const char *text;
31         char textbuf[SLAP_TEXT_BUFLEN] = { '\0' };
32         size_t textlen = sizeof textbuf;
33
34         slap_tool_init( "slapadd", SLAPADD, argc, argv );
35
36         if( !be->be_entry_open ||
37                 !be->be_entry_close ||
38                 !be->be_entry_put )
39         {
40                 fprintf( stderr, "%s: database doesn't support necessary operations.\n",
41                         progname );
42                 exit( EXIT_FAILURE );
43         }
44
45         lmax = 0;
46         lineno = 0;
47
48         if( be->be_entry_open( be, 1 ) != 0 ) {
49                 fprintf( stderr, "%s: could not open database.\n",
50                         progname );
51                 exit( EXIT_FAILURE );
52         }
53
54         while( ldif_read_record( ldiffp, &lineno, &buf, &lmax ) ) {
55                 ID id;
56                 Entry *e = str2entry( buf );
57                 struct berval bvtext = { textlen, textbuf };
58
59                 if( e == NULL ) {
60                         fprintf( stderr, "%s: could not parse entry (line=%d)\n",
61                                 progname, lineno );
62                         rc = EXIT_FAILURE;
63                         if( continuemode ) continue;
64                         break;
65                 }
66
67                 /* make sure the DN is not empty */
68                 if( !e->e_nname.bv_len ) {
69                         fprintf( stderr, "%s: empty dn=\"%s\" (line=%d)\n",
70                                 progname, e->e_dn, lineno );
71                         rc = EXIT_FAILURE;
72                         entry_free( e );
73                         if( continuemode ) continue;
74                         break;
75                 }
76
77                 /* check backend */
78                 if( select_backend( &e->e_nname, is_entry_referral(e), nosubordinates )
79                         != be )
80                 {
81                         fprintf( stderr, "%s: line %d: "
82                                 "database (%s) not configured to hold \"%s\"\n",
83                                 progname, lineno,
84                                 be ? be->be_suffix[0]->bv_val : "<none>",
85                                 e->e_dn );
86                         fprintf( stderr, "%s: line %d: "
87                                 "database (%s) not configured to hold \"%s\"\n",
88                                 progname, lineno,
89                                 be ? be->be_nsuffix[0]->bv_val : "<none>",
90                                 e->e_ndn );
91                         rc = EXIT_FAILURE;
92                         entry_free( e );
93                         if( continuemode ) continue;
94                         break;
95                 }
96
97                 {
98                         Attribute *sc = attr_find( e->e_attrs,
99                                 slap_schema.si_ad_structuralObjectClass );
100                         Attribute *oc = attr_find( e->e_attrs,
101                                 slap_schema.si_ad_objectClass );
102
103                         if( oc == NULL ) {
104                                 fprintf( stderr, "%s: dn=\"%s\" (line=%d): %s\n",
105                                         progname, e->e_dn, lineno,
106                                         "no objectClass attribute");
107                                 rc = EXIT_FAILURE;
108                                 entry_free( e );
109                                 if( continuemode ) continue;
110                                 break;
111                         }
112
113                         if( sc == NULL ) {
114                                 struct berval vals[2];
115
116                                 int ret = structural_class( oc->a_vals, vals,
117                                         NULL, &text, textbuf, textlen );
118
119                                 if( vals[0].bv_len == 0 ) {
120                                         fprintf( stderr, "%s: dn=\"%s\" (line=%d): %s\n",
121                                         progname, e->e_dn, lineno, text );
122                                         rc = EXIT_FAILURE;
123                                         entry_free( e );
124                                         if( continuemode ) continue;
125                                         break;
126                                 }
127
128                                 vals[1].bv_val = NULL;
129                                 attr_merge( e, slap_schema.si_ad_structuralObjectClass,
130                                         vals );
131                         }
132                 }
133
134                 if( global_schemacheck ) {
135                         /* check schema */
136
137                         rc = entry_schema_check( be, e, NULL, &text, textbuf, textlen );
138
139                         if( rc != LDAP_SUCCESS ) {
140                                 fprintf( stderr, "%s: dn=\"%s\" (line=%d): %s\n",
141                                         progname, e->e_dn, lineno, text );
142                                 rc = EXIT_FAILURE;
143                                 entry_free( e );
144                                 if( continuemode ) continue;
145                                 break;
146                         }
147                 }
148
149                 id = be->be_entry_put( be, e, &bvtext );
150                 if( id == NOID ) {
151                         fprintf( stderr, "%s: could not add entry dn=\"%s\" (line=%d): %s\n",
152                                 progname, e->e_dn, lineno, bvtext.bv_val );
153                         rc = EXIT_FAILURE;
154                         entry_free( e );
155                         if( continuemode ) continue;
156                         break;
157
158                 }
159                 
160                 if ( verbose ) {
161                         fprintf( stderr, "added: \"%s\" (%08lx)\n",
162                                 e->e_dn, (long) id );
163                 }
164
165                 entry_free( e );
166         }
167
168         ch_free( buf );
169
170         be->be_entry_close( be );
171
172         if( be->be_sync ) {
173                 be->be_sync( be );
174         }
175
176         slap_tool_destroy();
177         return rc;
178 }