xref: /titanic_50/usr/src/lib/libadutils/common/libadutils.h (revision 2b4a78020b9c38d1b95e2f3fefa6d6e4be382d1f)
1*2b4a7802SBaban Kenkre /*
2*2b4a7802SBaban Kenkre  * CDDL HEADER START
3*2b4a7802SBaban Kenkre  *
4*2b4a7802SBaban Kenkre  * The contents of this file are subject to the terms of the
5*2b4a7802SBaban Kenkre  * Common Development and Distribution License (the "License").
6*2b4a7802SBaban Kenkre  * You may not use this file except in compliance with the License.
7*2b4a7802SBaban Kenkre  *
8*2b4a7802SBaban Kenkre  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9*2b4a7802SBaban Kenkre  * or http://www.opensolaris.org/os/licensing.
10*2b4a7802SBaban Kenkre  * See the License for the specific language governing permissions
11*2b4a7802SBaban Kenkre  * and limitations under the License.
12*2b4a7802SBaban Kenkre  *
13*2b4a7802SBaban Kenkre  * When distributing Covered Code, include this CDDL HEADER in each
14*2b4a7802SBaban Kenkre  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15*2b4a7802SBaban Kenkre  * If applicable, add the following below this CDDL HEADER, with the
16*2b4a7802SBaban Kenkre  * fields enclosed by brackets "[]" replaced with your own identifying
17*2b4a7802SBaban Kenkre  * information: Portions Copyright [yyyy] [name of copyright owner]
18*2b4a7802SBaban Kenkre  *
19*2b4a7802SBaban Kenkre  * CDDL HEADER END
20*2b4a7802SBaban Kenkre  */
21*2b4a7802SBaban Kenkre /*
22*2b4a7802SBaban Kenkre  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
23*2b4a7802SBaban Kenkre  * Use is subject to license terms.
24*2b4a7802SBaban Kenkre  */
25*2b4a7802SBaban Kenkre 
26*2b4a7802SBaban Kenkre #ifndef	_LIBADUTILS_H
27*2b4a7802SBaban Kenkre #define	_LIBADUTILS_H
28*2b4a7802SBaban Kenkre 
29*2b4a7802SBaban Kenkre #include <stdlib.h>
30*2b4a7802SBaban Kenkre #include <stdio.h>
31*2b4a7802SBaban Kenkre #include <sys/types.h>
32*2b4a7802SBaban Kenkre #include <rpc/rpc.h>
33*2b4a7802SBaban Kenkre #include <ldap.h>
34*2b4a7802SBaban Kenkre 
35*2b4a7802SBaban Kenkre #ifdef	__cplusplus
36*2b4a7802SBaban Kenkre extern "C" {
37*2b4a7802SBaban Kenkre #endif
38*2b4a7802SBaban Kenkre 
39*2b4a7802SBaban Kenkre #define	ADUTILS_DEF_NUM_RETRIES	2
40*2b4a7802SBaban Kenkre #define	ADUTILS_SID_MAX_SUB_AUTHORITIES	15
41*2b4a7802SBaban Kenkre #define	ADUTILS_MAXBINSID\
42*2b4a7802SBaban Kenkre 	(1 + 1 + 6 + (ADUTILS_SID_MAX_SUB_AUTHORITIES * 4))
43*2b4a7802SBaban Kenkre #define	ADUTILS_MAXHEXBINSID	(ADUTILS_MAXBINSID * 3)
44*2b4a7802SBaban Kenkre 
45*2b4a7802SBaban Kenkre typedef struct adutils_ad adutils_ad_t;
46*2b4a7802SBaban Kenkre typedef struct adutils_entry adutils_entry_t;
47*2b4a7802SBaban Kenkre typedef struct adutils_result adutils_result_t;
48*2b4a7802SBaban Kenkre typedef struct adutils_ctx adutils_ctx_t;
49*2b4a7802SBaban Kenkre typedef struct adutils_query_state adutils_query_state_t;
50*2b4a7802SBaban Kenkre 
51*2b4a7802SBaban Kenkre /*
52*2b4a7802SBaban Kenkre  * Typedef for callback routine for adutils_lookup_batch_start.
53*2b4a7802SBaban Kenkre  * This callback routine is used to process the result of
54*2b4a7802SBaban Kenkre  * ldap_result(3LDAP).
55*2b4a7802SBaban Kenkre  *	ld   - LDAP handle used by ldap_result(3LDAP)
56*2b4a7802SBaban Kenkre  *	res  - Entry returned by ldap_result(3LDAP)
57*2b4a7802SBaban Kenkre  *	rc   - Return value of ldap_result(3LDAP)
58*2b4a7802SBaban Kenkre  *	qid  - Query ID that corresponds to the result.
59*2b4a7802SBaban Kenkre  *	argp - Argument passed by the caller at the time
60*2b4a7802SBaban Kenkre  *	       of adutils_lookup_batch_start.
61*2b4a7802SBaban Kenkre  */
62*2b4a7802SBaban Kenkre typedef void (*adutils_ldap_res_search_cb)(LDAP *ld, LDAPMessage **res,
63*2b4a7802SBaban Kenkre 	int rc, int qid, void *argp);
64*2b4a7802SBaban Kenkre 
65*2b4a7802SBaban Kenkre typedef enum {
66*2b4a7802SBaban Kenkre 	ADUTILS_SUCCESS = 0,
67*2b4a7802SBaban Kenkre 	ADUTILS_ERR_INTERNAL = -10000,
68*2b4a7802SBaban Kenkre 	ADUTILS_ERR_OTHER,
69*2b4a7802SBaban Kenkre 	ADUTILS_ERR_NOTFOUND,
70*2b4a7802SBaban Kenkre 	ADUTILS_ERR_RETRIABLE_NET_ERR,
71*2b4a7802SBaban Kenkre 	ADUTILS_ERR_MEMORY,
72*2b4a7802SBaban Kenkre 	ADUTILS_ERR_DOMAIN
73*2b4a7802SBaban Kenkre } adutils_rc;
74*2b4a7802SBaban Kenkre 
75*2b4a7802SBaban Kenkre /*
76*2b4a7802SBaban Kenkre  * We use the port numbers for normal LDAP and global catalog LDAP as
77*2b4a7802SBaban Kenkre  * the enum values for this enumeration.  Clever?  Silly?  You decide.
78*2b4a7802SBaban Kenkre  * Although we never actually use these enum values as port numbers and
79*2b4a7802SBaban Kenkre  * never will, so this is just cute.
80*2b4a7802SBaban Kenkre  */
81*2b4a7802SBaban Kenkre typedef enum adutils_ad_partition {
82*2b4a7802SBaban Kenkre 	ADUTILS_AD_DATA = 389,
83*2b4a7802SBaban Kenkre 	ADUTILS_AD_GLOBAL_CATALOG = 3268
84*2b4a7802SBaban Kenkre } adutils_ad_partition_t;
85*2b4a7802SBaban Kenkre 
86*2b4a7802SBaban Kenkre 
87*2b4a7802SBaban Kenkre /*
88*2b4a7802SBaban Kenkre  * adutils interfaces:
89*2b4a7802SBaban Kenkre  *
90*2b4a7802SBaban Kenkre  *  - an adutils_ad_t represents an AD partition
91*2b4a7802SBaban Kenkre  *  - a DS (hostname + port, if port != 0) can be added/removed from an
92*2b4a7802SBaban Kenkre  *  adutils_ad_t
93*2b4a7802SBaban Kenkre  *  - an adutils_ad_t can be allocated, ref'ed and released; last release
94*2b4a7802SBaban Kenkre  *  releases resources
95*2b4a7802SBaban Kenkre  *
96*2b4a7802SBaban Kenkre  *
97*2b4a7802SBaban Kenkre  * adutils_lookup_batch_xxx interfaces:
98*2b4a7802SBaban Kenkre  *
99*2b4a7802SBaban Kenkre  * These interfaces allow the caller to batch AD lookup requests. The
100*2b4a7802SBaban Kenkre  * batched requests are processed asynchronously. The actual lookup
101*2b4a7802SBaban Kenkre  * is currently implement using libldap's ldap_search_ext(3LDAP) and
102*2b4a7802SBaban Kenkre  * ldap_result(3LDAP) APIs.
103*2b4a7802SBaban Kenkre  *
104*2b4a7802SBaban Kenkre  *	Example:
105*2b4a7802SBaban Kenkre  *      	adutils_query_state_t	*qs;
106*2b4a7802SBaban Kenkre  *      	adutils_lookup_batch_start(..., &qs);
107*2b4a7802SBaban Kenkre  *		for each request {
108*2b4a7802SBaban Kenkre  *			rc = adutils_lookup_batch_add(qs, ...);
109*2b4a7802SBaban Kenkre  *			if (rc != success)
110*2b4a7802SBaban Kenkre  *				break;
111*2b4a7802SBaban Kenkre  *		}
112*2b4a7802SBaban Kenkre  *		if (rc == success)
113*2b4a7802SBaban Kenkre  *			adutils_lookup_batch_end(&qs);
114*2b4a7802SBaban Kenkre  *		else
115*2b4a7802SBaban Kenkre  *			adutils_lookup_batch_release(&qs);
116*2b4a7802SBaban Kenkre  *
117*2b4a7802SBaban Kenkre  *	The adutils_lookup_batch_start interface allows the caller to pass
118*2b4a7802SBaban Kenkre  *	in a callback function that's invoked when ldap_result() returns
119*2b4a7802SBaban Kenkre  *	LDAP_RES_SEARCH_RESULT and LDAP_RES_SEARCH_ENTRY for each request.
120*2b4a7802SBaban Kenkre  *
121*2b4a7802SBaban Kenkre  *	If no callback is provided then adutils batch API falls back to its
122*2b4a7802SBaban Kenkre  *	default behaviour which is:
123*2b4a7802SBaban Kenkre  *		For LDAP_RES_SEARCH_ENTRY, add the entry to the entry set.
124*2b4a7802SBaban Kenkre  *		For LDAP_RES_SEARCH_RESULT, set return code to
125*2b4a7802SBaban Kenkre  *			ADUTILS_ERR_NOTFOUND if the entry set is empty.
126*2b4a7802SBaban Kenkre  *
127*2b4a7802SBaban Kenkre  *	See $SRC/cmd/idmap/idmapd/adutils.c for an example of
128*2b4a7802SBaban Kenkre  *      non-default callback routine.
129*2b4a7802SBaban Kenkre  *
130*2b4a7802SBaban Kenkre  */
131*2b4a7802SBaban Kenkre 
132*2b4a7802SBaban Kenkre extern adutils_rc	adutils_ad_alloc(adutils_ad_t **new_ad,
133*2b4a7802SBaban Kenkre 				const char *default_domain,
134*2b4a7802SBaban Kenkre 				adutils_ad_partition_t part);
135*2b4a7802SBaban Kenkre extern void		adutils_ad_free(adutils_ad_t **ad);
136*2b4a7802SBaban Kenkre extern adutils_rc	adutils_add_ds(adutils_ad_t *ad,
137*2b4a7802SBaban Kenkre 				const char *host, int port);
138*2b4a7802SBaban Kenkre extern void		adutils_set_log(int pri, bool_t syslog,
139*2b4a7802SBaban Kenkre 				bool_t degraded);
140*2b4a7802SBaban Kenkre extern void		adutils_freeresult(adutils_result_t **result);
141*2b4a7802SBaban Kenkre extern adutils_rc	adutils_lookup(adutils_ad_t *ad,
142*2b4a7802SBaban Kenkre 				const char *searchfilter,
143*2b4a7802SBaban Kenkre 				const char **attrs, const char *domain,
144*2b4a7802SBaban Kenkre 				adutils_result_t **result);
145*2b4a7802SBaban Kenkre extern char		**adutils_getattr(const adutils_entry_t *entry,
146*2b4a7802SBaban Kenkre 				const char *attrname);
147*2b4a7802SBaban Kenkre extern const adutils_entry_t	*adutils_getfirstentry(
148*2b4a7802SBaban Kenkre 					adutils_result_t *result);
149*2b4a7802SBaban Kenkre extern int		adutils_txtsid2hexbinsid(const char *txt,
150*2b4a7802SBaban Kenkre 				const uint32_t *rid,
151*2b4a7802SBaban Kenkre 				char *hexbinsid, int hexbinsidlen);
152*2b4a7802SBaban Kenkre extern char		*adutils_bv_name2str(BerValue *bval);
153*2b4a7802SBaban Kenkre extern char		*adutils_bv_objsid2sidstr(BerValue *bval,
154*2b4a7802SBaban Kenkre 				uint32_t *rid);
155*2b4a7802SBaban Kenkre extern void		adutils_reap_idle_connections(void);
156*2b4a7802SBaban Kenkre extern char		*adutils_dn2dns(const char *dn);
157*2b4a7802SBaban Kenkre extern adutils_rc	adutils_lookup_batch_start(adutils_ad_t *ad,
158*2b4a7802SBaban Kenkre 				int nqueries,
159*2b4a7802SBaban Kenkre 				adutils_ldap_res_search_cb ldap_res_search_cb,
160*2b4a7802SBaban Kenkre 				void *ldap_res_search_argp,
161*2b4a7802SBaban Kenkre 				adutils_query_state_t **state);
162*2b4a7802SBaban Kenkre extern adutils_rc	adutils_lookup_batch_add(adutils_query_state_t *state,
163*2b4a7802SBaban Kenkre 				const char *filter, const char **attrs,
164*2b4a7802SBaban Kenkre 				const char *edomain, adutils_result_t **result,
165*2b4a7802SBaban Kenkre 				adutils_rc *rc);
166*2b4a7802SBaban Kenkre extern adutils_rc	adutils_lookup_batch_end(
167*2b4a7802SBaban Kenkre 				adutils_query_state_t **state);
168*2b4a7802SBaban Kenkre extern void		adutils_lookup_batch_release(
169*2b4a7802SBaban Kenkre 				adutils_query_state_t **state);
170*2b4a7802SBaban Kenkre extern const char	*adutils_lookup_batch_getdefdomain(
171*2b4a7802SBaban Kenkre 				adutils_query_state_t *state);
172*2b4a7802SBaban Kenkre 
173*2b4a7802SBaban Kenkre #ifdef	__cplusplus
174*2b4a7802SBaban Kenkre }
175*2b4a7802SBaban Kenkre #endif
176*2b4a7802SBaban Kenkre 
177*2b4a7802SBaban Kenkre #endif	/* _LIBADUTILS_H */
178