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