message.c 22.4 KB
Newer Older
Ted Lemon's avatar
Ted Lemon committed
1 2 3 4 5
/* message.c

   Subroutines for dealing with message objects. */

/*
6
 * Copyright (c) 2004-2017 by Internet Systems Consortium, Inc. ("ISC")
7
 * Copyright (c) 1999-2003 by Internet Software Consortium
Ted Lemon's avatar
Ted Lemon committed
8
 *
9 10 11
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
Ted Lemon's avatar
Ted Lemon committed
12
 *
13 14 15 16 17 18 19
 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
 * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
Ted Lemon's avatar
Ted Lemon committed
20
 *
21 22 23 24
 *   Internet Systems Consortium, Inc.
 *   950 Charter Street
 *   Redwood City, CA 94063
 *   <info@isc.org>
25
 *   https://www.isc.org/
Ted Lemon's avatar
Ted Lemon committed
26
 *
Ted Lemon's avatar
Ted Lemon committed
27 28
 */

29 30
#include "dhcpd.h"

31
#include <omapip/omapip_p.h>
Ted Lemon's avatar
Ted Lemon committed
32

33 34 35
OMAPI_OBJECT_ALLOC (omapi_message,
		    omapi_message_object_t, omapi_type_message)

Ted Lemon's avatar
Ted Lemon committed
36 37
omapi_message_object_t *omapi_registered_messages;

Ted Lemon's avatar
Ted Lemon committed
38
isc_result_t omapi_message_new (omapi_object_t **o, const char *file, int line)
Ted Lemon's avatar
Ted Lemon committed
39 40
{
	omapi_message_object_t *m;
41
	omapi_object_t *g;
Ted Lemon's avatar
Ted Lemon committed
42 43
	isc_result_t status;

Ted Lemon's avatar
Ted Lemon committed
44 45 46 47
	m = (omapi_message_object_t *)0;
	status = omapi_message_allocate (&m, file, line);
	if (status != ISC_R_SUCCESS)
		return status;
Ted Lemon's avatar
Ted Lemon committed
48

49
	g = (omapi_object_t *)0;
Ted Lemon's avatar
Ted Lemon committed
50
	status = omapi_generic_new (&g, file, line);
51
	if (status != ISC_R_SUCCESS) {
52
		dfree (m, file, line);
53 54
		return status;
	}
Ted Lemon's avatar
Ted Lemon committed
55
	status = omapi_object_reference (&m -> inner, g, file, line);
56
	if (status != ISC_R_SUCCESS) {
Ted Lemon's avatar
Ted Lemon committed
57
		omapi_object_dereference ((omapi_object_t **)&m, file, line);
58
		omapi_object_dereference (&g, file, line);
59 60 61
		return status;
	}
	status = omapi_object_reference (&g -> outer,
Ted Lemon's avatar
Ted Lemon committed
62
					 (omapi_object_t *)m, file, line);
63 64

	if (status != ISC_R_SUCCESS) {
Ted Lemon's avatar
Ted Lemon committed
65
		omapi_object_dereference ((omapi_object_t **)&m, file, line);
66
		omapi_object_dereference (&g, file, line);
67 68 69
		return status;
	}

Ted Lemon's avatar
Ted Lemon committed
70
	status = omapi_object_reference (o, (omapi_object_t *)m, file, line);
Ted Lemon's avatar
Ted Lemon committed
71
	omapi_message_dereference (&m, file, line);
Ted Lemon's avatar
Ted Lemon committed
72
	omapi_object_dereference (&g, file, line);
73 74 75
	if (status != ISC_R_SUCCESS)
		return status;

Ted Lemon's avatar
Ted Lemon committed
76 77 78 79 80 81 82 83 84 85 86 87
	return status;
}

isc_result_t omapi_message_set_value (omapi_object_t *h,
				      omapi_object_t *id,
				      omapi_data_string_t *name,
				      omapi_typed_data_t *value)
{
	omapi_message_object_t *m;
	isc_result_t status;

	if (h -> type != omapi_type_message)
88
		return DHCP_R_INVALIDARG;
Ted Lemon's avatar
Ted Lemon committed
89 90 91 92 93 94 95
	m = (omapi_message_object_t *)h;

	/* Can't set authlen. */

	/* Can set authenticator, but the value must be typed data. */
	if (!omapi_ds_strcmp (name, "authenticator")) {
		if (m -> authenticator)
96 97 98
			omapi_typed_data_dereference (&m -> authenticator,
						      MDL);
		omapi_typed_data_reference (&m -> authenticator, value, MDL);
Ted Lemon's avatar
Ted Lemon committed
99 100
		return ISC_R_SUCCESS;

101 102
	} else if (!omapi_ds_strcmp (name, "object")) {
		if (value -> type != omapi_datatype_object)
103
			return DHCP_R_INVALIDARG;
104
		if (m -> object)
105 106
			omapi_object_dereference (&m -> object, MDL);
		omapi_object_reference (&m -> object, value -> u.object, MDL);
107 108
		return ISC_R_SUCCESS;

109 110
	} else if (!omapi_ds_strcmp (name, "notify-object")) {
		if (value -> type != omapi_datatype_object)
111
			return DHCP_R_INVALIDARG;
112
		if (m -> notify_object)
113
			omapi_object_dereference (&m -> notify_object, MDL);
114
		omapi_object_reference (&m -> notify_object,
Ted Lemon's avatar
Ted Lemon committed
115
					value -> u.object, MDL);
116 117
		return ISC_R_SUCCESS;

Ted Lemon's avatar
Ted Lemon committed
118 119 120
	/* Can set authid, but it has to be an integer. */
	} else if (!omapi_ds_strcmp (name, "authid")) {
		if (value -> type != omapi_datatype_int)
121
			return DHCP_R_INVALIDARG;
Ted Lemon's avatar
Ted Lemon committed
122
		m -> authid = value -> u.integer;
123
		return ISC_R_SUCCESS;
Ted Lemon's avatar
Ted Lemon committed
124 125 126 127

	/* Can set op, but it has to be an integer. */
	} else if (!omapi_ds_strcmp (name, "op")) {
		if (value -> type != omapi_datatype_int)
128
			return DHCP_R_INVALIDARG;
Ted Lemon's avatar
Ted Lemon committed
129
		m -> op = value -> u.integer;
130
		return ISC_R_SUCCESS;
Ted Lemon's avatar
Ted Lemon committed
131 132 133 134

	/* Handle also has to be an integer. */
	} else if (!omapi_ds_strcmp (name, "handle")) {
		if (value -> type != omapi_datatype_int)
135
			return DHCP_R_INVALIDARG;
Ted Lemon's avatar
Ted Lemon committed
136
		m -> h = value -> u.integer;
137
		return ISC_R_SUCCESS;
Ted Lemon's avatar
Ted Lemon committed
138 139 140 141

	/* Transaction ID has to be an integer. */
	} else if (!omapi_ds_strcmp (name, "id")) {
		if (value -> type != omapi_datatype_int)
142
			return DHCP_R_INVALIDARG;
Ted Lemon's avatar
Ted Lemon committed
143
		m -> id = value -> u.integer;
144
		return ISC_R_SUCCESS;
Ted Lemon's avatar
Ted Lemon committed
145 146 147 148

	/* Remote transaction ID has to be an integer. */
	} else if (!omapi_ds_strcmp (name, "rid")) {
		if (value -> type != omapi_datatype_int)
149
			return DHCP_R_INVALIDARG;
Ted Lemon's avatar
Ted Lemon committed
150
		m -> rid = value -> u.integer;
151
		return ISC_R_SUCCESS;
Ted Lemon's avatar
Ted Lemon committed
152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171
	}

	/* Try to find some inner object that can take the value. */
	if (h -> inner && h -> inner -> type -> set_value) {
		status = ((*(h -> inner -> type -> set_value))
			  (h -> inner, id, name, value));
		if (status == ISC_R_SUCCESS)
			return status;
	}
			  
	return ISC_R_NOTFOUND;
}

isc_result_t omapi_message_get_value (omapi_object_t *h,
				      omapi_object_t *id,
				      omapi_data_string_t *name,
				      omapi_value_t **value)
{
	omapi_message_object_t *m;
	if (h -> type != omapi_type_message)
172
		return DHCP_R_INVALIDARG;
Ted Lemon's avatar
Ted Lemon committed
173 174 175 176
	m = (omapi_message_object_t *)h;

	/* Look for values that are in the message data structure. */
	if (!omapi_ds_strcmp (name, "authlen"))
177
		return omapi_make_int_value (value, name, (int)m -> authlen,
Ted Lemon's avatar
Ted Lemon committed
178
					     MDL);
Ted Lemon's avatar
Ted Lemon committed
179 180
	else if (!omapi_ds_strcmp (name, "authenticator")) {
		if (m -> authenticator)
Ted Lemon's avatar
Ted Lemon committed
181 182
			return omapi_make_value (value, name,
						 m -> authenticator, MDL);
Ted Lemon's avatar
Ted Lemon committed
183 184 185
		else
			return ISC_R_NOTFOUND;
	} else if (!omapi_ds_strcmp (name, "authid")) {
Ted Lemon's avatar
Ted Lemon committed
186 187
		return omapi_make_int_value (value,
					     name, (int)m -> authid, MDL);
Ted Lemon's avatar
Ted Lemon committed
188
	} else if (!omapi_ds_strcmp (name, "op")) {
Ted Lemon's avatar
Ted Lemon committed
189
		return omapi_make_int_value (value, name, (int)m -> op, MDL);
Ted Lemon's avatar
Ted Lemon committed
190
	} else if (!omapi_ds_strcmp (name, "handle")) {
Ted Lemon's avatar
Ted Lemon committed
191
		return omapi_make_int_value (value, name, (int)m -> h, MDL);
Ted Lemon's avatar
Ted Lemon committed
192
	} else if (!omapi_ds_strcmp (name, "id")) {
Ted Lemon's avatar
Ted Lemon committed
193
		return omapi_make_int_value (value, name, (int)m -> id, MDL);
Ted Lemon's avatar
Ted Lemon committed
194
	} else if (!omapi_ds_strcmp (name, "rid")) {
Ted Lemon's avatar
Ted Lemon committed
195
		return omapi_make_int_value (value, name, (int)m -> rid, MDL);
Ted Lemon's avatar
Ted Lemon committed
196 197 198 199 200 201 202 203 204
	}

	/* See if there's an inner object that has the value. */
	if (h -> inner && h -> inner -> type -> get_value)
		return (*(h -> inner -> type -> get_value))
			(h -> inner, id, name, value);
	return ISC_R_NOTFOUND;
}

Ted Lemon's avatar
Ted Lemon committed
205 206
isc_result_t omapi_message_destroy (omapi_object_t *h,
				    const char *file, int line)
Ted Lemon's avatar
Ted Lemon committed
207 208 209
{
	omapi_message_object_t *m;
	if (h -> type != omapi_type_message)
210
		return DHCP_R_INVALIDARG;
211
	m = (omapi_message_object_t *)h;
Ted Lemon's avatar
Ted Lemon committed
212
	if (m -> authenticator) {
Ted Lemon's avatar
Ted Lemon committed
213
		omapi_typed_data_dereference (&m -> authenticator, file, line);
Ted Lemon's avatar
Ted Lemon committed
214 215 216 217
	}
	if (!m -> prev && omapi_registered_messages != m)
		omapi_message_unregister (h);
	if (m -> id_object)
218
		omapi_object_dereference (&m -> id_object, file, line);
Ted Lemon's avatar
Ted Lemon committed
219
	if (m -> object)
220 221 222 223 224
		omapi_object_dereference (&m -> object, file, line);
	if (m -> notify_object)
		omapi_object_dereference (&m -> notify_object, file, line);
	if (m -> protocol_object)
		omapi_protocol_dereference (&m -> protocol_object, file, line);
Ted Lemon's avatar
Ted Lemon committed
225 226 227 228
	return ISC_R_SUCCESS;
}

isc_result_t omapi_message_signal_handler (omapi_object_t *h,
229
					   const char *name, va_list ap)
Ted Lemon's avatar
Ted Lemon committed
230
{
Ted Lemon's avatar
Ted Lemon committed
231
	omapi_message_object_t *m;
Ted Lemon's avatar
Ted Lemon committed
232
	if (h -> type != omapi_type_message)
233
		return DHCP_R_INVALIDARG;
Ted Lemon's avatar
Ted Lemon committed
234
	m = (omapi_message_object_t *)h;
Ted Lemon's avatar
Ted Lemon committed
235
	
236 237 238
	if (!strcmp (name, "status")) {
		if (m -> notify_object &&
		    m -> notify_object -> type -> signal_handler)
239 240
			return ((m -> notify_object -> type -> signal_handler))
				(m -> notify_object, name, ap);
241 242 243
		else if (m -> object && m -> object -> type -> signal_handler)
			return ((m -> object -> type -> signal_handler))
				(m -> object, name, ap);
Ted Lemon's avatar
Ted Lemon committed
244
	}
Ted Lemon's avatar
Ted Lemon committed
245 246 247 248 249 250 251 252 253 254 255 256 257 258
	if (h -> inner && h -> inner -> type -> signal_handler)
		return (*(h -> inner -> type -> signal_handler)) (h -> inner,
								  name, ap);
	return ISC_R_NOTFOUND;
}

/* Write all the published values associated with the object through the
   specified connection. */

isc_result_t omapi_message_stuff_values (omapi_object_t *c,
					 omapi_object_t *id,
					 omapi_object_t *m)
{
	if (m -> type != omapi_type_message)
259
		return DHCP_R_INVALIDARG;
Ted Lemon's avatar
Ted Lemon committed
260 261 262 263 264 265 266 267 268 269 270 271

	if (m -> inner && m -> inner -> type -> stuff_values)
		return (*(m -> inner -> type -> stuff_values)) (c, id,
								m -> inner);
	return ISC_R_SUCCESS;
}

isc_result_t omapi_message_register (omapi_object_t *mo)
{
	omapi_message_object_t *m;

	if (mo -> type != omapi_type_message)
272
		return DHCP_R_INVALIDARG;
Ted Lemon's avatar
Ted Lemon committed
273 274 275 276
	m = (omapi_message_object_t *)mo;
	
	/* Already registered? */
	if (m -> prev || m -> next || omapi_registered_messages == m)
277
		return DHCP_R_INVALIDARG;
Ted Lemon's avatar
Ted Lemon committed
278 279 280 281

	if (omapi_registered_messages) {
		omapi_object_reference
			((omapi_object_t **)&m -> next,
282
			 (omapi_object_t *)omapi_registered_messages, MDL);
Ted Lemon's avatar
Ted Lemon committed
283 284
		omapi_object_reference
			((omapi_object_t **)&omapi_registered_messages -> prev,
285
			 (omapi_object_t *)m, MDL);
Ted Lemon's avatar
Ted Lemon committed
286
		omapi_object_dereference
287
			((omapi_object_t **)&omapi_registered_messages, MDL);
Ted Lemon's avatar
Ted Lemon committed
288 289 290
	}
	omapi_object_reference
		((omapi_object_t **)&omapi_registered_messages,
291
		 (omapi_object_t *)m, MDL);
Ted Lemon's avatar
Ted Lemon committed
292 293 294 295 296 297 298 299 300
	return ISC_R_SUCCESS;;
}

isc_result_t omapi_message_unregister (omapi_object_t *mo)
{
	omapi_message_object_t *m;
	omapi_message_object_t *n;

	if (mo -> type != omapi_type_message)
301
		return DHCP_R_INVALIDARG;
Ted Lemon's avatar
Ted Lemon committed
302 303 304 305
	m = (omapi_message_object_t *)mo;
	
	/* Not registered? */
	if (!m -> prev && omapi_registered_messages != m)
306
		return DHCP_R_INVALIDARG;
Ted Lemon's avatar
Ted Lemon committed
307 308 309 310

	n = (omapi_message_object_t *)0;
	if (m -> next) {
		omapi_object_reference ((omapi_object_t **)&n,
Ted Lemon's avatar
Ted Lemon committed
311 312
					(omapi_object_t *)m -> next, MDL);
		omapi_object_dereference ((omapi_object_t **)&m -> next, MDL);
313
		omapi_object_dereference ((omapi_object_t **)&n -> prev, MDL);
Ted Lemon's avatar
Ted Lemon committed
314 315 316 317
	}
	if (m -> prev) {
		omapi_message_object_t *tmp = (omapi_message_object_t *)0;
		omapi_object_reference ((omapi_object_t **)&tmp,
Ted Lemon's avatar
Ted Lemon committed
318 319
					(omapi_object_t *)m -> prev, MDL);
		omapi_object_dereference ((omapi_object_t **)&m -> prev, MDL);
Ted Lemon's avatar
Ted Lemon committed
320 321
		if (tmp -> next)
			omapi_object_dereference
Ted Lemon's avatar
Ted Lemon committed
322
				((omapi_object_t **)&tmp -> next, MDL);
Ted Lemon's avatar
Ted Lemon committed
323 324 325
		if (n)
			omapi_object_reference
				((omapi_object_t **)&tmp -> next,
Ted Lemon's avatar
Ted Lemon committed
326 327
				 (omapi_object_t *)n, MDL);
		omapi_object_dereference ((omapi_object_t **)&tmp, MDL);
Ted Lemon's avatar
Ted Lemon committed
328 329
	} else {
		omapi_object_dereference
Ted Lemon's avatar
Ted Lemon committed
330
			((omapi_object_t **)&omapi_registered_messages, MDL);
Ted Lemon's avatar
Ted Lemon committed
331 332 333
		if (n)
			omapi_object_reference
				((omapi_object_t **)&omapi_registered_messages,
Ted Lemon's avatar
Ted Lemon committed
334
				 (omapi_object_t *)n, MDL);
Ted Lemon's avatar
Ted Lemon committed
335 336
	}
	if (n)
Ted Lemon's avatar
Ted Lemon committed
337
		omapi_object_dereference ((omapi_object_t **)&n, MDL);
Ted Lemon's avatar
Ted Lemon committed
338 339
	return ISC_R_SUCCESS;
}
340

341
#ifdef DEBUG_PROTOCOL
342
const char *omapi_message_op_name(int op) {
343 344 345 346 347 348 349 350 351 352 353 354
	switch (op) {
	case OMAPI_OP_OPEN:    return "OMAPI_OP_OPEN";
	case OMAPI_OP_REFRESH: return "OMAPI_OP_REFRESH";
	case OMAPI_OP_UPDATE:  return "OMAPI_OP_UPDATE";
	case OMAPI_OP_STATUS:  return "OMAPI_OP_STATUS";
	case OMAPI_OP_DELETE:  return "OMAPI_OP_DELETE";
	case OMAPI_OP_NOTIFY:  return "OMAPI_OP_NOTIFY";
	default:               return "(unknown op)";
	}
}
#endif

Ted Lemon's avatar
Ted Lemon committed
355 356 357
static isc_result_t
omapi_message_process_internal (omapi_object_t *, omapi_object_t *);

358
isc_result_t omapi_message_process (omapi_object_t *mo, omapi_object_t *po)
Ted Lemon's avatar
Ted Lemon committed
359 360
{
	isc_result_t status;
361
#if defined (DEBUG_MEMORY_LEAKAGE) && 0
Ted Lemon's avatar
Ted Lemon committed
362 363 364 365 366 367 368 369 370 371 372
	unsigned long previous_outstanding = dmalloc_outstanding;
#endif

	status = omapi_message_process_internal (mo, po);

#if defined (DEBUG_MEMORY_LEAKAGE) && 0
	log_info ("generation %ld: %ld new, %ld outstanding, %ld long-term",
		  dmalloc_generation,
		  dmalloc_outstanding - previous_outstanding,
		  dmalloc_outstanding, dmalloc_longterm);
#endif
373
#if defined (DEBUG_MEMORY_LEAKAGE) && 0
Ted Lemon's avatar
Ted Lemon committed
374 375 376 377 378 379 380 381 382 383 384
	dmalloc_dump_outstanding ();
#endif
#if defined (DEBUG_RC_HISTORY_EXHAUSTIVELY) && 0
	dump_rc_history ();
#endif

	return status;
}

static isc_result_t
omapi_message_process_internal (omapi_object_t *mo, omapi_object_t *po)
385 386 387
{
	omapi_message_object_t *message, *m;
	omapi_object_t *object = (omapi_object_t *)0;
Ted Lemon's avatar
Ted Lemon committed
388
	omapi_value_t *tv = (omapi_value_t *)0;
389 390
	unsigned long create, update, exclusive;
	unsigned long wsi;
391 392 393 394
	isc_result_t status, waitstatus;
	omapi_object_type_t *type;

	if (mo -> type != omapi_type_message)
395
		return DHCP_R_INVALIDARG;
396 397
	message = (omapi_message_object_t *)mo;

398 399 400 401 402 403 404
#ifdef DEBUG_PROTOCOL
	log_debug ("omapi_message_process(): "
		   "op=%s  handle=%#x  id=%#x  rid=%#x",
		   omapi_message_op_name (message -> op),
		   message -> h, message -> id, message -> rid);
#endif

405 406 407 408 409 410 411 412 413
	if (message -> rid) {
		for (m = omapi_registered_messages; m; m = m -> next)
			if (m -> id == message -> rid)
				break;
		/* If we don't have a real message corresponding to
		   the message ID to which this message claims it is a
		   response, something's fishy. */
		if (!m)
			return ISC_R_NOTFOUND;
414 415 416 417 418
		/* The authenticator on responses must match the initial
		   message. */
		if (message -> authid != m -> authid)
			return ISC_R_NOTFOUND;
	} else {
Ted Lemon's avatar
Ted Lemon committed
419
		m = (omapi_message_object_t *)0;
420

421 422
		/* All messages must have an authenticator, with the exception
		   of messages that are opening a new authenticator. */
423 424 425
		if (omapi_protocol_authenticated(po) &&
		    !message->id_object &&
		    message->op != OMAPI_OP_OPEN) {
426
			return omapi_protocol_send_status
427 428
				(po, message->id_object, DHCP_R_NOKEYS,
				 message->id, "No authenticator on message");
429 430 431
		}
	}

432 433 434
	switch (message -> op) {
	      case OMAPI_OP_OPEN:
		if (m) {
Ted Lemon's avatar
Ted Lemon committed
435
			return omapi_protocol_send_status
436 437
				(po, message->id_object, DHCP_R_INVALIDARG,
				 message->id, "OPEN can't be a response");
438 439 440 441
		}

		/* Get the type of the requested object, if one was
		   specified. */
442
		status = omapi_get_value_str (mo, message -> id_object,
443 444 445 446 447 448 449 450 451 452 453 454
					      "type", &tv);
		if (status == ISC_R_SUCCESS &&
		    (tv -> value -> type == omapi_datatype_data ||
		     tv -> value -> type == omapi_datatype_string)) {
			for (type = omapi_object_types;
			     type; type = type -> next)
				if (!omapi_td_strcmp (tv -> value,
						      type -> name))
					break;
		} else
			type = (omapi_object_type_t *)0;
		if (tv)
Ted Lemon's avatar
Ted Lemon committed
455
			omapi_value_dereference (&tv, MDL);
456

457 458
		/* If this object had no authenticator, the requested object
		   must be an authenticator object. */
459 460
		if (omapi_protocol_authenticated(po) &&
		    !message->id_object &&
461 462
		    type != omapi_type_auth_key) {
			return omapi_protocol_send_status
463 464
				(po, message->id_object, DHCP_R_NOKEYS,
				 message->id, "No authenticator on message");
465 466
		}

467
		/* Get the create flag. */
468
		status = omapi_get_value_str (mo, message -> id_object,
469 470
					      "create", &tv);
		if (status == ISC_R_SUCCESS) {
Ted Lemon's avatar
Ted Lemon committed
471
			status = omapi_get_int_value (&create, tv -> value);
Ted Lemon's avatar
Ted Lemon committed
472
			omapi_value_dereference (&tv, MDL);
473
			if (status != ISC_R_SUCCESS) {
Ted Lemon's avatar
Ted Lemon committed
474
				return omapi_protocol_send_status
475
					(po, message -> id_object,
Ted Lemon's avatar
Ted Lemon committed
476
					 status, message -> id,
477 478 479 480 481 482
					 "invalid create flag value");
			}
		} else
			create = 0;

		/* Get the update flag. */
483
		status = omapi_get_value_str (mo, message -> id_object,
484 485
					      "update", &tv);
		if (status == ISC_R_SUCCESS) {
Ted Lemon's avatar
Ted Lemon committed
486
			status = omapi_get_int_value (&update, tv -> value);
Ted Lemon's avatar
Ted Lemon committed
487
			omapi_value_dereference (&tv, MDL);
488
			if (status != ISC_R_SUCCESS) {
Ted Lemon's avatar
Ted Lemon committed
489
				return omapi_protocol_send_status
490
					(po, message -> id_object,
Ted Lemon's avatar
Ted Lemon committed
491
					 status, message -> id,
492 493 494 495 496 497
					 "invalid update flag value");
			}
		} else
			update = 0;

		/* Get the exclusive flag. */
498
		status = omapi_get_value_str (mo, message -> id_object,
499 500
					      "exclusive", &tv);
		if (status == ISC_R_SUCCESS) {
Ted Lemon's avatar
Ted Lemon committed
501
			status = omapi_get_int_value (&exclusive, tv -> value);
Ted Lemon's avatar
Ted Lemon committed
502
			omapi_value_dereference (&tv, MDL);
503
			if (status != ISC_R_SUCCESS) {
Ted Lemon's avatar
Ted Lemon committed
504
				return omapi_protocol_send_status
505
					(po, message -> id_object,
Ted Lemon's avatar
Ted Lemon committed
506
					 status, message -> id,
507 508 509 510 511 512 513 514 515
					 "invalid exclusive flag value");
			}
		} else
			exclusive = 0;

		/* If we weren't given a type, look the object up with
                   the handle. */
		if (!type) {
			if (create) {
Ted Lemon's avatar
Ted Lemon committed
516
				return omapi_protocol_send_status
517 518 519
					(po, message->id_object,
					 DHCP_R_INVALIDARG,
					 message->id,
520 521 522 523 524 525 526 527
					 "type required on create");
			}
			goto refresh;
		}

		/* If the type doesn't provide a lookup method, we can't
		   look up the object. */
		if (!type -> lookup) {
Ted Lemon's avatar
Ted Lemon committed
528
			return omapi_protocol_send_status
529
				(po, message -> id_object,
Ted Lemon's avatar
Ted Lemon committed
530
				 ISC_R_NOTIMPLEMENTED, message -> id,
531 532
				 "unsearchable object type");
		}
533

534
		status = (*(type -> lookup)) (&object, message -> id_object,
535 536
					      message -> object);

537 538
		if (status != ISC_R_SUCCESS &&
		    status != ISC_R_NOTFOUND &&
539
		    status != DHCP_R_NOKEYS) {
Ted Lemon's avatar
Ted Lemon committed
540
			return omapi_protocol_send_status
541
				(po, message -> id_object,
Ted Lemon's avatar
Ted Lemon committed
542
				 status, message -> id,
543 544 545 546 547 548
				 "object lookup failed");
		}

		/* If we didn't find the object and we aren't supposed to
		   create it, return an error. */
		if (status == ISC_R_NOTFOUND && !create) {
Ted Lemon's avatar
Ted Lemon committed
549
			return omapi_protocol_send_status
550
				(po, message -> id_object,
Ted Lemon's avatar
Ted Lemon committed
551
				 ISC_R_NOTFOUND, message -> id,
552 553 554 555 556 557 558
				 "no object matches specification");
		}			

		/* If we found an object, we're supposed to be creating an
		   object, and we're not supposed to have found an object,
		   return an error. */
		if (status == ISC_R_SUCCESS && create && exclusive) {
Ted Lemon's avatar
Ted Lemon committed
559
			omapi_object_dereference (&object, MDL);
Ted Lemon's avatar
Ted Lemon committed
560
			return omapi_protocol_send_status
561
				(po, message -> id_object,
Ted Lemon's avatar
Ted Lemon committed
562
				 ISC_R_EXISTS, message -> id,
563 564 565 566 567
				 "specified object already exists");
		}

		/* If we're creating the object, do it now. */
		if (!object) {
Ted Lemon's avatar
Ted Lemon committed
568
			status = omapi_object_create (&object,
569
						      message -> id_object,
Ted Lemon's avatar
Ted Lemon committed
570
						      type);
571
			if (status != ISC_R_SUCCESS) {
Ted Lemon's avatar
Ted Lemon committed
572
				return omapi_protocol_send_status
573
					(po, message -> id_object,
Ted Lemon's avatar
Ted Lemon committed
574
					 status, message -> id,
575 576 577 578 579 580
					 "can't create new object");
			}
		}

		/* If we're updating it, do so now. */
		if (create || update) {
581 582 583 584 585 586 587 588 589
			/* This check does not belong here. */
			if (object -> type == omapi_type_auth_key) {
				omapi_object_dereference (&object, MDL);
				return omapi_protocol_send_status
					(po, message -> id_object,
					 status, message -> id,
					 "can't update object");
			}

590
			status = omapi_object_update (object,
591
						      message -> id_object,
592
						      message -> object,
Ted Lemon's avatar
Ted Lemon committed
593
						      message -> h);
594
			if (status != ISC_R_SUCCESS) {
Ted Lemon's avatar
Ted Lemon committed
595
				omapi_object_dereference (&object, MDL);
Ted Lemon's avatar
Ted Lemon committed
596
				return omapi_protocol_send_status
597
					(po, message -> id_object,
Ted Lemon's avatar
Ted Lemon committed
598
					 status, message -> id,
599 600 601
					 "can't update object");
			}
		}
602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624

		/* If this is an authenticator object, add it to the active
		   set for the connection. */
		if (object -> type == omapi_type_auth_key) {
			omapi_handle_t handle;
			status = omapi_object_handle (&handle, object);
			if (status != ISC_R_SUCCESS) {
				omapi_object_dereference (&object, MDL);
				return omapi_protocol_send_status
					(po, message -> id_object,
					 status, message -> id,
					 "can't select authenticator");
			}

			status = omapi_protocol_add_auth (po, object, handle);
			if (status != ISC_R_SUCCESS) {
				omapi_object_dereference (&object, MDL);
				return omapi_protocol_send_status
					(po, message -> id_object,
					 status, message -> id,
					 "can't select authenticator");
			}
		}
625 626 627 628 629 630 631
		
		/* Now send the new contents of the object back in
		   response. */
		goto send;

	      case OMAPI_OP_REFRESH:
	      refresh:
Ted Lemon's avatar
Ted Lemon committed
632
		status = omapi_handle_lookup (&object, message -> h);
633
		if (status != ISC_R_SUCCESS) {
Ted Lemon's avatar
Ted Lemon committed
634
			return omapi_protocol_send_status
635
				(po, message -> id_object,
Ted Lemon's avatar
Ted Lemon committed
636
				 status, message -> id,
637 638 639
				 "no matching handle");
		}
	      send:		
640
		status = omapi_protocol_send_update (po, message -> id_object,
Ted Lemon's avatar
Ted Lemon committed
641
						     message -> id, object);
Ted Lemon's avatar
Ted Lemon committed
642
		omapi_object_dereference (&object, MDL);
Ted Lemon's avatar
Ted Lemon committed
643
		return status;
644 645

	      case OMAPI_OP_UPDATE:
646
		if (m && m -> object) {
647 648
			status = omapi_object_reference (&object, m -> object,
									MDL);
649
		} else {
Ted Lemon's avatar
Ted Lemon committed
650
			status = omapi_handle_lookup (&object, message -> h);
651
			if (status != ISC_R_SUCCESS) {
Ted Lemon's avatar
Ted Lemon committed
652
				return omapi_protocol_send_status
653
					(po, message -> id_object,
Ted Lemon's avatar
Ted Lemon committed
654
					 status, message -> id,
655 656 657 658
					 "no matching handle");
			}
		}

659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677
		if (object -> type == omapi_type_auth_key ||
		    (object -> inner &&
		     object -> inner -> type == omapi_type_auth_key)) {
			if (!m) {
				omapi_object_dereference (&object, MDL);
				return omapi_protocol_send_status
					(po, message -> id_object,
					 status, message -> id,
					 "cannot update authenticator");
			}
			
			status = omapi_protocol_add_auth (po, object,
							  message -> h);
		} else {
			status = omapi_object_update (object,
						      message -> id_object,
						      message -> object,
						      message -> h);
		}
678
		if (status != ISC_R_SUCCESS) {
Ted Lemon's avatar
Ted Lemon committed
679
			omapi_object_dereference (&object, MDL);
680
			if (!message -> rid)
Ted Lemon's avatar
Ted Lemon committed
681
				return omapi_protocol_send_status
682
					(po, message -> id_object,
Ted Lemon's avatar
Ted Lemon committed
683
					 status, message -> id,
684 685 686
					 "can't update object");
			if (m)
				omapi_signal ((omapi_object_t *)m,
Ted Lemon's avatar
Ted Lemon committed
687 688
					      "status", status,
					      (omapi_typed_data_t *)0);
689 690 691
			return ISC_R_SUCCESS;
		}
		if (!message -> rid)
Ted Lemon's avatar
Ted Lemon committed
692
			status = omapi_protocol_send_status
693
				(po, message -> id_object, ISC_R_SUCCESS,
Ted Lemon's avatar
Ted Lemon committed
694
				 message -> id, (char *)0);
695
		if (m) {
696
			omapi_signal ((omapi_object_t *)m,
Ted Lemon's avatar
Ted Lemon committed
697 698
				      "status", ISC_R_SUCCESS,
				      (omapi_typed_data_t *)0);
699 700 701 702 703
			omapi_message_unregister ((omapi_object_t *)m);
		}

		omapi_object_dereference (&object, MDL);

Ted Lemon's avatar
Ted Lemon committed
704
		return status;
705 706

	      case OMAPI_OP_NOTIFY:
Ted Lemon's avatar
Ted Lemon committed
707
		return omapi_protocol_send_status
708
			(po, message -> id_object, ISC_R_NOTIMPLEMENTED,
Ted Lemon's avatar
Ted Lemon committed
709
			 message -> id, "notify not implemented yet");
710

Ted Lemon's avatar
Ted Lemon committed
711 712
	      case OMAPI_OP_STATUS:
		/* The return status of a request. */
713 714 715 716
		if (!m)
			return ISC_R_UNEXPECTED;

		/* Get the wait status. */
717
		status = omapi_get_value_str (mo, message -> id_object,
718 719
					      "result", &tv);
		if (status == ISC_R_SUCCESS) {
720 721
			status = omapi_get_int_value (&wsi, tv -> value);
			waitstatus = wsi;
Ted Lemon's avatar
Ted Lemon committed
722
			omapi_value_dereference (&tv, MDL);
723 724 725 726 727
			if (status != ISC_R_SUCCESS)
				waitstatus = ISC_R_UNEXPECTED;
		} else
			waitstatus = ISC_R_UNEXPECTED;

728
		status = omapi_get_value_str (mo, message -> id_object,
Ted Lemon's avatar
Ted Lemon committed
729 730 731
					      "message", &tv);
		omapi_signal ((omapi_object_t *)m, "status", waitstatus, tv);
		if (status == ISC_R_SUCCESS)
Ted Lemon's avatar
Ted Lemon committed
732
			omapi_value_dereference (&tv, MDL);
733 734 735

		omapi_message_unregister((omapi_object_t *)m);

736
		return ISC_R_SUCCESS;
737 738

	      case OMAPI_OP_DELETE:
Ted Lemon's avatar
Ted Lemon committed
739
		status = omapi_handle_lookup (&object, message -> h);
740 741
		if (status != ISC_R_SUCCESS) {
			return omapi_protocol_send_status
742
				(po, message -> id_object,
743 744 745 746
				 status, message -> id,
				 "no matching handle");
		}

747
		if (!object -> type -> remove)
748
			return omapi_protocol_send_status
749
				(po, message -> id_object,
750
				 ISC_R_NOTIMPLEMENTED, message -> id,
751
				 "no remove method for object");
752

753
		status = (*(object -> type -> remove)) (object,
754
							message -> id_object);
Ted Lemon's avatar
Ted Lemon committed
755
		omapi_object_dereference (&object, MDL);
756

757
		return omapi_protocol_send_status (po, message -> id_object,
758 759
						   status, message -> id,
						   (char *)0);
760 761 762
	}
	return ISC_R_NOTIMPLEMENTED;
}