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