]> git.sur5r.net Git - openldap/blob - servers/slapd/tools/slapcommon.c
Make -d levels additive
[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", tool );
96                 exit( EXIT_FAILURE );
97         }
98
99         ldiffile = NULL;
100         conffile = SLAPD_DEFAULT_CONFIGFILE;
101         dbnum = -1;
102         while ( (i = getopt( argc, argv, options )) != EOF ) {
103                 switch ( i ) {
104                 case 'b':
105                         base = strdup( optarg );
106
107                 case 'd':       /* turn on debugging */
108                         ldap_debug += atoi( optarg );
109                         break;
110
111                 case 'f':       /* specify a conf file */
112                         conffile = strdup( optarg );
113                         break;
114
115                 case 'l':       /* LDIF file */
116                         ldiffile = strdup( optarg );
117                         break;
118
119                 case 'n':       /* which config file db to index */
120                         dbnum = atoi( optarg ) - 1;
121                         break;
122
123                 case 't':       /* turn on truncate */
124                         truncatemode++;
125                         mode |= SLAP_TRUNCATE_MODE;
126                         break;
127
128                 case 'v':       /* turn on verbose */
129                         verbose++;
130                         break;
131
132                 default:
133                         usage( tool );
134                         break;
135                 }
136         }
137
138         if ( ( argc != optind + (tool == SLAPINDEX ? 1 : 0) )
139                 || (dbnum >= 0 && base != NULL ) )
140         {
141                 usage( tool );
142         }
143
144         if ( ldiffile == NULL ) {
145                 ldiffp = tool == SLAPCAT ? stdout : stdin;
146
147         } else if( (ldiffp = fopen( ldiffile, tool == SLAPCAT ? "w" : "r" ))
148                 == NULL )
149         {
150                 perror( ldiffile );
151                 exit( EXIT_FAILURE );
152         }
153
154         /*
155          * initialize stuff and figure out which backend we're dealing with
156          */
157
158         rc = slap_init( mode, progname );
159
160         if (rc != 0 ) {
161                 fprintf( stderr, "%s: slap_init failed!\n", progname );
162                 exit( EXIT_FAILURE );
163         }
164
165         read_config( conffile );
166
167         if ( !nbackends ) {
168                 fprintf( stderr, "No databases found in config file\n" );
169                 exit( EXIT_FAILURE );
170         }
171
172         if( base != NULL ) {
173                 char *tbase = ch_strdup( base );
174
175                 if( dn_normalize_case( tbase ) == NULL ) {
176                         fprintf( stderr, "%s: slap_init invalid suffix (\"%s\")\n",
177                                 progname, base );
178                         exit( EXIT_FAILURE );
179                 }
180
181                 be = select_backend( tbase );
182                 free( tbase );
183
184                 if( be == NULL ) {
185                         fprintf( stderr, "%s: slap_init no backend for \"%s\"\n",
186                                 progname, base );
187                         exit( EXIT_FAILURE );
188                 }
189
190         } else if ( dbnum == -1 ) {
191                 be = &backends[dbnum=0];
192
193         } else if ( dbnum < 0 || dbnum > (nbackends-1) ) {
194                 fprintf( stderr,
195                         "Database number selected via -n is out of range\n"
196                         "Must be in the range 1 to %d (number of databases in the config file)\n",
197                         nbackends );
198                 exit( EXIT_FAILURE );
199
200         } else {
201                 be = &backends[dbnum];
202         }
203 }