xref: /titanic_44/usr/src/cmd/idmap/idmapd/adutils.h (revision c5c4113dfcabb1eed3d4bdf7609de5170027a794)
1*c5c4113dSnw141292 /*
2*c5c4113dSnw141292  * CDDL HEADER START
3*c5c4113dSnw141292  *
4*c5c4113dSnw141292  * The contents of this file are subject to the terms of the
5*c5c4113dSnw141292  * Common Development and Distribution License (the "License").
6*c5c4113dSnw141292  * You may not use this file except in compliance with the License.
7*c5c4113dSnw141292  *
8*c5c4113dSnw141292  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9*c5c4113dSnw141292  * or http://www.opensolaris.org/os/licensing.
10*c5c4113dSnw141292  * See the License for the specific language governing permissions
11*c5c4113dSnw141292  * and limitations under the License.
12*c5c4113dSnw141292  *
13*c5c4113dSnw141292  * When distributing Covered Code, include this CDDL HEADER in each
14*c5c4113dSnw141292  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15*c5c4113dSnw141292  * If applicable, add the following below this CDDL HEADER, with the
16*c5c4113dSnw141292  * fields enclosed by brackets "[]" replaced with your own identifying
17*c5c4113dSnw141292  * information: Portions Copyright [yyyy] [name of copyright owner]
18*c5c4113dSnw141292  *
19*c5c4113dSnw141292  * CDDL HEADER END
20*c5c4113dSnw141292  */
21*c5c4113dSnw141292 
22*c5c4113dSnw141292 /*
23*c5c4113dSnw141292  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
24*c5c4113dSnw141292  * Use is subject to license terms.
25*c5c4113dSnw141292  */
26*c5c4113dSnw141292 
27*c5c4113dSnw141292 #ifndef _ADUTILS_H
28*c5c4113dSnw141292 #define	_ADUTILS_H
29*c5c4113dSnw141292 
30*c5c4113dSnw141292 #pragma ident	"%Z%%M%	%I%	%E% SMI"
31*c5c4113dSnw141292 
32*c5c4113dSnw141292 #ifdef __cplusplus
33*c5c4113dSnw141292 extern "C" {
34*c5c4113dSnw141292 #endif
35*c5c4113dSnw141292 
36*c5c4113dSnw141292 /*
37*c5c4113dSnw141292  * Processes name2sid & sid2name lookups for a given user or computer
38*c5c4113dSnw141292  * from an AD Difrectory server using GSSAPI authentication
39*c5c4113dSnw141292  */
40*c5c4113dSnw141292 
41*c5c4113dSnw141292 #include <stdio.h>
42*c5c4113dSnw141292 #include <stdlib.h>
43*c5c4113dSnw141292 #include <unistd.h>
44*c5c4113dSnw141292 #include <lber.h>
45*c5c4113dSnw141292 #include <ldap.h>
46*c5c4113dSnw141292 #include <sasl/sasl.h>
47*c5c4113dSnw141292 #include <string.h>
48*c5c4113dSnw141292 #include <ctype.h>
49*c5c4113dSnw141292 #include <sys/types.h>
50*c5c4113dSnw141292 #include <time.h>
51*c5c4113dSnw141292 #include <thread.h>
52*c5c4113dSnw141292 #include <synch.h>
53*c5c4113dSnw141292 #include "idmap_prot.h"
54*c5c4113dSnw141292 #include <sys/idmap.h>
55*c5c4113dSnw141292 
56*c5c4113dSnw141292 /*
57*c5c4113dSnw141292  * idmapd interfaces stolen? from other idmapd code?
58*c5c4113dSnw141292  */
59*c5c4113dSnw141292 
60*c5c4113dSnw141292 /*
61*c5c4113dSnw141292  * Eventually these should be an enum here, but instead we share a
62*c5c4113dSnw141292  * namespace with other things in idmapd.
63*c5c4113dSnw141292  */
64*c5c4113dSnw141292 #define	_IDMAP_T_OTHER		0
65*c5c4113dSnw141292 #define	_IDMAP_T_USER		-1004
66*c5c4113dSnw141292 #define	_IDMAP_T_GROUP		-1005
67*c5c4113dSnw141292 #define	_IDMAP_T_DOMAIN		-1006
68*c5c4113dSnw141292 #define	_IDMAP_T_COMPUTER	-1007
69*c5c4113dSnw141292 
70*c5c4113dSnw141292 #define	SID_MAX_SUB_AUTHORITIES	15
71*c5c4113dSnw141292 #define	MAXBINSID	(1 + 1 + 6 + (SID_MAX_SUB_AUTHORITIES * 4))
72*c5c4113dSnw141292 #define	MAXHEXBINSID	(MAXBINSID * 3)
73*c5c4113dSnw141292 
74*c5c4113dSnw141292 typedef uint32_t rid_t;
75*c5c4113dSnw141292 
76*c5c4113dSnw141292 /*
77*c5c4113dSnw141292  * We use the port numbers for normal LDAP and global catalog LDAP as
78*c5c4113dSnw141292  * the enum values for this enumeration.  Clever?  Silly?  You decide.
79*c5c4113dSnw141292  * Although we never actually use these enum values as port numbers and
80*c5c4113dSnw141292  * never will, so this is just cute.
81*c5c4113dSnw141292  */
82*c5c4113dSnw141292 typedef enum idmap_ad_partition {
83*c5c4113dSnw141292 	IDMAP_AD_DATA = 389,
84*c5c4113dSnw141292 	IDMAP_AD_GLOBAL_CATALOG = 3268
85*c5c4113dSnw141292 } idmap_ad_partition_t;
86*c5c4113dSnw141292 
87*c5c4113dSnw141292 typedef struct ad ad_t;
88*c5c4113dSnw141292 typedef struct idmap_query_state idmap_query_state_t;
89*c5c4113dSnw141292 
90*c5c4113dSnw141292 /*
91*c5c4113dSnw141292  * Idmap interfaces:
92*c5c4113dSnw141292  *
93*c5c4113dSnw141292  *  - an ad_t represents an AD partition
94*c5c4113dSnw141292  *  - a DS (hostname + port, if port != 0) can be added/removed from an ad_t
95*c5c4113dSnw141292  *  - and because libldap supports space-separated lists of servers, a
96*c5c4113dSnw141292  *  single hostname value can actually be a set of hostnames.
97*c5c4113dSnw141292  *  - an ad_t can be allocated, ref'ed and released; last release
98*c5c4113dSnw141292  *  releases resources
99*c5c4113dSnw141292  *
100*c5c4113dSnw141292  *  - lookups are batched; see below.
101*c5c4113dSnw141292  *
102*c5c4113dSnw141292  * See below.
103*c5c4113dSnw141292  */
104*c5c4113dSnw141292 
105*c5c4113dSnw141292 /* Allocate/release ad_t objects */
106*c5c4113dSnw141292 int idmap_ad_alloc(ad_t **new_ad, const char *default_domain,
107*c5c4113dSnw141292 		idmap_ad_partition_t part);
108*c5c4113dSnw141292 void idmap_ad_free(ad_t **ad);
109*c5c4113dSnw141292 
110*c5c4113dSnw141292 /* Add/remove a DS to/from an ad_t */
111*c5c4113dSnw141292 int idmap_add_ds(ad_t *ad, const char *host, int port);
112*c5c4113dSnw141292 void idmap_delete_ds(ad_t *ad, const char *host, int port);
113*c5c4113dSnw141292 
114*c5c4113dSnw141292 /*
115*c5c4113dSnw141292  * Batch lookups
116*c5c4113dSnw141292  *
117*c5c4113dSnw141292  * Start a batch, add queries to the batch one by one (the output
118*c5c4113dSnw141292  * pointers should all differ, so that a query's results don't clobber
119*c5c4113dSnw141292  * any other's), end the batch to wait for replies for all outstanding
120*c5c4113dSnw141292  * queries.  The output parameters of each query are initialized to NULL
121*c5c4113dSnw141292  * or -1 as appropriate.
122*c5c4113dSnw141292  *
123*c5c4113dSnw141292  * LDAP searches are sent one by one without waiting (i.e., blocking)
124*c5c4113dSnw141292  * for replies.  Replies are handled as soon as they are available.
125*c5c4113dSnw141292  * Missing replies are waited for only when idmap_lookup_batch_end() is
126*c5c4113dSnw141292  * called.
127*c5c4113dSnw141292  *
128*c5c4113dSnw141292  * If an add1 function returns != 0 then abort the batch by calling
129*c5c4113dSnw141292  * idmap_lookup_batch_end(), but note that some queries may have been
130*c5c4113dSnw141292  * answered, so check the result code of each query.
131*c5c4113dSnw141292  */
132*c5c4113dSnw141292 
133*c5c4113dSnw141292 /* Start a batch of lookups */
134*c5c4113dSnw141292 idmap_retcode idmap_lookup_batch_start(ad_t *ad, int nqueries,
135*c5c4113dSnw141292 		idmap_query_state_t **state);
136*c5c4113dSnw141292 
137*c5c4113dSnw141292 /* End a batch and release its idmap_query_state_t object */
138*c5c4113dSnw141292 idmap_retcode idmap_lookup_batch_end(idmap_query_state_t **state,
139*c5c4113dSnw141292 		struct timeval *timeout);
140*c5c4113dSnw141292 
141*c5c4113dSnw141292 /* Abandon a batch and release its idmap_query_state_t object */
142*c5c4113dSnw141292 void idmap_lookup_free_batch(idmap_query_state_t **state);
143*c5c4113dSnw141292 
144*c5c4113dSnw141292 /*
145*c5c4113dSnw141292  * Add a name->SID lookup
146*c5c4113dSnw141292  *
147*c5c4113dSnw141292  *  - 'dname' is optional; if NULL or empty string then 'name' has to be
148*c5c4113dSnw141292  *  a user/group name qualified wih a domainname (e.g., foo@domain),
149*c5c4113dSnw141292  *  else the 'name' must not be qualified and the domainname must be
150*c5c4113dSnw141292  *  passed in 'dname'.
151*c5c4113dSnw141292  *
152*c5c4113dSnw141292  *  - if 'rid' is NULL then the output SID string will include the last
153*c5c4113dSnw141292  *  RID, else it won't and the last RID value will be stored in *rid.
154*c5c4113dSnw141292  *
155*c5c4113dSnw141292  *  The caller must free() *sid.
156*c5c4113dSnw141292  */
157*c5c4113dSnw141292 idmap_retcode idmap_name2sid_batch_add1(idmap_query_state_t *state,
158*c5c4113dSnw141292 		const char *name, const char *dname,
159*c5c4113dSnw141292 		char **sid, rid_t *rid, int *sid_type, idmap_retcode *rc);
160*c5c4113dSnw141292 /*
161*c5c4113dSnw141292  * Add a SID->name lookup
162*c5c4113dSnw141292  *
163*c5c4113dSnw141292  *  - 'rid' is optional; if NULL then 'sid' is expected to have the
164*c5c4113dSnw141292  *  user/group RID present, else 'sid' is expected not to have it, and
165*c5c4113dSnw141292  *  *rid will be used to qualify the given 'sid'
166*c5c4113dSnw141292  *
167*c5c4113dSnw141292  *  - 'dname' is optional; if NULL then the fully qualified user/group
168*c5c4113dSnw141292  *  name will be stored in *name, else the domain name will be stored in
169*c5c4113dSnw141292  *  *dname and the user/group name will be stored in *name without a
170*c5c4113dSnw141292  *  domain qualifier.
171*c5c4113dSnw141292  *
172*c5c4113dSnw141292  *  The caller must free() *name and *dname (if present).
173*c5c4113dSnw141292  */
174*c5c4113dSnw141292 idmap_retcode idmap_sid2name_batch_add1(idmap_query_state_t *state,
175*c5c4113dSnw141292 		const char *sid, const rid_t *rid,
176*c5c4113dSnw141292 		char **name, char **dname, int *sid_type, idmap_retcode *rc);
177*c5c4113dSnw141292 
178*c5c4113dSnw141292 #ifdef __cplusplus
179*c5c4113dSnw141292 }
180*c5c4113dSnw141292 #endif
181*c5c4113dSnw141292 
182*c5c4113dSnw141292 #endif	/* _ADUTILS_H */
183