1/*
2 * Copyright (c) 2008-2011 Apple Inc.  All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
11 * file.
12 *
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
20 *
21 * @APPLE_LICENSE_HEADER_END@
22 */
23
24#ifndef __SI_MODULE_H__
25#define __SI_MODULE_H__
26
27#include <stdlib.h>
28#include <stdint.h>
29#include <netinet/in.h>
30#include <sys/socket.h>
31#include <pwd.h>
32#include <grp.h>
33#include <uuid/uuid.h>
34#include <netdb.h>
35#include <aliasdb.h>
36#include <fstab.h>
37#include <mach/mach.h>
38#include <si_data.h>
39#include <ils.h>
40
41#define forever for(;;)
42#define string_equal(A,B) (strcmp(A,B)==0)
43#define string_not_equal(A,B) (strcmp(A,B)!=0)
44
45#define SI_CALL_USER_BYNAME        0
46#define SI_CALL_USER_BYUID         1
47#define SI_CALL_USER_ALL           2
48#define SI_CALL_GROUP_BYNAME       3
49#define SI_CALL_GROUP_BYGID        4
50#define SI_CALL_GROUP_ALL          5
51#define SI_CALL_NETGROUP_BYNAME    6
52#define SI_CALL_IN_NETGROUP        7
53#define SI_CALL_GROUPLIST          8
54#define SI_CALL_ALIAS_BYNAME       9
55#define SI_CALL_ALIAS_ALL         10
56#define SI_CALL_HOST_BYNAME       11
57#define SI_CALL_HOST_BYADDR       12
58#define SI_CALL_HOST_ALL          13
59#define SI_CALL_NETWORK_BYNAME    14
60#define SI_CALL_NETWORK_BYADDR    15
61#define SI_CALL_NETWORK_ALL       16
62#define SI_CALL_SERVICE_BYNAME    17
63#define SI_CALL_SERVICE_BYPORT    18
64#define SI_CALL_SERVICE_ALL       19
65#define SI_CALL_PROTOCOL_BYNAME   20
66#define SI_CALL_PROTOCOL_BYNUMBER 21
67#define SI_CALL_PROTOCOL_ALL      22
68#define SI_CALL_RPC_BYNAME        23
69#define SI_CALL_RPC_BYNUMBER      24
70#define SI_CALL_RPC_ALL           25
71#define SI_CALL_FS_BYSPEC         26
72#define SI_CALL_FS_BYFILE         27
73#define SI_CALL_FS_ALL            28
74#define SI_CALL_ADDRINFO          29
75#define SI_CALL_NAMEINFO          30
76#define SI_CALL_IPNODE_BYNAME     31
77#define SI_CALL_MAC_BYNAME        32
78#define SI_CALL_MAC_BYMAC         33
79#define SI_CALL_MAC_ALL           34
80#define SI_CALL_DNS_QUERY         35
81#define SI_CALL_DNS_SEARCH        36
82
83#define si_call_returns_list(A) \
84((A==SI_CALL_USER_ALL)||(A==SI_CALL_GROUP_ALL)||(A==SI_CALL_HOST_ALL)||(A==SI_CALL_NETWORK_ALL)||\
85 (A==SI_CALL_SERVICE_ALL)||(A==SI_CALL_PROTOCOL_ALL)||(A==SI_CALL_RPC_ALL)||(A==SI_CALL_FS_ALL)||\
86 (A==SI_CALL_ALIAS_ALL)||(A==SI_CALL_NETGROUP_BYNAME)||(A==SI_CALL_ADDRINFO)||(A==SI_CALL_MAC_ALL))
87
88#define si_call_str1_is_buffer(A) \
89((A==SI_CALL_HOST_BYADDR)||(A==SI_CALL_NAMEINFO))
90
91#define CATEGORY_INVALID  (-1)
92#define CATEGORY_DEFAULT    0
93#define CATEGORY_USER       1
94#define CATEGORY_GROUP      2
95#define CATEGORY_GROUPLIST  3
96#define CATEGORY_NETGROUP   4
97#define CATEGORY_ALIAS      5
98#define CATEGORY_HOST_IPV4  6
99#define CATEGORY_HOST_IPV6  7
100#define CATEGORY_NETWORK    8
101#define CATEGORY_SERVICE    9
102#define CATEGORY_PROTOCOL  10
103#define CATEGORY_RPC       11
104#define CATEGORY_FS        12
105#define CATEGORY_MAC       13
106#define CATEGORY_NAMEINFO  14
107#define CATEGORY_ADDRINFO  15
108#define CATEGORY_DNSPACKET 16
109#define CATEGORY_SRV       17
110#define CATEGORY_COUNT     18
111
112/* convenience */
113#define CATEGORY_HOST CATEGORY_HOST_IPV4
114
115#define SEL_ALL 0
116#define SEL_NAME 1
117#define SEL_NUMBER 2
118
119/* host, getaddrinfo, and getnameinfo status codes */
120#define SI_STATUS_NO_ERROR                 0
121#define SI_STATUS_H_ERRNO_HOST_NOT_FOUND   1
122#define SI_STATUS_H_ERRNO_TRY_AGAIN        2
123#define SI_STATUS_H_ERRNO_NO_RECOVERY      3
124#define SI_STATUS_H_ERRNO_NO_DATA          4
125#define SI_STATUS_INTERNAL                10
126#define SI_STATUS_WORKITEM_NOT_FOUND      11
127#define SI_STATUS_RETURNS_ITEM            12
128#define SI_STATUS_RETURNS_LIST            13
129#define SI_STATUS_CALL_IN_PROGRESS        14
130#define SI_STATUS_CALL_CANCELLED          15
131#define	SI_STATUS_EAI_PLUS_100           100
132#define	SI_STATUS_EAI_ADDRFAMILY         101
133#define	SI_STATUS_EAI_AGAIN              102
134#define	SI_STATUS_EAI_BADFLAGS           103
135#define	SI_STATUS_EAI_FAIL               104
136#define	SI_STATUS_EAI_FAMILY             105
137#define	SI_STATUS_EAI_MEMORY             106
138#define	SI_STATUS_EAI_NODATA             107
139#define	SI_STATUS_EAI_NONAME             108
140#define	SI_STATUS_EAI_SERVICE            109
141#define	SI_STATUS_EAI_SOCKTYPE           110
142#define	SI_STATUS_EAI_SYSTEM             111
143#define SI_STATUS_EAI_BADHINTS           112
144#define SI_STATUS_EAI_PROTOCOL           113
145#define SI_STATUS_EAI_OVERFLOW           114
146#define SI_STATUS_ERRNO_PLUS_200         200
147
148typedef void (*item_async_callback)(si_item_t *, uint32_t, void *);
149typedef void (*list_async_callback)(si_list_t *, uint32_t, void *);
150
151typedef struct grouplist_s
152{
153	char *gl_user;
154	int gl_count;
155	int gl_gid_siz;
156	gid_t *gl_gid;
157} si_grouplist_t;
158
159typedef struct addrinfo_s
160{
161	int ai_flags;
162	int ai_family;
163	int ai_socktype;
164	int ai_protocol;
165	uint32_t ai_addrlen;
166	socket_data_t ai_addr;
167	char *ai_canonname;
168} si_addrinfo_t;
169
170typedef struct nameinfo_s
171{
172	char *ni_node;
173	char *ni_serv;
174} si_nameinfo_t;
175
176typedef struct mac_s
177{
178	char *host;
179	char *mac;
180} si_mac_t;
181
182typedef struct netgrent_s
183{
184	char *ng_host;
185	char *ng_user;
186	char *ng_domain;
187} si_netgrent_t;
188
189typedef struct dnspacket_s
190{
191	int dns_packet_len;
192	char *dns_packet;
193	int dns_server_len;
194	struct sockaddr *dns_server;
195} si_dnspacket_t;
196
197typedef struct si_srv_s
198{
199	uint16_t priority;
200	uint16_t weight;
201	uint16_t port;
202	char *target;
203} si_srv_t;
204
205struct si_mod_s;
206
207struct si_mod_vtable_s
208{
209	void (*sim_close)(struct si_mod_s *si);
210
211	int (*sim_is_valid)(struct si_mod_s *si, si_item_t *item);
212
213	si_item_t *(*sim_user_byname)(struct si_mod_s *si, const char *name);
214	si_item_t *(*sim_user_byuid)(struct si_mod_s *si, uid_t uid);
215	si_item_t *(*sim_user_byuuid)(struct si_mod_s *si, uuid_t uuid);
216	si_list_t *(*sim_user_all)(struct si_mod_s *si);
217
218	si_item_t *(*sim_group_byname)(struct si_mod_s *si, const char *name);
219	si_item_t *(*sim_group_bygid)(struct si_mod_s *si, gid_t gid);
220	si_item_t *(*sim_group_byuuid)(struct si_mod_s *si, uuid_t uuid);
221	si_list_t *(*sim_group_all)(struct si_mod_s *si);
222
223	si_item_t *(*sim_grouplist)(struct si_mod_s *si, const char *name, uint32_t count);
224
225	si_list_t *(*sim_netgroup_byname)(struct si_mod_s *si, const char *name);
226	int (*sim_in_netgroup)(struct si_mod_s *si, const char *name, const char *host, const char *user, const char *domain);
227
228	si_item_t *(*sim_alias_byname)(struct si_mod_s *si, const char *name);
229	si_list_t *(*sim_alias_all)(struct si_mod_s *si);
230
231	si_item_t *(*sim_host_byname)(struct si_mod_s *si, const char *name, int af, const char *interface, uint32_t *err);
232	si_item_t *(*sim_host_byaddr)(struct si_mod_s *si, const void *addr, int af, const char *interface, uint32_t *err);
233	si_list_t *(*sim_host_all)(struct si_mod_s *si);
234
235	si_item_t *(*sim_network_byname)(struct si_mod_s *si, const char *name);
236	si_item_t *(*sim_network_byaddr)(struct si_mod_s *si, uint32_t addr);
237	si_list_t *(*sim_network_all)(struct si_mod_s *si);
238
239	si_item_t *(*sim_service_byname)(struct si_mod_s *si, const char *name, const char *proto);
240	si_item_t *(*sim_service_byport)(struct si_mod_s *si, int port, const char *proto);
241	si_list_t *(*sim_service_all)(struct si_mod_s *si);
242
243	si_item_t *(*sim_protocol_byname)(struct si_mod_s *si, const char *name);
244	si_item_t *(*sim_protocol_bynumber)(struct si_mod_s *si, int number);
245	si_list_t *(*sim_protocol_all)(struct si_mod_s *si);
246
247	si_item_t *(*sim_rpc_byname)(struct si_mod_s *si, const char *name);
248	si_item_t *(*sim_rpc_bynumber)(struct si_mod_s *si, int number);
249	si_list_t *(*sim_rpc_all)(struct si_mod_s *si);
250
251	si_item_t *(*sim_fs_byspec)(struct si_mod_s *si, const char *spec);
252	si_item_t *(*sim_fs_byfile)(struct si_mod_s *si, const char *file);
253	si_list_t *(*sim_fs_all)(struct si_mod_s *si);
254
255	si_item_t *(*sim_mac_byname)(struct si_mod_s *si, const char *name);
256	si_item_t *(*sim_mac_bymac)(struct si_mod_s *si, const char *mac);
257	si_list_t *(*sim_mac_all)(struct si_mod_s *si);
258
259	si_list_t *(*sim_addrinfo)(struct si_mod_s *si, const void *node, const void *serv, uint32_t family, uint32_t socktype, uint32_t protocol, uint32_t flags, const char *interface, uint32_t *err);
260	int (*sim_wants_addrinfo)(struct si_mod_s *si);
261
262	si_item_t *(*sim_nameinfo)(struct si_mod_s *si, const struct sockaddr *sa, int flags, const char *interface, uint32_t *err);
263
264	si_list_t *(*sim_srv_byname)(struct si_mod_s *si, const char *qname, const char *interface, uint32_t *err);
265
266	si_item_t *(*sim_item_call)(struct si_mod_s *si, int call, const char *str1, const char *str2, const char *str3, uint32_t num1, uint32_t num2, uint32_t *err);
267	si_list_t *(*sim_list_call)(struct si_mod_s *si, int call, const char *str1, const char *str2, const char *str3, uint32_t num1, uint32_t num2, uint32_t num3, uint32_t num4, uint32_t *err);
268
269	mach_port_t (*sim_async_call)(struct si_mod_s *si, int call, const char *str1, const char *str2, const char *str3, uint32_t num1, uint32_t num2, uint32_t num3, uint32_t num4, void *callback, void *context);
270	void (*sim_async_cancel)(mach_port_t p);
271	void (*sim_async_handle_reply)(mach_msg_header_t *msg);
272};
273
274#define SI_MOD_FLAG_STATIC 0x00000001
275
276typedef struct si_mod_s
277{
278	char *name;
279	uint32_t vers;
280	int32_t refcount;
281	uint32_t flags;
282
283	void *bundle;
284	void *private;
285
286	const struct si_mod_vtable_s *vtable;
287} si_mod_t;
288
289si_mod_t *si_module_with_name(const char *name);
290si_mod_t *si_module_retain(si_mod_t *si);
291void si_module_release(si_mod_t *si);
292const char *si_module_name(si_mod_t *si);
293int si_module_vers(si_mod_t *si);
294
295si_mod_t *si_search(void);
296
297int si_item_match(si_item_t *item, int cat, const void *name, uint32_t num, int which);
298int si_item_is_valid(si_item_t *item);
299
300si_item_t *si_user_byname(si_mod_t *si, const char *name);
301si_item_t *si_user_byuid(si_mod_t *si, uid_t uid);
302si_item_t *si_user_byuuid(si_mod_t *si, uuid_t uuid);
303si_list_t *si_user_all(si_mod_t *si);
304
305si_item_t *si_group_byname(si_mod_t *si, const char *name);
306si_item_t *si_group_bygid(si_mod_t *si, gid_t gid);
307si_item_t *si_group_byuuid(si_mod_t *si, uuid_t uuid);
308si_list_t *si_group_all(si_mod_t *si);
309
310si_item_t *si_grouplist(si_mod_t *si, const char *name, uint32_t count);
311
312int si_in_netgroup(struct si_mod_s *si, const char *name, const char *host, const char *user, const char *domain);
313si_list_t *si_netgroup_byname(struct si_mod_s *si, const char *name);
314
315si_item_t *si_alias_byname(struct si_mod_s *si, const char *name);
316si_list_t *si_alias_all(struct si_mod_s *si);
317
318si_item_t *si_host_byname(si_mod_t *si, const char *name, int af, const char *interface, uint32_t *err);
319si_item_t *si_host_byaddr(si_mod_t *si, const void *addr, int af, const char *interface, uint32_t *err);
320si_list_t *si_host_all(si_mod_t *si);
321
322si_item_t *si_mac_byname(struct si_mod_s *si, const char *name);
323si_item_t *si_mac_bymac(struct si_mod_s *si, const char *mac);
324si_list_t *si_mac_all(struct si_mod_s *si);
325
326si_item_t *si_network_byname(si_mod_t *si, const char *name);
327si_item_t *si_network_byaddr(si_mod_t *si, uint32_t addr);
328si_list_t *si_network_all(si_mod_t *si);
329
330si_item_t *si_service_byname(si_mod_t *si, const char *name, const char *proto);
331si_item_t *si_service_byport(si_mod_t *si, int port, const char *proto);
332si_list_t *si_service_all(si_mod_t *si);
333
334si_item_t *si_protocol_byname(si_mod_t *si, const char *name);
335si_item_t *si_protocol_bynumber(si_mod_t *si, uint32_t number);
336si_list_t *si_protocol_all(si_mod_t *si);
337
338si_item_t *si_rpc_byname(si_mod_t *si, const char *name);
339si_item_t *si_rpc_bynumber(si_mod_t *si, int number);
340si_list_t *si_rpc_all(si_mod_t *si);
341
342si_item_t *si_fs_byspec(si_mod_t *si, const char *spec);
343si_item_t *si_fs_byfile(si_mod_t *si, const char *file);
344si_list_t *si_fs_all(si_mod_t *si);
345
346int si_wants_addrinfo(si_mod_t *s);
347si_list_t *si_addrinfo(si_mod_t *si, const char *node, const char *serv, uint32_t family, uint32_t socktype, uint32_t protocol, uint32_t flags, const char *interface, uint32_t *err);
348
349si_item_t *si_nameinfo(si_mod_t *si, const struct sockaddr *sa, int flags, const char *interface, uint32_t *err);
350si_item_t *si_ipnode_byname(si_mod_t *si, const char *name, int family, int flags, const char *interface, uint32_t *err);
351
352si_list_t *si_srv_byname(si_mod_t *si, const char *qname, const char *interface, uint32_t *err);
353
354si_item_t *si_item_call(si_mod_t *si, int call, const char *str1, const char *str2, const char *str3, uint32_t num1, uint32_t num2, uint32_t *err);
355si_list_t *si_list_call(si_mod_t *si, int call, const char *str1, const char *str2, const char *str3, uint32_t num1, uint32_t num2, uint32_t num3, uint32_t num4, uint32_t *err);
356
357extern mach_port_t si_async_call(si_mod_t *si, int call, const char *str1, const char *str2, const char *str3, uint32_t num1, uint32_t num2, uint32_t num3, uint32_t num4, void *callback, void *context);
358extern void si_async_cancel(mach_port_t p);
359extern void si_async_handle_reply(mach_msg_header_t *msg);
360
361char *si_standardize_mac_address(const char *addr);
362si_item_t *si_addrinfo_v4(si_mod_t *si, int32_t flags, int32_t sock, int32_t proto, uint16_t port, struct in_addr *addr, uint16_t iface, const char *cname);
363si_item_t *si_addrinfo_v6(si_mod_t *si, int32_t flags, int32_t sock, int32_t proto, uint16_t port, struct in6_addr *addr, uint16_t iface, const char *cname);
364si_item_t *si_addrinfo_v4_mapped(si_mod_t *si, int32_t flags, int32_t sock, int32_t proto, uint16_t port, struct in_addr *addr, uint16_t iface, const char *cname);
365si_list_t *si_addrinfo_list(si_mod_t *si, uint32_t flags, int socktype, int proto, struct in_addr *a4, struct in6_addr *a6, int port, int scopeid, const char *cname4, const char *cname6);
366si_list_t *si_addrinfo_list_from_hostent(si_mod_t *si, uint32_t flags, uint32_t socktype, uint32_t proto, uint16_t port, uint16_t scope, const struct hostent *h4, const struct hostent *h6);
367
368int _gai_serv_to_port(const char *serv, uint32_t proto, uint16_t *port);
369si_list_t *_gai_simple(si_mod_t *si, const void *nodeptr, const void *servptr, uint32_t family, uint32_t socktype, uint32_t proto, uint32_t flags, const char *interface, uint32_t *err);
370int si_inet_config(uint32_t *inet4, uint32_t *inet6);
371
372#endif /* ! __SI_MODULE_H__ */
373