]> git.sur5r.net Git - openldap/blob - servers/slapd/main.c
Made add_values(), delete_values(), and replace_values() so they can be
[openldap] / servers / slapd / main.c
1 #include "portable.h"
2
3 #include <stdio.h>
4
5 #include <ac/signal.h>
6 #include <ac/socket.h>
7 #include <ac/string.h>
8 #include <ac/time.h>
9 #include <ac/unistd.h>
10
11 #include "ldapconfig.h"
12 #include "slap.h"
13 #include "lutil.h"                      /* Get lutil_detach() */
14
15 /*
16  * when more than one slapd is running on one machine, each one might have
17  * it's own LOCAL for syslogging and must have its own pid/args files
18  */
19
20 #ifdef LOG_LOCAL4
21
22 #define DEFAULT_SYSLOG_USER  LOG_LOCAL4
23
24 typedef struct _str2intDispatch {
25         char    *stringVal;
26         int      abbr;
27         int      intVal;
28 } STRDISP, *STRDISP_P;
29
30
31 /* table to compute syslog-options to integer */
32 static STRDISP  syslog_types[] = {
33     { "LOCAL0",         6, LOG_LOCAL0 },
34     { "LOCAL1",         6, LOG_LOCAL1 },
35     { "LOCAL2",         6, LOG_LOCAL2 },
36     { "LOCAL3",         6, LOG_LOCAL3 },
37     { "LOCAL4",         6, LOG_LOCAL4 },
38     { "LOCAL5",         6, LOG_LOCAL5 },
39     { "LOCAL6",         6, LOG_LOCAL6 },
40     { "LOCAL7",         6, LOG_LOCAL7 },
41     NULL
42 };
43
44 static int   cnvt_str2int();
45
46 #endif  /* LOG_LOCAL4 */
47
48
49 static void
50 usage( char *name )
51 {
52         fprintf( stderr, "usage: %s [-d ?|debuglevel] [-f configfile] [-p portnumber] [-s sysloglevel]", name );
53 #ifdef LOG_LOCAL4
54     fprintf( stderr, " [-l sysloguser]" );
55 #endif
56     fprintf( stderr, "\n" );
57 }
58
59 int
60 main( int argc, char **argv )
61 {
62         int             i;
63         int             inetd = 0;
64         int             rc = 0;
65         int             port;
66         int             udp;
67 #ifdef LOG_LOCAL4
68     int     syslogUser = DEFAULT_SYSLOG_USER;
69 #endif
70         char            *configfile;
71         char        *serverName;
72         int         serverMode = SLAP_SERVER_MODE;
73
74         configfile = SLAPD_DEFAULT_CONFIGFILE;
75         port = LDAP_PORT;
76         g_argc = argc;
77         g_argv = argv;
78
79 #ifdef SLAPD_BDB2
80         while ( (i = getopt( argc, argv, "d:f:ip:s:ut" )) != EOF ) {
81 #else
82         while ( (i = getopt( argc, argv, "d:f:ip:s:u" )) != EOF ) {
83 #endif
84                 switch ( i ) {
85 #ifdef LDAP_DEBUG
86                 case 'd':       /* turn on debugging */
87                         if ( optarg[0] == '?' ) {
88                                 printf( "Debug levels:\n" );
89                                 printf( "\tLDAP_DEBUG_TRACE\t%d\n",
90                                     LDAP_DEBUG_TRACE );
91                                 printf( "\tLDAP_DEBUG_PACKETS\t%d\n",
92                                     LDAP_DEBUG_PACKETS );
93                                 printf( "\tLDAP_DEBUG_ARGS\t\t%d\n",
94                                     LDAP_DEBUG_ARGS );
95                                 printf( "\tLDAP_DEBUG_CONNS\t%d\n",
96                                     LDAP_DEBUG_CONNS );
97                                 printf( "\tLDAP_DEBUG_BER\t\t%d\n",
98                                     LDAP_DEBUG_BER );
99                                 printf( "\tLDAP_DEBUG_FILTER\t%d\n",
100                                     LDAP_DEBUG_FILTER );
101                                 printf( "\tLDAP_DEBUG_CONFIG\t%d\n",
102                                     LDAP_DEBUG_CONFIG );
103                                 printf( "\tLDAP_DEBUG_ACL\t\t%d\n",
104                                     LDAP_DEBUG_ACL );
105                                 printf( "\tLDAP_DEBUG_STATS\t%d\n",
106                                     LDAP_DEBUG_STATS );
107                                 printf( "\tLDAP_DEBUG_STATS2\t%d\n",
108                                     LDAP_DEBUG_STATS2 );
109                                 printf( "\tLDAP_DEBUG_SHELL\t%d\n",
110                                     LDAP_DEBUG_SHELL );
111                                 printf( "\tLDAP_DEBUG_PARSE\t%d\n",
112                                     LDAP_DEBUG_PARSE );
113                                 printf( "\tLDAP_DEBUG_ANY\t\t%d\n",
114                                     LDAP_DEBUG_ANY );
115                                 exit( 0 );
116                         } else {
117                                 slap_debug |= atoi( optarg );
118                         }
119                         break;
120 #else
121                 case 'd':       /* turn on debugging */
122                         fprintf( stderr,
123                             "must compile with LDAP_DEBUG for debugging\n" );
124                         break;
125 #endif
126
127                 case 'f':       /* read config file */
128                         configfile = ch_strdup( optarg );
129                         break;
130
131                 case 'i':       /* run from inetd */
132                         inetd = 1;
133                         break;
134
135                 case 'p':       /* port on which to listen */
136                         port = atoi( optarg );
137                         break;
138
139                 case 's':       /* set syslog level */
140                         ldap_syslog = atoi( optarg );
141                         break;
142
143 #ifdef LOG_LOCAL4
144
145                 case 'l':       /* set syslog local user */
146                         syslogUser = cnvt_str2int( optarg, syslog_types,
147                                            DEFAULT_SYSLOG_USER );
148                         break;
149
150 #endif
151
152                 case 'u':       /* do udp */
153                         udp = 1;
154                         break;
155
156 #ifdef SLAPD_BDB2
157                 case 't':  /* timed server */
158                         serverMode = SLAP_TIMEDSERVER_MODE;
159                         break;
160 #endif
161
162                 default:
163                         usage( argv[0] );
164                         exit( 1 );
165                 }
166         }
167
168         lber_set_option(NULL, LBER_OPT_DEBUG_LEVEL, &slap_debug);
169         ldap_set_option(NULL, LDAP_OPT_DEBUG_LEVEL, &slap_debug);
170         ldif_debug = slap_debug;
171
172         Debug( LDAP_DEBUG_TRACE, "%s", Versionstr, 0, 0 );
173
174         if ( (serverName = strrchr( argv[0], '/' )) == NULL ) {
175                 serverName = ch_strdup( argv[0] );
176         } else {
177                 serverName = ch_strdup( serverName + 1 );
178         }
179
180 #ifdef LOG_LOCAL4
181         openlog( serverName, OPENLOG_OPTIONS, syslogUser );
182 #else
183         openlog( serverName, OPENLOG_OPTIONS );
184 #endif
185
186         if ( slap_init( serverMode, serverName ) != 0 ) {
187                 rc = 1;
188                 goto destroy;
189         }
190
191         if ( read_config( configfile ) != 0 ) {
192                 rc = 1;
193                 goto destroy;
194         }
195
196         if ( slap_startup(-1)  != 0 ) {
197                 rc = 1;
198                 goto shutdown;
199         }
200
201         if ( ! inetd ) {
202                 int             status;
203
204                 (void) SIGNAL( LDAP_SIGUSR1, slap_do_nothing );
205                 (void) SIGNAL( LDAP_SIGUSR2, slap_set_shutdown );
206 #ifdef SIGPIPE
207                 (void) SIGNAL( SIGPIPE, SIG_IGN );
208 #endif
209 #ifdef SIGHUP
210                 (void) SIGNAL( SIGHUP, slap_set_shutdown );
211 #endif
212                 (void) SIGNAL( SIGINT, slap_set_shutdown );
213                 (void) SIGNAL( SIGTERM, slap_set_shutdown );
214
215 #ifdef LDAP_DEBUG
216                 lutil_detach( ldap_debug, 0 );
217 #else
218                 lutil_detach( 0, 0 );
219 #endif
220
221                 time( &starttime );
222
223                 if ( status = ldap_pvt_thread_create( &listener_tid, 0,
224                         slapd_daemon, (void *) port ) != 0 )
225                 {
226                         Debug( LDAP_DEBUG_ANY,
227                             "listener ldap_pvt_thread_create failed (%d)\n", status, 0, 0 );
228
229                         rc = 1;
230
231                 } else {
232                         /* wait for the listener thread to complete */
233                         ldap_pvt_thread_join( listener_tid, (void *) NULL );
234                 }
235
236         } else {
237                 Connection              c;
238                 Operation               *o;
239                 BerElement              ber;
240                 unsigned long           len, tag;
241                 long                    msgid;
242                 int                     flen;
243                 struct sockaddr_in      from;
244                 struct hostent          *hp;
245
246                 c.c_dn = NULL;
247                 c.c_cdn = NULL;
248                 c.c_ops = NULL;
249            
250                 lber_pvt_sb_init( &c.c_sb );
251                 lber_pvt_sb_set_desc( &c.c_sb, 0 );
252                 lber_pvt_sb_set_io( &c.c_sb, 
253                         (udp) ? &lber_pvt_sb_io_udp : &lber_pvt_sb_io_tcp, 
254                         NULL );
255                 /* FIXME: handle udp here */
256
257                 ldap_pvt_thread_mutex_init( &c.c_dnmutex );
258                 ldap_pvt_thread_mutex_init( &c.c_opsmutex );
259                 ldap_pvt_thread_mutex_init( &c.c_pdumutex );
260 #ifdef notdefcldap
261                 c.c_sb.sb_addrs = (void **) saddrlist;
262                 c.c_sb.sb_fromaddr = &faddr;
263                 c.c_sb.sb_useaddr = saddrlist[ 0 ] = &saddr;
264 #endif
265                 flen = sizeof(from);
266                 if ( getpeername( 0, (struct sockaddr *) &from, &flen ) == 0 ) {
267 #ifdef SLAPD_RLOOKUPS
268                         hp = gethostbyaddr( (char *) &(from.sin_addr.s_addr),
269                             sizeof(from.sin_addr.s_addr), AF_INET );
270 #else
271                         hp = NULL;
272 #endif
273
274                         Debug( LDAP_DEBUG_ARGS, "connection from %s (%s)\n",
275                             hp == NULL ? "unknown" : hp->h_name,
276                             inet_ntoa( from.sin_addr ), 0 );
277
278                         c.c_addr = inet_ntoa( from.sin_addr );
279                         c.c_domain = ch_strdup( hp == NULL ? "" : hp->h_name );
280                 } else {
281                         Debug( LDAP_DEBUG_ARGS, "connection from unknown\n",
282                             0, 0, 0 );
283                 }
284
285                 ber_init_w_nullc( &ber, 0 );
286
287                 while ( (tag = ber_get_next( &c.c_sb, &len, &ber ))
288                     == LDAP_TAG_MESSAGE ) {
289                         ldap_pvt_thread_mutex_lock( &currenttime_mutex );
290                         time( &currenttime );
291                         ldap_pvt_thread_mutex_unlock( &currenttime_mutex );
292
293                         if ( (tag = ber_get_int( &ber, &msgid ))
294                             != LDAP_TAG_MSGID ) {
295                                 /* log and send error */
296                                 Debug( LDAP_DEBUG_ANY,
297                                    "ber_get_int returns 0x%lx\n", tag, 0, 0 );
298                                 ber_free( &ber, 1 );
299                                 return 1;
300                         }
301
302                         if ( (tag = ber_peek_tag( &ber, &len ))
303                             == LBER_ERROR ) {
304                                 /* log, close and send error */
305                                 Debug( LDAP_DEBUG_ANY,
306                                    "ber_peek_tag returns 0x%lx\n", tag, 0, 0 );
307                                 ber_free( &ber, 1 );
308                                 lber_pvt_sb_close( &c.c_sb );
309                                 lber_pvt_sb_destroy( &c.c_sb );
310                                 return 1;
311                         }
312
313                         connection_activity( &c );
314
315                         ber_free( &ber, 1 );
316                 }
317         }
318
319 shutdown:
320         /* remember an error during shutdown */
321         rc |= slap_shutdown(-1);
322 destroy:
323         /* remember an error during destroy */
324         rc |= slap_destroy();
325
326         Debug( LDAP_DEBUG_ANY, "slapd stopped.\n", 0, 0, 0 );
327
328         return rc;
329 }
330
331
332 #ifdef LOG_LOCAL4
333
334 /*
335  *  Convert a string to an integer by means of a dispatcher table
336  *  if the string is not in the table return the default
337  */
338
339 static int
340 cnvt_str2int (stringVal, dispatcher, defaultVal)
341 char      *stringVal;
342 STRDISP_P  dispatcher;
343 int        defaultVal;
344 {
345     int        retVal = defaultVal;
346     STRDISP_P  disp;
347
348     for (disp = dispatcher; disp->stringVal; disp++) {
349
350         if (!strncasecmp (stringVal, disp->stringVal, disp->abbr)) {
351
352             retVal = disp->intVal;
353             break;
354
355         }
356     }
357
358     return (retVal);
359
360 } /* cnvt_str2int */
361
362 #endif  /* LOG_LOCAL4 */
363