]> git.sur5r.net Git - openldap/blob - clients/ud/main.c
42395a042ec1f19ec997bf785ee836974c154264
[openldap] / clients / ud / main.c
1 /*
2  * Copyright 1998-1999 The OpenLDAP Foundation, All Rights Reserved.
3  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
4  */
5 /*
6  * Copyright (c) 1991, 1992, 1993 
7  * Regents of the University of Michigan.  All rights reserved.
8  *
9  * Redistribution and use in source and binary forms are permitted
10  * provided that this notice is preserved and that due credit is given
11  * to the University of Michigan at Ann Arbor. The name of the University
12  * may not be used to endorse or promote products derived from this
13  * software without specific prior written permission. This software
14  * is provided ``as is'' without express or implied warranty.
15  *
16  * The University of Michigan would like to thank the following people for
17  * their contributions to this piece of software:
18  *
19  *      Robert Urquhart    <robert@sfu.ca>
20  *      Simon Fraser University, Academic Computing Services
21  */
22
23 #include "portable.h"
24
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <setjmp.h>
28
29 #ifdef HAVE_PWD_H
30 #include <pwd.h>
31 #endif
32
33 #include <ac/signal.h>
34 #include <ac/string.h>
35 #include <ac/ctype.h>
36 #include <ac/termios.h>
37 #include <ac/time.h>
38 #include <ac/unistd.h>
39
40 #ifdef HAVE_SYS_FILE_H
41 #include <sys/file.h>
42 #endif
43
44 #include <lber.h>
45 #include <ldap.h>
46
47 #include <ldapconfig.h>
48 #include "ud.h"
49
50 /*
51  *  Used with change_base() to indicate which base we are changing.
52  */
53 #define BASE_SEARCH     0
54 #define BASE_GROUPS     1
55
56 #define iscom(x)        (!strncasecmp((x), cmd, strlen(cmd)))
57
58 static char *server = NULL;
59 static char *config_file = UD_CONFIG_FILE;
60 static char *filter_file = FILTERFILE;
61 static int ldap_port = 0;
62 static int dereference = TRUE;
63
64 char *default_bind_object = NULL;
65
66 char *bound_dn;                 /* bound user's Distinguished Name */
67 char *group_base;               /* place in LDAP tree where groups are */
68 char *search_base;              /* place in LDAP tree where searches start */
69
70 static jmp_buf env;             /* spot to jump to on an interrupt */
71
72 int lpp;                        /* lines per page */
73 int verbose;                    /* 1 if verbose mode on */
74 int col_size;                   /* characters across on the screen */
75 int bind_status;                /* user's bind status */
76
77 LDAP *ld;                       /* LDAP descriptor */
78 LDAPFiltDesc *lfdp;             /* LDAP filter descriptor */
79
80 #ifdef DEBUG
81 int debug;                      /* debug flag */
82 #endif
83 int ldebug;                     /* library debug flag */
84
85 #ifndef HAVE_MKVERSION
86 char Version[] = "OpenLDAP UserDirectory (ud)";
87 #endif
88
89 int
90 main( int argc, char **argv )
91 {
92         register int c;                         /* for parsing argv */
93         register char *cp;                      /* for parsing Version */
94
95         verbose = 1;
96
97         /*  handle argument list */
98         while ((c = getopt(argc, argv, "c:d:Df:l:p:s:u:vV")) != -1) {
99                 switch (c) {
100                 case 'l' :
101                         ldebug |= (int) strtol(optarg, (char **) NULL, 0);
102                         break;
103                 case 'd' :
104 #ifdef DEBUG
105                         debug |= (int) strtol(optarg, (char **) NULL, 0);
106 #endif
107                         break;
108                 case 's' :
109                         server = strdup(optarg);
110                         break;
111                 case 'c' :
112                         filter_file = strdup(optarg);
113                         break;
114                 case 'f' :
115                         config_file = optarg;
116                         break;
117                 case 'p' :
118                         ldap_port = atoi(optarg);
119                         break;
120                 case 'u' :
121                         default_bind_object = strdup(optarg);
122                         break;
123                 case 'v' :
124                         verbose = 1;    /* this is the default anyways... */
125                         break;
126                 case 'V' :
127                         verbose = 0;
128                         break;
129                 case 'D' :
130                         printf("\n\n  Debug flag values\n\n");
131                         printf("    1  function trace\n");
132                         printf("    2  find() information\n");
133                         printf("    4  group information\n");
134                         printf("    8  mod() information\n");
135                         printf("   16  parsing information\n");
136                         printf("   32  output information\n");
137                         printf("   64  authentication information\n");
138                         printf("  128  initialization information\n\n");
139                         format("These are masks, and may be added to form multiple debug levels.  For example, '-d 35' would perform a function trace, print out information about the find() function, and would print out information about the output routines too.", 75, 2);
140                         exit(0);
141                 default:
142                         fprintf(stderr, "Usage: %s [-c filter-config-file] [-d debug-level] [-l ldap-debug-level] [-s server] [-p port] [-V]\n", argv[0]);
143                         exit(-1);
144                         /* NOTREACHED */
145                 }
146         }
147
148         /* just print the first line of Version[] */
149         cp = strchr(Version, '\t');
150         if (cp != NULL)
151                 *cp = '\0';
152         printf(Version);
153         fflush( stdout );
154
155 #ifdef SIGPIPE
156         (void) SIGNAL (SIGPIPE, SIG_IGN);
157 #endif
158
159         initialize_client();
160         initialize_attribute_strings();
161
162         /* now tackle the user's commands */
163         do_commands();
164         /* NOTREACHED */
165
166         return 0;
167 }
168
169 void
170 do_commands( void )
171 {
172         LDAPMessage *mp;                        /* returned by find() */
173         register char *cp;                      /* misc char pointer */
174         register char *ap;                      /* misc char pointer */
175         static char cmd[MED_BUF_SIZE];          /* holds the command */
176         static char input[MED_BUF_SIZE];        /* buffer for input */
177
178 #ifdef DEBUG
179         if (debug & D_TRACE)
180                 printf("->do_commands()\n");
181 #endif
182         if (verbose) 
183                 printf("\n  Enter a command.  If you need help, type 'h' or '?' and hit RETURN.\n\n");
184         /* jump here on an interrupt */
185         (void) setjmp(env);
186         for (;;) {
187                 printf("* ");
188                 fflush(stdout);
189                 cp = input;
190 /* Temporary kludge - if cp is null, dumps core under Solaris */
191                 if (cp == NULL)
192                         break;
193                 fetch_buffer(input, sizeof(input), stdin);
194                 if (*input == '\0') {
195                         putchar('\n');
196                         continue;
197                 }
198                 while (isspace((unsigned char)*cp))
199                         cp++;   
200                 ap = cmd;
201                 if (memset(cmd, '\0', sizeof(cmd)) == NULL)
202                         fatal("memset");
203                 while (!isspace((unsigned char)*cp) && (*cp != '\0'))
204                         *ap++ = *cp++;
205                 if (iscom("status"))
206                         status();
207                 else if (iscom("stop") || iscom("quit"))
208                         break;
209                 else if (iscom("cb") || iscom("cd") || iscom("moveto")) {
210                         while (isspace((unsigned char)*cp) && (*cp != '\0'))
211                                 cp++;
212                         if (!strncasecmp(cp, "base", 4))
213                                 cp += 4;
214                         change_base(BASE_SEARCH, &search_base, nextstr(cp));
215                 }
216                 else if (iscom("memberships"))
217                         (void) list_memberships(nextstr(cp));
218                 else if (iscom("list"))
219                         (void) list_groups(nextstr(cp));
220                 else if (iscom("groupbase"))
221                         change_base(BASE_GROUPS, &group_base, nextstr(cp));
222                 else if (iscom("find") || iscom("display") || iscom("show")) {
223                         cp = nextstr(cp);
224                         if ((mp = find(cp, FALSE)) != NULL) {
225                                 parse_answer(mp);
226                                 print_an_entry();
227                                 ldap_msgfree(mp);
228                         }
229                         else
230                                 printf(" Could not find \"%s\".\n", cp);
231                 }
232 #ifdef UOFM
233                 else if (iscom("vedit") && isatty( 1 )) {
234 #else
235                 else if (iscom("vedit")) {
236 #endif
237                         (void) edit(nextstr(cp));
238                 }
239                 else if (iscom("modify") || iscom("change") || iscom("alter"))
240                         (void) modify(nextstr(cp));
241                 else if (iscom("bind") || iscom("iam"))
242                         (void) auth(nextstr(cp), 0);
243                 else if ((cmd[0] == '?') || iscom("help"))
244                         print_help(nextstr(cp));
245                 else if (iscom("join") || iscom("subscribe")) 
246                         (void) x_group(G_JOIN, nextstr(cp));
247                 else if (iscom("resign") || iscom("unsubscribe"))
248                         (void) x_group(G_RESIGN, nextstr(cp));
249                 else if (!strncasecmp("create", cmd, strlen(cmd)))
250                         add_group(nextstr(cp));
251                 else if (!strncasecmp("remove", cmd, strlen(cmd)))
252                         remove_group(nextstr(cp));
253                 else if (!strncasecmp("purge", cmd, strlen(cmd)))
254                         purge_group(nextstr(cp));
255                 else if (!strncasecmp("verbose", cmd, strlen(cmd))) {
256                         verbose = 1 - verbose;
257                         if (verbose)
258                                 printf("  Verbose mode has been turned on.\n");
259                 }
260                 else if (!strncasecmp("dereference", cmd, strlen(cmd))) {
261                         int deref;
262                         dereference = 1 - dereference;
263                         if (dereference == 1) {
264                                 deref = LDAP_DEREF_ALWAYS;
265                         } else {
266                                 deref = LDAP_DEREF_NEVER;
267                         }
268                         ldap_set_option(ld, LDAP_OPT_DEREF, (void *) &deref);
269                 }
270                 else if (!strncasecmp("tidy", cmd, strlen(cmd)))
271                         tidy_up();
272                 else if (cmd[0] == '\0')
273                         putchar('\n');
274                 else
275                         printf("  Invalid command.  Type \"help commands.\"\n");
276         }
277         printf(" Thank you!\n");
278         
279         ldap_unbind(ld);
280 #ifdef HAVE_KERBEROS
281         destroy_tickets();
282 #endif
283         exit(0);
284         /* NOTREACHED */
285 }
286
287 void
288 status( void )
289 {
290         register char **rdns;
291
292 #ifdef DEBUG
293         if (debug & D_TRACE)
294                 printf("->status()\n");
295 #endif
296         printf("  Current server is %s", server);
297         if ( ld != NULL ) {
298                 char *host = NULL;
299                 
300                 ldap_get_option(ld, LDAP_OPT_HOST_NAME, &host);
301
302                 if (( host != NULL ) && (strcasecmp( host, server ) != 0 )) {
303                         printf( " (%s)", host );
304                 }
305         }
306         putchar( '\n' );
307         printbase("  Search base is ", search_base);
308         printbase("  Group  base is ", group_base);
309         if ( bound_dn != NULL ) {
310                 rdns = ldap_explode_dn(bound_dn, TRUE);
311                 printf("  Bound as \"%s\"\n", *rdns);
312                 ldap_value_free(rdns);
313         } else {
314                 printf("  Bound as Nobody\n" );
315         }
316         printf( "  Verbose mode is %sabled\n", ( verbose ? "en" : "dis" ));
317         if ( ld != NULL ) {
318                 int deref = LDAP_DEREF_NEVER;
319                 ldap_get_option(ld, LDAP_OPT_DEREF, &deref);
320                 printf( "  Aliases are %sbeing dereferenced\n",
321                         ( deref == LDAP_DEREF_ALWAYS ) ? "" : "not" );
322         }
323 }
324
325 void
326 change_base( int type, char **base, char *s )
327 {
328         register char *cp;                      /* utility pointers */
329         char **rdns;                            /* for parsing */
330         char *output_string;                    /* for nice output */
331         int num_picked;                         /* # of selected base */
332         int j;                                  /* used with num_picked */
333         int i = 1;                              /* index into choices array */
334         int matches;                            /* # of matches found */
335         int rest = 1;                           /* # left to display */
336         char tmp[MED_BUF_SIZE];                 /* temporary buffer */
337         static char *choices[MED_BUF_SIZE];     /* bases from which to choose */
338         static char resp[SMALL_BUF_SIZE];       /* for prompting user */
339         static char buf[MED_BUF_SIZE];
340         static char *attrs[] = { "objectClass", NULL };
341         LDAPMessage *mp;                        /* results from a search */
342         LDAPMessage *ep;                        /* for going thru bases */
343
344 #ifdef DEBUG
345         if (debug & D_TRACE)
346                 printf("->change_base(%s, %s)\n", s, s);
347 #endif
348         /*
349          *  If s is NULL we need to prompt the user for an argument.
350          */
351         while (s == NULL) {
352                 if (verbose) {
353                         printf("  You need to specify how the base is to be changed.  Valid choices are:\n");
354                         printf("     ?       - list the choices immediately below this level\n");
355                         printf("     ..      - move up one level in the Directory tree\n");
356                         printf("     root    - move to the root of the Directory tree\n");
357                         printf("     default - move to the default level built into this program\n");
358                         printf("     <entry> - move to the entry specified\n");
359                 }
360                 printf("  Change base to? ");
361                 fflush(stdout);
362                 fetch_buffer(buf, sizeof(buf), stdin);
363                 if ((buf != NULL) && (buf[0] != '\0'))
364                         s = buf;
365         }
366
367         /* set the output string */
368         if (type == BASE_SEARCH)
369                 output_string = "  Search base is now ";
370         else if (type == BASE_GROUPS)
371                 output_string = "  Group base is now ";
372
373         if (!strcasecmp(s, "root")) {
374                 StrFreeDup(base, NULL);
375                 printbase("  Search base is ", *base);
376                 return;
377         }
378
379         /*
380          *  User wants to ascend one level in the LDAP tree.
381          *  Easy:  Just strip off the first element of the
382          *  current search base, unless it's the root, in
383          *  which case we just do nothing.
384          */
385         if (!strcasecmp(s, "..")) {
386                 if (*base == NULL) {
387                         printf("  You are already at the root\n");
388                         return;
389                 }
390                 cp = strchr(*base, '=');
391                 cp++;
392                 /*
393                  *  If there isn't a second "=" in the base, then this was
394                  *  a one element base, and so now it should be NULL.
395                  */
396                 if ((cp = strchr(cp, '=')) == NULL)
397                         StrFreeDup(base, NULL);
398                 else {
399                         /*
400                          *  Back up to the start of this
401                          *
402                          *      attr=value
403                          *
404                          *  sequence now that 'cp' is pointing to the '='.
405                          */
406                         while(!isspace((unsigned char)*cp))
407                                 cp--;
408                         cp++;
409                         /*
410                          *  Goofy, but need to do it this way since both *base
411                          *  and cp point into the same chunk of memory, and
412                          *  we want to free *base, but keep part of it around.
413                          */
414                         cp = strdup(cp);
415                         StrFreeDup(base, cp);
416                         Free(cp);
417                 }
418                 printbase(output_string, *base);
419                 return;
420         }
421
422         /* user wants to see what is directly below this level */
423         if (*s == '?') {
424                 /*
425                  *  Fetch the list of entries directly below this level.
426                  *  Once we have the list, we will print it for the user, one
427                  *  screenful at a time.  At the end of each screen, we ask
428                  *  the user if they want to see more.  They can also just
429                  *  type a number at that point too.
430                  */
431                 if (ldap_search_s(ld, *base, LDAP_SCOPE_ONELEVEL, "(|(objectClass=quipuNonLeafObject)(objectClass=externalNonLeafObject))", attrs, FALSE, &mp) != LDAP_SUCCESS) {
432                         int ld_errno = 0;
433                         ldap_get_option(ld, LDAP_OPT_ERROR_NUMBER, &ld_errno);
434                         if ((ld_errno == LDAP_TIMELIMIT_EXCEEDED) ||
435                             (ld_errno == LDAP_SIZELIMIT_EXCEEDED)) {
436                                 if (verbose) {
437                                         printf("  Your query was too general and a limit was exceeded.  The results listed\n");
438                                         printf("  are not complete.  You may want to try again with a more refined query.\n\n");
439                                 }
440                                 else
441                                         printf("  Time or size limit exceeded.  Partial results follow.\n\n");
442                         } else {
443                                 ldap_perror(ld, "ldap_search_s");
444                                 return;
445                         }
446                 }
447                 if ((matches = ldap_count_entries(ld, mp)) < 1) {
448                         printf("  There is nothing below this level.\n");
449                         (void) ldap_msgfree(mp);
450                         return;
451                 }
452                 num_picked = 0;
453                 printf(" There are %d choices:\n", matches);
454                 for (ep = ldap_first_entry(ld, mp); ep != NULL; ep = ldap_next_entry(ld, ep)) {
455                         /*
456                          *  Put the last component of the DN into 'lastDN'.
457                          *  If we are at the root level, convert any country
458                          *  codes to recognizable names for printing.
459                          */
460                         choices[i] = ldap_get_dn(ld, ep);
461                         rdns = ldap_explode_dn(choices[i], TRUE);
462                         printf(" %2d. %s\n", i, friendly_name(*rdns));
463                         (void) ldap_value_free(rdns);
464                         i++;
465                         if ((rest++ > (lpp - 3)) && (i < matches)) {
466                                 printf("More? ");
467                                 fflush(stdout);
468                                 fetch_buffer(resp, sizeof(resp), stdin);
469                                 if ((resp[0] == 'n') || (resp[0] == 'N'))
470                                         break;
471                                 else if (((num_picked = atoi(resp)) != 0) && (num_picked < i))
472                                         break;
473                                 rest = 1;
474                         }
475                 }
476                 for (;;) {
477                         if (num_picked != 0) {
478                                 j = num_picked;
479                                 num_picked = 0;
480                         }
481                         else {
482                                 printf(" Which number? ");
483                                 fflush(stdout);
484                                 fetch_buffer(resp, sizeof(resp), stdin);
485                                 j = atoi(resp);
486                         }
487                         if (j == 0) {
488                                 (void) ldap_msgfree(mp);
489                                 for (i = 0; i < matches; i++)
490                                         ldap_memfree(choices[i]);
491                                 return;
492                         }
493                         if ((j < 1) || (j >= i))
494                                 printf(" Invalid number\n");
495                         else {
496                                 StrFreeDup(base, choices[j]);
497                                 printbase(output_string, *base);
498                                 (void) ldap_msgfree(mp);
499                                 for (i = 0; choices[i] != NULL; i++)
500                                         ldap_memfree(choices[i]);
501                                 return;
502                         }
503                 }
504         }
505         /* set the search base back to the original default value */
506         else if (!strcasecmp(s, "default")) {
507                 if (type == BASE_SEARCH)
508                         StrFreeDup(base, NULL);
509                 else if (type == BASE_GROUPS)
510                         StrFreeDup(base, UD_WHERE_GROUPS_ARE_CREATED);
511                 printbase(output_string, *base);
512         }
513         /* they typed in something -- see if it is legit */
514         else {
515                 /* user cannot do something like 'cb 33' */
516                 if (atoi(s) != 0) {
517                         printf("  \"%s\" is not a valid search base\n", s);
518                         printf("  Base unchanged.\n");
519                         printf("  Try using 'cb ?'\n");
520                         return;
521                 }
522                 /* was it a fully-specified DN? */
523                 if (vrfy(s)) {
524                         StrFreeDup(base, s);
525                         printbase(output_string, *base);
526                         return;
527                 }
528                 /* was it a RDN relative to the current base? */
529                 sprintf(tmp, "ou=%s, %s", s, *base);
530                 if (vrfy(tmp)) {
531                         StrFreeDup(base, tmp);
532                         printbase(output_string, *base);
533                         return;
534                 }
535                 printf("  \"%s\" is not a valid base\n  Base unchanged.\n", s);
536         }
537 }
538
539 void
540 initialize_client( void )
541 {
542         FILE *fp;                               /* for config file */
543         static char buffer[MED_BUF_SIZE];       /* for input */
544 #ifdef HAVE_GETPWUID
545         struct passwd *pw;                      /* for getting the home dir */
546 #endif
547         register char *cp;                      /* for fiddling with buffer */
548         char *config;                           /* config file to use */
549         static char bp[1024];                   /* for tty set-up */
550
551 #ifdef DEBUG
552         if (debug & D_TRACE)
553                 printf("->initialize_client()\n");
554 #endif
555
556         if (ldebug) {
557                 ber_set_option(NULL, LBER_OPT_DEBUG_LEVEL, &ldebug);
558                 ldap_set_option(NULL, LDAP_OPT_DEBUG_LEVEL, &ldebug);
559         }
560
561         /*
562          *  A per-user config file has precedence over any system-wide
563          *  config file, if one exists.
564          */
565 #ifdef HAVE_GETPWUID
566         if ((pw = getpwuid((uid_t) geteuid())) == (struct passwd *) NULL)
567                 config = config_file;
568         else {
569                 if (pw->pw_dir == NULL)
570                         config = config_file;
571                 else {
572                         sprintf(buffer, "%s/%s", pw->pw_dir,
573                             UD_USER_CONFIG_FILE);
574                         if (access(buffer, R_OK) == 0)
575                                 config = buffer;
576                         else
577                                 config = config_file;
578                 }
579         }
580 #else
581         config = config_file;
582 #endif /* getpwduid() */
583 #ifdef DEBUG
584         if (debug & D_INITIALIZE)
585                 printf("Using config file %s\n", config);
586 #endif
587
588         /*
589          *  If there is a config file, read it.
590          *
591          *  Could have lines that look like this:
592          *
593          *      server <ip-address or domain-name>
594          *      base   <default search base>
595          *      groupbase <default place where groups are created>
596          *
597          */
598         if ((fp = fopen(config, "r")) != NULL) {
599                 while (fgets(buffer, sizeof(buffer), fp) != NULL) {
600                         buffer[strlen(buffer) - 1] = '\0';
601                         if (!strncasecmp(buffer, "server", 6)) {
602                                 if (server != NULL)
603                                         continue;
604                                 cp = buffer + 6;
605                                 while (isspace((unsigned char)*cp))
606                                         cp++;
607                                 if ((*cp == '\0') || (*cp == '\n'))
608                                         continue;
609                                 server = strdup(cp);
610                         } 
611                         else if (!strncasecmp(buffer, "host", 4)) {
612                                 if (server != NULL)
613                                         continue;
614                                 cp = buffer + 4;
615                                 while (isspace((unsigned char)*cp))
616                                         cp++;
617                                 if ((*cp == '\0') || (*cp == '\n'))
618                                         continue;
619                                 server = strdup(cp);
620                         }
621                         else if (!strncasecmp(buffer, "base", 4)) {
622                                 cp = buffer + 4;
623                                 while (isspace((unsigned char)*cp))
624                                         cp++;
625                                 if ((*cp == '\0') || (*cp == '\n'))
626                                         continue;
627                                 search_base = strdup(cp);
628                         }
629                         else if (!strncasecmp(buffer, "groupbase", 9)) {
630                                 cp = buffer + 9;
631                                 while (isspace((unsigned char)*cp))
632                                         cp++;
633                                 if ((*cp == '\0') || (*cp == '\n'))
634                                         continue;
635                                 group_base = strdup(cp);
636                         }
637                         else
638                                 fprintf(stderr, "?? -> %s\n", buffer);
639                 }
640         }
641         if (group_base == NULL)
642                 group_base = strdup(UD_WHERE_GROUPS_ARE_CREATED);
643
644         /*
645          *  Set up our LDAP connection.  The values of retry and timeout
646          *  are meaningless since we will immediately be doing a null bind
647          *  because we want to be sure to use TCP, not UDP.
648          */
649         if ((ld = ldap_open(server, ldap_port)) == NULL) {
650                 fprintf(stderr, "  The LDAP Directory is temporarily unavailable.  Please try again later.\n");
651                 exit(0);
652                 /* NOTREACHED */
653         }
654         if (ldap_bind_s(ld, (char *) default_bind_object, NULL,
655             LDAP_AUTH_SIMPLE) != LDAP_SUCCESS) {
656                 int ld_errno = 0;
657                 ldap_get_option(ld, LDAP_OPT_ERROR_NUMBER, &ld_errno);
658
659                 fprintf(stderr, "  The LDAP Directory is temporarily unavailable.  Please try again later.\n");
660                 if (ld_errno != LDAP_UNAVAILABLE)
661                         ldap_perror(ld, "  ldap_bind_s");
662                 exit(0);
663                 /* NOTREACHED */
664         }
665         {
666                 int deref = LDAP_DEREF_ALWAYS;
667                 ldap_set_option(ld, LDAP_OPT_DEREF, (void *) &deref);
668         }
669         bind_status = UD_NOT_BOUND;
670         if ( default_bind_object != NULL ) {
671                 bound_dn = strdup(default_bind_object);
672         } else {
673                 bound_dn = NULL;
674         }
675
676         /* enabled local caching of ldap results, 15 minute lifetime */
677         ldap_enable_cache( ld, 60 * 15, 0 );            /* no memory limit */
678
679         /* initialize the search filters */
680         if ((lfdp = ldap_init_getfilter(filter_file)) == NULL) {
681                 fprintf(stderr, "  Problem with ldap_init_getfilter\n");
682                 fatal(filter_file);
683                 /*NOTREACHED*/
684         }
685
686         /* terminal initialization stuff goes here */
687         lpp = DEFAULT_TTY_HEIGHT;
688         col_size = DEFAULT_TTY_WIDTH;
689
690         (void) SIGNAL (SIGINT, attn);
691
692 #ifndef NO_TERMCAP
693         {
694         char *term;
695
696         if (((term = getenv("TERM")) == NULL) || (tgetent(bp, term) <= 0))
697                 return;
698         else {
699 #ifdef TIOCGWINSZ
700                 struct winsize win;             /* for tty set-up */
701                 if (ioctl(fileno(stdout), TIOCGWINSZ, &win) >= 0) {
702                         if ((lpp = win.ws_row) == 0)
703                                 lpp = tgetnum("li");
704                         if ((col_size = win.ws_col) == 0)
705                                 col_size = tgetnum("co");
706                         if ((lpp <= 0) || tgetflag("hc"))
707                                 lpp = DEFAULT_TTY_HEIGHT;
708                         if ((col_size <= 0) || tgetflag("hc"))
709                                 col_size = DEFAULT_TTY_WIDTH;
710                         (void) SIGNAL (SIGWINCH, chwinsz);
711                 } else
712 #endif
713                 {
714                         lpp = tgetnum("li");
715                         col_size = tgetnum("co");
716                 }
717         }
718         }
719 #endif
720 }
721
722 RETSIGTYPE
723 attn( int sig )
724 {
725         fflush(stderr);
726         fflush(stdout);
727         printf("\n\n  INTERRUPTED!\n");
728
729         (void) SIGNAL (SIGINT, attn);
730
731         longjmp(env, 1);
732 }
733
734 #if !defined(NO_TERMCAP) && defined(TIOCGWINSZ)
735 RETSIGTYPE
736 chwinsz( int sig )
737 {
738         struct winsize win;
739
740         (void) SIGNAL (SIGWINCH, SIG_IGN);
741         if (ioctl(fileno(stdout), TIOCGWINSZ, &win) != -1) {
742                 if (win.ws_row != 0)
743                         lpp = win.ws_row;
744                 if (win.ws_col != 0)
745                         col_size = win.ws_col;
746         }
747
748         (void) SIGNAL (SIGWINCH, chwinsz);
749 }
750 #endif