]> git.sur5r.net Git - openldap/blob - servers/slurpd/main.c
ITS#4082 tls ctx requirements are only applicable to servers, or clients
[openldap] / servers / slurpd / main.c
1 /* $OpenLDAP$ */
2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
3  *
4  * Copyright 1998-2005 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).  Additional significant contributors
28  * include:
29  *     Howard Chu
30  */
31
32
33 /* 
34  * main.c - main routine for slurpd.
35  */
36
37 #include "portable.h"
38
39 #include <stdio.h>
40 #include <sys/stat.h>
41 #include <ac/stdlib.h>
42 #include <ac/unistd.h>
43
44 #include "slurp.h"
45 #include "globals.h"
46 #include "lutil.h"
47
48 #include <ldap_pvt.h>
49
50 #ifdef HAVE_NT_SERVICE_MANAGER
51 #define MAIN_RETURN(x)  return
52 #define SERVICE_EXIT( e, n )    do { \
53         if ( is_NT_Service ) { \
54                 lutil_ServiceStatus.dwWin32ExitCode = (e); \
55                 lutil_ServiceStatus.dwServiceSpecificExitCode = (n); \
56         } \
57 } while ( 0 )
58 #else
59 #define SERVICE_EXIT( e, n )
60 #define MAIN_RETURN(x)  return(x)
61 #endif
62
63 #ifndef HAVE_MKVERSION
64 const char Versionstr[] =
65         OPENLDAP_PACKAGE " " OPENLDAP_VERSION " Standalone LDAP Replicator (slurpd)";
66 #endif
67
68 #ifdef HAVE_NT_SERVICE_MANAGER
69 void WINAPI ServiceMain( DWORD argc, LPTSTR *argv )
70 #else
71 int main( int argc, char **argv )
72 #endif
73 {
74 #ifdef NO_THREADS
75     /* Haven't yet written the non-threaded version */
76     fputs( "slurpd currently requires threads support\n", stderr );
77     return( 1 );
78 #else
79
80     int                 i, rc = 0;
81
82     /* initialize thread package */
83     ldap_pvt_thread_initialize();
84
85     /* 
86      * Create and initialize globals.  init_globals() also initializes
87      * the main replication queue.
88      */
89     if (( sglob = init_globals()) == NULL ) {
90         fprintf( stderr, "Out of memory initializing globals\n" );
91         SERVICE_EXIT( ERROR_NOT_ENOUGH_MEMORY, 0 );
92         rc = 1;
93         goto stop;
94     }
95
96 #ifdef HAVE_NT_SERVICE_MANAGER
97         {
98                 int *i;
99                 char *newConfigFile;
100                 char *regService = NULL;
101
102                 if ( is_NT_Service ) {
103                         sglob->serverName = argv[0];
104                         lutil_CommenceStartupProcessing( sglob->serverName, slurp_set_shutdown );
105                         if ( strcmp(sglob->serverName, SERVICE_NAME) )
106                             regService = sglob->serverName;
107                 }
108
109                 i = (int*)lutil_getRegParam( regService, "DebugLevel" );
110                 if ( i != NULL ) 
111                 {
112                         ldap_debug = *i;
113                         Debug( LDAP_DEBUG_ANY, "new debug level from registry is: %d\n", ldap_debug, 0, 0 );
114                 }
115
116                 newConfigFile = (char*)lutil_getRegParam( regService, "ConfigFile" );
117                 if ( newConfigFile != NULL ) 
118                 {
119                         sglob->slapd_configfile = newConfigFile;
120                         Debug ( LDAP_DEBUG_ANY, "new config file from registry is: %s\n", sglob->slapd_configfile, 0, 0 );
121
122                 }
123         }
124 #endif
125
126     /*
127      * Process command-line args and fill in globals.
128      */
129     if ( doargs( argc, argv, sglob ) < 0 ) {
130         SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 15 );
131         rc = 1;
132         goto stop;
133     }
134
135     if ( sglob->version ) {
136                 fprintf(stderr, "%s\n", Versionstr);
137                 if (sglob->version > 1 ) {
138                         rc = 1;
139                         goto stop;
140                 }
141     }
142
143         Debug ( LDAP_DEBUG_ANY, "%s\n", Versionstr, 0, 0 );
144     
145     /*
146      * Read slapd config file and initialize Re (per-replica) structs.
147      */
148     if ( slurpd_read_config( sglob->slapd_configfile ) < 0 ) {
149         fprintf( stderr,
150                 "Errors encountered while processing config file \"%s\"\n",
151                 sglob->slapd_configfile );
152         SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 19 );
153         rc = 1;
154         goto stop;
155     }
156
157 #ifdef HAVE_TLS
158         if( ldap_pvt_tls_init() || ldap_pvt_tls_init_def_ctx( 0 ) ) {
159                 rc = 0;
160                 /* See if we actually need TLS */
161                 for ( i=0; i < sglob->num_replicas; i++ ) {
162                         if ( sglob->replicas[i]->ri_tls || ( sglob->replicas[i]->ri_uri &&
163                                 !strncmp( sglob->replicas[i]->ri_uri, "ldaps:", 6 ))) {
164                                 rc = 1;
165                                 break;
166                         }
167                 }
168                 if ( rc ) {
169                         fprintf( stderr, "TLS Initialization failed.\n" );
170                         SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 20 );
171                         goto stop;
172                 }
173         }
174 #endif
175
176     /* 
177      * Make sure our directory exists
178      */
179     if ( mkdir(sglob->slurpd_rdir, 0755) == -1 && errno != EEXIST) {
180         perror(sglob->slurpd_rdir);
181         SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 16 );
182         rc = 1;
183         goto stop;
184     }
185
186     /*
187      * Get any saved state information off the disk.
188      */
189     if ( sglob->st->st_read( sglob->st )) {
190         fprintf( stderr, "Malformed slurpd status file \"%s\"\n",
191                 sglob->slurpd_status_file );
192         SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 17 );
193         rc = 1;
194         goto stop;
195     }
196
197     /*
198      * All readonly data should now be initialized. 
199      * Check for any fatal error conditions before we get started
200      */
201      if ( sanity() < 0 ) {
202         SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 18 );
203         rc = 1;
204         goto stop;
205     }
206
207
208     /*
209      * Detach from the controlling terminal
210      * unless the -d flag is given or in one-shot mode.
211      */
212 #ifndef HAVE_WINSOCK
213         if ( ! (sglob->no_detach || sglob->one_shot_mode) ) {
214                 lutil_detach( 0, 0 );
215         }
216 #endif
217
218         if ( slurpd_pid_file != NULL ) {
219                 FILE *fp = fopen( slurpd_pid_file, "w" );
220
221                 if( fp != NULL ) {
222                         fprintf( fp, "%d\n", (int) getpid() );
223                         fclose( fp );
224
225                 } else {
226                 free(slurpd_pid_file);
227                 slurpd_pid_file = NULL;
228                 }
229         }
230
231         if ( slurpd_args_file != NULL ) {
232                 FILE *fp = fopen( slurpd_args_file, "w" );
233
234                 if( fp != NULL ) {
235                         for ( i = 0; i < argc; i++ ) {
236                                 fprintf( fp, "%s ", argv[i] );
237                         }
238                         fprintf( fp, "\n" );
239                         fclose( fp );
240                 } else {
241                         free(slurpd_args_file);
242                         slurpd_args_file = NULL;
243                 }
244         }
245
246     if ( (rc = lutil_pair( sglob->wake_sds )) < 0 ) {
247         SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 16 );
248         rc = 1;
249         goto stop;
250     }
251         
252 #ifdef HAVE_NT_EVENT_LOG
253         if (is_NT_Service) lutil_LogStartedEvent( sglob->serverName, ldap_debug, sglob->slapd_configfile, "n/a" );
254 #endif
255
256     /*
257      * Start the main file manager thread (in fm.c).
258      */
259     if ( ldap_pvt_thread_create( &(sglob->fm_tid),
260                 0, fm, (void *) NULL ) != 0 )
261         {
262         Debug( LDAP_DEBUG_ANY, "file manager ldap_pvt_thread_create failed\n",
263                 0, 0, 0 );
264         SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 21 );
265         rc = 1;
266         goto stop;
267
268     }
269
270     /*
271      * wait for fm to finish if in oneshot mode
272      */
273     if ( sglob->one_shot_mode ) {
274         ldap_pvt_thread_join( sglob->fm_tid, (void *) NULL );
275     }
276
277     /*
278      * Start threads - one thread for each replica
279      */
280     for ( i = 0; sglob->replicas[ i ] != NULL; i++ ) {
281         start_replica_thread( sglob->replicas[ i ]);
282     }
283
284 #ifdef HAVE_NT_SERVICE_MANAGER
285     if ( started_event ) ldap_pvt_thread_cond_signal( &started_event );
286 #endif
287
288     /*
289      * Wait for the fm thread to finish.
290      */
291     if ( !sglob->one_shot_mode ) {
292         ldap_pvt_thread_join( sglob->fm_tid, (void *) NULL );
293     }
294
295     /*
296      * Wait for the replica threads to finish.
297      */
298     for ( i = 0; sglob->replicas[ i ] != NULL; i++ ) {
299         ldap_pvt_thread_join( sglob->replicas[ i ]->ri_tid, (void *) NULL );
300     }
301
302 stop:
303 #ifdef HAVE_NT_SERVICE_MANAGER
304         if (is_NT_Service) {
305                 ldap_pvt_thread_cond_destroy( &started_event );
306                 lutil_LogStoppedEvent( sglob->serverName );
307                 lutil_ReportShutdownComplete();
308         }
309 #endif
310     /* destroy the thread package */
311     ldap_pvt_thread_destroy();
312
313 #ifdef HAVE_TLS
314     ldap_pvt_tls_destroy();
315 #endif
316
317     Debug( LDAP_DEBUG_ANY, "slurpd: terminated.\n", 0, 0, 0 );
318
319     if ( slurpd_pid_file != NULL ) {
320         unlink( slurpd_pid_file );
321     }
322     if ( slurpd_args_file != NULL ) {
323         unlink( slurpd_args_file );
324     }
325
326
327         MAIN_RETURN(rc);
328 #endif /* !NO_THREADS */
329 }