]> git.sur5r.net Git - openldap/blob - servers/slapd/main.c
Import cn=config crash fix from -devel.
[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         int     syslogUser = DEFAULT_SYSLOG_USER;
76 #endif
77
78         configfile = SLAPD_DEFAULT_CONFIGFILE;
79         port = LDAP_PORT;
80         g_argc = argc;
81         g_argv = argv;
82
83         while ( (i = getopt( argc, argv, "d:f:ip:s:u" )) != EOF ) {
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                                 ldap_debug |= atoi( optarg );
118                                 lber_debug = (ldap_debug & LDAP_DEBUG_BER);
119                         }
120                         break;
121 #else
122                 case 'd':       /* turn on debugging */
123                         fprintf( stderr,
124                             "must compile with LDAP_DEBUG for debugging\n" );
125                         break;
126 #endif
127
128                 case 'f':       /* read config file */
129                         configfile = ch_strdup( optarg );
130                         break;
131
132                 case 'i':       /* run from inetd */
133                         inetd = 1;
134                         break;
135
136                 case 'p':       /* port on which to listen */
137                         port = atoi( optarg );
138                         break;
139
140                 case 's':       /* set syslog level */
141                         ldap_syslog = atoi( optarg );
142                         break;
143
144 #ifdef LOG_LOCAL4
145
146                 case 'l':       /* set syslog local user */
147                         syslogUser = cnvt_str2int( optarg, syslog_types,
148                                            DEFAULT_SYSLOG_USER );
149                         break;
150
151 #endif
152
153                 case 'u':       /* do udp */
154                         udp = 1;
155                         break;
156
157                 default:
158                         usage( argv[0] );
159                         exit( 1 );
160                 }
161         }
162
163         Debug( LDAP_DEBUG_TRACE, "%s", Versionstr, 0, 0 );
164
165         if ( (serverName = strrchr( argv[0], '/' )) == NULL ) {
166                 serverName = ch_strdup( argv[0] );
167         } else {
168                 serverName = ch_strdup( serverName + 1 );
169         }
170
171         if ( ! inetd ) {
172                 /* pre-open config file before detach in case it is a relative path */
173                 fp = fopen( configfile, "r" );
174 #ifdef LDAP_DEBUG
175                 lutil_detach( ldap_debug, 0 );
176 #else
177                 lutil_detach( 0, 0 );
178 #endif
179         }
180
181 #ifdef LOG_LOCAL4
182         openlog( serverName, OPENLOG_OPTIONS, syslogUser );
183 #else
184         openlog( serverName, OPENLOG_OPTIONS );
185 #endif
186
187         init();
188         read_config( configfile, &be, fp );
189
190         if ( ! inetd ) {
191                 int             status;
192
193                 (void) SIGNAL( SIGPIPE, SIG_IGN );
194                 (void) SIGNAL( LDAP_SIGUSR1, slap_do_nothing );
195                 (void) SIGNAL( LDAP_SIGUSR2, slap_set_shutdown );
196                 (void) SIGNAL( SIGTERM, slap_set_shutdown );
197                 (void) SIGNAL( SIGINT, slap_set_shutdown );
198                 (void) SIGNAL( SIGHUP, slap_set_shutdown );
199
200                 time( &starttime );
201
202                 if ( status = ldap_pvt_thread_create( &listener_tid, 0,
203                         slapd_daemon, (void *) port ) != 0 )
204                 {
205                         Debug( LDAP_DEBUG_ANY,
206                             "listener ldap_pvt_thread_create failed (%d)\n", status, 0, 0 );
207                         exit( 1 );
208                 }
209
210                 ldap_pvt_thread_join( listener_tid, (void *) NULL );
211
212                 return 0;
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( &ber, 0 );
262                 while ( (tag = ber_get_next( &c.c_sb, &len, &ber ))
263                     == LDAP_TAG_MESSAGE ) {
264                         ldap_pvt_thread_mutex_lock( &currenttime_mutex );
265                         time( &currenttime );
266                         ldap_pvt_thread_mutex_unlock( &currenttime_mutex );
267
268                         if ( (tag = ber_get_int( &ber, &msgid ))
269                             != LDAP_TAG_MSGID ) {
270                                 /* log and send error */
271                                 Debug( LDAP_DEBUG_ANY,
272                                    "ber_get_int returns 0x%lx\n", tag, 0, 0 );
273                                 ber_free( &ber, 1 );
274                                 return 1;
275                         }
276
277                         if ( (tag = ber_peek_tag( &ber, &len ))
278                             == LBER_ERROR ) {
279                                 /* log, close and send error */
280                                 Debug( LDAP_DEBUG_ANY,
281                                    "ber_peek_tag returns 0x%lx\n", tag, 0, 0 );
282                                 ber_free( &ber, 1 );
283                                 close( c.c_sb.sb_sd );
284                                 c.c_sb.sb_sd = -1;
285                                 return 1;
286                         }
287
288                         connection_activity( &c );
289
290                         ber_free( &ber, 1 );
291                 }
292         }
293         return 1;
294 }
295
296
297 #ifdef LOG_LOCAL4
298
299 /*
300  *  Convert a string to an integer by means of a dispatcher table
301  *  if the string is not in the table return the default
302  */
303
304 static int
305 cnvt_str2int (stringVal, dispatcher, defaultVal)
306 char      *stringVal;
307 STRDISP_P  dispatcher;
308 int        defaultVal;
309 {
310     int        retVal = defaultVal;
311     STRDISP_P  disp;
312
313     for (disp = dispatcher; disp->stringVal; disp++) {
314
315         if (!strncasecmp (stringVal, disp->stringVal, disp->abbr)) {
316
317             retVal = disp->intVal;
318             break;
319
320         }
321     }
322
323     return (retVal);
324
325 } /* cnvt_str2int */
326
327 #endif  /* LOG_LOCAL4 */
328