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