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
14 * Author: David E. Storey <dave@tamos.net>
15 * Last Modified: 1998-12-05
17 * ToDo: passwd style change of password (termcap?)
18 * option for referral handling
20 * kerberos support? (is this really necessary?)
21 * update "shadow" fields?
22 * create/view/change password policies?
24 * Note: I am totally FOR comments and suggestions!
33 #include <ac/string.h>
34 #include <ac/unistd.h>
39 #include <lutil_md5.h>
40 #include <lutil_sha1.h>
42 #include "ldapconfig.h"
44 #define LDAP_PASSWD_ATTRIB "userPassword"
56 int (*func)(const char *, char *);
60 const char crypt64[] =
61 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789./";
65 char *ldaphost = "localhost";
66 char *pwattr = LDAP_PASSWD_ATTRIB;
67 char *targetdn = NULL;
68 char *filtpattern = NULL;
69 int ldapport = LDAP_PORT;
72 int hashtype = HASHTYPE_CRYPT;
73 int scope = LDAP_SCOPE_SUBTREE;
78 * if you'd like to write a better salt generator, please, be my guest.
79 * I just needed *something*. It's actually halfway effective for small,
80 * two character salts and it can come up with sequentially different
85 crypt_make_salt(char *salt)
91 /* grab current time */
92 gettimeofday(&tv, (struct timezone *) 0);
93 i += tv.tv_usec + (int)&salt;
94 strncpy(t_salt, (char *)&i, sizeof(i));
96 for (i = 0; i < sizeof(i); i++)
97 salt[i] = crypt64[t_salt[i] % (sizeof(crypt64) - 1)];
102 hash_none(const char *pw_in, char *pw_out)
104 strcpy(pw_out, pw_in);
109 hash_crypt(const char *pw_in, char *pw_out)
112 crypt_make_salt(salt);
113 strcpy(pw_out, crypt(pw_in, salt));
118 hash_md5(const char *pw_in, char *pw_out)
120 lutil_MD5_CTX MD5context;
121 unsigned char MD5digest[16];
122 char base64digest[25]; /* ceiling(sizeof(input)/3) * 4 + 1 */
124 lutil_MD5Init(&MD5context);
125 lutil_MD5Update(&MD5context, (unsigned char *)pw_in, strlen(pw_in));
126 lutil_MD5Final(MD5digest, &MD5context);
127 if (lutil_b64_ntop(MD5digest, sizeof(MD5digest), base64digest, sizeof(base64digest)) < 0)
130 strcpy(pw_out, base64digest);
135 hash_sha1(const char *pw_in, char *pw_out)
137 lutil_SHA1_CTX SHA1context;
138 unsigned char SHA1digest[20];
139 char base64digest[29]; /* ceiling(sizeof(input)/3) * 4 + 1 */
141 lutil_SHA1Init(&SHA1context);
142 lutil_SHA1Update(&SHA1context, (unsigned char *)pw_in, strlen(pw_in));
143 lutil_SHA1Final(SHA1digest, &SHA1context);
144 if (lutil_b64_ntop(SHA1digest, sizeof(SHA1digest), base64digest, sizeof(base64digest)) < 0)
147 strcpy(pw_out, base64digest);
151 static struct hash_t hashes[] = {
152 {"none", 4, hash_none, HASHTYPE_NONE},
153 {"crypt", 5, hash_crypt, HASHTYPE_CRYPT},
154 {"md5", 3, hash_md5, HASHTYPE_MD5},
155 {"sha", 3, hash_sha1, HASHTYPE_SHA1},
156 {NULL, 0, NULL, HASHTYPE_NONE}
160 modify_dn(LDAP *ld, char *targetdn, char *newpw)
163 char hashed_pw[128] = {'\0'};
164 char buf[128] = {'\0'};
165 char *strvals[2] = {buf, NULL};
166 LDAPMod mod, *mods[2] = {&mod, NULL};
168 if (!ld || !targetdn || !newpw)
172 hashes[hashtype].func(newpw, hashed_pw);
174 sprintf(buf, "{%s}%s", hashes[hashtype].name, hashed_pw);
176 sprintf(buf, "%s", hashed_pw);
180 printf("%s", targetdn);
185 printf(":%s", newpw);
190 mod.mod_vals.modv_strvals = strvals;
191 mod.mod_type = pwattr;
192 mod.mod_op = LDAP_MOD_REPLACE;
194 if (!noupdates && (ret = ldap_modify_s(ld, targetdn, mods)) != LDAP_SUCCESS)
195 ldap_perror(ld, "ldap_modify_s");
202 fprintf(stderr, "usage: %s [options] [filter]\n", s);
203 fprintf(stderr, "\t-a attrib password attribute (default: userPassword)\n");
204 fprintf(stderr, "\t-b basedn basedn to perform searches\n");
205 fprintf(stderr, "\t-c hash hash type: none, crypt, md5, sha (default: crypt)\n");
206 fprintf(stderr, "\t-D binddn bind dn\n");
207 fprintf(stderr, "\t-d level debugging level\n");
208 fprintf(stderr, "\t-h host ldap server (default: localhost)\n");
209 fprintf(stderr, "\t-l time time limit\n");
210 fprintf(stderr, "\t-n make no modifications\n");
211 fprintf(stderr, "\t-p port ldap port\n");
212 fprintf(stderr, "\t-s scope search scope: base, one, sub (default: sub)\n");
213 fprintf(stderr, "\t-t targetdn dn to change password\n");
214 fprintf(stderr, "\t-W newpass new password\n");
215 fprintf(stderr, "\t-w passwd bind password (for simple authentication)\n");
216 fprintf(stderr, "\t-v verbose\n");
217 fprintf(stderr, "\t-z size size limit\n");
222 main(int argc, char *argv[])
226 int sizelimit = LDAP_NO_LIMIT;
227 int timelimit = LDAP_NO_LIMIT;
230 while ((i = getopt(argc, argv, "D:W:a:b:c:d:h:l:np:s:t:vw:z:")) != EOF)
234 case 'D': /* bind distinguished name */
235 binddn = strdup(optarg);
238 case 'W': /* new password */
240 newpw = strdup(optarg);
243 case 'a': /* password attribute */
245 pwattr = strdup(optarg);
248 case 'b': /* base search dn */
250 base = strdup(optarg);
253 case 'c': /* hashes */
254 for (j = 0; hashes[j].name; j++)
256 if (!strncasecmp(optarg, hashes[j].name, hashes[j].namesz))
258 hashtype = hashes[j].type;
265 fprintf(stderr, "hash type: %s is unknown\n", optarg);
270 case 'd': /* debugging option */
272 ldap_debug = lber_debug = atoi(optarg); /* */
274 fprintf(stderr, "compile with -DLDAP_DEBUG for debugging\n");
278 case 'h': /* ldap host */
280 ldaphost = strdup(optarg);
283 case 'l': /* time limit */
285 timelimit = strtol(optarg, NULL, 10);
288 case 'n': /* don't update entry(s) */
292 case 'p': /* ldap port */
294 ldapport = strtol(optarg, NULL, 10);
297 case 's': /* scope */
298 if (strncasecmp(optarg, "base", 4) == 0)
299 scope = LDAP_SCOPE_BASE;
300 else if (strncasecmp(optarg, "one", 3) == 0)
301 scope = LDAP_SCOPE_ONELEVEL;
302 else if (strncasecmp(optarg, "sub", 3) == 0)
303 scope = LDAP_SCOPE_SUBTREE;
305 fprintf(stderr, "scope should be base, one, or sub\n" );
310 case 't': /* password type */
312 targetdn = strdup(optarg);
317 case 'v': /* verbose */
321 case 'w': /* bind password */
322 bindpw = strdup(optarg);
325 case 'z': /* time limit */
327 sizelimit = strtol(optarg, NULL, 10);
335 if (!(argc - optind < 1))
336 filtpattern = strdup(argv[optind]);
338 if (!filtpattern && !targetdn)
340 fprintf(stderr, "No filter or targetdn(-t)\n");
346 fprintf(stderr, "Need a password (-W)\n");
350 /* connect to server */
351 if ((ld = ldap_open(ldaphost, ldapport)) == NULL)
358 ldap_set_option(ld, LDAP_OPT_TIMELIMIT, (void *)&timelimit);
359 ldap_set_option(ld, LDAP_OPT_SIZELIMIT, (void *)&sizelimit);
361 /* authenticate to server */
362 if (ldap_bind_s(ld, binddn, bindpw, LDAP_AUTH_SIMPLE) != LDAP_SUCCESS)
364 ldap_perror(ld, "ldap_bind");
371 LDAPMessage *result = NULL, *e = NULL;
372 char *attrs[] = {"dn", NULL};
375 sprintf(filter, "%s", filtpattern);
376 i = ldap_search_s(ld, base, scope, filter, attrs, 1, &result);
377 if (i != LDAP_SUCCESS && i != LDAP_TIMELIMIT_EXCEEDED && i != LDAP_SIZELIMIT_EXCEEDED)
379 ldap_perror(ld, "ldap_search_s");
383 for (e = ldap_first_entry(ld, result); e; e = ldap_next_entry(ld, e))
385 char *dn = ldap_get_dn(ld, e);
388 modify_dn(ld, dn, newpw);
395 modify_dn(ld, targetdn, newpw);
397 /* disconnect from server */