2 * Copyright 1998, David E. Storey, All rights reserved.
3 * This software is not subject to any license of The Murphy Group, Inc.
4 * or George Mason University.
6 * Redistribution and use in source and binary forms are permitted only
7 * as authorized by the OpenLDAP Public License. A copy of this
8 * license is available at http://www.OpenLDAP.org/license.html or
9 * in file LICENSE in the top-level directory of the distribution.
11 * ldappasswd.c - program to modify passwords in an LDAP tree
13 * Author: David E. Storey <dave@tamos.net>
20 #include <ac/stdlib.h>
23 #include <ac/signal.h>
24 #include <ac/socket.h>
25 #include <ac/string.h>
27 #include <ac/unistd.h>
32 #include <lutil_md5.h>
33 #include <lutil_sha1.h>
35 #include "ldap_defaults.h"
38 #define CEILING(x) ((double)(x) > (int)(x) ? (int)(x) + 1 : (int)(x))
40 #define LDAP_PASSWD_ATTRIB "userPassword"
41 #define LDAP_PASSWD_CONF LDAP_SYSCONFDIR LDAP_DIRSEP "passwd.conf"
69 char *(*func) (const char *, Salt *);
70 unsigned char takes_salt;
72 HashTypes type_salted;
73 unsigned int default_salt_len;
77 static int noupdates = 0;
78 static int verbose = 0;
79 static int want_entryhash = 0;
80 static int auto_gen_pw = 0;
85 * pw_encode() essentially base64 encodes a password and its salt
89 pw_encode (unsigned char *passwd, Salt * salt, unsigned int len)
91 int salted = salt && salt->salt && salt->len;
93 char *base64digest = NULL;
94 unsigned char *npasswd = passwd;
98 npasswd = (unsigned char *)malloc (len + salt->len);
99 memcpy (npasswd, passwd, len);
100 memcpy (&npasswd[len], salt->salt, salt->len);
104 b64_len = CEILING (len / 3) * 4 + 1;
105 base64digest = (char *)malloc (b64_len);
106 if (lutil_b64_ntop (npasswd, len, base64digest, b64_len) < 0)
115 return (base64digest);
119 * if you'd like to write a better salt generator, please, be my guest.
123 make_salt (Salt * salt, unsigned int len)
130 salt->salt = (unsigned char *)malloc (len);
132 for (len = 0; len < salt->len; len++)
133 salt->salt[len] = rand () & 0xff;
141 gen_pass (unsigned int len)
143 static const unsigned char autogen[] =
144 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890.,";
151 make_salt (&salt, len);
152 for (i = 0; i < len; i++)
153 salt.salt[i] = autogen[salt.salt[i] % (sizeof (autogen) - 1)];
155 return ((char *)salt.salt);
158 #ifdef SLAPD_CLEARTEXT
160 hash_none (const char *pw_in, Salt * salt)
162 return (strdup (pw_in));
168 hash_crypt (const char *pw_in, Salt * salt)
170 static const unsigned char crypt64[] =
171 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890./";
172 char *crypted_pw = NULL;
175 if (salt && salt->salt && strlen ((char *)salt->salt) >= 2)
178 if (!(isalnum(salt->salt[0]) || salt->salt[0] == '.' || salt->salt[0] == '/'))
179 salt->salt[0] = crypt64[salt->salt[0] % (sizeof (crypt64) - 1)];
180 if (!(isalnum(salt->salt[1]) || salt->salt[1] == '.' || salt->salt[1] == '/'))
181 salt->salt[1] = crypt64[salt->salt[1] % (sizeof (crypt64) - 1)];
183 crypted_pw = crypt (pw_in, (char *)salt->salt);
187 make_salt (&lsalt, 2);
188 lsalt.salt[0] = crypt64[lsalt.salt[0] % (sizeof (crypt64) - 1)];
189 lsalt.salt[1] = crypt64[lsalt.salt[1] % (sizeof (crypt64) - 1)];
190 crypted_pw = crypt (pw_in, (char *)lsalt.salt);
193 return (strdup (crypted_pw));
198 hash_md5 (const char *pw_in, Salt * salt)
200 lutil_MD5_CTX MD5context;
201 unsigned char MD5digest[16];
203 lutil_MD5Init (&MD5context);
204 lutil_MD5Update (&MD5context,
205 (const unsigned char *)pw_in, strlen(pw_in));
206 if (salt && salt->salt && salt->len)
207 lutil_MD5Update (&MD5context, salt->salt, salt->len);
208 lutil_MD5Final (MD5digest, &MD5context);
210 return (pw_encode (MD5digest, salt, sizeof (MD5digest)));
214 hash_sha1 (const char *pw_in, Salt * salt)
216 lutil_SHA1_CTX SHA1context;
217 unsigned char SHA1digest[20];
219 lutil_SHA1Init (&SHA1context);
220 lutil_SHA1Update (&SHA1context,
221 (const unsigned char *)pw_in, strlen(pw_in));
222 if (salt && salt->salt && salt->len)
223 lutil_SHA1Update (&SHA1context, salt->salt, salt->len);
224 lutil_SHA1Final (SHA1digest, &SHA1context);
226 return (pw_encode (SHA1digest, salt, sizeof (SHA1digest)));
229 static Hash hashes[] =
231 #ifdef SLAPD_CLEARTEXT
232 {"none", 4, hash_none, 0, HASHTYPE_NONE, HASHTYPE_NONE, 0},
235 {"crypt", 5, hash_crypt, 1, HASHTYPE_CRYPT, HASHTYPE_CRYPT, 2},
237 {"md5", 3, hash_md5, 0, HASHTYPE_MD5, HASHTYPE_SMD5, 0},
238 {"smd5", 4, hash_md5, 1, HASHTYPE_SMD5, HASHTYPE_SMD5, 4},
239 {"sha", 3, hash_sha1, 0, HASHTYPE_SHA1, HASHTYPE_SSHA1, 0},
240 {"ssha", 4, hash_sha1, 1, HASHTYPE_SSHA1, HASHTYPE_SSHA1, 4},
241 {NULL, 0, NULL, 0, HASHTYPE_NONE, HASHTYPE_NONE, 0}
245 modify_dn (LDAP * ld, char *targetdn, char *pwattr, char *oldpw,
246 char *newpw, HashTypes htype, Salt * salt)
249 int salted = salt->salt ? 1 : 0;
250 int want_salt = salt->len && !salted;
252 char *hashed_pw = NULL;
254 LDAPMod mod, *mods[2];
256 if (!ld || !targetdn || !newpw)
259 /* auto-generate password */
261 newpw = gen_pass (auto_gen_pw);
266 make_salt (salt, salt->len);
267 htype = hashes[htype].type_salted;
269 else if (hashes[htype].default_salt_len)
271 /* user chose a salted hash and needs a salt */
275 salt->len = hashes[htype].default_salt_len;
276 make_salt (salt, salt->len);
281 hashed_pw = hashes[htype].func (newpw, salt->len ? salt : NULL);
283 /* return salt back to its original state */
290 buf = (char *)malloc (hashes[htype].namesz + 3 + strlen (hashed_pw));
292 sprintf (buf, "{%s}%s", hashes[htype].name, hashed_pw);
294 sprintf (buf, "%s", hashed_pw);
298 printf ("%s", targetdn);
303 printf (":%s", newpw);
310 mod.mod_values = strvals;
311 mod.mod_type = pwattr;
312 mod.mod_op = LDAP_MOD_REPLACE;
316 if (!noupdates && (ret = ldap_modify_s (ld, targetdn, mods)) != LDAP_SUCCESS)
317 ldap_perror (ld, "ldap_modify");
327 fprintf (stderr, "Usage: %s [options] [filter]\n", s);
328 fprintf (stderr, " -a attrib\tpassword attribute (default: %s)\n", LDAP_PASSWD_ATTRIB);
329 fprintf (stderr, " -b basedn\tbasedn to perform searches\n");
330 /* fprintf (stderr, " -C\t\tuse entry's current hash mechanism\n"); */
331 fprintf (stderr, " -D binddn\tbind dn\n");
332 fprintf (stderr, " -d level\tdebugging level\n");
333 fprintf (stderr, " -E\t\tprompt for new password\n");
334 fprintf (stderr, " -e passwd\tnew password\n");
335 fprintf (stderr, " -g passlen\tauto-generate passwords with length pwlen\n");
336 fprintf (stderr, " -H hash\thash type (default: crypt)\n");
337 fprintf (stderr, " -h host\tldap server (default: localhost)\n");
339 fprintf (stderr, " -K\t\tuse Kerberos step 1\n");
340 fprintf (stderr, " -k\t\tuse Kerberos\n");
342 fprintf (stderr, " -l time\ttime limit\n");
343 fprintf (stderr, " -n\t\tmake no modifications\n");
344 fprintf (stderr, " -P version\tprotocol version (2 or 3)\n");
345 fprintf (stderr, " -p port\tldap port\n");
346 fprintf (stderr, " -s scope\tsearch scope: base, one, sub (default: sub)\n");
347 fprintf (stderr, " -t targetdn\tdn to change password\n");
348 fprintf (stderr, " -v\t\tverbose (more v's, more verbose)\n");
349 fprintf (stderr, " -W\t\tprompt for bind password\n");
350 fprintf (stderr, " -w passwd\tbind password (for simple authentication)\n");
351 fprintf (stderr, " -Y saltlen\tsalt length to use\n");
352 /* fprintf (stderr, " -y salt\tsalt to use\n"); */
353 fprintf (stderr, " -z size\tsize limit\n");
358 main (int argc, char *argv[])
363 char *filtpattern = NULL;
364 char *ldaphost = NULL;
365 char *targetdn = NULL;
366 char *pwattr = LDAP_PASSWD_ATTRIB;
368 int authmethod = LDAP_AUTH_SIMPLE;
369 int hashtype = HASHTYPE_CRYPT;
373 int scope = LDAP_SCOPE_SUBTREE;
388 while ((i = getopt (argc, argv, "a:b:C:D:d:Ee:g:H:h:Kkl:nP:p:s:t:vWw:Y:y:z:")) != EOF)
392 case 'a': /* password attribute */
393 pwattr = strdup (optarg);
396 case 'b': /* base search dn */
397 base = strdup (optarg);
404 case 'D': /* bind distinguished name */
405 binddn = strdup (optarg);
408 case 'd': /* debugging option */
409 debug |= atoi (optarg);
412 case 'E': /* prompt for new password */
416 case 'e': /* new password */
417 newpw = strdup (optarg);
421 auto_gen_pw = strtol (optarg, NULL, 10);
424 case 'H': /* hashes */
425 for (j = 0; hashes[j].name; j++)
427 if (!strncasecmp (optarg, hashes[j].name, hashes[j].namesz))
429 hashtype = hashes[j].type;
436 fprintf (stderr, "hash type: %s is unknown\n", optarg);
441 case 'h': /* ldap host */
442 ldaphost = strdup (optarg);
445 case 'K': /* use kerberos bind, 1st part only */
447 authmethod = LDAP_AUTH_KRBV41;
449 fprintf (stderr, "%s was not compiled with Kerberos support\n", argv[0]);
454 case 'k': /* use kerberos bind */
456 authmethod = LDAP_AUTH_KRBV4;
458 fprintf (stderr, "%s was not compiled with Kerberos support\n", argv[0]);
463 case 'l': /* time limit */
464 timelimit = strtol (optarg, NULL, 10);
467 case 'n': /* don't update entry(s) */
472 switch( atoi( optarg ) ) {
474 version = LDAP_VERSION2;
477 version = LDAP_VERSION3;
480 fprintf( stderr, "protocol version should be 2 or 3\n" );
485 case 'p': /* ldap port */
486 ldapport = strtol (optarg, NULL, 10);
489 case 's': /* scope */
490 if (strcasecmp (optarg, "base") == 0)
491 scope = LDAP_SCOPE_BASE;
492 else if (strcasecmp (optarg, "one") == 0)
493 scope = LDAP_SCOPE_ONELEVEL;
494 else if (strcasecmp (optarg, "sub") == 0)
495 scope = LDAP_SCOPE_SUBTREE;
498 fprintf (stderr, "scope should be base, one, or sub\n");
503 case 't': /* target dn */
504 targetdn = strdup (optarg);
507 case 'v': /* verbose */
511 case 'W': /* promt for bind password */
515 case 'w': /* bind password */
516 bindpw = strdup (optarg);
519 case 'Y': /* salt length */
520 salt.len = strtol (optarg, NULL, 10);
523 case 'y': /* user specified salt */
524 salt.len = strlen (optarg);
525 salt.salt = (unsigned char *)strdup (optarg);
528 case 'z': /* time limit */
529 sizelimit = strtol (optarg, NULL, 10);
538 if (!(argc - optind < 1))
539 filtpattern = strdup (argv[optind]);
541 /* check for target(s) */
542 if (!filtpattern && !targetdn)
545 /* handle bind password */
547 bindpw = strdup (getpass ("Enter LDAP password: "));
549 /* handle new password */
553 newpw = strdup (getpass ("New password: "));
554 cknewpw = getpass ("Re-enter new password: ");
556 if (strncmp (newpw, cknewpw, strlen (newpw)))
558 fprintf (stderr, "passwords do not match\n");
559 return ( EXIT_FAILURE );
564 if( ber_set_option( NULL, LBER_OPT_DEBUG_LEVEL, &debug ) != LBER_OPT_SUCCESS ) {
565 fprintf( stderr, "Could not set LBER_OPT_DEBUG_LEVEL %d\n", debug );
567 if( ldap_set_option( NULL, LDAP_OPT_DEBUG_LEVEL, &debug ) != LDAP_OPT_SUCCESS ) {
568 fprintf( stderr, "Could not set LDAP_OPT_DEBUG_LEVEL %d\n", debug );
573 (void) SIGNAL( SIGPIPE, SIG_IGN );
575 /* seed random number generator */
577 #ifdef HAVE_GETTIMEOFDAY
578 /* this is of questionable value
579 * gettimeofday not provide much usec
583 gettimeofday (&tv, NULL);
587 /* The traditional seed */
588 srand((unsigned)time( NULL ));
591 /* connect to server */
592 if ((ld = ldap_init (ldaphost, ldapport)) == NULL)
594 perror ("ldap_init");
595 return ( EXIT_FAILURE );
599 if (timelimit != -1 &&
600 ldap_set_option( ld, LDAP_OPT_TIMELIMIT, (void *) &timelimit ) != LDAP_OPT_SUCCESS )
602 fprintf( stderr, "Could not set LDAP_OPT_TIMELIMIT %d\n", timelimit );
604 if (sizelimit != -1 &&
605 ldap_set_option( ld, LDAP_OPT_SIZELIMIT, (void *) &sizelimit ) != LDAP_OPT_SUCCESS )
607 fprintf( stderr, "Could not set LDAP_OPT_SIZELIMIT %d\n", sizelimit );
610 /* this seems prudent */
612 int deref = LDAP_DEREF_NEVER;
613 ldap_set_option( ld, LDAP_OPT_DEREF, &deref);
617 ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, &version ) != LDAP_OPT_SUCCESS )
619 fprintf( stderr, "Could not set LDAP_OPT_PROTOCOL_VERSION %d\n", version );
622 /* authenticate to server */
623 if (ldap_bind_s (ld, binddn, bindpw, authmethod) != LDAP_SUCCESS)
625 ldap_perror (ld, "ldap_bind");
626 return ( EXIT_FAILURE );
633 /* insert code here =) */
636 modify_dn (ld, targetdn, pwattr, NULL, newpw, hashtype, &salt);
642 LDAPMessage *result = NULL, *e;
649 sprintf (filter, "%s", filtpattern);
650 i = ldap_search_s (ld, base, scope, filter, attrs, 0, &result);
651 if (i != LDAP_SUCCESS &&
652 i != LDAP_TIMELIMIT_EXCEEDED &&
653 i != LDAP_SIZELIMIT_EXCEEDED)
655 ldap_perror (ld, "ldap_search");
656 return ( EXIT_FAILURE );
659 for (e = ldap_first_entry (ld, result); e; e = ldap_next_entry (ld, e))
661 char *dn = ldap_get_dn (ld, e);
664 struct berval **pw_vals = ldap_get_values_len (ld, e, pwattr);
665 modify_dn (ld, dn, pwattr, pw_vals ? pw_vals[0]->bv_val : NULL, newpw, hashtype, &salt);
667 ldap_value_free_len (pw_vals);
673 /* disconnect from server */
676 return ( EXIT_SUCCESS );