3 * Copyright 1998-1999 The OpenLDAP Foundation, All Rights Reserved.
4 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
7 * Copyright 1998, David E. Storey, All rights reserved.
8 * This software is not subject to any license of The Murphy Group, Inc.
9 * or George Mason University.
11 * Redistribution and use in source and binary forms are permitted only
12 * as authorized by the OpenLDAP Public License. A copy of this
13 * license is available at http://www.OpenLDAP.org/license.html or
14 * in file LICENSE in the top-level directory of the distribution.
16 * ldappasswd.c - program to modify passwords in an LDAP tree
18 * Author: David E. Storey <dave@tamos.net>
25 #include <ac/stdlib.h>
28 #include <ac/signal.h>
29 #include <ac/socket.h>
30 #include <ac/string.h>
32 #include <ac/unistd.h>
37 #include <lutil_md5.h>
38 #include <lutil_sha1.h>
40 #include "ldap_defaults.h"
43 #define CEILING(x) ((double)(x) > (int)(x) ? (int)(x) + 1 : (int)(x))
45 #define LDAP_PASSWD_ATTRIB "userPassword"
46 #define LDAP_PASSWD_CONF LDAP_SYSCONFDIR LDAP_DIRSEP "passwd.conf"
74 char *(*func) (const char *, Salt *);
75 unsigned char takes_salt;
77 HashTypes type_salted;
78 unsigned int default_salt_len;
82 static int noupdates = 0;
83 static int verbose = 0;
84 static int want_entryhash = 0;
85 static int auto_gen_pw = 0;
90 * pw_encode() essentially base64 encodes a password and its salt
94 pw_encode (unsigned char *passwd, Salt * salt, unsigned int len)
96 int salted = salt && salt->salt && salt->len;
98 char *base64digest = NULL;
99 unsigned char *npasswd = passwd;
103 npasswd = (unsigned char *)malloc (len + salt->len);
104 memcpy (npasswd, passwd, len);
105 memcpy (&npasswd[len], salt->salt, salt->len);
109 b64_len = CEILING (len / 3) * 4 + 1;
110 base64digest = (char *)malloc (b64_len);
111 if (lutil_b64_ntop (npasswd, len, base64digest, b64_len) < 0)
120 return (base64digest);
124 * if you'd like to write a better salt generator, please, be my guest.
128 make_salt (Salt * salt, unsigned int len)
135 salt->salt = (unsigned char *)malloc (len);
137 for (len = 0; len < salt->len; len++)
138 salt->salt[len] = rand () & 0xff;
146 gen_pass (unsigned int len)
148 static const unsigned char autogen[] =
149 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890.,";
156 make_salt (&salt, len);
157 for (i = 0; i < len; i++)
158 salt.salt[i] = autogen[salt.salt[i] % (sizeof (autogen) - 1)];
160 return ((char *)salt.salt);
163 #ifdef SLAPD_CLEARTEXT
165 hash_none (const char *pw_in, Salt * salt)
167 return (strdup (pw_in));
173 hash_crypt (const char *pw_in, Salt * salt)
175 static const unsigned char crypt64[] =
176 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890./";
177 char *crypted_pw = NULL;
180 if (salt && salt->salt && strlen ((char *)salt->salt) >= 2)
183 if (!(isalnum(salt->salt[0]) || salt->salt[0] == '.' || salt->salt[0] == '/'))
184 salt->salt[0] = crypt64[salt->salt[0] % (sizeof (crypt64) - 1)];
185 if (!(isalnum(salt->salt[1]) || salt->salt[1] == '.' || salt->salt[1] == '/'))
186 salt->salt[1] = crypt64[salt->salt[1] % (sizeof (crypt64) - 1)];
188 crypted_pw = crypt (pw_in, (char *)salt->salt);
192 make_salt (&lsalt, 2);
193 lsalt.salt[0] = crypt64[lsalt.salt[0] % (sizeof (crypt64) - 1)];
194 lsalt.salt[1] = crypt64[lsalt.salt[1] % (sizeof (crypt64) - 1)];
195 crypted_pw = crypt (pw_in, (char *)lsalt.salt);
198 return (strdup (crypted_pw));
203 hash_md5 (const char *pw_in, Salt * salt)
205 lutil_MD5_CTX MD5context;
206 unsigned char MD5digest[16];
208 lutil_MD5Init (&MD5context);
209 lutil_MD5Update (&MD5context,
210 (const unsigned char *)pw_in, strlen(pw_in));
211 if (salt && salt->salt && salt->len)
212 lutil_MD5Update (&MD5context, salt->salt, salt->len);
213 lutil_MD5Final (MD5digest, &MD5context);
215 return (pw_encode (MD5digest, salt, sizeof (MD5digest)));
219 hash_sha1 (const char *pw_in, Salt * salt)
221 lutil_SHA1_CTX SHA1context;
222 unsigned char SHA1digest[20];
224 lutil_SHA1Init (&SHA1context);
225 lutil_SHA1Update (&SHA1context,
226 (const unsigned char *)pw_in, strlen(pw_in));
227 if (salt && salt->salt && salt->len)
228 lutil_SHA1Update (&SHA1context, salt->salt, salt->len);
229 lutil_SHA1Final (SHA1digest, &SHA1context);
231 return (pw_encode (SHA1digest, salt, sizeof (SHA1digest)));
234 static const Hash hashes[] =
236 #ifdef SLAPD_CLEARTEXT
237 {"none", 4, hash_none, 0, HASHTYPE_NONE, HASHTYPE_NONE, 0},
240 {"crypt", 5, hash_crypt, 1, HASHTYPE_CRYPT, HASHTYPE_CRYPT, 2},
242 {"md5", 3, hash_md5, 0, HASHTYPE_MD5, HASHTYPE_SMD5, 0},
243 {"smd5", 4, hash_md5, 1, HASHTYPE_SMD5, HASHTYPE_SMD5, 4},
244 {"sha", 3, hash_sha1, 0, HASHTYPE_SHA1, HASHTYPE_SSHA1, 0},
245 {"ssha", 4, hash_sha1, 1, HASHTYPE_SSHA1, HASHTYPE_SSHA1, 4},
246 {NULL, 0, NULL, 0, HASHTYPE_NONE, HASHTYPE_NONE, 0}
250 modify_dn (LDAP * ld, char *targetdn, char *pwattr, char *oldpw,
251 char *newpw, HashTypes htype, Salt * salt)
254 int salted = salt->salt ? 1 : 0;
255 int want_salt = salt->len && !salted;
257 char *hashed_pw = NULL;
259 LDAPMod mod, *mods[2];
261 if (!ld || !targetdn || !newpw)
264 /* auto-generate password */
266 newpw = gen_pass (auto_gen_pw);
271 make_salt (salt, salt->len);
272 htype = hashes[htype].type_salted;
274 else if (hashes[htype].default_salt_len)
276 /* user chose a salted hash and needs a salt */
280 salt->len = hashes[htype].default_salt_len;
281 make_salt (salt, salt->len);
286 hashed_pw = hashes[htype].func (newpw, salt->len ? salt : NULL);
288 /* return salt back to its original state */
295 buf = (char *)malloc (hashes[htype].namesz + 3 + strlen (hashed_pw));
297 sprintf (buf, "{%s}%s", hashes[htype].name, hashed_pw);
299 sprintf (buf, "%s", hashed_pw);
303 printf ("%s", targetdn);
308 printf (":%s", newpw);
315 mod.mod_values = strvals;
316 mod.mod_type = pwattr;
317 mod.mod_op = LDAP_MOD_REPLACE;
321 if (!noupdates && (ret = ldap_modify_s (ld, targetdn, mods)) != LDAP_SUCCESS)
322 ldap_perror (ld, "ldap_modify");
332 fprintf (stderr, "Usage: %s [options] [filter]\n", s);
333 fprintf (stderr, " -a attrib\tpassword attribute (default: " LDAP_PASSWD_ATTRIB ")\n");
334 fprintf (stderr, " -b basedn\tbasedn to perform searches\n");
335 /* fprintf (stderr, " -C\t\tuse entry's current hash mechanism\n"); */
336 fprintf (stderr, " -D binddn\tbind dn\n");
337 fprintf (stderr, " -d level\tdebugging level\n");
338 fprintf (stderr, " -E\t\tprompt for new password\n");
339 fprintf (stderr, " -e passwd\tnew password\n");
340 fprintf (stderr, " -g passlen\tauto-generate passwords with length pwlen\n");
341 fprintf (stderr, " -H hash\thash type (default: crypt)\n");
342 fprintf (stderr, " -h host\tldap server (default: localhost)\n");
344 fprintf (stderr, " -K\t\tuse Kerberos step 1\n");
345 fprintf (stderr, " -k\t\tuse Kerberos\n");
347 fprintf (stderr, " -l time\ttime limit\n");
348 fprintf (stderr, " -n\t\tmake no modifications\n");
349 fprintf (stderr, " -P version\tprotocol version (2 or 3)\n");
350 fprintf (stderr, " -p port\tldap port\n");
351 fprintf (stderr, " -s scope\tsearch scope: base, one, sub (default: sub)\n");
352 fprintf (stderr, " -t targetdn\tdn to change password\n");
353 fprintf (stderr, " -v\t\tverbose (more v's, more verbose)\n");
354 fprintf (stderr, " -W\t\tprompt for bind password\n");
355 fprintf (stderr, " -w passwd\tbind password (for simple authentication)\n");
356 fprintf (stderr, " -Y saltlen\tsalt length to use\n");
357 /* fprintf (stderr, " -y salt\tsalt to use\n"); */
358 fprintf (stderr, " -z size\tsize limit\n");
359 exit( EXIT_FAILURE );
363 main (int argc, char *argv[])
368 char *filtpattern = NULL;
369 char *ldaphost = NULL;
370 char *targetdn = NULL;
371 char *pwattr = LDAP_PASSWD_ATTRIB;
373 int authmethod = LDAP_AUTH_SIMPLE;
374 int hashtype = HASHTYPE_CRYPT;
378 int scope = LDAP_SCOPE_SUBTREE;
393 while ((i = getopt (argc, argv, "a:b:C:D:d:Ee:g:H:h:Kkl:nP:p:s:t:vWw:Y:y:z:")) != EOF)
397 case 'a': /* password attribute */
398 pwattr = strdup (optarg);
401 case 'b': /* base search dn */
402 base = strdup (optarg);
409 case 'D': /* bind distinguished name */
410 binddn = strdup (optarg);
413 case 'd': /* debugging option */
414 debug |= atoi (optarg);
417 case 'E': /* prompt for new password */
421 case 'e': /* new password */
422 newpw = strdup (optarg);
426 auto_gen_pw = strtol (optarg, NULL, 10);
429 case 'H': /* hashes */
430 for (j = 0; hashes[j].name; j++)
432 if (!strncasecmp (optarg, hashes[j].name, hashes[j].namesz))
434 hashtype = hashes[j].type;
441 fprintf (stderr, "hash type: %s is unknown\n", optarg);
446 case 'h': /* ldap host */
447 ldaphost = strdup (optarg);
450 case 'K': /* use kerberos bind, 1st part only */
452 authmethod = LDAP_AUTH_KRBV41;
454 fprintf (stderr, "%s was not compiled with Kerberos support\n", argv[0]);
459 case 'k': /* use kerberos bind */
461 authmethod = LDAP_AUTH_KRBV4;
463 fprintf (stderr, "%s was not compiled with Kerberos support\n", argv[0]);
468 case 'l': /* time limit */
469 timelimit = strtol (optarg, NULL, 10);
472 case 'n': /* don't update entry(s) */
477 switch( atoi( optarg ) ) {
479 version = LDAP_VERSION2;
482 version = LDAP_VERSION3;
485 fprintf( stderr, "protocol version should be 2 or 3\n" );
490 case 'p': /* ldap port */
491 ldapport = strtol (optarg, NULL, 10);
494 case 's': /* scope */
495 if (strcasecmp (optarg, "base") == 0)
496 scope = LDAP_SCOPE_BASE;
497 else if (strcasecmp (optarg, "one") == 0)
498 scope = LDAP_SCOPE_ONELEVEL;
499 else if (strcasecmp (optarg, "sub") == 0)
500 scope = LDAP_SCOPE_SUBTREE;
503 fprintf (stderr, "scope should be base, one, or sub\n");
508 case 't': /* target dn */
509 targetdn = strdup (optarg);
512 case 'v': /* verbose */
516 case 'W': /* promt for bind password */
520 case 'w': /* bind password */
521 bindpw = strdup (optarg);
525 for( p = optarg; *p == '\0'; p++ ) {
531 case 'Y': /* salt length */
532 salt.len = strtol (optarg, NULL, 10);
535 case 'y': /* user specified salt */
536 salt.len = strlen (optarg);
537 salt.salt = (unsigned char *)strdup (optarg);
540 case 'z': /* time limit */
541 sizelimit = strtol (optarg, NULL, 10);
550 if (!(argc - optind < 1))
551 filtpattern = strdup (argv[optind]);
553 /* check for target(s) */
554 if (!filtpattern && !targetdn)
557 /* handle bind password */
559 bindpw = strdup (getpass ("Enter LDAP password: "));
561 /* handle new password */
565 newpw = strdup (getpass ("New password: "));
566 cknewpw = getpass ("Re-enter new password: ");
568 if (strncmp (newpw, cknewpw, strlen (newpw)))
570 fprintf (stderr, "passwords do not match\n");
571 return ( EXIT_FAILURE );
576 if( ber_set_option( NULL, LBER_OPT_DEBUG_LEVEL, &debug ) != LBER_OPT_SUCCESS ) {
577 fprintf( stderr, "Could not set LBER_OPT_DEBUG_LEVEL %d\n", debug );
579 if( ldap_set_option( NULL, LDAP_OPT_DEBUG_LEVEL, &debug ) != LDAP_OPT_SUCCESS ) {
580 fprintf( stderr, "Could not set LDAP_OPT_DEBUG_LEVEL %d\n", debug );
585 (void) SIGNAL( SIGPIPE, SIG_IGN );
587 /* seed random number generator */
589 #ifdef HAVE_GETTIMEOFDAY
590 /* this is of questionable value
591 * gettimeofday may not provide much usec
595 gettimeofday (&tv, NULL);
596 srand(tv.tv_sec * (tv.tv_usec + 1));
599 /* The traditional seed */
600 srand((unsigned)time( NULL ));
603 /* connect to server */
604 if ((ld = ldap_init (ldaphost, ldapport)) == NULL)
606 perror ("ldap_init");
607 return ( EXIT_FAILURE );
611 if (timelimit != -1 &&
612 ldap_set_option( ld, LDAP_OPT_TIMELIMIT, (void *) &timelimit ) != LDAP_OPT_SUCCESS )
614 fprintf( stderr, "Could not set LDAP_OPT_TIMELIMIT %d\n", timelimit );
616 if (sizelimit != -1 &&
617 ldap_set_option( ld, LDAP_OPT_SIZELIMIT, (void *) &sizelimit ) != LDAP_OPT_SUCCESS )
619 fprintf( stderr, "Could not set LDAP_OPT_SIZELIMIT %d\n", sizelimit );
622 /* this seems prudent */
624 int deref = LDAP_DEREF_NEVER;
625 ldap_set_option( ld, LDAP_OPT_DEREF, &deref);
627 /* don't chase referrals */
628 ldap_set_option( ld, LDAP_OPT_REFERRALS, LDAP_OPT_OFF );
631 ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, &version ) != LDAP_OPT_SUCCESS )
633 fprintf( stderr, "Could not set LDAP_OPT_PROTOCOL_VERSION %d\n", version );
636 /* authenticate to server */
637 if (ldap_bind_s (ld, binddn, bindpw, authmethod) != LDAP_SUCCESS)
639 ldap_perror (ld, "ldap_bind");
640 return ( EXIT_FAILURE );
647 /* insert code here =) */
650 modify_dn (ld, targetdn, pwattr, NULL, newpw, hashtype, &salt);
656 LDAPMessage *result = NULL, *e;
662 sprintf (filter, "%s", filtpattern);
663 i = ldap_search_s (ld, base, scope, filter, attrs, 0, &result);
664 if (i != LDAP_SUCCESS &&
665 i != LDAP_TIMELIMIT_EXCEEDED &&
666 i != LDAP_SIZELIMIT_EXCEEDED)
668 ldap_perror (ld, "ldap_search");
669 return ( EXIT_FAILURE );
672 for (e = ldap_first_entry (ld, result); e; e = ldap_next_entry (ld, e))
674 char *dn = ldap_get_dn (ld, e);
677 struct berval **pw_vals = ldap_get_values_len (ld, e, pwattr);
678 modify_dn (ld, dn, pwattr, pw_vals ? pw_vals[0]->bv_val : NULL, newpw, hashtype, &salt);
680 ldap_value_free_len (pw_vals);
686 /* disconnect from server */
689 return ( EXIT_SUCCESS );