]> git.sur5r.net Git - openldap/blob - servers/slapd/main.c
Import slapd.conf mode change (with typo correction) 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 static void
15 usage( char *name )
16 {
17         fprintf( stderr, "usage: %s [-d ?|debuglevel] [-f configfile] [-p portnumber] [-s sysloglevel]\n", name );
18 }
19
20 int
21 main( int argc, char **argv )
22 {
23         int             i;
24         int             inetd = 0;
25         int             port;
26         char            *myname;
27         int             udp;
28         Backend         *be = NULL;
29         FILE            *fp = NULL;
30         char            *configfile;
31
32         configfile = SLAPD_DEFAULT_CONFIGFILE;
33         port = LDAP_PORT;
34         g_argc = argc;
35         g_argv = argv;
36
37         while ( (i = getopt( argc, argv, "d:f:ip:s:u" )) != EOF ) {
38                 switch ( i ) {
39 #ifdef LDAP_DEBUG
40                 case 'd':       /* turn on debugging */
41                         if ( optarg[0] == '?' ) {
42                                 printf( "Debug levels:\n" );
43                                 printf( "\tLDAP_DEBUG_TRACE\t%d\n",
44                                     LDAP_DEBUG_TRACE );
45                                 printf( "\tLDAP_DEBUG_PACKETS\t%d\n",
46                                     LDAP_DEBUG_PACKETS );
47                                 printf( "\tLDAP_DEBUG_ARGS\t\t%d\n",
48                                     LDAP_DEBUG_ARGS );
49                                 printf( "\tLDAP_DEBUG_CONNS\t%d\n",
50                                     LDAP_DEBUG_CONNS );
51                                 printf( "\tLDAP_DEBUG_BER\t\t%d\n",
52                                     LDAP_DEBUG_BER );
53                                 printf( "\tLDAP_DEBUG_FILTER\t%d\n",
54                                     LDAP_DEBUG_FILTER );
55                                 printf( "\tLDAP_DEBUG_CONFIG\t%d\n",
56                                     LDAP_DEBUG_CONFIG );
57                                 printf( "\tLDAP_DEBUG_ACL\t\t%d\n",
58                                     LDAP_DEBUG_ACL );
59                                 printf( "\tLDAP_DEBUG_STATS\t%d\n",
60                                     LDAP_DEBUG_STATS );
61                                 printf( "\tLDAP_DEBUG_STATS2\t%d\n",
62                                     LDAP_DEBUG_STATS2 );
63                                 printf( "\tLDAP_DEBUG_SHELL\t%d\n",
64                                     LDAP_DEBUG_SHELL );
65                                 printf( "\tLDAP_DEBUG_PARSE\t%d\n",
66                                     LDAP_DEBUG_PARSE );
67                                 printf( "\tLDAP_DEBUG_ANY\t\t%d\n",
68                                     LDAP_DEBUG_ANY );
69                                 exit( 0 );
70                         } else {
71                                 ldap_debug |= atoi( optarg );
72                                 lber_debug = (ldap_debug & LDAP_DEBUG_BER);
73                         }
74                         break;
75 #else
76                 case 'd':       /* turn on debugging */
77                         fprintf( stderr,
78                             "must compile with LDAP_DEBUG for debugging\n" );
79                         break;
80 #endif
81
82                 case 'f':       /* read config file */
83                         configfile = ch_strdup( optarg );
84                         break;
85
86                 case 'i':       /* run from inetd */
87                         inetd = 1;
88                         break;
89
90                 case 'p':       /* port on which to listen */
91                         port = atoi( optarg );
92                         break;
93
94                 case 's':       /* set syslog level */
95                         ldap_syslog = atoi( optarg );
96                         break;
97
98                 case 'u':       /* do udp */
99                         udp = 1;
100                         break;
101
102                 default:
103                         usage( argv[0] );
104                         exit( 1 );
105                 }
106         }
107
108         Debug( LDAP_DEBUG_TRACE, "%s", Versionstr, 0, 0 );
109
110         if ( (myname = strrchr( argv[0], '/' )) == NULL ) {
111                 myname = ch_strdup( argv[0] );
112         } else {
113                 myname = ch_strdup( myname + 1 );
114         }
115
116         if ( ! inetd ) {
117                 /* pre-open config file before detach in case it is a relative path */
118                 fp = fopen( configfile, "r" );
119 #ifdef LDAP_DEBUG
120                 lutil_detach( ldap_debug, 0 );
121 #else
122                 lutil_detach( 0, 0 );
123 #endif
124         }
125 #ifdef LOG_LOCAL4
126         openlog( myname, OPENLOG_OPTIONS, LOG_LOCAL4 );
127 #else
128         openlog( myname, OPENLOG_OPTIONS );
129 #endif
130
131         init();
132         read_config( configfile, &be, fp );
133
134         if ( ! inetd ) {
135                 int             status;
136
137                 time( &starttime );
138
139                 if ( pthread_create( &listener_tid, NULL, slapd_daemon,
140                     (void *) port ) != 0 ) {
141                         Debug( LDAP_DEBUG_ANY,
142                             "listener pthread_create failed\n", 0, 0, 0 );
143                         exit( 1 );
144                 }
145
146 #ifdef HAVE_PHREADS_FINAL
147                 pthread_join( listener_tid, (void *) NULL );
148 #else
149                 pthread_join( listener_tid, (void *) &status );
150 #endif
151
152                 return 0;
153
154         } else {
155                 Connection              c;
156                 Operation               *o;
157                 BerElement              ber;
158                 unsigned long           len, tag;
159                 long                    msgid;
160                 int                     flen;
161                 struct sockaddr_in      from;
162                 struct hostent          *hp;
163
164                 c.c_dn = NULL;
165                 c.c_ops = NULL;
166                 c.c_sb.sb_sd = 0;
167                 c.c_sb.sb_options = 0;
168                 c.c_sb.sb_naddr = udp ? 1 : 0;
169                 c.c_sb.sb_ber.ber_buf = NULL;
170                 c.c_sb.sb_ber.ber_ptr = NULL;
171                 c.c_sb.sb_ber.ber_end = NULL;
172                 pthread_mutex_init( &c.c_dnmutex, pthread_mutexattr_default );
173                 pthread_mutex_init( &c.c_opsmutex, pthread_mutexattr_default );
174                 pthread_mutex_init( &c.c_pdumutex, pthread_mutexattr_default );
175 #ifdef notdefcldap
176                 c.c_sb.sb_addrs = (void **) saddrlist;
177                 c.c_sb.sb_fromaddr = &faddr;
178                 c.c_sb.sb_useaddr = saddrlist[ 0 ] = &saddr;
179 #endif
180                 flen = sizeof(from);
181                 if ( getpeername( 0, (struct sockaddr *) &from, &flen ) == 0 ) {
182 #ifdef SLAPD_RLOOKUPS
183                         hp = gethostbyaddr( (char *) &(from.sin_addr.s_addr),
184                             sizeof(from.sin_addr.s_addr), AF_INET );
185 #else
186                         hp = NULL;
187 #endif
188
189                         Debug( LDAP_DEBUG_ARGS, "connection from %s (%s)\n",
190                             hp == NULL ? "unknown" : hp->h_name,
191                             inet_ntoa( from.sin_addr ), 0 );
192
193                         c.c_addr = inet_ntoa( from.sin_addr );
194                         c.c_domain = ch_strdup( hp == NULL ? "" : hp->h_name );
195                 } else {
196                         Debug( LDAP_DEBUG_ARGS, "connection from unknown\n",
197                             0, 0, 0 );
198                 }
199
200                 ber_init( &ber, 0 );
201                 while ( (tag = ber_get_next( &c.c_sb, &len, &ber ))
202                     == LDAP_TAG_MESSAGE ) {
203                         pthread_mutex_lock( &currenttime_mutex );
204                         time( &currenttime );
205                         pthread_mutex_unlock( &currenttime_mutex );
206
207                         if ( (tag = ber_get_int( &ber, &msgid ))
208                             != LDAP_TAG_MSGID ) {
209                                 /* log and send error */
210                                 Debug( LDAP_DEBUG_ANY,
211                                    "ber_get_int returns 0x%lx\n", tag, 0, 0 );
212                                 ber_free( &ber, 1 );
213                                 return 1;
214                         }
215
216                         if ( (tag = ber_peek_tag( &ber, &len ))
217                             == LBER_ERROR ) {
218                                 /* log, close and send error */
219                                 Debug( LDAP_DEBUG_ANY,
220                                    "ber_peek_tag returns 0x%lx\n", tag, 0, 0 );
221                                 ber_free( &ber, 1 );
222                                 close( c.c_sb.sb_sd );
223                                 c.c_sb.sb_sd = -1;
224                                 return 1;
225                         }
226
227                         connection_activity( &c );
228
229                         ber_free( &ber, 1 );
230                 }
231         }
232         return 1;
233 }