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