]> git.sur5r.net Git - openldap/blob - servers/slapd/tools/slapadd.c
C portability from HEAD
[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                 Entry *e = str2entry( buf );
56                 struct berval bvtext;
57
58                 bvtext.bv_len = textlen;
59                 bvtext.bv_val = textbuf;
60
61                 if( e == NULL ) {
62                         fprintf( stderr, "%s: could not parse entry (line=%d)\n",
63                                 progname, lineno );
64                         rc = EXIT_FAILURE;
65                         if( continuemode ) continue;
66                         break;
67                 }
68
69                 /* make sure the DN is not empty */
70                 if( !e->e_nname.bv_len ) {
71                         fprintf( stderr, "%s: empty dn=\"%s\" (line=%d)\n",
72                                 progname, e->e_dn, lineno );
73                         rc = EXIT_FAILURE;
74                         entry_free( e );
75                         if( continuemode ) continue;
76                         break;
77                 }
78
79                 /* check backend */
80                 if( select_backend( &e->e_nname, is_entry_referral(e), nosubordinates )
81                         != be )
82                 {
83                         fprintf( stderr, "%s: line %d: "
84                                 "database (%s) not configured to hold \"%s\"\n",
85                                 progname, lineno,
86                                 be ? be->be_suffix[0]->bv_val : "<none>",
87                                 e->e_dn );
88                         fprintf( stderr, "%s: line %d: "
89                                 "database (%s) not configured to hold \"%s\"\n",
90                                 progname, lineno,
91                                 be ? be->be_nsuffix[0]->bv_val : "<none>",
92                                 e->e_ndn );
93                         rc = EXIT_FAILURE;
94                         entry_free( e );
95                         if( continuemode ) continue;
96                         break;
97                 }
98
99                 {
100                         Attribute *sc = attr_find( e->e_attrs,
101                                 slap_schema.si_ad_structuralObjectClass );
102                         Attribute *oc = attr_find( e->e_attrs,
103                                 slap_schema.si_ad_objectClass );
104
105                         if( oc == NULL ) {
106                                 fprintf( stderr, "%s: dn=\"%s\" (line=%d): %s\n",
107                                         progname, e->e_dn, lineno,
108                                         "no objectClass attribute");
109                                 rc = EXIT_FAILURE;
110                                 entry_free( e );
111                                 if( continuemode ) continue;
112                                 break;
113                         }
114
115                         if( sc == NULL ) {
116                                 struct berval vals[2];
117
118                                 int ret = structural_class( oc->a_vals, vals,
119                                         NULL, &text, textbuf, textlen );
120
121                                 if( vals[0].bv_len == 0 ) {
122                                         fprintf( stderr, "%s: dn=\"%s\" (line=%d): %s\n",
123                                         progname, e->e_dn, lineno, text );
124                                         rc = EXIT_FAILURE;
125                                         entry_free( e );
126                                         if( continuemode ) continue;
127                                         break;
128                                 }
129
130                                 vals[1].bv_val = NULL;
131                                 attr_merge( e, slap_schema.si_ad_structuralObjectClass,
132                                         vals );
133                         }
134                 }
135
136                 if( global_schemacheck ) {
137                         /* check schema */
138
139                         rc = entry_schema_check( be, e, NULL, &text, textbuf, textlen );
140
141                         if( rc != LDAP_SUCCESS ) {
142                                 fprintf( stderr, "%s: dn=\"%s\" (line=%d): %s\n",
143                                         progname, e->e_dn, lineno, text );
144                                 rc = EXIT_FAILURE;
145                                 entry_free( e );
146                                 if( continuemode ) continue;
147                                 break;
148                         }
149                 }
150
151                 if (!dryrun) {
152                         ID id = be->be_entry_put( be, e, &bvtext );
153                         if( id == NOID ) {
154                                 fprintf( stderr, "%s: could not add entry dn=\"%s\" (line=%d): %s\n",
155                                         progname, e->e_dn, lineno, bvtext.bv_val );
156                                 rc = EXIT_FAILURE;
157                                 entry_free( e );
158                                 if( continuemode ) continue;
159                                 break;
160                         }
161                 
162                         if ( verbose ) {
163                                 fprintf( stderr, "added: \"%s\" (%08lx)\n",
164                                         e->e_dn, (long) id );
165                         }
166                 } else {
167                         if ( verbose ) {
168                                 fprintf( stderr, "(dry) added: \"%s\"\n", e->e_dn );
169                         }
170                 }
171
172                 entry_free( e );
173         }
174
175         ch_free( buf );
176
177         be->be_entry_close( be );
178
179         if( be->be_sync ) {
180                 be->be_sync( be );
181         }
182
183         slap_tool_destroy();
184         return rc;
185 }