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