xref: /titanic_52/usr/src/cmd/idmap/idmapd/idmapd.h (revision c1ecd8b9404ee0d96d93f02e82c441b9bb149a3d)
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 2008 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #ifndef _IDMAPD_H
27 #define	_IDMAPD_H
28 
29 #pragma ident	"%Z%%M%	%I%	%E% SMI"
30 
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <stdarg.h>
34 #include <rpc/rpc.h>
35 #include <synch.h>
36 #include <thread.h>
37 #include <libintl.h>
38 #include <strings.h>
39 #include <sqlite/sqlite.h>
40 #include <inttypes.h>
41 #include "idmap_prot.h"
42 #include "adutils.h"
43 #include "idmap_config.h"
44 
45 #ifdef __cplusplus
46 extern "C" {
47 #endif
48 
49 /* States a server can be in wrt request */
50 #define	_IDLE	0
51 #define	_SERVED	1
52 
53 #define	SENTINEL_PID	UINT32_MAX
54 #define	CHECK_NULL(s)	(s != NULL ? s : "null")
55 
56 extern int _rpcsvcstate;	/* set when a request is serviced */
57 extern int _rpcsvccount;	/* number of requests being serviced */
58 extern mutex_t _svcstate_lock;	/* lock for _rpcsvcstate, _rpcsvccount */
59 
60 typedef enum idmap_namemap_mode {
61 	IDMAP_NM_NONE = 0,
62 	IDMAP_NM_AD,
63 	IDMAP_NM_NLDAP,
64 	IDMAP_NM_MIXED
65 } idmap_namemap_mode_t;
66 
67 /*
68  * Global state of idmapd daemon.
69  */
70 #define	IDMAP_MAX_NAME_LEN	512
71 typedef struct idmapd_state {
72 	rwlock_t	rwlk_cfg;		/* config lock */
73 	idmap_cfg_t	*cfg;			/* config */
74 	bool_t		daemon_mode;
75 	bool_t		debug_mode;
76 	char		hostname[MAX_NAME_LEN];	/* my hostname */
77 	uid_t		next_uid;
78 	gid_t		next_gid;
79 	uid_t		limit_uid;
80 	gid_t		limit_gid;
81 	int		new_eph_db;	/* was the ephem ID db [re-]created? */
82 	bool_t		eph_map_unres_sids;
83 	ad_t		*ad;
84 } idmapd_state_t;
85 extern idmapd_state_t	_idmapdstate;
86 
87 #define	RDLOCK_CONFIG() \
88 	(void) rw_rdlock(&_idmapdstate.rwlk_cfg);
89 #define	WRLOCK_CONFIG() \
90 	(void) rw_wrlock(&_idmapdstate.rwlk_cfg);
91 #define	UNLOCK_CONFIG() \
92 	(void) rw_unlock(&_idmapdstate.rwlk_cfg);
93 
94 typedef struct hashentry {
95 	uint_t	key;
96 	uint_t	next;
97 } hashentry_t;
98 
99 typedef struct lookup_state {
100 	bool_t			sid2pid_done;
101 	bool_t			pid2sid_done;
102 	int			ad_nqueries;
103 	int			nldap_nqueries;
104 	bool_t			eph_map_unres_sids;
105 	uint_t			curpos;
106 	hashentry_t		*sid_history;
107 	uint_t			sid_history_size;
108 	idmap_mapping_batch	*batch;
109 	idmap_ids_res		*result;
110 	idmap_namemap_mode_t	nm_siduid;
111 	idmap_namemap_mode_t	nm_sidgid;
112 	char			*ad_unixuser_attr;
113 	char			*ad_unixgroup_attr;
114 	char			*nldap_winname_attr;
115 	char			*defdom;
116 	sqlite			*cache;
117 	sqlite			*db;
118 } lookup_state_t;
119 
120 #define	NLDAP_OR_MIXED(nm) \
121 	(nm == IDMAP_NM_NLDAP || nm == IDMAP_NM_MIXED)
122 #define	AD_OR_MIXED(nm) \
123 	(nm == IDMAP_NM_AD || nm == IDMAP_NM_MIXED)
124 
125 #define	NLDAP_OR_MIXED_MODE(pidtype, ls) \
126 	((pidtype == IDMAP_UID && NLDAP_OR_MIXED(ls->nm_siduid)) || \
127 	(pidtype == IDMAP_GID && NLDAP_OR_MIXED(ls->nm_sidgid)))
128 #define	AD_OR_MIXED_MODE(pidtype, ls)\
129 	((pidtype == IDMAP_UID && AD_OR_MIXED(ls->nm_siduid)) || \
130 	(pidtype == IDMAP_GID && AD_OR_MIXED(ls->nm_sidgid)))
131 #define	NLDAP_MODE(pidtype, ls) \
132 	((pidtype == IDMAP_UID && ls->nm_siduid == IDMAP_NM_NLDAP) || \
133 	(pidtype == IDMAP_GID && ls->nm_sidgid == IDMAP_NM_NLDAP))
134 #define	AD_MODE(pidtype, ls) \
135 	((pidtype == IDMAP_UID && ls->nm_siduid == IDMAP_NM_AD) || \
136 	(pidtype == IDMAP_GID && ls->nm_sidgid == IDMAP_NM_AD))
137 #define	MIXED_MODE(pidtype, ls) \
138 	((pidtype == IDMAP_UID && ls->nm_siduid == IDMAP_NM_MIXED) || \
139 	(pidtype == IDMAP_GID && ls->nm_sidgid == IDMAP_NM_MIXED))
140 
141 
142 typedef struct list_cb_data {
143 	void		*result;
144 	uint64_t	next;
145 	uint64_t	len;
146 	uint64_t	limit;
147 	int		flag;
148 } list_cb_data_t;
149 
150 typedef struct msg_table {
151 	idmap_retcode	retcode;
152 	const char	*msg;
153 } msg_table_t;
154 
155 #define	IDMAPD_SEARCH_TIMEOUT		3   /* seconds */
156 #define	IDMAPD_LDAP_OPEN_TIMEOUT	1   /* secs; initial, w/ exp backoff */
157 
158 /*
159  * The following flags are used by idmapd while processing a
160  * given mapping request. Note that idmapd uses multiple passes to
161  * process the request and the flags are used to pass information
162  * about the state of the request between these passes.
163  */
164 
165 /* Initial state. Done. Reset all flags. Remaining passes can be skipped */
166 #define	_IDMAP_F_DONE			0x00000000
167 /* Set when subsequent passes are required */
168 #define	_IDMAP_F_NOTDONE		0x00000001
169 /* Don't update name_cache. (e.g. set when winname,SID found in name_cache) */
170 #define	_IDMAP_F_DONT_UPDATE_NAMECACHE	0x00000002
171 /* Batch this request for AD lookup */
172 #define	_IDMAP_F_LOOKUP_AD		0x00000004
173 /* Batch this request for nldap directory lookup */
174 #define	_IDMAP_F_LOOKUP_NLDAP		0x00000008
175 /*
176  * Expired ephemeral mapping found in cache when processing sid2uid request.
177  * Use it if the given SID cannot be mapped by name
178  */
179 #define	_IDMAP_F_EXP_EPH_UID		0x00000010
180 /* Same as above. Used for sid2gid request */
181 #define	_IDMAP_F_EXP_EPH_GID		0x00000020
182 
183 /*
184  * Check if we are done. If so, subsequent passes can be skipped
185  * when processing a given mapping request.
186  */
187 #define	ARE_WE_DONE(f)	((f & _IDMAP_F_NOTDONE) == 0)
188 
189 #define	SIZE_INCR	5
190 #define	MAX_TRIES	5
191 #define	IDMAP_DBDIR	"/var/idmap"
192 #define	IDMAP_CACHEDIR	"/var/run/idmap"
193 #define	IDMAP_DBNAME	IDMAP_DBDIR "/idmap.db"
194 #define	IDMAP_CACHENAME	IDMAP_CACHEDIR "/idmap.db"
195 
196 #define	IS_BATCH_SID(batch, i) \
197 	(batch.idmap_mapping_batch_val[i].id1.idtype == IDMAP_SID ||	\
198 	batch.idmap_mapping_batch_val[i].id1.idtype == IDMAP_USID ||	\
199 	batch.idmap_mapping_batch_val[i].id1.idtype == IDMAP_GSID)
200 
201 #define	IS_BATCH_UID(batch, i) \
202 	(batch.idmap_mapping_batch_val[i].id1.idtype == IDMAP_UID)
203 
204 #define	IS_BATCH_GID(batch, i) \
205 	(batch.idmap_mapping_batch_val[i].id1.idtype == IDMAP_GID)
206 
207 #define	IS_REQUEST_SID(req, n) \
208 	((req).id##n.idtype == IDMAP_SID ||	\
209 	(req).id##n.idtype == IDMAP_USID ||	\
210 	(req).id##n.idtype == IDMAP_GSID)	\
211 
212 
213 #define	IS_REQUEST_UID(request) \
214 	((request).id1.idtype == IDMAP_UID)
215 
216 #define	IS_REQUEST_GID(request) \
217 	((request).id1.idtype == IDMAP_GID)
218 
219 typedef idmap_retcode (*update_list_res_cb)(void *, const char **, uint64_t);
220 typedef int (*list_svc_cb)(void *, int, char **, char **);
221 
222 extern void	idmap_prog_1(struct svc_req *, register SVCXPRT *);
223 extern void	idmapdlog(int, const char *, ...);
224 extern int	init_mapping_system();
225 extern void	fini_mapping_system();
226 extern void	print_idmapdstate();
227 extern int	create_directory(const char *, uid_t, gid_t);
228 extern int	load_config();
229 extern void	reload_ad();
230 extern int	idmap_init_tsd_key(void);
231 extern void	degrade_svc(int, const char *);
232 extern void	restore_svc(void);
233 
234 
235 extern int		init_dbs();
236 extern void		fini_dbs();
237 extern idmap_retcode	get_db_handle(sqlite **);
238 extern idmap_retcode	get_cache_handle(sqlite **);
239 extern idmap_retcode	sql_exec_no_cb(sqlite *, const char *, char *);
240 extern idmap_retcode	add_namerule(sqlite *, idmap_namerule *);
241 extern idmap_retcode	rm_namerule(sqlite *, idmap_namerule *);
242 extern idmap_retcode	flush_namerules(sqlite *);
243 
244 extern char 		*tolower_u8(const char *);
245 
246 extern idmap_retcode	gen_sql_expr_from_rule(idmap_namerule *, char **);
247 extern idmap_retcode	validate_list_cb_data(list_cb_data_t *, int,
248 				char **, int, uchar_t **, size_t);
249 extern idmap_retcode	process_list_svc_sql(sqlite *, const char *, char *,
250 				uint64_t, int, list_svc_cb, void *);
251 extern idmap_retcode	sid2pid_first_pass(lookup_state_t *,
252 				idmap_mapping *, idmap_id_res *);
253 extern idmap_retcode	sid2pid_second_pass(lookup_state_t *,
254 				idmap_mapping *, idmap_id_res *);
255 extern idmap_retcode	pid2sid_first_pass(lookup_state_t *,
256 				idmap_mapping *, idmap_id_res *, int, int);
257 extern idmap_retcode	pid2sid_second_pass(lookup_state_t *,
258 				idmap_mapping *, idmap_id_res *, int);
259 extern idmap_retcode	update_cache_sid2pid(lookup_state_t *,
260 				idmap_mapping *, idmap_id_res *);
261 extern idmap_retcode	update_cache_pid2sid(lookup_state_t *,
262 				idmap_mapping *, idmap_id_res *);
263 extern idmap_retcode	get_u2w_mapping(sqlite *, sqlite *, idmap_mapping *,
264 				idmap_mapping *, int);
265 extern idmap_retcode	get_w2u_mapping(sqlite *, sqlite *, idmap_mapping *,
266 				idmap_mapping *);
267 extern idmap_retcode	load_cfg_in_state(lookup_state_t *);
268 extern void		cleanup_lookup_state(lookup_state_t *);
269 
270 extern idmap_retcode	ad_lookup_batch(lookup_state_t *,
271 				idmap_mapping_batch *, idmap_ids_res *);
272 extern idmap_retcode	lookup_name2sid(sqlite *, const char *, const char *,
273 				int *, char **, char **, idmap_rid_t *,
274 				idmap_mapping *, int);
275 extern idmap_retcode	lookup_wksids_name2sid(const char *, char **, char **,
276 				idmap_rid_t *, int *);
277 
278 #ifdef __cplusplus
279 }
280 #endif
281 
282 #endif /* _IDMAPD_H */
283