]> git.sur5r.net Git - openldap/blobdiff - servers/slapd/slappasswd.c
Additional fix for ITS#4522. The "dn=" ist not optional.
[openldap] / servers / slapd / slappasswd.c
index 0d18eadf785e6308c3aa0d94b4a2db700f380e62..ea77df719d0a4ae3fadb6ab742c779382a4ec6c8 100644 (file)
@@ -46,8 +46,9 @@ usage(const char *s)
        fprintf(stderr,
                "Usage: %s [options]\n"
                "  -c format\tcrypt(3) salt format\n"
-               "  -g\n"
+               "  -g\t\tgenerate random password\n"
                "  -h hash\tpassword scheme\n"
+               "  -n\t\tomit trailing newline\n"
                "  -s secret\tnew password\n"
                "  -u\t\tgenerate RFC2307 values (default)\n"
                "  -v\t\tincrease verbosity\n"
@@ -60,7 +61,6 @@ usage(const char *s)
 int
 slappasswd( int argc, char *argv[] )
 {
-       char    *cleartext_scheme = "{CLEARTEXT}";
 #ifdef LUTIL_SHA1_BYTES
        char    *default_scheme = "{SSHA}";
 #else
@@ -74,11 +74,12 @@ slappasswd( int argc, char *argv[] )
        const char *progname = "slappasswd";
 
        int             i;
-       struct berval passwd;
+       char            *newline = "\n";
+       struct berval passwd = BER_BVNULL;
        struct berval hash;
 
        while( (i = getopt( argc, argv,
-               "c:d:gh:s:T:vu" )) != EOF )
+               "c:d:gh:ns:T:vu" )) != EOF )
        {
                switch (i) {
                case 'c':       /* crypt salt format */
@@ -88,36 +89,21 @@ slappasswd( int argc, char *argv[] )
 
                case 'g':       /* new password (generate) */
                        if ( pwfile != NULL ) {
-                               fprintf( stderr, "Option -s incompatible with -T\n" );
+                               fprintf( stderr, "Option -g incompatible with -T\n" );
                                return EXIT_FAILURE;
 
                        } else if ( newpw != NULL ) {
                                fprintf( stderr, "New password already provided\n" );
                                return EXIT_FAILURE;
 
-                       } else if ( scheme != default_scheme && strcmp( scheme, cleartext_scheme ) != 0 ) {
-                               fprintf( stderr, "Option -g incompatible with scheme \"%s\"\n", scheme );
+                       } else if ( lutil_passwd_generate( &passwd, 8 )) {
+                               fprintf( stderr, "Password generation failed\n" );
                                return EXIT_FAILURE;
-
-                       } else {
-                               struct berval   p = BER_BVNULL;
-
-                               lutil_passwd_generate( &p, 8 );
-
-                               newpw = p.bv_val;
-
-                               scheme = cleartext_scheme;
                        }
                        break;
 
                case 'h':       /* scheme */
-                       if ( scheme == cleartext_scheme ) {
-                               if ( strcmp( optarg, cleartext_scheme ) != 0 ) {
-                                       fprintf( stderr, "Option -h incompatible with -g\n" );
-                                       return EXIT_FAILURE;
-                               }
-                               
-                       } else if ( scheme != default_scheme ) {
+                       if ( scheme != default_scheme ) {
                                fprintf( stderr, "Scheme already provided\n" );
                                return EXIT_FAILURE;
 
@@ -126,6 +112,10 @@ slappasswd( int argc, char *argv[] )
                        }
                        break;
 
+               case 'n':
+                       newline = "";
+                       break;
+
                case 's':       /* new password (secret) */
                        if ( pwfile != NULL ) {
                                fprintf( stderr, "Option -s incompatible with -T\n" );
@@ -178,7 +168,7 @@ slappasswd( int argc, char *argv[] )
                if( lutil_get_filed_password( pwfile, &passwd )) {
                        return EXIT_FAILURE;
                }
-       } else {
+       } else if ( BER_BVISEMPTY( &passwd )) {
                if( newpw == NULL ) {
                        /* prompt for new password */
                        char *cknewpw;
@@ -193,6 +183,9 @@ slappasswd( int argc, char *argv[] )
 
                passwd.bv_val = newpw;
                passwd.bv_len = strlen(passwd.bv_val);
+       } else {
+               hash = passwd;
+               goto print_pw;
        }
 
        lutil_passwd_hash( &passwd, scheme, &hash, &text );
@@ -209,6 +202,7 @@ slappasswd( int argc, char *argv[] )
                return EXIT_FAILURE;
        }
 
-       printf( "%s\n" , hash.bv_val );
+print_pw:;
+       printf( "%s%s" , hash.bv_val, newline );
        return EXIT_SUCCESS;
 }