1*549b59edSchristos /* $NetBSD: account_usability.c,v 1.2 2021/08/14 16:14:55 christos Exp $ */
2e670fd5cSchristos
3e670fd5cSchristos /* $OpenLDAP$ */
4e670fd5cSchristos /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5e670fd5cSchristos *
6e670fd5cSchristos * Copyright 2004-2021 The OpenLDAP Foundation.
7e670fd5cSchristos * Portions Copyright 2004 Hewlett-Packard Company.
8e670fd5cSchristos * Portions Copyright 2004 Howard Chu, Symas Corp.
9e670fd5cSchristos * All rights reserved.
10e670fd5cSchristos *
11e670fd5cSchristos * Redistribution and use in source and binary forms, with or without
12e670fd5cSchristos * modification, are permitted only as authorized by the OpenLDAP
13e670fd5cSchristos * Public License.
14e670fd5cSchristos *
15e670fd5cSchristos * A copy of this license is available in the file LICENSE in the
16e670fd5cSchristos * top-level directory of the distribution or, alternatively, at
17e670fd5cSchristos * <http://www.OpenLDAP.org/license.html>.
18e670fd5cSchristos */
19e670fd5cSchristos /* ACKNOWLEDGEMENTS:
20e670fd5cSchristos * This work was developed by Howard Chu for inclusion in
21e670fd5cSchristos * OpenLDAP Software, based on prior work by Neil Dunbar (HP).
22e670fd5cSchristos * This work was sponsored by the Hewlett-Packard Company.
23e670fd5cSchristos */
24e670fd5cSchristos
25e670fd5cSchristos #include <sys/cdefs.h>
26*549b59edSchristos __RCSID("$NetBSD: account_usability.c,v 1.2 2021/08/14 16:14:55 christos Exp $");
27e670fd5cSchristos
28e670fd5cSchristos #include "portable.h"
29e670fd5cSchristos
30e670fd5cSchristos #include "ldap-int.h"
31e670fd5cSchristos
32e670fd5cSchristos #ifdef LDAP_CONTROL_X_ACCOUNT_USABILITY
33e670fd5cSchristos
34e670fd5cSchristos int
ldap_create_accountusability_control(LDAP * ld,LDAPControl ** ctrlp)35e670fd5cSchristos ldap_create_accountusability_control( LDAP *ld,
36e670fd5cSchristos LDAPControl **ctrlp )
37e670fd5cSchristos {
38e670fd5cSchristos assert( ld != NULL );
39e670fd5cSchristos assert( LDAP_VALID( ld ) );
40e670fd5cSchristos assert( ctrlp != NULL );
41e670fd5cSchristos
42e670fd5cSchristos ld->ld_errno = ldap_control_create( LDAP_CONTROL_X_ACCOUNT_USABILITY,
43e670fd5cSchristos 0, NULL, 0, ctrlp );
44e670fd5cSchristos
45e670fd5cSchristos return ld->ld_errno;
46e670fd5cSchristos }
47e670fd5cSchristos
48e670fd5cSchristos int
ldap_parse_accountusability_control(LDAP * ld,LDAPControl * ctrl,int * availablep,LDAPAccountUsability * usabilityp)49e670fd5cSchristos ldap_parse_accountusability_control(
50e670fd5cSchristos LDAP *ld,
51e670fd5cSchristos LDAPControl *ctrl,
52e670fd5cSchristos int *availablep,
53e670fd5cSchristos LDAPAccountUsability *usabilityp )
54e670fd5cSchristos {
55e670fd5cSchristos BerElement *ber;
56e670fd5cSchristos int available = 0;
57e670fd5cSchristos ber_tag_t tag;
58e670fd5cSchristos ber_len_t berLen;
59e670fd5cSchristos char *last;
60e670fd5cSchristos
61e670fd5cSchristos assert( ld != NULL );
62e670fd5cSchristos assert( LDAP_VALID( ld ) );
63e670fd5cSchristos assert( ctrl != NULL );
64e670fd5cSchristos
65e670fd5cSchristos if ( !ctrl->ldctl_value.bv_val ) {
66e670fd5cSchristos ld->ld_errno = LDAP_DECODING_ERROR;
67e670fd5cSchristos return(ld->ld_errno);
68e670fd5cSchristos }
69e670fd5cSchristos
70e670fd5cSchristos /* Create a BerElement from the berval returned in the control. */
71e670fd5cSchristos ber = ber_init(&ctrl->ldctl_value);
72e670fd5cSchristos
73e670fd5cSchristos if (ber == NULL) {
74e670fd5cSchristos ld->ld_errno = LDAP_NO_MEMORY;
75e670fd5cSchristos return(ld->ld_errno);
76e670fd5cSchristos }
77e670fd5cSchristos
78e670fd5cSchristos tag = ber_peek_tag( ber, &berLen );
79e670fd5cSchristos
80e670fd5cSchristos if ( tag == LDAP_TAG_X_ACCOUNT_USABILITY_AVAILABLE ) {
81e670fd5cSchristos available = 1;
82e670fd5cSchristos
83e670fd5cSchristos if ( usabilityp != NULL ) {
84e670fd5cSchristos if (ber_get_int( ber, &usabilityp->seconds_remaining ) == LBER_DEFAULT) goto exit;
85e670fd5cSchristos }
86e670fd5cSchristos } else if ( tag == LDAP_TAG_X_ACCOUNT_USABILITY_NOT_AVAILABLE ) {
87e670fd5cSchristos available = 0;
88e670fd5cSchristos LDAPAccountUsabilityMoreInfo more_info = { 0, 0, 0, -1, -1 };
89e670fd5cSchristos
90e670fd5cSchristos ber_skip_tag( ber, &berLen );
91e670fd5cSchristos while ( (tag = ber_peek_tag( ber, &berLen )) != LBER_DEFAULT ) {
92e670fd5cSchristos switch (tag) {
93e670fd5cSchristos case LDAP_TAG_X_ACCOUNT_USABILITY_INACTIVE:
94e670fd5cSchristos if (ber_get_boolean( ber, &more_info.inactive ) == LBER_DEFAULT) goto exit;
95e670fd5cSchristos break;
96e670fd5cSchristos case LDAP_TAG_X_ACCOUNT_USABILITY_RESET:
97e670fd5cSchristos if (ber_get_boolean( ber, &more_info.reset ) == LBER_DEFAULT) goto exit;
98e670fd5cSchristos break;
99e670fd5cSchristos case LDAP_TAG_X_ACCOUNT_USABILITY_EXPIRED:
100e670fd5cSchristos if (ber_get_boolean( ber, &more_info.expired ) == LBER_DEFAULT) goto exit;
101e670fd5cSchristos break;
102e670fd5cSchristos case LDAP_TAG_X_ACCOUNT_USABILITY_REMAINING_GRACE:
103e670fd5cSchristos if (ber_get_int( ber, &more_info.remaining_grace ) == LBER_DEFAULT) goto exit;
104e670fd5cSchristos break;
105e670fd5cSchristos case LDAP_TAG_X_ACCOUNT_USABILITY_UNTIL_UNLOCK:
106e670fd5cSchristos if (ber_get_int( ber, &more_info.seconds_before_unlock ) == LBER_DEFAULT) goto exit;
107e670fd5cSchristos break;
108e670fd5cSchristos default:
109e670fd5cSchristos goto exit;
110e670fd5cSchristos }
111e670fd5cSchristos }
112e670fd5cSchristos if ( usabilityp != NULL ) {
113e670fd5cSchristos usabilityp->more_info = more_info;
114e670fd5cSchristos }
115e670fd5cSchristos } else {
116e670fd5cSchristos goto exit;
117e670fd5cSchristos }
118e670fd5cSchristos if ( availablep != NULL ) {
119e670fd5cSchristos *availablep = available;
120e670fd5cSchristos }
121e670fd5cSchristos
122e670fd5cSchristos ber_free(ber, 1);
123e670fd5cSchristos
124e670fd5cSchristos ld->ld_errno = LDAP_SUCCESS;
125e670fd5cSchristos return(ld->ld_errno);
126e670fd5cSchristos
127e670fd5cSchristos exit:
128e670fd5cSchristos ber_free(ber, 1);
129e670fd5cSchristos ld->ld_errno = LDAP_DECODING_ERROR;
130e670fd5cSchristos return(ld->ld_errno);
131e670fd5cSchristos }
132e670fd5cSchristos
133e670fd5cSchristos #endif /* LDAP_CONTROL_X_ACCOUNT_USABILITY */
134