]> git.sur5r.net Git - openldap/blob - servers/slapd/tools/slappasswd.c
64b7a1be5e2889a83cc56cfc623032f933012990
[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                 "  -u\t\tgenerate RFC2307 values\n"
35                 "  -v\t\tincrease verbosity\n"
36                 , s );
37
38         exit( EXIT_FAILURE );
39 }
40
41 int
42 main( int argc, char *argv[] )
43 {
44         int rc;
45         char    *scheme = "{SSHA}";
46         char    *newpw = NULL;
47
48         int             i;
49         int             version = -1;
50         struct berval passwd;
51         struct berval *hash = NULL;
52
53         while( (i = getopt( argc, argv,
54                 "d:h:s:vu" )) != EOF )
55         {
56                 switch (i) {
57                 case 'h':       /* scheme */
58                         scheme = strdup (optarg);
59                         break;
60
61                 case 's':       /* new password (secret) */
62                         newpw = strdup (optarg);
63
64                         {
65                                 char* p;
66
67                                 for( p = optarg; *p != '\0'; p++ ) {
68                                         *p = '\0';
69                                 }
70                         }
71                         break;
72
73                 case 'u':       /* RFC2307 userPassword */
74                         break;
75
76                 case 'v':       /* verbose */
77                         verbose++;
78                         break;
79
80                 default:
81                         usage (argv[0]);
82                 }
83         }
84
85         if( argc - optind != 0 ) {
86                 usage( argv[0] );
87         } 
88
89         if( newpw == NULL ) {
90                 /* prompt for new password */
91                 char *cknewpw;
92                 newpw = strdup(getpassphrase("New password: "));
93                 cknewpw = getpassphrase("Re-enter new password: ");
94
95                 if( strncmp( newpw, cknewpw, strlen(newpw) )) {
96                         fprintf( stderr, "Password values do not match\n" );
97                         return EXIT_FAILURE;
98                 }
99         }
100
101         passwd.bv_val = newpw;
102         passwd.bv_len = strlen(passwd.bv_val);
103
104         hash = lutil_passwd_hash( &passwd, scheme );
105
106         if( hash == NULL || hash->bv_val == NULL ) {
107                 fprintf( stderr, "Password generation failed.\n");
108                 return EXIT_FAILURE;
109         }
110
111         if( lutil_passwd( hash, &passwd, NULL ) ) {
112                 fprintf( stderr, "Password verification failed.\n");
113                 return EXIT_FAILURE;
114         }
115
116         printf( "%s\n" , hash->bv_val );
117         return EXIT_SUCCESS;
118 }