1998-12-06 11:44:28 +08:00
|
|
|
/*
|
1998-12-14 18:10:41 +08:00
|
|
|
* Copyright 1998, David E. Storey, All rights reserved.
|
|
|
|
* This software is not subject to any license of The Murphy Group, Inc.
|
|
|
|
* or George Mason University.
|
1998-12-06 11:44:28 +08:00
|
|
|
*
|
1998-12-14 18:10:41 +08:00
|
|
|
* Redistribution and use in source and binary forms are permitted only
|
|
|
|
* as authorized by the OpenLDAP Public License. A copy of this
|
|
|
|
* license is available at http://www.OpenLDAP.org/license.html or
|
|
|
|
* in file LICENSE in the top-level directory of the distribution.
|
1998-12-06 11:44:28 +08:00
|
|
|
*
|
1998-12-14 18:10:41 +08:00
|
|
|
* ldappasswd.c - program to modify passwords in an LDAP tree
|
1998-12-06 11:44:28 +08:00
|
|
|
*
|
1998-12-14 18:10:41 +08:00
|
|
|
* Author: David E. Storey <dave@tamos.net>
|
1998-12-06 11:44:28 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "portable.h"
|
|
|
|
|
|
|
|
#include <stdio.h>
|
1999-06-03 08:37:44 +08:00
|
|
|
|
|
|
|
#include <ac/stdlib.h>
|
1998-12-06 11:44:28 +08:00
|
|
|
|
1999-05-19 09:12:33 +08:00
|
|
|
#include <ac/ctype.h>
|
1999-01-21 06:01:14 +08:00
|
|
|
#include <ac/signal.h>
|
1999-05-19 09:12:33 +08:00
|
|
|
#include <ac/socket.h>
|
1998-12-06 11:44:28 +08:00
|
|
|
#include <ac/string.h>
|
1999-05-19 09:12:33 +08:00
|
|
|
#include <ac/time.h>
|
1998-12-06 11:44:28 +08:00
|
|
|
#include <ac/unistd.h>
|
|
|
|
|
|
|
|
#include <lber.h>
|
|
|
|
#include <ldap.h>
|
|
|
|
#include <lutil.h>
|
|
|
|
#include <lutil_md5.h>
|
|
|
|
#include <lutil_sha1.h>
|
|
|
|
|
|
|
|
#include "ldapconfig.h"
|
|
|
|
|
1998-12-15 02:36:51 +08:00
|
|
|
/* local macros */
|
1999-04-30 07:30:01 +08:00
|
|
|
#define CEILING(x) ((double)(x) > (int)(x) ? (int)(x) + 1 : (int)(x))
|
1998-12-15 02:36:51 +08:00
|
|
|
|
1998-12-06 11:44:28 +08:00
|
|
|
#define LDAP_PASSWD_ATTRIB "userPassword"
|
1999-05-30 02:53:40 +08:00
|
|
|
#define LDAP_PASSWD_CONF DEFAULT_SYSCONFDIR DIRSEP "passwd.conf"
|
1998-12-15 02:36:51 +08:00
|
|
|
|
|
|
|
#define HS_NONE 0
|
|
|
|
#define HS_PLAIN 1
|
|
|
|
#define HS_CONV 2
|
1998-12-06 11:44:28 +08:00
|
|
|
|
1998-12-15 02:36:51 +08:00
|
|
|
typedef enum
|
|
|
|
{
|
1998-12-06 11:44:28 +08:00
|
|
|
HASHTYPE_NONE,
|
|
|
|
HASHTYPE_CRYPT,
|
|
|
|
HASHTYPE_MD5,
|
1998-12-15 02:36:51 +08:00
|
|
|
HASHTYPE_SMD5,
|
|
|
|
HASHTYPE_SHA1,
|
|
|
|
HASHTYPE_SSHA1
|
|
|
|
}
|
|
|
|
HashTypes;
|
1998-12-06 11:44:28 +08:00
|
|
|
|
1998-12-15 02:36:51 +08:00
|
|
|
typedef struct salt_t
|
|
|
|
{
|
|
|
|
unsigned char *salt;
|
|
|
|
unsigned int len;
|
|
|
|
}
|
|
|
|
Salt;
|
|
|
|
|
|
|
|
typedef struct hash_t
|
|
|
|
{
|
|
|
|
char *name;
|
|
|
|
unsigned int namesz;
|
|
|
|
char *(*func) (const char *, Salt *);
|
|
|
|
unsigned char takes_salt;
|
|
|
|
HashTypes type;
|
|
|
|
HashTypes type_salted;
|
|
|
|
unsigned int default_salt_len;
|
|
|
|
}
|
|
|
|
Hash;
|
|
|
|
|
|
|
|
static int noupdates = 0;
|
|
|
|
static int verbose = 0;
|
|
|
|
static int want_entryhash = 0;
|
|
|
|
static int auto_gen_pw = 0;
|
1998-12-06 11:44:28 +08:00
|
|
|
|
|
|
|
/*** functions ***/
|
|
|
|
|
1998-12-15 02:36:51 +08:00
|
|
|
/*
|
1999-03-06 14:13:23 +08:00
|
|
|
* pw_encode() essentially base64 encodes a password and its salt
|
1998-12-15 02:36:51 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
char *
|
|
|
|
pw_encode (unsigned char *passwd, Salt * salt, unsigned int len)
|
|
|
|
{
|
|
|
|
int salted = salt && salt->salt && salt->len;
|
|
|
|
int b64_len = 0;
|
|
|
|
char *base64digest = NULL;
|
|
|
|
unsigned char *npasswd = passwd;
|
|
|
|
|
|
|
|
if (salted)
|
|
|
|
{
|
|
|
|
npasswd = (unsigned char *)malloc (len + salt->len);
|
|
|
|
memcpy (npasswd, passwd, len);
|
|
|
|
memcpy (&npasswd[len], salt->salt, salt->len);
|
|
|
|
len += salt->len;
|
|
|
|
}
|
|
|
|
|
|
|
|
b64_len = CEILING (len / 3) * 4 + 1;
|
|
|
|
base64digest = (char *)malloc (b64_len);
|
|
|
|
if (lutil_b64_ntop (npasswd, len, base64digest, b64_len) < 0)
|
|
|
|
{
|
|
|
|
free (base64digest);
|
|
|
|
base64digest = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (salted)
|
|
|
|
free (npasswd);
|
|
|
|
|
|
|
|
return (base64digest);
|
|
|
|
}
|
|
|
|
|
1998-12-06 11:44:28 +08:00
|
|
|
/*
|
|
|
|
* if you'd like to write a better salt generator, please, be my guest.
|
|
|
|
*/
|
|
|
|
|
|
|
|
void
|
1998-12-15 02:36:51 +08:00
|
|
|
make_salt (Salt * salt, unsigned int len)
|
|
|
|
{
|
|
|
|
|
|
|
|
if (!salt)
|
|
|
|
return;
|
|
|
|
|
|
|
|
salt->len = len;
|
|
|
|
salt->salt = (unsigned char *)malloc (len);
|
|
|
|
|
|
|
|
for (len = 0; len < salt->len; len++)
|
1999-05-19 09:12:33 +08:00
|
|
|
salt->salt[len] = rand () & 0xff;
|
1998-12-15 02:36:51 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* password generator
|
|
|
|
*/
|
|
|
|
|
|
|
|
char *
|
|
|
|
gen_pass (unsigned int len)
|
1998-12-06 11:44:28 +08:00
|
|
|
{
|
1999-04-05 06:02:20 +08:00
|
|
|
static const unsigned char autogen[] =
|
1998-12-15 02:36:51 +08:00
|
|
|
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890.,";
|
1999-04-27 13:19:32 +08:00
|
|
|
unsigned int i;
|
1999-01-20 09:00:12 +08:00
|
|
|
Salt salt;
|
|
|
|
|
|
|
|
salt.salt = NULL;
|
|
|
|
salt.len = 0;
|
1998-12-15 02:36:51 +08:00
|
|
|
|
|
|
|
make_salt (&salt, len);
|
|
|
|
for (i = 0; i < len; i++)
|
|
|
|
salt.salt[i] = autogen[salt.salt[i] % (sizeof (autogen) - 1)];
|
|
|
|
|
|
|
|
return ((char *)salt.salt);
|
1998-12-06 11:44:28 +08:00
|
|
|
}
|
|
|
|
|
1998-12-30 05:45:08 +08:00
|
|
|
#ifdef SLAPD_CLEARTEXT
|
1998-12-15 02:36:51 +08:00
|
|
|
char *
|
|
|
|
hash_none (const char *pw_in, Salt * salt)
|
1998-12-06 11:44:28 +08:00
|
|
|
{
|
1999-05-30 02:53:40 +08:00
|
|
|
return (strdup (pw_in));
|
1998-12-06 11:44:28 +08:00
|
|
|
}
|
1998-12-30 05:45:08 +08:00
|
|
|
#endif
|
1998-12-06 11:44:28 +08:00
|
|
|
|
1998-12-30 05:45:08 +08:00
|
|
|
#ifdef SLAPD_CRYPT
|
1998-12-15 02:36:51 +08:00
|
|
|
char *
|
|
|
|
hash_crypt (const char *pw_in, Salt * salt)
|
1998-12-12 14:12:03 +08:00
|
|
|
{
|
1999-04-05 06:02:20 +08:00
|
|
|
static const unsigned char crypt64[] =
|
1998-12-15 02:36:51 +08:00
|
|
|
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890./";
|
|
|
|
char *crypted_pw = NULL;
|
|
|
|
Salt lsalt;
|
|
|
|
|
|
|
|
if (salt && salt->salt && strlen ((char *)salt->salt) >= 2)
|
|
|
|
{
|
|
|
|
/* sanity check */
|
|
|
|
if (!(isalnum(salt->salt[0]) || salt->salt[0] == '.' || salt->salt[0] == '/'))
|
|
|
|
salt->salt[0] = crypt64[salt->salt[0] % (sizeof (crypt64) - 1)];
|
|
|
|
if (!(isalnum(salt->salt[1]) || salt->salt[1] == '.' || salt->salt[1] == '/'))
|
|
|
|
salt->salt[1] = crypt64[salt->salt[1] % (sizeof (crypt64) - 1)];
|
|
|
|
|
|
|
|
crypted_pw = crypt (pw_in, (char *)salt->salt);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
make_salt (&lsalt, 2);
|
|
|
|
lsalt.salt[0] = crypt64[lsalt.salt[0] % (sizeof (crypt64) - 1)];
|
|
|
|
lsalt.salt[1] = crypt64[lsalt.salt[1] % (sizeof (crypt64) - 1)];
|
|
|
|
crypted_pw = crypt (pw_in, (char *)lsalt.salt);
|
|
|
|
free (lsalt.salt);
|
|
|
|
}
|
1999-05-30 03:08:57 +08:00
|
|
|
return (strdup (crypted_pw));
|
1998-12-12 14:12:03 +08:00
|
|
|
}
|
1998-12-30 05:45:08 +08:00
|
|
|
#endif
|
1998-12-12 14:12:03 +08:00
|
|
|
|
1998-12-15 02:36:51 +08:00
|
|
|
char *
|
|
|
|
hash_md5 (const char *pw_in, Salt * salt)
|
1998-12-06 11:44:28 +08:00
|
|
|
{
|
1998-12-15 02:36:51 +08:00
|
|
|
lutil_MD5_CTX MD5context;
|
|
|
|
unsigned char MD5digest[16];
|
|
|
|
|
|
|
|
lutil_MD5Init (&MD5context);
|
1999-02-20 16:13:35 +08:00
|
|
|
lutil_MD5Update (&MD5context,
|
|
|
|
(const unsigned char *)pw_in, strlen(pw_in));
|
1998-12-22 22:31:31 +08:00
|
|
|
if (salt && salt->salt && salt->len)
|
|
|
|
lutil_MD5Update (&MD5context, salt->salt, salt->len);
|
1998-12-15 02:36:51 +08:00
|
|
|
lutil_MD5Final (MD5digest, &MD5context);
|
|
|
|
|
1998-12-22 22:31:31 +08:00
|
|
|
return (pw_encode (MD5digest, salt, sizeof (MD5digest)));
|
1998-12-06 11:44:28 +08:00
|
|
|
}
|
|
|
|
|
1998-12-15 02:36:51 +08:00
|
|
|
char *
|
|
|
|
hash_sha1 (const char *pw_in, Salt * salt)
|
1998-12-06 11:44:28 +08:00
|
|
|
{
|
1998-12-15 02:36:51 +08:00
|
|
|
lutil_SHA1_CTX SHA1context;
|
|
|
|
unsigned char SHA1digest[20];
|
|
|
|
|
|
|
|
lutil_SHA1Init (&SHA1context);
|
1999-02-20 16:13:35 +08:00
|
|
|
lutil_SHA1Update (&SHA1context,
|
|
|
|
(const unsigned char *)pw_in, strlen(pw_in));
|
1998-12-22 22:31:31 +08:00
|
|
|
if (salt && salt->salt && salt->len)
|
|
|
|
lutil_SHA1Update (&SHA1context, salt->salt, salt->len);
|
1998-12-15 02:36:51 +08:00
|
|
|
lutil_SHA1Final (SHA1digest, &SHA1context);
|
|
|
|
|
1998-12-22 22:31:31 +08:00
|
|
|
return (pw_encode (SHA1digest, salt, sizeof (SHA1digest)));
|
1998-12-06 11:44:28 +08:00
|
|
|
}
|
|
|
|
|
1998-12-15 02:36:51 +08:00
|
|
|
static Hash hashes[] =
|
|
|
|
{
|
1998-12-30 05:45:08 +08:00
|
|
|
#ifdef SLAPD_CLEARTEXT
|
1998-12-15 02:36:51 +08:00
|
|
|
{"none", 4, hash_none, 0, HASHTYPE_NONE, HASHTYPE_NONE, 0},
|
1998-12-30 05:45:08 +08:00
|
|
|
#endif
|
|
|
|
#ifdef SLAPD_CRYPT
|
1998-12-15 02:36:51 +08:00
|
|
|
{"crypt", 5, hash_crypt, 1, HASHTYPE_CRYPT, HASHTYPE_CRYPT, 2},
|
1998-12-30 05:45:08 +08:00
|
|
|
#endif
|
1998-12-15 02:36:51 +08:00
|
|
|
{"md5", 3, hash_md5, 0, HASHTYPE_MD5, HASHTYPE_SMD5, 0},
|
|
|
|
{"smd5", 4, hash_md5, 1, HASHTYPE_SMD5, HASHTYPE_SMD5, 4},
|
|
|
|
{"sha", 3, hash_sha1, 0, HASHTYPE_SHA1, HASHTYPE_SSHA1, 0},
|
|
|
|
{"ssha", 4, hash_sha1, 1, HASHTYPE_SSHA1, HASHTYPE_SSHA1, 4},
|
|
|
|
{NULL, 0, NULL, 0, HASHTYPE_NONE, HASHTYPE_NONE, 0}
|
1998-12-06 11:44:28 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
int
|
1998-12-15 02:36:51 +08:00
|
|
|
modify_dn (LDAP * ld, char *targetdn, char *pwattr, char *oldpw,
|
|
|
|
char *newpw, HashTypes htype, Salt * salt)
|
1998-12-06 11:44:28 +08:00
|
|
|
{
|
1998-12-15 02:36:51 +08:00
|
|
|
int ret = 0;
|
|
|
|
int salted = salt->salt ? 1 : 0;
|
|
|
|
int want_salt = salt->len && !salted;
|
|
|
|
char *buf = NULL;
|
|
|
|
char *hashed_pw = NULL;
|
1998-12-27 17:04:06 +08:00
|
|
|
char *strvals[2];
|
|
|
|
LDAPMod mod, *mods[2];
|
1998-12-06 11:44:28 +08:00
|
|
|
|
|
|
|
if (!ld || !targetdn || !newpw)
|
1998-12-15 02:36:51 +08:00
|
|
|
return (1);
|
|
|
|
|
|
|
|
/* auto-generate password */
|
|
|
|
if (auto_gen_pw)
|
|
|
|
newpw = gen_pass (auto_gen_pw);
|
|
|
|
|
|
|
|
/* handle salt */
|
|
|
|
if (want_salt)
|
|
|
|
{
|
|
|
|
make_salt (salt, salt->len);
|
|
|
|
htype = hashes[htype].type_salted;
|
|
|
|
}
|
|
|
|
else if (hashes[htype].default_salt_len)
|
|
|
|
{
|
|
|
|
/* user chose a salted hash and needs a salt */
|
|
|
|
if (!salted)
|
|
|
|
{
|
|
|
|
want_salt++;
|
|
|
|
salt->len = hashes[htype].default_salt_len;
|
|
|
|
make_salt (salt, salt->len);
|
|
|
|
}
|
|
|
|
}
|
1998-12-06 11:44:28 +08:00
|
|
|
|
|
|
|
/* hash password */
|
1998-12-15 02:36:51 +08:00
|
|
|
hashed_pw = hashes[htype].func (newpw, salt->len ? salt : NULL);
|
|
|
|
|
1999-03-06 14:13:23 +08:00
|
|
|
/* return salt back to its original state */
|
1998-12-15 02:36:51 +08:00
|
|
|
if (want_salt)
|
|
|
|
{
|
|
|
|
free (salt->salt);
|
|
|
|
salt->salt = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
buf = (char *)malloc (hashes[htype].namesz + 3 + strlen (hashed_pw));
|
|
|
|
if (htype)
|
|
|
|
sprintf (buf, "{%s}%s", hashes[htype].name, hashed_pw);
|
1998-12-06 11:44:28 +08:00
|
|
|
else
|
1998-12-15 02:36:51 +08:00
|
|
|
sprintf (buf, "%s", hashed_pw);
|
1998-12-06 11:44:28 +08:00
|
|
|
|
|
|
|
if (verbose > 0)
|
|
|
|
{
|
1998-12-15 02:36:51 +08:00
|
|
|
printf ("%s", targetdn);
|
1998-12-06 11:44:28 +08:00
|
|
|
if (verbose > 1)
|
|
|
|
{
|
1998-12-15 02:36:51 +08:00
|
|
|
printf (":%s", buf);
|
1998-12-06 11:44:28 +08:00
|
|
|
if (verbose > 2)
|
1998-12-15 02:36:51 +08:00
|
|
|
printf (":%s", newpw);
|
1998-12-06 11:44:28 +08:00
|
|
|
}
|
1998-12-15 02:36:51 +08:00
|
|
|
printf ("\n");
|
1998-12-06 11:44:28 +08:00
|
|
|
}
|
|
|
|
|
1998-12-15 02:36:51 +08:00
|
|
|
strvals[0] = buf;
|
1998-12-27 17:04:06 +08:00
|
|
|
strvals[1] = NULL;
|
1999-06-03 06:40:58 +08:00
|
|
|
mod.mod_values = strvals;
|
1998-12-06 11:44:28 +08:00
|
|
|
mod.mod_type = pwattr;
|
|
|
|
mod.mod_op = LDAP_MOD_REPLACE;
|
1998-12-27 17:04:06 +08:00
|
|
|
mods[0] = &mod;
|
|
|
|
mods[1] =NULL;
|
1998-12-06 11:44:28 +08:00
|
|
|
|
1998-12-15 02:36:51 +08:00
|
|
|
if (!noupdates && (ret = ldap_modify_s (ld, targetdn, mods)) != LDAP_SUCCESS)
|
1999-02-08 12:07:49 +08:00
|
|
|
ldap_perror (ld, "ldap_modify");
|
1998-12-15 02:36:51 +08:00
|
|
|
|
|
|
|
free (hashed_pw);
|
|
|
|
free (buf);
|
|
|
|
return (ret);
|
1998-12-06 11:44:28 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
1998-12-15 02:36:51 +08:00
|
|
|
usage (char *s)
|
1998-12-06 11:44:28 +08:00
|
|
|
{
|
1998-12-15 02:36:51 +08:00
|
|
|
fprintf (stderr, "Usage: %s [options] [filter]\n", s);
|
|
|
|
fprintf (stderr, " -a attrib\tpassword attribute (default: %s)\n", LDAP_PASSWD_ATTRIB);
|
|
|
|
fprintf (stderr, " -b basedn\tbasedn to perform searches\n");
|
|
|
|
/* fprintf (stderr, " -C\t\tuse entry's current hash mechanism\n"); */
|
|
|
|
fprintf (stderr, " -D binddn\tbind dn\n");
|
|
|
|
fprintf (stderr, " -d level\tdebugging level\n");
|
|
|
|
fprintf (stderr, " -E\t\tprompt for new password\n");
|
|
|
|
fprintf (stderr, " -e passwd\tnew password\n");
|
|
|
|
fprintf (stderr, " -g passlen\tauto-generate passwords with length pwlen\n");
|
|
|
|
fprintf (stderr, " -H hash\thash type (default: crypt)\n");
|
|
|
|
fprintf (stderr, " -h host\tldap server (default: localhost)\n");
|
|
|
|
#ifdef HAVE_KERBEROS
|
|
|
|
fprintf (stderr, " -K\t\tuse Kerberos step 1\n");
|
|
|
|
fprintf (stderr, " -k\t\tuse Kerberos\n");
|
|
|
|
#endif
|
|
|
|
fprintf (stderr, " -l time\ttime limit\n");
|
|
|
|
fprintf (stderr, " -n\t\tmake no modifications\n");
|
1998-12-27 22:08:46 +08:00
|
|
|
fprintf (stderr, " -P version\tprotocol version (2 or 3)\n");
|
1998-12-15 02:36:51 +08:00
|
|
|
fprintf (stderr, " -p port\tldap port\n");
|
|
|
|
fprintf (stderr, " -s scope\tsearch scope: base, one, sub (default: sub)\n");
|
|
|
|
fprintf (stderr, " -t targetdn\tdn to change password\n");
|
|
|
|
fprintf (stderr, " -v\t\tverbose (more v's, more verbose)\n");
|
|
|
|
fprintf (stderr, " -W\t\tprompt for bind password\n");
|
|
|
|
fprintf (stderr, " -w passwd\tbind password (for simple authentication)\n");
|
|
|
|
fprintf (stderr, " -Y saltlen\tsalt length to use\n");
|
|
|
|
/* fprintf (stderr, " -y salt\tsalt to use\n"); */
|
|
|
|
fprintf (stderr, " -z size\tsize limit\n");
|
|
|
|
exit (1);
|
1998-12-06 11:44:28 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
1998-12-15 02:36:51 +08:00
|
|
|
main (int argc, char *argv[])
|
1998-12-06 11:44:28 +08:00
|
|
|
{
|
1998-12-15 02:36:51 +08:00
|
|
|
char *base = NULL;
|
|
|
|
char *binddn = NULL;
|
|
|
|
char *bindpw = NULL;
|
|
|
|
char *filtpattern = NULL;
|
|
|
|
char *ldaphost = NULL;
|
|
|
|
char *targetdn = NULL;
|
|
|
|
char *pwattr = LDAP_PASSWD_ATTRIB;
|
|
|
|
char *newpw = NULL;
|
|
|
|
int authmethod = LDAP_AUTH_SIMPLE;
|
|
|
|
int hashtype = HASHTYPE_CRYPT;
|
|
|
|
int i, j;
|
|
|
|
int ldapport = 0;
|
1998-12-22 09:34:01 +08:00
|
|
|
int debug = 0;
|
1998-12-29 13:44:18 +08:00
|
|
|
int scope = LDAP_SCOPE_SUBTREE;
|
1998-12-29 13:33:34 +08:00
|
|
|
int sizelimit = -1;
|
|
|
|
int timelimit = -1;
|
|
|
|
int version = -1;
|
1998-12-15 02:36:51 +08:00
|
|
|
int want_bindpw = 0;
|
|
|
|
int want_newpw = 0;
|
|
|
|
LDAP *ld;
|
|
|
|
Salt salt;
|
|
|
|
|
|
|
|
salt.salt = NULL;
|
|
|
|
salt.len = 0;
|
|
|
|
|
|
|
|
if (argc == 1)
|
|
|
|
usage (argv[0]);
|
|
|
|
|
1998-12-27 22:08:46 +08:00
|
|
|
while ((i = getopt (argc, argv, "a:b:C:D:d:Ee:g:H:h:Kkl:nP:p:s:t:vWw:Y:y:z:")) != EOF)
|
1998-12-06 11:44:28 +08:00
|
|
|
{
|
1998-12-15 02:36:51 +08:00
|
|
|
switch (i)
|
1998-12-14 18:10:41 +08:00
|
|
|
{
|
1998-12-15 02:36:51 +08:00
|
|
|
case 'a': /* password attribute */
|
1999-05-30 02:53:40 +08:00
|
|
|
pwattr = strdup (optarg);
|
1998-12-15 02:36:51 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 'b': /* base search dn */
|
1999-05-30 02:53:40 +08:00
|
|
|
base = strdup (optarg);
|
1998-12-15 02:36:51 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 'C':
|
|
|
|
want_entryhash++;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'D': /* bind distinguished name */
|
1999-05-30 02:53:40 +08:00
|
|
|
binddn = strdup (optarg);
|
1998-12-15 02:36:51 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 'd': /* debugging option */
|
1998-12-22 09:34:01 +08:00
|
|
|
debug |= atoi (optarg);
|
1998-12-06 11:44:28 +08:00
|
|
|
break;
|
|
|
|
|
1998-12-15 02:36:51 +08:00
|
|
|
case 'E': /* prompt for new password */
|
|
|
|
want_newpw++;
|
1998-12-06 11:44:28 +08:00
|
|
|
break;
|
|
|
|
|
1998-12-15 02:36:51 +08:00
|
|
|
case 'e': /* new password */
|
1999-05-30 02:53:40 +08:00
|
|
|
newpw = strdup (optarg);
|
1998-12-06 11:44:28 +08:00
|
|
|
break;
|
|
|
|
|
1998-12-15 02:36:51 +08:00
|
|
|
case 'g':
|
|
|
|
auto_gen_pw = strtol (optarg, NULL, 10);
|
1998-12-12 14:12:03 +08:00
|
|
|
break;
|
|
|
|
|
1998-12-15 02:36:51 +08:00
|
|
|
case 'H': /* hashes */
|
1998-12-06 11:44:28 +08:00
|
|
|
for (j = 0; hashes[j].name; j++)
|
|
|
|
{
|
1998-12-15 02:36:51 +08:00
|
|
|
if (!strncasecmp (optarg, hashes[j].name, hashes[j].namesz))
|
1998-12-06 11:44:28 +08:00
|
|
|
{
|
|
|
|
hashtype = hashes[j].type;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!hashes[j].name)
|
|
|
|
{
|
1998-12-15 02:36:51 +08:00
|
|
|
fprintf (stderr, "hash type: %s is unknown\n", optarg);
|
|
|
|
usage (argv[0]);
|
1998-12-06 11:44:28 +08:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
1998-12-15 02:36:51 +08:00
|
|
|
case 'h': /* ldap host */
|
1999-05-30 02:53:40 +08:00
|
|
|
ldaphost = strdup (optarg);
|
1998-12-15 02:36:51 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 'K': /* use kerberos bind, 1st part only */
|
|
|
|
#ifdef HAVE_KERBEROS
|
|
|
|
authmethod = LDAP_AUTH_KRBV41;
|
1998-12-06 11:44:28 +08:00
|
|
|
#else
|
1998-12-15 02:36:51 +08:00
|
|
|
fprintf (stderr, "%s was not compiled with Kerberos support\n", argv[0]);
|
1999-05-30 02:53:40 +08:00
|
|
|
usage (argv[0]);
|
1998-12-06 11:44:28 +08:00
|
|
|
#endif
|
1998-12-14 18:10:41 +08:00
|
|
|
break;
|
1998-12-06 11:44:28 +08:00
|
|
|
|
1998-12-15 02:36:51 +08:00
|
|
|
case 'k': /* use kerberos bind */
|
|
|
|
#ifdef HAVE_KERBEROS
|
|
|
|
authmethod = LDAP_AUTH_KRBV4;
|
|
|
|
#else
|
|
|
|
fprintf (stderr, "%s was not compiled with Kerberos support\n", argv[0]);
|
1999-05-30 02:53:40 +08:00
|
|
|
usage (argv[0]);
|
1998-12-15 02:36:51 +08:00
|
|
|
#endif
|
1998-12-14 18:10:41 +08:00
|
|
|
break;
|
1998-12-06 11:44:28 +08:00
|
|
|
|
1998-12-15 02:36:51 +08:00
|
|
|
case 'l': /* time limit */
|
|
|
|
timelimit = strtol (optarg, NULL, 10);
|
1998-12-06 11:44:28 +08:00
|
|
|
break;
|
|
|
|
|
1998-12-15 02:36:51 +08:00
|
|
|
case 'n': /* don't update entry(s) */
|
1998-12-06 11:44:28 +08:00
|
|
|
noupdates++;
|
|
|
|
break;
|
|
|
|
|
1998-12-27 22:08:46 +08:00
|
|
|
case 'P':
|
1999-05-30 02:53:40 +08:00
|
|
|
switch( atoi( optarg ) ) {
|
|
|
|
case 2:
|
1998-12-27 22:08:46 +08:00
|
|
|
version = LDAP_VERSION2;
|
|
|
|
break;
|
1999-05-30 02:53:40 +08:00
|
|
|
case 3:
|
1998-12-27 22:08:46 +08:00
|
|
|
version = LDAP_VERSION3;
|
|
|
|
break;
|
1999-05-30 02:53:40 +08:00
|
|
|
default:
|
|
|
|
fprintf( stderr, "protocol version should be 2 or 3\n" );
|
|
|
|
usage( argv[0] );
|
1998-12-27 22:08:46 +08:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
1998-12-15 02:36:51 +08:00
|
|
|
case 'p': /* ldap port */
|
|
|
|
ldapport = strtol (optarg, NULL, 10);
|
1998-12-06 11:44:28 +08:00
|
|
|
break;
|
|
|
|
|
1998-12-15 02:36:51 +08:00
|
|
|
case 's': /* scope */
|
1999-05-30 02:53:40 +08:00
|
|
|
if (strcasecmp (optarg, "base") == 0)
|
1998-12-06 11:44:28 +08:00
|
|
|
scope = LDAP_SCOPE_BASE;
|
1999-05-30 02:53:40 +08:00
|
|
|
else if (strcasecmp (optarg, "one") == 0)
|
1998-12-06 11:44:28 +08:00
|
|
|
scope = LDAP_SCOPE_ONELEVEL;
|
1999-05-30 02:53:40 +08:00
|
|
|
else if (strcasecmp (optarg, "sub") == 0)
|
1998-12-06 11:44:28 +08:00
|
|
|
scope = LDAP_SCOPE_SUBTREE;
|
1998-12-15 02:36:51 +08:00
|
|
|
else
|
|
|
|
{
|
|
|
|
fprintf (stderr, "scope should be base, one, or sub\n");
|
|
|
|
usage (argv[0]);
|
1998-12-06 11:44:28 +08:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
1998-12-15 02:36:51 +08:00
|
|
|
case 't': /* target dn */
|
1999-05-30 02:53:40 +08:00
|
|
|
targetdn = strdup (optarg);
|
1998-12-06 11:44:28 +08:00
|
|
|
break;
|
|
|
|
|
1998-12-15 02:36:51 +08:00
|
|
|
case 'v': /* verbose */
|
1998-12-06 11:44:28 +08:00
|
|
|
verbose++;
|
|
|
|
break;
|
|
|
|
|
1998-12-15 02:36:51 +08:00
|
|
|
case 'W': /* promt for bind password */
|
|
|
|
want_bindpw++;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'w': /* bind password */
|
1999-05-30 02:53:40 +08:00
|
|
|
bindpw = strdup (optarg);
|
1998-12-15 02:36:51 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 'Y': /* salt length */
|
|
|
|
salt.len = strtol (optarg, NULL, 10);
|
|
|
|
break;
|
1998-12-06 11:44:28 +08:00
|
|
|
|
1998-12-15 02:36:51 +08:00
|
|
|
case 'y': /* user specified salt */
|
|
|
|
salt.len = strlen (optarg);
|
1999-05-30 02:53:40 +08:00
|
|
|
salt.salt = (unsigned char *)strdup (optarg);
|
1998-12-15 02:36:51 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 'z': /* time limit */
|
|
|
|
sizelimit = strtol (optarg, NULL, 10);
|
1998-12-06 11:44:28 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
1998-12-15 02:36:51 +08:00
|
|
|
usage (argv[0]);
|
1998-12-06 11:44:28 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1998-12-07 04:07:29 +08:00
|
|
|
/* grab filter */
|
1998-12-06 11:44:28 +08:00
|
|
|
if (!(argc - optind < 1))
|
1999-05-30 02:53:40 +08:00
|
|
|
filtpattern = strdup (argv[optind]);
|
1998-12-06 11:44:28 +08:00
|
|
|
|
1998-12-07 04:07:29 +08:00
|
|
|
/* check for target(s) */
|
1998-12-06 11:44:28 +08:00
|
|
|
if (!filtpattern && !targetdn)
|
1998-12-07 04:07:29 +08:00
|
|
|
targetdn = binddn;
|
1998-12-06 11:44:28 +08:00
|
|
|
|
1998-12-07 04:07:29 +08:00
|
|
|
/* handle bind password */
|
|
|
|
if (want_bindpw)
|
1998-12-15 02:36:51 +08:00
|
|
|
bindpw = strdup (getpass ("Enter LDAP password: "));
|
1998-12-07 04:07:29 +08:00
|
|
|
|
|
|
|
/* handle new password */
|
1998-12-06 11:44:28 +08:00
|
|
|
if (!newpw)
|
|
|
|
{
|
1998-12-07 04:07:29 +08:00
|
|
|
char *cknewpw;
|
1998-12-15 02:36:51 +08:00
|
|
|
newpw = strdup (getpass ("New password: "));
|
|
|
|
cknewpw = getpass ("Re-enter new password: ");
|
1998-12-07 04:07:29 +08:00
|
|
|
|
1998-12-15 02:36:51 +08:00
|
|
|
if (strncmp (newpw, cknewpw, strlen (newpw)))
|
1998-12-07 04:07:29 +08:00
|
|
|
{
|
1998-12-15 02:36:51 +08:00
|
|
|
fprintf (stderr, "passwords do not match\n");
|
1999-05-30 02:53:40 +08:00
|
|
|
return ( EXIT_FAILURE );
|
1998-12-07 04:07:29 +08:00
|
|
|
}
|
1998-12-06 11:44:28 +08:00
|
|
|
}
|
|
|
|
|
1998-12-22 09:34:01 +08:00
|
|
|
if ( debug ) {
|
1999-05-30 02:53:40 +08:00
|
|
|
if( ber_set_option( NULL, LBER_OPT_DEBUG_LEVEL, &debug ) != LBER_OPT_ERROR ) {
|
|
|
|
fprintf( stderr, "Could not set LBER_OPT_DEBUG_LEVEL %d\n", debug );
|
|
|
|
}
|
|
|
|
if( ldap_set_option( NULL, LDAP_OPT_DEBUG_LEVEL, &debug ) != LDAP_OPT_ERROR ) {
|
|
|
|
fprintf( stderr, "Could not set LDAP_OPT_DEBUG_LEVEL %d\n", debug );
|
|
|
|
}
|
1998-12-22 09:34:01 +08:00
|
|
|
}
|
|
|
|
|
1999-01-21 06:01:14 +08:00
|
|
|
#ifdef SIGPIPE
|
|
|
|
(void) SIGNAL( SIGPIPE, SIG_IGN );
|
|
|
|
#endif
|
1999-05-19 09:12:33 +08:00
|
|
|
/* seed random number generator */
|
|
|
|
|
|
|
|
#ifdef HAVE_GETTIMEOFDAY
|
|
|
|
/* this is of questionable value
|
|
|
|
* gettimeofday not provide much usec
|
|
|
|
*/
|
1999-05-19 15:14:27 +08:00
|
|
|
{
|
|
|
|
struct timeval tv;
|
|
|
|
gettimeofday (&tv, NULL);
|
|
|
|
srand (tv.tv_usec);
|
|
|
|
}
|
1999-05-19 09:12:33 +08:00
|
|
|
#else
|
|
|
|
/* The traditional seed */
|
|
|
|
srand((unsigned)time( NULL ));
|
|
|
|
#endif
|
1999-01-21 06:01:14 +08:00
|
|
|
|
1998-12-06 11:44:28 +08:00
|
|
|
/* connect to server */
|
1999-01-31 09:09:00 +08:00
|
|
|
if ((ld = ldap_init (ldaphost, ldapport)) == NULL)
|
1998-12-06 11:44:28 +08:00
|
|
|
{
|
1999-01-31 09:09:00 +08:00
|
|
|
perror ("ldap_init");
|
1999-05-30 02:53:40 +08:00
|
|
|
return ( EXIT_FAILURE );
|
1998-12-06 11:44:28 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* set options */
|
1999-05-30 02:53:40 +08:00
|
|
|
if (timelimit != -1 &&
|
|
|
|
ldap_set_option( ld, LDAP_OPT_TIMELIMIT, (void *) &timelimit ) == LDAP_OPT_ERROR )
|
|
|
|
{
|
|
|
|
fprintf( stderr, "Could not set LDAP_OPT_TIMELIMIT %d\n", timelimit );
|
1998-12-29 13:33:34 +08:00
|
|
|
}
|
1999-05-30 02:53:40 +08:00
|
|
|
if (sizelimit != -1 &&
|
|
|
|
ldap_set_option( ld, LDAP_OPT_SIZELIMIT, (void *) &sizelimit ) == LDAP_OPT_ERROR )
|
|
|
|
{
|
|
|
|
fprintf( stderr, "Could not set LDAP_OPT_SIZELIMIT %d\n", sizelimit );
|
1998-12-29 13:33:34 +08:00
|
|
|
}
|
1998-12-06 11:44:28 +08:00
|
|
|
|
1998-12-22 09:34:01 +08:00
|
|
|
/* this seems prudent */
|
|
|
|
{
|
|
|
|
int deref = LDAP_DEREF_NEVER;
|
|
|
|
ldap_set_option( ld, LDAP_OPT_DEREF, &deref);
|
|
|
|
}
|
|
|
|
|
1999-05-30 02:53:40 +08:00
|
|
|
if (version != -1 &&
|
|
|
|
ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, &version ) == LDAP_OPT_ERROR)
|
|
|
|
{
|
|
|
|
fprintf( stderr, "Could not set LDAP_OPT_PROTOCOL_VERSION %d\n", version );
|
1998-12-29 13:33:34 +08:00
|
|
|
}
|
1998-12-22 09:34:01 +08:00
|
|
|
|
1998-12-14 18:10:41 +08:00
|
|
|
/* authenticate to server */
|
1998-12-15 02:36:51 +08:00
|
|
|
if (ldap_bind_s (ld, binddn, bindpw, authmethod) != LDAP_SUCCESS)
|
|
|
|
{
|
|
|
|
ldap_perror (ld, "ldap_bind");
|
1999-05-30 02:53:40 +08:00
|
|
|
return ( EXIT_FAILURE );
|
1998-12-15 02:36:51 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (targetdn)
|
1998-12-06 11:44:28 +08:00
|
|
|
{
|
1998-12-15 02:36:51 +08:00
|
|
|
if (want_entryhash)
|
|
|
|
{
|
|
|
|
/* insert code here =) */
|
|
|
|
}
|
|
|
|
else
|
|
|
|
modify_dn (ld, targetdn, pwattr, NULL, newpw, hashtype, &salt);
|
1998-12-06 11:44:28 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (filtpattern)
|
|
|
|
{
|
1998-12-15 02:36:51 +08:00
|
|
|
char filter[BUFSIZ];
|
1999-04-05 06:02:20 +08:00
|
|
|
LDAPMessage *result = NULL, *e;
|
|
|
|
char *attrs[3];
|
|
|
|
attrs[0] = "dn";
|
1998-12-15 02:36:51 +08:00
|
|
|
attrs[1] = pwattr;
|
1999-04-05 06:02:20 +08:00
|
|
|
attrs[2] = NULL;
|
1998-12-06 11:44:28 +08:00
|
|
|
|
|
|
|
/* search */
|
1998-12-15 02:36:51 +08:00
|
|
|
sprintf (filter, "%s", filtpattern);
|
|
|
|
i = ldap_search_s (ld, base, scope, filter, attrs, 0, &result);
|
|
|
|
if (i != LDAP_SUCCESS &&
|
|
|
|
i != LDAP_TIMELIMIT_EXCEEDED &&
|
|
|
|
i != LDAP_SIZELIMIT_EXCEEDED)
|
1998-12-06 11:44:28 +08:00
|
|
|
{
|
1999-02-08 12:07:49 +08:00
|
|
|
ldap_perror (ld, "ldap_search");
|
1999-05-30 02:53:40 +08:00
|
|
|
return ( EXIT_FAILURE );
|
1998-12-06 11:44:28 +08:00
|
|
|
}
|
|
|
|
|
1998-12-15 02:36:51 +08:00
|
|
|
for (e = ldap_first_entry (ld, result); e; e = ldap_next_entry (ld, e))
|
1998-12-06 11:44:28 +08:00
|
|
|
{
|
1998-12-15 02:36:51 +08:00
|
|
|
char *dn = ldap_get_dn (ld, e);
|
1998-12-06 11:44:28 +08:00
|
|
|
if (dn)
|
|
|
|
{
|
1998-12-15 02:36:51 +08:00
|
|
|
struct berval **pw_vals = ldap_get_values_len (ld, e, pwattr);
|
|
|
|
modify_dn (ld, dn, pwattr, pw_vals ? pw_vals[0]->bv_val : NULL, newpw, hashtype, &salt);
|
|
|
|
if (pw_vals)
|
|
|
|
ldap_value_free_len (pw_vals);
|
|
|
|
free (dn);
|
1998-12-06 11:44:28 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* disconnect from server */
|
1998-12-15 02:36:51 +08:00
|
|
|
ldap_unbind (ld);
|
|
|
|
|
1999-05-30 02:53:40 +08:00
|
|
|
return ( EXIT_SUCCESS );
|
1998-12-06 11:44:28 +08:00
|
|
|
}
|