]> git.sur5r.net Git - openldap/blob - servers/slapd/main.c
Added note about fixing oc_schema_check (ITS#204).
[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
26         char    *stringVal;
27         int      abbr;
28         int      intVal;
29
30 } STRDISP, *STRDISP_P;
31
32
33 /* table to compute syslog-options to integer */
34 static STRDISP  syslog_types[] = {
35
36     { "LOCAL0",         6, LOG_LOCAL0 },
37     { "LOCAL1",         6, LOG_LOCAL1 },
38     { "LOCAL2",         6, LOG_LOCAL2 },
39     { "LOCAL3",         6, LOG_LOCAL3 },
40     { "LOCAL4",         6, LOG_LOCAL4 },
41     { "LOCAL5",         6, LOG_LOCAL5 },
42     { "LOCAL6",         6, LOG_LOCAL6 },
43     { "LOCAL7",         6, LOG_LOCAL7 },
44     NULL
45
46 };
47
48 static int   cnvt_str2int();
49
50 #endif  /* LOG_LOCAL4 */
51
52
53 static void
54 usage( char *name )
55 {
56         fprintf( stderr, "usage: %s [-d ?|debuglevel] [-f configfile] [-p portnumber] [-s sysloglevel]", name );
57 #ifdef LOG_LOCAL4
58     fprintf( stderr, " [-l sysloguser]" );
59 #endif
60     fprintf( stderr, "\n" );
61 }
62
63 int
64 main( int argc, char **argv )
65 {
66         int             i;
67         int             inetd = 0;
68         int             port;
69         int             udp;
70         Backend         *be = NULL;
71         FILE            *fp = NULL;
72         char            *configfile;
73         char    *serverName;
74 #ifdef LOG_LOCAL4
75         char *optstring = "d:f:ip:s:ul:";
76         int     syslogUser = DEFAULT_SYSLOG_USER;
77 #else
78         char *optstring = "d:f:ip:s:u";
79 #endif
80
81         configfile = SLAPD_DEFAULT_CONFIGFILE;
82         port = LDAP_PORT;
83         g_argc = argc;
84         g_argv = argv;
85
86         while ( (i = getopt( argc, argv, optstring )) != EOF ) {
87                 switch ( i ) {
88 #ifdef LDAP_DEBUG
89                 case 'd':       /* turn on debugging */
90                         if ( optarg[0] == '?' ) {
91                                 printf( "Debug levels:\n" );
92                                 printf( "\tLDAP_DEBUG_TRACE\t%d\n",
93                                     LDAP_DEBUG_TRACE );
94                                 printf( "\tLDAP_DEBUG_PACKETS\t%d\n",
95                                     LDAP_DEBUG_PACKETS );
96                                 printf( "\tLDAP_DEBUG_ARGS\t\t%d\n",
97                                     LDAP_DEBUG_ARGS );
98                                 printf( "\tLDAP_DEBUG_CONNS\t%d\n",
99                                     LDAP_DEBUG_CONNS );
100                                 printf( "\tLDAP_DEBUG_BER\t\t%d\n",
101                                     LDAP_DEBUG_BER );
102                                 printf( "\tLDAP_DEBUG_FILTER\t%d\n",
103                                     LDAP_DEBUG_FILTER );
104                                 printf( "\tLDAP_DEBUG_CONFIG\t%d\n",
105                                     LDAP_DEBUG_CONFIG );
106                                 printf( "\tLDAP_DEBUG_ACL\t\t%d\n",
107                                     LDAP_DEBUG_ACL );
108                                 printf( "\tLDAP_DEBUG_STATS\t%d\n",
109                                     LDAP_DEBUG_STATS );
110                                 printf( "\tLDAP_DEBUG_STATS2\t%d\n",
111                                     LDAP_DEBUG_STATS2 );
112                                 printf( "\tLDAP_DEBUG_SHELL\t%d\n",
113                                     LDAP_DEBUG_SHELL );
114                                 printf( "\tLDAP_DEBUG_PARSE\t%d\n",
115                                     LDAP_DEBUG_PARSE );
116                                 printf( "\tLDAP_DEBUG_ANY\t\t%d\n",
117                                     LDAP_DEBUG_ANY );
118                                 exit( 0 );
119                         } else {
120                                 ldap_debug |= atoi( optarg );
121                                 lber_debug = (ldap_debug & LDAP_DEBUG_BER);
122                         }
123                         break;
124 #else
125                 case 'd':       /* turn on debugging */
126                         fprintf( stderr,
127                             "must compile with LDAP_DEBUG for debugging\n" );
128                         break;
129 #endif
130
131                 case 'f':       /* read config file */
132                         configfile = ch_strdup( optarg );
133                         break;
134
135                 case 'i':       /* run from inetd */
136                         inetd = 1;
137                         break;
138
139                 case 'p':       /* port on which to listen */
140                         port = atoi( optarg );
141                         break;
142
143                 case 's':       /* set syslog level */
144                         ldap_syslog = atoi( optarg );
145                         break;
146
147 #ifdef LOG_LOCAL4
148                 case 'l':       /* set syslog local user */
149                         syslogUser = cnvt_str2int( optarg, syslog_types,
150                                            DEFAULT_SYSLOG_USER );
151                         break;
152 #endif
153
154                 case 'u':       /* do udp */
155                         udp = 1;
156                         break;
157
158                 default:
159                         usage( argv[0] );
160                         exit( 1 );
161                 }
162         }
163
164         Debug( LDAP_DEBUG_TRACE, "%s", Versionstr, 0, 0 );
165
166         if ( (serverName = strrchr( argv[0], '/' )) == NULL ) {
167                 serverName = ch_strdup( argv[0] );
168         } else {
169                 serverName = ch_strdup( serverName + 1 );
170         }
171
172         if ( ! inetd ) {
173                 /* pre-open config file before detach in case it is a relative path */
174                 fp = fopen( configfile, "r" );
175 #ifdef LDAP_DEBUG
176                 lutil_detach( ldap_debug, 0 );
177 #else
178                 lutil_detach( 0, 0 );
179 #endif
180         }
181
182 #ifdef LOG_LOCAL4
183         openlog( serverName, OPENLOG_OPTIONS, syslogUser );
184 #else
185         openlog( serverName, OPENLOG_OPTIONS );
186 #endif
187
188         init();
189         read_config( configfile, &be, fp );
190
191         if ( ! inetd ) {
192                 int             status;
193
194                 (void) SIGNAL( SIGPIPE, SIG_IGN );
195                 (void) SIGNAL( LDAP_SIGUSR1, slap_do_nothing );
196                 (void) SIGNAL( LDAP_SIGUSR2, slap_set_shutdown );
197                 (void) SIGNAL( SIGTERM, slap_set_shutdown );
198                 (void) SIGNAL( SIGINT, slap_set_shutdown );
199                 (void) SIGNAL( SIGHUP, slap_set_shutdown );
200
201                 time( &starttime );
202
203                 if ( status = ldap_pvt_thread_create( &listener_tid, 0,
204                         slapd_daemon, (void *) port ) != 0 )
205                 {
206                         Debug( LDAP_DEBUG_ANY,
207                             "listener ldap_pvt_thread_create failed (%d)\n", status, 0, 0 );
208                         exit( 1 );
209                 }
210
211                 ldap_pvt_thread_join( listener_tid, (void *) NULL );
212
213                 return 0;
214
215         } else {
216                 Connection              c;
217                 Operation               *o;
218                 BerElement              ber;
219                 unsigned long           len, tag;
220                 long                    msgid;
221                 int                     flen;
222                 struct sockaddr_in      from;
223                 struct hostent          *hp;
224
225                 c.c_dn = NULL;
226                 c.c_cdn = NULL;
227                 c.c_ops = NULL;
228                 c.c_sb.sb_sd = 0;
229                 c.c_sb.sb_options = 0;
230                 c.c_sb.sb_naddr = udp ? 1 : 0;
231                 c.c_sb.sb_ber.ber_buf = NULL;
232                 c.c_sb.sb_ber.ber_ptr = NULL;
233                 c.c_sb.sb_ber.ber_end = NULL;
234                 ldap_pvt_thread_mutex_init( &c.c_dnmutex );
235                 ldap_pvt_thread_mutex_init( &c.c_opsmutex );
236                 ldap_pvt_thread_mutex_init( &c.c_pdumutex );
237 #ifdef notdefcldap
238                 c.c_sb.sb_addrs = (void **) saddrlist;
239                 c.c_sb.sb_fromaddr = &faddr;
240                 c.c_sb.sb_useaddr = saddrlist[ 0 ] = &saddr;
241 #endif
242                 flen = sizeof(from);
243                 if ( getpeername( 0, (struct sockaddr *) &from, &flen ) == 0 ) {
244 #ifdef SLAPD_RLOOKUPS
245                         hp = gethostbyaddr( (char *) &(from.sin_addr.s_addr),
246                             sizeof(from.sin_addr.s_addr), AF_INET );
247 #else
248                         hp = NULL;
249 #endif
250
251                         Debug( LDAP_DEBUG_ARGS, "connection from %s (%s)\n",
252                             hp == NULL ? "unknown" : hp->h_name,
253                             inet_ntoa( from.sin_addr ), 0 );
254
255                         c.c_addr = inet_ntoa( from.sin_addr );
256                         c.c_domain = ch_strdup( hp == NULL ? "" : hp->h_name );
257                 } else {
258                         Debug( LDAP_DEBUG_ARGS, "connection from unknown\n",
259                             0, 0, 0 );
260                 }
261
262                 ber_init( &ber, 0 );
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         return 1;
295 }
296
297
298 #ifdef LOG_LOCAL4
299
300 /*
301  *  Convert a string to an integer by means of a dispatcher table
302  *  if the string is not in the table return the default
303  */
304
305 static int
306 cnvt_str2int (stringVal, dispatcher, defaultVal)
307 char      *stringVal;
308 STRDISP_P  dispatcher;
309 int        defaultVal;
310 {
311     int        retVal = defaultVal;
312     STRDISP_P  disp;
313
314     for (disp = dispatcher; disp->stringVal; disp++) {
315
316         if (!strncasecmp (stringVal, disp->stringVal, disp->abbr)) {
317
318             retVal = disp->intVal;
319             break;
320
321         }
322     }
323
324     return (retVal);
325
326 } /* cnvt_str2int */
327
328 #endif  /* LOG_LOCAL4 */
329