]> git.sur5r.net Git - openldap/blob - servers/slapd/tools/slapcommon.c
8a678bac261988e604463a5fac1e0227d83c696f
[openldap] / servers / slapd / tools / slapcommon.c
1 /*
2  * Copyright 1998-1999 The OpenLDAP Foundation, All Rights Reserved.
3  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
4  */
5 /* slapcommon.c - common routine for the slap tools */
6
7 #include "portable.h"
8
9 #include <stdio.h>
10
11 #include <ac/stdlib.h>
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 #include "lutil.h"
19
20
21 char    *progname       = NULL;
22 char    *conffile       = SLAPD_DEFAULT_CONFIGFILE;
23 int             truncatemode = 0;
24 int             verbose         = 0;
25
26 char    *ldiffile       = NULL;
27 FILE    *ldiffp         = NULL;
28
29 Backend *be = NULL;
30
31 static void
32 usage( int tool )
33 {
34         char *options = NULL;
35         fprintf( stderr,
36                 "usage: %s [-v] [-d debuglevel] [-f configfile]\n"
37                          "\t[-n databasenumber | -b suffix]", progname );
38
39         switch( tool ) {
40         case SLAPADD:
41                 options = "\t[-l ldiffile]\n";
42                 break;
43
44         case SLAPCAT:
45                 options = "\t[-l ldiffile]\n";
46                 break;
47
48         case SLAPINDEX:
49                 options = "\tattributetype\n";
50                 break;
51         }
52
53         if( options != NULL ) {
54                 fputs( options, stderr );
55         }
56         exit( EXIT_FAILURE );
57 }
58
59
60 /*
61  * slap_tool_init - initialize slap utility, handle program options.
62  * arguments:
63  *      name            program name
64  *      tool            tool code
65  *      argc, argv      command line arguments
66  */
67
68 void
69 slap_tool_init(
70         const char* name,
71         int tool,
72         int argc, char **argv )
73 {
74         char *options;
75         char *base = NULL;
76         int rc, i, dbnum;
77         int mode = SLAP_TOOL_MODE;
78
79         progname = lutil_progname( name, argc, argv );
80
81         switch( tool ) {
82         case SLAPADD:
83                 options = "b:d:f:l:n:tv";
84                 break;
85
86         case SLAPINDEX:
87                 options = "b:d:f:n:v";
88                 break;
89
90         case SLAPCAT:
91                 options = "b:d:f:l:n:v";
92                 break;
93
94         default:
95                 fprintf( stderr, "%s: unknown tool mode (%d)\n",
96                          progname, tool );
97                 exit( EXIT_FAILURE );
98         }
99
100         ldiffile = NULL;
101         conffile = SLAPD_DEFAULT_CONFIGFILE;
102         dbnum = -1;
103         while ( (i = getopt( argc, argv, options )) != EOF ) {
104                 switch ( i ) {
105                 case 'b':
106                         base = strdup( optarg );
107
108                 case 'd':       /* turn on debugging */
109                         ldap_debug += atoi( optarg );
110                         break;
111
112                 case 'f':       /* specify a conf file */
113                         conffile = strdup( optarg );
114                         break;
115
116                 case 'l':       /* LDIF file */
117                         ldiffile = strdup( optarg );
118                         break;
119
120                 case 'n':       /* which config file db to index */
121                         dbnum = atoi( optarg ) - 1;
122                         break;
123
124                 case 't':       /* turn on truncate */
125                         truncatemode++;
126                         mode |= SLAP_TRUNCATE_MODE;
127                         break;
128
129                 case 'v':       /* turn on verbose */
130                         verbose++;
131                         break;
132
133                 default:
134                         usage( tool );
135                         break;
136                 }
137         }
138
139         if ( ( argc != optind + (tool == SLAPINDEX ? 1 : 0) )
140                 || (dbnum >= 0 && base != NULL ) )
141         {
142                 usage( tool );
143         }
144
145         if ( ldiffile == NULL ) {
146                 ldiffp = tool == SLAPCAT ? stdout : stdin;
147
148         } else if( (ldiffp = fopen( ldiffile, tool == SLAPCAT ? "w" : "r" ))
149                 == NULL )
150         {
151                 perror( ldiffile );
152                 exit( EXIT_FAILURE );
153         }
154
155         /*
156          * initialize stuff and figure out which backend we're dealing with
157          */
158
159         rc = slap_init( mode, progname );
160
161         if (rc != 0 ) {
162                 fprintf( stderr, "%s: slap_init failed!\n", progname );
163                 exit( EXIT_FAILURE );
164         }
165
166         read_config( conffile );
167
168         if ( !nbackends ) {
169                 fprintf( stderr, "No databases found in config file\n" );
170                 exit( EXIT_FAILURE );
171         }
172
173         if( base != NULL ) {
174                 char *tbase = ch_strdup( base );
175
176                 if( dn_normalize_case( tbase ) == NULL ) {
177                         fprintf( stderr, "%s: slap_init invalid suffix (\"%s\")\n",
178                                 progname, base );
179                         exit( EXIT_FAILURE );
180                 }
181
182                 be = select_backend( tbase );
183                 free( tbase );
184
185                 if( be == NULL ) {
186                         fprintf( stderr, "%s: slap_init no backend for \"%s\"\n",
187                                 progname, base );
188                         exit( EXIT_FAILURE );
189                 }
190
191         } else if ( dbnum == -1 ) {
192                 be = &backends[dbnum=0];
193
194         } else if ( dbnum < 0 || dbnum > (nbackends-1) ) {
195                 fprintf( stderr,
196                         "Database number selected via -n is out of range\n"
197                         "Must be in the range 1 to %d (number of databases in the config file)\n",
198                         nbackends );
199                 exit( EXIT_FAILURE );
200
201         } else {
202                 be = &backends[dbnum];
203         }
204 }