xref: /illumos-gate/usr/src/cmd/idmap/idmapd/idmapd.h (revision 96fe64c1)
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 	char	domainname[IDMAP_MAX_NAME_LEN];	/* my domain */
72 	uid_t		next_uid;
73 	gid_t		next_gid;
74 	uid_t		limit_uid;
75 	gid_t		limit_gid;
76 	int		new_eph_db;	/* was the ephem ID db [re-]created? */
77 	ad_t		*ad;
78 } idmapd_state_t;
79 extern idmapd_state_t	_idmapdstate;
80 
81 #define	INIT_IDMAPD_STATE() \
82 	(void) memset(&_idmapdstate, 0, sizeof (_idmapdstate));
83 
84 #define	RDLOCK_CONFIG() \
85 	(void) rw_rdlock(&_idmapdstate.rwlk_cfg);
86 #define	WRLOCK_CONFIG() \
87 	(void) rw_wrlock(&_idmapdstate.rwlk_cfg);
88 #define	UNLOCK_CONFIG() \
89 	(void) rw_unlock(&_idmapdstate.rwlk_cfg);
90 
91 typedef struct hashentry {
92 	uint_t	key;
93 	uint_t	next;
94 } hashentry_t;
95 
96 typedef struct lookup_state {
97 	bool_t			sid2pid_done;
98 	bool_t			pid2sid_done;
99 	idmap_query_state_t	*ad_lookup;
100 	int			ad_nqueries;
101 	uint_t			curpos;
102 	hashentry_t		*sid_history;
103 	uint_t			sid_history_size;
104 	idmap_mapping_batch	*batch;
105 	idmap_ids_res		*result;
106 } lookup_state_t;
107 
108 typedef struct list_cb_data {
109 	void		*result;
110 	uint64_t	next;
111 	uint64_t	len;
112 	uint64_t	limit;
113 } list_cb_data_t;
114 
115 typedef struct msg_table {
116 	idmap_retcode	retcode;
117 	const char	*msg;
118 } msg_table_t;
119 
120 /*
121  * Data structure to store well-known SIDs and
122  * associated mappings (if any)
123  */
124 typedef struct wksids_table {
125 	const char	*sidprefix;
126 	uint32_t	rid;
127 	const char	*winname;
128 	int		is_user;
129 	uid_t		pid;
130 	int		direction;
131 } wksids_table_t;
132 
133 
134 #define	_IDMAP_F_DONE		0x00000000
135 #define	_IDMAP_F_S2N_CACHE	0x00000001
136 #define	_IDMAP_F_S2N_AD		0x00000002
137 #define	_IDMAP_F_EXP_EPH_UID	0x00000004
138 #define	_IDMAP_F_EXP_EPH_GID	0x00000010
139 
140 #define	SIZE_INCR	5
141 #define	MAX_TRIES	5
142 #define	IDMAP_DBDIR	"/var/idmap"
143 #define	IDMAP_CACHEDIR	"/var/run/idmap"
144 #define	IDMAP_DBNAME	IDMAP_DBDIR "/idmap.db"
145 #define	IDMAP_CACHENAME	IDMAP_CACHEDIR "/idmap.db"
146 
147 typedef idmap_retcode (*update_list_res_cb)(void *, const char **, uint64_t);
148 typedef int (*list_svc_cb)(void *, int, char **, char **);
149 
150 extern void	idmap_prog_1(struct svc_req *, register SVCXPRT *);
151 extern void	idmapdlog(int, const char *, ...);
152 extern int	init_mapping_system();
153 extern void	fini_mapping_system();
154 extern void	print_idmapdstate();
155 extern int	create_directory(const char *, uid_t, gid_t);
156 extern int	load_config();
157 
158 
159 extern int		init_dbs();
160 extern void		fini_dbs();
161 extern idmap_retcode	get_db_handle(sqlite **);
162 extern idmap_retcode	get_cache_handle(sqlite **);
163 extern idmap_retcode	sql_exec_no_cb(sqlite *, char *);
164 extern idmap_retcode	add_namerule(sqlite *, idmap_namerule *);
165 extern idmap_retcode	rm_namerule(sqlite *, idmap_namerule *);
166 extern idmap_retcode	flush_namerules(sqlite *, bool_t);
167 
168 extern idmap_retcode	gen_sql_expr_from_utf8str(const char *,
169 				const char *, const char *,
170 				idmap_utf8str *, const char *,
171 				char **);
172 extern idmap_retcode	validate_list_cb_data(list_cb_data_t *, int,
173 				char **, int, uchar_t **, size_t);
174 extern idmap_retcode	process_list_svc_sql(sqlite *, char *, uint64_t,
175 				list_svc_cb, void *);
176 extern idmap_retcode	sid2pid_first_pass(lookup_state_t *, sqlite *,
177 				idmap_mapping *, idmap_id_res *);
178 extern idmap_retcode	sid2pid_second_pass(lookup_state_t *, sqlite *,
179 				sqlite *, idmap_mapping *, idmap_id_res *);
180 extern idmap_retcode	pid2sid_first_pass(lookup_state_t *, sqlite *,
181 				sqlite *, idmap_mapping *, idmap_id_res *,
182 				int, int);
183 extern idmap_retcode	update_cache_sid2pid(lookup_state_t *, sqlite *,
184 				idmap_mapping *, idmap_id_res *);
185 extern idmap_retcode	update_cache_pid2sid(lookup_state_t *, sqlite *,
186 				idmap_mapping *, idmap_id_res *);
187 extern idmap_retcode	get_u2w_mapping(sqlite *, sqlite *, idmap_mapping *,
188 				idmap_mapping *, int);
189 extern idmap_retcode	get_w2u_mapping(sqlite *, sqlite *, idmap_mapping *,
190 				idmap_mapping *);
191 
192 extern idmap_retcode	lookup_win_batch_sid2name(lookup_state_t *,
193 				idmap_mapping_batch *, idmap_ids_res *);
194 
195 
196 #ifdef __cplusplus
197 }
198 #endif
199 
200 #endif /* _IDMAPD_H */
201