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