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>
23 #include <ac/signal.h>
24 #include <ac/string.h>
25 #include <ac/unistd.h>
30 #include <lutil_md5.h>
31 #include <lutil_sha1.h>
33 #include "ldapconfig.h"
36 #define CEILING(x) ((double)x > (int)x ? (int)x + 1 : (int)x)
37 #define STRDUP(x) (x ? strcpy(malloc(strlen(x) + 1), x) : NULL)
39 #define LDAP_PASSWD_ATTRIB "userPassword"
40 #define LDAP_PASSWD_CONF DEFAULT_SYSCONFDIR"/passwd.conf"
68 char *(*func) (const char *, Salt *);
69 unsigned char takes_salt;
71 HashTypes type_salted;
72 unsigned int default_salt_len;
76 static int noupdates = 0;
77 static int verbose = 0;
78 static int want_entryhash = 0;
79 static int auto_gen_pw = 0;
84 * pw_encode() essentially base64 encodes a password and its salt
88 pw_encode (unsigned char *passwd, Salt * salt, unsigned int len)
90 int salted = salt && salt->salt && salt->len;
92 char *base64digest = NULL;
93 unsigned char *npasswd = passwd;
97 npasswd = (unsigned char *)malloc (len + salt->len);
98 memcpy (npasswd, passwd, len);
99 memcpy (&npasswd[len], salt->salt, salt->len);
103 b64_len = CEILING (len / 3) * 4 + 1;
104 base64digest = (char *)malloc (b64_len);
105 if (lutil_b64_ntop (npasswd, len, base64digest, b64_len) < 0)
114 return (base64digest);
118 * if you'd like to write a better salt generator, please, be my guest.
122 make_salt (Salt * salt, unsigned int len)
129 /* seed random number generator */
130 gettimeofday (&tv, NULL);
134 salt->salt = (unsigned char *)malloc (len);
136 for (len = 0; len < salt->len; len++)
137 salt->salt[len] = (tv.tv_usec ^ rand ()) & 0xff;
145 gen_pass (unsigned int len)
147 const unsigned char autogen[] =
148 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890.,";
155 make_salt (&salt, len);
156 for (i = 0; i < len; i++)
157 salt.salt[i] = autogen[salt.salt[i] % (sizeof (autogen) - 1)];
159 return ((char *)salt.salt);
162 #ifdef SLAPD_CLEARTEXT
164 hash_none (const char *pw_in, Salt * salt)
166 return (STRDUP (pw_in));
172 hash_crypt (const char *pw_in, Salt * salt)
174 const unsigned char crypt64[] =
175 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890./";
176 char *crypted_pw = NULL;
179 if (salt && salt->salt && strlen ((char *)salt->salt) >= 2)
182 if (!(isalnum(salt->salt[0]) || salt->salt[0] == '.' || salt->salt[0] == '/'))
183 salt->salt[0] = crypt64[salt->salt[0] % (sizeof (crypt64) - 1)];
184 if (!(isalnum(salt->salt[1]) || salt->salt[1] == '.' || salt->salt[1] == '/'))
185 salt->salt[1] = crypt64[salt->salt[1] % (sizeof (crypt64) - 1)];
187 crypted_pw = crypt (pw_in, (char *)salt->salt);
191 make_salt (&lsalt, 2);
192 lsalt.salt[0] = crypt64[lsalt.salt[0] % (sizeof (crypt64) - 1)];
193 lsalt.salt[1] = crypt64[lsalt.salt[1] % (sizeof (crypt64) - 1)];
194 crypted_pw = crypt (pw_in, (char *)lsalt.salt);
197 return (STRDUP (crypted_pw));
202 hash_md5 (const char *pw_in, Salt * salt)
204 lutil_MD5_CTX MD5context;
205 unsigned char MD5digest[16];
207 lutil_MD5Init (&MD5context);
208 lutil_MD5Update (&MD5context,
209 (const unsigned char *)pw_in, strlen(pw_in));
210 if (salt && salt->salt && salt->len)
211 lutil_MD5Update (&MD5context, salt->salt, salt->len);
212 lutil_MD5Final (MD5digest, &MD5context);
214 return (pw_encode (MD5digest, salt, sizeof (MD5digest)));
218 hash_sha1 (const char *pw_in, Salt * salt)
220 lutil_SHA1_CTX SHA1context;
221 unsigned char SHA1digest[20];
223 lutil_SHA1Init (&SHA1context);
224 lutil_SHA1Update (&SHA1context,
225 (const unsigned char *)pw_in, strlen(pw_in));
226 if (salt && salt->salt && salt->len)
227 lutil_SHA1Update (&SHA1context, salt->salt, salt->len);
228 lutil_SHA1Final (SHA1digest, &SHA1context);
230 return (pw_encode (SHA1digest, salt, sizeof (SHA1digest)));
233 static Hash hashes[] =
235 #ifdef SLAPD_CLEARTEXT
236 {"none", 4, hash_none, 0, HASHTYPE_NONE, HASHTYPE_NONE, 0},
239 {"crypt", 5, hash_crypt, 1, HASHTYPE_CRYPT, HASHTYPE_CRYPT, 2},
241 {"md5", 3, hash_md5, 0, HASHTYPE_MD5, HASHTYPE_SMD5, 0},
242 {"smd5", 4, hash_md5, 1, HASHTYPE_SMD5, HASHTYPE_SMD5, 4},
243 {"sha", 3, hash_sha1, 0, HASHTYPE_SHA1, HASHTYPE_SSHA1, 0},
244 {"ssha", 4, hash_sha1, 1, HASHTYPE_SSHA1, HASHTYPE_SSHA1, 4},
245 {NULL, 0, NULL, 0, HASHTYPE_NONE, HASHTYPE_NONE, 0}
249 modify_dn (LDAP * ld, char *targetdn, char *pwattr, char *oldpw,
250 char *newpw, HashTypes htype, Salt * salt)
253 int salted = salt->salt ? 1 : 0;
254 int want_salt = salt->len && !salted;
256 char *hashed_pw = NULL;
258 LDAPMod mod, *mods[2];
260 if (!ld || !targetdn || !newpw)
263 /* auto-generate password */
265 newpw = gen_pass (auto_gen_pw);
270 make_salt (salt, salt->len);
271 htype = hashes[htype].type_salted;
273 else if (hashes[htype].default_salt_len)
275 /* user chose a salted hash and needs a salt */
279 salt->len = hashes[htype].default_salt_len;
280 make_salt (salt, salt->len);
285 hashed_pw = hashes[htype].func (newpw, salt->len ? salt : NULL);
287 /* return salt back to its original state */
294 buf = (char *)malloc (hashes[htype].namesz + 3 + strlen (hashed_pw));
296 sprintf (buf, "{%s}%s", hashes[htype].name, hashed_pw);
298 sprintf (buf, "%s", hashed_pw);
302 printf ("%s", targetdn);
307 printf (":%s", newpw);
314 mod.mod_vals.modv_strvals = strvals;
315 mod.mod_type = pwattr;
316 mod.mod_op = LDAP_MOD_REPLACE;
320 if (!noupdates && (ret = ldap_modify_s (ld, targetdn, mods)) != LDAP_SUCCESS)
321 ldap_perror (ld, "ldap_modify");
331 fprintf (stderr, "Usage: %s [options] [filter]\n", s);
332 fprintf (stderr, " -a attrib\tpassword attribute (default: %s)\n", LDAP_PASSWD_ATTRIB);
333 fprintf (stderr, " -b basedn\tbasedn to perform searches\n");
334 /* fprintf (stderr, " -C\t\tuse entry's current hash mechanism\n"); */
335 fprintf (stderr, " -D binddn\tbind dn\n");
336 fprintf (stderr, " -d level\tdebugging level\n");
337 fprintf (stderr, " -E\t\tprompt for new password\n");
338 fprintf (stderr, " -e passwd\tnew password\n");
339 fprintf (stderr, " -g passlen\tauto-generate passwords with length pwlen\n");
340 fprintf (stderr, " -H hash\thash type (default: crypt)\n");
341 fprintf (stderr, " -h host\tldap server (default: localhost)\n");
343 fprintf (stderr, " -K\t\tuse Kerberos step 1\n");
344 fprintf (stderr, " -k\t\tuse Kerberos\n");
346 fprintf (stderr, " -l time\ttime limit\n");
347 fprintf (stderr, " -n\t\tmake no modifications\n");
348 fprintf (stderr, " -P version\tprotocol version (2 or 3)\n");
349 fprintf (stderr, " -p port\tldap port\n");
350 fprintf (stderr, " -s scope\tsearch scope: base, one, sub (default: sub)\n");
351 fprintf (stderr, " -t targetdn\tdn to change password\n");
352 fprintf (stderr, " -v\t\tverbose (more v's, more verbose)\n");
353 fprintf (stderr, " -W\t\tprompt for bind password\n");
354 fprintf (stderr, " -w passwd\tbind password (for simple authentication)\n");
355 fprintf (stderr, " -Y saltlen\tsalt length to use\n");
356 /* fprintf (stderr, " -y salt\tsalt to use\n"); */
357 fprintf (stderr, " -z size\tsize limit\n");
362 main (int argc, char *argv[])
367 char *filtpattern = NULL;
368 char *ldaphost = NULL;
369 char *targetdn = NULL;
370 char *pwattr = LDAP_PASSWD_ATTRIB;
372 int authmethod = LDAP_AUTH_SIMPLE;
373 int hashtype = HASHTYPE_CRYPT;
377 int scope = LDAP_SCOPE_SUBTREE;
392 while ((i = getopt (argc, argv, "a:b:C:D:d:Ee:g:H:h:Kkl:nP:p:s:t:vWw:Y:y:z:")) != EOF)
396 case 'a': /* password attribute */
397 pwattr = STRDUP (optarg);
400 case 'b': /* base search dn */
401 base = STRDUP (optarg);
408 case 'D': /* bind distinguished name */
409 binddn = STRDUP (optarg);
412 case 'd': /* debugging option */
413 debug |= atoi (optarg);
416 case 'E': /* prompt for new password */
420 case 'e': /* new password */
421 newpw = STRDUP (optarg);
425 auto_gen_pw = strtol (optarg, NULL, 10);
428 case 'H': /* hashes */
429 for (j = 0; hashes[j].name; j++)
431 if (!strncasecmp (optarg, hashes[j].name, hashes[j].namesz))
433 hashtype = hashes[j].type;
440 fprintf (stderr, "hash type: %s is unknown\n", optarg);
445 case 'h': /* ldap host */
446 ldaphost = STRDUP (optarg);
449 case 'K': /* use kerberos bind, 1st part only */
451 authmethod = LDAP_AUTH_KRBV41;
453 fprintf (stderr, "%s was not compiled with Kerberos support\n", argv[0]);
457 case 'k': /* use kerberos bind */
459 authmethod = LDAP_AUTH_KRBV4;
461 fprintf (stderr, "%s was not compiled with Kerberos support\n", argv[0]);
465 case 'l': /* time limit */
466 timelimit = strtol (optarg, NULL, 10);
469 case 'n': /* don't update entry(s) */
477 version = LDAP_VERSION2;
480 version = LDAP_VERSION3;
485 case 'p': /* ldap port */
486 ldapport = strtol (optarg, NULL, 10);
489 case 's': /* scope */
490 if (strncasecmp (optarg, "base", 4) == 0)
491 scope = LDAP_SCOPE_BASE;
492 else if (strncasecmp (optarg, "one", 3) == 0)
493 scope = LDAP_SCOPE_ONELEVEL;
494 else if (strncasecmp (optarg, "sub", 3) == 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");
564 lber_set_option( NULL, LBER_OPT_DEBUG_LEVEL, &debug );
565 ldap_set_option( NULL, LDAP_OPT_DEBUG_LEVEL, &debug );
569 (void) SIGNAL( SIGPIPE, SIG_IGN );
572 /* connect to server */
573 if ((ld = ldap_init (ldaphost, ldapport)) == NULL)
575 perror ("ldap_init");
580 if( timelimit != -1 ) {
581 ldap_set_option (ld, LDAP_OPT_TIMELIMIT, (void *)&timelimit);
583 if( sizelimit != -1 ) {
584 ldap_set_option (ld, LDAP_OPT_SIZELIMIT, (void *)&sizelimit);
587 /* this seems prudent */
589 int deref = LDAP_DEREF_NEVER;
590 ldap_set_option( ld, LDAP_OPT_DEREF, &deref);
593 if( version != -1 ) {
594 ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, &version );
597 /* authenticate to server */
598 if (ldap_bind_s (ld, binddn, bindpw, authmethod) != LDAP_SUCCESS)
600 ldap_perror (ld, "ldap_bind");
608 /* insert code here =) */
611 modify_dn (ld, targetdn, pwattr, NULL, newpw, hashtype, &salt);
617 LDAPMessage *result = NULL, *e = NULL;
618 char *attrs[3] = {"dn", NULL, NULL};
622 sprintf (filter, "%s", filtpattern);
623 i = ldap_search_s (ld, base, scope, filter, attrs, 0, &result);
624 if (i != LDAP_SUCCESS &&
625 i != LDAP_TIMELIMIT_EXCEEDED &&
626 i != LDAP_SIZELIMIT_EXCEEDED)
628 ldap_perror (ld, "ldap_search");
632 for (e = ldap_first_entry (ld, result); e; e = ldap_next_entry (ld, e))
634 char *dn = ldap_get_dn (ld, e);
637 struct berval **pw_vals = ldap_get_values_len (ld, e, pwattr);
638 modify_dn (ld, dn, pwattr, pw_vals ? pw_vals[0]->bv_val : NULL, newpw, hashtype, &salt);
640 ldap_value_free_len (pw_vals);
646 /* disconnect from server */