1 /*	$NetBSD: omapip_p.h,v 1.1.1.4 2014/07/12 11:57:57 spz Exp $	*/
2 /* omapip_p.h
3 
4    Private master include file for the OMAPI library. */
5 
6 /*
7  * Copyright (c) 2009-2010,2014 by Internet Systems Consortium, Inc. ("ISC")
8  * Copyright (c) 2004,2007 by Internet Systems Consortium, Inc. ("ISC")
9  * Copyright (c) 1996-2003 by Internet Software Consortium
10  *
11  * Permission to use, copy, modify, and distribute this software for any
12  * purpose with or without fee is hereby granted, provided that the above
13  * copyright notice and this permission notice appear in all copies.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
16  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
17  * MERCHANTABILITY AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR
18  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
19  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
20  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
21  * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
22  *
23  *   Internet Systems Consortium, Inc.
24  *   950 Charter Street
25  *   Redwood City, CA 94063
26  *   <info@isc.org>
27  *   https://www.isc.org/
28  *
29  */
30 
31 #ifndef __OMAPIP_OMAPIP_P_H__
32 #define __OMAPIP_OMAPIP_P_H__
33 
34 #ifndef __CYGWIN32__
35 #include <sys/types.h>
36 #include <netinet/in.h>
37 #include <sys/socket.h>
38 #include <sys/un.h>
39 #include <arpa/inet.h>
40 
41 #include <netdb.h>
42 #else
43 #define fd_set cygwin_fd_set
44 #include <sys/types.h>
45 #endif
46 #include <fcntl.h>
47 #include <stdio.h>
48 #include <unistd.h>
49 #include <string.h>
50 #include <memory.h>
51 #include <stdlib.h>
52 #include <sys/stat.h>
53 #include <ctype.h>
54 #include <time.h>
55 
56 /*
57  * XXX: I'm not sure why these were here.
58 #include "cdefs.h"
59 #include "osdep.h"
60  */
61 
62 #include <dst/dst.h>
63 #include "result.h"
64 
65 #include <omapip/convert.h>
66 #include <omapip/hash.h>
67 #include <omapip/omapip.h>
68 #include <omapip/trace.h>
69 
70 /* DST_API control flags */
71 /* These are used in functions dst_sign_data and dst_verify_data */
72 #define SIG_MODE_INIT		1  /* initalize digest */
73 #define SIG_MODE_UPDATE		2  /* add data to digest */
74 #define SIG_MODE_FINAL		4  /* generate/verify signature */
75 #define SIG_MODE_ALL		(SIG_MODE_INIT|SIG_MODE_UPDATE|SIG_MODE_FINAL)
76 
77 /* OMAPI protocol header, version 1.00 */
78 typedef struct {
79 	u_int32_t authlen;	/* Length of authenticator. */
80 	u_int32_t authid;	/* Authenticator object ID. */
81 	u_int32_t op;		/* Opcode. */
82 	omapi_handle_t handle;	/* Handle of object being operated on,
83                                    or zero. */
84 	u_int32_t id;		/* Transaction ID. */
85 	u_int32_t rid;	/* ID of transaction to which this is a response. */
86 } omapi_protocol_header_t;
87 
88 #define OMAPI_PROTOCOL_VERSION	100
89 
90 #define OMAPI_OP_OPEN		1
91 #define OMAPI_OP_REFRESH	2
92 #define	OMAPI_OP_UPDATE		3
93 #define OMAPI_OP_NOTIFY		4
94 #define OMAPI_OP_STATUS		5
95 #define OMAPI_OP_DELETE		6
96 
97 typedef enum {
98 	omapi_connection_unconnected,
99 	omapi_connection_connecting,
100 	omapi_connection_connected,
101 	omapi_connection_disconnecting,
102 	omapi_connection_closed
103 } omapi_connection_state_t;
104 
105 typedef enum {
106 	omapi_protocol_intro_wait,
107 	omapi_protocol_header_wait,
108 	omapi_protocol_signature_wait,
109 	omapi_protocol_name_wait,
110 	omapi_protocol_name_length_wait,
111 	omapi_protocol_value_wait,
112 	omapi_protocol_value_length_wait
113 } omapi_protocol_state_t;
114 
115 typedef struct __omapi_message_object {
116 	OMAPI_OBJECT_PREAMBLE;
117 	struct __omapi_message_object *next, *prev;
118 	omapi_object_t *object;
119 	omapi_object_t *notify_object;
120 	struct __omapi_protocol_object *protocol_object;
121 	u_int32_t authlen;
122 	omapi_typed_data_t *authenticator;
123 	u_int32_t authid;
124 	omapi_object_t *id_object;
125 	u_int32_t op;
126 	u_int32_t h;
127 	u_int32_t id;
128 	u_int32_t rid;
129 } omapi_message_object_t;
130 
131 typedef struct __omapi_remote_auth {
132 	struct __omapi_remote_auth *next;
133 	omapi_handle_t remote_handle;
134 	omapi_object_t *a;
135 } omapi_remote_auth_t;
136 
137 typedef struct __omapi_protocol_object {
138 	OMAPI_OBJECT_PREAMBLE;
139 	u_int32_t header_size;
140 	u_int32_t protocol_version;
141 	u_int32_t next_xid;
142 
143 	omapi_protocol_state_t state;	/* Input state. */
144 	int reading_message_values;	/* True if reading message-specific
145 					   values. */
146 	omapi_message_object_t *message;	/* Incoming message. */
147 	omapi_data_string_t *name;	/* Incoming name. */
148 	omapi_typed_data_t *value;	/* Incoming value. */
149 	isc_result_t verify_result;
150 	omapi_remote_auth_t *default_auth; /* Default authinfo to use. */
151 	omapi_remote_auth_t *remote_auth_list;	/* Authenticators active on
152 						   this connection. */
153 
154 	isc_boolean_t insecure;		/* Set to allow unauthenticated
155 					   messages. */
156 
157 	isc_result_t (*verify_auth) (omapi_object_t *, omapi_auth_key_t *);
158 } omapi_protocol_object_t;
159 
160 typedef struct {
161 	OMAPI_OBJECT_PREAMBLE;
162 
163 	isc_boolean_t insecure;		/* Set to allow unauthenticated
164 					   messages. */
165 
166 	isc_result_t (*verify_auth) (omapi_object_t *, omapi_auth_key_t *);
167 } omapi_protocol_listener_object_t;
168 
169 #include <omapip/buffer.h>
170 
171 typedef struct __omapi_listener_object {
172 	OMAPI_OBJECT_PREAMBLE;
173 	int socket;		/* Connection socket. */
174 	int index;
175 	struct sockaddr_in address;
176 	isc_result_t (*verify_addr) (omapi_object_t *, omapi_addr_t *);
177 } omapi_listener_object_t;
178 
179 typedef struct __omapi_connection_object {
180 	OMAPI_OBJECT_PREAMBLE;
181 	int socket;		/* Connection socket. */
182 	int32_t index;
183 	omapi_connection_state_t state;
184 	struct sockaddr_in remote_addr;
185 	struct sockaddr_in local_addr;
186 	omapi_addr_list_t *connect_list;	/* List of addresses to which
187 						   to connect. */
188 	int cptr;		/* Current element we are connecting to. */
189 	u_int32_t bytes_needed;	/* Bytes of input needed before wakeup. */
190 	u_int32_t in_bytes;	/* Bytes of input already buffered. */
191 	omapi_buffer_t *inbufs;
192 	u_int32_t out_bytes;	/* Bytes of output in buffers. */
193 	omapi_buffer_t *outbufs;
194 	omapi_listener_object_t *listener;	/* Listener that accepted this
195 						   connection, if any. */
196 	dst_key_t *in_key;	/* Authenticator signing incoming
197 				   data. */
198 	void *in_context;	/* Input hash context. */
199 	dst_key_t *out_key;	/* Authenticator signing outgoing
200 				   data. */
201 	void *out_context;	/* Output hash context. */
202 } omapi_connection_object_t;
203 
204 typedef struct __omapi_io_object {
205 	OMAPI_OBJECT_PREAMBLE;
206 	struct __omapi_io_object *next;
207 	int (*readfd) (omapi_object_t *);
208 	int (*writefd) (omapi_object_t *);
209 	isc_result_t (*reader) (omapi_object_t *);
210 	isc_result_t (*writer) (omapi_object_t *);
211 	isc_result_t (*reaper) (omapi_object_t *);
212 	isc_socket_t *fd;
213 	isc_boolean_t closed; /* ISC_TRUE = closed, do not use */
214 } omapi_io_object_t;
215 
216 typedef struct __omapi_generic_object {
217 	OMAPI_OBJECT_PREAMBLE;
218 	omapi_value_t **values;
219 	u_int8_t *changed;
220 	int nvalues, va_max;
221 } omapi_generic_object_t;
222 
223 typedef struct __omapi_waiter_object {
224 	OMAPI_OBJECT_PREAMBLE;
225 	int ready;
226 	isc_result_t waitstatus;
227 	struct __omapi_waiter_object *next;
228 } omapi_waiter_object_t;
229 
230 #define OMAPI_HANDLE_TABLE_SIZE 120
231 
232 typedef struct __omapi_handle_table {
233 	omapi_handle_t first, limit;
234 	omapi_handle_t next;
235 	int leafp;
236 	union {
237 		omapi_object_t *object;
238 		struct __omapi_handle_table *table;
239 	} children [OMAPI_HANDLE_TABLE_SIZE];
240 } omapi_handle_table_t;
241 
242 #include <omapip/alloc.h>
243 
244 OMAPI_OBJECT_ALLOC_DECL (omapi_protocol, omapi_protocol_object_t,
245 			 omapi_type_protocol)
246 OMAPI_OBJECT_ALLOC_DECL (omapi_protocol_listener,
247 			 omapi_protocol_listener_object_t,
248 			 omapi_type_protocol_listener)
249 OMAPI_OBJECT_ALLOC_DECL (omapi_connection,
250 			 omapi_connection_object_t, omapi_type_connection)
251 OMAPI_OBJECT_ALLOC_DECL (omapi_listener,
252 			 omapi_listener_object_t, omapi_type_listener)
253 OMAPI_OBJECT_ALLOC_DECL (omapi_io,
254 			 omapi_io_object_t, omapi_type_io_object)
255 OMAPI_OBJECT_ALLOC_DECL (omapi_waiter,
256 			 omapi_waiter_object_t, omapi_type_waiter)
257 OMAPI_OBJECT_ALLOC_DECL (omapi_generic,
258 			 omapi_generic_object_t, omapi_type_generic)
259 OMAPI_OBJECT_ALLOC_DECL (omapi_message,
260 			 omapi_message_object_t, omapi_type_message)
261 
262 isc_result_t omapi_connection_sign_data (int mode,
263 					 dst_key_t *key,
264 					 void **context,
265 					 const unsigned char *data,
266 					 const unsigned len,
267 					 omapi_typed_data_t **result);
268 isc_result_t omapi_listener_connect (omapi_connection_object_t **obj,
269 				     omapi_listener_object_t *listener,
270 				     int socket,
271 				     struct sockaddr_in *remote_addr);
272 void omapi_listener_trace_setup (void);
273 void omapi_connection_trace_setup (void);
274 void omapi_buffer_trace_setup (void);
275 void omapi_connection_register (omapi_connection_object_t *,
276 				const char *, int);
277 OMAPI_ARRAY_TYPE_DECL(omapi_listener, omapi_listener_object_t);
278 OMAPI_ARRAY_TYPE_DECL(omapi_connection, omapi_connection_object_t);
279 
280 isc_result_t omapi_handle_clear(omapi_handle_t);
281 
282 extern int log_priority;
283 extern int log_perror;
284 extern void (*log_cleanup) (void);
285 
286 void log_fatal (const char *, ...)
287 	__attribute__((__format__(__printf__,1,2))) ISC_DHCP_NORETURN;
288 int log_error (const char *, ...)
289 	__attribute__((__format__(__printf__,1,2)));
290 int log_info (const char *, ...)
291 	__attribute__((__format__(__printf__,1,2)));
292 int log_debug (const char *, ...)
293 	__attribute__((__format__(__printf__,1,2)));
294 void do_percentm (char *obuf, const char *ibuf);
295 
296 isc_result_t uerr2isc (int);
297 isc_result_t ns_rcode_to_isc (int);
298 
299 extern omapi_message_object_t *omapi_registered_messages;
300 
301 #endif /* __OMAPIP_OMAPIP_P_H__ */
302