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