]> git.sur5r.net Git - openldap/blob - servers/slapd/main.c
If dn2id returns ID but id2entry returns NULL, log it.
[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
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 #ifdef LOG_LOCAL4
73     int     syslogUser = DEFAULT_SYSLOG_USER;
74 #endif
75         char            *configfile;
76         char        *serverName;
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                                 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                 default:
157                         usage( argv[0] );
158                         exit( 1 );
159                 }
160         }
161
162         lber_set_option(NULL, LBER_OPT_DEBUG_LEVEL, &slap_debug);
163         ldap_set_option(NULL, LDAP_OPT_DEBUG_LEVEL, &slap_debug);
164         ldif_debug = slap_debug;
165
166         Debug( LDAP_DEBUG_TRACE, "%s", Versionstr, 0, 0 );
167
168         if ( (serverName = strrchr( argv[0], '/' )) == NULL ) {
169                 serverName = ch_strdup( argv[0] );
170         } else {
171                 serverName = ch_strdup( serverName + 1 );
172         }
173
174         if ( ! inetd ) {
175                 /* pre-open config file before detach in case it is a relative path */
176                 fp = fopen( configfile, "r" );
177 #ifdef LDAP_DEBUG
178                 lutil_detach( ldap_debug, 0 );
179 #else
180                 lutil_detach( 0, 0 );
181 #endif
182         }
183
184 #ifdef LOG_LOCAL4
185         openlog( serverName, OPENLOG_OPTIONS, syslogUser );
186 #else
187         openlog( serverName, OPENLOG_OPTIONS );
188 #endif
189
190         init();
191         read_config( configfile, &be, fp );
192
193         if ( ! inetd ) {
194                 int             status;
195
196                 time( &starttime );
197
198                 if ( status = pthread_create( &listener_tid, NULL,
199                         slapd_daemon, (void *) port ) != 0 )
200                 {
201                         Debug( LDAP_DEBUG_ANY,
202                             "listener pthread_create failed (%d)\n", status, 0, 0 );
203                         exit( 1 );
204                 }
205
206 #ifdef HAVE_PHREADS_FINAL
207                 pthread_join( listener_tid, (void *) NULL );
208 #else
209                 pthread_join( listener_tid, (void *) &status );
210 #endif
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                 pthread_mutex_init( &c.c_dnmutex, pthread_mutexattr_default );
234                 pthread_mutex_init( &c.c_opsmutex, pthread_mutexattr_default );
235                 pthread_mutex_init( &c.c_pdumutex, pthread_mutexattr_default );
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                         pthread_mutex_lock( &currenttime_mutex );
266                         time( &currenttime );
267                         pthread_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