]> git.sur5r.net Git - openldap/blob - clients/gopher/go500.c
Added lber_get/set_option. Removed lber_debug/ldap_debug.
[openldap] / clients / gopher / go500.c
1 /*
2  * Copyright (c) 1990 Regents of the University of Michigan.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms are permitted
6  * provided that this notice is preserved and that due credit is given
7  * to the University of Michigan at Ann Arbor. The name of the University
8  * may not be used to endorse or promote products derived from this
9  * software without specific prior written permission. This software
10  * is provided ``as is'' without express or implied warranty.
11  */
12
13 #include "portable.h"
14
15 #include <stdio.h>
16 #include <stdlib.h>
17
18 #include <ac/ctype.h>
19 #include <ac/signal.h>
20 #include <ac/socket.h>
21 #include <ac/string.h>
22 #include <ac/syslog.h>
23 #include <ac/time.h>
24 #include <ac/unistd.h>
25 #include <ac/wait.h>
26
27 #include <ac/setproctitle.h>
28
29 #ifdef HAVE_SYS_PARAM_H
30 #include <sys/param.h>
31 #endif
32
33 #ifdef HAVE_SYS_RESOURCE_H
34 #include <sys/resource.h>
35 #endif
36
37 #include "ldapconfig.h"
38 #include "lber.h"
39 #include "ldap.h"
40
41 #define ldap_debug debug
42 #include "ldap_log.h"
43
44 #include "lutil.h"
45
46 #include "disptmpl.h"
47
48 int     debug;
49 int     dosyslog;
50 int     inetd;
51 int     dtblsize;
52
53 char    *ldaphost = NULL;
54 char    *base = NULL;
55 int     rdncount = GO500_RDNCOUNT;
56 char    *filterfile = FILTERFILE;
57 char    *templatefile = TEMPLATEFILE;
58
59 char    myhost[MAXHOSTNAMELEN];
60 int     myport;
61
62 static void usage       ( char *name );
63 static int  set_socket  (int port);
64 static RETSIGTYPE wait4child(int sig);
65 static void do_queries  (int s);
66 static void do_error    (FILE *fp, char *s);
67 static void do_search   (LDAP *ld, FILE *fp, char *buf);
68 static void do_read     (LDAP *ld, FILE *fp, char *dn);
69
70 static void
71 usage( char *name )
72 {
73         fprintf( stderr, "usage: %s [-d debuglevel] [-f filterfile] [-t templatefile]\r\n\t[-a] [-l] [-p port] [-x ldaphost] [-b searchbase] [-c rdncount]\r\n", name );
74         exit( 1 );
75 }
76
77 int
78 main( int argc, char **argv )
79 {
80         int                     s, ns, rc;
81         int                     port = -1;
82         int                     i, pid;
83         char                    *myname;
84         fd_set                  readfds;
85         struct hostent          *hp;
86         struct sockaddr_in      from;
87         int                     fromlen;
88
89 #if defined( LDAP_PROCTITLE ) && !defined( HAVE_SETPROCTITLE )
90         /* for setproctitle */
91         Argv = argv;
92         Argc = argc;
93 #endif
94
95         while ( (i = getopt( argc, argv, "b:d:f:lp:c:t:x:I" )) != EOF ) {
96                 switch( i ) {
97                 case 'b':       /* searchbase */
98                         base = strdup( optarg );
99                         break;
100
101                 case 'd':       /* debug level */
102                         debug |= atoi( optarg );
103                         break;
104
105                 case 'f':       /* ldap filter file */
106                         filterfile = strdup( optarg );
107                         break;
108
109                 case 'l':       /* log via LOG_LOCAL3 */
110                         dosyslog = 1;
111                         break;
112
113                 case 'p':       /* port to listen to */
114                         port = atoi( optarg );
115                         break;
116
117                 case 'c':       /* number of DN components to show */
118                         rdncount = atoi( optarg );
119                         break;
120
121                 case 't':       /* ldap template file */
122                         templatefile = strdup( optarg );
123                         break;
124
125                 case 'x':       /* ldap server hostname */
126                         ldaphost = strdup( optarg );
127                         break;
128
129                 case 'I':       /* run from inetd */
130                         inetd = 1;
131                         break;
132
133                 default:
134                         usage( argv[0] );
135                 }
136         }
137
138 #ifdef GO500_HOSTNAME
139         strcpy( myhost, GO500_HOSTNAME );
140 #else
141         if ( myhost[0] == '\0' && gethostname( myhost, sizeof(myhost) )
142             == -1 ) {
143                 perror( "gethostname" );
144                 exit( 1 );
145         }
146 #endif
147
148 #ifdef HAVE_SYSCONF
149         dtblsize = sysconf( _SC_OPEN_MAX );
150 #elif HAVE_GETDTABLESIZE
151         dtblsize = getdtablesize();
152 #else
153         dtblsize = FD_SETSIZE;
154 #endif
155
156 #ifdef FD_SETSIZE
157         if (dtblsize > FD_SETSIZE) {
158                 dtblsize = FD_SETSIZE;
159         }
160 #endif  /* FD_SETSIZE*/
161
162         /* detach if stderr is redirected or no debugging */
163         if ( inetd == 0 )
164                 lutil_detach( debug && !isatty( 1 ), 1 );
165
166         if ( (myname = strrchr( argv[0], '/' )) == NULL )
167                 myname = strdup( argv[0] );
168         else
169                 myname = strdup( myname + 1 );
170
171         if ( debug ) {
172                 lber_set_option(NULL, LDAP_OPT_DEBUG_LEVEL, &debug);
173                 ldap_set_option(NULL, LDAP_OPT_DEBUG_LEVEL, &debug);
174         }
175         
176         if ( dosyslog ) {
177 #ifdef LOG_LOCAL3
178                 openlog( myname, OPENLOG_OPTIONS, LOG_LOCAL3 );
179 #else
180                 openlog( myname, OPENLOG_OPTIONS );
181 #endif
182         }
183         if ( dosyslog )
184                 syslog( LOG_INFO, "initializing" );
185
186         /* set up the socket to listen on */
187         if ( inetd == 0 ) {
188                 s = set_socket( port );
189
190                 /* arrange to reap children */
191                 (void) SIGNAL( SIGCHLD, wait4child );
192         } else {
193                 myport = GO500_PORT;
194
195                 fromlen = sizeof(from);
196                 if ( getpeername( 0, (struct sockaddr *) &from, &fromlen )
197                     == 0 ) {
198                         hp = gethostbyaddr( (char *) &(from.sin_addr.s_addr),
199                             sizeof(from.sin_addr.s_addr), AF_INET );
200                         Debug( LDAP_DEBUG_ARGS, "connection from %s (%s)\n",
201                             (hp == NULL) ? "unknown" : hp->h_name,
202                             inet_ntoa( from.sin_addr ), 0 );
203
204                         if ( dosyslog ) {
205                                 syslog( LOG_INFO, "connection from %s (%s)",
206                                     (hp == NULL) ? "unknown" : hp->h_name,
207                                     inet_ntoa( from.sin_addr ) );
208                         }
209
210 #ifdef LDAP_PROCTITLE
211                         setproctitle( hp == NULL ? inet_ntoa( from.sin_addr ) :
212                             hp->h_name );
213 #endif
214                 }
215
216                 do_queries( 0 );
217
218                 exit( 0 );
219         }
220
221         for ( ;; ) {
222                 FD_ZERO( &readfds );
223                 FD_SET( s, &readfds );
224
225                 if ( (rc = select( dtblsize, &readfds, 0, 0 ,0 )) == -1 ) {
226                         if ( debug ) perror( "select" );
227                         continue;
228                 } else if ( rc == 0 ) {
229                         continue;
230                 }
231
232                 if ( ! FD_ISSET( s, &readfds ) )
233                         continue;
234
235                 fromlen = sizeof(from);
236                 if ( (ns = accept( s, (struct sockaddr *) &from, &fromlen ))
237                     == -1 ) {
238                         if ( debug ) perror( "accept" );
239                         exit( 1 );
240                 }
241
242                 hp = gethostbyaddr( (char *) &(from.sin_addr.s_addr),
243                     sizeof(from.sin_addr.s_addr), AF_INET );
244
245                 if ( dosyslog ) {
246                         syslog( LOG_INFO, "TCP connection from %s (%s)",
247                             (hp == NULL) ? "unknown" : hp->h_name,
248                             inet_ntoa( from.sin_addr ) );
249                 }
250
251                 switch( pid = fork() ) {
252                 case 0:         /* child */
253                         close( s );
254                         do_queries( ns );
255                         break;
256
257                 case -1:        /* failed */
258                         perror( "fork" );
259                         break;
260
261                 default:        /* parent */
262                         close( ns );
263                         if ( debug )
264                                 fprintf( stderr, "forked child %d\n", pid );
265                         break;
266                 }
267         }
268         /* NOT REACHED */
269 }
270
271 static int
272 set_socket( int port )
273 {
274         int                     s, one;
275         struct sockaddr_in      addr;
276
277         if ( port == -1 )
278                 port = GO500_PORT;
279         myport = port;
280
281         if ( (s = socket( AF_INET, SOCK_STREAM, 0 )) == -1 ) {
282                 perror( "socket" );
283                 exit( 1 );
284         }
285
286         /* set option so clients can't keep us from coming back up */
287         one = 1;
288         if ( setsockopt( s, SOL_SOCKET, SO_REUSEADDR, (char *) &one,
289             sizeof(one) ) < 0 ) {
290                 perror( "setsockopt" );
291                 exit( 1 );
292         }
293
294         /* bind to a name */
295         addr.sin_family = AF_INET;
296         addr.sin_addr.s_addr = INADDR_ANY;
297         addr.sin_port = htons( port );
298         if ( bind( s, (struct sockaddr *) &addr, sizeof(addr) ) ) {
299                 perror( "bind" );
300                 exit( 1 );
301         }
302
303         /* listen for connections */
304         if ( listen( s, 5 ) == -1 ) {
305                 perror( "listen" );
306                 exit( 1 );
307         }
308
309         if ( debug ) printf("tcp socket allocated, bound, and listening\n");
310
311         return( s );
312 }
313
314 static RETSIGTYPE
315 wait4child( int sig )
316 {
317 #ifndef HAVE_WAITPID
318         WAITSTATUSTYPE     status;
319 #endif
320
321         if ( debug ) printf( "parent: catching child status\n" );
322
323 #ifdef HAVE_WAITPID
324         while (waitpid ((pid_t) -1, 0, WAIT_FLAGS) > 0)
325                 ;       /* NULL */
326 #else
327         while ( wait3( &status, WAIT_FLAGS, 0 ) > 0 )
328                 ;       /* NULL */
329 #endif
330
331         (void) SIGNAL( SIGCHLD, wait4child );
332 }
333
334 static void
335 do_queries( int s )
336 {
337         char            buf[1024], *query;
338         int             len;
339         FILE            *fp;
340         int             rc;
341         struct timeval  timeout;
342         fd_set          readfds;
343         LDAP            *ld;
344
345         if ( (fp = fdopen( s, "a+")) == NULL ) {
346                 exit( 1 );
347         }
348
349         timeout.tv_sec = GO500_TIMEOUT;
350         timeout.tv_usec = 0;
351         FD_ZERO( &readfds );
352         FD_SET( fileno( fp ), &readfds );
353
354         if ( (rc = select( dtblsize, &readfds, 0, 0, &timeout )) <= 0 )
355                 exit( 1 );
356
357         if ( fgets( buf, sizeof(buf), fp ) == NULL )
358                 exit( 1 );
359
360         len = strlen( buf );
361         if ( debug ) {
362                 fprintf( stderr, "got %d bytes\n", len );
363 #ifdef LDAP_DEBUG
364                 lber_bprint( buf, len );
365 #endif
366         }
367
368         /* strip of \r \n */
369         if ( buf[len - 1] == '\n' )
370                 buf[len - 1] = '\0';
371         len--;
372         if ( buf[len - 1] == '\r' )
373                 buf[len - 1] = '\0';
374         len--;
375
376         query = buf;
377
378         /* strip off leading white space */
379         while ( isspace( *query )) {
380                 ++query;
381                 --len;
382         }
383
384         rewind(fp);
385
386         if ( *query == '~' || *query == '@' ) {
387                 ld = NULL;
388         } else if ( (ld = ldap_open( ldaphost, 0 )) == NULL ) {
389                 fprintf(fp,
390                         "0An error occurred (explanation)\t@%d\t%s\t%d\r\n",
391                         LDAP_SERVER_DOWN, myhost, myport );
392                 fprintf( fp, ".\r\n" );
393                 rewind(fp);
394                 exit( 1 );
395         } else {
396                 int deref = GO500_DEREF;
397                 ldap_set_option(ld, LDAP_OPT_DEREF, &deref);
398
399                 rc = ldap_simple_bind_s( ld, NULL, NULL );
400                 if ( rc != LDAP_SUCCESS ) {
401                         fprintf(fp,
402                             "0An error occurred (explanation)\t@%d\t%s\t%d\r\n",
403                             rc, myhost, myport );
404                         fprintf( fp, ".\r\n" );
405                         rewind(fp);
406                         exit( 1 );
407                 }
408         }
409
410         switch ( *query ) {
411         case '~':
412                 fprintf( fp, "The query you specified was not specific enough, causing a size limit\r\n" );
413                 fprintf( fp, "to be exceeded and the first several matches found to be returned.\r\n" );
414                 fprintf( fp, "If you did not find the match you were looking for, try issuing a more\r\n" );
415                 fprintf( fp, "specific query, for example one that contains both first and last name.\r\n" );
416                 fprintf( fp, ".\r\n" );
417                 break;
418
419         case '=':
420                 do_read( ld, fp, ++query );
421                 break;
422
423         case '@':
424                 do_error( fp, ++query );
425                 break;
426
427         default:
428                 do_search( ld, fp, query );
429                 break;
430         }
431
432         fprintf( fp, ".\r\n" );
433         rewind(fp);
434
435         if ( ld != NULL) {
436                 ldap_unbind( ld );
437         }
438
439         exit( 1 );
440         /* NOT REACHED */
441 }
442
443 static void
444 do_error( FILE *fp, char *s )
445 {
446         int     code;
447
448         code = atoi( s );
449
450         fprintf( fp, "An error occurred searching X.500.  The error code was %d\r\n", code );
451         fprintf( fp, "The corresponding error is: %s\r\n", ldap_err2string( code ) );
452         fprintf( fp, "No additional information is available\r\n" );
453         fprintf( fp, ".\r\n" );
454 }
455
456 static void
457 do_search( LDAP *ld, FILE *fp, char *buf )
458 {
459         char            *dn, *rdn;
460         char            **title;
461         int             rc, matches = 0;
462         struct timeval  tv;
463         LDAPFiltInfo    *fi;
464         LDAPFiltDesc    *filtd;
465         LDAPMessage     *e, *res;
466         static char     *attrs[] = { "title", 0 };
467
468 #ifdef GO500_UFN
469         if ( strchr( buf, ',' ) != NULL ) {
470                 ldap_ufn_setprefix( ld, base );
471                 tv.tv_sec = GO500_TIMEOUT;
472                 tv.tv_usec = 0;
473                 ldap_ufn_timeout( (void *) &tv );
474
475                 if ( (rc = ldap_ufn_search_s( ld, buf, attrs, 0, &res ))
476                     != LDAP_SUCCESS && rc != LDAP_SIZELIMIT_EXCEEDED ) {
477                         fprintf(fp,
478                             "0An error occurred (explanation)\t@%d\t%s\t%d\r\n",
479                             rc, myhost, myport );
480                         return;
481                 }
482
483                 matches = ldap_count_entries( ld, res );
484         } else {
485 #endif
486                 if ( (filtd = ldap_init_getfilter( filterfile )) == NULL ) {
487                         fprintf( stderr, "Cannot open filter file (%s)\n",
488                             filterfile );
489                         exit( 1 );
490                 }
491
492                 tv.tv_sec = GO500_TIMEOUT;
493                 tv.tv_usec = 0;
494                 for ( fi = ldap_getfirstfilter( filtd, "go500", buf );
495                     fi != NULL;
496                     fi = ldap_getnextfilter( filtd ) )
497                 {
498                         if ( (rc = ldap_search_st( ld, base, LDAP_SCOPE_SUBTREE,
499                             fi->lfi_filter, attrs, 0, &tv, &res ))
500                             != LDAP_SUCCESS && rc != LDAP_SIZELIMIT_EXCEEDED ) {
501                                 fprintf(fp, "0An error occurred (explanation)\t@%d\t%s\t%d\r\n",
502                                     rc, myhost, myport );
503                                 ldap_getfilter_free( filtd );
504                                 return;
505                         }
506
507                         if ( (matches = ldap_count_entries( ld, res )) != 0 )
508                                 break;
509                 }
510                 ldap_getfilter_free( filtd );
511 #ifdef GO500_UFN
512         }
513 #endif
514
515         if ( matches <= 0 ) {
516                 return;
517         }
518
519 #ifdef GO500_SORT_ATTR
520         ldap_sort_entries( ld, &res, GO500_SORT_ATTR, strcasecmp );
521 #endif
522
523         for ( e = ldap_first_entry( ld, res ); e != NULL;
524             e = ldap_next_entry( ld, e ) ) {
525                 char    *s;
526
527                 dn = ldap_get_dn( ld, e );
528                 rdn = strdup( dn );
529                 if ( (s = strchr( rdn, ',' )) != NULL )
530                         *s = '\0';
531
532                 if ( (s = strchr( rdn, '=' )) == NULL )
533                         s = rdn;
534                 else
535                         ++s;
536
537                 title = ldap_get_values( ld, e, "title" );
538
539                 if ( title != NULL ) {
540                         char    *p;
541
542                         for ( p = title[0]; *p; p++ ) {
543                                 if ( *p == '/' )
544                                         *p = '\\';
545                         }
546                 }
547
548                 fprintf( fp, "0%-20s    %s\t=%s\t%s\t%d\r\n", s,
549                     title ? title[0] : "", dn, myhost, myport );
550
551                 if ( title != NULL )
552                         ldap_value_free( title );
553
554                 free( rdn );
555                 free( dn );
556         }
557
558         if ( ldap_result2error( ld, res, 1 ) == LDAP_SIZELIMIT_EXCEEDED ) {
559                 fprintf( fp, "0A size limit was exceeded (explanation)\t~\t%s\t%d\r\n",
560                     myhost, myport );
561         }
562 }
563
564 static int
565 entry2textwrite( void *fp, char *buf, int len )
566 {
567         return( fwrite( buf, len, 1, (FILE *)fp ) == 0 ? -1 : len );
568 }
569
570 static void
571 do_read( LDAP *ld, FILE *fp, char *dn )
572 {
573         static struct ldap_disptmpl *tmpllist;
574
575         ldap_init_templates( templatefile, &tmpllist );
576
577         if ( ldap_entry2text_search( ld, dn, base, NULL, tmpllist, NULL, NULL,
578             entry2textwrite, (void *) fp, "\r\n", rdncount,
579             LDAP_DISP_OPT_DOSEARCHACTIONS ) != LDAP_SUCCESS ) {
580                 ldap_perror( ld, "ldap_entry2text_search" );
581                 exit( 1 );
582         }
583
584         if ( tmpllist != NULL ) {
585                 ldap_free_templates( tmpllist );
586         }
587 }