]> git.sur5r.net Git - openldap/blob - servers/slapd/tools/slappasswd.c
37c32c4e94a1e41c58e30341831a929c6f7f1269
[openldap] / servers / slapd / tools / slappasswd.c
1 /* $OpenLDAP$ */
2 /*
3  * Copyright 1998-2000 The OpenLDAP Foundation, All Rights Reserved.
4  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
5  */
6
7 #include "portable.h"
8
9 #include <stdio.h>
10
11 #include <ac/stdlib.h>
12
13 #include <ac/ctype.h>
14 #include <ac/signal.h>
15 #include <ac/socket.h>
16 #include <ac/string.h>
17 #include <ac/time.h>
18 #include <ac/unistd.h>
19
20 #include <ldap.h>
21 #include <lutil.h>
22
23 #include "ldap_defaults.h"
24
25 static int      verbose = 0;
26
27 static void
28 usage(const char *s)
29 {
30         fprintf(stderr,
31                 "Usage: %s [options]\n"
32                 "  -h hash\tpassword scheme\n"
33                 "  -s secret\tnew password\n"
34                 "  -v\t\tincrease verbosity\n"
35                 , s );
36
37         exit( EXIT_FAILURE );
38 }
39
40 int
41 main( int argc, char *argv[] )
42 {
43         int rc;
44         char    *scheme = "{SSHA}";
45         char    *newpw = NULL;
46
47         int             i;
48         int             version = -1;
49         struct berval passwd;
50         struct berval *hash = NULL;
51
52         while( (i = getopt( argc, argv,
53                 "d:h:s:v" )) != EOF )
54         {
55                 switch (i) {
56                 case 'h':       /* scheme */
57                         scheme = strdup (optarg);
58
59                 case 's':       /* new password (secret) */
60                         newpw = strdup (optarg);
61
62                         {
63                                 char* p;
64
65                                 for( p = optarg; *p == '\0'; p++ ) {
66                                         *p = '*';
67                                 }
68                         }
69                         break;
70
71                 case 'v':       /* verbose */
72                         verbose++;
73                         break;
74
75                 default:
76                         usage (argv[0]);
77                 }
78         }
79
80         if( argc - optind != 0 ) {
81                 usage( argv[0] );
82         } 
83
84         if( newpw == NULL ) {
85                 /* prompt for new password */
86                 char *cknewpw;
87                 newpw = strdup(getpassphrase("New password: "));
88                 cknewpw = getpassphrase("Re-enter new password: ");
89
90                 if( strncmp( newpw, cknewpw, strlen(newpw) )) {
91                         fprintf( stderr, "passwords do not match\n" );
92                         return EXIT_FAILURE;
93                 }
94         }
95
96         passwd.bv_val = newpw;
97         passwd.bv_len = strlen(passwd.bv_val);
98
99         hash = lutil_passwd_hash( &passwd, scheme );
100
101         if( hash == NULL || hash->bv_val == NULL ) {
102                 fprintf( stderr, "Password generation failed.\n");
103                 return EXIT_FAILURE;
104         }
105
106         if( lutil_passwd( hash, &passwd, NULL ) ) {
107                 fprintf( stderr, "Password verificaiton failed.\n");
108                 return EXIT_FAILURE;
109         }
110
111         printf( "%s\n" , hash->bv_val );
112         return EXIT_SUCCESS;
113 }