Home | History | Annotate | Line # | Download | only in lastbind
lastbind.c revision 1.1.1.1.10.1
      1  1.1.1.1.10.1  pgoyette /*	$NetBSD: lastbind.c,v 1.1.1.1.10.1 2017/03/20 06:56:10 pgoyette Exp $	*/
      2           1.1      tron 
      3           1.1      tron /* lastbind.c - Record timestamp of the last successful bind to entries */
      4           1.1      tron /* $OpenLDAP$ */
      5           1.1      tron /*
      6           1.1      tron  * Copyright 2009 Jonathan Clarke <jonathan (at) phillipoux.net>.
      7           1.1      tron  * All rights reserved.
      8           1.1      tron  *
      9           1.1      tron  * Redistribution and use in source and binary forms, with or without
     10           1.1      tron  * modification, are permitted only as authorized by the OpenLDAP
     11           1.1      tron  * Public License.
     12           1.1      tron  *
     13           1.1      tron  * A copy of this license is available in the file LICENSE in the
     14           1.1      tron  * top-level directory of the distribution or, alternatively, at
     15           1.1      tron  * <http://www.OpenLDAP.org/license.html>.
     16           1.1      tron  */
     17           1.1      tron /* ACKNOWLEDGEMENTS:
     18           1.1      tron  * This work is loosely derived from the ppolicy overlay.
     19           1.1      tron  */
     20           1.1      tron 
     21  1.1.1.1.10.1  pgoyette #include <sys/cdefs.h>
     22  1.1.1.1.10.1  pgoyette __RCSID("$NetBSD: lastbind.c,v 1.1.1.1.10.1 2017/03/20 06:56:10 pgoyette Exp $");
     23  1.1.1.1.10.1  pgoyette 
     24           1.1      tron #include "portable.h"
     25           1.1      tron 
     26           1.1      tron /*
     27           1.1      tron  * This file implements an overlay that stores the timestamp of the
     28           1.1      tron  * last successful bind operation in a directory entry.
     29           1.1      tron  *
     30           1.1      tron  * Optimization: to avoid performing a write on each bind,
     31           1.1      tron  * a precision for this timestamp may be configured, causing it to
     32           1.1      tron  * only be updated if it is older than a given number of seconds.
     33           1.1      tron  */
     34           1.1      tron 
     35           1.1      tron #ifdef SLAPD_OVER_LASTBIND
     36           1.1      tron 
     37           1.1      tron #include <ldap.h>
     38           1.1      tron #include "lutil.h"
     39           1.1      tron #include "slap.h"
     40           1.1      tron #include <ac/errno.h>
     41           1.1      tron #include <ac/time.h>
     42           1.1      tron #include <ac/string.h>
     43           1.1      tron #include <ac/ctype.h>
     44           1.1      tron #include "config.h"
     45           1.1      tron 
     46           1.1      tron /* Per-instance configuration information */
     47           1.1      tron typedef struct lastbind_info {
     48           1.1      tron 	/* precision to update timestamp in authTimestamp attribute */
     49           1.1      tron 	int timestamp_precision;
     50           1.1      tron } lastbind_info;
     51           1.1      tron 
     52           1.1      tron /* Operational attributes */
     53           1.1      tron static AttributeDescription *ad_authTimestamp;
     54           1.1      tron 
     55           1.1      tron /* This is the definition used by ISODE, as supplied to us in
     56           1.1      tron  * ITS#6238 Followup #9
     57           1.1      tron  */
     58           1.1      tron static struct schema_info {
     59           1.1      tron 	char *def;
     60           1.1      tron 	AttributeDescription **ad;
     61           1.1      tron } lastBind_OpSchema[] = {
     62           1.1      tron 	{	"( 1.3.6.1.4.1.453.16.2.188 "
     63           1.1      tron 		"NAME 'authTimestamp' "
     64           1.1      tron 		"DESC 'last successful authentication using any method/mech' "
     65           1.1      tron 		"EQUALITY generalizedTimeMatch "
     66           1.1      tron 		"ORDERING generalizedTimeOrderingMatch "
     67           1.1      tron 		"SYNTAX 1.3.6.1.4.1.1466.115.121.1.24 "
     68           1.1      tron 		"SINGLE-VALUE NO-USER-MODIFICATION USAGE dsaOperation )",
     69           1.1      tron 		&ad_authTimestamp},
     70           1.1      tron 	{ NULL, NULL }
     71           1.1      tron };
     72           1.1      tron 
     73           1.1      tron /* configuration attribute and objectclass */
     74           1.1      tron static ConfigTable lastbindcfg[] = {
     75           1.1      tron 	{ "lastbind-precision", "seconds", 2, 2, 0,
     76           1.1      tron 	  ARG_INT|ARG_OFFSET,
     77           1.1      tron 	  (void *)offsetof(lastbind_info, timestamp_precision),
     78  1.1.1.1.10.1  pgoyette 	  "( OLcfgCtAt:5.1 "
     79           1.1      tron 	  "NAME 'olcLastBindPrecision' "
     80           1.1      tron 	  "DESC 'Precision of authTimestamp attribute' "
     81           1.1      tron 	  "SYNTAX OMsInteger SINGLE-VALUE )", NULL, NULL },
     82           1.1      tron 	{ NULL, NULL, 0, 0, 0, ARG_IGNORED }
     83           1.1      tron };
     84           1.1      tron 
     85           1.1      tron static ConfigOCs lastbindocs[] = {
     86  1.1.1.1.10.1  pgoyette 	{ "( OLcfgCtOc:5.1 "
     87           1.1      tron 	  "NAME 'olcLastBindConfig' "
     88           1.1      tron 	  "DESC 'Last Bind configuration' "
     89           1.1      tron 	  "SUP olcOverlayConfig "
     90           1.1      tron 	  "MAY ( olcLastBindPrecision ) )",
     91           1.1      tron 	  Cft_Overlay, lastbindcfg, NULL, NULL },
     92           1.1      tron 	{ NULL, 0, NULL }
     93           1.1      tron };
     94           1.1      tron 
     95           1.1      tron static time_t
     96           1.1      tron parse_time( char *atm )
     97           1.1      tron {
     98           1.1      tron 	struct lutil_tm tm;
     99           1.1      tron 	struct lutil_timet tt;
    100           1.1      tron 	time_t ret = (time_t)-1;
    101           1.1      tron 
    102           1.1      tron 	if ( lutil_parsetime( atm, &tm ) == 0) {
    103           1.1      tron 		lutil_tm2time( &tm, &tt );
    104           1.1      tron 		ret = tt.tt_sec;
    105           1.1      tron 	}
    106           1.1      tron 	return ret;
    107           1.1      tron }
    108           1.1      tron 
    109           1.1      tron static int
    110           1.1      tron lastbind_bind_response( Operation *op, SlapReply *rs )
    111           1.1      tron {
    112           1.1      tron 	Modifications *mod = NULL;
    113           1.1      tron 	BackendInfo *bi = op->o_bd->bd_info;
    114           1.1      tron 	Entry *e;
    115           1.1      tron 	int rc;
    116           1.1      tron 
    117           1.1      tron 	/* we're only interested if the bind was successful */
    118           1.1      tron 	if ( rs->sr_err != LDAP_SUCCESS )
    119           1.1      tron 		return SLAP_CB_CONTINUE;
    120           1.1      tron 
    121           1.1      tron 	rc = be_entry_get_rw( op, &op->o_req_ndn, NULL, NULL, 0, &e );
    122           1.1      tron 	op->o_bd->bd_info = bi;
    123           1.1      tron 
    124           1.1      tron 	if ( rc != LDAP_SUCCESS ) {
    125           1.1      tron 		return SLAP_CB_CONTINUE;
    126           1.1      tron 	}
    127           1.1      tron 
    128           1.1      tron 	{
    129           1.1      tron 		lastbind_info *lbi = (lastbind_info *) op->o_callback->sc_private;
    130           1.1      tron 
    131           1.1      tron 		time_t now, bindtime = (time_t)-1;
    132           1.1      tron 		Attribute *a;
    133           1.1      tron 		Modifications *m;
    134           1.1      tron 		char nowstr[ LDAP_LUTIL_GENTIME_BUFSIZE ];
    135           1.1      tron 		struct berval timestamp;
    136           1.1      tron 
    137           1.1      tron 		/* get the current time */
    138           1.1      tron 		now = slap_get_time();
    139           1.1      tron 
    140           1.1      tron 		/* get authTimestamp attribute, if it exists */
    141           1.1      tron 		if ((a = attr_find( e->e_attrs, ad_authTimestamp)) != NULL) {
    142           1.1      tron 			bindtime = parse_time( a->a_nvals[0].bv_val );
    143           1.1      tron 
    144           1.1      tron 			if (bindtime != (time_t)-1) {
    145           1.1      tron 				/* if the recorded bind time is within our precision, we're done
    146           1.1      tron 				 * it doesn't need to be updated (save a write for nothing) */
    147           1.1      tron 				if ((now - bindtime) < lbi->timestamp_precision) {
    148           1.1      tron 					goto done;
    149           1.1      tron 				}
    150           1.1      tron 			}
    151           1.1      tron 		}
    152           1.1      tron 
    153           1.1      tron 		/* update the authTimestamp in the user's entry with the current time */
    154           1.1      tron 		timestamp.bv_val = nowstr;
    155           1.1      tron 		timestamp.bv_len = sizeof(nowstr);
    156           1.1      tron 		slap_timestamp( &now, &timestamp );
    157           1.1      tron 
    158           1.1      tron 		m = ch_calloc( sizeof(Modifications), 1 );
    159           1.1      tron 		m->sml_op = LDAP_MOD_REPLACE;
    160           1.1      tron 		m->sml_flags = 0;
    161           1.1      tron 		m->sml_type = ad_authTimestamp->ad_cname;
    162           1.1      tron 		m->sml_desc = ad_authTimestamp;
    163           1.1      tron 		m->sml_numvals = 1;
    164           1.1      tron 		m->sml_values = ch_calloc( sizeof(struct berval), 2 );
    165           1.1      tron 		m->sml_nvalues = ch_calloc( sizeof(struct berval), 2 );
    166           1.1      tron 
    167           1.1      tron 		ber_dupbv( &m->sml_values[0], &timestamp );
    168           1.1      tron 		ber_dupbv( &m->sml_nvalues[0], &timestamp );
    169           1.1      tron 		m->sml_next = mod;
    170           1.1      tron 		mod = m;
    171           1.1      tron 	}
    172           1.1      tron 
    173           1.1      tron done:
    174           1.1      tron 	be_entry_release_r( op, e );
    175           1.1      tron 
    176           1.1      tron 	/* perform the update, if necessary */
    177           1.1      tron 	if ( mod ) {
    178           1.1      tron 		Operation op2 = *op;
    179           1.1      tron 		SlapReply r2 = { REP_RESULT };
    180           1.1      tron 		slap_callback cb = { NULL, slap_null_cb, NULL, NULL };
    181           1.1      tron 
    182           1.1      tron 		/* This is a DSA-specific opattr, it never gets replicated. */
    183           1.1      tron 		op2.o_tag = LDAP_REQ_MODIFY;
    184           1.1      tron 		op2.o_callback = &cb;
    185           1.1      tron 		op2.orm_modlist = mod;
    186           1.1      tron 		op2.o_dn = op->o_bd->be_rootdn;
    187           1.1      tron 		op2.o_ndn = op->o_bd->be_rootndn;
    188           1.1      tron 		op2.o_dont_replicate = 1;
    189           1.1      tron 		rc = op->o_bd->be_modify( &op2, &r2 );
    190           1.1      tron 		slap_mods_free( mod, 1 );
    191           1.1      tron 	}
    192           1.1      tron 
    193           1.1      tron 	op->o_bd->bd_info = bi;
    194           1.1      tron 	return SLAP_CB_CONTINUE;
    195           1.1      tron }
    196           1.1      tron 
    197           1.1      tron static int
    198           1.1      tron lastbind_bind( Operation *op, SlapReply *rs )
    199           1.1      tron {
    200           1.1      tron 	slap_callback *cb;
    201           1.1      tron 	slap_overinst *on = (slap_overinst *) op->o_bd->bd_info;
    202           1.1      tron 
    203           1.1      tron 	/* setup a callback to intercept result of this bind operation
    204           1.1      tron 	 * and pass along the lastbind_info struct */
    205           1.1      tron 	cb = op->o_tmpcalloc( sizeof(slap_callback), 1, op->o_tmpmemctx );
    206           1.1      tron 	cb->sc_response = lastbind_bind_response;
    207           1.1      tron 	cb->sc_next = op->o_callback->sc_next;
    208           1.1      tron 	cb->sc_private = on->on_bi.bi_private;
    209           1.1      tron 	op->o_callback->sc_next = cb;
    210           1.1      tron 
    211           1.1      tron 	return SLAP_CB_CONTINUE;
    212           1.1      tron }
    213           1.1      tron 
    214           1.1      tron static int
    215           1.1      tron lastbind_db_init(
    216           1.1      tron 	BackendDB *be,
    217           1.1      tron 	ConfigReply *cr
    218           1.1      tron )
    219           1.1      tron {
    220           1.1      tron 	slap_overinst *on = (slap_overinst *) be->bd_info;
    221           1.1      tron 
    222           1.1      tron 	/* initialize private structure to store configuration */
    223           1.1      tron 	on->on_bi.bi_private = ch_calloc( 1, sizeof(lastbind_info) );
    224           1.1      tron 
    225           1.1      tron 	return 0;
    226           1.1      tron }
    227           1.1      tron 
    228           1.1      tron static int
    229           1.1      tron lastbind_db_close(
    230           1.1      tron 	BackendDB *be,
    231           1.1      tron 	ConfigReply *cr
    232           1.1      tron )
    233           1.1      tron {
    234           1.1      tron 	slap_overinst *on = (slap_overinst *) be->bd_info;
    235           1.1      tron 	lastbind_info *lbi = (lastbind_info *) on->on_bi.bi_private;
    236           1.1      tron 
    237           1.1      tron 	/* free private structure to store configuration */
    238           1.1      tron 	free( lbi );
    239           1.1      tron 
    240           1.1      tron 	return 0;
    241           1.1      tron }
    242           1.1      tron 
    243           1.1      tron static slap_overinst lastbind;
    244           1.1      tron 
    245           1.1      tron int lastbind_initialize()
    246           1.1      tron {
    247           1.1      tron 	int i, code;
    248           1.1      tron 
    249           1.1      tron 	/* register operational schema for this overlay (authTimestamp attribute) */
    250           1.1      tron 	for (i=0; lastBind_OpSchema[i].def; i++) {
    251           1.1      tron 		code = register_at( lastBind_OpSchema[i].def, lastBind_OpSchema[i].ad, 0 );
    252           1.1      tron 		if ( code ) {
    253           1.1      tron 			Debug( LDAP_DEBUG_ANY,
    254           1.1      tron 				"lastbind_initialize: register_at failed\n", 0, 0, 0 );
    255           1.1      tron 			return code;
    256           1.1      tron 		}
    257           1.1      tron 	}
    258           1.1      tron 
    259           1.1      tron 	ad_authTimestamp->ad_type->sat_flags |= SLAP_AT_MANAGEABLE;
    260           1.1      tron 
    261           1.1      tron 	lastbind.on_bi.bi_type = "lastbind";
    262           1.1      tron 	lastbind.on_bi.bi_db_init = lastbind_db_init;
    263           1.1      tron 	lastbind.on_bi.bi_db_close = lastbind_db_close;
    264           1.1      tron 	lastbind.on_bi.bi_op_bind = lastbind_bind;
    265           1.1      tron 
    266           1.1      tron 	/* register configuration directives */
    267           1.1      tron 	lastbind.on_bi.bi_cf_ocs = lastbindocs;
    268           1.1      tron 	code = config_register_schema( lastbindcfg, lastbindocs );
    269           1.1      tron 	if ( code ) return code;
    270           1.1      tron 
    271           1.1      tron 	return overlay_register( &lastbind );
    272           1.1      tron }
    273           1.1      tron 
    274           1.1      tron #if SLAPD_OVER_LASTBIND == SLAPD_MOD_DYNAMIC
    275           1.1      tron int init_module(int argc, char *argv[]) {
    276           1.1      tron 	return lastbind_initialize();
    277           1.1      tron }
    278           1.1      tron #endif
    279           1.1      tron 
    280           1.1      tron #endif	/* defined(SLAPD_OVER_LASTBIND) */
    281