]> git.sur5r.net Git - openldap/blob - servers/slapd/main.c
67de0a0fdacf20ce0d7e6f7113fe5148d04b08ee
[openldap] / servers / slapd / main.c
1 #include "portable.h"
2
3 #include <stdio.h>
4
5 #include <ac/socket.h>
6 #include <ac/string.h>
7 #include <ac/time.h>
8 #include <ac/unistd.h>
9
10 #include "ldapconfig.h"
11 #include "slap.h"
12 #include "lutil.h"                      /* Get lutil_detach() */
13
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
73         configfile = SLAPD_DEFAULT_CONFIGFILE;
74         port = LDAP_PORT;
75         g_argc = argc;
76         g_argv = argv;
77
78         while ( (i = getopt( argc, argv, "d:f:ip:s:u" )) != EOF ) {
79                 switch ( i ) {
80 #ifdef LDAP_DEBUG
81                 case 'd':       /* turn on debugging */
82                         if ( optarg[0] == '?' ) {
83                                 printf( "Debug levels:\n" );
84                                 printf( "\tLDAP_DEBUG_TRACE\t%d\n",
85                                     LDAP_DEBUG_TRACE );
86                                 printf( "\tLDAP_DEBUG_PACKETS\t%d\n",
87                                     LDAP_DEBUG_PACKETS );
88                                 printf( "\tLDAP_DEBUG_ARGS\t\t%d\n",
89                                     LDAP_DEBUG_ARGS );
90                                 printf( "\tLDAP_DEBUG_CONNS\t%d\n",
91                                     LDAP_DEBUG_CONNS );
92                                 printf( "\tLDAP_DEBUG_BER\t\t%d\n",
93                                     LDAP_DEBUG_BER );
94                                 printf( "\tLDAP_DEBUG_FILTER\t%d\n",
95                                     LDAP_DEBUG_FILTER );
96                                 printf( "\tLDAP_DEBUG_CONFIG\t%d\n",
97                                     LDAP_DEBUG_CONFIG );
98                                 printf( "\tLDAP_DEBUG_ACL\t\t%d\n",
99                                     LDAP_DEBUG_ACL );
100                                 printf( "\tLDAP_DEBUG_STATS\t%d\n",
101                                     LDAP_DEBUG_STATS );
102                                 printf( "\tLDAP_DEBUG_STATS2\t%d\n",
103                                     LDAP_DEBUG_STATS2 );
104                                 printf( "\tLDAP_DEBUG_SHELL\t%d\n",
105                                     LDAP_DEBUG_SHELL );
106                                 printf( "\tLDAP_DEBUG_PARSE\t%d\n",
107                                     LDAP_DEBUG_PARSE );
108                                 printf( "\tLDAP_DEBUG_ANY\t\t%d\n",
109                                     LDAP_DEBUG_ANY );
110                                 exit( 0 );
111                         } else {
112                                 slap_debug |= atoi( optarg );
113                         }
114                         break;
115 #else
116                 case 'd':       /* turn on debugging */
117                         fprintf( stderr,
118                             "must compile with LDAP_DEBUG for debugging\n" );
119                         break;
120 #endif
121
122                 case 'f':       /* read config file */
123                         configfile = ch_strdup( optarg );
124                         break;
125
126                 case 'i':       /* run from inetd */
127                         inetd = 1;
128                         break;
129
130                 case 'p':       /* port on which to listen */
131                         port = atoi( optarg );
132                         break;
133
134                 case 's':       /* set syslog level */
135                         ldap_syslog = atoi( optarg );
136                         break;
137
138 #ifdef LOG_LOCAL4
139
140                 case 'l':       /* set syslog local user */
141                         syslogUser = cnvt_str2int( optarg, syslog_types,
142                                            DEFAULT_SYSLOG_USER );
143                         break;
144
145 #endif
146
147                 case 'u':       /* do udp */
148                         udp = 1;
149                         break;
150
151                 default:
152                         usage( argv[0] );
153                         exit( 1 );
154                 }
155         }
156
157         lber_set_option(NULL, LBER_OPT_DEBUG_LEVEL, &slap_debug);
158         ldap_set_option(NULL, LDAP_OPT_DEBUG_LEVEL, &slap_debug);
159         ldif_debug = slap_debug;
160
161         Debug( LDAP_DEBUG_TRACE, "%s", Versionstr, 0, 0 );
162
163         if ( (serverName = strrchr( argv[0], '/' )) == NULL ) {
164                 serverName = ch_strdup( argv[0] );
165         } else {
166                 serverName = ch_strdup( serverName + 1 );
167         }
168
169 #ifdef LOG_LOCAL4
170         openlog( serverName, OPENLOG_OPTIONS, syslogUser );
171 #else
172         openlog( serverName, OPENLOG_OPTIONS );
173 #endif
174
175         if ( slap_init( SLAP_SERVER_MODE, serverName ) != 0 ) {
176                 rc = 1;
177                 goto destroy;
178         }
179
180         if ( read_config( configfile ) != 0 ) {
181                 rc = 1;
182                 goto destroy;
183         }
184
185         if ( slap_startup(-1)  != 0 ) {
186                 rc = 1;
187                 goto shutdown;
188         }
189
190         if ( ! inetd ) {
191                 int             status;
192
193 #ifdef LDAP_DEBUG
194                 lutil_detach( ldap_debug, 0 );
195 #else
196                 lutil_detach( 0, 0 );
197 #endif
198
199                 time( &starttime );
200
201                 if ( status = ldap_pvt_thread_create( &listener_tid, 0,
202                         slapd_daemon, (void *) port ) != 0 )
203                 {
204                         Debug( LDAP_DEBUG_ANY,
205                             "listener ldap_pvt_thread_create failed (%d)\n", status, 0, 0 );
206
207                         rc = 1;
208                 } else {
209
210                         /* wait for the listener thread to complete */
211                         ldap_pvt_thread_join( listener_tid, (void *) NULL );
212                 }
213
214         } else {
215                 Connection              c;
216                 Operation               *o;
217                 BerElement              ber;
218                 unsigned long           len, tag;
219                 long                    msgid;
220                 int                     flen;
221                 struct sockaddr_in      from;
222                 struct hostent          *hp;
223
224                 c.c_dn = NULL;
225                 c.c_cdn = NULL;
226                 c.c_ops = NULL;
227                 c.c_sb.sb_sd = 0;
228                 c.c_sb.sb_options = 0;
229                 c.c_sb.sb_naddr = udp ? 1 : 0;
230                 c.c_sb.sb_ber.ber_buf = NULL;
231                 c.c_sb.sb_ber.ber_ptr = NULL;
232                 c.c_sb.sb_ber.ber_end = NULL;
233                 ldap_pvt_thread_mutex_init( &c.c_dnmutex );
234                 ldap_pvt_thread_mutex_init( &c.c_opsmutex );
235                 ldap_pvt_thread_mutex_init( &c.c_pdumutex );
236 #ifdef notdefcldap
237                 c.c_sb.sb_addrs = (void **) saddrlist;
238                 c.c_sb.sb_fromaddr = &faddr;
239                 c.c_sb.sb_useaddr = saddrlist[ 0 ] = &saddr;
240 #endif
241                 flen = sizeof(from);
242                 if ( getpeername( 0, (struct sockaddr *) &from, &flen ) == 0 ) {
243 #ifdef SLAPD_RLOOKUPS
244                         hp = gethostbyaddr( (char *) &(from.sin_addr.s_addr),
245                             sizeof(from.sin_addr.s_addr), AF_INET );
246 #else
247                         hp = NULL;
248 #endif
249
250                         Debug( LDAP_DEBUG_ARGS, "connection from %s (%s)\n",
251                             hp == NULL ? "unknown" : hp->h_name,
252                             inet_ntoa( from.sin_addr ), 0 );
253
254                         c.c_addr = inet_ntoa( from.sin_addr );
255                         c.c_domain = ch_strdup( hp == NULL ? "" : hp->h_name );
256                 } else {
257                         Debug( LDAP_DEBUG_ARGS, "connection from unknown\n",
258                             0, 0, 0 );
259                 }
260
261                 ber_init_w_nullc( &ber, 0 );
262
263                 while ( (tag = ber_get_next( &c.c_sb, &len, &ber ))
264                     == LDAP_TAG_MESSAGE ) {
265                         ldap_pvt_thread_mutex_lock( &currenttime_mutex );
266                         time( &currenttime );
267                         ldap_pvt_thread_mutex_unlock( &currenttime_mutex );
268
269                         if ( (tag = ber_get_int( &ber, &msgid ))
270                             != LDAP_TAG_MSGID ) {
271                                 /* log and send error */
272                                 Debug( LDAP_DEBUG_ANY,
273                                    "ber_get_int returns 0x%lx\n", tag, 0, 0 );
274                                 ber_free( &ber, 1 );
275                                 return 1;
276                         }
277
278                         if ( (tag = ber_peek_tag( &ber, &len ))
279                             == LBER_ERROR ) {
280                                 /* log, close and send error */
281                                 Debug( LDAP_DEBUG_ANY,
282                                    "ber_peek_tag returns 0x%lx\n", tag, 0, 0 );
283                                 ber_free( &ber, 1 );
284                                 close( c.c_sb.sb_sd );
285                                 c.c_sb.sb_sd = -1;
286                                 return 1;
287                         }
288
289                         connection_activity( &c );
290
291                         ber_free( &ber, 1 );
292                 }
293         }
294
295 shutdown:
296         slap_shutdown(-1);
297 destroy:
298         slap_destroy();
299         return rc;
300 }
301
302
303 #ifdef LOG_LOCAL4
304
305 /*
306  *  Convert a string to an integer by means of a dispatcher table
307  *  if the string is not in the table return the default
308  */
309
310 static int
311 cnvt_str2int (stringVal, dispatcher, defaultVal)
312 char      *stringVal;
313 STRDISP_P  dispatcher;
314 int        defaultVal;
315 {
316     int        retVal = defaultVal;
317     STRDISP_P  disp;
318
319     for (disp = dispatcher; disp->stringVal; disp++) {
320
321         if (!strncasecmp (stringVal, disp->stringVal, disp->abbr)) {
322
323             retVal = disp->intVal;
324             break;
325
326         }
327     }
328
329     return (retVal);
330
331 } /* cnvt_str2int */
332
333 #endif  /* LOG_LOCAL4 */
334