]> git.sur5r.net Git - openldap/blob - args.c
c5685817efc9587dac665a5ad150da4c991c5d40
[openldap] / args.c
1 /* $OpenLDAP$ */
2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
3  *
4  * Copyright 1998-2007 The OpenLDAP Foundation.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted only as authorized by the OpenLDAP
9  * Public License.
10  *
11  * A copy of this license is available in file LICENSE in the
12  * top-level directory of the distribution or, alternatively, at
13  * <http://www.OpenLDAP.org/license.html>.
14  */
15 /* Portions Copyright (c) 1996 Regents of the University of Michigan.
16  * All rights reserved.
17  *
18  * Redistribution and use in source and binary forms are permitted
19  * provided that this notice is preserved and that due credit is given
20  * to the University of Michigan at Ann Arbor. The name of the University
21  * may not be used to endorse or promote products derived from this
22  * software without specific prior written permission. This software
23  * is provided ``as is'' without express or implied warranty.
24  */
25 /* ACKNOWLEDGEMENTS:
26  * This work was originally developed by the University of Michigan
27  * (as part of U-MICH LDAP).
28  */
29
30 /*
31  * args.c - process command-line arguments, and set appropriate globals.
32  */
33
34 #include "portable.h"
35
36 #include <stdio.h>
37
38 #include <ac/stdlib.h>
39 #include <ac/string.h>
40 #include <ac/time.h>
41 #include <ac/unistd.h>
42
43 #include <ldap.h>
44 #include <lutil.h>
45
46 #include "slurp.h"
47 #include "globals.h"
48
49
50 static void
51 usage( char *name )
52 {
53     fprintf( stderr, "usage: %s\t[-d debug-level] [-s syslog-level]\n", name );
54     fprintf( stderr, "\t\t[-f slapd-config-file] [-r replication-log-file]\n" );
55     fprintf( stderr, "\t\t[-t tmp-dir] [-o]\n" );
56     fprintf( stderr, "\t\t[-n service-name]\n" );
57 }
58
59
60
61 /*
62  * Interpret argv, and fill in any appropriate globals.
63  */
64 int
65 doargs(
66     int         argc,
67     char        **argv,
68     Globals     *g
69 )
70 {
71     int         i;
72     int         rflag = 0;
73
74     g->myname = strdup( lutil_progname( "slurpd", argc, argv ));
75
76     while ( (i = getopt( argc, argv, "d:f:n:or:t:V" )) != EOF ) {
77         switch ( i ) {
78         case 'd': {     /* set debug level and 'do not detach' flag */
79             int level;
80             g->no_detach = 1;
81             if ( optarg[0] == '?' ) {
82 #ifdef LDAP_DEBUG
83                 printf( "Debug levels:\n" );
84                 printf( "\tLDAP_DEBUG_TRACE\t%d\n",
85                         LDAP_DEBUG_TRACE );
86                 printf( "\tLDAP_DEBUG_PACKETS\t%d\n",
87                         LDAP_DEBUG_PACKETS );
88                 printf( "\tLDAP_DEBUG_ARGS\t\t%d\n",
89                         LDAP_DEBUG_ARGS );
90                 printf( "\tLDAP_DEBUG_CONNS\t%d\n",
91                         LDAP_DEBUG_CONNS );
92                 printf( "\tLDAP_DEBUG_BER\t\t%d\n",
93                         LDAP_DEBUG_BER );
94                 printf( "\tLDAP_DEBUG_FILTER\t%d\n",
95                         LDAP_DEBUG_FILTER );
96                 printf( "\tLDAP_DEBUG_CONFIG\t%d\n",
97                         LDAP_DEBUG_CONFIG );
98                 printf( "\tLDAP_DEBUG_ACL\t\t%d\n",
99                         LDAP_DEBUG_ACL );
100                 printf( "\tLDAP_DEBUG_ANY\t\t%d\n",
101                         LDAP_DEBUG_ANY );
102                 puts( "\tThe -d flag also prevents slurpd from detaching." );
103 #endif /* LDAP_DEBUG */
104                 puts( "\tDebugging is disabled.  -d 0 prevents slurpd from detaching." );
105                 return( -1 );
106             }
107 #ifdef LDAP_DEBUG
108             if ( lutil_atoix( &level, optarg, 0 ) != 0 ) {
109                 fprintf( stderr, "unable to parse debug flag \"%s\".\n", optarg );
110                 usage( g->myname );
111                 return( -1 );
112             }
113             ldap_debug |= level;
114 #else /* !LDAP_DEBUG */
115             if ( lutil_atoi( &level, optarg ) != 0 || level != 0 )
116                 /* can't enable debugging - not built with debug code */
117                 fputs( "must compile with LDAP_DEBUG for debugging\n",
118                        stderr );
119 #endif /* LDAP_DEBUG */
120             } break;
121         case 'f':       /* slapd config file */
122             LUTIL_SLASHPATH( optarg );
123             g->slapd_configfile = strdup( optarg );
124             break;
125         case 'n':       /* NT service name */
126             if ( g->serverName ) free( g->serverName );
127             g->serverName = strdup( optarg );
128             break;
129         case 'o':
130             g->one_shot_mode = 1;
131             break;
132         case 'r':       /* slapd replog file */
133             LUTIL_SLASHPATH( optarg );
134                 snprintf( g->slapd_replogfile, sizeof g->slapd_replogfile,
135                         "%s", optarg );
136             rflag++;
137             break;
138         case 't': {     /* dir to use for our copies of replogs */
139                 size_t sz;
140             LUTIL_SLASHPATH( optarg );
141             g->slurpd_rdir = (char *)malloc (sz = (strlen(optarg) + sizeof(LDAP_DIRSEP "replica")));
142             snprintf(g->slurpd_rdir, sz,
143                         "%s" LDAP_DIRSEP "replica", optarg);
144             } break;
145         case 'V':
146             (g->version)++;
147             break;
148         default:
149             usage( g->myname );
150             return( -1 );
151         }
152     }
153
154     if ( g->one_shot_mode && !rflag ) {
155         fprintf( stderr, "If -o flag is given, -r flag must also be given.\n" );
156         usage( g->myname );
157         return( -1 );
158     }
159
160     /* Set location/name of our private copy of the slapd replog file */
161     snprintf( g->slurpd_replogfile, sizeof g->slurpd_replogfile,
162                 "%s" LDAP_DIRSEP "%s", g->slurpd_rdir,
163             DEFAULT_SLURPD_REPLOGFILE );
164
165     /* Set location/name of the slurpd status file */
166     snprintf( g->slurpd_status_file, sizeof g->slurpd_status_file,
167                 "%s" LDAP_DIRSEP "%s", g->slurpd_rdir,
168             DEFAULT_SLURPD_STATUS_FILE );
169
170         ber_set_option(NULL, LBER_OPT_DEBUG_LEVEL, &ldap_debug);
171         ldap_set_option(NULL, LDAP_OPT_DEBUG_LEVEL, &ldap_debug);
172         ldif_debug = ldap_debug;
173
174 #ifdef LOG_LOCAL4
175     openlog( g->myname, OPENLOG_OPTIONS, LOG_LOCAL4 );
176 #elif LOG_DEBUG
177     openlog( g->myname, OPENLOG_OPTIONS );
178 #endif
179
180     return 0;
181 }