1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
23  * Copyright 2014 Nexenta Systems, Inc.  All rights reserved.
24  */
25 
26 #ifndef	_ADUTILS_IMPL_H
27 #define	_ADUTILS_IMPL_H
28 
29 #include <stdlib.h>
30 #include <stdio.h>
31 #include <sys/types.h>
32 #include <ldap.h>
33 #include <pthread.h>
34 #include "addisc.h"
35 #include "libadutils.h"
36 
37 #ifdef	__cplusplus
38 extern "C" {
39 #endif
40 
41 #define	DBG(type, lev)	\
42 	(ad_debug[AD_DEBUG_##type] >= (lev) || \
43 	    ad_debug[AD_DEBUG_ALL] >= (lev))
44 extern int ad_debug[AD_DEBUG_MAX + 1];
45 
46 #define	ADUTILS_SEARCH_TIMEOUT	3
47 #define	ADUTILS_LDAP_OPEN_TIMEOUT	1
48 
49 
50 typedef struct adutils_sid {
51 	uchar_t		version;
52 	uchar_t		sub_authority_count;
53 	uint64_t	authority;  /* really, 48-bits */
54 	uint32_t	sub_authorities[ADUTILS_SID_MAX_SUB_AUTHORITIES];
55 } adutils_sid_t;
56 
57 struct adutils_host;
58 
59 struct known_domain {
60 	char		name[MAXDOMAINNAME];
61 	char		sid[MAXSTRSID];
62 };
63 
64 
65 /* A set of DSs for a given AD partition */
66 struct adutils_ad {
67 	int			num_known_domains;
68 	struct known_domain	*known_domains;
69 	pthread_mutex_t		lock;
70 	uint32_t		ref;
71 	struct adutils_host	*last_adh;
72 	adutils_ad_partition_t	partition;	/* Data or global catalog? */
73 	/* If this is a reference to DC, this is the base DN for that DC */
74 	char			*basedn;
75 };
76 
77 typedef struct adutils_attr {
78 	char	*attr_name;
79 	uint_t	num_values;
80 	char	**attr_values;
81 } adutils_attr_t;
82 
83 /* typedef in libadutils.h */
84 struct adutils_entry {
85 	uint_t			num_nvpairs;
86 	adutils_attr_t		*attr_nvpairs;
87 	struct adutils_entry	*next;
88 };
89 
90 /* typedef in libadutils.h */
91 struct adutils_result {
92 	uint_t		num_entries;
93 	adutils_entry_t	*entries;
94 };
95 
96 /* A single DS */
97 typedef struct adutils_host {
98 	struct adutils_host	*next;
99 	struct adutils_ad	*owner;		/* ad_t to which this belongs */
100 	pthread_mutex_t		lock;
101 	LDAP			*ld;		/* LDAP connection */
102 	uint32_t		ref;		/* ref count */
103 	time_t			idletime;	/* time since last activity */
104 	int			dead;		/* error on LDAP connection */
105 	/*
106 	 * Used to distinguish between different instances of LDAP
107 	 * connections to this same DS.  We need this so we never mix up
108 	 * results for a given msgID from one connection with those of
109 	 * another earlier connection where two batch state structures
110 	 * share this adutils_host object but used different LDAP connections
111 	 * to send their LDAP searches.
112 	 */
113 	uint64_t		generation;
114 
115 	/* LDAP DS info */
116 	char			*host;
117 	int			port;
118 
119 	/* hardwired to SASL GSSAPI only for now */
120 	char			*saslmech;
121 	unsigned		saslflags;
122 
123 	/* Number of outstanding search requests */
124 	uint32_t		max_requests;
125 	uint32_t		num_requests;
126 } adutils_host_t;
127 
128 /*  A place to put the results of a batched (async) query */
129 typedef struct adutils_q {
130 	const char		*edomain;	/* expected domain name */
131 	struct adutils_result	**result;	/* The LDAP search result */
132 	adutils_rc		*rc;
133 	int			msgid;		/* LDAP message ID */
134 } adutils_q_t;
135 
136 /* Batch context structure */
137 struct adutils_query_state {
138 	struct adutils_query_state	*next;
139 	int			qsize;		/* Size of queries */
140 	int			ref_cnt;	/* reference count */
141 	pthread_cond_t		cv;		/* Condition wait variable */
142 	uint32_t		qcount;		/* Number of items queued */
143 	uint32_t		qinflight;	/* how many queries in flight */
144 	uint16_t		qdead;		/* oops, lost LDAP connection */
145 	adutils_host_t		*qadh;		/* LDAP connection */
146 	uint64_t		qadh_gen;	/* same as qadh->generation */
147 	adutils_ldap_res_search_cb ldap_res_search_cb;
148 	void			*ldap_res_search_argp;
149 	adutils_q_t		queries[1];	/* array of query results */
150 };
151 
152 /* Private routines */
153 
154 char *DN_to_DNS(const char *dn_name);
155 
156 int adutils_getsid(BerValue *bval, adutils_sid_t *sidp);
157 
158 char *adutils_sid2txt(adutils_sid_t *sidp);
159 
160 int saslcallback(LDAP *ld, unsigned flags, void *defaults, void *prompts);
161 
162 int adutils_set_thread_functions(LDAP *ld);
163 
164 /* Global logger function */
165 
166 extern adutils_logger logger;
167 
168 #ifdef	__cplusplus
169 }
170 #endif
171 
172 #endif	/* _ADUTILS_IMPL_H */
173