]> git.sur5r.net Git - openldap/blobdiff - clients/tools/ldappasswd.c
Update copyright statements
[openldap] / clients / tools / ldappasswd.c
index 03fe88bef4cabc0fe4efcbc5dcf5fd72be2dcc3d..a5cfa4aa387892a01e125eb24a79162919c94def 100644 (file)
@@ -1,6 +1,6 @@
 /* $OpenLDAP$ */
 /*
- * Copyright 1998-2000 The OpenLDAP Foundation, All Rights Reserved.
+ * Copyright 1998-2002 The OpenLDAP Foundation, All Rights Reserved.
  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
  */
 
@@ -29,8 +29,8 @@ usage(const char *s)
 {
        fprintf(stderr,
 "Change password of an LDAP user\n\n"
-"usage: %s [options] user\n"
-"  user: the identity of the user, normally a DN\n"
+"usage: %s [options] [user]\n"
+"  user: the autentication identity, commonly a DN\n"
 "Password change options:\n"
 "  -a secret  old password\n"
 "  -A         prompt for old password\n"
@@ -49,12 +49,12 @@ usage(const char *s)
 "  -p port    port on LDAP server\n"
 "  -Q         use SASL Quiet mode\n"
 "  -R realm   SASL realm\n"
-"  -U user    SASL authentication identity (username)\n"
+"  -U authcid SASL authentication identity\n"
 "  -v         run in verbose mode (diagnostics to standard output)\n"
 "  -w passwd  bind passwd (for simple authentication)\n"
 "  -W         prompt for bind passwd\n"
 "  -x         Simple authentication\n"
-"  -X id      SASL authorization identity (\"dn:<dn>\" or \"u:<user>\")\n"
+"  -X authzid SASL authorization identity (\"dn:<dn>\" or \"u:<user>\")\n"
 "  -Y mech    SASL mechanism\n"
 "  -Z         Start TLS request (-ZZ to require successful response)\n"
                , s );
@@ -70,7 +70,7 @@ main( int argc, char *argv[] )
        char    *ldaphost = NULL;
        char    *ldapuri = NULL;
 
-       char    *dn = NULL;
+       char    *user = NULL;
        char    *binddn = NULL;
 
        struct berval passwd = { 0, NULL };
@@ -109,11 +109,8 @@ main( int argc, char *argv[] )
 
     prog = (prog = strrchr(argv[0], *LDAP_DIRSEP)) == NULL ? argv[0] : prog + 1;
 
-       if (argc == 1)
-               usage (argv[0]);
-
-       while( (i = getopt( argc, argv,
-               "Aa:Ss:" "Cd:D:h:H:InO:p:QRU:vw:WxX:Y:Z" )) != EOF )
+       while( (i = getopt( argc, argv, "Aa:Ss:"
+               "Cd:D:h:H:InO:p:QR:U:vw:WxX:Y:Z" )) != EOF )
        {
                switch (i) {
                /* Password Options */
@@ -127,7 +124,7 @@ main( int argc, char *argv[] )
                        {
                                char* p;
 
-                               for( p = optarg; *p == '\0'; p++ ) {
+                               for( p = optarg; *p != '\0'; p++ ) {
                                        *p = '\0';
                                }
                        }
@@ -142,7 +139,7 @@ main( int argc, char *argv[] )
                        {
                                char* p;
 
-                               for( p = optarg; *p == '\0'; p++ ) {
+                               for( p = optarg; *p != '\0'; p++ ) {
                                        *p = '\0';
                                }
                        }
@@ -157,33 +154,33 @@ main( int argc, char *argv[] )
            break;
        case 'D':       /* bind DN */
                if( binddn != NULL ) {
-                       fprintf( stderr, "%s: -D previously specified\n" );
+                       fprintf( stderr, "%s: -D previously specified\n", prog );
                        return EXIT_FAILURE;
                }
            binddn = strdup( optarg );
            break;
        case 'h':       /* ldap host */
                if( ldapuri != NULL ) {
-                       fprintf( stderr, "%s: -h incompatible with -H\n" );
+                       fprintf( stderr, "%s: -h incompatible with -H\n", prog );
                        return EXIT_FAILURE;
                }
                if( ldaphost != NULL ) {
-                       fprintf( stderr, "%s: -h previously specified\n" );
+                       fprintf( stderr, "%s: -h previously specified\n", prog );
                        return EXIT_FAILURE;
                }
            ldaphost = strdup( optarg );
            break;
        case 'H':       /* ldap URI */
                if( ldaphost != NULL ) {
-                       fprintf( stderr, "%s: -H incompatible with -h\n" );
+                       fprintf( stderr, "%s: -H incompatible with -h\n", prog );
                        return EXIT_FAILURE;
                }
                if( ldapport ) {
-                       fprintf( stderr, "%s: -H incompatible with -p\n" );
+                       fprintf( stderr, "%s: -H incompatible with -p\n", prog );
                        return EXIT_FAILURE;
                }
                if( ldapuri != NULL ) {
-                       fprintf( stderr, "%s: -H previously specified\n" );
+                       fprintf( stderr, "%s: -H previously specified\n", prog );
                        return EXIT_FAILURE;
                }
            ldapuri = strdup( optarg );
@@ -265,7 +262,7 @@ main( int argc, char *argv[] )
        case 'O':
 #ifdef HAVE_CYRUS_SASL
                if( sasl_secprops != NULL ) {
-                       fprintf( stderr, "%s: -O previously specified\n" );
+                       fprintf( stderr, "%s: -O previously specified\n", prog );
                        return EXIT_FAILURE;
                }
                if( version == LDAP_VERSION2 ) {
@@ -289,7 +286,7 @@ main( int argc, char *argv[] )
                break;
        case 'p':
                if( ldapport ) {
-                       fprintf( stderr, "%s: -p previously specified\n" );
+                       fprintf( stderr, "%s: -p previously specified\n", prog );
                        return EXIT_FAILURE;
                }
            ldapport = atoi( optarg );
@@ -343,7 +340,7 @@ main( int argc, char *argv[] )
        case 'R':
 #ifdef HAVE_CYRUS_SASL
                if( sasl_realm != NULL ) {
-                       fprintf( stderr, "%s: -R previously specified\n" );
+                       fprintf( stderr, "%s: -R previously specified\n", prog );
                        return EXIT_FAILURE;
                }
                if( version == LDAP_VERSION2 ) {
@@ -369,7 +366,7 @@ main( int argc, char *argv[] )
        case 'U':
 #ifdef HAVE_CYRUS_SASL
                if( sasl_authc_id != NULL ) {
-                       fprintf( stderr, "%s: -U previously specified\n" );
+                       fprintf( stderr, "%s: -U previously specified\n", prog );
                        return EXIT_FAILURE;
                }
                if( version == LDAP_VERSION2 ) {
@@ -400,7 +397,7 @@ main( int argc, char *argv[] )
                {
                        char* p;
 
-                       for( p = optarg; *p == '\0'; p++ ) {
+                       for( p = optarg; *p != '\0'; p++ ) {
                                *p = '\0';
                        }
                }
@@ -412,7 +409,7 @@ main( int argc, char *argv[] )
        case 'Y':
 #ifdef HAVE_CYRUS_SASL
                if( sasl_mech != NULL ) {
-                       fprintf( stderr, "%s: -Y previously specified\n" );
+                       fprintf( stderr, "%s: -Y previously specified\n", prog );
                        return EXIT_FAILURE;
                }
                if( version == LDAP_VERSION2 ) {
@@ -444,7 +441,7 @@ main( int argc, char *argv[] )
        case 'X':
 #ifdef HAVE_CYRUS_SASL
                if( sasl_authz_id != NULL ) {
-                       fprintf( stderr, "%s: -X previously specified\n" );
+                       fprintf( stderr, "%s: -X previously specified\n", prog );
                        return EXIT_FAILURE;
                }
                if( version == LDAP_VERSION2 ) {
@@ -484,16 +481,12 @@ main( int argc, char *argv[] )
 
 
                default:
-                       fprintf( stderr, "%s: unrecongized option -%c\n",
+                       fprintf( stderr, "%s: unrecognized option -%c\n",
                                prog, optopt );
-                       usage (argv[0]);
+                       usage (prog);
                }
        }
 
-       if( argc - optind != 1 ) {
-               usage( argv[0] );
-       } 
-
        if (authmethod == -1) {
 #ifdef HAVE_CYRUS_SASL
                authmethod = LDAP_AUTH_SASL;
@@ -502,7 +495,13 @@ main( int argc, char *argv[] )
 #endif
        }
 
-       dn = strdup( argv[optind] );
+       if( argc - optind > 1 ) {
+               usage( prog );
+       } else if ( argc - optind == 1 ) {
+               user = strdup( argv[optind] );
+       } else {
+               user = NULL;
+       }
 
        if( want_oldpw && oldpw == NULL ) {
                /* prompt for old password */
@@ -511,7 +510,7 @@ main( int argc, char *argv[] )
                ckoldpw = getpassphrase("Re-enter old password: ");
 
                if( oldpw== NULL || ckoldpw == NULL ||
-                       strncmp( oldpw, ckoldpw, strlen(oldpw) ))
+                       strcmp( oldpw, ckoldpw ))
                {
                        fprintf( stderr, "passwords do not match\n" );
                        return EXIT_FAILURE;
@@ -525,26 +524,15 @@ main( int argc, char *argv[] )
                cknewpw = getpassphrase("Re-enter new password: ");
 
                if( newpw== NULL || cknewpw == NULL ||
-                       strncmp( newpw, cknewpw, strlen(newpw) ))
+                       strcmp( newpw, cknewpw ))
                {
                        fprintf( stderr, "passwords do not match\n" );
                        return EXIT_FAILURE;
                }
        }
 
-       if( binddn == NULL && dn != NULL ) {
-               binddn = dn;
-               dn = NULL;
-
-               if( passwd.bv_val == NULL ) {
-                       passwd.bv_val = oldpw;
-                       passwd.bv_len = oldpw == NULL ? 0 : strlen( oldpw );
-               }
-       }
-
        if (want_bindpw && passwd.bv_val == NULL ) {
                /* handle bind password */
-               fprintf( stderr, "Bind DN: %s\n", binddn );
                passwd.bv_val = strdup( getpassphrase("Enter bind password: "));
                passwd.bv_len = passwd.bv_val ? strlen( passwd.bv_val ) : 0;
        }
@@ -569,20 +557,25 @@ main( int argc, char *argv[] )
                                ldaphost != NULL ? ldaphost : "<DEFAULT>",
                                ldapport );
                }
+
                ld = ldap_init( ldaphost, ldapport );
+               if( ld == NULL ) {
+                       perror("ldapsearch: ldap_init");
+                       return EXIT_FAILURE;
+               }
 
        } else {
                if ( verbose ) {
                        fprintf( stderr, "ldap_initialize( %s )\n",
                                ldapuri != NULL ? ldapuri : "<DEFAULT>" );
                }
-               (void) ldap_initialize( &ld, ldapuri );
-       }
 
-       if( ld == NULL ) {
-               fprintf( stderr, "Could not create LDAP session handle (%d): %s\n",
-                       rc, ldap_err2string(rc) );
-               return EXIT_FAILURE;
+               rc = ldap_initialize( &ld, ldapuri );
+               if( rc != LDAP_SUCCESS ) {
+                       fprintf( stderr, "Could not create LDAP session handle (%d): %s\n",
+                               rc, ldap_err2string(rc) );
+                       return EXIT_FAILURE;
+               }
        }
 
        /* referrals */
@@ -595,7 +588,7 @@ main( int argc, char *argv[] )
        }
 
        /* LDAPv3 only */
-       version = 3;
+       version = LDAP_VERSION3;
        rc = ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, &version );
 
        if(rc != LDAP_OPT_SUCCESS ) {
@@ -603,12 +596,11 @@ main( int argc, char *argv[] )
                return EXIT_FAILURE;
        }
 
-       if ( use_tls && ldap_start_tls_s( ld, NULL, NULL ) != LDAP_SUCCESS ) {
+       if ( use_tls && ( ldap_start_tls_s( ld, NULL, NULL ) != LDAP_SUCCESS )) {
+               ldap_perror( ld, "ldap_start_tls" );
                if ( use_tls > 1 ) {
-                       ldap_perror( ld, "ldap_start_tls" );
                        return( EXIT_FAILURE );
                }
-               fprintf( stderr, "WARNING: could not start TLS\n" );
        }
 
        if ( authmethod == LDAP_AUTH_SASL ) {
@@ -644,7 +636,7 @@ main( int argc, char *argv[] )
                }
 #else
                fprintf( stderr, "%s: not compiled with SASL support\n",
-                       argv[0] );
+                       prog );
                return( EXIT_FAILURE );
 #endif
        }
@@ -656,7 +648,7 @@ main( int argc, char *argv[] )
                }
        }
 
-       if( dn != NULL || oldpw != NULL || newpw != NULL ) {
+       if( user != NULL || oldpw != NULL || newpw != NULL ) {
                /* build change password control */
                BerElement *ber = ber_alloc_t( LBER_USE_DER );
 
@@ -668,15 +660,15 @@ main( int argc, char *argv[] )
 
                ber_printf( ber, "{" /*}*/ );
 
-               if( dn != NULL ) {
+               if( user != NULL ) {
                        ber_printf( ber, "ts",
-                               LDAP_TAG_EXOP_X_MODIFY_PASSWD_ID, dn );
-                       free(dn);
+                               LDAP_TAG_EXOP_X_MODIFY_PASSWD_ID, user );
+                       free(user);
                }
 
                if( oldpw != NULL ) {
                        ber_printf( ber, "ts",
-                               LDAP_TAG_EXOP_X_MODIFY_PASSWD_NEW, oldpw );
+                               LDAP_TAG_EXOP_X_MODIFY_PASSWD_OLD, oldpw );
                        free(oldpw);
                }