xref: /illumos-gate/usr/src/cmd/idmap/idmapd/idmapd.h (revision c5c4113d)
1*c5c4113dSnw /*
2*c5c4113dSnw  * CDDL HEADER START
3*c5c4113dSnw  *
4*c5c4113dSnw  * The contents of this file are subject to the terms of the
5*c5c4113dSnw  * Common Development and Distribution License (the "License").
6*c5c4113dSnw  * You may not use this file except in compliance with the License.
7*c5c4113dSnw  *
8*c5c4113dSnw  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9*c5c4113dSnw  * or http://www.opensolaris.org/os/licensing.
10*c5c4113dSnw  * See the License for the specific language governing permissions
11*c5c4113dSnw  * and limitations under the License.
12*c5c4113dSnw  *
13*c5c4113dSnw  * When distributing Covered Code, include this CDDL HEADER in each
14*c5c4113dSnw  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15*c5c4113dSnw  * If applicable, add the following below this CDDL HEADER, with the
16*c5c4113dSnw  * fields enclosed by brackets "[]" replaced with your own identifying
17*c5c4113dSnw  * information: Portions Copyright [yyyy] [name of copyright owner]
18*c5c4113dSnw  *
19*c5c4113dSnw  * CDDL HEADER END
20*c5c4113dSnw  */
21*c5c4113dSnw /*
22*c5c4113dSnw  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
23*c5c4113dSnw  * Use is subject to license terms.
24*c5c4113dSnw  */
25*c5c4113dSnw 
26*c5c4113dSnw #ifndef _IDMAPD_H
27*c5c4113dSnw #define	_IDMAPD_H
28*c5c4113dSnw 
29*c5c4113dSnw #pragma ident	"%Z%%M%	%I%	%E% SMI"
30*c5c4113dSnw 
31*c5c4113dSnw #include <stdio.h>
32*c5c4113dSnw #include <stdlib.h>
33*c5c4113dSnw #include <syslog.h>
34*c5c4113dSnw #include <stdarg.h>
35*c5c4113dSnw #include <rpc/rpc.h>
36*c5c4113dSnw #include <synch.h>
37*c5c4113dSnw #include <thread.h>
38*c5c4113dSnw #include <libintl.h>
39*c5c4113dSnw #include <strings.h>
40*c5c4113dSnw #include <sqlite/sqlite.h>
41*c5c4113dSnw #include <inttypes.h>
42*c5c4113dSnw #include "idmap_prot.h"
43*c5c4113dSnw #include "adutils.h"
44*c5c4113dSnw #include "idmap_config.h"
45*c5c4113dSnw 
46*c5c4113dSnw #ifdef __cplusplus
47*c5c4113dSnw extern "C" {
48*c5c4113dSnw #endif
49*c5c4113dSnw 
50*c5c4113dSnw /* States a server can be in wrt request */
51*c5c4113dSnw #define	_IDLE	0
52*c5c4113dSnw #define	_SERVED	1
53*c5c4113dSnw 
54*c5c4113dSnw #define	CHECK_NULL(s)	s?s:"null"
55*c5c4113dSnw 
56*c5c4113dSnw #define	SENTINEL_PID	UINT32_MAX
57*c5c4113dSnw 
58*c5c4113dSnw extern int _rpcsvcstate;	/* set when a request is serviced */
59*c5c4113dSnw extern int _rpcsvccount;	/* number of requests being serviced */
60*c5c4113dSnw extern mutex_t _svcstate_lock;	/* lock for _rpcsvcstate, _rpcsvccount */
61*c5c4113dSnw 
62*c5c4113dSnw /*
63*c5c4113dSnw  * Global state of idmapd daemon.
64*c5c4113dSnw  */
65*c5c4113dSnw #define	IDMAP_MAX_NAME_LEN	512
66*c5c4113dSnw typedef struct idmapd_state {
67*c5c4113dSnw 	rwlock_t	rwlk_cfg;		/* config lock */
68*c5c4113dSnw 	idmap_cfg_t	*cfg;			/* config */
69*c5c4113dSnw 	bool_t		daemon_mode;		/* daemon mode? yes/no */
70*c5c4113dSnw 	char		hostname[MAX_NAME_LEN];	/* my hostname */
71*c5c4113dSnw 	char	domainname[IDMAP_MAX_NAME_LEN];	/* my domain */
72*c5c4113dSnw 	uid_t		next_uid;
73*c5c4113dSnw 	gid_t		next_gid;
74*c5c4113dSnw 	uid_t		limit_uid;
75*c5c4113dSnw 	gid_t		limit_gid;
76*c5c4113dSnw 	int		new_eph_db;	/* was the ephem ID db [re-]created? */
77*c5c4113dSnw 	ad_t		*ad;
78*c5c4113dSnw } idmapd_state_t;
79*c5c4113dSnw extern idmapd_state_t	_idmapdstate;
80*c5c4113dSnw 
81*c5c4113dSnw #define	INIT_IDMAPD_STATE() \
82*c5c4113dSnw 	(void) memset(&_idmapdstate, 0, sizeof (_idmapdstate));
83*c5c4113dSnw 
84*c5c4113dSnw #define	RDLOCK_CONFIG() \
85*c5c4113dSnw 	(void) rw_rdlock(&_idmapdstate.rwlk_cfg);
86*c5c4113dSnw #define	WRLOCK_CONFIG() \
87*c5c4113dSnw 	(void) rw_wrlock(&_idmapdstate.rwlk_cfg);
88*c5c4113dSnw #define	UNLOCK_CONFIG() \
89*c5c4113dSnw 	(void) rw_unlock(&_idmapdstate.rwlk_cfg);
90*c5c4113dSnw 
91*c5c4113dSnw typedef struct lookup_state {
92*c5c4113dSnw 	bool_t			sid2pid_done;
93*c5c4113dSnw 	bool_t			pid2sid_done;
94*c5c4113dSnw 	idmap_query_state_t	*ad_lookup;
95*c5c4113dSnw 	int			ad_nqueries;
96*c5c4113dSnw } lookup_state_t;
97*c5c4113dSnw 
98*c5c4113dSnw typedef struct list_cb_data {
99*c5c4113dSnw 	void		*result;
100*c5c4113dSnw 	uint64_t	next;
101*c5c4113dSnw 	uint64_t	len;
102*c5c4113dSnw 	uint64_t	limit;
103*c5c4113dSnw } list_cb_data_t;
104*c5c4113dSnw 
105*c5c4113dSnw typedef struct msg_table {
106*c5c4113dSnw 	idmap_retcode	retcode;
107*c5c4113dSnw 	const char	*msg;
108*c5c4113dSnw } msg_table_t;
109*c5c4113dSnw 
110*c5c4113dSnw typedef struct wksids_table {
111*c5c4113dSnw 	const char	*sidprefix;
112*c5c4113dSnw 	uint32_t	rid;
113*c5c4113dSnw 	int		is_user;
114*c5c4113dSnw 	uid_t		pid;
115*c5c4113dSnw 	int		direction;
116*c5c4113dSnw } wksids_table_t;
117*c5c4113dSnw 
118*c5c4113dSnw 
119*c5c4113dSnw #define	_IDMAP_F_DONE		0x00000000
120*c5c4113dSnw #define	_IDMAP_F_S2N_CACHE	0x00000001
121*c5c4113dSnw #define	_IDMAP_F_S2N_AD		0x00000002
122*c5c4113dSnw #define	_IDMAP_F_EXP_EPH_UID	0x00000004
123*c5c4113dSnw #define	_IDMAP_F_EXP_EPH_GID	0x00000010
124*c5c4113dSnw 
125*c5c4113dSnw #define	SIZE_INCR	5
126*c5c4113dSnw #define	MAX_TRIES	5
127*c5c4113dSnw #define	IDMAP_DBDIR	"/var/idmap"
128*c5c4113dSnw #define	IDMAP_CACHEDIR	"/var/run/idmap"
129*c5c4113dSnw #define	IDMAP_DBNAME	IDMAP_DBDIR "/idmap.db"
130*c5c4113dSnw #define	IDMAP_CACHENAME	IDMAP_CACHEDIR "/idmap.db"
131*c5c4113dSnw 
132*c5c4113dSnw typedef idmap_retcode (*update_list_res_cb)(void *, const char **, uint64_t);
133*c5c4113dSnw typedef int (*list_svc_cb)(void *, int, char **, char **);
134*c5c4113dSnw 
135*c5c4113dSnw extern void	idmap_prog_1(struct svc_req *, register SVCXPRT *);
136*c5c4113dSnw extern void	idmapdlog(int, const char *, ...);
137*c5c4113dSnw extern int	init_mapping_system();
138*c5c4113dSnw extern void	fini_mapping_system();
139*c5c4113dSnw extern void	print_idmapdstate();
140*c5c4113dSnw extern int	create_directory(const char *, uid_t, gid_t);
141*c5c4113dSnw extern int	load_config();
142*c5c4113dSnw 
143*c5c4113dSnw 
144*c5c4113dSnw extern int		init_dbs();
145*c5c4113dSnw extern void		fini_dbs();
146*c5c4113dSnw extern idmap_retcode	get_db_handle(sqlite **);
147*c5c4113dSnw extern idmap_retcode	get_cache_handle(sqlite **);
148*c5c4113dSnw extern idmap_retcode	sql_exec_no_cb(sqlite *, char *);
149*c5c4113dSnw extern idmap_retcode	add_namerule(sqlite *, idmap_namerule *);
150*c5c4113dSnw extern idmap_retcode	rm_namerule(sqlite *, idmap_namerule *);
151*c5c4113dSnw extern idmap_retcode	flush_namerules(sqlite *, bool_t);
152*c5c4113dSnw 
153*c5c4113dSnw extern idmap_retcode	gen_sql_expr_from_utf8str(const char *,
154*c5c4113dSnw 				const char *, const char *,
155*c5c4113dSnw 				idmap_utf8str *, const char *,
156*c5c4113dSnw 				char **);
157*c5c4113dSnw extern idmap_retcode	validate_list_cb_data(list_cb_data_t *, int,
158*c5c4113dSnw 				char **, int, uchar_t **, size_t);
159*c5c4113dSnw extern idmap_retcode	process_list_svc_sql(sqlite *, char *, uint64_t,
160*c5c4113dSnw 				list_svc_cb, void *);
161*c5c4113dSnw extern idmap_retcode	sid2pid_first_pass(lookup_state_t *, sqlite *,
162*c5c4113dSnw 				idmap_mapping *, idmap_id_res *);
163*c5c4113dSnw extern idmap_retcode	sid2pid_second_pass(lookup_state_t *, sqlite *,
164*c5c4113dSnw 				sqlite *, idmap_mapping *, idmap_id_res *);
165*c5c4113dSnw extern idmap_retcode	pid2sid_first_pass(lookup_state_t *, sqlite *,
166*c5c4113dSnw 				sqlite *, idmap_mapping *, idmap_id_res *,
167*c5c4113dSnw 				int, int);
168*c5c4113dSnw extern idmap_retcode	update_cache_sid2pid(lookup_state_t *, sqlite *,
169*c5c4113dSnw 				idmap_mapping *, idmap_id_res *);
170*c5c4113dSnw extern idmap_retcode	update_cache_pid2sid(lookup_state_t *, sqlite *,
171*c5c4113dSnw 				idmap_mapping *, idmap_id_res *);
172*c5c4113dSnw extern idmap_retcode	get_u2w_mapping(sqlite *, sqlite *, idmap_mapping *,
173*c5c4113dSnw 				idmap_mapping *, int);
174*c5c4113dSnw extern idmap_retcode	get_w2u_mapping(sqlite *, sqlite *, idmap_mapping *,
175*c5c4113dSnw 				idmap_mapping *);
176*c5c4113dSnw 
177*c5c4113dSnw extern idmap_retcode	lookup_win_batch_sid2name(lookup_state_t *,
178*c5c4113dSnw 				idmap_mapping_batch *, idmap_ids_res *);
179*c5c4113dSnw 
180*c5c4113dSnw 
181*c5c4113dSnw #ifdef __cplusplus
182*c5c4113dSnw }
183*c5c4113dSnw #endif
184*c5c4113dSnw 
185*c5c4113dSnw #endif /* _IDMAPD_H */
186