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