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/string.h>
24 #include <ac/unistd.h>
29 #include <lutil_md5.h>
30 #include <lutil_sha1.h>
32 #include "ldapconfig.h"
35 #define CEILING(x) ((double)x > (int)x ? (int)x + 1 : (int)x)
36 #define STRDUP(x) (x ? strcpy(malloc(strlen(x) + 1), x) : NULL)
38 #define LDAP_PASSWD_ATTRIB "userPassword"
39 #define LDAP_PASSWD_CONF DEFAULT_SYSCONFDIR"/passwd.conf"
67 char *(*func) (const char *, Salt *);
68 unsigned char takes_salt;
70 HashTypes type_salted;
71 unsigned int default_salt_len;
75 static int noupdates = 0;
76 static int verbose = 0;
77 static int want_entryhash = 0;
78 static int auto_gen_pw = 0;
83 * pw_encode() essentially base64 encodes a password and it's salt
87 pw_encode (unsigned char *passwd, Salt * salt, unsigned int len)
89 int salted = salt && salt->salt && salt->len;
91 char *base64digest = NULL;
92 unsigned char *npasswd = passwd;
96 npasswd = (unsigned char *)malloc (len + salt->len);
97 memcpy (npasswd, passwd, len);
98 memcpy (&npasswd[len], salt->salt, salt->len);
102 b64_len = CEILING (len / 3) * 4 + 1;
103 base64digest = (char *)malloc (b64_len);
104 if (lutil_b64_ntop (npasswd, len, base64digest, b64_len) < 0)
113 return (base64digest);
117 * if you'd like to write a better salt generator, please, be my guest.
121 make_salt (Salt * salt, unsigned int len)
128 /* seed random number generator */
129 gettimeofday (&tv, NULL);
133 salt->salt = (unsigned char *)malloc (len);
135 for (len = 0; len < salt->len; len++)
136 salt->salt[len] = (tv.tv_usec ^ rand ()) & 0xff;
144 gen_pass (unsigned int len)
146 const unsigned char autogen[] =
147 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890.,";
149 Salt salt = {NULL, 0};
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 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, pw_in, strlen(pw_in));
205 if (salt && salt->salt && salt->len)
206 lutil_MD5Update (&MD5context, salt->salt, salt->len);
207 lutil_MD5Final (MD5digest, &MD5context);
209 return (pw_encode (MD5digest, salt, sizeof (MD5digest)));
213 hash_sha1 (const char *pw_in, Salt * salt)
215 lutil_SHA1_CTX SHA1context;
216 unsigned char SHA1digest[20];
218 lutil_SHA1Init (&SHA1context);
219 lutil_SHA1Update (&SHA1context, pw_in, strlen(pw_in));
220 if (salt && salt->salt && salt->len)
221 lutil_SHA1Update (&SHA1context, salt->salt, salt->len);
222 lutil_SHA1Final (SHA1digest, &SHA1context);
224 return (pw_encode (SHA1digest, salt, sizeof (SHA1digest)));
227 static Hash hashes[] =
229 #ifdef SLAPD_CLEARTEXT
230 {"none", 4, hash_none, 0, HASHTYPE_NONE, HASHTYPE_NONE, 0},
233 {"crypt", 5, hash_crypt, 1, HASHTYPE_CRYPT, HASHTYPE_CRYPT, 2},
235 {"md5", 3, hash_md5, 0, HASHTYPE_MD5, HASHTYPE_SMD5, 0},
236 {"smd5", 4, hash_md5, 1, HASHTYPE_SMD5, HASHTYPE_SMD5, 4},
237 {"sha", 3, hash_sha1, 0, HASHTYPE_SHA1, HASHTYPE_SSHA1, 0},
238 {"ssha", 4, hash_sha1, 1, HASHTYPE_SSHA1, HASHTYPE_SSHA1, 4},
239 {NULL, 0, NULL, 0, HASHTYPE_NONE, HASHTYPE_NONE, 0}
243 modify_dn (LDAP * ld, char *targetdn, char *pwattr, char *oldpw,
244 char *newpw, HashTypes htype, Salt * salt)
247 int salted = salt->salt ? 1 : 0;
248 int want_salt = salt->len && !salted;
250 char *hashed_pw = NULL;
252 LDAPMod mod, *mods[2];
254 if (!ld || !targetdn || !newpw)
257 /* auto-generate password */
259 newpw = gen_pass (auto_gen_pw);
264 make_salt (salt, salt->len);
265 htype = hashes[htype].type_salted;
267 else if (hashes[htype].default_salt_len)
269 /* user chose a salted hash and needs a salt */
273 salt->len = hashes[htype].default_salt_len;
274 make_salt (salt, salt->len);
279 hashed_pw = hashes[htype].func (newpw, salt->len ? salt : NULL);
281 /* return salt back to it's original state */
288 buf = (char *)malloc (hashes[htype].namesz + 3 + strlen (hashed_pw));
290 sprintf (buf, "{%s}%s", hashes[htype].name, hashed_pw);
292 sprintf (buf, "%s", hashed_pw);
296 printf ("%s", targetdn);
301 printf (":%s", newpw);
308 mod.mod_vals.modv_strvals = strvals;
309 mod.mod_type = pwattr;
310 mod.mod_op = LDAP_MOD_REPLACE;
314 if (!noupdates && (ret = ldap_modify_s (ld, targetdn, mods)) != LDAP_SUCCESS)
315 ldap_perror (ld, "ldap_modify_s");
325 fprintf (stderr, "Usage: %s [options] [filter]\n", s);
326 fprintf (stderr, " -a attrib\tpassword attribute (default: %s)\n", LDAP_PASSWD_ATTRIB);
327 fprintf (stderr, " -b basedn\tbasedn to perform searches\n");
328 /* fprintf (stderr, " -C\t\tuse entry's current hash mechanism\n"); */
329 fprintf (stderr, " -D binddn\tbind dn\n");
330 fprintf (stderr, " -d level\tdebugging level\n");
331 fprintf (stderr, " -E\t\tprompt for new password\n");
332 fprintf (stderr, " -e passwd\tnew password\n");
333 fprintf (stderr, " -g passlen\tauto-generate passwords with length pwlen\n");
334 fprintf (stderr, " -H hash\thash type (default: crypt)\n");
335 fprintf (stderr, " -h host\tldap server (default: localhost)\n");
337 fprintf (stderr, " -K\t\tuse Kerberos step 1\n");
338 fprintf (stderr, " -k\t\tuse Kerberos\n");
340 fprintf (stderr, " -l time\ttime limit\n");
341 fprintf (stderr, " -n\t\tmake no modifications\n");
342 fprintf (stderr, " -P version\tprotocol version (2 or 3)\n");
343 fprintf (stderr, " -p port\tldap port\n");
344 fprintf (stderr, " -s scope\tsearch scope: base, one, sub (default: sub)\n");
345 fprintf (stderr, " -t targetdn\tdn to change password\n");
346 fprintf (stderr, " -v\t\tverbose (more v's, more verbose)\n");
347 fprintf (stderr, " -W\t\tprompt for bind password\n");
348 fprintf (stderr, " -w passwd\tbind password (for simple authentication)\n");
349 fprintf (stderr, " -Y saltlen\tsalt length to use\n");
350 /* fprintf (stderr, " -y salt\tsalt to use\n"); */
351 fprintf (stderr, " -z size\tsize limit\n");
356 main (int argc, char *argv[])
361 char *filtpattern = NULL;
362 char *ldaphost = NULL;
363 char *targetdn = NULL;
364 char *pwattr = LDAP_PASSWD_ATTRIB;
366 int authmethod = LDAP_AUTH_SIMPLE;
367 int hashtype = HASHTYPE_CRYPT;
371 int scope = LDAP_SCOPE_SUBTREE;
386 while ((i = getopt (argc, argv, "a:b:C:D:d:Ee:g:H:h:Kkl:nP:p:s:t:vWw:Y:y:z:")) != EOF)
390 case 'a': /* password attribute */
391 pwattr = STRDUP (optarg);
394 case 'b': /* base search dn */
395 base = STRDUP (optarg);
402 case 'D': /* bind distinguished name */
403 binddn = STRDUP (optarg);
406 case 'd': /* debugging option */
407 debug |= atoi (optarg);
410 case 'E': /* prompt for new password */
414 case 'e': /* new password */
415 newpw = STRDUP (optarg);
419 auto_gen_pw = strtol (optarg, NULL, 10);
422 case 'H': /* hashes */
423 for (j = 0; hashes[j].name; j++)
425 if (!strncasecmp (optarg, hashes[j].name, hashes[j].namesz))
427 hashtype = hashes[j].type;
434 fprintf (stderr, "hash type: %s is unknown\n", optarg);
439 case 'h': /* ldap host */
440 ldaphost = STRDUP (optarg);
443 case 'K': /* use kerberos bind, 1st part only */
445 authmethod = LDAP_AUTH_KRBV41;
447 fprintf (stderr, "%s was not compiled with Kerberos support\n", argv[0]);
451 case 'k': /* use kerberos bind */
453 authmethod = LDAP_AUTH_KRBV4;
455 fprintf (stderr, "%s was not compiled with Kerberos support\n", argv[0]);
459 case 'l': /* time limit */
460 timelimit = strtol (optarg, NULL, 10);
463 case 'n': /* don't update entry(s) */
471 version = LDAP_VERSION2;
474 version = LDAP_VERSION3;
479 case 'p': /* ldap port */
480 ldapport = strtol (optarg, NULL, 10);
483 case 's': /* scope */
484 if (strncasecmp (optarg, "base", 4) == 0)
485 scope = LDAP_SCOPE_BASE;
486 else if (strncasecmp (optarg, "one", 3) == 0)
487 scope = LDAP_SCOPE_ONELEVEL;
488 else if (strncasecmp (optarg, "sub", 3) == 0)
489 scope = LDAP_SCOPE_SUBTREE;
492 fprintf (stderr, "scope should be base, one, or sub\n");
497 case 't': /* target dn */
498 targetdn = STRDUP (optarg);
501 case 'v': /* verbose */
505 case 'W': /* promt for bind password */
509 case 'w': /* bind password */
510 bindpw = STRDUP (optarg);
513 case 'Y': /* salt length */
514 salt.len = strtol (optarg, NULL, 10);
517 case 'y': /* user specified salt */
518 salt.len = strlen (optarg);
519 salt.salt = (unsigned char *)STRDUP (optarg);
522 case 'z': /* time limit */
523 sizelimit = strtol (optarg, NULL, 10);
532 if (!(argc - optind < 1))
533 filtpattern = STRDUP (argv[optind]);
535 /* check for target(s) */
536 if (!filtpattern && !targetdn)
539 /* handle bind password */
541 bindpw = strdup (getpass ("Enter LDAP password: "));
543 /* handle new password */
547 newpw = strdup (getpass ("New password: "));
548 cknewpw = getpass ("Re-enter new password: ");
550 if (strncmp (newpw, cknewpw, strlen (newpw)))
552 fprintf (stderr, "passwords do not match\n");
558 lber_set_option( NULL, LBER_OPT_DEBUG_LEVEL, &debug );
559 ldap_set_option( NULL, LDAP_OPT_DEBUG_LEVEL, &debug );
562 /* connect to server */
563 if ((ld = ldap_open (ldaphost, ldapport)) == NULL)
570 if( timelimit != -1 ) {
571 ldap_set_option (ld, LDAP_OPT_TIMELIMIT, (void *)&timelimit);
573 if( sizelimit != -1 ) {
574 ldap_set_option (ld, LDAP_OPT_SIZELIMIT, (void *)&sizelimit);
577 /* this seems prudent */
579 int deref = LDAP_DEREF_NEVER;
580 ldap_set_option( ld, LDAP_OPT_DEREF, &deref);
583 if( version != -1 ) {
584 ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, &version );
587 /* authenticate to server */
588 if (ldap_bind_s (ld, binddn, bindpw, authmethod) != LDAP_SUCCESS)
590 ldap_perror (ld, "ldap_bind");
598 /* insert code here =) */
601 modify_dn (ld, targetdn, pwattr, NULL, newpw, hashtype, &salt);
607 LDAPMessage *result = NULL, *e = NULL;
608 char *attrs[3] = {"dn", NULL, NULL};
612 sprintf (filter, "%s", filtpattern);
613 i = ldap_search_s (ld, base, scope, filter, attrs, 0, &result);
614 if (i != LDAP_SUCCESS &&
615 i != LDAP_TIMELIMIT_EXCEEDED &&
616 i != LDAP_SIZELIMIT_EXCEEDED)
618 ldap_perror (ld, "ldap_search_s");
622 for (e = ldap_first_entry (ld, result); e; e = ldap_next_entry (ld, e))
624 char *dn = ldap_get_dn (ld, e);
627 struct berval **pw_vals = ldap_get_values_len (ld, e, pwattr);
628 modify_dn (ld, dn, pwattr, pw_vals ? pw_vals[0]->bv_val : NULL, newpw, hashtype, &salt);
630 ldap_value_free_len (pw_vals);
636 /* disconnect from server */