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