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