]> git.sur5r.net Git - openldap/blob - servers/slapd/main.c
Import memory leak fix 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 ( status = ldap_pvt_thread_create( &listener_tid, 0,
195                         slapd_daemon, (void *) port ) != 0 )
196                 {
197                         Debug( LDAP_DEBUG_ANY,
198                             "listener ldap_pvt_thread_create failed (%d)\n", status, 0, 0 );
199                         exit( 1 );
200                 }
201
202                 ldap_pvt_thread_join( listener_tid, (void *) NULL );
203
204                 return 0;
205
206         } else {
207                 Connection              c;
208                 Operation               *o;
209                 BerElement              ber;
210                 unsigned long           len, tag;
211                 long                    msgid;
212                 int                     flen;
213                 struct sockaddr_in      from;
214                 struct hostent          *hp;
215
216                 c.c_dn = NULL;
217                 c.c_cdn = NULL;
218                 c.c_ops = NULL;
219                 c.c_sb.sb_sd = 0;
220                 c.c_sb.sb_options = 0;
221                 c.c_sb.sb_naddr = udp ? 1 : 0;
222                 c.c_sb.sb_ber.ber_buf = NULL;
223                 c.c_sb.sb_ber.ber_ptr = NULL;
224                 c.c_sb.sb_ber.ber_end = NULL;
225                 ldap_pvt_thread_mutex_init( &c.c_dnmutex );
226                 ldap_pvt_thread_mutex_init( &c.c_opsmutex );
227                 ldap_pvt_thread_mutex_init( &c.c_pdumutex );
228 #ifdef notdefcldap
229                 c.c_sb.sb_addrs = (void **) saddrlist;
230                 c.c_sb.sb_fromaddr = &faddr;
231                 c.c_sb.sb_useaddr = saddrlist[ 0 ] = &saddr;
232 #endif
233                 flen = sizeof(from);
234                 if ( getpeername( 0, (struct sockaddr *) &from, &flen ) == 0 ) {
235 #ifdef SLAPD_RLOOKUPS
236                         hp = gethostbyaddr( (char *) &(from.sin_addr.s_addr),
237                             sizeof(from.sin_addr.s_addr), AF_INET );
238 #else
239                         hp = NULL;
240 #endif
241
242                         Debug( LDAP_DEBUG_ARGS, "connection from %s (%s)\n",
243                             hp == NULL ? "unknown" : hp->h_name,
244                             inet_ntoa( from.sin_addr ), 0 );
245
246                         c.c_addr = inet_ntoa( from.sin_addr );
247                         c.c_domain = ch_strdup( hp == NULL ? "" : hp->h_name );
248                 } else {
249                         Debug( LDAP_DEBUG_ARGS, "connection from unknown\n",
250                             0, 0, 0 );
251                 }
252
253                 ber_init( &ber, 0 );
254                 while ( (tag = ber_get_next( &c.c_sb, &len, &ber ))
255                     == LDAP_TAG_MESSAGE ) {
256                         ldap_pvt_thread_mutex_lock( &currenttime_mutex );
257                         time( &currenttime );
258                         ldap_pvt_thread_mutex_unlock( &currenttime_mutex );
259
260                         if ( (tag = ber_get_int( &ber, &msgid ))
261                             != LDAP_TAG_MSGID ) {
262                                 /* log and send error */
263                                 Debug( LDAP_DEBUG_ANY,
264                                    "ber_get_int returns 0x%lx\n", tag, 0, 0 );
265                                 ber_free( &ber, 1 );
266                                 return 1;
267                         }
268
269                         if ( (tag = ber_peek_tag( &ber, &len ))
270                             == LBER_ERROR ) {
271                                 /* log, close and send error */
272                                 Debug( LDAP_DEBUG_ANY,
273                                    "ber_peek_tag returns 0x%lx\n", tag, 0, 0 );
274                                 ber_free( &ber, 1 );
275                                 close( c.c_sb.sb_sd );
276                                 c.c_sb.sb_sd = -1;
277                                 return 1;
278                         }
279
280                         connection_activity( &c );
281
282                         ber_free( &ber, 1 );
283                 }
284         }
285         return 1;
286 }
287
288
289 #ifdef LOG_LOCAL4
290
291 /*
292  *  Convert a string to an integer by means of a dispatcher table
293  *  if the string is not in the table return the default
294  */
295
296 static int
297 cnvt_str2int (stringVal, dispatcher, defaultVal)
298 char      *stringVal;
299 STRDISP_P  dispatcher;
300 int        defaultVal;
301 {
302     int        retVal = defaultVal;
303     STRDISP_P  disp;
304
305     for (disp = dispatcher; disp->stringVal; disp++) {
306
307         if (!strncasecmp (stringVal, disp->stringVal, disp->abbr)) {
308
309             retVal = disp->intVal;
310             break;
311
312         }
313     }
314
315     return (retVal);
316
317 } /* cnvt_str2int */
318
319 #endif  /* LOG_LOCAL4 */
320