]> git.sur5r.net Git - openldap/blob - servers/slapd/main.c
Removed o_suffix and o_suffixalias as they were 1) leaked and 2) unused
[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  * the server's name must be accessible from the daemon module,
54  * to construct the pid/args file names
55  */
56 char  *serverName = NULL;
57
58
59 static void
60 usage( char *name )
61 {
62         fprintf( stderr, "usage: %s [-d ?|debuglevel] [-f configfile] [-p portnumber] [-s sysloglevel]", name );
63 #ifdef LOG_LOCAL4
64     fprintf( stderr, " [-l sysloguser]" );
65 #endif
66     fprintf( stderr, "\n" );
67 }
68
69 int
70 main( int argc, char **argv )
71 {
72         int             i;
73         int             inetd = 0;
74         int             port;
75         int             udp;
76         Backend         *be = NULL;
77         FILE            *fp = NULL;
78 #ifdef LOG_LOCAL4
79     int     syslogUser = DEFAULT_SYSLOG_USER;
80 #endif
81         char            *configfile;
82
83         configfile = SLAPD_DEFAULT_CONFIGFILE;
84         port = LDAP_PORT;
85         g_argc = argc;
86         g_argv = argv;
87
88         while ( (i = getopt( argc, argv, "d:f:ip:s:u" )) != EOF ) {
89                 switch ( i ) {
90 #ifdef LDAP_DEBUG
91                 case 'd':       /* turn on debugging */
92                         if ( optarg[0] == '?' ) {
93                                 printf( "Debug levels:\n" );
94                                 printf( "\tLDAP_DEBUG_TRACE\t%d\n",
95                                     LDAP_DEBUG_TRACE );
96                                 printf( "\tLDAP_DEBUG_PACKETS\t%d\n",
97                                     LDAP_DEBUG_PACKETS );
98                                 printf( "\tLDAP_DEBUG_ARGS\t\t%d\n",
99                                     LDAP_DEBUG_ARGS );
100                                 printf( "\tLDAP_DEBUG_CONNS\t%d\n",
101                                     LDAP_DEBUG_CONNS );
102                                 printf( "\tLDAP_DEBUG_BER\t\t%d\n",
103                                     LDAP_DEBUG_BER );
104                                 printf( "\tLDAP_DEBUG_FILTER\t%d\n",
105                                     LDAP_DEBUG_FILTER );
106                                 printf( "\tLDAP_DEBUG_CONFIG\t%d\n",
107                                     LDAP_DEBUG_CONFIG );
108                                 printf( "\tLDAP_DEBUG_ACL\t\t%d\n",
109                                     LDAP_DEBUG_ACL );
110                                 printf( "\tLDAP_DEBUG_STATS\t%d\n",
111                                     LDAP_DEBUG_STATS );
112                                 printf( "\tLDAP_DEBUG_STATS2\t%d\n",
113                                     LDAP_DEBUG_STATS2 );
114                                 printf( "\tLDAP_DEBUG_SHELL\t%d\n",
115                                     LDAP_DEBUG_SHELL );
116                                 printf( "\tLDAP_DEBUG_PARSE\t%d\n",
117                                     LDAP_DEBUG_PARSE );
118                                 printf( "\tLDAP_DEBUG_ANY\t\t%d\n",
119                                     LDAP_DEBUG_ANY );
120                                 exit( 0 );
121                         } else {
122                                 slap_debug |= atoi( optarg );
123                         }
124                         break;
125 #else
126                 case 'd':       /* turn on debugging */
127                         fprintf( stderr,
128                             "must compile with LDAP_DEBUG for debugging\n" );
129                         break;
130 #endif
131
132                 case 'f':       /* read config file */
133                         configfile = ch_strdup( optarg );
134                         break;
135
136                 case 'i':       /* run from inetd */
137                         inetd = 1;
138                         break;
139
140                 case 'p':       /* port on which to listen */
141                         port = atoi( optarg );
142                         break;
143
144                 case 's':       /* set syslog level */
145                         ldap_syslog = atoi( optarg );
146                         break;
147
148 #ifdef LOG_LOCAL4
149
150                 case 'l':       /* set syslog local user */
151                         syslogUser = cnvt_str2int( optarg, syslog_types,
152                                            DEFAULT_SYSLOG_USER );
153                         break;
154
155 #endif
156
157                 case 'u':       /* do udp */
158                         udp = 1;
159                         break;
160
161                 default:
162                         usage( argv[0] );
163                         exit( 1 );
164                 }
165         }
166
167         lber_set_option(NULL, LBER_OPT_DEBUG_LEVEL, &slap_debug);
168         ldap_set_option(NULL, LDAP_OPT_DEBUG_LEVEL, &slap_debug);
169         ldif_debug = slap_debug;
170
171         Debug( LDAP_DEBUG_TRACE, "%s", Versionstr, 0, 0 );
172
173         if ( (serverName = strrchr( argv[0], '/' )) == NULL ) {
174                 serverName = ch_strdup( argv[0] );
175         } else {
176                 serverName = ch_strdup( serverName + 1 );
177         }
178
179         if ( ! inetd ) {
180                 /* pre-open config file before detach in case it is a relative path */
181                 fp = fopen( configfile, "r" );
182 #ifdef LDAP_DEBUG
183                 lutil_detach( ldap_debug, 0 );
184 #else
185                 lutil_detach( 0, 0 );
186 #endif
187         }
188
189 #ifdef LOG_LOCAL4
190         openlog( serverName, OPENLOG_OPTIONS, syslogUser );
191 #else
192         openlog( serverName, OPENLOG_OPTIONS );
193 #endif
194
195         init();
196         read_config( configfile, &be, fp );
197
198         if ( ! inetd ) {
199                 int             status;
200
201                 time( &starttime );
202
203                 if ( status = pthread_create( &listener_tid, NULL,
204                         slapd_daemon, (void *) port ) != 0 )
205                 {
206                         Debug( LDAP_DEBUG_ANY,
207                             "listener pthread_create failed (%d)\n", status, 0, 0 );
208                         exit( 1 );
209                 }
210
211 #ifdef HAVE_PHREADS_FINAL
212                 pthread_join( listener_tid, (void *) NULL );
213 #else
214                 pthread_join( listener_tid, (void *) &status );
215 #endif
216
217                 return 0;
218
219         } else {
220                 Connection              c;
221                 Operation               *o;
222                 BerElement              ber;
223                 unsigned long           len, tag;
224                 long                    msgid;
225                 int                     flen;
226                 struct sockaddr_in      from;
227                 struct hostent          *hp;
228
229                 c.c_dn = NULL;
230                 c.c_cdn = NULL;
231                 c.c_ops = NULL;
232                 c.c_sb.sb_sd = 0;
233                 c.c_sb.sb_options = 0;
234                 c.c_sb.sb_naddr = udp ? 1 : 0;
235                 c.c_sb.sb_ber.ber_buf = NULL;
236                 c.c_sb.sb_ber.ber_ptr = NULL;
237                 c.c_sb.sb_ber.ber_end = NULL;
238                 pthread_mutex_init( &c.c_dnmutex, pthread_mutexattr_default );
239                 pthread_mutex_init( &c.c_opsmutex, pthread_mutexattr_default );
240                 pthread_mutex_init( &c.c_pdumutex, pthread_mutexattr_default );
241 #ifdef notdefcldap
242                 c.c_sb.sb_addrs = (void **) saddrlist;
243                 c.c_sb.sb_fromaddr = &faddr;
244                 c.c_sb.sb_useaddr = saddrlist[ 0 ] = &saddr;
245 #endif
246                 flen = sizeof(from);
247                 if ( getpeername( 0, (struct sockaddr *) &from, &flen ) == 0 ) {
248 #ifdef SLAPD_RLOOKUPS
249                         hp = gethostbyaddr( (char *) &(from.sin_addr.s_addr),
250                             sizeof(from.sin_addr.s_addr), AF_INET );
251 #else
252                         hp = NULL;
253 #endif
254
255                         Debug( LDAP_DEBUG_ARGS, "connection from %s (%s)\n",
256                             hp == NULL ? "unknown" : hp->h_name,
257                             inet_ntoa( from.sin_addr ), 0 );
258
259                         c.c_addr = inet_ntoa( from.sin_addr );
260                         c.c_domain = ch_strdup( hp == NULL ? "" : hp->h_name );
261                 } else {
262                         Debug( LDAP_DEBUG_ARGS, "connection from unknown\n",
263                             0, 0, 0 );
264                 }
265
266                 ber_init_w_nullc( &ber, 0 );
267
268                 while ( (tag = ber_get_next( &c.c_sb, &len, &ber ))
269                     == LDAP_TAG_MESSAGE ) {
270                         pthread_mutex_lock( &currenttime_mutex );
271                         time( &currenttime );
272                         pthread_mutex_unlock( &currenttime_mutex );
273
274                         if ( (tag = ber_get_int( &ber, &msgid ))
275                             != LDAP_TAG_MSGID ) {
276                                 /* log and send error */
277                                 Debug( LDAP_DEBUG_ANY,
278                                    "ber_get_int returns 0x%lx\n", tag, 0, 0 );
279                                 ber_free( &ber, 1 );
280                                 return 1;
281                         }
282
283                         if ( (tag = ber_peek_tag( &ber, &len ))
284                             == LBER_ERROR ) {
285                                 /* log, close and send error */
286                                 Debug( LDAP_DEBUG_ANY,
287                                    "ber_peek_tag returns 0x%lx\n", tag, 0, 0 );
288                                 ber_free( &ber, 1 );
289                                 close( c.c_sb.sb_sd );
290                                 c.c_sb.sb_sd = -1;
291                                 return 1;
292                         }
293
294                         connection_activity( &c );
295
296                         ber_free( &ber, 1 );
297                 }
298         }
299         return 1;
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