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