]> git.sur5r.net Git - openldap/blob - servers/slapd/slappasswd.c
02de9ee93e8bdf1ead9c9a90376b08745166685e
[openldap] / servers / slapd / slappasswd.c
1 /* $OpenLDAP$ */
2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
3  *
4  * Copyright 1998-2006 The OpenLDAP Foundation.
5  * Portions Copyright 1998-2003 Kurt D. Zeilenga.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted only as authorized by the OpenLDAP
10  * Public License.
11  *
12  * A copy of this license is available in file LICENSE in the
13  * top-level directory of the distribution or, alternatively, at
14  * <http://www.OpenLDAP.org/license.html>.
15  */
16 /* ACKNOWLEDGEMENTS:
17  * This work was initially developed by Kurt Zeilenga for inclusion
18  * in OpenLDAP Software.
19  */
20
21 #include "portable.h"
22
23 #include <stdio.h>
24
25 #include <ac/stdlib.h>
26
27 #include <ac/ctype.h>
28 #include <ac/signal.h>
29 #include <ac/socket.h>
30 #include <ac/string.h>
31 #include <ac/time.h>
32 #include <ac/unistd.h>
33
34 #include <ldap.h>
35 #include <lber_pvt.h>
36 #include <lutil.h>
37 #include <lutil_sha1.h>
38
39 #include "ldap_defaults.h"
40
41 static int      verbose = 0;
42
43 static void
44 usage(const char *s)
45 {
46         fprintf(stderr,
47                 "Usage: %s [options]\n"
48                 "  -c format\tcrypt(3) salt format\n"
49                 "  -g\t\tgenerate random password\n"
50                 "  -h hash\tpassword scheme\n"
51                 "  -s secret\tnew password\n"
52                 "  -u\t\tgenerate RFC2307 values (default)\n"
53                 "  -v\t\tincrease verbosity\n"
54                 "  -T file\tread file for new password\n"
55                 , s );
56
57         exit( EXIT_FAILURE );
58 }
59
60 int
61 slappasswd( int argc, char *argv[] )
62 {
63 #ifdef LUTIL_SHA1_BYTES
64         char    *default_scheme = "{SSHA}";
65 #else
66         char    *default_scheme = "{SMD5}";
67 #endif
68         char    *scheme = default_scheme;
69
70         char    *newpw = NULL;
71         char    *pwfile = NULL;
72         const char *text;
73         const char *progname = "slappasswd";
74
75         int             i;
76         struct berval passwd = BER_BVNULL;
77         struct berval hash;
78
79         while( (i = getopt( argc, argv,
80                 "c:d:gh:s:T:vu" )) != EOF )
81         {
82                 switch (i) {
83                 case 'c':       /* crypt salt format */
84                         scheme = "{CRYPT}";
85                         lutil_salt_format( optarg );
86                         break;
87
88                 case 'g':       /* new password (generate) */
89                         if ( pwfile != NULL ) {
90                                 fprintf( stderr, "Option -g incompatible with -T\n" );
91                                 return EXIT_FAILURE;
92
93                         } else if ( newpw != NULL ) {
94                                 fprintf( stderr, "New password already provided\n" );
95                                 return EXIT_FAILURE;
96
97                         } else if ( lutil_passwd_generate( &passwd, 8 )) {
98                                 fprintf( stderr, "Password generation failed\n" );
99                                 return EXIT_FAILURE;
100                         }
101                         break;
102
103                 case 'h':       /* scheme */
104                         if ( scheme != default_scheme ) {
105                                 fprintf( stderr, "Scheme already provided\n" );
106                                 return EXIT_FAILURE;
107
108                         } else {
109                                 scheme = strdup( optarg );
110                         }
111                         break;
112
113                 case 's':       /* new password (secret) */
114                         if ( pwfile != NULL ) {
115                                 fprintf( stderr, "Option -s incompatible with -T\n" );
116                                 return EXIT_FAILURE;
117
118                         } else if ( newpw != NULL ) {
119                                 fprintf( stderr, "New password already provided\n" );
120                                 return EXIT_FAILURE;
121
122                         } else {
123                                 char* p;
124                                 newpw = strdup( optarg );
125
126                                 for( p = optarg; *p != '\0'; p++ ) {
127                                         *p = '\0';
128                                 }
129                         }
130                         break;
131
132                 case 'T':       /* password file */
133                         if ( pwfile != NULL ) {
134                                 fprintf( stderr, "Password file already provided\n" );
135                                 return EXIT_FAILURE;
136
137                         } else if ( newpw != NULL ) {
138                                 fprintf( stderr, "Option -T incompatible with -s/-g\n" );
139                                 return EXIT_FAILURE;
140
141                         }
142                         pwfile = optarg;
143                         break;
144
145                 case 'u':       /* RFC2307 userPassword */
146                         break;
147
148                 case 'v':       /* verbose */
149                         verbose++;
150                         break;
151
152                 default:
153                         usage ( progname );
154                 }
155         }
156
157         if( argc - optind != 0 ) {
158                 usage( progname );
159         } 
160
161         if( pwfile != NULL ) {
162                 if( lutil_get_filed_password( pwfile, &passwd )) {
163                         return EXIT_FAILURE;
164                 }
165         } else if ( BER_BVISEMPTY( &passwd )) {
166                 if( newpw == NULL ) {
167                         /* prompt for new password */
168                         char *cknewpw;
169                         newpw = strdup(getpassphrase("New password: "));
170                         cknewpw = getpassphrase("Re-enter new password: ");
171         
172                         if( strcmp( newpw, cknewpw )) {
173                                 fprintf( stderr, "Password values do not match\n" );
174                                 return EXIT_FAILURE;
175                         }
176                 }
177
178                 passwd.bv_val = newpw;
179                 passwd.bv_len = strlen(passwd.bv_val);
180         } else {
181                 /* Omit trailing newline so it may be directed to a pwfile */
182                 printf( "%s", passwd.bv_val );
183                 return EXIT_SUCCESS;
184         }
185
186         lutil_passwd_hash( &passwd, scheme, &hash, &text );
187         if( hash.bv_val == NULL ) {
188                 fprintf( stderr,
189                         "Password generation failed for scheme %s: %s\n",
190                         scheme, text ? text : "" );
191                 return EXIT_FAILURE;
192         }
193
194         if( lutil_passwd( &hash, &passwd, NULL, &text ) ) {
195                 fprintf( stderr, "Password verification failed. %s\n",
196                         text ? text : "" );
197                 return EXIT_FAILURE;
198         }
199
200         printf( "%s\n" , hash.bv_val );
201         return EXIT_SUCCESS;
202 }