xref: /titanic_52/usr/src/cmd/idmap/idmapd/idmapd.h (revision 967072a15e834441c4b221b17b3859169a2c9af2)
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 typedef idmap_retcode (*update_list_res_cb)(void *, const char **, uint64_t);
147 typedef int (*list_svc_cb)(void *, int, char **, char **);
148 
149 extern void	idmap_prog_1(struct svc_req *, register SVCXPRT *);
150 extern void	idmapdlog(int, const char *, ...);
151 extern int	init_mapping_system();
152 extern void	fini_mapping_system();
153 extern void	print_idmapdstate();
154 extern int	create_directory(const char *, uid_t, gid_t);
155 extern int	load_config();
156 extern int	reload_ad();
157 extern int	idmap_init_tsd_key(void);
158 extern void	degrade_svc(void);
159 extern void	restore_svc(void);
160 
161 
162 extern int		init_dbs();
163 extern void		fini_dbs();
164 extern idmap_retcode	get_db_handle(sqlite **);
165 extern idmap_retcode	get_cache_handle(sqlite **);
166 extern idmap_retcode	sql_exec_no_cb(sqlite *, char *);
167 extern idmap_retcode	add_namerule(sqlite *, idmap_namerule *);
168 extern idmap_retcode	rm_namerule(sqlite *, idmap_namerule *);
169 extern idmap_retcode	flush_namerules(sqlite *, bool_t);
170 
171 extern idmap_retcode	gen_sql_expr_from_utf8str(const char *,
172 				const char *, const char *,
173 				char *, const char *,
174 				char **);
175 extern idmap_retcode	validate_list_cb_data(list_cb_data_t *, int,
176 				char **, int, uchar_t **, size_t);
177 extern idmap_retcode	process_list_svc_sql(sqlite *, char *, uint64_t,
178 				list_svc_cb, void *);
179 extern idmap_retcode	sid2pid_first_pass(lookup_state_t *, sqlite *,
180 				idmap_mapping *, idmap_id_res *);
181 extern idmap_retcode	sid2pid_second_pass(lookup_state_t *, sqlite *,
182 				sqlite *, idmap_mapping *, idmap_id_res *);
183 extern idmap_retcode	pid2sid_first_pass(lookup_state_t *, sqlite *,
184 				sqlite *, idmap_mapping *, idmap_id_res *,
185 				int, int);
186 extern idmap_retcode	update_cache_sid2pid(lookup_state_t *, sqlite *,
187 				idmap_mapping *, idmap_id_res *);
188 extern idmap_retcode	update_cache_pid2sid(lookup_state_t *, sqlite *,
189 				idmap_mapping *, idmap_id_res *);
190 extern idmap_retcode	get_u2w_mapping(sqlite *, sqlite *, idmap_mapping *,
191 				idmap_mapping *, int);
192 extern idmap_retcode	get_w2u_mapping(sqlite *, sqlite *, idmap_mapping *,
193 				idmap_mapping *);
194 
195 extern idmap_retcode	lookup_win_batch_sid2name(lookup_state_t *,
196 				idmap_mapping_batch *, idmap_ids_res *);
197 
198 
199 #ifdef __cplusplus
200 }
201 #endif
202 
203 #endif /* _IDMAPD_H */
204