Back to index

openldap  2.4.31
slappasswd.c
Go to the documentation of this file.
00001 /* $OpenLDAP$ */
00002 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
00003  *
00004  * Copyright 1998-2012 The OpenLDAP Foundation.
00005  * Portions Copyright 1998-2003 Kurt D. Zeilenga.
00006  * All rights reserved.
00007  *
00008  * Redistribution and use in source and binary forms, with or without
00009  * modification, are permitted only as authorized by the OpenLDAP
00010  * Public License.
00011  *
00012  * A copy of this license is available in file LICENSE in the
00013  * top-level directory of the distribution or, alternatively, at
00014  * <http://www.OpenLDAP.org/license.html>.
00015  */
00016 /* ACKNOWLEDGEMENTS:
00017  * This work was initially developed by Kurt Zeilenga for inclusion
00018  * in OpenLDAP Software.
00019  */
00020 
00021 #include "portable.h"
00022 
00023 #include <stdio.h>
00024 
00025 #include <ac/stdlib.h>
00026 
00027 #include <ac/ctype.h>
00028 #include <ac/signal.h>
00029 #include <ac/socket.h>
00030 #include <ac/string.h>
00031 #include <ac/time.h>
00032 #include <ac/unistd.h>
00033 
00034 #include <ldap.h>
00035 #include <lber_pvt.h>
00036 #include <lutil.h>
00037 #include <lutil_sha1.h>
00038 
00039 #include "ldap_defaults.h"
00040 #include "slap.h"
00041 
00042 static int    verbose = 0;
00043 
00044 static void
00045 usage(const char *s)
00046 {
00047        fprintf(stderr,
00048               "Usage: %s [options]\n"
00049               "  -c format\tcrypt(3) salt format\n"
00050               "  -g\t\tgenerate random password\n"
00051               "  -h hash\tpassword scheme\n"
00052               "  -n\t\tomit trailing newline\n"
00053               "  -s secret\tnew password\n"
00054               "  -u\t\tgenerate RFC2307 values (default)\n"
00055               "  -v\t\tincrease verbosity\n"
00056               "  -T file\tread file for new password\n"
00057               , s );
00058 
00059        exit( EXIT_FAILURE );
00060 }
00061 
00062 int
00063 slappasswd( int argc, char *argv[] )
00064 {
00065 #ifdef LUTIL_SHA1_BYTES
00066        char   *default_scheme = "{SSHA}";
00067 #else
00068        char   *default_scheme = "{SMD5}";
00069 #endif
00070        char   *scheme = default_scheme;
00071 
00072        char   *newpw = NULL;
00073        char   *pwfile = NULL;
00074        const char *text;
00075        const char *progname = "slappasswd";
00076 
00077        int           i;
00078        char          *newline = "\n";
00079        struct berval passwd = BER_BVNULL;
00080        struct berval hash;
00081 
00082        while( (i = getopt( argc, argv,
00083               "c:d:gh:ns:T:vu" )) != EOF )
00084        {
00085               switch (i) {
00086               case 'c':     /* crypt salt format */
00087                      scheme = "{CRYPT}";
00088                      lutil_salt_format( optarg );
00089                      break;
00090 
00091               case 'g':     /* new password (generate) */
00092                      if ( pwfile != NULL ) {
00093                             fprintf( stderr, "Option -g incompatible with -T\n" );
00094                             return EXIT_FAILURE;
00095 
00096                      } else if ( newpw != NULL ) {
00097                             fprintf( stderr, "New password already provided\n" );
00098                             return EXIT_FAILURE;
00099 
00100                      } else if ( lutil_passwd_generate( &passwd, 8 )) {
00101                             fprintf( stderr, "Password generation failed\n" );
00102                             return EXIT_FAILURE;
00103                      }
00104                      break;
00105 
00106               case 'h':     /* scheme */
00107                      if ( scheme != default_scheme ) {
00108                             fprintf( stderr, "Scheme already provided\n" );
00109                             return EXIT_FAILURE;
00110 
00111                      } else {
00112                             scheme = ch_strdup( optarg );
00113                      }
00114                      break;
00115 
00116               case 'n':
00117                      newline = "";
00118                      break;
00119 
00120               case 's':     /* new password (secret) */
00121                      if ( pwfile != NULL ) {
00122                             fprintf( stderr, "Option -s incompatible with -T\n" );
00123                             return EXIT_FAILURE;
00124 
00125                      } else if ( newpw != NULL ) {
00126                             fprintf( stderr, "New password already provided\n" );
00127                             return EXIT_FAILURE;
00128 
00129                      } else {
00130                             char* p;
00131                             newpw = ch_strdup( optarg );
00132 
00133                             for( p = optarg; *p != '\0'; p++ ) {
00134                                    *p = '\0';
00135                             }
00136                      }
00137                      break;
00138 
00139               case 'T':     /* password file */
00140                      if ( pwfile != NULL ) {
00141                             fprintf( stderr, "Password file already provided\n" );
00142                             return EXIT_FAILURE;
00143 
00144                      } else if ( newpw != NULL ) {
00145                             fprintf( stderr, "Option -T incompatible with -s/-g\n" );
00146                             return EXIT_FAILURE;
00147 
00148                      }
00149                      pwfile = optarg;
00150                      break;
00151 
00152               case 'u':     /* RFC2307 userPassword */
00153                      break;
00154 
00155               case 'v':     /* verbose */
00156                      verbose++;
00157                      break;
00158 
00159               default:
00160                      usage ( progname );
00161               }
00162        }
00163 
00164        if( argc - optind != 0 ) {
00165               usage( progname );
00166        } 
00167 
00168        if( pwfile != NULL ) {
00169               if( lutil_get_filed_password( pwfile, &passwd )) {
00170                      return EXIT_FAILURE;
00171               }
00172        } else if ( BER_BVISEMPTY( &passwd )) {
00173               if( newpw == NULL ) {
00174                      /* prompt for new password */
00175                      char *cknewpw;
00176                      newpw = ch_strdup(getpassphrase("New password: "));
00177                      cknewpw = getpassphrase("Re-enter new password: ");
00178        
00179                      if( strcmp( newpw, cknewpw )) {
00180                             fprintf( stderr, "Password values do not match\n" );
00181                             return EXIT_FAILURE;
00182                      }
00183               }
00184 
00185               passwd.bv_val = newpw;
00186               passwd.bv_len = strlen(passwd.bv_val);
00187        } else {
00188               hash = passwd;
00189               goto print_pw;
00190        }
00191 
00192        lutil_passwd_hash( &passwd, scheme, &hash, &text );
00193        if( hash.bv_val == NULL ) {
00194               fprintf( stderr,
00195                      "Password generation failed for scheme %s: %s\n",
00196                      scheme, text ? text : "" );
00197               return EXIT_FAILURE;
00198        }
00199 
00200        if( lutil_passwd( &hash, &passwd, NULL, &text ) ) {
00201               fprintf( stderr, "Password verification failed. %s\n",
00202                      text ? text : "" );
00203               return EXIT_FAILURE;
00204        }
00205 
00206 print_pw:;
00207        printf( "%s%s" , hash.bv_val, newline );
00208        return EXIT_SUCCESS;
00209 }