]> git.sur5r.net Git - openldap/blobdiff - clients/ud/main.c
Don't depend error handling on exact resultCode.
[openldap] / clients / ud / main.c
index ebbe0b46d1f502b3d39f1b7a1b7f901998ee0038..794b77d38cdda474e138a7b0ed5bffc7de3ac2ef 100644 (file)
@@ -1,3 +1,8 @@
+/* $OpenLDAP$ */
+/*
+ * Copyright 1998-1999 The OpenLDAP Foundation, All Rights Reserved.
+ * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
+ */
 /*
  * Copyright (c) 1991, 1992, 1993 
  * Regents of the University of Michigan.  All rights reserved.
@@ -19,7 +24,9 @@
 #include "portable.h"
 
 #include <stdio.h>
-#include <stdlib.h>
+
+#include <ac/stdlib.h>
+
 #include <setjmp.h>
 
 #ifdef HAVE_PWD_H
 #include <lber.h>
 #include <ldap.h>
 
-#include <ldapconfig.h>
+#include "ldap_defaults.h"
 #include "ud.h"
 
-#ifndef lint
-char copyright[] =
-"@(#) Copyright (c) 1991, 1992, 1993 Regents of the University of Michigan.\nAll rights reserved.\n";
-#endif
-
 /*
  *  Used with change_base() to indicate which base we are changing.
  */
 #define BASE_SEARCH     0
 #define BASE_GROUPS     1
 
-#define        iscom(x)        (!strncasecmp(x, cmd, strlen(cmd)))
+#define        iscom(x)        (!strncasecmp((x), cmd, strlen(cmd)))
 
 static char *server = NULL;
 static char *config_file = UD_CONFIG_FILE;
@@ -83,6 +85,9 @@ int debug;                    /* debug flag */
 #endif
 int ldebug;                    /* library debug flag */
 
+#ifndef HAVE_MKVERSION
+char Version[] = OPENLDAP_PACKAGE " " OPENLDAP_VERSION " UserDirectory (ud)";
+#endif
 
 int
 main( int argc, char **argv )
@@ -135,10 +140,10 @@ main( int argc, char **argv )
                        printf("   64  authentication information\n");
                        printf("  128  initialization information\n\n");
                        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);
-                       exit(0);
+                       exit( EXIT_SUCCESS );
                default:
                        fprintf(stderr, "Usage: %s [-c filter-config-file] [-d debug-level] [-l ldap-debug-level] [-s server] [-p port] [-V]\n", argv[0]);
-                       exit(-1);
+                       exit( EXIT_FAILURE);
                        /* NOTREACHED */
                }
        }
@@ -170,7 +175,6 @@ do_commands( void )
        LDAPMessage *mp;                        /* returned by find() */
        register char *cp;                      /* misc char pointer */
        register char *ap;                      /* misc char pointer */
-       static char buf[MED_BUF_SIZE];          /* for prompting */
        static char cmd[MED_BUF_SIZE];          /* holds the command */
        static char input[MED_BUF_SIZE];        /* buffer for input */
 
@@ -194,19 +198,19 @@ do_commands( void )
                        putchar('\n');
                        continue;
                }
-               while (isspace(*cp))
+               while (isspace((unsigned char)*cp))
                        cp++;   
                ap = cmd;
                if (memset(cmd, '\0', sizeof(cmd)) == NULL)
                        fatal("memset");
-               while (!isspace(*cp) && (*cp != '\0'))
+               while (!isspace((unsigned char)*cp) && (*cp != '\0'))
                        *ap++ = *cp++;
                if (iscom("status"))
                        status();
                else if (iscom("stop") || iscom("quit"))
                        break;
                else if (iscom("cb") || iscom("cd") || iscom("moveto")) {
-                       while (isspace(*cp) && (*cp != '\0')) 
+                       while (isspace((unsigned char)*cp) && (*cp != '\0'))
                                cp++;
                        if (!strncasecmp(cp, "base", 4))
                                cp += 4;
@@ -276,10 +280,10 @@ do_commands( void )
        printf(" Thank you!\n");
        
        ldap_unbind(ld);
-#ifdef HAVE_KERBEROS
+#ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
        destroy_tickets();
 #endif
-       exit(0);
+       exit( EXIT_SUCCESS );
        /* NOTREACHED */
 }
 
@@ -292,13 +296,15 @@ status( void )
        if (debug & D_TRACE)
                printf("->status()\n");
 #endif
-       printf("  Current server is %s", server);
+       printf("  Current server is %s", server != NULL ? server : "<default>" );
        if ( ld != NULL ) {
                char *host = NULL;
                
                ldap_get_option(ld, LDAP_OPT_HOST_NAME, &host);
 
-               if (( host != NULL ) && (strcasecmp( host, server ) != 0 )) {
+               if ( host != NULL &&
+                       ( server == NULL || strcasecmp( host, server ) != 0 ) )
+               {
                        printf( " (%s)", host );
                }
        }
@@ -347,7 +353,7 @@ change_base( int type, char **base, char *s )
        /*
         *  If s is NULL we need to prompt the user for an argument.
         */
-       while (s == NULL) {
+       if (s == NULL) {
                if (verbose) {
                        printf("  You need to specify how the base is to be changed.  Valid choices are:\n");
                        printf("     ?       - list the choices immediately below this level\n");
@@ -361,6 +367,8 @@ change_base( int type, char **base, char *s )
                fetch_buffer(buf, sizeof(buf), stdin);
                if ((buf != NULL) && (buf[0] != '\0'))
                        s = buf;
+               else
+                       return;
        }
 
        /* set the output string */
@@ -402,7 +410,7 @@ change_base( int type, char **base, char *s )
                         *
                         *  sequence now that 'cp' is pointing to the '='.
                         */
-                       while(!isspace(*cp))
+                       while(!isspace((unsigned char)*cp))
                                cp--;
                        cp++;
                        /*
@@ -427,7 +435,7 @@ change_base( int type, char **base, char *s )
                 *  the user if they want to see more.  They can also just
                 *  type a number at that point too.
                 */
-               if (ldap_search_s(ld, *base, LDAP_SCOPE_ONELEVEL, "(|(objectClass=quipuNonLeafObject)(objectClass=externalNonLeafObject))", attrs, FALSE, &mp) != LDAP_SUCCESS) {
+               if (ldap_search_s(ld, *base, LDAP_SCOPE_ONELEVEL, NULL, attrs, FALSE, &mp) != LDAP_SUCCESS) {
                        int ld_errno = 0;
                        ldap_get_option(ld, LDAP_OPT_ERROR_NUMBER, &ld_errno);
                        if ((ld_errno == LDAP_TIMELIMIT_EXCEEDED) ||
@@ -486,7 +494,7 @@ change_base( int type, char **base, char *s )
                        if (j == 0) {
                                (void) ldap_msgfree(mp);
                                for (i = 0; i < matches; i++)
-                                       Free(choices[i]);
+                                       ldap_memfree(choices[i]);
                                return;
                        }
                        if ((j < 1) || (j >= i))
@@ -496,7 +504,7 @@ change_base( int type, char **base, char *s )
                                printbase(output_string, *base);
                                (void) ldap_msgfree(mp);
                                for (i = 0; choices[i] != NULL; i++)
-                                       Free(choices[i]);
+                                       ldap_memfree(choices[i]);
                                return;
                        }
                }
@@ -553,8 +561,8 @@ initialize_client( void )
 #endif
 
        if (ldebug) {
-               lber_set_option(NULL, LBER_OPT_DEBUG_LEVEL, &ldebug);
-               ldap_set_option(NULL, LDAP_OPT_DEBUG_LEVEL, &ldebug);
+               ber_set_option( NULL, LBER_OPT_DEBUG_LEVEL, &ldebug );
+               ldap_set_option( NULL, LDAP_OPT_DEBUG_LEVEL, &ldebug );
        }
 
        /*
@@ -601,7 +609,7 @@ initialize_client( void )
                                if (server != NULL)
                                        continue;
                                cp = buffer + 6;
-                               while (isspace(*cp))
+                               while (isspace((unsigned char)*cp))
                                        cp++;
                                if ((*cp == '\0') || (*cp == '\n'))
                                        continue;
@@ -611,7 +619,7 @@ initialize_client( void )
                                if (server != NULL)
                                        continue;
                                cp = buffer + 4;
-                               while (isspace(*cp))
+                               while (isspace((unsigned char)*cp))
                                        cp++;
                                if ((*cp == '\0') || (*cp == '\n'))
                                        continue;
@@ -619,7 +627,7 @@ initialize_client( void )
                        }
                        else if (!strncasecmp(buffer, "base", 4)) {
                                cp = buffer + 4;
-                               while (isspace(*cp))
+                               while (isspace((unsigned char)*cp))
                                        cp++;
                                if ((*cp == '\0') || (*cp == '\n'))
                                        continue;
@@ -627,7 +635,7 @@ initialize_client( void )
                        }
                        else if (!strncasecmp(buffer, "groupbase", 9)) {
                                cp = buffer + 9;
-                               while (isspace(*cp))
+                               while (isspace((unsigned char)*cp))
                                        cp++;
                                if ((*cp == '\0') || (*cp == '\n'))
                                        continue;
@@ -645,20 +653,15 @@ initialize_client( void )
         *  are meaningless since we will immediately be doing a null bind
         *  because we want to be sure to use TCP, not UDP.
         */
-       if ((ld = ldap_open(server, ldap_port)) == NULL) {
-               fprintf(stderr, "  The LDAP Directory is temporarily unavailable.  Please try again later.\n");
-               exit(0);
+       if ((ld = ldap_init(server, ldap_port)) == NULL) {
+               fprintf(stderr, "  Initialization of LDAP session failed.\n");
+               exit( EXIT_FAILURE );
                /* NOTREACHED */
        }
        if (ldap_bind_s(ld, (char *) default_bind_object, NULL,
            LDAP_AUTH_SIMPLE) != LDAP_SUCCESS) {
-               int ld_errno = 0;
-               ldap_get_option(ld, LDAP_OPT_ERROR_NUMBER, &ld_errno);
-
-               fprintf(stderr, "  The LDAP Directory is temporarily unavailable.  Please try again later.\n");
-               if (ld_errno != LDAP_UNAVAILABLE)
-                       ldap_perror(ld, "  ldap_bind_s");
-               exit(0);
+               ldap_perror(ld, "  ldap_bind_s");
+               exit( EXIT_FAILURE );
                /* NOTREACHED */
        }
        {