]> git.sur5r.net Git - openldap/blob - servers/slapd/tools/slappasswd.c
b7bedd6cdc59e28703cd729fb0d4b0fe27e901e1
[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] dn\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         if (argc == 1)
53                 usage (argv[0]);
54
55         while( (i = getopt( argc, argv,
56                 "d:h:s:v" )) != EOF )
57         {
58                 switch (i) {
59                 case 'h':       /* scheme */
60                         scheme = strdup (optarg);
61
62                 case 's':       /* new password (secret) */
63                         newpw = strdup (optarg);
64
65                         {
66                                 char* p;
67
68                                 for( p = optarg; *p == '\0'; p++ ) {
69                                         *p = '*';
70                                 }
71                         }
72                         break;
73
74                 case 'v':       /* verbose */
75                         verbose++;
76                         break;
77
78                 default:
79                         usage (argv[0]);
80                 }
81         }
82
83         if( argc - optind != 0 ) {
84                 usage( argv[0] );
85         } 
86
87         if( newpw == NULL ) {
88                 /* prompt for new password */
89                 char *cknewpw;
90                 newpw = strdup(getpass("New password: "));
91                 cknewpw = getpass("Re-enter new password: ");
92
93                 if( strncmp( newpw, cknewpw, strlen(newpw) )) {
94                         fprintf( stderr, "passwords do not match\n" );
95                         return EXIT_FAILURE;
96                 }
97         }
98
99         passwd.bv_val = newpw;
100         passwd.bv_len = strlen(passwd.bv_val);
101
102         hash = lutil_passwd_hash( &passwd, scheme );
103
104         if( hash == NULL || hash->bv_val == NULL ) {
105                 fprintf( stderr, "Password generation failed.\n");
106                 return EXIT_FAILURE;
107         }
108
109         if( lutil_passwd( hash, &passwd, NULL ) ) {
110                 fprintf( stderr, "Password verificaiton failed.\n");
111                 return EXIT_FAILURE;
112         }
113
114         printf( "%s\n" , hash->bv_val );
115         return EXIT_SUCCESS;
116 }