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