]> git.sur5r.net Git - openldap/blob - servers/slurpd/args.c
97dd2bd4df9c16040374d820394dcf909d19dba3
[openldap] / servers / slurpd / args.c
1 /* $OpenLDAP$ */
2 /*
3  * Copyright 1998-2003 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     fprintf( stderr, "\t\t[-n service-name]\n" );
48 }
49
50
51
52 /*
53  * Interpret argv, and fill in any appropriate globals.
54  */
55 int
56 doargs(
57     int         argc,
58     char        **argv,
59     Globals     *g
60 )
61 {
62     int         i;
63     int         rflag = 0;
64
65     if ( (g->myname = strrchr( argv[0], LDAP_DIRSEP[0] )) == NULL ) {
66         g->myname = strdup( argv[0] );
67     } else {
68         g->myname = strdup( g->myname + 1 );
69     }
70
71     while ( (i = getopt( argc, argv, "d:f:n:or:t:" )) != EOF ) {
72         switch ( i ) {
73         case 'd':       /* set debug level and 'do not detach' flag */
74             g->no_detach = 1;
75             if ( optarg[0] == '?' ) {
76 #ifdef LDAP_DEBUG
77                 printf( "Debug levels:\n" );
78                 printf( "\tLDAP_DEBUG_TRACE\t%d\n",
79                         LDAP_DEBUG_TRACE );
80                 printf( "\tLDAP_DEBUG_PACKETS\t%d\n",
81                         LDAP_DEBUG_PACKETS );
82                 printf( "\tLDAP_DEBUG_ARGS\t\t%d\n",
83                         LDAP_DEBUG_ARGS );
84                 printf( "\tLDAP_DEBUG_CONNS\t%d\n",
85                         LDAP_DEBUG_CONNS );
86                 printf( "\tLDAP_DEBUG_BER\t\t%d\n",
87                         LDAP_DEBUG_BER );
88                 printf( "\tLDAP_DEBUG_FILTER\t%d\n",
89                         LDAP_DEBUG_FILTER );
90                 printf( "\tLDAP_DEBUG_CONFIG\t%d\n",
91                         LDAP_DEBUG_CONFIG );
92                 printf( "\tLDAP_DEBUG_ACL\t\t%d\n",
93                         LDAP_DEBUG_ACL );
94                 printf( "\tLDAP_DEBUG_ANY\t\t%d\n",
95                         LDAP_DEBUG_ANY );
96                 puts( "\tThe -d flag also prevents slurpd from detaching." );
97 #endif /* LDAP_DEBUG */
98                 puts( "\tDebugging is disabled.  -d 0 prevents slurpd from detaching." );
99                 return( -1 );
100             }
101 #ifdef LDAP_DEBUG
102             ldap_debug |= atoi( optarg );
103 #else /* !LDAP_DEBUG */
104             if ( atoi( optarg ) != 0 )
105                 /* can't enable debugging - not built with debug code */
106                 fputs( "must compile with LDAP_DEBUG for debugging\n",
107                        stderr );
108 #endif /* LDAP_DEBUG */
109             break;
110         case 'f':       /* slapd config file */
111             g->slapd_configfile = strdup( optarg );
112             break;
113         case 'n':       /* NT service name *.
114             if ( g->serverName ) free( g->serverName );
115             g->serverName = strdup( optarg );
116             break;
117         case 'o':
118             g->one_shot_mode = 1;
119             break;
120         case 'r':       /* slapd replog file */
121                 snprintf( g->slapd_replogfile, sizeof g->slapd_replogfile,
122                         "%s", optarg );
123             rflag++;
124             break;
125         case 't': {     /* dir to use for our copies of replogs */
126                 size_t sz;
127             g->slurpd_rdir = (char *)malloc (sz = (strlen(optarg) + sizeof("/replica")));
128             snprintf(g->slurpd_rdir, sz,
129                         "%s" LDAP_DIRSEP "replica", optarg);
130             } break;
131         default:
132             usage( g->myname );
133             return( -1 );
134         }
135     }
136
137     if ( g->one_shot_mode && !rflag ) {
138         fprintf( stderr, "If -o flag is given, -r flag must also be given.\n" );
139         usage( g->myname );
140         return( -1 );
141     }
142
143     /* Set location/name of our private copy of the slapd replog file */
144     snprintf( g->slurpd_replogfile, sizeof g->slurpd_replogfile,
145                 "%s" LDAP_DIRSEP "%s", g->slurpd_rdir,
146             DEFAULT_SLURPD_REPLOGFILE );
147
148     /* Set location/name of the slurpd status file */
149     snprintf( g->slurpd_status_file, sizeof g->slurpd_status_file,
150                 "%s" LDAP_DIRSEP "%s", g->slurpd_rdir,
151             DEFAULT_SLURPD_STATUS_FILE );
152
153         ber_set_option(NULL, LBER_OPT_DEBUG_LEVEL, &ldap_debug);
154         ldap_set_option(NULL, LDAP_OPT_DEBUG_LEVEL, &ldap_debug);
155         ldif_debug = ldap_debug;
156
157 #ifdef LOG_LOCAL4
158     openlog( g->myname, OPENLOG_OPTIONS, LOG_LOCAL4 );
159 #elif LOG_DEBUG
160     openlog( g->myname, OPENLOG_OPTIONS );
161 #endif
162
163     return 0;
164 }