]> git.sur5r.net Git - openldap/blob - servers/slapd/slappasswd.c
Happy new year! (belated)
[openldap] / servers / slapd / slappasswd.c
1 /* $OpenLDAP$ */
2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
3  *
4  * Copyright 1998-2008 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 <lutil.h>
36 #include <lutil_sha1.h>
37
38 #include "ldap_defaults.h"
39
40 static int      verbose = 0;
41
42 static void
43 usage(const char *s)
44 {
45         fprintf(stderr,
46                 "Usage: %s [options]\n"
47                 "  -h hash\tpassword scheme\n"
48                 "  -s secret\tnew password\n"
49                 "  -c format\tcrypt(3) salt format\n"
50                 "  -u\t\tgenerate RFC2307 values (default)\n"
51                 "  -v\t\tincrease verbosity\n"
52                 "  -T file\tread file for new password\n"
53                 , s );
54
55         exit( EXIT_FAILURE );
56 }
57
58 int
59 slappasswd( int argc, char *argv[] )
60 {
61 #ifdef LUTIL_SHA1_BYTES
62         char    *scheme = "{SSHA}";
63 #else
64         char    *scheme = "{SMD5}";
65 #endif
66
67         char    *newpw = NULL;
68         char    *pwfile = NULL;
69         const char *text;
70         const char *progname = "slappasswd";
71
72         int             i;
73         struct berval passwd;
74         struct berval hash;
75
76         while( (i = getopt( argc, argv,
77                 "c:d:h:s:T:vu" )) != EOF )
78         {
79                 switch (i) {
80                 case 'c':       /* crypt salt format */
81                         scheme = "{CRYPT}";
82                         lutil_salt_format( optarg );
83                         break;
84
85                 case 'h':       /* scheme */
86                         scheme = strdup( optarg );
87                         break;
88
89                 case 's':       /* new password (secret) */
90                         {
91                                 char* p;
92                                 newpw = strdup( optarg );
93
94                                 for( p = optarg; *p != '\0'; p++ ) {
95                                         *p = '\0';
96                                 }
97                         } break;
98
99                 case 'T':       /* password file */
100                         pwfile = optarg;
101                         break;
102
103                 case 'u':       /* RFC2307 userPassword */
104                         break;
105
106                 case 'v':       /* verbose */
107                         verbose++;
108                         break;
109
110                 default:
111                         usage ( progname );
112                 }
113         }
114
115         if( argc - optind != 0 ) {
116                 usage( progname );
117         } 
118
119         if( pwfile != NULL ) {
120                 if( lutil_get_filed_password( pwfile, &passwd )) {
121                         return EXIT_FAILURE;
122                 }
123         } else {
124                 if( newpw == NULL ) {
125                         /* prompt for new password */
126                         char *cknewpw;
127                         newpw = strdup(getpassphrase("New password: "));
128                         cknewpw = getpassphrase("Re-enter new password: ");
129         
130                         if( strcmp( newpw, cknewpw )) {
131                                 fprintf( stderr, "Password values do not match\n" );
132                                 return EXIT_FAILURE;
133                         }
134                 }
135
136                 passwd.bv_val = newpw;
137                 passwd.bv_len = strlen(passwd.bv_val);
138         }
139
140         lutil_passwd_hash( &passwd, scheme, &hash, &text );
141         if( hash.bv_val == NULL ) {
142                 fprintf( stderr,
143                         "Password generation failed for scheme %s: %s\n",
144                         scheme, text ? text : "" );
145                 return EXIT_FAILURE;
146         }
147
148         if( lutil_passwd( &hash, &passwd, NULL, &text ) ) {
149                 fprintf( stderr, "Password verification failed. %s\n",
150                         text ? text : "" );
151                 return EXIT_FAILURE;
152         }
153
154         printf( "%s\n" , hash.bv_val );
155         return EXIT_SUCCESS;
156 }