]> git.sur5r.net Git - openldap/blob - servers/slapd/main.c
Import updated SLAPD .args/.pid file handling from devel.
[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  * when more than one slapd is running on one machine, each one might have
16  * it's own LOCAL for syslogging and must have its own pid/args files
17  */
18
19 #ifdef LOG_LOCAL4
20
21 #define DEFAULT_SYSLOG_USER  LOG_LOCAL4
22
23 typedef struct _str2intDispatch {
24
25         char    *stringVal;
26         int      abbr;
27         int      intVal;
28
29 } STRDISP, *STRDISP_P;
30
31
32 /* table to compute syslog-options to integer */
33 static STRDISP  syslog_types[] = {
34
35     { "LOCAL0",         6, LOG_LOCAL0 },
36     { "LOCAL1",         6, LOG_LOCAL1 },
37     { "LOCAL2",         6, LOG_LOCAL2 },
38     { "LOCAL3",         6, LOG_LOCAL3 },
39     { "LOCAL4",         6, LOG_LOCAL4 },
40     { "LOCAL5",         6, LOG_LOCAL5 },
41     { "LOCAL6",         6, LOG_LOCAL6 },
42     { "LOCAL7",         6, LOG_LOCAL7 },
43     NULL
44
45 };
46
47 static int   cnvt_str2int();
48
49 #endif  /* LOG_LOCAL4 */
50
51
52 static void
53 usage( char *name )
54 {
55         fprintf( stderr, "usage: %s [-d ?|debuglevel] [-f configfile] [-p portnumber] [-s sysloglevel]", name );
56 #ifdef LOG_LOCAL4
57     fprintf( stderr, " [-l sysloguser]" );
58 #endif
59     fprintf( stderr, "\n" );
60 }
61
62 int
63 main( int argc, char **argv )
64 {
65         int             i;
66         int             inetd = 0;
67         int             port;
68         int             udp;
69         Backend         *be = NULL;
70         FILE            *fp = NULL;
71         char            *configfile;
72         char    *serverName;
73 #ifdef LOG_LOCAL4
74         int     syslogUser = DEFAULT_SYSLOG_USER;
75 #endif
76
77         configfile = SLAPD_DEFAULT_CONFIGFILE;
78         port = LDAP_PORT;
79         g_argc = argc;
80         g_argv = argv;
81
82         while ( (i = getopt( argc, argv, "d:f:ip:s:u" )) != EOF ) {
83                 switch ( i ) {
84 #ifdef LDAP_DEBUG
85                 case 'd':       /* turn on debugging */
86                         if ( optarg[0] == '?' ) {
87                                 printf( "Debug levels:\n" );
88                                 printf( "\tLDAP_DEBUG_TRACE\t%d\n",
89                                     LDAP_DEBUG_TRACE );
90                                 printf( "\tLDAP_DEBUG_PACKETS\t%d\n",
91                                     LDAP_DEBUG_PACKETS );
92                                 printf( "\tLDAP_DEBUG_ARGS\t\t%d\n",
93                                     LDAP_DEBUG_ARGS );
94                                 printf( "\tLDAP_DEBUG_CONNS\t%d\n",
95                                     LDAP_DEBUG_CONNS );
96                                 printf( "\tLDAP_DEBUG_BER\t\t%d\n",
97                                     LDAP_DEBUG_BER );
98                                 printf( "\tLDAP_DEBUG_FILTER\t%d\n",
99                                     LDAP_DEBUG_FILTER );
100                                 printf( "\tLDAP_DEBUG_CONFIG\t%d\n",
101                                     LDAP_DEBUG_CONFIG );
102                                 printf( "\tLDAP_DEBUG_ACL\t\t%d\n",
103                                     LDAP_DEBUG_ACL );
104                                 printf( "\tLDAP_DEBUG_STATS\t%d\n",
105                                     LDAP_DEBUG_STATS );
106                                 printf( "\tLDAP_DEBUG_STATS2\t%d\n",
107                                     LDAP_DEBUG_STATS2 );
108                                 printf( "\tLDAP_DEBUG_SHELL\t%d\n",
109                                     LDAP_DEBUG_SHELL );
110                                 printf( "\tLDAP_DEBUG_PARSE\t%d\n",
111                                     LDAP_DEBUG_PARSE );
112                                 printf( "\tLDAP_DEBUG_ANY\t\t%d\n",
113                                     LDAP_DEBUG_ANY );
114                                 exit( 0 );
115                         } else {
116                                 ldap_debug |= atoi( optarg );
117                                 lber_debug = (ldap_debug & LDAP_DEBUG_BER);
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         Debug( LDAP_DEBUG_TRACE, "%s", Versionstr, 0, 0 );
163
164         if ( (serverName = strrchr( argv[0], '/' )) == NULL ) {
165                 serverName = ch_strdup( argv[0] );
166         } else {
167                 serverName = ch_strdup( serverName + 1 );
168         }
169
170         if ( ! inetd ) {
171                 /* pre-open config file before detach in case it is a relative path */
172                 fp = fopen( configfile, "r" );
173 #ifdef LDAP_DEBUG
174                 lutil_detach( ldap_debug, 0 );
175 #else
176                 lutil_detach( 0, 0 );
177 #endif
178         }
179
180 #ifdef LOG_LOCAL4
181         openlog( serverName, OPENLOG_OPTIONS, syslogUser );
182 #else
183         openlog( serverName, OPENLOG_OPTIONS );
184 #endif
185
186         init();
187         read_config( configfile, &be, fp );
188
189         if ( ! inetd ) {
190                 int             status;
191
192                 time( &starttime );
193
194                 if ( pthread_create( &listener_tid, NULL, slapd_daemon,
195                     (void *) port ) != 0 ) {
196                         Debug( LDAP_DEBUG_ANY,
197                             "listener pthread_create failed\n", 0, 0, 0 );
198                         exit( 1 );
199                 }
200
201 #ifdef HAVE_PHREADS_FINAL
202                 pthread_join( listener_tid, (void *) NULL );
203 #else
204                 pthread_join( listener_tid, (void *) &status );
205 #endif
206
207                 return 0;
208
209         } else {
210                 Connection              c;
211                 Operation               *o;
212                 BerElement              ber;
213                 unsigned long           len, tag;
214                 long                    msgid;
215                 int                     flen;
216                 struct sockaddr_in      from;
217                 struct hostent          *hp;
218
219                 c.c_dn = NULL;
220                 c.c_cdn = NULL;
221                 c.c_ops = NULL;
222                 c.c_sb.sb_sd = 0;
223                 c.c_sb.sb_options = 0;
224                 c.c_sb.sb_naddr = udp ? 1 : 0;
225                 c.c_sb.sb_ber.ber_buf = NULL;
226                 c.c_sb.sb_ber.ber_ptr = NULL;
227                 c.c_sb.sb_ber.ber_end = NULL;
228                 pthread_mutex_init( &c.c_dnmutex, pthread_mutexattr_default );
229                 pthread_mutex_init( &c.c_opsmutex, pthread_mutexattr_default );
230                 pthread_mutex_init( &c.c_pdumutex, pthread_mutexattr_default );
231 #ifdef notdefcldap
232                 c.c_sb.sb_addrs = (void **) saddrlist;
233                 c.c_sb.sb_fromaddr = &faddr;
234                 c.c_sb.sb_useaddr = saddrlist[ 0 ] = &saddr;
235 #endif
236                 flen = sizeof(from);
237                 if ( getpeername( 0, (struct sockaddr *) &from, &flen ) == 0 ) {
238 #ifdef SLAPD_RLOOKUPS
239                         hp = gethostbyaddr( (char *) &(from.sin_addr.s_addr),
240                             sizeof(from.sin_addr.s_addr), AF_INET );
241 #else
242                         hp = NULL;
243 #endif
244
245                         Debug( LDAP_DEBUG_ARGS, "connection from %s (%s)\n",
246                             hp == NULL ? "unknown" : hp->h_name,
247                             inet_ntoa( from.sin_addr ), 0 );
248
249                         c.c_addr = inet_ntoa( from.sin_addr );
250                         c.c_domain = ch_strdup( hp == NULL ? "" : hp->h_name );
251                 } else {
252                         Debug( LDAP_DEBUG_ARGS, "connection from unknown\n",
253                             0, 0, 0 );
254                 }
255
256                 ber_init( &ber, 0 );
257                 while ( (tag = ber_get_next( &c.c_sb, &len, &ber ))
258                     == LDAP_TAG_MESSAGE ) {
259                         pthread_mutex_lock( &currenttime_mutex );
260                         time( &currenttime );
261                         pthread_mutex_unlock( &currenttime_mutex );
262
263                         if ( (tag = ber_get_int( &ber, &msgid ))
264                             != LDAP_TAG_MSGID ) {
265                                 /* log and send error */
266                                 Debug( LDAP_DEBUG_ANY,
267                                    "ber_get_int returns 0x%lx\n", tag, 0, 0 );
268                                 ber_free( &ber, 1 );
269                                 return 1;
270                         }
271
272                         if ( (tag = ber_peek_tag( &ber, &len ))
273                             == LBER_ERROR ) {
274                                 /* log, close and send error */
275                                 Debug( LDAP_DEBUG_ANY,
276                                    "ber_peek_tag returns 0x%lx\n", tag, 0, 0 );
277                                 ber_free( &ber, 1 );
278                                 close( c.c_sb.sb_sd );
279                                 c.c_sb.sb_sd = -1;
280                                 return 1;
281                         }
282
283                         connection_activity( &c );
284
285                         ber_free( &ber, 1 );
286                 }
287         }
288         return 1;
289 }
290
291
292 #ifdef LOG_LOCAL4
293
294 /*
295  *  Convert a string to an integer by means of a dispatcher table
296  *  if the string is not in the table return the default
297  */
298
299 static int
300 cnvt_str2int (stringVal, dispatcher, defaultVal)
301 char      *stringVal;
302 STRDISP_P  dispatcher;
303 int        defaultVal;
304 {
305     int        retVal = defaultVal;
306     STRDISP_P  disp;
307
308     for (disp = dispatcher; disp->stringVal; disp++) {
309
310         if (!strncasecmp (stringVal, disp->stringVal, disp->abbr)) {
311
312             retVal = disp->intVal;
313             break;
314
315         }
316     }
317
318     return (retVal);
319
320 } /* cnvt_str2int */
321
322 #endif  /* LOG_LOCAL4 */
323