xref: /illumos-gate/usr/src/cmd/idmap/idmapd/idmapd.h (revision cd37da74)
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 2007 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 <syslog.h>
34 #include <stdarg.h>
35 #include <rpc/rpc.h>
36 #include <synch.h>
37 #include <thread.h>
38 #include <libintl.h>
39 #include <strings.h>
40 #include <sqlite/sqlite.h>
41 #include <inttypes.h>
42 #include "idmap_prot.h"
43 #include "adutils.h"
44 #include "idmap_config.h"
45 
46 #ifdef __cplusplus
47 extern "C" {
48 #endif
49 
50 /* States a server can be in wrt request */
51 #define	_IDLE	0
52 #define	_SERVED	1
53 
54 #define	CHECK_NULL(s)	s?s:"null"
55 
56 #define	SENTINEL_PID	UINT32_MAX
57 
58 extern int _rpcsvcstate;	/* set when a request is serviced */
59 extern int _rpcsvccount;	/* number of requests being serviced */
60 extern mutex_t _svcstate_lock;	/* lock for _rpcsvcstate, _rpcsvccount */
61 
62 /*
63  * Global state of idmapd daemon.
64  */
65 #define	IDMAP_MAX_NAME_LEN	512
66 typedef struct idmapd_state {
67 	rwlock_t	rwlk_cfg;		/* config lock */
68 	idmap_cfg_t	*cfg;			/* config */
69 	bool_t		daemon_mode;		/* daemon mode? yes/no */
70 	char		hostname[MAX_NAME_LEN];	/* my hostname */
71 	uid_t		next_uid;
72 	gid_t		next_gid;
73 	uid_t		limit_uid;
74 	gid_t		limit_gid;
75 	int		new_eph_db;	/* was the ephem ID db [re-]created? */
76 	ad_t		*ad;
77 } idmapd_state_t;
78 extern idmapd_state_t	_idmapdstate;
79 
80 #define	RDLOCK_CONFIG() \
81 	(void) rw_rdlock(&_idmapdstate.rwlk_cfg);
82 #define	WRLOCK_CONFIG() \
83 	(void) rw_wrlock(&_idmapdstate.rwlk_cfg);
84 #define	UNLOCK_CONFIG() \
85 	(void) rw_unlock(&_idmapdstate.rwlk_cfg);
86 
87 typedef struct hashentry {
88 	uint_t	key;
89 	uint_t	next;
90 } hashentry_t;
91 
92 typedef struct lookup_state {
93 	bool_t			sid2pid_done;
94 	bool_t			pid2sid_done;
95 	idmap_query_state_t	*ad_lookup;
96 	int			ad_nqueries;
97 	uint_t			curpos;
98 	hashentry_t		*sid_history;
99 	uint_t			sid_history_size;
100 	idmap_mapping_batch	*batch;
101 	idmap_ids_res		*result;
102 } lookup_state_t;
103 
104 typedef struct list_cb_data {
105 	void		*result;
106 	uint64_t	next;
107 	uint64_t	len;
108 	uint64_t	limit;
109 } list_cb_data_t;
110 
111 typedef struct msg_table {
112 	idmap_retcode	retcode;
113 	const char	*msg;
114 } msg_table_t;
115 
116 /*
117  * Data structure to store well-known SIDs and
118  * associated mappings (if any)
119  */
120 typedef struct wksids_table {
121 	const char	*sidprefix;
122 	uint32_t	rid;
123 	const char	*winname;
124 	int		is_user;
125 	uid_t		pid;
126 	int		direction;
127 } wksids_table_t;
128 
129 
130 #define	_IDMAP_F_DONE		0x00000000
131 #define	_IDMAP_F_S2N_CACHE	0x00000001
132 #define	_IDMAP_F_S2N_AD		0x00000002
133 #define	_IDMAP_F_EXP_EPH_UID	0x00000004
134 #define	_IDMAP_F_EXP_EPH_GID	0x00000010
135 
136 #define	SIZE_INCR	5
137 #define	MAX_TRIES	5
138 #define	IDMAP_DBDIR	"/var/idmap"
139 #define	IDMAP_CACHEDIR	"/var/run/idmap"
140 #define	IDMAP_DBNAME	IDMAP_DBDIR "/idmap.db"
141 #define	IDMAP_CACHENAME	IDMAP_CACHEDIR "/idmap.db"
142 #define	IDMAP_CACHENAME	IDMAP_CACHEDIR "/idmap.db"
143 
144 #define	EMPTY_STRING(str)	(str == NULL || *str == 0)
145 
146 #define	IS_BATCH_SID(batch, i) \
147 	(batch.idmap_mapping_batch_val[i].id1.idtype == IDMAP_SID ||	\
148 	batch.idmap_mapping_batch_val[i].id1.idtype == IDMAP_USID ||	\
149 	batch.idmap_mapping_batch_val[i].id1.idtype == IDMAP_GSID)
150 
151 #define	IS_BATCH_UID(batch, i) \
152 	batch.idmap_mapping_batch_val[i].id1.idtype == IDMAP_UID
153 
154 #define	IS_BATCH_GID(batch, i) \
155 	batch.idmap_mapping_batch_val[i].id1.idtype == IDMAP_GID
156 
157 #define	IS_REQUEST_SID(req, n) \
158 	((req).id##n.idtype == IDMAP_SID ||	\
159 	(req).id##n.idtype == IDMAP_USID ||	\
160 	(req).id##n.idtype == IDMAP_GSID)	\
161 
162 
163 #define	IS_REQUEST_UID(request) \
164 	request.id1.idtype == IDMAP_UID
165 
166 #define	IS_REQUEST_GID(request) \
167 	request.id1.idtype == IDMAP_GID
168 
169 typedef idmap_retcode (*update_list_res_cb)(void *, const char **, uint64_t);
170 typedef int (*list_svc_cb)(void *, int, char **, char **);
171 
172 extern void	idmap_prog_1(struct svc_req *, register SVCXPRT *);
173 extern void	idmapdlog(int, const char *, ...);
174 extern int	init_mapping_system();
175 extern void	fini_mapping_system();
176 extern void	print_idmapdstate();
177 extern int	create_directory(const char *, uid_t, gid_t);
178 extern int	load_config();
179 extern int	reload_ad();
180 extern int	idmap_init_tsd_key(void);
181 extern void	degrade_svc(void);
182 extern void	restore_svc(void);
183 
184 
185 extern int		init_dbs();
186 extern void		fini_dbs();
187 extern idmap_retcode	get_db_handle(sqlite **);
188 extern idmap_retcode	get_cache_handle(sqlite **);
189 extern idmap_retcode	sql_exec_no_cb(sqlite *, char *);
190 extern idmap_retcode	add_namerule(sqlite *, idmap_namerule *);
191 extern idmap_retcode	rm_namerule(sqlite *, idmap_namerule *);
192 extern idmap_retcode	flush_namerules(sqlite *);
193 
194 extern char 		*tolower_u8(const char *);
195 
196 extern idmap_retcode	gen_sql_expr_from_rule(idmap_namerule *, char **);
197 extern idmap_retcode	validate_list_cb_data(list_cb_data_t *, int,
198 				char **, int, uchar_t **, size_t);
199 extern idmap_retcode	process_list_svc_sql(sqlite *, char *, uint64_t,
200 				list_svc_cb, void *);
201 extern idmap_retcode	sid2pid_first_pass(lookup_state_t *, sqlite *,
202 				idmap_mapping *, idmap_id_res *);
203 extern idmap_retcode	sid2pid_second_pass(lookup_state_t *, sqlite *,
204 				sqlite *, idmap_mapping *, idmap_id_res *);
205 extern idmap_retcode	pid2sid_first_pass(lookup_state_t *, sqlite *,
206 				sqlite *, idmap_mapping *, idmap_id_res *,
207 				int, int);
208 extern idmap_retcode	update_cache_sid2pid(lookup_state_t *, sqlite *,
209 				idmap_mapping *, idmap_id_res *);
210 extern idmap_retcode	update_cache_pid2sid(lookup_state_t *, sqlite *,
211 				idmap_mapping *, idmap_id_res *);
212 extern idmap_retcode	get_u2w_mapping(sqlite *, sqlite *, idmap_mapping *,
213 				idmap_mapping *, int);
214 extern idmap_retcode	get_w2u_mapping(sqlite *, sqlite *, idmap_mapping *,
215 				idmap_mapping *);
216 
217 extern idmap_retcode	lookup_win_batch_sid2name(lookup_state_t *,
218 				idmap_mapping_batch *, idmap_ids_res *);
219 
220 
221 #ifdef __cplusplus
222 }
223 #endif
224 
225 #endif /* _IDMAPD_H */
226