1 /*	$NetBSD: omapip_p.h,v 1.4 2022/04/03 01:10:59 christos Exp $	*/
2 
3 /* omapip_p.h
4 
5    Private master include file for the OMAPI library. */
6 
7 /*
8  * Copyright (C) 2004-2022 Internet Systems Consortium, Inc. ("ISC")
9  * Copyright (c) 1996-2003 by Internet Software Consortium
10  *
11  * This Source Code Form is subject to the terms of the Mozilla Public
12  * License, v. 2.0. If a copy of the MPL was not distributed with this
13  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
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  *   PO Box 360
25  *   Newmarket, NH 03857 USA
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 /* Uncomment this to enable protocol level debug logs */
66 /* #define DEBUG_PROTOCOL 1 */
67 
68 #include <omapip/convert.h>
69 #include <omapip/hash.h>
70 #include <omapip/omapip.h>
71 #include <omapip/trace.h>
72 
73 /* DST_API control flags */
74 /* These are used in functions dst_sign_data and dst_verify_data */
75 #define SIG_MODE_INIT		1  /* initalize digest */
76 #define SIG_MODE_UPDATE		2  /* add data to digest */
77 #define SIG_MODE_FINAL		4  /* generate/verify signature */
78 #define SIG_MODE_ALL		(SIG_MODE_INIT|SIG_MODE_UPDATE|SIG_MODE_FINAL)
79 
80 /* OMAPI protocol header, version 1.00 */
81 typedef struct {
82 	u_int32_t authlen;	/* Length of authenticator. */
83 	u_int32_t authid;	/* Authenticator object ID. */
84 	u_int32_t op;		/* Opcode. */
85 	omapi_handle_t handle;	/* Handle of object being operated on,
86                                    or zero. */
87 	u_int32_t id;		/* Transaction ID. */
88 	u_int32_t rid;	/* ID of transaction to which this is a response. */
89 } omapi_protocol_header_t;
90 
91 #define OMAPI_PROTOCOL_VERSION	100
92 
93 #define OMAPI_OP_OPEN		1
94 #define OMAPI_OP_REFRESH	2
95 #define	OMAPI_OP_UPDATE		3
96 #define OMAPI_OP_NOTIFY		4
97 #define OMAPI_OP_STATUS		5
98 #define OMAPI_OP_DELETE		6
99 
100 typedef enum {
101 	omapi_connection_unconnected,
102 	omapi_connection_connecting,
103 	omapi_connection_connected,
104 	omapi_connection_disconnecting,
105 	omapi_connection_closed
106 } omapi_connection_state_t;
107 
108 typedef enum {
109 	omapi_protocol_intro_wait,
110 	omapi_protocol_header_wait,
111 	omapi_protocol_signature_wait,
112 	omapi_protocol_name_wait,
113 	omapi_protocol_name_length_wait,
114 	omapi_protocol_value_wait,
115 	omapi_protocol_value_length_wait
116 } omapi_protocol_state_t;
117 
118 typedef struct __omapi_message_object {
119 	OMAPI_OBJECT_PREAMBLE;
120 	struct __omapi_message_object *next, *prev;
121 	omapi_object_t *object;
122 	omapi_object_t *notify_object;
123 	struct __omapi_protocol_object *protocol_object;
124 	u_int32_t authlen;
125 	omapi_typed_data_t *authenticator;
126 	u_int32_t authid;
127 	omapi_object_t *id_object;
128 	u_int32_t op;
129 	u_int32_t h;
130 	u_int32_t id;
131 	u_int32_t rid;
132 } omapi_message_object_t;
133 
134 typedef struct __omapi_remote_auth {
135 	struct __omapi_remote_auth *next;
136 	omapi_handle_t remote_handle;
137 	omapi_object_t *a;
138 } omapi_remote_auth_t;
139 
140 typedef struct __omapi_protocol_object {
141 	OMAPI_OBJECT_PREAMBLE;
142 	u_int32_t header_size;
143 	u_int32_t protocol_version;
144 	u_int32_t next_xid;
145 
146 	omapi_protocol_state_t state;	/* Input state. */
147 	int reading_message_values;	/* True if reading message-specific
148 					   values. */
149 	omapi_message_object_t *message;	/* Incoming message. */
150 	omapi_data_string_t *name;	/* Incoming name. */
151 	omapi_typed_data_t *value;	/* Incoming value. */
152 	isc_result_t verify_result;
153 	omapi_remote_auth_t *default_auth; /* Default authinfo to use. */
154 	omapi_remote_auth_t *remote_auth_list;	/* Authenticators active on
155 						   this connection. */
156 
157 	isc_boolean_t insecure;		/* Set to allow unauthenticated
158 					   messages. */
159 
160 	isc_result_t (*verify_auth) (omapi_object_t *, omapi_auth_key_t *);
161 } omapi_protocol_object_t;
162 
163 typedef struct {
164 	OMAPI_OBJECT_PREAMBLE;
165 
166 	isc_boolean_t insecure;		/* Set to allow unauthenticated
167 					   messages. */
168 
169 	isc_result_t (*verify_auth) (omapi_object_t *, omapi_auth_key_t *);
170 } omapi_protocol_listener_object_t;
171 
172 #include <omapip/buffer.h>
173 
174 typedef struct __omapi_listener_object {
175 	OMAPI_OBJECT_PREAMBLE;
176 	int socket;		/* Connection socket. */
177 	int index;
178 	struct sockaddr_in address;
179 	isc_result_t (*verify_addr) (omapi_object_t *, omapi_addr_t *);
180 } omapi_listener_object_t;
181 
182 typedef struct __omapi_connection_object {
183 	OMAPI_OBJECT_PREAMBLE;
184 	int socket;		/* Connection socket. */
185 	int32_t index;
186 	omapi_connection_state_t state;
187 	struct sockaddr_in remote_addr;
188 	struct sockaddr_in local_addr;
189 	omapi_addr_list_t *connect_list;	/* List of addresses to which
190 						   to connect. */
191 	int cptr;		/* Current element we are connecting to. */
192 	u_int32_t bytes_needed;	/* Bytes of input needed before wakeup. */
193 	u_int32_t in_bytes;	/* Bytes of input already buffered. */
194 	omapi_buffer_t *inbufs;
195 	u_int32_t out_bytes;	/* Bytes of output in buffers. */
196 	omapi_buffer_t *outbufs;
197 	omapi_listener_object_t *listener;	/* Listener that accepted this
198 						   connection, if any. */
199 	dst_key_t *in_key;	/* Authenticator signing incoming
200 				   data. */
201 	void *in_context;	/* Input hash context. */
202 	dst_key_t *out_key;	/* Authenticator signing outgoing
203 				   data. */
204 	void *out_context;	/* Output hash context. */
205 } omapi_connection_object_t;
206 
207 typedef struct __omapi_io_object {
208 	OMAPI_OBJECT_PREAMBLE;
209 	struct __omapi_io_object *next;
210 	int (*readfd) (omapi_object_t *);
211 	int (*writefd) (omapi_object_t *);
212 	isc_result_t (*reader) (omapi_object_t *);
213 	isc_result_t (*writer) (omapi_object_t *);
214 	isc_result_t (*reaper) (omapi_object_t *);
215 	isc_socket_t *fd;
216 	isc_boolean_t closed; /* ISC_TRUE = closed, do not use */
217 } omapi_io_object_t;
218 
219 typedef struct __omapi_generic_object {
220 	OMAPI_OBJECT_PREAMBLE;
221 	omapi_value_t **values;
222 	u_int8_t *changed;
223 	int nvalues, va_max;
224 } omapi_generic_object_t;
225 
226 typedef struct __omapi_waiter_object {
227 	OMAPI_OBJECT_PREAMBLE;
228 	int ready;
229 	isc_result_t waitstatus;
230 	struct __omapi_waiter_object *next;
231 } omapi_waiter_object_t;
232 
233 #define OMAPI_HANDLE_TABLE_SIZE 120
234 
235 typedef struct __omapi_handle_table {
236 	omapi_handle_t first, limit;
237 	omapi_handle_t next;
238 	int leafp;
239 	union {
240 		omapi_object_t *object;
241 		struct __omapi_handle_table *table;
242 	} children [OMAPI_HANDLE_TABLE_SIZE];
243 } omapi_handle_table_t;
244 
245 #include <omapip/alloc.h>
246 
247 OMAPI_OBJECT_ALLOC_DECL (omapi_protocol, omapi_protocol_object_t,
248 			 omapi_type_protocol)
249 OMAPI_OBJECT_ALLOC_DECL (omapi_protocol_listener,
250 			 omapi_protocol_listener_object_t,
251 			 omapi_type_protocol_listener)
252 OMAPI_OBJECT_ALLOC_DECL (omapi_connection,
253 			 omapi_connection_object_t, omapi_type_connection)
254 OMAPI_OBJECT_ALLOC_DECL (omapi_listener,
255 			 omapi_listener_object_t, omapi_type_listener)
256 OMAPI_OBJECT_ALLOC_DECL (omapi_io,
257 			 omapi_io_object_t, omapi_type_io_object)
258 OMAPI_OBJECT_ALLOC_DECL (omapi_waiter,
259 			 omapi_waiter_object_t, omapi_type_waiter)
260 OMAPI_OBJECT_ALLOC_DECL (omapi_generic,
261 			 omapi_generic_object_t, omapi_type_generic)
262 OMAPI_OBJECT_ALLOC_DECL (omapi_message,
263 			 omapi_message_object_t, omapi_type_message)
264 
265 isc_result_t omapi_connection_sign_data (int mode,
266 					 dst_key_t *key,
267 					 void **context,
268 					 const unsigned char *data,
269 					 const unsigned len,
270 					 omapi_typed_data_t **result);
271 isc_result_t omapi_listener_connect (omapi_connection_object_t **obj,
272 				     omapi_listener_object_t *listener,
273 				     int socket,
274 				     struct sockaddr_in *remote_addr);
275 void omapi_listener_trace_setup (void);
276 void omapi_connection_trace_setup (void);
277 void omapi_buffer_trace_setup (void);
278 void omapi_connection_register (omapi_connection_object_t *,
279 				const char *, int);
280 OMAPI_ARRAY_TYPE_DECL(omapi_listener, omapi_listener_object_t);
281 OMAPI_ARRAY_TYPE_DECL(omapi_connection, omapi_connection_object_t);
282 
283 isc_result_t omapi_handle_clear(omapi_handle_t);
284 
285 extern int log_perror;
286 extern void (*log_cleanup) (void);
287 
288 void log_fatal (const char *, ...) __sysloglike(1, 2) ISC_DHCP_NORETURN;
289 int log_error (const char *, ...) __sysloglike(1, 2);
290 int log_info (const char *, ...) __sysloglike(1, 2);
291 int log_debug (const char *, ...) __sysloglike(1, 2);
292 
293 void do_percentm (char *obuf, size_t obufsiz, const char *ibuf);
294 
295 isc_result_t uerr2isc (int);
296 isc_result_t ns_rcode_to_isc (int);
297 
298 extern omapi_message_object_t *omapi_registered_messages;
299 
300 #endif /* __OMAPIP_OMAPIP_P_H__ */
301