xref: /titanic_50/usr/src/cmd/idmap/idmapd/idmapd.h (revision 41efec2219526a9b3ecce26f97aba761ef1e1d0d)
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 lookup_state {
92 	bool_t			sid2pid_done;
93 	bool_t			pid2sid_done;
94 	idmap_query_state_t	*ad_lookup;
95 	int			ad_nqueries;
96 } lookup_state_t;
97 
98 typedef struct list_cb_data {
99 	void		*result;
100 	uint64_t	next;
101 	uint64_t	len;
102 	uint64_t	limit;
103 } list_cb_data_t;
104 
105 typedef struct msg_table {
106 	idmap_retcode	retcode;
107 	const char	*msg;
108 } msg_table_t;
109 
110 typedef struct wksids_table {
111 	const char	*sidprefix;
112 	uint32_t	rid;
113 	int		is_user;
114 	uid_t		pid;
115 	int		direction;
116 } wksids_table_t;
117 
118 
119 #define	_IDMAP_F_DONE		0x00000000
120 #define	_IDMAP_F_S2N_CACHE	0x00000001
121 #define	_IDMAP_F_S2N_AD		0x00000002
122 #define	_IDMAP_F_EXP_EPH_UID	0x00000004
123 #define	_IDMAP_F_EXP_EPH_GID	0x00000010
124 
125 #define	SIZE_INCR	5
126 #define	MAX_TRIES	5
127 #define	IDMAP_DBDIR	"/var/idmap"
128 #define	IDMAP_CACHEDIR	"/var/run/idmap"
129 #define	IDMAP_DBNAME	IDMAP_DBDIR "/idmap.db"
130 #define	IDMAP_CACHENAME	IDMAP_CACHEDIR "/idmap.db"
131 
132 typedef idmap_retcode (*update_list_res_cb)(void *, const char **, uint64_t);
133 typedef int (*list_svc_cb)(void *, int, char **, char **);
134 
135 extern void	idmap_prog_1(struct svc_req *, register SVCXPRT *);
136 extern void	idmapdlog(int, const char *, ...);
137 extern int	init_mapping_system();
138 extern void	fini_mapping_system();
139 extern void	print_idmapdstate();
140 extern int	create_directory(const char *, uid_t, gid_t);
141 extern int	load_config();
142 
143 
144 extern int		init_dbs();
145 extern void		fini_dbs();
146 extern idmap_retcode	get_db_handle(sqlite **);
147 extern idmap_retcode	get_cache_handle(sqlite **);
148 extern idmap_retcode	sql_exec_no_cb(sqlite *, char *);
149 extern idmap_retcode	add_namerule(sqlite *, idmap_namerule *);
150 extern idmap_retcode	rm_namerule(sqlite *, idmap_namerule *);
151 extern idmap_retcode	flush_namerules(sqlite *, bool_t);
152 
153 extern idmap_retcode	gen_sql_expr_from_utf8str(const char *,
154 				const char *, const char *,
155 				idmap_utf8str *, const char *,
156 				char **);
157 extern idmap_retcode	validate_list_cb_data(list_cb_data_t *, int,
158 				char **, int, uchar_t **, size_t);
159 extern idmap_retcode	process_list_svc_sql(sqlite *, char *, uint64_t,
160 				list_svc_cb, void *);
161 extern idmap_retcode	sid2pid_first_pass(lookup_state_t *, sqlite *,
162 				idmap_mapping *, idmap_id_res *);
163 extern idmap_retcode	sid2pid_second_pass(lookup_state_t *, sqlite *,
164 				sqlite *, idmap_mapping *, idmap_id_res *);
165 extern idmap_retcode	pid2sid_first_pass(lookup_state_t *, sqlite *,
166 				sqlite *, idmap_mapping *, idmap_id_res *,
167 				int, int);
168 extern idmap_retcode	update_cache_sid2pid(lookup_state_t *, sqlite *,
169 				idmap_mapping *, idmap_id_res *);
170 extern idmap_retcode	update_cache_pid2sid(lookup_state_t *, sqlite *,
171 				idmap_mapping *, idmap_id_res *);
172 extern idmap_retcode	get_u2w_mapping(sqlite *, sqlite *, idmap_mapping *,
173 				idmap_mapping *, int);
174 extern idmap_retcode	get_w2u_mapping(sqlite *, sqlite *, idmap_mapping *,
175 				idmap_mapping *);
176 
177 extern idmap_retcode	lookup_win_batch_sid2name(lookup_state_t *,
178 				idmap_mapping_batch *, idmap_ids_res *);
179 
180 
181 #ifdef __cplusplus
182 }
183 #endif
184 
185 #endif /* _IDMAPD_H */
186