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