]> git.sur5r.net Git - openldap/blob - contrib/slapd-modules/smbk5pwd/smbk5pwd.c
cleanup
[openldap] / contrib / slapd-modules / smbk5pwd / smbk5pwd.c
1 /* smbk5pwd.c - Overlay for managing Samba and Heimdal passwords */
2 /* $OpenLDAP$ */
3 /*
4  * Copyright 2004-2005 by Howard Chu, Symas Corp.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted only as authorized by the OpenLDAP
9  * Public License.
10  *
11  * A copy of this license is available in the file LICENSE in the
12  * top-level directory of the distribution or, alternatively, at
13  * <http://www.OpenLDAP.org/license.html>.
14  */
15
16 #include <portable.h>
17
18 #ifndef SLAPD_OVER_SMBK5PWD
19 #define SLAPD_OVER_SMBK5PWD SLAPD_MOD_DYNAMIC
20 #endif
21
22 #ifdef SLAPD_OVER_SMBK5PWD
23
24 #include <slap.h>
25 #include <ac/errno.h>
26 #include <ac/string.h>
27
28 #ifdef DO_KRB5
29 #include <lber.h>
30 #include <lber_pvt.h>
31 #include <lutil.h>
32
33 /* make ASN1_MALLOC_ENCODE use our allocator */
34 #define malloc  ch_malloc
35
36 #include <krb5.h>
37 #include <kadm5/admin.h>
38 #include <hdb.h>
39
40 #ifndef HDB_INTERFACE_VERSION
41 #define HDB_MASTER_KEY_SET      master_key_set
42 #else
43 #define HDB_MASTER_KEY_SET      hdb_master_key_set
44 #endif
45
46 static krb5_context context;
47 static void *kadm_context;
48 static kadm5_config_params conf;
49 static HDB *db;
50
51 static AttributeDescription *ad_krb5Key;
52 static AttributeDescription *ad_krb5KeyVersionNumber;
53 static AttributeDescription *ad_krb5PrincipalName;
54 static ObjectClass *oc_krb5KDCEntry;
55 #endif
56
57 #ifdef DO_SAMBA
58 #include <openssl/des.h>
59 #include <openssl/md4.h>
60 #include "ldap_utf8.h"
61
62 static AttributeDescription *ad_sambaLMPassword;
63 static AttributeDescription *ad_sambaNTPassword;
64 static AttributeDescription *ad_sambaPwdLastSet;
65 static ObjectClass *oc_sambaSamAccount;
66 #endif
67
68 #if 0
69 static void smbk5pwd_destroy() {
70         kadm5_destroy(kadm_context);
71         krb5_free_context(context);
72 }
73 #endif
74
75 #ifdef DO_SAMBA
76 static const char hex[] = "0123456789abcdef";
77
78 /* From liblutil/passwd.c... */
79 static void lmPasswd_to_key(
80         const char *lmPasswd,
81         des_cblock *key)
82 {
83         const unsigned char *lpw = (const unsigned char *)lmPasswd;
84         unsigned char *k = (unsigned char *)key;
85
86         /* make room for parity bits */
87         k[0] = lpw[0];
88         k[1] = ((lpw[0]&0x01)<<7) | (lpw[1]>>1);
89         k[2] = ((lpw[1]&0x03)<<6) | (lpw[2]>>2);
90         k[3] = ((lpw[2]&0x07)<<5) | (lpw[3]>>3);
91         k[4] = ((lpw[3]&0x0F)<<4) | (lpw[4]>>4);
92         k[5] = ((lpw[4]&0x1F)<<3) | (lpw[5]>>5);
93         k[6] = ((lpw[5]&0x3F)<<2) | (lpw[6]>>6);
94         k[7] = ((lpw[6]&0x7F)<<1);
95
96         des_set_odd_parity( key );
97 }
98
99 #define MAX_PWLEN 256
100 #define HASHLEN 16
101
102 static void hexify(
103         const char in[HASHLEN],
104         struct berval *out
105 )
106 {
107         int i;
108         char *a;
109         unsigned char *b;
110
111         out->bv_val = ch_malloc(HASHLEN*2 + 1);
112         out->bv_len = HASHLEN*2;
113
114         a = out->bv_val;
115         b = (unsigned char *)in;
116         for (i=0; i<HASHLEN; i++) {
117                 *a++ = hex[*b >> 4];
118                 *a++ = hex[*b++ & 0x0f];
119         }
120         *a++ = '\0';
121 }
122
123 static void lmhash(
124         struct berval *passwd,
125         struct berval *hash
126 )
127 {
128         char UcasePassword[15];
129         des_cblock key;
130         des_key_schedule schedule;
131         des_cblock StdText = "KGS!@#$%";
132         des_cblock hbuf[2];
133
134         strncpy( UcasePassword, passwd->bv_val, 14 );
135         UcasePassword[14] = '\0';
136         ldap_pvt_str2upper( UcasePassword );
137
138         lmPasswd_to_key( UcasePassword, &key );
139         des_set_key_unchecked( &key, schedule );
140         des_ecb_encrypt( &StdText, &hbuf[0], schedule , DES_ENCRYPT );
141
142         lmPasswd_to_key( &UcasePassword[7], &key );
143         des_set_key_unchecked( &key, schedule );
144         des_ecb_encrypt( &StdText, &hbuf[1], schedule , DES_ENCRYPT );
145
146         hexify( (char *)hbuf, hash );
147 }
148
149 static void nthash(
150         struct berval *passwd,
151         struct berval *hash
152 )
153 {
154         /* Windows currently only allows 14 character passwords, but
155          * may support up to 256 in the future. We assume this means
156          * 256 UCS2 characters, not 256 bytes...
157          */
158         char hbuf[HASHLEN];
159         MD4_CTX ctx;
160
161         if (passwd->bv_len > MAX_PWLEN*2)
162                 passwd->bv_len = MAX_PWLEN*2;
163                 
164         MD4_Init( &ctx );
165         MD4_Update( &ctx, passwd->bv_val, passwd->bv_len );
166         MD4_Final( (unsigned char *)hbuf, &ctx );
167
168         hexify( hbuf, hash );
169 }
170 #endif /* DO_SAMBA */
171
172 #ifdef DO_KRB5
173
174 static int smbk5pwd_op_cleanup(
175         Operation *op,
176         SlapReply *rs )
177 {
178         slap_callback *cb;
179
180         /* clear out the current key */
181         ldap_pvt_thread_pool_setkey( op->o_threadctx, smbk5pwd_op_cleanup,
182                 NULL, NULL );
183
184         /* free the callback */
185         cb = op->o_callback;
186         op->o_callback = cb->sc_next;
187         op->o_tmpfree( cb, op->o_tmpmemctx );
188         return 0;
189 }
190
191 static int smbk5pwd_op_bind(
192         Operation *op,
193         SlapReply *rs )
194 {
195         /* If this is a simple Bind, stash the Op pointer so our chk
196          * function can find it. Set a cleanup callback to clear it
197          * out when the Bind completes.
198          */
199         if ( op->oq_bind.rb_method == LDAP_AUTH_SIMPLE ) {
200                 slap_callback *cb;
201                 ldap_pvt_thread_pool_setkey( op->o_threadctx, smbk5pwd_op_cleanup, op,
202                         NULL );
203                 cb = op->o_tmpcalloc( 1, sizeof(slap_callback), op->o_tmpmemctx );
204                 cb->sc_cleanup = smbk5pwd_op_cleanup;
205                 cb->sc_next = op->o_callback;
206                 op->o_callback = cb;
207         }
208         return SLAP_CB_CONTINUE;
209 }
210
211 static LUTIL_PASSWD_CHK_FUNC k5key_chk;
212 static LUTIL_PASSWD_HASH_FUNC k5key_hash;
213 static const struct berval k5key_scheme = BER_BVC("{K5KEY}");
214
215 /* This password scheme stores no data in the userPassword attribute
216  * other than the scheme name. It assumes the invoking entry is a
217  * krb5KDCentry and compares the passed-in credentials against the
218  * krb5Key attribute. The krb5Key may be multi-valued, but they are
219  * simply multiple keytypes generated from the same input string, so
220  * only the first value needs to be compared here.
221  *
222  * Since the lutil_passwd API doesn't pass the Entry object in, we
223  * have to fetch it ourselves in order to get access to the other
224  * attributes. We accomplish this with the help of the overlay's Bind
225  * function, which stores the current Operation pointer in thread-specific
226  * storage so we can retrieve it here. The Operation provides all
227  * the necessary context for us to get Entry from the database.
228  */
229 static int k5key_chk(
230         const struct berval *sc,
231         const struct berval *passwd,
232         const struct berval *cred,
233         const char **text )
234 {
235         void *ctx;
236         Operation *op;
237         int rc;
238         Entry *e;
239         Attribute *a;
240     krb5_error_code ret;
241     krb5_keyblock key;
242     krb5_salt salt;
243         hdb_entry ent;
244
245         /* Find our thread context, find our Operation */
246         ctx = ldap_pvt_thread_pool_context();
247
248         if ( ldap_pvt_thread_pool_getkey( ctx, smbk5pwd_op_cleanup, (void **)&op, NULL ) ||
249                 !op )
250                 return LUTIL_PASSWD_ERR;
251
252         rc = be_entry_get_rw( op, &op->o_req_ndn, NULL, NULL, 0, &e );
253         if ( rc != LDAP_SUCCESS ) return LUTIL_PASSWD_ERR;
254
255         rc = LUTIL_PASSWD_ERR;
256         do {
257                 size_t l;
258                 Key ekey = {0};
259
260                 a = attr_find( e->e_attrs, ad_krb5PrincipalName );
261                 if (!a ) break;
262
263                 memset( &ent, 0, sizeof(ent) );
264                 ret = krb5_parse_name(context, a->a_vals[0].bv_val, &ent.principal);
265                 if ( ret ) break;
266                 krb5_get_pw_salt( context, ent.principal, &salt );
267                 krb5_free_principal( context, ent.principal );
268
269                 a = attr_find( e->e_attrs, ad_krb5Key );
270                 if ( !a ) break;
271
272                 ent.keys.len = 1;
273                 ent.keys.val = &ekey;
274                 decode_Key((unsigned char *) a->a_vals[0].bv_val,
275                         (size_t) a->a_vals[0].bv_len, &ent.keys.val[0], &l);
276                 if ( db->HDB_MASTER_KEY_SET )
277                         hdb_unseal_keys( context, db, &ent );
278
279                 krb5_string_to_key_salt( context, ekey.key.keytype, cred->bv_val,
280                         salt, &key );
281
282                 krb5_free_salt( context, salt );
283
284                 if ( memcmp( ekey.key.keyvalue.data, key.keyvalue.data,
285                         key.keyvalue.length ) == 0 ) rc = LUTIL_PASSWD_OK;
286
287                 krb5_free_keyblock_contents( context, &key );
288                 krb5_free_keyblock_contents( context, &ekey.key );
289
290         } while(0);
291         be_entry_release_r( op, e );
292         return rc;
293 }
294
295 static int k5key_hash(
296         const struct berval *scheme,
297         const struct berval *passwd,
298         struct berval *hash,
299         const char **text )
300 {
301         ber_dupbv( hash, (struct berval *)&k5key_scheme );
302         return LUTIL_PASSWD_OK;
303 }
304 #endif /* DO_KRB5 */
305
306 static int smbk5pwd_exop_passwd(
307         Operation *op,
308         SlapReply *rs )
309 {
310         int rc;
311         req_pwdexop_s *qpw = &op->oq_pwdexop;
312         Entry *e;
313         Modifications *ml;
314         slap_overinst *on = (slap_overinst *)op->o_bd->bd_info;
315
316         /* Not the operation we expected, pass it on... */
317         if ( ber_bvcmp( &slap_EXOP_MODIFY_PASSWD, &op->ore_reqoid ) ) {
318                 return SLAP_CB_CONTINUE;
319         }
320
321         op->o_bd->bd_info = (BackendInfo *)on->on_info;
322         rc = be_entry_get_rw( op, &op->o_req_ndn, NULL, NULL, 0, &e );
323         if ( rc != LDAP_SUCCESS ) return rc;
324
325 #ifdef DO_KRB5
326         /* Kerberos stuff */
327         do {
328                 krb5_error_code ret;
329                 hdb_entry ent;
330                 struct berval *keys;
331                 int kvno, i;
332                 Attribute *a;
333
334                 if ( !is_entry_objectclass(e, oc_krb5KDCEntry, 0 ) ) break;
335
336                 a = attr_find( e->e_attrs, ad_krb5PrincipalName );
337                 if ( !a ) break;
338
339                 memset( &ent, 0, sizeof(ent) );
340                 ret = krb5_parse_name(context, a->a_vals[0].bv_val, &ent.principal);
341                 if ( ret ) break;
342
343                 a = attr_find( e->e_attrs, ad_krb5KeyVersionNumber );
344                 if ( a ) {
345                         kvno = atoi(a->a_vals[0].bv_val);
346                 } else {
347                         /* shouldn't happen, this is a required attr */
348                         kvno = 0;
349                 }
350
351                 ret = _kadm5_set_keys(kadm_context, &ent, qpw->rs_new.bv_val);
352                 hdb_seal_keys(context, db, &ent);
353                 krb5_free_principal( context, ent.principal );
354
355                 keys = ch_malloc( (ent.keys.len + 1) * sizeof(struct berval));
356
357                 for (i = 0; i < ent.keys.len; i++) {
358                         unsigned char *buf;
359                         size_t len;
360
361                         ASN1_MALLOC_ENCODE(Key, buf, len, &ent.keys.val[i], &len, ret);
362                         if (ret != 0)
363                                 break;
364                         
365                         keys[i].bv_val = (char *)buf;
366                         keys[i].bv_len = len;
367                 }
368                 BER_BVZERO( &keys[i] );
369
370                 _kadm5_free_keys(kadm_context, ent.keys.len, ent.keys.val);
371
372                 if ( i != ent.keys.len ) {
373                         ber_bvarray_free( keys );
374                         break;
375                 }
376
377                 ml = ch_malloc(sizeof(Modifications));
378                 if (!qpw->rs_modtail) qpw->rs_modtail = &ml->sml_next;
379                 ml->sml_next = qpw->rs_mods;
380                 qpw->rs_mods = ml;
381
382                 ml->sml_desc = ad_krb5Key;
383                 ml->sml_op = LDAP_MOD_REPLACE;
384 #ifdef SLAP_MOD_INTERNAL
385                 ml->sml_flags = SLAP_MOD_INTERNAL;
386 #endif
387                 ml->sml_values = keys;
388                 ml->sml_nvalues = NULL;
389                 
390                 ml = ch_malloc(sizeof(Modifications));
391                 ml->sml_next = qpw->rs_mods;
392                 qpw->rs_mods = ml;
393                 
394                 ml->sml_desc = ad_krb5KeyVersionNumber;
395                 ml->sml_op = LDAP_MOD_REPLACE;
396 #ifdef SLAP_MOD_INTERNAL
397                 ml->sml_flags = SLAP_MOD_INTERNAL;
398 #endif
399                 ml->sml_values = ch_malloc( 2 * sizeof(struct berval));
400                 ml->sml_values[0].bv_val = ch_malloc( 64 );
401                 ml->sml_values[0].bv_len = sprintf(ml->sml_values[0].bv_val,
402                         "%d", kvno+1 );
403                 BER_BVZERO( &ml->sml_values[1] );
404                 ml->sml_nvalues = NULL;
405         } while(0);
406 #endif /* DO_KRB5 */
407
408 #ifdef DO_SAMBA
409         /* Samba stuff */
410         if ( is_entry_objectclass(e, oc_sambaSamAccount, 0 ) ) {
411                 struct berval *keys;
412                 ber_len_t j,l;
413                 wchar_t *wcs, wc;
414                 char *c, *d;
415                 struct berval pwd;
416                 
417                 /* Expand incoming UTF8 string to UCS4 */
418                 l = ldap_utf8_chars(qpw->rs_new.bv_val);
419                 wcs = ch_malloc((l+1) * sizeof(wchar_t));
420
421                 ldap_x_utf8s_to_wcs( wcs, qpw->rs_new.bv_val, l );
422                 
423                 /* Truncate UCS4 to UCS2 */
424                 c = (char *)wcs;
425                 for (j=0; j<l; j++) {
426                         wc = wcs[j];
427                         *c++ = wc & 0xff;
428                         *c++ = (wc >> 8) & 0xff;
429                 }
430                 *c++ = 0;
431                 pwd.bv_val = (char *)wcs;
432                 pwd.bv_len = l * 2;
433
434                 ml = ch_malloc(sizeof(Modifications));
435                 if (!qpw->rs_modtail) qpw->rs_modtail = &ml->sml_next;
436                 ml->sml_next = qpw->rs_mods;
437                 qpw->rs_mods = ml;
438
439                 keys = ch_malloc( 2 * sizeof(struct berval) );
440                 BER_BVZERO( &keys[1] );
441                 nthash( &pwd, keys );
442                 
443                 ml->sml_desc = ad_sambaNTPassword;
444                 ml->sml_op = LDAP_MOD_REPLACE;
445 #ifdef SLAP_MOD_INTERNAL
446                 ml->sml_flags = SLAP_MOD_INTERNAL;
447 #endif
448                 ml->sml_values = keys;
449                 ml->sml_nvalues = NULL;
450
451                 /* Truncate UCS2 to 8-bit ASCII */
452                 c = pwd.bv_val+1;
453                 d = pwd.bv_val+2;
454                 for (j=1; j<l; j++) {
455                         *c++ = *d++;
456                         d++;
457                 }
458                 pwd.bv_len /= 2;
459                 pwd.bv_val[pwd.bv_len] = '\0';
460
461                 ml = ch_malloc(sizeof(Modifications));
462                 ml->sml_next = qpw->rs_mods;
463                 qpw->rs_mods = ml;
464
465                 keys = ch_malloc( 2 * sizeof(struct berval) );
466                 BER_BVZERO( &keys[1] );
467                 lmhash( &pwd, keys );
468                 
469                 ml->sml_desc = ad_sambaLMPassword;
470                 ml->sml_op = LDAP_MOD_REPLACE;
471 #ifdef SLAP_MOD_INTERNAL
472                 ml->sml_flags = SLAP_MOD_INTERNAL;
473 #endif
474                 ml->sml_values = keys;
475                 ml->sml_nvalues = NULL;
476
477                 ch_free(wcs);
478
479                 ml = ch_malloc(sizeof(Modifications));
480                 ml->sml_next = qpw->rs_mods;
481                 qpw->rs_mods = ml;
482
483                 keys = ch_malloc( 2 * sizeof(struct berval) );
484                 keys[0].bv_val = ch_malloc( STRLENOF( "9223372036854775807L" ) + 1 );
485                 keys[0].bv_len = snprintf(keys[0].bv_val,
486                         STRLENOF( "9223372036854775807L" ) + 1,
487                         "%ld", slap_get_time());
488                 BER_BVZERO( &keys[1] );
489                 
490                 ml->sml_desc = ad_sambaPwdLastSet;
491                 ml->sml_op = LDAP_MOD_REPLACE;
492 #ifdef SLAP_MOD_INTERNAL
493                 ml->sml_flags = SLAP_MOD_INTERNAL;
494 #endif
495                 ml->sml_values = keys;
496                 ml->sml_nvalues = NULL;
497         }
498 #endif /* DO_SAMBA */
499         be_entry_release_r( op, e );
500
501         return SLAP_CB_CONTINUE;
502 }
503
504 static slap_overinst smbk5pwd;
505
506 int smbk5pwd_init() {
507         int rc;
508         const char *text;
509
510 #ifdef DO_KRB5
511         krb5_error_code ret;
512         extern HDB * _kadm5_s_get_db(void *);
513
514         /* Make sure all of our necessary schema items are loaded */
515         oc_krb5KDCEntry = oc_find("krb5KDCEntry");
516         if ( !oc_krb5KDCEntry ) return -1;
517
518         rc = slap_str2ad( "krb5Key", &ad_krb5Key, &text );
519         if ( rc ) return rc;
520         rc = slap_str2ad( "krb5KeyVersionNumber", &ad_krb5KeyVersionNumber, &text );
521         if ( rc ) return rc;
522         rc = slap_str2ad( "krb5PrincipalName", &ad_krb5PrincipalName, &text );
523         if ( rc ) return rc;
524
525         /* Initialize Kerberos context */
526         ret = krb5_init_context(&context);
527         if (ret) {
528                 return -1;
529         }
530
531         ret = kadm5_s_init_with_password_ctx( context,
532                 KADM5_ADMIN_SERVICE,
533                 NULL,
534                 KADM5_ADMIN_SERVICE,
535                 &conf, 0, 0, &kadm_context );
536         
537         db = _kadm5_s_get_db(kadm_context);
538 #endif /* DO_KRB5 */
539
540 #ifdef DO_SAMBA
541         oc_sambaSamAccount = oc_find("sambaSamAccount");
542         if ( !oc_sambaSamAccount ) return -1;
543
544         rc = slap_str2ad( "sambaLMPassword", &ad_sambaLMPassword, &text );
545         if ( rc ) return rc;
546         rc = slap_str2ad( "sambaNTPassword", &ad_sambaNTPassword, &text );
547         if ( rc ) return rc;
548         rc = slap_str2ad( "sambaPwdLastSet", &ad_sambaPwdLastSet, &text );
549         if ( rc ) return rc;
550 #endif /* DO_SAMBA */
551
552         smbk5pwd.on_bi.bi_type = "smbk5pwd";
553         smbk5pwd.on_bi.bi_extended = smbk5pwd_exop_passwd;
554
555 #ifdef DO_KRB5
556         smbk5pwd.on_bi.bi_op_bind = smbk5pwd_op_bind;
557
558         lutil_passwd_add( (struct berval *)&k5key_scheme, k5key_chk, k5key_hash );
559 #endif
560
561         return overlay_register( &smbk5pwd );
562 }
563
564 #if SLAPD_OVER_SMBK5PWD == SLAPD_MOD_DYNAMIC
565 int init_module(int argc, char *argv[]) {
566         return smbk5pwd_init();
567 }
568 #endif
569
570 #endif /* defined(SLAPD_OVER_SMBK5PWD) */